Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/cosmos/cosmos-sdk v0.50.5
github.com/cosmos/gogoproto v1.4.11
github.com/cosmos/ibc-go/v8 v8.2.1
github.com/datachainlab/ethereum-ibc-relay-chain v0.3.16
github.com/datachainlab/ethereum-ibc-relay-chain v0.3.17
github.com/datachainlab/ibc-hd-signer v0.1.2
github.com/ethereum/go-ethereum v1.15.0
github.com/hyperledger-labs/yui-relayer v0.5.11
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,8 @@ github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7Do
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/danieljoos/wincred v1.1.2 h1:QLdCxFs1/Yl4zduvBdcHB8goaYk9RARS2SgLLRuAyr0=
github.com/danieljoos/wincred v1.1.2/go.mod h1:GijpziifJoIBfYh+S7BbkdUTU4LfM+QnGqR5Vl2tAx0=
github.com/datachainlab/ethereum-ibc-relay-chain v0.3.16 h1:C4xwZ1Pq5O+ITjP5UphpwThtTHnWiTe0V4pckEvqfwI=
github.com/datachainlab/ethereum-ibc-relay-chain v0.3.16/go.mod h1:sf+ti1uM1joxhukxIyj8oDQ5yf7d+e+nl+3vtn9f5CM=
github.com/datachainlab/ethereum-ibc-relay-chain v0.3.17 h1:oyDLIbc9hyp31c3OoIB5wHm51tpyDrYAu1KBA1KxfEw=
github.com/datachainlab/ethereum-ibc-relay-chain v0.3.17/go.mod h1:sf+ti1uM1joxhukxIyj8oDQ5yf7d+e+nl+3vtn9f5CM=
github.com/datachainlab/ibc-hd-signer v0.1.2 h1:fWAYjMBVyM390OllX/l58mZYA7we0spEBFLYKWYTwfw=
github.com/datachainlab/ibc-hd-signer v0.1.2/go.mod h1:nwH0Z3TK/7jbu/oInGGWGRimQ+LCn6yvDyjMZrw9mR4=
github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
6 changes: 3 additions & 3 deletions module/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Chain interface {
Header(ctx context.Context, height uint64) (*types.Header, error)
IBCAddress() common.Address
CanonicalChainID(ctx context.Context) (uint64, error)
GetProof(address common.Address, storageKeys [][]byte, blockNumber *big.Int) (*client.StateProof, error)
GetProof(ctx context.Context, address common.Address, storageKeys [][]byte, blockNumber *big.Int) (*client.StateProof, error)
}

