2020-03-29 21:32:01 +08:00
|
|
|
package tester
|
|
|
|
|
|
|
|
import (
|
2020-06-29 14:12:27 +08:00
|
|
|
"encoding/json"
|
|
|
|
"path/filepath"
|
2020-03-29 21:32:01 +08:00
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/meshplus/bitxhub-kit/crypto"
|
2020-08-11 14:07:15 +08:00
|
|
|
"github.com/meshplus/bitxhub-kit/crypto/asym"
|
2020-04-28 17:08:21 +08:00
|
|
|
"github.com/meshplus/bitxhub-kit/types"
|
2020-03-29 21:32:01 +08:00
|
|
|
"github.com/meshplus/bitxhub-model/pb"
|
2020-04-28 17:08:21 +08:00
|
|
|
"github.com/meshplus/bitxhub/internal/constant"
|
|
|
|
"github.com/meshplus/bitxhub/internal/coreapi/api"
|
2020-03-29 21:32:01 +08:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RegisterAppchain struct {
|
|
|
|
suite.Suite
|
2020-04-28 17:08:21 +08:00
|
|
|
api api.CoreAPI
|
2020-03-29 21:32:01 +08:00
|
|
|
privKey crypto.PrivateKey
|
2020-10-21 19:26:41 +08:00
|
|
|
from *types.Address
|
2020-03-29 21:32:01 +08:00
|
|
|
}
|
|
|
|
|
2020-06-29 14:12:27 +08:00
|
|
|
type Appchain struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Validators string `json:"validators"`
|
|
|
|
ConsensusType int32 `json:"consensus_type"`
|
|
|
|
// 0 => registered, 1 => approved, -1 => rejected
|
|
|
|
Status int32 `json:"status"`
|
|
|
|
ChainType string `json:"chain_type"`
|
|
|
|
Desc string `json:"desc"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
PublicKey string `json:"public_key"`
|
|
|
|
}
|
|
|
|
|
2020-03-29 21:32:01 +08:00
|
|
|
func (suite *RegisterAppchain) SetupSuite() {
|
|
|
|
var err error
|
2020-08-11 14:07:15 +08:00
|
|
|
suite.privKey, err = asym.GenerateKeyPair(crypto.Secp256k1)
|
2020-04-28 17:08:21 +08:00
|
|
|
suite.Require().Nil(err)
|
2020-03-29 21:32:01 +08:00
|
|
|
|
2020-04-28 17:08:21 +08:00
|
|
|
suite.from, err = suite.privKey.PublicKey().Address()
|
|
|
|
suite.Require().Nil(err)
|
2020-03-29 21:32:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Appchain registers in bitxhub
|
|
|
|
func (suite *RegisterAppchain) TestRegisterAppchain() {
|
2020-04-28 17:08:21 +08:00
|
|
|
pub, err := suite.privKey.PublicKey().Bytes()
|
|
|
|
suite.Require().Nil(err)
|
|
|
|
|
2020-03-29 21:32:01 +08:00
|
|
|
args := []*pb.Arg{
|
2020-04-28 17:08:21 +08:00
|
|
|
pb.String(""),
|
|
|
|
pb.Int32(0),
|
|
|
|
pb.String("hyperchain"),
|
|
|
|
pb.String("税务链"),
|
|
|
|
pb.String("趣链税务链"),
|
|
|
|
pb.String("1.8"),
|
|
|
|
pb.String(string(pub)),
|
2020-03-29 21:32:01 +08:00
|
|
|
}
|
2020-04-28 17:08:21 +08:00
|
|
|
|
2020-09-29 15:01:30 +08:00
|
|
|
ret, err := invokeBVMContract(suite.api, suite.privKey, 1, constant.AppchainMgrContractAddr.Address(), "Register", args...)
|
2020-04-28 17:08:21 +08:00
|
|
|
suite.Require().Nil(err)
|
|
|
|
suite.Require().True(ret.IsSuccess(), string(ret.Ret))
|
|
|
|
suite.Require().Equal("hyperchain", gjson.Get(string(ret.Ret), "chain_type").String())
|
2020-03-29 21:32:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *RegisterAppchain) TestFetchAppchains() {
|
2020-08-11 14:07:15 +08:00
|
|
|
k1, err := asym.GenerateKeyPair(crypto.Secp256k1)
|
2020-04-28 17:08:21 +08:00
|
|
|
suite.Require().Nil(err)
|
2020-08-11 14:07:15 +08:00
|
|
|
k2, err := asym.GenerateKeyPair(crypto.Secp256k1)
|
2020-04-28 17:08:21 +08:00
|
|
|
suite.Require().Nil(err)
|
2020-09-29 15:01:30 +08:00
|
|
|
k1Nonce := uint64(1)
|
|
|
|
k2Nonce := uint64(1)
|
2020-04-28 17:08:21 +08:00
|
|
|
|
|
|
|
pub1, err := k1.PublicKey().Bytes()
|
|
|
|
suite.Require().Nil(err)
|
|
|
|
pub2, err := k2.PublicKey().Bytes()
|
|
|
|
suite.Require().Nil(err)
|
2020-03-29 21:32:01 +08:00
|
|
|
|
|
|
|
args := []*pb.Arg{
|
2020-04-28 17:08:21 +08:00
|
|
|
pb.String(""),
|
|
|
|
pb.Int32(0),
|
|
|
|
pb.String("hyperchain"),
|
|
|
|
pb.String("税务链"),
|
|
|
|
pb.String("趣链税务链"),
|
|
|
|
pb.String("1.8"),
|
|
|
|
pb.String(string(pub1)),
|
2020-03-29 21:32:01 +08:00
|
|
|
}
|
2020-09-29 15:01:30 +08:00
|
|
|
ret, err := invokeBVMContract(suite.api, k1, k1Nonce, constant.AppchainMgrContractAddr.Address(), "Register", args...)
|
2020-04-28 17:08:21 +08:00
|
|
|
suite.Require().Nil(err)
|
|
|
|
suite.Require().True(ret.IsSuccess(), string(ret.Ret))
|
2020-09-29 15:01:30 +08:00
|
|
|
k1Nonce++
|
2020-03-29 21:32:01 +08:00
|
|
|
|
2020-06-29 14:12:27 +08:00
|
|
|
appchain := Appchain{}
|
|
|
|
err = json.Unmarshal(ret.Ret, &appchain)
|
|
|
|
suite.Require().Nil(err)
|
|
|
|
id1 := appchain.ID
|
|
|
|
|
2020-03-29 21:32:01 +08:00
|
|
|
args = []*pb.Arg{
|
2020-04-28 17:08:21 +08:00
|
|
|
pb.String(""),
|
|
|
|
pb.Int32(0),
|
|
|
|
pb.String("fabric"),
|
|
|
|
pb.String("政务链"),
|
|
|
|
pb.String("fabric政务"),
|
|
|
|
pb.String("1.4"),
|
|
|
|
pb.String(string(pub2)),
|
2020-03-29 21:32:01 +08:00
|
|
|
}
|
2020-09-29 15:01:30 +08:00
|
|
|
ret, err = invokeBVMContract(suite.api, k2, k2Nonce, constant.AppchainMgrContractAddr.Address(), "Register", args...)
|
2020-04-28 17:08:21 +08:00
|
|
|
suite.Require().True(ret.IsSuccess(), string(ret.Ret))
|
|
|
|
suite.Require().Nil(err)
|
2020-09-29 15:01:30 +08:00
|
|
|
k2Nonce++
|
2020-04-28 17:08:21 +08:00
|
|
|
|
2020-09-29 15:01:30 +08:00
|
|
|
ret, err = invokeBVMContract(suite.api, k2, k2Nonce, constant.AppchainMgrContractAddr.Address(), "Appchains")
|
2020-04-28 17:08:21 +08:00
|
|
|
suite.Require().Nil(err)
|
|
|
|
suite.Require().True(ret.IsSuccess())
|
2020-09-29 15:01:30 +08:00
|
|
|
k2Nonce++
|
2020-03-29 21:32:01 +08:00
|
|
|
|
2020-09-29 15:01:30 +08:00
|
|
|
rec, err := invokeBVMContract(suite.api, k2, k2Nonce, constant.AppchainMgrContractAddr.Address(), "CountAppchains")
|
2020-04-28 17:08:21 +08:00
|
|
|
suite.Require().Nil(err)
|
|
|
|
suite.Require().True(ret.IsSuccess())
|
2020-03-29 21:32:01 +08:00
|
|
|
num, err := strconv.Atoi(string(rec.Ret))
|
2020-04-28 17:08:21 +08:00
|
|
|
suite.Require().Nil(err)
|
|
|
|
result := gjson.Parse(string(ret.Ret))
|
|
|
|
suite.Require().GreaterOrEqual(num, len(result.Array()))
|
2020-09-29 15:01:30 +08:00
|
|
|
k2Nonce++
|
2020-04-28 17:08:21 +08:00
|
|
|
|
2020-09-29 15:01:30 +08:00
|
|
|
ret, err = invokeBVMContract(suite.api, k2, k2Nonce, constant.AppchainMgrContractAddr.Address(), "CountApprovedAppchains")
|
2020-04-28 17:08:21 +08:00
|
|
|
suite.Require().Nil(err)
|
|
|
|
suite.Require().True(ret.IsSuccess())
|
|
|
|
num, err = strconv.Atoi(string(ret.Ret))
|
|
|
|
suite.Require().Nil(err)
|
|
|
|
suite.Require().EqualValues(0, num)
|
2020-09-29 15:01:30 +08:00
|
|
|
k2Nonce++
|
2020-06-29 14:12:27 +08:00
|
|
|
|
|
|
|
//FetchAuditRecords
|
2020-09-29 15:01:30 +08:00
|
|
|
ret, err = invokeBVMContract(suite.api, k1, k1Nonce, constant.AppchainMgrContractAddr.Address(), "FetchAuditRecords", pb.String(string(id1)))
|
2020-06-29 14:12:27 +08:00
|
|
|
suite.Require().Nil(err)
|
|
|
|
suite.Require().True(ret.IsSuccess())
|
2020-09-29 15:01:30 +08:00
|
|
|
k1Nonce++
|
2020-06-29 14:12:27 +08:00
|
|
|
|
|
|
|
//AppChain
|
2020-09-29 15:01:30 +08:00
|
|
|
ret, err = invokeBVMContract(suite.api, k1, k1Nonce, constant.AppchainMgrContractAddr.Address(), "Appchain")
|
2020-06-29 14:12:27 +08:00
|
|
|
suite.Require().Nil(err)
|
|
|
|
suite.Require().True(ret.IsSuccess())
|
2020-09-29 15:01:30 +08:00
|
|
|
k1Nonce++
|
2020-06-29 14:12:27 +08:00
|
|
|
|
|
|
|
//GetAppchain
|
2020-09-29 15:01:30 +08:00
|
|
|
ret2, err := invokeBVMContract(suite.api, k2, k2Nonce, constant.AppchainMgrContractAddr.Address(), "GetAppchain", pb.String(string(id1)))
|
2020-06-29 14:12:27 +08:00
|
|
|
suite.Require().Nil(err)
|
|
|
|
suite.Require().True(ret2.IsSuccess())
|
|
|
|
suite.Require().Equal(ret.Ret, ret2.Ret)
|
2020-09-29 15:01:30 +08:00
|
|
|
k2Nonce++
|
2020-06-29 14:12:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *RegisterAppchain) TestGetPubKeyByChainID() {
|
2020-08-11 14:07:15 +08:00
|
|
|
k1, err := asym.GenerateKeyPair(crypto.Secp256k1)
|
2020-06-29 14:12:27 +08:00
|
|
|
suite.Require().Nil(err)
|
2020-08-11 14:07:15 +08:00
|
|
|
k2, err := asym.GenerateKeyPair(crypto.Secp256k1)
|
2020-06-29 14:12:27 +08:00
|
|
|
suite.Require().Nil(err)
|
2020-09-29 15:01:30 +08:00
|
|
|
k1Nonce := uint64(1)
|
|
|
|
k2Nonce := uint64(1)
|
2020-06-29 14:12:27 +08:00
|
|
|
|
|
|
|
pub1, err := k1.PublicKey().Bytes()
|
|
|
|
suite.Require().Nil(err)
|
|
|
|
pub2, err := k2.PublicKey().Bytes()
|
|
|
|
suite.Require().Nil(err)
|
|
|
|
|
|
|
|
args := []*pb.Arg{
|
|
|
|
pb.String(""),
|
|
|
|
pb.Int32(0),
|
|
|
|
pb.String("hyperchain"),
|
|
|
|
pb.String("税务链"),
|
|
|
|
pb.String("趣链税务链"),
|
|
|
|
pb.String("1.8"),
|
|
|
|
pb.String(string(pub1)),
|
|
|
|
}
|
2020-09-29 15:01:30 +08:00
|
|
|
ret, err := invokeBVMContract(suite.api, k1, k1Nonce, constant.AppchainMgrContractAddr.Address(), "Register", args...)
|
2020-06-29 14:12:27 +08:00
|
|
|
suite.Require().Nil(err)
|
|
|
|
suite.Require().True(ret.IsSuccess(), string(ret.Ret))
|
2020-09-29 15:01:30 +08:00
|
|
|
k1Nonce++
|
2020-06-29 14:12:27 +08:00
|
|
|
|
|
|
|
args = []*pb.Arg{
|
|
|
|
pb.String(""),
|
|
|
|
pb.Int32(0),
|
|
|
|
pb.String("fabric"),
|
|
|
|
pb.String("政务链"),
|
|
|
|
pb.String("fabric政务"),
|
|
|
|
pb.String("1.4"),
|
|
|
|
pb.String(string(pub2)),
|
|
|
|
}
|
2020-09-29 15:01:30 +08:00
|
|
|
ret, err = invokeBVMContract(suite.api, k2, k2Nonce, constant.AppchainMgrContractAddr.Address(), "Register", args...)
|
2020-06-29 14:12:27 +08:00
|
|
|
suite.Require().True(ret.IsSuccess(), string(ret.Ret))
|
|
|
|
suite.Require().Nil(err)
|
2020-09-29 15:01:30 +08:00
|
|
|
k2Nonce++
|
2020-06-29 14:12:27 +08:00
|
|
|
|
|
|
|
appchain2 := Appchain{}
|
|
|
|
err = json.Unmarshal(ret.Ret, &appchain2)
|
|
|
|
suite.Require().Nil(err)
|
|
|
|
id2 := appchain2.ID
|
|
|
|
|
|
|
|
//GetPubKeyByChainID
|
2020-09-29 15:01:30 +08:00
|
|
|
ret, err = invokeBVMContract(suite.api, k1, k1Nonce, constant.AppchainMgrContractAddr.Address(), "GetPubKeyByChainID", pb.String(string(id2)))
|
2020-06-29 14:12:27 +08:00
|
|
|
suite.Require().Nil(err)
|
|
|
|
suite.Require().True(ret.IsSuccess())
|
|
|
|
suite.Require().Equal([]byte(appchain2.PublicKey), ret.Ret)
|
2020-09-29 15:01:30 +08:00
|
|
|
k1Nonce++
|
2020-06-29 14:12:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *RegisterAppchain) TestUpdateAppchains() {
|
2020-08-11 14:07:15 +08:00
|
|
|
k1, err := asym.GenerateKeyPair(crypto.Secp256k1)
|
2020-06-29 14:12:27 +08:00
|
|
|
suite.Require().Nil(err)
|
|
|
|
pub1, err := k1.PublicKey().Bytes()
|
|
|
|
suite.Require().Nil(err)
|
2020-09-29 15:01:30 +08:00
|
|
|
k1Nonce := uint64(1)
|
2020-06-29 14:12:27 +08:00
|
|
|
|
|
|
|
args := []*pb.Arg{
|
|
|
|
pb.String(""),
|
|
|
|
pb.Int32(0),
|
|
|
|
pb.String("hyperchain"),
|
|
|
|
pb.String("税务链"),
|
|
|
|
pb.String("趣链税务链"),
|
|
|
|
pb.String("1.8"),
|
|
|
|
pb.String(string(pub1)),
|
|
|
|
}
|
2020-09-29 15:01:30 +08:00
|
|
|
ret, err := invokeBVMContract(suite.api, k1, k1Nonce, constant.AppchainMgrContractAddr.Address(), "Register", args...)
|
2020-06-29 14:12:27 +08:00
|
|
|
suite.Require().Nil(err)
|
|
|
|
suite.Require().True(ret.IsSuccess(), string(ret.Ret))
|
2020-09-29 15:01:30 +08:00
|
|
|
k1Nonce++
|
2020-06-29 14:12:27 +08:00
|
|
|
|
|
|
|
appchain := Appchain{}
|
|
|
|
err = json.Unmarshal(ret.Ret, &appchain)
|
|
|
|
suite.Require().Nil(err)
|
|
|
|
id1 := appchain.ID
|
|
|
|
|
|
|
|
//Admin Chain
|
|
|
|
path := "./test_data/config/node1/key.json"
|
|
|
|
keyPath := filepath.Join(path)
|
2020-08-11 14:07:15 +08:00
|
|
|
priAdmin, err := asym.RestorePrivateKey(keyPath, "bitxhub")
|
2020-06-29 14:12:27 +08:00
|
|
|
suite.Require().Nil(err)
|
|
|
|
pubAdmin, err := priAdmin.PublicKey().Bytes()
|
|
|
|
suite.Require().Nil(err)
|
2020-09-29 15:01:30 +08:00
|
|
|
adminNonce := uint64(1)
|
2020-06-29 14:12:27 +08:00
|
|
|
|
|
|
|
args = []*pb.Arg{
|
|
|
|
pb.String(""),
|
|
|
|
pb.Int32(0),
|
|
|
|
pb.String("hyperchain"),
|
|
|
|
pb.String("管理链"),
|
|
|
|
pb.String("趣链管理链"),
|
|
|
|
pb.String("1.0"),
|
|
|
|
pb.String(string(pubAdmin)),
|
|
|
|
}
|
2020-09-29 15:01:30 +08:00
|
|
|
ret, err = invokeBVMContract(suite.api, priAdmin, adminNonce, constant.AppchainMgrContractAddr.Address(), "Register", args...)
|
2020-06-29 14:12:27 +08:00
|
|
|
suite.Require().Nil(err)
|
|
|
|
suite.Require().True(ret.IsSuccess(), string(ret.Ret))
|
2020-09-29 15:01:30 +08:00
|
|
|
adminNonce++
|
2020-06-29 14:12:27 +08:00
|
|
|
|
|
|
|
//Audit
|
2020-09-29 15:01:30 +08:00
|
|
|
ret, err = invokeBVMContract(suite.api, priAdmin, adminNonce, constant.AppchainMgrContractAddr.Address(), "Audit",
|
2020-06-29 14:12:27 +08:00
|
|
|
pb.String(string(id1)),
|
|
|
|
pb.Int32(1),
|
|
|
|
pb.String("通过"),
|
|
|
|
)
|
|
|
|
suite.Require().Nil(err)
|
|
|
|
suite.Require().True(ret.IsSuccess())
|
2020-09-29 15:01:30 +08:00
|
|
|
adminNonce++
|
2020-06-29 14:12:27 +08:00
|
|
|
|
|
|
|
//UpdateAppchain
|
|
|
|
args = []*pb.Arg{
|
|
|
|
pb.String(""),
|
|
|
|
pb.Int32(0),
|
|
|
|
pb.String("hyperchain"),
|
|
|
|
pb.String("税务链"),
|
|
|
|
pb.String("趣链税务链"),
|
|
|
|
pb.String("1.9"),
|
|
|
|
pb.String(string(pub1)),
|
|
|
|
}
|
2020-09-29 15:01:30 +08:00
|
|
|
ret, err = invokeBVMContract(suite.api, k1, k1Nonce, constant.AppchainMgrContractAddr.Address(), "UpdateAppchain", args...)
|
2020-06-29 14:12:27 +08:00
|
|
|
suite.Require().Nil(err)
|
|
|
|
suite.Require().True(ret.IsSuccess())
|
2020-09-29 15:01:30 +08:00
|
|
|
k1Nonce++
|
2020-03-29 21:32:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRegisterAppchain(t *testing.T) {
|
|
|
|
suite.Run(t, &RegisterAppchain{})
|
|
|
|
}
|