Skip to content

Commit

Permalink
add evm support for ontology (#120)
Browse files Browse the repository at this point in the history
* modify for ont evm

* mod MakeTxParamWithSender

* fix bug

* fix
  • Loading branch information
zhiqiangxu authored Apr 6, 2022
1 parent f2c2a6b commit ea51f84
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 31 deletions.
29 changes: 29 additions & 0 deletions native/service/cross_chain_manager/common/param.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down
42 changes: 18 additions & 24 deletions native/service/cross_chain_manager/common/param_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
9 changes: 8 additions & 1 deletion native/service/cross_chain_manager/ont/ont_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
Expand Down
31 changes: 25 additions & 6 deletions native/service/cross_chain_manager/ont/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

}

0 comments on commit ea51f84

Please sign in to comment.