bitxhub/tester/case002_appchain_test.go

271 lines
7.9 KiB
Go
Raw Normal View History

2020-03-29 21:32:01 +08:00
package tester
import (
"encoding/json"
"path/filepath"
2020-03-29 21:32:01 +08:00
"strconv"
"testing"
"github.com/meshplus/bitxhub-kit/crypto"
"github.com/meshplus/bitxhub-kit/crypto/asym/ecdsa"
"github.com/meshplus/bitxhub-kit/key"
"github.com/meshplus/bitxhub-kit/types"
2020-03-29 21:32:01 +08:00
"github.com/meshplus/bitxhub-model/pb"
"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
api api.CoreAPI
2020-03-29 21:32:01 +08:00
privKey crypto.PrivateKey
from types.Address
2020-03-29 21:32:01 +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
suite.privKey, err = ecdsa.GenerateKey(ecdsa.Secp256r1)
suite.Require().Nil(err)
2020-03-29 21:32:01 +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() {
pub, err := suite.privKey.PublicKey().Bytes()
suite.Require().Nil(err)
2020-03-29 21:32:01 +08:00
args := []*pb.Arg{
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
}
ret, err := invokeBVMContract(suite.api, suite.privKey, constant.AppchainMgrContractAddr.Address(), "Register", args...)
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() {
k1, err := ecdsa.GenerateKey(ecdsa.Secp256r1)
suite.Require().Nil(err)
2020-03-29 21:32:01 +08:00
k2, err := ecdsa.GenerateKey(ecdsa.Secp256r1)
suite.Require().Nil(err)
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{
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
}
ret, err := invokeBVMContract(suite.api, k1, constant.AppchainMgrContractAddr.Address(), "Register", args...)
suite.Require().Nil(err)
suite.Require().True(ret.IsSuccess(), string(ret.Ret))
2020-03-29 21:32:01 +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{
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
}
ret, err = invokeBVMContract(suite.api, k2, constant.AppchainMgrContractAddr.Address(), "Register", args...)
suite.Require().True(ret.IsSuccess(), string(ret.Ret))
suite.Require().Nil(err)
ret, err = invokeBVMContract(suite.api, k2, constant.AppchainMgrContractAddr.Address(), "Appchains")
suite.Require().Nil(err)
suite.Require().True(ret.IsSuccess())
2020-03-29 21:32:01 +08:00
rec, err := invokeBVMContract(suite.api, k2, constant.AppchainMgrContractAddr.Address(), "CountAppchains")
suite.Require().Nil(err)
suite.Require().True(ret.IsSuccess())
2020-03-29 21:32:01 +08:00
num, err := strconv.Atoi(string(rec.Ret))
suite.Require().Nil(err)
result := gjson.Parse(string(ret.Ret))
suite.Require().GreaterOrEqual(num, len(result.Array()))
ret, err = invokeBVMContract(suite.api, k2, constant.AppchainMgrContractAddr.Address(), "CountApprovedAppchains")
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)
//FetchAuditRecords
ret, err = invokeBVMContract(suite.api, k1, constant.AppchainMgrContractAddr.Address(), "FetchAuditRecords", pb.String(string(id1)))
suite.Require().Nil(err)
suite.Require().True(ret.IsSuccess())
//AppChain
ret, err = invokeBVMContract(suite.api, k1, constant.AppchainMgrContractAddr.Address(), "Appchain")
suite.Require().Nil(err)
suite.Require().True(ret.IsSuccess())
//GetAppchain
ret2, err := invokeBVMContract(suite.api, k2, constant.AppchainMgrContractAddr.Address(), "GetAppchain", pb.String(string(id1)))
suite.Require().Nil(err)
suite.Require().True(ret2.IsSuccess())
suite.Require().Equal(ret.Ret, ret2.Ret)
}
func (suite *RegisterAppchain) TestGetPubKeyByChainID() {
k1, err := ecdsa.GenerateKey(ecdsa.Secp256r1)
suite.Require().Nil(err)
k2, err := ecdsa.GenerateKey(ecdsa.Secp256r1)
suite.Require().Nil(err)
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)),
}
ret, err := invokeBVMContract(suite.api, k1, constant.AppchainMgrContractAddr.Address(), "Register", args...)
suite.Require().Nil(err)
suite.Require().True(ret.IsSuccess(), string(ret.Ret))
args = []*pb.Arg{
pb.String(""),
pb.Int32(0),
pb.String("fabric"),
pb.String("政务链"),
pb.String("fabric政务"),
pb.String("1.4"),
pb.String(string(pub2)),
}
ret, err = invokeBVMContract(suite.api, k2, constant.AppchainMgrContractAddr.Address(), "Register", args...)
suite.Require().True(ret.IsSuccess(), string(ret.Ret))
suite.Require().Nil(err)
appchain2 := Appchain{}
err = json.Unmarshal(ret.Ret, &appchain2)
suite.Require().Nil(err)
id2 := appchain2.ID
//GetPubKeyByChainID
ret, err = invokeBVMContract(suite.api, k1, constant.AppchainMgrContractAddr.Address(), "GetPubKeyByChainID", pb.String(string(id2)))
suite.Require().Nil(err)
suite.Require().True(ret.IsSuccess())
suite.Require().Equal([]byte(appchain2.PublicKey), ret.Ret)
}
func (suite *RegisterAppchain) TestUpdateAppchains() {
k1, err := ecdsa.GenerateKey(ecdsa.Secp256r1)
suite.Require().Nil(err)
pub1, err := k1.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)),
}
ret, err := invokeBVMContract(suite.api, k1, constant.AppchainMgrContractAddr.Address(), "Register", args...)
suite.Require().Nil(err)
suite.Require().True(ret.IsSuccess(), string(ret.Ret))
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)
key, err := key.LoadKey(keyPath)
suite.Require().Nil(err)
priAdmin, err := key.GetPrivateKey("bitxhub")
suite.Require().Nil(err)
pubAdmin, err := priAdmin.PublicKey().Bytes()
suite.Require().Nil(err)
args = []*pb.Arg{
pb.String(""),
pb.Int32(0),
pb.String("hyperchain"),
pb.String("管理链"),
pb.String("趣链管理链"),
pb.String("1.0"),
pb.String(string(pubAdmin)),
}
ret, err = invokeBVMContract(suite.api, priAdmin, constant.AppchainMgrContractAddr.Address(), "Register", args...)
suite.Require().Nil(err)
suite.Require().True(ret.IsSuccess(), string(ret.Ret))
//Audit
ret, err = invokeBVMContract(suite.api, priAdmin, constant.AppchainMgrContractAddr.Address(), "Audit",
pb.String(string(id1)),
pb.Int32(1),
pb.String("通过"),
)
suite.Require().Nil(err)
suite.Require().True(ret.IsSuccess())
//UpdateAppchain
args = []*pb.Arg{
pb.String(""),
pb.Int32(0),
pb.String("hyperchain"),
pb.String("税务链"),
pb.String("趣链税务链"),
pb.String("1.9"),
pb.String(string(pub1)),
}
ret, err = invokeBVMContract(suite.api, k1, constant.AppchainMgrContractAddr.Address(), "UpdateAppchain", args...)
suite.Require().Nil(err)
suite.Require().True(ret.IsSuccess())
2020-03-29 21:32:01 +08:00
}
func TestRegisterAppchain(t *testing.T) {
suite.Run(t, &RegisterAppchain{})
}