fix(*): fix integration test and contract address creation issue
This commit is contained in:
parent
a138baa674
commit
c043db63e2
|
@ -1,12 +1,13 @@
|
|||
package wasm
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
"github.com/meshplus/bitxhub-core/wasm"
|
||||
"github.com/meshplus/bitxhub-kit/types"
|
||||
"github.com/meshplus/bitxhub/pkg/vm"
|
||||
|
@ -43,7 +44,7 @@ func New(ctx *vm.Context, imports *wasmer.Imports, instances map[string]wasmer.I
|
|||
ctx: ctx,
|
||||
}
|
||||
|
||||
if *ctx.Callee == (types.Address{}) {
|
||||
if ctx.Callee == nil || bytes.Equal(ctx.Callee.Bytes(), (&types.Address{}).Bytes()) {
|
||||
return wasmVM, nil
|
||||
}
|
||||
|
||||
|
@ -70,7 +71,7 @@ func EmptyImports() (*wasmer.Imports, error) {
|
|||
|
||||
// Run let the wasm vm excute or deploy the smart contract which depends on whether the callee is empty
|
||||
func (w *WasmVM) Run(input []byte) (ret []byte, err error) {
|
||||
if *w.ctx.Callee == (types.Address{}) {
|
||||
if w.ctx.Callee == nil || bytes.Equal(w.ctx.Callee.Bytes(), (&types.Address{}).Bytes()) {
|
||||
return w.deploy()
|
||||
}
|
||||
|
||||
|
@ -100,7 +101,12 @@ func (w *WasmVM) deploy() ([]byte, error) {
|
|||
}
|
||||
|
||||
func createAddress(b *types.Address, nonce uint64) *types.Address {
|
||||
data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})
|
||||
var data []byte
|
||||
nonceBytes := make([]byte, 8)
|
||||
|
||||
binary.LittleEndian.PutUint64(nonceBytes, nonce)
|
||||
data = append(data, b.Bytes()...)
|
||||
data = append(data, nonceBytes...)
|
||||
hashBytes := sha256.Sum256(data)
|
||||
|
||||
return types.NewAddress(hashBytes[12:])
|
||||
|
|
|
@ -82,11 +82,8 @@ func (suite *Interchain) TestHandleIBTP() {
|
|||
|
||||
proof := []byte("true")
|
||||
proofHash := sha256.Sum256(proof)
|
||||
ib := &pb.IBTP{From: f.String(), To: t.String(), Index: 1, Timestamp: time.Now().UnixNano(), Proof: proofHash[:]}
|
||||
data, err := ib.Marshal()
|
||||
suite.Require().Nil(err)
|
||||
|
||||
tx, err := genBVMContractTransaction(k1, ibtpNonce, constant.InterchainContractAddr.Address(), "HandleIBTP", pb.Bytes(data))
|
||||
ib := &pb.IBTP{From: f.String(), To: t.String(), Index: ibtpNonce, Timestamp: time.Now().UnixNano(), Proof: proofHash[:]}
|
||||
tx, err := genIBTPTransaction(k1, ib)
|
||||
suite.Require().Nil(err)
|
||||
|
||||
tx.Extra = proof
|
||||
|
@ -158,11 +155,8 @@ func (suite *Interchain) TestGetIBTPByID() {
|
|||
suite.Require().Nil(err)
|
||||
|
||||
proofHash := sha256.Sum256(proof)
|
||||
ib := &pb.IBTP{From: f.String(), To: t.String(), Index: 1, Payload: []byte("111"), Timestamp: time.Now().UnixNano(), Proof: proofHash[:]}
|
||||
data, err := ib.Marshal()
|
||||
suite.Require().Nil(err)
|
||||
|
||||
tx, err := genBVMContractTransaction(k1, ibtpNonce, constant.InterchainContractAddr.Address(), "HandleIBTP", pb.Bytes(data))
|
||||
ib := &pb.IBTP{From: f.String(), To: t.String(), Index: ibtpNonce, Payload: []byte("111"), Timestamp: time.Now().UnixNano(), Proof: proofHash[:]}
|
||||
tx, err := genIBTPTransaction(k1, ib)
|
||||
suite.Require().Nil(err)
|
||||
tx.Extra = proof
|
||||
receipt, err := sendTransactionWithReceipt(suite.api, tx)
|
||||
|
@ -170,11 +164,8 @@ func (suite *Interchain) TestGetIBTPByID() {
|
|||
suite.Require().EqualValues(true, receipt.IsSuccess(), string(receipt.Ret))
|
||||
ibtpNonce++
|
||||
|
||||
ib.Index = 2
|
||||
data, err = ib.Marshal()
|
||||
suite.Require().Nil(err)
|
||||
|
||||
tx, err = genBVMContractTransaction(k1, ibtpNonce, constant.InterchainContractAddr.Address(), "HandleIBTP", pb.Bytes(data))
|
||||
ib2 := &pb.IBTP{From: f.String(), To: t.String(), Index: ibtpNonce, Payload: []byte("111"), Timestamp: time.Now().UnixNano(), Proof: proofHash[:]}
|
||||
tx, err = genIBTPTransaction(k1, ib2)
|
||||
suite.Require().Nil(err)
|
||||
tx.Extra = proof
|
||||
receipt, err = sendTransactionWithReceipt(suite.api, tx)
|
||||
|
@ -182,11 +173,8 @@ func (suite *Interchain) TestGetIBTPByID() {
|
|||
suite.Require().EqualValues(true, receipt.IsSuccess(), string(receipt.Ret))
|
||||
ibtpNonce++
|
||||
|
||||
ib.Index = 3
|
||||
data, err = ib.Marshal()
|
||||
suite.Assert().Nil(err)
|
||||
|
||||
tx, err = genBVMContractTransaction(k1, ibtpNonce, constant.InterchainContractAddr.Address(), "HandleIBTP", pb.Bytes(data))
|
||||
ib3 := &pb.IBTP{From: f.String(), To: t.String(), Index: ibtpNonce, Payload: []byte("111"), Timestamp: time.Now().UnixNano(), Proof: proofHash[:]}
|
||||
tx, err = genIBTPTransaction(k1, ib3)
|
||||
suite.Require().Nil(err)
|
||||
tx.Extra = proof
|
||||
receipt, err = sendTransactionWithReceipt(suite.api, tx)
|
||||
|
|
|
@ -6,11 +6,11 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/meshplus/bitxhub/internal/coreapi/api"
|
||||
|
||||
"github.com/meshplus/bitxhub-kit/crypto"
|
||||
"github.com/meshplus/bitxhub-kit/types"
|
||||
"github.com/meshplus/bitxhub-model/pb"
|
||||
"github.com/meshplus/bitxhub/internal/constant"
|
||||
"github.com/meshplus/bitxhub/internal/coreapi/api"
|
||||
)
|
||||
|
||||
func genBVMContractTransaction(privateKey crypto.PrivateKey, nonce uint64, address *types.Address, method string, args ...*pb.Arg) (*pb.Transaction, error) {
|
||||
|
@ -21,6 +21,56 @@ func genXVMContractTransaction(privateKey crypto.PrivateKey, nonce uint64, addre
|
|||
return genContractTransaction(pb.TransactionData_XVM, privateKey, nonce, address, method, args...)
|
||||
}
|
||||
|
||||
func genIBTPTransaction(privateKey crypto.PrivateKey, ibtp *pb.IBTP) (*pb.Transaction, error) {
|
||||
from, err := privateKey.PublicKey().Address()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ibtpd, err := ibtp.Marshal()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pl := &pb.InvokePayload{
|
||||
Method: "HandleIBTP",
|
||||
Args: []*pb.Arg{pb.Bytes(ibtpd)},
|
||||
}
|
||||
|
||||
data, err := pl.Marshal()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
td := &pb.TransactionData{
|
||||
Type: pb.TransactionData_INVOKE,
|
||||
VmType: pb.TransactionData_BVM,
|
||||
Payload: data,
|
||||
}
|
||||
|
||||
payload, err := td.Marshal()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tx := &pb.Transaction{
|
||||
From: from,
|
||||
To: constant.InterchainContractAddr.Address(),
|
||||
Payload: payload,
|
||||
Timestamp: time.Now().UnixNano(),
|
||||
Nonce: ibtp.Index,
|
||||
IBTP: ibtp,
|
||||
}
|
||||
|
||||
if err := tx.Sign(privateKey); err != nil {
|
||||
return nil, fmt.Errorf("tx sign: %w", err)
|
||||
}
|
||||
|
||||
tx.TransactionHash = tx.Hash()
|
||||
|
||||
return tx, nil
|
||||
}
|
||||
|
||||
func invokeBVMContract(api api.CoreAPI, privateKey crypto.PrivateKey, nonce uint64, address *types.Address, method string, args ...*pb.Arg) (*pb.Receipt, error) {
|
||||
tx, err := genBVMContractTransaction(privateKey, nonce, address, method, args...)
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in New Issue