2020-03-29 21:32:01 +08:00
|
|
|
package tester
|
|
|
|
|
|
|
|
import (
|
2020-07-02 16:03:53 +08:00
|
|
|
"encoding/json"
|
2020-03-29 21:32:01 +08:00
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/meshplus/bitxhub-kit/crypto/asym/ecdsa"
|
|
|
|
"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"
|
2020-07-02 16:04:45 +08:00
|
|
|
"github.com/tidwall/gjson"
|
2020-03-29 21:32:01 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Interchain struct {
|
|
|
|
suite.Suite
|
2020-04-28 17:08:21 +08:00
|
|
|
api api.CoreAPI
|
2020-03-29 21:32:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *Interchain) SetupSuite() {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *Interchain) TestHandleIBTP() {
|
|
|
|
k1, err := ecdsa.GenerateKey(ecdsa.Secp256r1)
|
2020-04-28 17:08:21 +08:00
|
|
|
suite.Require().Nil(err)
|
2020-03-29 21:32:01 +08:00
|
|
|
k2, err := ecdsa.GenerateKey(ecdsa.Secp256r1)
|
2020-04-28 17:08:21 +08:00
|
|
|
suite.Require().Nil(err)
|
2020-03-29 21:32:01 +08:00
|
|
|
f, err := k1.PublicKey().Address()
|
|
|
|
suite.Require().Nil(err)
|
|
|
|
t, err := k2.PublicKey().Address()
|
|
|
|
suite.Require().Nil(err)
|
|
|
|
|
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
|
|
|
|
2020-05-07 14:18:19 +08:00
|
|
|
ret, err := invokeBVMContract(suite.api, k1, constant.AppchainMgrContractAddr.Address(), "Register",
|
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-04-28 17:08:21 +08:00
|
|
|
suite.Require().Nil(err)
|
|
|
|
suite.Require().True(ret.IsSuccess(), string(ret.Ret))
|
|
|
|
|
2020-05-07 14:18:19 +08:00
|
|
|
ret, err = invokeBVMContract(suite.api, k2, constant.AppchainMgrContractAddr.Address(), "Register",
|
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-04-28 17:08:21 +08:00
|
|
|
suite.Require().Nil(err)
|
|
|
|
suite.Require().True(ret.IsSuccess())
|
|
|
|
|
|
|
|
// deploy rule
|
|
|
|
bytes, err := ioutil.ReadFile("./test_data/hpc_rule.wasm")
|
|
|
|
suite.Require().Nil(err)
|
|
|
|
addr, err := deployContract(suite.api, k1, bytes)
|
|
|
|
suite.Require().Nil(err)
|
2020-03-29 21:32:01 +08:00
|
|
|
|
|
|
|
// register rule
|
2020-04-28 17:08:21 +08:00
|
|
|
ret, err = invokeBVMContract(suite.api, k1, constant.RuleManagerContractAddr.Address(), "RegisterRule", pb.String(f.Hex()), pb.String(addr.Hex()))
|
|
|
|
suite.Require().Nil(err)
|
|
|
|
suite.Require().True(ret.IsSuccess())
|
2020-03-29 21:32:01 +08:00
|
|
|
|
|
|
|
ib := &pb.IBTP{From: f.Hex(), To: t.Hex(), Index: 1, Timestamp: time.Now().UnixNano()}
|
|
|
|
data, err := ib.Marshal()
|
|
|
|
suite.Require().Nil(err)
|
2020-04-28 17:08:21 +08:00
|
|
|
|
|
|
|
ret, err = invokeBVMContract(suite.api, k1, constant.InterchainContractAddr.Address(), "HandleIBTP", pb.Bytes(data))
|
2020-03-29 21:32:01 +08:00
|
|
|
suite.Require().Nil(err)
|
2020-04-28 17:08:21 +08:00
|
|
|
suite.Require().True(ret.IsSuccess(), string(ret.Ret))
|
2020-03-29 21:32:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *Interchain) TestGetIBTPByID() {
|
|
|
|
k1, err := ecdsa.GenerateKey(ecdsa.Secp256r1)
|
2020-04-28 17:08:21 +08:00
|
|
|
suite.Require().Nil(err)
|
2020-03-29 21:32:01 +08:00
|
|
|
k2, err := ecdsa.GenerateKey(ecdsa.Secp256r1)
|
2020-04-28 17:08:21 +08:00
|
|
|
suite.Require().Nil(err)
|
2020-03-29 21:32:01 +08:00
|
|
|
f, err := k1.PublicKey().Address()
|
|
|
|
suite.Require().Nil(err)
|
|
|
|
t, err := k2.PublicKey().Address()
|
|
|
|
suite.Require().Nil(err)
|
|
|
|
|
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
|
|
|
|
|
|
|
confByte, err := ioutil.ReadFile("./test_data/validator")
|
2020-04-28 17:08:21 +08:00
|
|
|
suite.Require().Nil(err)
|
|
|
|
|
2020-05-07 14:18:19 +08:00
|
|
|
ret, err := invokeBVMContract(suite.api, k1, constant.AppchainMgrContractAddr.Address(), "Register",
|
2020-04-28 17:08:21 +08:00
|
|
|
pb.String(string(confByte)),
|
|
|
|
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-04-28 17:08:21 +08:00
|
|
|
suite.Require().Nil(err)
|
|
|
|
suite.Require().True(ret.IsSuccess(), string(ret.Ret))
|
|
|
|
|
2020-05-07 14:18:19 +08:00
|
|
|
ret, err = invokeBVMContract(suite.api, k2, constant.AppchainMgrContractAddr.Address(), "Register",
|
2020-04-28 17:08:21 +08:00
|
|
|
pb.String(""),
|
|
|
|
pb.Int32(0),
|
|
|
|
pb.String("fabric"),
|
|
|
|
pb.String("税务链"),
|
|
|
|
pb.String("fabric税务链"),
|
|
|
|
pb.String("1.8"),
|
|
|
|
pb.String(string(pub2)),
|
2020-03-29 21:32:01 +08:00
|
|
|
)
|
2020-04-28 17:08:21 +08:00
|
|
|
suite.Require().Nil(err)
|
|
|
|
suite.Require().True(ret.IsSuccess(), string(ret.Ret))
|
2020-03-29 21:32:01 +08:00
|
|
|
|
|
|
|
contractByte, err := ioutil.ReadFile("./test_data/fabric_policy.wasm")
|
2020-04-28 17:08:21 +08:00
|
|
|
suite.Require().Nil(err)
|
|
|
|
addr, err := deployContract(suite.api, k1, contractByte)
|
|
|
|
suite.Require().Nil(err)
|
|
|
|
|
2020-03-29 21:32:01 +08:00
|
|
|
// register rule
|
2020-04-28 17:08:21 +08:00
|
|
|
_, err = invokeBVMContract(suite.api, k1, constant.RuleManagerContractAddr.Address(), "RegisterRule", pb.String(f.Hex()), pb.String(addr.Hex()))
|
|
|
|
suite.Require().Nil(err)
|
2020-03-29 21:32:01 +08:00
|
|
|
|
|
|
|
proof, err := ioutil.ReadFile("./test_data/proof")
|
2020-04-28 17:08:21 +08:00
|
|
|
suite.Require().Nil(err)
|
2020-04-21 19:50:56 +08:00
|
|
|
ib := &pb.IBTP{From: f.Hex(), To: t.Hex(), Index: 1, Payload: []byte("111"), Timestamp: time.Now().UnixNano(), Proof: proof}
|
2020-03-29 21:32:01 +08:00
|
|
|
data, err := ib.Marshal()
|
2020-04-28 17:08:21 +08:00
|
|
|
suite.Require().Nil(err)
|
|
|
|
_, err = invokeBVMContract(suite.api, k1, constant.InterchainContractAddr.Address(), "HandleIBTP", pb.Bytes(data))
|
|
|
|
suite.Require().Nil(err)
|
2020-03-29 21:32:01 +08:00
|
|
|
|
|
|
|
ib.Index = 2
|
|
|
|
data, err = ib.Marshal()
|
2020-04-28 17:08:21 +08:00
|
|
|
suite.Require().Nil(err)
|
|
|
|
receipt, err := invokeBVMContract(suite.api, k1, constant.InterchainContractAddr.Address(), "HandleIBTP", pb.Bytes(data))
|
|
|
|
suite.Require().Nil(err)
|
|
|
|
suite.Require().EqualValues(true, receipt.IsSuccess(), string(receipt.Ret))
|
2020-03-29 21:32:01 +08:00
|
|
|
|
|
|
|
ib.Index = 3
|
|
|
|
data, err = ib.Marshal()
|
|
|
|
suite.Assert().Nil(err)
|
2020-04-28 17:08:21 +08:00
|
|
|
_, err = invokeBVMContract(suite.api, k1, constant.InterchainContractAddr.Address(), "HandleIBTP", pb.Bytes(data))
|
2020-03-29 21:32:01 +08:00
|
|
|
suite.Assert().Nil(err)
|
|
|
|
|
|
|
|
ib.Index = 2
|
2020-04-28 17:08:21 +08:00
|
|
|
ret, err = invokeBVMContract(suite.api, k1, constant.InterchainContractAddr.Address(), "GetIBTPByID", pb.String(ib.ID()))
|
2020-03-29 21:32:01 +08:00
|
|
|
suite.Assert().Nil(err)
|
2020-04-16 00:37:53 +08:00
|
|
|
suite.Assert().Equal(true, ret.IsSuccess(), string(ret.Ret))
|
2020-03-29 21:32:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *Interchain) TestAudit() {
|
|
|
|
k, err := ecdsa.GenerateKey(ecdsa.Secp256r1)
|
|
|
|
suite.Require().Nil(err)
|
|
|
|
|
2020-05-07 14:18:19 +08:00
|
|
|
ret, err := invokeBVMContract(suite.api, k, constant.AppchainMgrContractAddr.Address(), "Audit",
|
2020-04-28 17:08:21 +08:00
|
|
|
pb.String("0x123"),
|
|
|
|
pb.Int32(1),
|
|
|
|
pb.String("通过"),
|
2020-03-29 21:32:01 +08:00
|
|
|
)
|
|
|
|
suite.Require().Nil(err)
|
|
|
|
suite.Contains(string(ret.Ret), "caller is not an admin account")
|
|
|
|
}
|
|
|
|
|
2020-07-02 16:03:53 +08:00
|
|
|
func (suite *Interchain) TestInterchain() {
|
|
|
|
k1, err := ecdsa.GenerateKey(ecdsa.Secp256r1)
|
|
|
|
suite.Require().Nil(err)
|
|
|
|
|
|
|
|
pub1, err := k1.PublicKey().Bytes()
|
|
|
|
suite.Require().Nil(err)
|
|
|
|
|
|
|
|
ret, err := invokeBVMContract(suite.api, k1, constant.AppchainMgrContractAddr.Address(), "Register",
|
|
|
|
pb.String(""),
|
|
|
|
pb.Int32(0),
|
|
|
|
pb.String("hyperchain"),
|
|
|
|
pb.String("婚姻链"),
|
|
|
|
pb.String("趣链婚姻链"),
|
|
|
|
pb.String("1.8"),
|
|
|
|
pb.String(string(pub1)),
|
|
|
|
)
|
|
|
|
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
|
|
|
|
|
|
|
|
ret, err = invokeBVMContract(suite.api, k1, constant.InterchainContractAddr.Address(), "Interchain")
|
|
|
|
suite.Require().Nil(err)
|
|
|
|
suite.Require().True(ret.IsSuccess(), string(ret.Ret))
|
|
|
|
suite.Require().Equal(id1, gjson.Get(string(ret.Ret), "id").String())
|
|
|
|
suite.Require().Equal("", gjson.Get(string(ret.Ret), "interchain_counter").String())
|
|
|
|
suite.Require().Equal("", gjson.Get(string(ret.Ret), "receipt_counter").String())
|
|
|
|
suite.Require().Equal("", gjson.Get(string(ret.Ret), "source_receipt_counter").String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *Interchain) TestRegister() {
|
|
|
|
k1, err := ecdsa.GenerateKey(ecdsa.Secp256r1)
|
|
|
|
suite.Require().Nil(err)
|
|
|
|
|
|
|
|
ret, err := invokeBVMContract(suite.api, k1, constant.InterchainContractAddr.Address(), "Register")
|
|
|
|
suite.Require().Nil(err)
|
|
|
|
suite.Require().True(ret.IsSuccess(), string(ret.Ret))
|
|
|
|
}
|
|
|
|
|
2020-03-29 21:32:01 +08:00
|
|
|
func TestInterchain(t *testing.T) {
|
|
|
|
suite.Run(t, &Interchain{})
|
|
|
|
}
|