From d62e491f8b9ec60033beef203c4aacb739c7544c Mon Sep 17 00:00:00 2001 From: abicky Date: Tue, 4 Mar 2025 18:29:03 +0900 Subject: [PATCH 1/7] Update ethereum-ibc-relay-chain Signed-off-by: abicky --- go.mod | 2 +- go.sum | 4 ++-- module/chain.go | 2 +- module/prover_test.go | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index f2fb2b1b..e627b34f 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 59999ad4..aca16d0d 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/module/chain.go b/module/chain.go index 3d6f1052..dca2c025 100644 --- a/module/chain.go +++ b/module/chain.go @@ -48,5 +48,5 @@ func (c *ethChain) CanonicalChainID(ctx context.Context) (uint64, error) { } func (c *ethChain) GetProof(address common.Address, storageKeys [][]byte, blockNumber *big.Int) (*client.StateProof, error) { - return c.Client().GetProof(address, storageKeys, blockNumber) + return c.Client().GetProof(context.TODO(), address, storageKeys, blockNumber) } diff --git a/module/prover_test.go b/module/prover_test.go index e9a31fe5..d3f1a6b7 100644 --- a/module/prover_test.go +++ b/module/prover_test.go @@ -106,7 +106,7 @@ func (ts *ProverTestSuite) SetupTest() { } anySignerConfig, err := codectypes.NewAnyWithValue(signerConfig) ts.Require().NoError(err) - chain, err := ethereum.NewChain(ethereum.ChainConfig{ + chain, err := ethereum.NewChain(context.Background(), ethereum.ChainConfig{ EthChainId: 9999, IbcAddress: common.Address{}.String(), Signer: anySignerConfig, From 7572a67600703e286da07de04b8b333ad3b041b3 Mon Sep 17 00:00:00 2001 From: Takeshi Arabiki Date: Thu, 13 Mar 2025 17:36:03 +0900 Subject: [PATCH 2/7] Add context to ethChain.GetProof Signed-off-by: Takeshi Arabiki --- module/chain.go | 6 +++--- module/proof.go | 6 +++++- module/prover_test.go | 4 ++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/module/chain.go b/module/chain.go index dca2c025..5bc09409 100644 --- a/module/chain.go +++ b/module/chain.go @@ -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 { @@ -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(context.TODO(), 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) } diff --git a/module/proof.go b/module/proof.go index 0f54071d..d402e416 100644 --- a/module/proof.go +++ b/module/proof.go @@ -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" @@ -18,6 +20,7 @@ import ( func (pr *Prover) getAccountProof(height uint64) ([]byte, common.Hash, error) { stateProof, err := pr.chain.GetProof( + context.TODO(), pr.chain.IBCAddress(), nil, big.NewInt(0).SetUint64(height), @@ -41,6 +44,7 @@ func (pr *Prover) getStateCommitmentProof(path []byte, height exported.Height) ( // call eth_getProof stateProof, err := pr.chain.GetProof( + context.TODO(), pr.chain.IBCAddress(), [][]byte{storageKeyHex}, big.NewInt(int64(height.GetRevisionHeight())), diff --git a/module/prover_test.go b/module/prover_test.go index d3f1a6b7..7ccad211 100644 --- a/module/prover_test.go +++ b/module/prover_test.go @@ -31,7 +31,7 @@ type mockChain struct { trustedHeight uint64 } -func (c *mockChain) GetProof(_ common.Address, _ [][]byte, _ *big.Int) (*client.StateProof, error) { +func (c *mockChain) GetProof(_ context.Context, _ common.Address, _ [][]byte, _ *big.Int) (*client.StateProof, error) { // eth.getProof("0xaa43d337145E8930d01cb4E60Abf6595C692921E",["0x0c0dd47e5867d48cad725de0d09f9549bd564c1d143f6c1f451b26ccd981eeae"], 21400) // storageHash: "0xc3608871098f21b59607ef3fb9412a091de9246ad1281a92f5b07dc2f465b7a0", accountProof := []string{ @@ -166,7 +166,7 @@ func (ts *ProverTestSuite) TestQueryClientStateWithProof() { decoded := ProveState{} ts.Require().NoError(decoded.Unmarshal(proof)) - expected, _ := ts.chain.GetProof(common.Address{}, nil, nil) + expected, _ := ts.chain.GetProof(context.Background(), common.Address{}, nil, nil) ts.Require().Equal(common.Bytes2Hex(decoded.AccountProof), common.Bytes2Hex(expected.AccountProofRLP)) // storage_key is 0x0c0dd47e5867d48cad725de0d09f9549bd564c1d143f6c1f451b26ccd981eeae ts.Require().Equal(common.Bytes2Hex(decoded.CommitmentProof), "f853f8518080a0143145e818eeff83817419a6632ea193fd1acaa4f791eb17282f623f38117f568080808080808080a016cbf6e0ba10512eb618d99a1e34025adb7e6f31d335bda7fb20c8bb95fb5b978080808080") From 4abe3a8e3d5be65b05b6eab07e80673c54bb039b Mon Sep 17 00:00:00 2001 From: Takeshi Arabiki Date: Thu, 13 Mar 2025 18:24:49 +0900 Subject: [PATCH 3/7] Add query* functions in module package Signed-off-by: Takeshi Arabiki --- module/facade.go | 10 +++++++--- module/header_query.go | 19 ++++++++++--------- module/header_query_test.go | 17 +++++++++-------- module/proof.go | 4 ++-- module/prover.go | 12 ++++++------ module/setup.go | 4 +++- module/validator_set.go | 5 +++-- 7 files changed, 40 insertions(+), 31 deletions(-) diff --git a/module/facade.go b/module/facade.go index 9b806186..43bb1d0a 100644 --- a/module/facade.go +++ b/module/facade.go @@ -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 @@ -13,11 +17,11 @@ func GetCurrentEpoch(v uint64) uint64 { } func QueryFinalizedHeader(fn getHeaderFn, height uint64, limitHeight uint64) ([]*ETHHeader, error) { - return queryFinalizedHeader(fn, height, limitHeight) + return queryFinalizedHeader(context.TODO(), fn, height, limitHeight) } func QueryValidatorSetAndTurnLength(fn getHeaderFn, height uint64) (Validators, uint8, error) { - return queryValidatorSetAndTurnLength(fn, height) + return queryValidatorSetAndTurnLength(context.TODO(), fn, height) } func ExtractValidatorSetAndTurnLength(h *types.Header) (Validators, uint8, error) { diff --git a/module/header_query.go b/module/header_query.go index 209e651b..723d303c 100644 --- a/module/header_query.go +++ b/module/header_query.go @@ -3,6 +3,7 @@ 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" @@ -10,10 +11,10 @@ import ( 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 } @@ -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 } @@ -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 } @@ -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 } diff --git a/module/header_query_test.go b/module/header_query_test.go index 264cff69..1c23d224 100644 --- a/module/header_query_test.go +++ b/module/header_query_test.go @@ -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 { @@ -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) @@ -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) } @@ -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) } @@ -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) @@ -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")) } diff --git a/module/proof.go b/module/proof.go index d402e416..2f4fa1f0 100644 --- a/module/proof.go +++ b/module/proof.go @@ -184,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(context.TODO(), 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(context.TODO(), 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) diff --git a/module/prover.go b/module/prover.go index 17608d99..989473dd 100644 --- a/module/prover.go +++ b/module/prover.go @@ -58,9 +58,9 @@ func (pr *Prover) CreateInitialLightClientState(ctx context.Context, height expo } var finalizedHeader []*ETHHeader if height == nil { - _, finalizedHeader, err = queryLatestFinalizedHeader(pr.chain.Header, latestHeight.GetRevisionHeight()) + _, finalizedHeader, err = queryLatestFinalizedHeader(context.TODO(), pr.chain.Header, latestHeight.GetRevisionHeight()) } else { - finalizedHeader, err = queryFinalizedHeader(pr.chain.Header, height.GetRevisionHeight(), latestHeight.GetRevisionHeight()) + finalizedHeader, err = queryFinalizedHeader(context.TODO(), pr.chain.Header, height.GetRevisionHeight(), latestHeight.GetRevisionHeight()) } if err != nil { return nil, nil, err @@ -90,7 +90,7 @@ func (pr *Prover) GetLatestFinalizedHeader(ctx context.Context) (out core.Header // GetLatestFinalizedHeaderByLatestHeight returns the latest finalized verifiable header from the chain func (pr *Prover) GetLatestFinalizedHeaderByLatestHeight(ctx context.Context, latestBlockNumber uint64) (core.Header, error) { - height, finalizedHeader, err := queryLatestFinalizedHeader(pr.chain.Header, latestBlockNumber) + height, finalizedHeader, err := queryLatestFinalizedHeader(context.TODO(), pr.chain.Header, latestBlockNumber) if err != nil { return nil, err } @@ -119,7 +119,7 @@ func (pr *Prover) SetupHeadersForUpdate(ctx context.Context, counterparty core.F func (pr *Prover) SetupHeadersForUpdateByLatestHeight(ctx context.Context, clientStateLatestHeight exported.Height, latestFinalizedHeader *Header) ([]core.Header, error) { queryVerifiableNeighboringEpochHeader := func(height uint64, limitHeight uint64) (core.Header, error) { - ethHeaders, err := queryFinalizedHeader(pr.chain.Header, height, limitHeight) + ethHeaders, err := queryFinalizedHeader(context.TODO(), pr.chain.Header, height, limitHeight) if err != nil { return nil, err } @@ -232,13 +232,13 @@ func (pr *Prover) withValidators(height uint64, ethHeaders []*ETHHeader) (core.H func (pr *Prover) buildInitialState(dstHeader core.Header) (exported.ClientState, exported.ConsensusState, error) { currentEpoch := getCurrentEpoch(dstHeader.GetHeight().GetRevisionHeight()) - currentValidators, currentTurnLength, err := queryValidatorSetAndTurnLength(pr.chain.Header, currentEpoch) + currentValidators, currentTurnLength, err := queryValidatorSetAndTurnLength(context.TODO(), pr.chain.Header, currentEpoch) if err != nil { return nil, nil, err } previousEpoch := getPreviousEpoch(dstHeader.GetHeight().GetRevisionHeight()) - previousValidators, previousTurnLength, err := queryValidatorSetAndTurnLength(pr.chain.Header, previousEpoch) + previousValidators, previousTurnLength, err := queryValidatorSetAndTurnLength(context.TODO(), pr.chain.Header, previousEpoch) if err != nil { return nil, nil, err } diff --git a/module/setup.go b/module/setup.go index dc86ec3d..bcb844cf 100644 --- a/module/setup.go +++ b/module/setup.go @@ -1,7 +1,9 @@ package module import ( + "context" "fmt" + clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types" "github.com/cosmos/ibc-go/v8/modules/core/exported" "github.com/datachainlab/ibc-parlia-relay/module/constant" @@ -60,7 +62,7 @@ func setupNeighboringEpochHeader( latestHeight exported.Height, ) (core.Header, error) { // neighboring epoch needs block before checkpoint - currentValidatorSet, currentTurnLength, err := queryValidatorSetAndTurnLength(getHeader, epochHeight) + currentValidatorSet, currentTurnLength, err := queryValidatorSetAndTurnLength(context.TODO(), getHeader, epochHeight) if err != nil { return nil, fmt.Errorf("setupNeighboringEpochHeader: failed to get current validator set: epochHeight=%d : %+v", epochHeight, err) } diff --git a/module/validator_set.go b/module/validator_set.go index 448f95b7..00218a17 100644 --- a/module/validator_set.go +++ b/module/validator_set.go @@ -3,6 +3,7 @@ package module import ( "context" "fmt" + "github.com/ethereum/go-ethereum/core/types" ) @@ -12,8 +13,8 @@ func (v Validators) Checkpoint(turnLength uint8) uint64 { return uint64(len(v)/2+1) * uint64(turnLength) } -func queryValidatorSetAndTurnLength(fn getHeaderFn, epochBlockNumber uint64) (Validators, uint8, error) { - header, err := fn(context.TODO(), epochBlockNumber) +func queryValidatorSetAndTurnLength(ctx context.Context, fn getHeaderFn, epochBlockNumber uint64) (Validators, uint8, error) { + header, err := fn(ctx, epochBlockNumber) if err != nil { return nil, 1, err } From 04ca9f2e2f88f9880ec04e287ea212068b2e7c09 Mon Sep 17 00:00:00 2001 From: Takeshi Arabiki Date: Fri, 14 Mar 2025 11:30:10 +0900 Subject: [PATCH 4/7] Add context to functions in proof.go Signed-off-by: Takeshi Arabiki --- module/proof.go | 18 +++++++++--------- module/prover.go | 4 ++-- .../internal/membership/verify_membership.go | 9 +++++---- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/module/proof.go b/module/proof.go index 2f4fa1f0..376eb8a2 100644 --- a/module/proof.go +++ b/module/proof.go @@ -18,9 +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( - context.TODO(), + ctx, pr.chain.IBCAddress(), nil, big.NewInt(0).SetUint64(height), @@ -31,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(), @@ -44,7 +44,7 @@ func (pr *Prover) getStateCommitmentProof(path []byte, height exported.Height) ( // call eth_getProof stateProof, err := pr.chain.GetProof( - context.TODO(), + ctx, pr.chain.IBCAddress(), [][]byte{storageKeyHex}, big.NewInt(int64(height.GetRevisionHeight())), @@ -56,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 } @@ -174,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, @@ -184,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(context.TODO(), 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(context.TODO(), 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) diff --git a/module/prover.go b/module/prover.go index 989473dd..854a8017 100644 --- a/module/prover.go +++ b/module/prover.go @@ -143,7 +143,7 @@ func (pr *Prover) SetupHeadersForUpdateByLatestHeight(ctx context.Context, clien func (pr *Prover) ProveState(ctx core.QueryContext, path string, value []byte) ([]byte, clienttypes.Height, error) { proofHeight := toHeight(ctx.Height()) - accountProof, commitmentProof, err := pr.getStateCommitmentProof([]byte(path), proofHeight) + accountProof, commitmentProof, err := pr.getStateCommitmentProof(context.TODO(), []byte(path), proofHeight) if err != nil { return nil, proofHeight, err } @@ -227,7 +227,7 @@ func (pr *Prover) CheckRefreshRequired(ctx context.Context, counterparty core.Ch } func (pr *Prover) withValidators(height uint64, ethHeaders []*ETHHeader) (core.Header, error) { - return withValidators(pr.chain.Header, height, ethHeaders) + return withValidators(context.TODO(), pr.chain.Header, height, ethHeaders) } func (pr *Prover) buildInitialState(dstHeader core.Header) (exported.ClientState, exported.ConsensusState, error) { diff --git a/tool/testdata/internal/membership/verify_membership.go b/tool/testdata/internal/membership/verify_membership.go index 29fb9a5e..bee8aed1 100644 --- a/tool/testdata/internal/membership/verify_membership.go +++ b/tool/testdata/internal/membership/verify_membership.go @@ -2,6 +2,10 @@ package membership import ( "context" + "log" + "math/big" + "os" + "github.com/cosmos/ibc-go/v8/modules/core/02-client/types" conntypes "github.com/cosmos/ibc-go/v8/modules/core/03-connection/types" types3 "github.com/cosmos/ibc-go/v8/modules/core/23-commitment/types" @@ -12,9 +16,6 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/hyperledger-labs/yui-relayer/core" "github.com/spf13/cobra" - "log" - "math/big" - "os" ) type verifyMembershipModule struct { @@ -89,7 +90,7 @@ func (m *verifyMembershipModule) proveState(chainID uint64, path string, value [ } proof, proofHeight, err := prover.ProveState(ctx, path, value) - storageRoot, err := prover.GetStorageRoot(header) + storageRoot, err := prover.GetStorageRoot(context.TODO(), header) if err != nil { return common.Hash{}, nil, types.Height{}, err } From 065cc1a3c4e8f7a4610a28d331a1b7969d121952 Mon Sep 17 00:00:00 2001 From: Takeshi Arabiki Date: Fri, 14 Mar 2025 11:56:14 +0900 Subject: [PATCH 5/7] Add context to functions in setup.go Signed-off-by: Takeshi Arabiki --- module/prover.go | 5 +++-- module/setup.go | 10 ++++++---- module/setup_test.go | 17 +++++++++-------- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/module/prover.go b/module/prover.go index 854a8017..c18d7e93 100644 --- a/module/prover.go +++ b/module/prover.go @@ -118,8 +118,8 @@ func (pr *Prover) SetupHeadersForUpdate(ctx context.Context, counterparty core.F } func (pr *Prover) SetupHeadersForUpdateByLatestHeight(ctx context.Context, clientStateLatestHeight exported.Height, latestFinalizedHeader *Header) ([]core.Header, error) { - queryVerifiableNeighboringEpochHeader := func(height uint64, limitHeight uint64) (core.Header, error) { - ethHeaders, err := queryFinalizedHeader(context.TODO(), pr.chain.Header, height, limitHeight) + queryVerifiableNeighboringEpochHeader := func(ctx context.Context, height uint64, limitHeight uint64) (core.Header, error) { + ethHeaders, err := queryFinalizedHeader(ctx, pr.chain.Header, height, limitHeight) if err != nil { return nil, err } @@ -134,6 +134,7 @@ func (pr *Prover) SetupHeadersForUpdateByLatestHeight(ctx context.Context, clien return nil, err } return setupHeadersForUpdate( + context.TODO(), queryVerifiableNeighboringEpochHeader, pr.chain.Header, clientStateLatestHeight, diff --git a/module/setup.go b/module/setup.go index bcb844cf..689fa600 100644 --- a/module/setup.go +++ b/module/setup.go @@ -11,9 +11,10 @@ import ( "github.com/hyperledger-labs/yui-relayer/log" ) -type queryVerifiableNeighboringEpochHeaderFn = func(uint64, uint64) (core.Header, error) +type queryVerifiableNeighboringEpochHeaderFn = func(context.Context, uint64, uint64) (core.Header, error) func setupHeadersForUpdate( + ctx context.Context, queryVerifiableNeighboringEpochHeader queryVerifiableNeighboringEpochHeaderFn, getHeader getHeaderFn, clientStateLatestHeight exported.Height, @@ -39,7 +40,7 @@ func setupHeadersForUpdate( // Append insufficient epoch blocks for epochHeight := firstUnsavedEpoch; epochHeight < latestFinalizedHeight; epochHeight += constant.BlocksPerEpoch { - verifiableEpoch, err := setupNeighboringEpochHeader(getHeader, queryVerifiableNeighboringEpochHeader, epochHeight, trustedEpochHeight, latestHeight) + verifiableEpoch, err := setupNeighboringEpochHeader(ctx, getHeader, queryVerifiableNeighboringEpochHeader, epochHeight, trustedEpochHeight, latestHeight) if err != nil { return nil, err } @@ -55,6 +56,7 @@ func setupHeadersForUpdate( } func setupNeighboringEpochHeader( + ctx context.Context, getHeader getHeaderFn, queryVerifiableHeader queryVerifiableNeighboringEpochHeaderFn, epochHeight uint64, @@ -62,14 +64,14 @@ func setupNeighboringEpochHeader( latestHeight exported.Height, ) (core.Header, error) { // neighboring epoch needs block before checkpoint - currentValidatorSet, currentTurnLength, err := queryValidatorSetAndTurnLength(context.TODO(), getHeader, epochHeight) + currentValidatorSet, currentTurnLength, err := queryValidatorSetAndTurnLength(ctx, getHeader, epochHeight) if err != nil { return nil, fmt.Errorf("setupNeighboringEpochHeader: failed to get current validator set: epochHeight=%d : %+v", epochHeight, err) } // ex) trusted(prevSaved = 200), epochHeight = 400 must be finalized by min(610,latest) nextCheckpoint := currentValidatorSet.Checkpoint(currentTurnLength) + (epochHeight + constant.BlocksPerEpoch) limit := minUint64(nextCheckpoint-1, latestHeight.GetRevisionHeight()) - return queryVerifiableHeader(epochHeight, limit) + return queryVerifiableHeader(ctx, epochHeight, limit) } func withTrustedHeight(targetHeaders []core.Header, clientStateLatestHeight exported.Height) []core.Header { diff --git a/module/setup_test.go b/module/setup_test.go index 161b5e61..5ee37bf9 100644 --- a/module/setup_test.go +++ b/module/setup_test.go @@ -2,14 +2,15 @@ package module import ( "context" + "math/big" + "testing" + clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types" "github.com/datachainlab/ibc-parlia-relay/module/constant" types2 "github.com/ethereum/go-ethereum/core/types" "github.com/hyperledger-labs/yui-relayer/core" "github.com/hyperledger-labs/yui-relayer/log" "github.com/stretchr/testify/suite" - "math/big" - "testing" ) type SetupTestSuite struct { @@ -38,7 +39,7 @@ func (ts *SetupTestSuite) TestSuccess_setupHeadersForUpdate_neighboringEpoch() { CurrentValidators: [][]byte{{1}}, PreviousValidators: [][]byte{{1}}, } - neighborFn := func(height uint64, _ uint64) (core.Header, error) { + neighborFn := func(_ context.Context, height uint64, _ uint64) (core.Header, error) { h, e := newETHHeader(&types2.Header{ Number: big.NewInt(int64(height)), }) @@ -53,7 +54,7 @@ func (ts *SetupTestSuite) TestSuccess_setupHeadersForUpdate_neighboringEpoch() { }, nil } - targets, err := setupHeadersForUpdate(neighborFn, headerFn, clientStateLatestHeight, latestFinalizedHeader, clienttypes.NewHeight(0, 100000)) + targets, err := setupHeadersForUpdate(context.Background(), neighborFn, headerFn, clientStateLatestHeight, latestFinalizedHeader, clienttypes.NewHeight(0, 100000)) ts.Require().NoError(err) ts.Require().Len(targets, expected) for i, h := range targets { @@ -101,7 +102,7 @@ func (ts *SetupTestSuite) TestSuccess_setupHeadersForUpdate_allEmpty() { latestFinalizedHeader := &Header{ Headers: []*ETHHeader{target}, } - neighboringEpochFn := func(height uint64, _ uint64) (core.Header, error) { + neighboringEpochFn := func(_ context.Context, height uint64, _ uint64) (core.Header, error) { // insufficient vote attestation return nil, nil } @@ -111,7 +112,7 @@ func (ts *SetupTestSuite) TestSuccess_setupHeadersForUpdate_allEmpty() { Extra: epochHeader().Extra, }, nil } - targets, err := setupHeadersForUpdate(neighboringEpochFn, headerFn, clientStateLatestHeight, latestFinalizedHeader, clienttypes.NewHeight(0, 1000000)) + targets, err := setupHeadersForUpdate(context.Background(), neighboringEpochFn, headerFn, clientStateLatestHeight, latestFinalizedHeader, clienttypes.NewHeight(0, 1000000)) ts.Require().NoError(err) ts.Require().Len(targets, expected) } @@ -144,7 +145,7 @@ func (ts *SetupTestSuite) TestSuccess_setupNeighboringEpochHeader() { epochHeight := uint64(400) trustedEpochHeight := uint64(200) - neighboringEpochFn := func(height uint64, limit uint64) (core.Header, error) { + neighboringEpochFn := func(_ context.Context, height uint64, limit uint64) (core.Header, error) { target, err := newETHHeader(&types2.Header{ Number: big.NewInt(int64(limit)), }) @@ -156,7 +157,7 @@ func (ts *SetupTestSuite) TestSuccess_setupNeighboringEpochHeader() { headerFn := func(_ context.Context, height uint64) (*types2.Header, error) { return headerByHeight(int64(height)), nil } - hs, err := setupNeighboringEpochHeader(headerFn, neighboringEpochFn, epochHeight, trustedEpochHeight, clienttypes.NewHeight(0, 10000)) + hs, err := setupNeighboringEpochHeader(context.Background(), headerFn, neighboringEpochFn, epochHeight, trustedEpochHeight, clienttypes.NewHeight(0, 10000)) ts.Require().NoError(err) target, err := hs.(*Header).Target() ts.Require().NoError(err) From be3012fcb9a0be02c6dc9fe7ebf968c0f115939d Mon Sep 17 00:00:00 2001 From: Takeshi Arabiki Date: Fri, 14 Mar 2025 12:21:48 +0900 Subject: [PATCH 6/7] Replace context.TODO() with appropriate context Signed-off-by: Takeshi Arabiki --- module/facade.go | 8 +-- module/prover.go | 50 +++++++++---------- module/prover_test.go | 15 +++--- module/validator_set_test.go | 9 ++-- tests/prover_network_test.go | 17 ++++--- .../internal/membership/verify_membership.go | 2 +- 6 files changed, 52 insertions(+), 49 deletions(-) diff --git a/module/facade.go b/module/facade.go index 43bb1d0a..ff50225d 100644 --- a/module/facade.go +++ b/module/facade.go @@ -16,12 +16,12 @@ func GetCurrentEpoch(v uint64) uint64 { return getCurrentEpoch(v) } -func QueryFinalizedHeader(fn getHeaderFn, height uint64, limitHeight uint64) ([]*ETHHeader, error) { - return queryFinalizedHeader(context.TODO(), 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(context.TODO(), 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) { diff --git a/module/prover.go b/module/prover.go index c18d7e93..e6e7f481 100644 --- a/module/prover.go +++ b/module/prover.go @@ -52,15 +52,15 @@ func (pr *Prover) SetupForRelay(ctx context.Context) error { // These states will be submitted to the counterparty chain as MsgCreateClient. // If `height` is nil, the latest finalized height is selected automatically. func (pr *Prover) CreateInitialLightClientState(ctx context.Context, height exported.Height) (exported.ClientState, exported.ConsensusState, error) { - latestHeight, err := pr.chain.LatestHeight(context.TODO()) + latestHeight, err := pr.chain.LatestHeight(ctx) if err != nil { return nil, nil, err } var finalizedHeader []*ETHHeader if height == nil { - _, finalizedHeader, err = queryLatestFinalizedHeader(context.TODO(), pr.chain.Header, latestHeight.GetRevisionHeight()) + _, finalizedHeader, err = queryLatestFinalizedHeader(ctx, pr.chain.Header, latestHeight.GetRevisionHeight()) } else { - finalizedHeader, err = queryFinalizedHeader(context.TODO(), pr.chain.Header, height.GetRevisionHeight(), latestHeight.GetRevisionHeight()) + finalizedHeader, err = queryFinalizedHeader(ctx, pr.chain.Header, height.GetRevisionHeight(), latestHeight.GetRevisionHeight()) } if err != nil { return nil, nil, err @@ -69,18 +69,18 @@ func (pr *Prover) CreateInitialLightClientState(ctx context.Context, height expo return nil, nil, fmt.Errorf("no finalized headers were found up to %d", latestHeight.GetRevisionHeight()) } //Header should be Finalized, not necessarily Verifiable. - return pr.buildInitialState(&Header{ + return pr.buildInitialState(ctx, &Header{ Headers: finalizedHeader, }) } // GetLatestFinalizedHeader returns the latest finalized header from the chain func (pr *Prover) GetLatestFinalizedHeader(ctx context.Context) (out core.Header, err error) { - latestHeight, err := pr.chain.LatestHeight(context.TODO()) + latestHeight, err := pr.chain.LatestHeight(ctx) if err != nil { return nil, err } - header, err := pr.GetLatestFinalizedHeaderByLatestHeight(context.TODO(), latestHeight.GetRevisionHeight()) + header, err := pr.GetLatestFinalizedHeaderByLatestHeight(ctx, latestHeight.GetRevisionHeight()) if err != nil { return nil, err } @@ -90,23 +90,23 @@ func (pr *Prover) GetLatestFinalizedHeader(ctx context.Context) (out core.Header // GetLatestFinalizedHeaderByLatestHeight returns the latest finalized verifiable header from the chain func (pr *Prover) GetLatestFinalizedHeaderByLatestHeight(ctx context.Context, latestBlockNumber uint64) (core.Header, error) { - height, finalizedHeader, err := queryLatestFinalizedHeader(context.TODO(), pr.chain.Header, latestBlockNumber) + height, finalizedHeader, err := queryLatestFinalizedHeader(ctx, pr.chain.Header, latestBlockNumber) if err != nil { return nil, err } // Make headers verifiable - return pr.withValidators(height, finalizedHeader) + return pr.withValidators(ctx, height, finalizedHeader) } // SetupHeadersForUpdate creates a new header based on a given header func (pr *Prover) SetupHeadersForUpdate(ctx context.Context, counterparty core.FinalityAwareChain, latestFinalizedHeader core.Header) ([]core.Header, error) { header := latestFinalizedHeader.(*Header) // LCP doesn't need height / EVM needs latest height - latestHeightOnDstChain, err := counterparty.LatestHeight(context.TODO()) + latestHeightOnDstChain, err := counterparty.LatestHeight(ctx) if err != nil { return nil, err } - csRes, err := counterparty.QueryClientState(core.NewQueryContext(context.TODO(), latestHeightOnDstChain)) + csRes, err := counterparty.QueryClientState(core.NewQueryContext(ctx, latestHeightOnDstChain)) if err != nil { return nil, fmt.Errorf("no client state found : SetupHeadersForUpdate: height = %d, %+v", latestHeightOnDstChain.GetRevisionHeight(), err) } @@ -114,7 +114,7 @@ func (pr *Prover) SetupHeadersForUpdate(ctx context.Context, counterparty core.F if err = pr.chain.Codec().UnpackAny(csRes.ClientState, &cs); err != nil { return nil, err } - return pr.SetupHeadersForUpdateByLatestHeight(context.TODO(), cs.GetLatestHeight(), header) + return pr.SetupHeadersForUpdateByLatestHeight(ctx, cs.GetLatestHeight(), header) } func (pr *Prover) SetupHeadersForUpdateByLatestHeight(ctx context.Context, clientStateLatestHeight exported.Height, latestFinalizedHeader *Header) ([]core.Header, error) { @@ -127,14 +127,14 @@ func (pr *Prover) SetupHeadersForUpdateByLatestHeight(ctx context.Context, clien if ethHeaders == nil { return nil, nil } - return pr.withValidators(height, ethHeaders) + return pr.withValidators(ctx, height, ethHeaders) } - latestHeight, err := pr.chain.LatestHeight(context.TODO()) + latestHeight, err := pr.chain.LatestHeight(ctx) if err != nil { return nil, err } return setupHeadersForUpdate( - context.TODO(), + ctx, queryVerifiableNeighboringEpochHeader, pr.chain.Header, clientStateLatestHeight, @@ -144,7 +144,7 @@ func (pr *Prover) SetupHeadersForUpdateByLatestHeight(ctx context.Context, clien func (pr *Prover) ProveState(ctx core.QueryContext, path string, value []byte) ([]byte, clienttypes.Height, error) { proofHeight := toHeight(ctx.Height()) - accountProof, commitmentProof, err := pr.getStateCommitmentProof(context.TODO(), []byte(path), proofHeight) + accountProof, commitmentProof, err := pr.getStateCommitmentProof(ctx.Context(), []byte(path), proofHeight) if err != nil { return nil, proofHeight, err } @@ -161,11 +161,11 @@ func (pr *Prover) ProveState(ctx core.QueryContext, path string, value []byte) ( } func (pr *Prover) CheckRefreshRequired(ctx context.Context, counterparty core.ChainInfoICS02Querier) (bool, error) { - cpQueryHeight, err := counterparty.LatestHeight(context.TODO()) + cpQueryHeight, err := counterparty.LatestHeight(ctx) if err != nil { return false, fmt.Errorf("failed to get the latest height of the counterparty chain: %+v", err) } - cpQueryCtx := core.NewQueryContext(context.TODO(), cpQueryHeight) + cpQueryCtx := core.NewQueryContext(ctx, cpQueryHeight) resCs, err := counterparty.QueryClientState(cpQueryCtx) if err != nil { @@ -188,12 +188,12 @@ func (pr *Prover) CheckRefreshRequired(ctx context.Context, counterparty core.Ch } lcLastTimestamp := time.Unix(0, int64(cons.GetTimestamp())) - selfQueryHeight, err := pr.chain.LatestHeight(context.TODO()) + selfQueryHeight, err := pr.chain.LatestHeight(ctx) if err != nil { return false, fmt.Errorf("failed to get the latest height of the self chain: %+v", err) } - selfTimestamp, err := pr.chain.Timestamp(context.TODO(), selfQueryHeight) + selfTimestamp, err := pr.chain.Timestamp(ctx, selfQueryHeight) if err != nil { return false, fmt.Errorf("failed to get timestamp of the self chain: %+v", err) } @@ -227,19 +227,19 @@ func (pr *Prover) CheckRefreshRequired(ctx context.Context, counterparty core.Ch } -func (pr *Prover) withValidators(height uint64, ethHeaders []*ETHHeader) (core.Header, error) { - return withValidators(context.TODO(), pr.chain.Header, height, ethHeaders) +func (pr *Prover) withValidators(ctx context.Context, height uint64, ethHeaders []*ETHHeader) (core.Header, error) { + return withValidators(ctx, pr.chain.Header, height, ethHeaders) } -func (pr *Prover) buildInitialState(dstHeader core.Header) (exported.ClientState, exported.ConsensusState, error) { +func (pr *Prover) buildInitialState(ctx context.Context, dstHeader core.Header) (exported.ClientState, exported.ConsensusState, error) { currentEpoch := getCurrentEpoch(dstHeader.GetHeight().GetRevisionHeight()) - currentValidators, currentTurnLength, err := queryValidatorSetAndTurnLength(context.TODO(), pr.chain.Header, currentEpoch) + currentValidators, currentTurnLength, err := queryValidatorSetAndTurnLength(ctx, pr.chain.Header, currentEpoch) if err != nil { return nil, nil, err } previousEpoch := getPreviousEpoch(dstHeader.GetHeight().GetRevisionHeight()) - previousValidators, previousTurnLength, err := queryValidatorSetAndTurnLength(context.TODO(), pr.chain.Header, previousEpoch) + previousValidators, previousTurnLength, err := queryValidatorSetAndTurnLength(ctx, pr.chain.Header, previousEpoch) if err != nil { return nil, nil, err } @@ -248,7 +248,7 @@ func (pr *Prover) buildInitialState(dstHeader core.Header) (exported.ClientState return nil, nil, err } - chainID, err := pr.chain.CanonicalChainID(context.TODO()) + chainID, err := pr.chain.CanonicalChainID(ctx) if err != nil { return nil, nil, err } diff --git a/module/prover_test.go b/module/prover_test.go index 7ccad211..951900f7 100644 --- a/module/prover_test.go +++ b/module/prover_test.go @@ -156,7 +156,7 @@ func (ts *ProverTestSuite) TestQueryClientStateWithProof() { bzCs, err := ts.prover.chain.Codec().Marshal(cs) ts.Require().NoError(err) - ctx := core.NewQueryContext(context.TODO(), clienttypes.NewHeight(0, 21400)) + ctx := core.NewQueryContext(context.Background(), clienttypes.NewHeight(0, 21400)) proof, proofHeight, err := ts.prover.ProveState(ctx, host.FullClientStatePath(ts.prover.chain.Path().ClientID), bzCs) ts.Require().NoError(err) @@ -248,6 +248,7 @@ func (ts *ProverTestSuite) TestCheckRefreshRequired() { ts.chain.trustedHeight = 0 }() + ctx := context.Background() now := time.Now() chainHeight := clienttypes.NewHeight(0, 0) csHeight := clienttypes.NewHeight(0, 0) @@ -255,26 +256,26 @@ func (ts *ProverTestSuite) TestCheckRefreshRequired() { // should refresh by trusting_period ts.chain.consensusStateTimestamp[csHeight] = uint64(now.Add(-51 * time.Second).UnixNano()) - required, err := ts.prover.CheckRefreshRequired(context.TODO(), dst) + required, err := ts.prover.CheckRefreshRequired(ctx, dst) ts.Require().NoError(err) ts.Require().True(required) // needless by trusting_period ts.chain.consensusStateTimestamp[csHeight] = uint64(now.Add(-50 * time.Second).UnixNano()) - required, err = ts.prover.CheckRefreshRequired(context.TODO(), dst) + required, err = ts.prover.CheckRefreshRequired(ctx, dst) ts.Require().NoError(err) ts.Require().False(required) // should refresh by block difference ts.chain.latestHeight = 2 ts.prover.config.RefreshBlockDifferenceThreshold = 1 - required, err = ts.prover.CheckRefreshRequired(context.TODO(), dst) + required, err = ts.prover.CheckRefreshRequired(ctx, dst) ts.Require().NoError(err) ts.Require().True(required) // needless by block difference ts.prover.config.RefreshBlockDifferenceThreshold = 2 - required, err = ts.prover.CheckRefreshRequired(context.TODO(), dst) + required, err = ts.prover.CheckRefreshRequired(ctx, dst) ts.Require().NoError(err) ts.Require().False(required) @@ -282,7 +283,7 @@ func (ts *ProverTestSuite) TestCheckRefreshRequired() { ts.chain.latestHeight = 1 ts.chain.trustedHeight = 3 ts.prover.config.RefreshBlockDifferenceThreshold = 1 - required, err = ts.prover.CheckRefreshRequired(context.TODO(), dst) + required, err = ts.prover.CheckRefreshRequired(ctx, dst) ts.Require().NoError(err) ts.Require().False(required) } @@ -299,7 +300,7 @@ func (ts *ProverTestSuite) TestProveHostConsensusState() { (*exported.ConsensusState)(nil), &ConsensusState{}, ) - ctx := core.NewQueryContext(context.TODO(), clienttypes.NewHeight(0, 0)) + ctx := core.NewQueryContext(context.Background(), clienttypes.NewHeight(0, 0)) prove, err := ts.prover.ProveHostConsensusState(ctx, clienttypes.NewHeight(0, 0), &cs) ts.Require().NoError(err) ts.Require().Len(prove, 150) diff --git a/module/validator_set_test.go b/module/validator_set_test.go index 0a0c0dd7..890e67c5 100644 --- a/module/validator_set_test.go +++ b/module/validator_set_test.go @@ -3,10 +3,11 @@ package module import ( "context" "errors" - "github.com/ethereum/go-ethereum/core/types" - "github.com/stretchr/testify/suite" "math/big" "testing" + + "github.com/ethereum/go-ethereum/core/types" + "github.com/stretchr/testify/suite" ) type ValidatorSetTestSuite struct { @@ -54,7 +55,7 @@ func (ts *ValidatorSetTestSuite) TestSuccessQueryValidatorSet() { fn := func(ctx context.Context, height uint64) (*types.Header, error) { return epochHeader(), nil } - validators, turnLength, err := QueryValidatorSetAndTurnLength(fn, 400) + validators, turnLength, err := QueryValidatorSetAndTurnLength(context.Background(), fn, 400) ts.Require().NoError(err) ts.Require().Len(validators, 4) ts.Require().Equal(turnLength, uint8(1)) @@ -64,7 +65,7 @@ func (ts *ValidatorSetTestSuite) TestErrorQueryValidatorSet() { fn := func(ctx context.Context, height uint64) (*types.Header, error) { return nil, errors.New("error") } - _, _, err := QueryValidatorSetAndTurnLength(fn, 200) + _, _, err := QueryValidatorSetAndTurnLength(context.Background(), fn, 200) ts.Require().Equal(err.Error(), "error") } diff --git a/tests/prover_network_test.go b/tests/prover_network_test.go index 630bca49..2b498a34 100644 --- a/tests/prover_network_test.go +++ b/tests/prover_network_test.go @@ -47,7 +47,7 @@ func (ts *ProverNetworkTestSuite) SetupTest() { } func (ts *ProverNetworkTestSuite) TestQueryLatestFinalizedHeader() { - header, err := ts.prover.GetLatestFinalizedHeader(context.TODO()) + header, err := ts.prover.GetLatestFinalizedHeader(context.Background()) ts.Require().NoError(err) ts.Require().NoError(header.ValidateBasic()) ts.Require().Len(header.(*module.Header).Headers, 3) @@ -65,9 +65,9 @@ func (ts *ProverNetworkTestSuite) TestSetupHeadersForUpdate() { dst := dstChain{ Chain: ts.makeChain("http://localhost:8645", "ibc0"), } - header, err := ts.prover.GetLatestFinalizedHeader(context.TODO()) + header, err := ts.prover.GetLatestFinalizedHeader(context.Background()) ts.Require().NoError(err) - setupDone, err := ts.prover.SetupHeadersForUpdate(context.TODO(), dst, header) + setupDone, err := ts.prover.SetupHeadersForUpdate(context.Background(), dst, header) ts.Require().NoError(err) ts.Require().True(len(setupDone) > 0) for _, h := range setupDone { @@ -76,7 +76,8 @@ func (ts *ProverNetworkTestSuite) TestSetupHeadersForUpdate() { } func (ts *ProverNetworkTestSuite) TestSuccessCreateInitialLightClientState() { - s1, s2, err := ts.prover.CreateInitialLightClientState(context.TODO(), nil) + ctx := context.Background() + s1, s2, err := ts.prover.CreateInitialLightClientState(ctx, nil) ts.Require().NoError(err) cs := s1.(*module.ClientState) @@ -87,13 +88,13 @@ func (ts *ProverNetworkTestSuite) TestSuccessCreateInitialLightClientState() { ts.Require().Equal(common.Bytes2Hex(cs.IbcStoreAddress), strings.ToLower(ts.chain.IBCAddress().String()[2:])) ts.Require().Equal(common.Bytes2Hex(cs.IbcCommitmentsSlot), common.Bytes2Hex(module.IBCCommitmentsSlot[:])) - header, err := ts.chain.Header(context.Background(), cs.GetLatestHeight().GetRevisionHeight()) + header, err := ts.chain.Header(ctx, cs.GetLatestHeight().GetRevisionHeight()) ts.Require().NoError(err) ts.Require().Equal(cs.GetLatestHeight().GetRevisionHeight(), header.Number.Uint64()) - cVal, cTurn, err := module.QueryValidatorSetAndTurnLength(ts.chain.Header, module.GetCurrentEpoch(header.Number.Uint64())) + cVal, cTurn, err := module.QueryValidatorSetAndTurnLength(ctx, ts.chain.Header, module.GetCurrentEpoch(header.Number.Uint64())) ts.Require().NoError(err) - pVal, pTurn, err := module.QueryValidatorSetAndTurnLength(ts.chain.Header, module.GetPreviousEpoch(header.Number.Uint64())) + pVal, pTurn, err := module.QueryValidatorSetAndTurnLength(ctx, ts.chain.Header, module.GetPreviousEpoch(header.Number.Uint64())) ts.Require().NoError(err) consState := s2.(*module.ConsensusState) ts.Require().Equal(consState.CurrentValidatorsHash, module.MakeEpochHash(cVal, cTurn)) @@ -109,7 +110,7 @@ func (ts *ProverNetworkTestSuite) makeChain(rpcAddr string, ibcChainID string) m } anySignerConfig, err := codectypes.NewAnyWithValue(signerConfig) ts.Require().NoError(err) - chain, err := ethereum.NewChain(ethereum.ChainConfig{ + chain, err := ethereum.NewChain(context.Background(), ethereum.ChainConfig{ EthChainId: 9999, IbcAddress: "0xaa43d337145E8930d01cb4E60Abf6595C692921E", Signer: anySignerConfig, diff --git a/tool/testdata/internal/membership/verify_membership.go b/tool/testdata/internal/membership/verify_membership.go index bee8aed1..c21f2d1d 100644 --- a/tool/testdata/internal/membership/verify_membership.go +++ b/tool/testdata/internal/membership/verify_membership.go @@ -90,7 +90,7 @@ func (m *verifyMembershipModule) proveState(chainID uint64, path string, value [ } proof, proofHeight, err := prover.ProveState(ctx, path, value) - storageRoot, err := prover.GetStorageRoot(context.TODO(), header) + storageRoot, err := prover.GetStorageRoot(ctx.Context(), header) if err != nil { return common.Hash{}, nil, types.Height{}, err } From 1b58af6d231ac72b333dabda41b74b5bef27eb4d Mon Sep 17 00:00:00 2001 From: Takeshi Arabiki Date: Fri, 14 Mar 2025 18:55:21 +0900 Subject: [PATCH 7/7] Fix tool/testdata Signed-off-by: Takeshi Arabiki --- tool/testdata/internal/common.go | 10 +++-- tool/testdata/internal/create_client.go | 11 ++--- tool/testdata/internal/header.go | 9 ++-- tool/testdata/internal/histroy.go | 36 ++++++++-------- .../internal/membership/verify_membership.go | 16 +++---- tool/testdata/internal/misbehavior.go | 30 ++++++------- tool/testdata/internal/update_client.go | 42 ++++++++++--------- 7 files changed, 82 insertions(+), 72 deletions(-) diff --git a/tool/testdata/internal/common.go b/tool/testdata/internal/common.go index de0d9ae4..f3a43aa6 100644 --- a/tool/testdata/internal/common.go +++ b/tool/testdata/internal/common.go @@ -1,14 +1,16 @@ package internal import ( + "context" "fmt" + "os" + "time" + "github.com/cosmos/cosmos-sdk/codec/types" "github.com/datachainlab/ethereum-ibc-relay-chain/pkg/relay/ethereum" "github.com/datachainlab/ibc-hd-signer/pkg/hd" "github.com/datachainlab/ibc-parlia-relay/module" "github.com/spf13/viper" - "os" - "time" ) const ( @@ -36,12 +38,12 @@ func CreateSignerConfig() *types.Any { return anySignerConfig } -func createProver() (*module.Prover, module.Chain, error) { +func createProver(ctx context.Context) (*module.Prover, module.Chain, error) { rpcAddr, err := createRPCAddr() if err != nil { return nil, nil, err } - chain, err := ethereum.NewChain(ethereum.ChainConfig{ + chain, err := ethereum.NewChain(ctx, ethereum.ChainConfig{ EthChainId: 9999, RpcAddr: rpcAddr, Signer: CreateSignerConfig(), diff --git a/tool/testdata/internal/create_client.go b/tool/testdata/internal/create_client.go index 37aa475c..1e639f64 100644 --- a/tool/testdata/internal/create_client.go +++ b/tool/testdata/internal/create_client.go @@ -1,12 +1,13 @@ package internal import ( + "log" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/gogoproto/proto" "github.com/datachainlab/ibc-parlia-relay/module" "github.com/ethereum/go-ethereum/common" "github.com/spf13/cobra" - "log" ) type createClientModule struct { @@ -16,11 +17,11 @@ func (m *createClientModule) createClientSuccessCmd() *cobra.Command { cmd := &cobra.Command{ Use: "success", RunE: func(cmd *cobra.Command, args []string) error { - prover, chain, err := createProver() + prover, chain, err := createProver(cmd.Context()) if err != nil { return err } - cs, consState, err := prover.CreateInitialLightClientState(nil) + cs, consState, err := prover.CreateInitialLightClientState(cmd.Context(), nil) if err != nil { return err } @@ -41,11 +42,11 @@ func (m *createClientModule) createClientSuccessCmd() *cobra.Command { if err != nil { return err } - currentValidatorSet, currentTurnLength, err := module.QueryValidatorSetAndTurnLength(chain.Header, module.GetCurrentEpoch(cs.GetLatestHeight().GetRevisionHeight())) + currentValidatorSet, currentTurnLength, err := module.QueryValidatorSetAndTurnLength(cmd.Context(), chain.Header, module.GetCurrentEpoch(cs.GetLatestHeight().GetRevisionHeight())) if err != nil { return err } - previousValidatorSet, previousTurnLength, err := module.QueryValidatorSetAndTurnLength(chain.Header, module.GetPreviousEpoch(cs.GetLatestHeight().GetRevisionHeight())) + previousValidatorSet, previousTurnLength, err := module.QueryValidatorSetAndTurnLength(cmd.Context(), chain.Header, module.GetPreviousEpoch(cs.GetLatestHeight().GetRevisionHeight())) if err != nil { return err } diff --git a/tool/testdata/internal/header.go b/tool/testdata/internal/header.go index d63d8c8e..6b091aac 100644 --- a/tool/testdata/internal/header.go +++ b/tool/testdata/internal/header.go @@ -2,13 +2,14 @@ package internal import ( "context" + "log" + "github.com/datachainlab/ibc-parlia-relay/module" "github.com/datachainlab/ibc-parlia-relay/module/constant" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/rlp" "github.com/pkg/errors" "github.com/spf13/cobra" - "log" ) type headerModule struct { @@ -21,11 +22,11 @@ func (m *headerModule) success() *cobra.Command { cmd.AddCommand(&cobra.Command{ Use: "latest", RunE: func(cmd *cobra.Command, args []string) error { - _, chain, err := createProver() + _, chain, err := createProver(cmd.Context()) if err != nil { return err } - latest, err := chain.LatestHeight() + latest, err := chain.LatestHeight(cmd.Context()) if err != nil { return err } @@ -37,7 +38,7 @@ func (m *headerModule) success() *cobra.Command { specified := &cobra.Command{ Use: "specified", RunE: func(cmd *cobra.Command, args []string) error { - _, chain, err := createProver() + _, chain, err := createProver(cmd.Context()) if err != nil { return err } diff --git a/tool/testdata/internal/histroy.go b/tool/testdata/internal/histroy.go index 6f59ad83..c4b55cca 100644 --- a/tool/testdata/internal/histroy.go +++ b/tool/testdata/internal/histroy.go @@ -1,6 +1,11 @@ package internal import ( + "context" + "log" + "os" + "time" + "github.com/cometbft/cometbft/libs/json" codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/ibc-go/v8/modules/core/02-client/types" @@ -9,9 +14,6 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/golang/protobuf/proto" "github.com/spf13/cobra" - "log" - "os" - "time" ) type historyModule struct { @@ -24,21 +26,21 @@ func (m *historyModule) mainnet() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { log.Printf("num = %d\n", num) - prover, chain, err := createProver() + prover, chain, err := createProver(cmd.Context()) if err != nil { return err } - latest, err := chain.LatestHeight() + latest, err := chain.LatestHeight(cmd.Context()) if err != nil { return err } - createdEpoch, err := m.outputMsgClient(prover, latest.GetRevisionHeight()-num, "create_mainnet.json") + createdEpoch, err := m.outputMsgClient(cmd.Context(), prover, latest.GetRevisionHeight()-num, "create_mainnet.json") if err != nil { return err } - return m.outputMsgUpdate(prover, createdEpoch, latest.GetRevisionHeight(), num, "update_mainnet.json") + return m.outputMsgUpdate(cmd.Context(), prover, createdEpoch, latest.GetRevisionHeight(), num, "update_mainnet.json") }, } cmd.Flags().Uint64Var(&num, "num", 240, "--num") @@ -52,28 +54,28 @@ func (m *historyModule) testnet() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { log.Printf("num = %d\n", num) - prover, chain, err := createProver() + prover, chain, err := createProver(cmd.Context()) if err != nil { return err } - latest, err := chain.LatestHeight() + latest, err := chain.LatestHeight(cmd.Context()) if err != nil { return err } - createdEpoch, err := m.outputMsgClient(prover, latest.GetRevisionHeight()-num, "create_testnet.json") + createdEpoch, err := m.outputMsgClient(cmd.Context(), prover, latest.GetRevisionHeight()-num, "create_testnet.json") if err != nil { return err } - return m.outputMsgUpdate(prover, createdEpoch, latest.GetRevisionHeight(), num, "update_testnet.json") + return m.outputMsgUpdate(cmd.Context(), prover, createdEpoch, latest.GetRevisionHeight(), num, "update_testnet.json") }, } cmd.Flags().Uint64Var(&num, "num", 240, "--num") return cmd } -func (m *historyModule) outputMsgUpdate(prover *module.Prover, createdEpoch, latest uint64, num uint64, path string) error { +func (m *historyModule) outputMsgUpdate(ctx context.Context, prover *module.Prover, createdEpoch, latest, num uint64, path string) error { type updatingData struct { Header string `json:"header"` } @@ -87,12 +89,12 @@ func (m *historyModule) outputMsgUpdate(prover *module.Prover, createdEpoch, lat for i := num; i > 0; i-- { log.Println(num - i) targetLatest := latest - i - header, err := prover.GetLatestFinalizedHeaderByLatestHeight(targetLatest) + header, err := prover.GetLatestFinalizedHeaderByLatestHeight(ctx, targetLatest) if err != nil { log.Println(err) continue } - blocks, err := prover.SetupHeadersForUpdateByLatestHeight(lastFinalized, header.(*module.Header)) + blocks, err := prover.SetupHeadersForUpdateByLatestHeight(ctx, lastFinalized, header.(*module.Header)) if err != nil { log.Println(err) continue @@ -123,8 +125,8 @@ func (m *historyModule) outputMsgUpdate(prover *module.Prover, createdEpoch, lat return os.WriteFile(path, serialized, 0666) } -func (m *historyModule) outputMsgClient(prover *module.Prover, firstNumber uint64, path string) (uint64, error) { - firstHeader, err := prover.GetLatestFinalizedHeaderByLatestHeight(firstNumber) +func (m *historyModule) outputMsgClient(ctx context.Context, prover *module.Prover, firstNumber uint64, path string) (uint64, error) { + firstHeader, err := prover.GetLatestFinalizedHeaderByLatestHeight(ctx, firstNumber) if err != nil { return 0, err } @@ -132,7 +134,7 @@ func (m *historyModule) outputMsgClient(prover *module.Prover, firstNumber uint6 ClientState string `json:"clientState"` ConsensusState string `json:"consensusState"` } - cs, consState, err := prover.CreateInitialLightClientState(types.NewHeight(0, firstNumber)) + cs, consState, err := prover.CreateInitialLightClientState(ctx, types.NewHeight(0, firstNumber)) if err != nil { return 0, err } diff --git a/tool/testdata/internal/membership/verify_membership.go b/tool/testdata/internal/membership/verify_membership.go index c21f2d1d..c81b706e 100644 --- a/tool/testdata/internal/membership/verify_membership.go +++ b/tool/testdata/internal/membership/verify_membership.go @@ -49,7 +49,7 @@ func (m *verifyMembershipModule) latest() *cobra.Command { } path := host.ConnectionPath("connection-0") - stateRoot, proof, proofHeight, err := m.proveState(chainID, path, commitment) + stateRoot, proof, proofHeight, err := m.proveState(cmd.Context(), chainID, path, commitment) if err != nil { log.Panic(err) } @@ -62,11 +62,11 @@ func (m *verifyMembershipModule) latest() *cobra.Command { } } -func (m *verifyMembershipModule) proveState(chainID uint64, path string, value []byte) (common.Hash, []byte, types.Height, error) { +func (m *verifyMembershipModule) proveState(ctx context.Context, chainID uint64, path string, value []byte) (common.Hash, []byte, types.Height, error) { rpcAddr := os.Getenv("BSC_RPC_ADDR") ibcAddress := os.Getenv("BSC_IBC_ADDR") log.Println(rpcAddr, ibcAddress) - chain, err := ethereum.NewChain(ethereum.ChainConfig{ + chain, err := ethereum.NewChain(ctx, ethereum.ChainConfig{ EthChainId: chainID, RpcAddr: rpcAddr, Signer: internal.CreateSignerConfig(), @@ -75,22 +75,22 @@ func (m *verifyMembershipModule) proveState(chainID uint64, path string, value [ if err != nil { return common.Hash{}, nil, types.Height{}, err } - latest, err := chain.LatestHeight() + latest, err := chain.LatestHeight(ctx) if err != nil { return common.Hash{}, nil, types.Height{}, err } config := module.ProverConfig{} prover := module.NewProver(module.NewChain(chain), &config).(*module.Prover) - ctx := core.NewQueryContext(context.Background(), latest) + queryCtx := core.NewQueryContext(ctx, latest) - header, err := chain.Client().HeaderByNumber(ctx.Context(), big.NewInt(int64(latest.GetRevisionHeight()))) + header, err := chain.Client().HeaderByNumber(ctx, big.NewInt(int64(latest.GetRevisionHeight()))) if err != nil { return common.Hash{}, nil, types.Height{}, err } - proof, proofHeight, err := prover.ProveState(ctx, path, value) - storageRoot, err := prover.GetStorageRoot(ctx.Context(), header) + proof, proofHeight, err := prover.ProveState(queryCtx, path, value) + storageRoot, err := prover.GetStorageRoot(ctx, header) if err != nil { return common.Hash{}, nil, types.Height{}, err } diff --git a/tool/testdata/internal/misbehavior.go b/tool/testdata/internal/misbehavior.go index f1fc347f..d66d8bd2 100644 --- a/tool/testdata/internal/misbehavior.go +++ b/tool/testdata/internal/misbehavior.go @@ -1,7 +1,11 @@ package internal import ( + "context" "fmt" + "log" + "os" + "github.com/cosmos/ibc-go/v8/modules/core/02-client/types" "github.com/datachainlab/ethereum-ibc-relay-chain/pkg/relay/ethereum" "github.com/datachainlab/ibc-parlia-relay/module" @@ -9,8 +13,6 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/rlp" "github.com/spf13/cobra" - "log" - "os" ) type misbehaviorModule struct { @@ -21,11 +23,11 @@ func (m *misbehaviorModule) success() *cobra.Command { Use: "success", RunE: func(cmd *cobra.Command, args []string) error { chainID := uint64(9999) - targetHeight, header1, err := m.getLocalHeader(chainID, 8645, 0, 1) + targetHeight, header1, err := m.getLocalHeader(cmd.Context(), chainID, 8645, 0, 1) if err != nil { log.Panic(err) } - _, header2, err := m.getLocalHeader(chainID, 8545, targetHeight, 2) + _, header2, err := m.getLocalHeader(cmd.Context(), chainID, 8545, targetHeight, 2) if err != nil { log.Panic(err) } @@ -53,20 +55,20 @@ func (m *misbehaviorModule) error() *cobra.Command { return &cobra.Command{ Use: "error", RunE: func(cmd *cobra.Command, args []string) error { - prover, chain, err := createProver() + prover, chain, err := createProver(cmd.Context()) if err != nil { return err } - latestHeight, err := chain.LatestHeight() + latestHeight, err := chain.LatestHeight(cmd.Context()) if err != nil { return err } latest := latestHeight.GetRevisionHeight() println(latest) - header, err := prover.GetLatestFinalizedHeaderByLatestHeight(latest) + header, err := prover.GetLatestFinalizedHeaderByLatestHeight(cmd.Context(), latest) target, err := header.(*module.Header).Target() - updating, err := prover.SetupHeadersForUpdateByLatestHeight(types.NewHeight(header.GetHeight().GetRevisionNumber(), target.Number.Uint64()-1), header.(*module.Header)) + updating, err := prover.SetupHeadersForUpdateByLatestHeight(cmd.Context(), types.NewHeight(header.GetHeight().GetRevisionNumber(), target.Number.Uint64()-1), header.(*module.Header)) if err != nil { return err } @@ -84,8 +86,8 @@ func (m *misbehaviorModule) error() *cobra.Command { log.Println("Exactly same block: height", target.Number.Int64()) // Invalid block - header2, _ := prover.GetLatestFinalizedHeaderByLatestHeight(latest) - updating2, _ := prover.SetupHeadersForUpdateByLatestHeight(types.NewHeight(header2.GetHeight().GetRevisionNumber(), target.Number.Uint64()-1), header2.(*module.Header)) + header2, _ := prover.GetLatestFinalizedHeaderByLatestHeight(cmd.Context(), latest) + updating2, _ := prover.SetupHeadersForUpdateByLatestHeight(cmd.Context(), types.NewHeight(header2.GetHeight().GetRevisionNumber(), target.Number.Uint64()-1), header2.(*module.Header)) target2, _ := updating2[0].(*module.Header).Target() target2.Root = common.Hash{} rlpTarget, err := rlp.EncodeToBytes(target2) @@ -110,8 +112,8 @@ func (m *misbehaviorModule) error() *cobra.Command { } } -func (m *misbehaviorModule) getLocalHeader(chainID uint64, port int64, targetHeight uint64, trustedDiff uint64) (uint64, *module.Header, error) { - chain, err := ethereum.NewChain(ethereum.ChainConfig{ +func (m *misbehaviorModule) getLocalHeader(ctx context.Context, chainID uint64, port int64, targetHeight uint64, trustedDiff uint64) (uint64, *module.Header, error) { + chain, err := ethereum.NewChain(ctx, ethereum.ChainConfig{ EthChainId: chainID, RpcAddr: fmt.Sprintf("http://localhost:%d", port), Signer: CreateSignerConfig(), @@ -121,7 +123,7 @@ func (m *misbehaviorModule) getLocalHeader(chainID uint64, port int64, targetHei return targetHeight, nil, err } if targetHeight == 0 { - latest, err := chain.LatestHeight() + latest, err := chain.LatestHeight(ctx) if err != nil { return targetHeight, nil, err } @@ -133,7 +135,7 @@ func (m *misbehaviorModule) getLocalHeader(chainID uint64, port int64, targetHei // Get Finalized header latestHeight := types.NewHeight(0, targetHeight) latest := latestHeight.GetRevisionHeight() - iHeader, err := prover.GetLatestFinalizedHeaderByLatestHeight(latest) + iHeader, err := prover.GetLatestFinalizedHeaderByLatestHeight(ctx, latest) if err != nil { return latest, nil, err } diff --git a/tool/testdata/internal/update_client.go b/tool/testdata/internal/update_client.go index dd402c2e..b47ed67c 100644 --- a/tool/testdata/internal/update_client.go +++ b/tool/testdata/internal/update_client.go @@ -1,13 +1,15 @@ package internal import ( + "context" + "log" + "github.com/cosmos/ibc-go/v8/modules/core/02-client/types" "github.com/datachainlab/ibc-parlia-relay/module" "github.com/datachainlab/ibc-parlia-relay/module/constant" "github.com/ethereum/go-ethereum/common" "github.com/pkg/errors" "github.com/spf13/cobra" - "log" ) type updateClientModule struct { @@ -22,31 +24,31 @@ func (m *updateClientModule) success() *cobra.Command { Use: "latest", Short: "for latest block", RunE: func(cmd *cobra.Command, args []string) error { - prover, chain, err := createProver() + prover, chain, err := createProver(cmd.Context()) if err != nil { return errors.WithStack(err) } - latest, err := chain.LatestHeight() + latest, err := chain.LatestHeight(cmd.Context()) if err != nil { return errors.WithStack(err) } - return m.printHeader(prover, chain, latest.GetRevisionHeight()) + return m.printHeader(cmd.Context(), prover, chain, latest.GetRevisionHeight()) }, }) cmd.AddCommand(&cobra.Command{ Use: "epoch", Short: "for epoch block", RunE: func(cmd *cobra.Command, args []string) error { - prover, chain, err := createProver() + prover, chain, err := createProver(cmd.Context()) if err != nil { return err } - latest, err := chain.LatestHeight() + latest, err := chain.LatestHeight(cmd.Context()) if err != nil { return err } epochCount := latest.GetRevisionHeight() / constant.BlocksPerEpoch - return m.printHeader(prover, chain, epochCount*constant.BlocksPerEpoch+2) + return m.printHeader(cmd.Context(), prover, chain, epochCount*constant.BlocksPerEpoch+2) }, }) var num uint64 @@ -54,23 +56,23 @@ func (m *updateClientModule) success() *cobra.Command { specified := &cobra.Command{ Use: "specified", RunE: func(cmd *cobra.Command, args []string) error { - prover, chain, err := createProver() + prover, chain, err := createProver(cmd.Context()) if err != nil { return errors.WithStack(err) } currentEpoch := module.GetCurrentEpoch(num) previousEpoch := module.GetPreviousEpoch(num) - validator, turnLength, err := module.QueryValidatorSetAndTurnLength(chain.Header, previousEpoch) + validator, turnLength, err := module.QueryValidatorSetAndTurnLength(cmd.Context(), chain.Header, previousEpoch) if err != nil { return errors.WithStack(err) } checkpoint := currentEpoch + validator.Checkpoint(turnLength) - target, err := prover.GetLatestFinalizedHeaderByLatestHeight(uint64(int64(num) + 2 + diff)) + target, err := prover.GetLatestFinalizedHeaderByLatestHeight(cmd.Context(), uint64(int64(num)+2+diff)) if err != nil { return errors.WithStack(err) } log.Println("checkpoint", checkpoint, "turnLength", turnLength, "target", target.GetHeight()) - headers, err := prover.SetupHeadersForUpdateByLatestHeight(types.NewHeight(0, previousEpoch), target.(*module.Header)) + headers, err := prover.SetupHeadersForUpdateByLatestHeight(cmd.Context(), types.NewHeight(0, previousEpoch), target.(*module.Header)) if err != nil { return errors.WithStack(err) } @@ -99,20 +101,20 @@ func (m *updateClientModule) error() *cobra.Command { Use: "error", Short: "create updateClient testdata for error", RunE: func(cmd *cobra.Command, args []string) error { - prover, chain, err := createProver() + prover, chain, err := createProver(cmd.Context()) if err != nil { return errors.WithStack(err) } - latest, err := chain.LatestHeight() + latest, err := chain.LatestHeight(cmd.Context()) if err != nil { return errors.WithStack(err) } epoch := module.GetCurrentEpoch(latest.GetRevisionHeight()) - header, err := prover.GetLatestFinalizedHeaderByLatestHeight(epoch + 2) + header, err := prover.GetLatestFinalizedHeaderByLatestHeight(cmd.Context(), epoch+2) if err != nil { return errors.WithStack(err) } - updating, err := prover.SetupHeadersForUpdateByLatestHeight(types.NewHeight(0, header.GetHeight().GetRevisionNumber()-constant.BlocksPerEpoch), header.(*module.Header)) + updating, err := prover.SetupHeadersForUpdateByLatestHeight(cmd.Context(), types.NewHeight(0, header.GetHeight().GetRevisionNumber()-constant.BlocksPerEpoch), header.(*module.Header)) if err != nil { return errors.WithStack(err) } @@ -134,9 +136,9 @@ func (m *updateClientModule) error() *cobra.Command { } } -func (m *updateClientModule) printHeader(prover *module.Prover, chain module.Chain, height uint64) error { +func (m *updateClientModule) printHeader(ctx context.Context, prover *module.Prover, chain module.Chain, height uint64) error { log.Println("printHeader latest=", height) - iHeader, err := prover.GetLatestFinalizedHeaderByLatestHeight(height) + iHeader, err := prover.GetLatestFinalizedHeaderByLatestHeight(ctx, height) if err != nil { return errors.WithStack(err) } @@ -150,7 +152,7 @@ func (m *updateClientModule) printHeader(prover *module.Prover, chain module.Cha } // setup - updating, err := prover.SetupHeadersForUpdateByLatestHeight(types.NewHeight(header.GetHeight().GetRevisionNumber(), target.Number.Uint64()-1), header) + updating, err := prover.SetupHeadersForUpdateByLatestHeight(ctx, types.NewHeight(header.GetHeight().GetRevisionNumber(), target.Number.Uint64()-1), header) if err != nil { return err } @@ -166,11 +168,11 @@ func (m *updateClientModule) printHeader(prover *module.Prover, chain module.Cha } trustedHeight := updating[0].(*module.Header).TrustedHeight.GetRevisionHeight() - currentValidatorSetOfTrustedHeight, currentTurnLengthOfTrustedHeight, err := module.QueryValidatorSetAndTurnLength(chain.Header, module.GetCurrentEpoch(trustedHeight)) + currentValidatorSetOfTrustedHeight, currentTurnLengthOfTrustedHeight, err := module.QueryValidatorSetAndTurnLength(ctx, chain.Header, module.GetCurrentEpoch(trustedHeight)) if err != nil { return err } - previousValidatorSetOfTrustedHeight, previousTurnLengthOfTrustedHeight, err := module.QueryValidatorSetAndTurnLength(chain.Header, module.GetPreviousEpoch(trustedHeight)) + previousValidatorSetOfTrustedHeight, previousTurnLengthOfTrustedHeight, err := module.QueryValidatorSetAndTurnLength(ctx, chain.Header, module.GetPreviousEpoch(trustedHeight)) if err != nil { return err }