Skip to content

Commit

Permalink
fix: zilliqa genesis ds block issue (#81)
Browse files Browse the repository at this point in the history
* fix: zilliqa genesis ds block issue

* fix: rename zilliqa to zilliqalegacy

* fix: udpate zilliqa legacy sdk version

* feat: add zilliqa chain

* fix: some doc

* fix: use legacy version for zilliqa handler

* put todo

* fix(zilliqa): use a newer sdk version

* feat: use 17 for zilliqa sidechain id

* feat: add zilliqa rounter

* dep(zilliqa): use latest version

* test(zilliqa): sync header test
  • Loading branch information
renlulu authored Sep 27, 2021
1 parent e12c3fb commit 48fe538
Show file tree
Hide file tree
Showing 12 changed files with 1,649 additions and 91 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/polynetwork/poly
go 1.14

require (
github.com/Zilliqa/gozilliqa-sdk v1.2.1-0.20210329093354-1b8e0a7a2e25
github.com/Zilliqa/gozilliqa-sdk v1.2.1-0.20210927032600-4c733f2cb879
github.com/btcsuite/btcd v0.20.1-beta
github.com/btcsuite/btcutil v1.0.2
github.com/cosmos/cosmos-sdk v0.39.1
Expand All @@ -24,6 +24,7 @@ require (
github.com/pborman/uuid v1.2.0
github.com/pkg/errors v0.9.1
github.com/polynetwork/poly-io-test v0.0.0-20200819093740-8cf514b07750
github.com/renlulu/gozilliqa-sdklegacy v0.0.0-20210926114807-88a08c5ab803
github.com/stretchr/testify v1.7.0
github.com/syndtr/goleveldb v1.0.1-0.20190923125748-758128399b1d
github.com/tendermint/go-amino v0.15.1
Expand Down
16 changes: 10 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/
github.com/Workiva/go-datastructures v1.0.50/go.mod h1:Z+F2Rca0qCsVYDS8z7bAGm8f3UkzuWYS/oBZz5a7VVA=
github.com/Workiva/go-datastructures v1.0.52 h1:PLSK6pwn8mYdaoaCZEMsXBpBotr4HHn9abU0yMQt0NI=
github.com/Workiva/go-datastructures v1.0.52/go.mod h1:Z+F2Rca0qCsVYDS8z7bAGm8f3UkzuWYS/oBZz5a7VVA=
github.com/Zilliqa/gozilliqa-sdk v1.2.1-0.20210329093354-1b8e0a7a2e25 h1:DFzNXEpvnU8Wdo2+51OptoHY/WJQenrhJFslbQydRR0=
github.com/Zilliqa/gozilliqa-sdk v1.2.1-0.20210329093354-1b8e0a7a2e25/go.mod h1:XLd05IRvH+nQt2lLvW6I2pfWBtRYE4i8Tpx45xBrlUE=
github.com/Zilliqa/gozilliqa-sdk v1.2.1-0.20210926082820-a45f577f4d32 h1:DBZLPtpq6jM+xl5lqM8SxBd47gATsW61gwVH57ohk+0=
github.com/Zilliqa/gozilliqa-sdk v1.2.1-0.20210926082820-a45f577f4d32/go.mod h1:XLd05IRvH+nQt2lLvW6I2pfWBtRYE4i8Tpx45xBrlUE=
github.com/Zilliqa/gozilliqa-sdk v1.2.1-0.20210927003807-36e2b6dbcb7a h1:mkD6iNAC2ZCvKHEjHJF8+dNU1Ng4G+GRGRIVoqqabX4=
github.com/Zilliqa/gozilliqa-sdk v1.2.1-0.20210927003807-36e2b6dbcb7a/go.mod h1:XLd05IRvH+nQt2lLvW6I2pfWBtRYE4i8Tpx45xBrlUE=
github.com/Zilliqa/gozilliqa-sdk v1.2.1-0.20210927032600-4c733f2cb879 h1:/OXqInjRm8CEd81EEFCGJ5Q24TTlY1/qu6DQ0D91WPA=
github.com/Zilliqa/gozilliqa-sdk v1.2.1-0.20210927032600-4c733f2cb879/go.mod h1:XLd05IRvH+nQt2lLvW6I2pfWBtRYE4i8Tpx45xBrlUE=
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
Expand Down Expand Up @@ -273,7 +277,6 @@ github.com/gorilla/websocket v1.4.1-0.20190629185528-ae1634f6a989/go.mod h1:E7qH
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gosuri/uilive v0.0.3 h1:kvo6aB3pez9Wbudij8srWo4iY6SFTTxTKOkb+uRCE8I=
github.com/gosuri/uilive v0.0.3/go.mod h1:qkLSc0A5EXSP6B04TrN4oQoxqFI7A8XvoXSlJi8cwk8=
github.com/gosuri/uilive v0.0.4 h1:hUEBpQDj8D8jXgtCdBu7sWsy5sbW/5GhuO8KBwJ2jyY=
github.com/gosuri/uilive v0.0.4/go.mod h1:V/epo5LjjlDE5RJUcqx8dbw+zc93y5Ya3yg8tfZ74VI=
Expand Down Expand Up @@ -345,8 +348,6 @@ github.com/jmhodges/levigo v1.0.0/go.mod h1:Q6Qx+uH3RAqyK4rFQroq9RL7mdkABMcfhEI+
github.com/joeqian10/neo-gogogo v0.0.0-20200611102831-c17de5e1f0f8/go.mod h1:1fVDp4U1ROZQBRIooecbGNHHJpfs3bG9528sqlZ096g=
github.com/joeqian10/neo-gogogo v1.1.0 h1:TjqBwFQnNCtw6LK3QMog3yHa9sTZaZmqP+zqGOZ3SZ0=
github.com/joeqian10/neo-gogogo v1.1.0/go.mod h1:1fVDp4U1ROZQBRIooecbGNHHJpfs3bG9528sqlZ096g=
github.com/joeqian10/neo3-gogogo v0.3.4 h1:obvYV5QCJELY5RmA8+pBPj03SQrHaUId22vK654jzPQ=
github.com/joeqian10/neo3-gogogo v0.3.4/go.mod h1:k0wb1hcBjjspDpyHtEXIpDUEXAw5SfX7coi5AkNtxoU=
github.com/joeqian10/neo3-gogogo v0.3.8 h1:oOAcdUeIFjE4g+93Fsgf/fjYkZdOPt3PNefE223Mi/A=
github.com/joeqian10/neo3-gogogo v0.3.8/go.mod h1:k0wb1hcBjjspDpyHtEXIpDUEXAw5SfX7coi5AkNtxoU=
github.com/joeqian10/neo3-gogogo-legacy v1.0.0 h1:UeYhbn2q75WpuXlj6WrEzsygcWRws06LE2QB3/u/DCE=
Expand Down Expand Up @@ -449,7 +450,6 @@ github.com/onsi/gomega v1.7.0 h1:XPnZz8VVBHjVsy1vzJmRwIcSwiUO+JFfrv/xGiigmME=
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/ontio/go-bip32 v0.0.0-20190520025953-d3cea6894a2b/go.mod h1:J0eVc7BEMmVVXbGv9PHoxjRSEwOwLr0qfzPk8Rdl5iw=
github.com/ontio/ontology v1.10.0/go.mod h1:iok/imHJVQXi5/Yr88dcbrKBRHGdiota1ZC6qh6l6Rc=
github.com/ontio/ontology v1.11.0 h1:0T/hxFDHQqRcs1+yEdgaym5YIvGx5yebOsHYdKVWgHI=
github.com/ontio/ontology v1.11.0/go.mod h1:Qw74bfTBlIQka+jQX4nXuWvyOYGGt368/V7XFxaf4tY=
github.com/ontio/ontology v1.11.1-0.20200812075204-26cf1fa5dd47 h1:9iZitqJe7SBGF8f6jOHjhotrB7ZLKKMM+g6S0tMbOL4=
github.com/ontio/ontology v1.11.1-0.20200812075204-26cf1fa5dd47/go.mod h1:aoLM6pLdjBLx2CwC/AUtxdHvLZzAVqYH/xehh6/sRP4=
Expand Down Expand Up @@ -534,6 +534,10 @@ github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40T
github.com/rakyll/statik v0.1.6/go.mod h1:OEi9wJV/fMUAGx1eNjq75DKDsJVuEv1U0oYdX6GX8Zs=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/renlulu/gozilliqa-sdklegacy v0.0.0-20210926102505-c3fd6bb52053 h1:4SPSsgX0Kb3U0grS3ubyy3CFMyXwD7g65RUKX3RXsk0=
github.com/renlulu/gozilliqa-sdklegacy v0.0.0-20210926102505-c3fd6bb52053/go.mod h1:G1F6zA775maiVm3UlsGYB/YKwFu2jGZfVYzUqqi7dHg=
github.com/renlulu/gozilliqa-sdklegacy v0.0.0-20210926114807-88a08c5ab803 h1:IRlxA6Ii+xclKSSzZ2iiTDLbh4y48boQS+0VDS8tXNo=
github.com/renlulu/gozilliqa-sdklegacy v0.0.0-20210926114807-88a08c5ab803/go.mod h1:G1F6zA775maiVm3UlsGYB/YKwFu2jGZfVYzUqqi7dHg=
github.com/rjeczalik/notify v0.9.1 h1:CLCKso/QK1snAlnhNR/CNvNiFU2saUtjV0bx3EwNeCE=
github.com/rjeczalik/notify v0.9.1/go.mod h1:rKwnCoCGeuQnwBtTSPL9Dad03Vh2n40ePRrjvIXnJho=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
Expand Down
5 changes: 4 additions & 1 deletion native/service/cross_chain_manager/entrance.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package cross_chain_manager
import (
"encoding/hex"
"fmt"
"github.com/polynetwork/poly/native/service/cross_chain_manager/zilliqa"
"github.com/polynetwork/poly/native/service/cross_chain_manager/consensus_vote"

"github.com/polynetwork/poly/common"
Expand All @@ -36,7 +37,7 @@ import (
"github.com/polynetwork/poly/native/service/cross_chain_manager/ont"
"github.com/polynetwork/poly/native/service/cross_chain_manager/polygon"
"github.com/polynetwork/poly/native/service/cross_chain_manager/quorum"
"github.com/polynetwork/poly/native/service/cross_chain_manager/zilliqa"
"github.com/polynetwork/poly/native/service/cross_chain_manager/zilliqalegacy"
"github.com/polynetwork/poly/native/service/governance/node_manager"
"github.com/polynetwork/poly/native/service/governance/side_chain_manager"
"github.com/polynetwork/poly/native/service/utils"
Expand Down Expand Up @@ -81,6 +82,8 @@ func GetChainHandler(router uint64) (scom.ChainHandler, error) {
return bsc.NewHandler(), nil
case utils.HECO_ROUTER:
return heco.NewHecoHandler(), nil
case utils.ZILLIQA_LEGACY_ROUTER:
return zilliqalegacy.NewHandler(), nil
case utils.ZILLIQA_ROUTER:
return zilliqa.NewHandler(), nil
case utils.MSC_ROUTER:
Expand Down
170 changes: 170 additions & 0 deletions native/service/cross_chain_manager/zilliqalegacy/zilliqa_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
* Copyright (C) 2021 The poly network Authors
* This file is part of The poly network library.
*
* The poly network is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The poly network is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* along with The poly network . If not, see <http://www.gnu.org/licenses/>.
*/

package zilliqalegacy

import (
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/crypto"
"github.com/polynetwork/poly/native/service/header_sync/zilliqalegacy"
"github.com/renlulu/gozilliqa-sdklegacy/core"
"github.com/renlulu/gozilliqa-sdklegacy/mpt"
"github.com/renlulu/gozilliqa-sdklegacy/util"
"strings"

"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"
)

// Handler ...
type Handler struct {
}

// NewHandler ...
func NewHandler() *Handler {
return &Handler{}
}

// MakeDepositProposal ...
func (h *Handler) MakeDepositProposal(service *native.NativeService) (*scom.MakeTxParam, error) {
params := new(scom.EntranceParam)
if err := params.Deserialization(common.NewZeroCopySource(service.GetInput())); err != nil {
return nil, fmt.Errorf("zilliqalegacy MakeDepositProposal, contract params deserialize error: %s", err)
}

sideChain, err := side_chain_manager.GetSideChain(service, params.SourceChainID)
if err != nil {
return nil, fmt.Errorf("zilliqalegacy MakeDepositProposal, side_chain_manager.GetSideChain error: %v", err)
}

value, err := verifyFromTx(service, params.Proof, params.Extra, params.SourceChainID, params.Height, sideChain)
if err != nil {
return nil, fmt.Errorf("zil MakeDepositProposal, verifyFromZILTx error: %s", err)
}

if err := scom.CheckDoneTx(service, value.CrossChainID, params.SourceChainID); err != nil {
return nil, fmt.Errorf("zil MakeDepositProposal, check done transaction error:%s", err)
}
if err := scom.PutDoneTx(service, value.CrossChainID, params.SourceChainID); err != nil {
return nil, fmt.Errorf("zil MakeDepositProposal, PutDoneTx error:%s", err)
}
return value, nil
}

// should be the same as relayer side
type ZILProof struct {
AccountProof []string `json:"accountProof"`
StorageProofs []StorageProof `json:"storageProof"`
}

// key should be storage key (in zilliqalegacy)
type StorageProof struct {
Key []byte `json:"key"`
Value []byte `json:"value"`
Proof []string `json:"proof"`
}

func verifyFromTx(native *native.NativeService, proof, extra []byte, fromChainID uint64, height uint32, sideChain *side_chain_manager.SideChain) (param *scom.MakeTxParam, err error) {
bestHeader, err := zilliqalegacy.GetCurrentTxHeader(native, fromChainID)
if err != nil {
return nil, fmt.Errorf("VerifyFromZilProof, get current header fail, error:%s", err)
}

bestHeight := uint32(bestHeader.BlockHeader.BlockNum)
if bestHeight < height {
return nil, fmt.Errorf("VerifyFromZilProof, transaction is not confirmed, current height: %d, input height: %d", bestHeight, height)
}
blockData, err := zilliqalegacy.GetTxHeaderByHeight(native, uint64(height), fromChainID)
if err != nil {
return nil, fmt.Errorf("VerifyFromZilProof, get header by height, height:%d, error:%s", height, err)
}

var zilProof ZILProof
err = json.Unmarshal(proof, &zilProof)
if err != nil {
return nil, fmt.Errorf("VerifyFromZilProof, unmarshal proof error:%s", err)
}

if len(zilProof.StorageProofs) != 1 {
return nil, fmt.Errorf("VerifyFromZilProof, incorrect proof format")
}

var pf [][]byte
for _, p := range zilProof.AccountProof {
bytes := util.DecodeHex(p)
pf = append(pf, bytes)
}

db := mpt.NewFromProof(pf)
root := blockData.BlockHeader.HashSet.StateRootHash[:]
key := strings.TrimPrefix(util.EncodeHex(sideChain.CCMCAddress), "0x")
accountBaseBytes, err := mpt.Verify([]byte(key), db, root)
if err != nil {
return nil, fmt.Errorf("verifyMerkleProof, verify account proof error:%s, key is %s proof is: %+v, root is %s", err, key, zilProof.AccountProof, util.EncodeHex(root))
}

accountBase, err := core.AccountBaseFromBytes(accountBaseBytes)
if err != nil {
return nil, fmt.Errorf("verifyMerkleProof, get account info error:%s\n", err)
}

var proof2 [][]byte
for _, p := range zilProof.StorageProofs[0].Proof {
bytes := util.DecodeHex(p)
proof2 = append(proof2, bytes)
}

db2 := mpt.NewFromProof(proof2)
storageKey := util.DecodeHex(string(zilProof.StorageProofs[0].Key))
hashedStorageKey := util.Sha256(storageKey)
proofResult, err := mpt.Verify([]byte((util.EncodeHex(hashedStorageKey))), db2, accountBase.StorageRoot)
if err != nil {
return nil, fmt.Errorf("verifyMerkleProof, verify state proof error:%s, key is %s account proof is: %+v, state proof is: %+v, account bytes is: %s, root is %s", err,
util.EncodeHex(storageKey), zilProof.AccountProof, zilProof.StorageProofs[0].Proof, util.EncodeHex(accountBaseBytes), util.EncodeHex(accountBase.StorageRoot))
}

if proofResult == nil {
return nil, fmt.Errorf("verifyMerkleProof, verify state proof error:%s, key is %s account proof is: %+v, state proof is: %+v, account bytes is: %s, root is %s", "result is nil",
util.EncodeHex(storageKey), zilProof.AccountProof, zilProof.StorageProofs[0].Proof, util.EncodeHex(accountBaseBytes), util.EncodeHex(accountBase.StorageRoot))
}

if !checkProofResult(proofResult, extra) {
return nil, fmt.Errorf("verifyMerkleProof, check state proof result failed proof result: %s, extra: %s", util.EncodeHex(proofResult), util.EncodeHex(extra))
}

data := common.NewZeroCopySource(extra)
txParam := new(scom.MakeTxParam)
if err := txParam.Deserialization(data); err != nil {
return nil, fmt.Errorf("VerifyFromZilProof, deserialize merkleValue error:%s", err)
}
return txParam, nil
}

func checkProofResult(result, value []byte) bool {
origin := strings.ToLower(string(result))
origin = strings.TrimPrefix(strings.ReplaceAll(origin, "\"", ""), "0x")

hash := crypto.Keccak256(value)
target := util.EncodeHex(hash)
target = strings.ToLower(target)
target = strings.TrimPrefix(target, "0x")

return origin == target
}
5 changes: 4 additions & 1 deletion native/service/header_sync/entrance.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package header_sync

import (
"fmt"
"github.com/polynetwork/poly/native/service/header_sync/zilliqa"
"github.com/polynetwork/poly/native/service/header_sync/zilliqalegacy"

"github.com/polynetwork/poly/native/service/header_sync/neo"
"github.com/polynetwork/poly/native/service/header_sync/neo3"
Expand All @@ -39,7 +41,6 @@ import (
"github.com/polynetwork/poly/native/service/header_sync/eth"
"github.com/polynetwork/poly/native/service/header_sync/ont"
"github.com/polynetwork/poly/native/service/header_sync/quorum"
"github.com/polynetwork/poly/native/service/header_sync/zilliqa"
"github.com/polynetwork/poly/native/service/utils"
)

Expand Down Expand Up @@ -78,6 +79,8 @@ func GetChainHandler(router uint64) (hscommon.HeaderSyncHandler, error) {
return bsc.NewHandler(), nil
case utils.HECO_ROUTER:
return heco.NewHecoHandler(), nil
case utils.ZILLIQA_LEGACY_ROUTER:
return zilliqalegacy.NewHandler(), nil
case utils.ZILLIQA_ROUTER:
return zilliqa.NewHandler(), nil
case utils.MSC_ROUTER:
Expand Down
Loading

0 comments on commit 48fe538

Please sign in to comment.