type ethChain struct {
Expand Down Expand Up @@ -47,6 +47,6 @@ func (c *ethChain) CanonicalChainID(ctx context.Context) (uint64, error) {
return chainID.Uint64(), nil
}

func (c *ethChain) GetProof(address common.Address, storageKeys [][]byte, blockNumber *big.Int) (*client.StateProof, error) {
return c.Client().GetProof(address, storageKeys, blockNumber)
func (c *ethChain) GetProof(ctx context.Context, address common.Address, storageKeys [][]byte, blockNumber *big.Int) (*client.StateProof, error) {
return c.Client().GetProof(ctx, address, storageKeys, blockNumber)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The signature has changed in the PR datachainlab/ethereum-ibc-relay-chain#64 (comment).

}
14 changes: 9 additions & 5 deletions module/facade.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package module

import "github.com/ethereum/go-ethereum/core/types"
import (
"context"

"github.com/ethereum/go-ethereum/core/types"
)

// Facade for tool modules

Expand All @@ -12,12 +16,12 @@ func GetCurrentEpoch(v uint64) uint64 {
return getCurrentEpoch(v)
}

func QueryFinalizedHeader(fn getHeaderFn, height uint64, limitHeight uint64) ([]*ETHHeader, error) {
return queryFinalizedHeader(fn, height, limitHeight)
func QueryFinalizedHeader(ctx context.Context, fn getHeaderFn, height uint64, limitHeight uint64) ([]*ETHHeader, error) {
return queryFinalizedHeader(ctx, fn, height, limitHeight)
}

func QueryValidatorSetAndTurnLength(fn getHeaderFn, height uint64) (Validators, uint8, error) {
return queryValidatorSetAndTurnLength(fn, height)
func QueryValidatorSetAndTurnLength(ctx context.Context, fn getHeaderFn, height uint64) (Validators, uint8, error) {
return queryValidatorSetAndTurnLength(ctx, fn, height)
}

func ExtractValidatorSetAndTurnLength(h *types.Header) (Validators, uint8, error) {
Expand Down
19 changes: 10 additions & 9 deletions module/header_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ package module
import (
"context"
"fmt"

"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
"github.com/hyperledger-labs/yui-relayer/log"
)

type getHeaderFn func(context.Context, uint64) (*types.Header, error)

func queryLatestFinalizedHeader(getHeader getHeaderFn, latestBlockNumber uint64) (uint64, []*ETHHeader, error) {
func queryLatestFinalizedHeader(ctx context.Context, getHeader getHeaderFn, latestBlockNumber uint64) (uint64, []*ETHHeader, error) {
logger := log.GetLogger()
for i := latestBlockNumber; i > 0; i-- {
header, err := getHeader(context.Background(), i)
header, err := getHeader(ctx, i)
if err != nil {
return 0, nil, err
}
Expand All @@ -28,7 +29,7 @@ func queryLatestFinalizedHeader(getHeader getHeaderFn, latestBlockNumber uint64)

logger.Debug("Try to seek verifying headers to finalize", "probablyFinalized", probablyFinalized, "latest", latestBlockNumber)

headers, err := queryFinalizedHeader(getHeader, probablyFinalized, latestBlockNumber)
headers, err := queryFinalizedHeader(ctx, getHeader, probablyFinalized, latestBlockNumber)
if err != nil {
return 0, nil, err
}
Expand All @@ -40,18 +41,18 @@ func queryLatestFinalizedHeader(getHeader getHeaderFn, latestBlockNumber uint64)
return 0, nil, fmt.Errorf("no finalized header found: %d", latestBlockNumber)
}

func queryFinalizedHeader(fn getHeaderFn, height uint64, limitHeight uint64) ([]*ETHHeader, error) {
func queryFinalizedHeader(ctx context.Context, fn getHeaderFn, height uint64, limitHeight uint64) ([]*ETHHeader, error) {
var ethHeaders []*ETHHeader
for i := height; i+2 <= limitHeight; i++ {
targetBlock, targetETHHeader, _, err := queryETHHeader(fn, i)
targetBlock, targetETHHeader, _, err := queryETHHeader(ctx, fn, i)
if err != nil {
return nil, err
}
childBlock, childETHHeader, childVote, err := queryETHHeader(fn, i+1)
childBlock, childETHHeader, childVote, err := queryETHHeader(ctx, fn, i+1)
if err != nil {
return nil, err
}
_, grandChildETHHeader, grandChildVote, err := queryETHHeader(fn, i+2)
_, grandChildETHHeader, grandChildVote, err := queryETHHeader(ctx, fn, i+2)
if err != nil {
return nil, err
}
Expand All @@ -70,8 +71,8 @@ func queryFinalizedHeader(fn getHeaderFn, height uint64, limitHeight uint64) ([]
return nil, nil
}

func queryETHHeader(fn getHeaderFn, height uint64) (*types.Header, *ETHHeader, *VoteAttestation, error) {
block, err := fn(context.TODO(), height)
func queryETHHeader(ctx context.Context, fn getHeaderFn, height uint64) (*types.Header, *ETHHeader, *VoteAttestation, error) {
block, err := fn(ctx, height)
if err != nil {
return nil, nil, nil, err
}
Expand Down
17 changes: 9 additions & 8 deletions module/header_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package module

import (
"context"
"math/big"
"strings"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/hyperledger-labs/yui-relayer/log"
"github.com/stretchr/testify/suite"
"math/big"
"strings"
"testing"
)

type HeaderQueryTestSuite struct {
Expand All @@ -33,7 +34,7 @@ func (ts *HeaderQueryTestSuite) TestErrorQueryFinalizedHeader() {
}

// No finalized header found
headers, err := queryFinalizedHeader(fn, 1, 10)
headers, err := queryFinalizedHeader(context.Background(), fn, 1, 10)
ts.Require().NoError(err)
ts.Require().Nil(headers)

Expand All @@ -45,7 +46,7 @@ func (ts *HeaderQueryTestSuite) TestErrorQueryFinalizedHeader() {
return &types.Header{Number: big.NewInt(int64(height))}, nil
}

headers, err = queryFinalizedHeader(fn, 360, 400)
headers, err = queryFinalizedHeader(context.Background(), fn, 360, 400)
ts.Require().NoError(err)
ts.Require().Nil(headers)
}
Expand All @@ -60,7 +61,7 @@ func (ts *HeaderQueryTestSuite) TestSuccessQueryFinalizedHeader() {
return &types.Header{Number: big.NewInt(int64(height))}, nil
}

headers, err := queryFinalizedHeader(fn, 360, 402)
headers, err := queryFinalizedHeader(context.Background(), fn, 360, 402)
ts.Require().NoError(err)
ts.Require().Len(headers, 402-360)
}
Expand All @@ -75,7 +76,7 @@ func (ts *HeaderQueryTestSuite) TestSuccessQueryLatestFinalizedHeader() {
}
return &types.Header{Number: big.NewInt(int64(height))}, nil
}
height, h, err := queryLatestFinalizedHeader(getHeader, latestBlockNumber)
height, h, err := queryLatestFinalizedHeader(context.Background(), getHeader, latestBlockNumber)
ts.Require().NoError(err)
ts.Require().Len(h, 3)
ts.Require().Equal(int(height), 401)
Expand All @@ -94,7 +95,7 @@ func (ts *HeaderQueryTestSuite) TestErrorQueryLatestFinalizedHeader_NoVote() {
Extra: extra,
}, nil
}
_, _, err := queryLatestFinalizedHeader(getHeader, latestBlockNumber)
_, _, err := queryLatestFinalizedHeader(context.Background(), getHeader, latestBlockNumber)
ts.Require().True(strings.Contains(err.Error(), "no finalized header found"))
}

Expand Down
20 changes: 12 additions & 8 deletions module/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package module

import (
"bytes"
"context"
"fmt"
"math/big"

clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
"github.com/hyperledger-labs/yui-relayer/core"
"math/big"

"github.com/cosmos/gogoproto/proto"
"github.com/cosmos/ibc-go/v8/modules/core/exported"
Expand All @@ -16,8 +18,9 @@ import (
"github.com/ethereum/go-ethereum/trie"
)

func (pr *Prover) getAccountProof(height uint64) ([]byte, common.Hash, error) {
func (pr *Prover) getAccountProof(ctx context.Context, height uint64) ([]byte, common.Hash, error) {
stateProof, err := pr.chain.GetProof(
ctx,
pr.chain.IBCAddress(),
nil,
big.NewInt(0).SetUint64(height),
Expand All @@ -28,7 +31,7 @@ func (pr *Prover) getAccountProof(height uint64) ([]byte, common.Hash, error) {
return stateProof.AccountProofRLP, common.BytesToHash(stateProof.StorageHash[:]), nil
}

func (pr *Prover) getStateCommitmentProof(path []byte, height exported.Height) ([]byte, []byte, error) {
func (pr *Prover) getStateCommitmentProof(ctx context.Context, path []byte, height exported.Height) ([]byte, []byte, error) {
// calculate slot for commitment
storageKey := crypto.Keccak256Hash(append(
crypto.Keccak256Hash(path).Bytes(),
Expand All @@ -41,6 +44,7 @@ func (pr *Prover) getStateCommitmentProof(path []byte, height exported.Height) (

// call eth_getProof
stateProof, err := pr.chain.GetProof(
ctx,
pr.chain.IBCAddress(),
[][]byte{storageKeyHex},
big.NewInt(int64(height.GetRevisionHeight())),
Expand All @@ -52,8 +56,8 @@ func (pr *Prover) getStateCommitmentProof(path []byte, height exported.Height) (
return stateProof.AccountProofRLP, stateProof.StorageProofRLP[0], nil
}

func (pr *Prover) GetStorageRoot(header *types.Header) (common.Hash, error) {
rlpAccountProof, _, err := pr.getAccountProof(header.Number.Uint64())
func (pr *Prover) GetStorageRoot(ctx context.Context, header *types.Header) (common.Hash, error) {
rlpAccountProof, _, err := pr.getAccountProof(ctx, header.Number.Uint64())
if err != nil {
return common.Hash{}, err
}
Expand Down Expand Up @@ -170,7 +174,7 @@ func verifyAccount(target *types.Header, accountProof []byte, path common.Addres
return &account, nil
}

func withValidators(headerFn getHeaderFn, height uint64, ethHeaders []*ETHHeader) (core.Header, error) {
func withValidators(ctx context.Context, headerFn getHeaderFn, height uint64, ethHeaders []*ETHHeader) (core.Header, error) {

header := &Header{
Headers: ethHeaders,
Expand All @@ -180,14 +184,14 @@ func withValidators(headerFn getHeaderFn, height uint64, ethHeaders []*ETHHeader
previousEpoch := getPreviousEpoch(height)
var previousTurnLength uint8
var err error
header.PreviousValidators, previousTurnLength, err = queryValidatorSetAndTurnLength(headerFn, previousEpoch)
header.PreviousValidators, previousTurnLength, err = queryValidatorSetAndTurnLength(ctx, headerFn, previousEpoch)
header.PreviousTurnLength = uint32(previousTurnLength)
if err != nil {
return nil, fmt.Errorf("ValidatorSet was not found in previous epoch : number = %d : %+v", previousEpoch, err)
}
currentEpoch := getCurrentEpoch(height)
var currentTurnLength uint8
header.CurrentValidators, currentTurnLength, err = queryValidatorSetAndTurnLength(headerFn, currentEpoch)
header.CurrentValidators, currentTurnLength, err = queryValidatorSetAndTurnLength(ctx, headerFn, currentEpoch)
header.CurrentTurnLength = uint32(currentTurnLength)
if err != nil {
return nil, fmt.Errorf("ValidatorSet was not found in current epoch : number= %d : %+v", currentEpoch, err)
Expand Down
Loading