diff --git a/native/service/cross_chain_manager/common/param.go b/native/service/cross_chain_manager/common/param.go index 2f5f3389..9cd2b0eb 100644 --- a/native/service/cross_chain_manager/common/param.go +++ b/native/service/cross_chain_manager/common/param.go @@ -18,6 +18,8 @@ package common import ( "fmt" + + ethcommon "github.com/ethereum/go-ethereum/common" "github.com/polynetwork/poly/common" "github.com/polynetwork/poly/native" ) @@ -116,6 +118,33 @@ func (this *EntranceParam) Deserialization(source *common.ZeroCopySource) error return nil } +type MakeTxParamWithSender struct { + Sender ethcommon.Address + MakeTxParam +} + +// this method is only used in test +func (this *MakeTxParamWithSender) Serialization() (data []byte, err error) { + sink := common.NewZeroCopySink(nil) + sink.WriteAddress(common.Address(this.Sender)) + this.MakeTxParam.Serialization(sink) + data = sink.Bytes() + return +} + +func (this *MakeTxParamWithSender) Deserialization(data []byte) (err error) { + + source := common.NewZeroCopySource(data) + + addr, eof := source.NextAddress() + if eof { + err = fmt.Errorf("MakeTxParamWithSender NextAddress fail") + return + } + this.Sender = ethcommon.Address(addr) + return this.MakeTxParam.Deserialization(source) +} + type MakeTxParam struct { TxHash []byte CrossChainID []byte diff --git a/native/service/cross_chain_manager/common/param_test.go b/native/service/cross_chain_manager/common/param_test.go index 83d234a6..ba7baf66 100644 --- a/native/service/cross_chain_manager/common/param_test.go +++ b/native/service/cross_chain_manager/common/param_test.go @@ -17,35 +17,29 @@ package common import ( - "github.com/polynetwork/poly/common" - "github.com/stretchr/testify/assert" "testing" + + ethcommon "github.com/ethereum/go-ethereum/common" + "github.com/stretchr/testify/assert" ) -func TestVoteParam(t *testing.T) { - param := VoteParam{ - Address: "1234", - TxHash: []byte{1, 2, 3}, - } +func TestMakeTxParamWithSender(t *testing.T) { + txParam := MakeTxParam{TxHash: []byte("hash"), + CrossChainID: []byte("1"), + FromContractAddress: []byte("from addr"), + ToChainID: 1, + ToContractAddress: []byte("to addr"), + Method: "test", + Args: []byte("args")} - sink := common.NewZeroCopySink(nil) - param.Serialization(sink) + value := MakeTxParamWithSender{Sender: ethcommon.HexToAddress("abc"), MakeTxParam: txParam} + data, err := value.Serialization() - var p VoteParam - err := p.Deserialization(common.NewZeroCopySource(sink.Bytes())) - assert.NoError(t, err) -} + assert.Nil(t, err) -func TestVote(t *testing.T) { - m := make(map[string]string, 0) - m["123"] = "123" - vote := Vote{ - VoteMap: m, - } - sink := common.NewZeroCopySink(nil) - vote.Serialization(sink) + var decoded MakeTxParamWithSender + err = decoded.Deserialization(data) + assert.Nil(t, err) - var v Vote - err := v.Deserialization(common.NewZeroCopySource(sink.Bytes())) - assert.NoError(t, err) + assert.Equal(t, value, decoded) } diff --git a/native/service/cross_chain_manager/ont/ont_handler.go b/native/service/cross_chain_manager/ont/ont_handler.go index ad5c6d45..017e4a6f 100644 --- a/native/service/cross_chain_manager/ont/ont_handler.go +++ b/native/service/cross_chain_manager/ont/ont_handler.go @@ -26,6 +26,7 @@ import ( "github.com/polynetwork/poly/common" "github.com/polynetwork/poly/native" scom "github.com/polynetwork/poly/native/service/cross_chain_manager/common" + "github.com/polynetwork/poly/native/service/governance/side_chain_manager" "github.com/polynetwork/poly/native/service/header_sync/ont" ) @@ -76,7 +77,13 @@ func (this *ONTHandler) MakeDepositProposal(service *native.NativeService) (*sco } } - value, err := VerifyFromOntTx(params.Proof, crossChainMsg) + //get registered side chain information from poly chain + sideChain, err := side_chain_manager.GetSideChain(service, params.SourceChainID) + if err != nil { + return nil, fmt.Errorf("ont MakeDepositProposal, side_chain_manager.GetSideChain error: %v", err) + } + + value, err := VerifyFromOntTx(params.Proof, crossChainMsg, sideChain) if err != nil { return nil, fmt.Errorf("ont MakeDepositProposal, VerifyOntTx error: %v", err) } diff --git a/native/service/cross_chain_manager/ont/utils.go b/native/service/cross_chain_manager/ont/utils.go index 02613dd5..54bf225c 100644 --- a/native/service/cross_chain_manager/ont/utils.go +++ b/native/service/cross_chain_manager/ont/utils.go @@ -18,23 +18,42 @@ package ont import ( + "bytes" "fmt" + otypes "github.com/ontio/ontology/core/types" "github.com/polynetwork/poly/common" "github.com/polynetwork/poly/merkle" scom "github.com/polynetwork/poly/native/service/cross_chain_manager/common" + "github.com/polynetwork/poly/native/service/governance/side_chain_manager" ) -func VerifyFromOntTx(proof []byte, crossChainMsg *otypes.CrossChainMsg) (*scom.MakeTxParam, error) { +func VerifyFromOntTx(proof []byte, crossChainMsg *otypes.CrossChainMsg, sideChain *side_chain_manager.SideChain) (*scom.MakeTxParam, error) { v, err := merkle.MerkleProve(proof, crossChainMsg.StatesRoot.ToArray()) if err != nil { return nil, fmt.Errorf("VerifyFromOntTx, merkle.MerkleProve verify merkle proof error") } - s := common.NewZeroCopySource(v) - txParam := new(scom.MakeTxParam) - if err := txParam.Deserialization(s); err != nil { - return nil, fmt.Errorf("VerifyFromOntTx, deserialize merkleValue error:%s", err) + if len(sideChain.CCMCAddress) == 0 { + // old sideChain for ontology + s := common.NewZeroCopySource(v) + txParam := new(scom.MakeTxParam) + if err := txParam.Deserialization(s); err != nil { + return nil, fmt.Errorf("VerifyFromOntTx, deserialize MakeTxParam error:%s", err) + } + return txParam, nil + } + + // new sideChain for ontology + txParam := new(scom.MakeTxParamWithSender) + if err := txParam.Deserialization(v); err != nil { + return nil, fmt.Errorf("VerifyFromOntTx, deserialize MakeTxParamWithSender error:%s", err) } - return txParam, nil + + if !bytes.Equal(txParam.Sender[:], sideChain.CCMCAddress) { + return nil, fmt.Errorf("VerifyFromOntTx, invalid sender:%s", err) + } + + return &txParam.MakeTxParam, nil + }