Skip to content

Commit b4a60cf

Browse files
committed
Add context to functions in setup.go
Signed-off-by: Takeshi Arabiki <takeshi.arabiki@datachain.jp>
1 parent ae508a2 commit b4a60cf

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

module/prover.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ func (pr *Prover) SetupHeadersForUpdate(ctx context.Context, counterparty core.F
118118
}
119119

120120
func (pr *Prover) SetupHeadersForUpdateByLatestHeight(ctx context.Context, clientStateLatestHeight exported.Height, latestFinalizedHeader *Header) ([]core.Header, error) {
121-
queryVerifiableNeighboringEpochHeader := func(height uint64, limitHeight uint64) (core.Header, error) {
122-
ethHeaders, err := queryFinalizedHeader(context.TODO(), pr.chain.Header, height, limitHeight)
121+
queryVerifiableNeighboringEpochHeader := func(ctx context.Context, height uint64, limitHeight uint64) (core.Header, error) {
122+
ethHeaders, err := queryFinalizedHeader(ctx, pr.chain.Header, height, limitHeight)
123123
if err != nil {
124124
return nil, err
125125
}
@@ -134,6 +134,7 @@ func (pr *Prover) SetupHeadersForUpdateByLatestHeight(ctx context.Context, clien
134134
return nil, err
135135
}
136136
return setupHeadersForUpdate(
137+
context.TODO(),
137138
queryVerifiableNeighboringEpochHeader,
138139
pr.chain.Header,
139140
clientStateLatestHeight,

module/setup.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import (
1111
"github.com/hyperledger-labs/yui-relayer/log"
1212
)
1313

14-
type queryVerifiableNeighboringEpochHeaderFn = func(uint64, uint64) (core.Header, error)
14+
type queryVerifiableNeighboringEpochHeaderFn = func(context.Context, uint64, uint64) (core.Header, error)
1515

1616
func setupHeadersForUpdate(
17+
ctx context.Context,
1718
queryVerifiableNeighboringEpochHeader queryVerifiableNeighboringEpochHeaderFn,
1819
getHeader getHeaderFn,
1920
clientStateLatestHeight exported.Height,
@@ -39,7 +40,7 @@ func setupHeadersForUpdate(
3940

4041
// Append insufficient epoch blocks
4142
for epochHeight := firstUnsavedEpoch; epochHeight < latestFinalizedHeight; epochHeight += constant.BlocksPerEpoch {
42-
verifiableEpoch, err := setupNeighboringEpochHeader(getHeader, queryVerifiableNeighboringEpochHeader, epochHeight, trustedEpochHeight, latestHeight)
43+
verifiableEpoch, err := setupNeighboringEpochHeader(ctx, getHeader, queryVerifiableNeighboringEpochHeader, epochHeight, trustedEpochHeight, latestHeight)
4344
if err != nil {
4445
return nil, err
4546
}
@@ -55,21 +56,22 @@ func setupHeadersForUpdate(
5556
}
5657

5758
func setupNeighboringEpochHeader(
59+
ctx context.Context,
5860
getHeader getHeaderFn,
5961
queryVerifiableHeader queryVerifiableNeighboringEpochHeaderFn,
6062
epochHeight uint64,
6163
trustedEpochHeight uint64,
6264
latestHeight exported.Height,
6365
) (core.Header, error) {
6466
// neighboring epoch needs block before checkpoint
65-
currentValidatorSet, currentTurnLength, err := queryValidatorSetAndTurnLength(context.TODO(), getHeader, epochHeight)
67+
currentValidatorSet, currentTurnLength, err := queryValidatorSetAndTurnLength(ctx, getHeader, epochHeight)
6668
if err != nil {
6769
return nil, fmt.Errorf("setupNeighboringEpochHeader: failed to get current validator set: epochHeight=%d : %+v", epochHeight, err)
6870
}
6971
// ex) trusted(prevSaved = 200), epochHeight = 400 must be finalized by min(610,latest)
7072
nextCheckpoint := currentValidatorSet.Checkpoint(currentTurnLength) + (epochHeight + constant.BlocksPerEpoch)
7173
limit := minUint64(nextCheckpoint-1, latestHeight.GetRevisionHeight())
72-
return queryVerifiableHeader(epochHeight, limit)
74+
return queryVerifiableHeader(ctx, epochHeight, limit)
7375
}
7476

7577
func withTrustedHeight(targetHeaders []core.Header, clientStateLatestHeight exported.Height) []core.Header {

module/setup_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package module
22

33
import (
44
"context"
5+
"math/big"
6+
"testing"
7+
58
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
69
"github.com/datachainlab/ibc-parlia-relay/module/constant"
710
types2 "github.com/ethereum/go-ethereum/core/types"
811
"github.com/hyperledger-labs/yui-relayer/core"
912
"github.com/hyperledger-labs/yui-relayer/log"
1013
"github.com/stretchr/testify/suite"
11-
"math/big"
12-
"testing"
1314
)
1415

1516
type SetupTestSuite struct {
@@ -38,7 +39,7 @@ func (ts *SetupTestSuite) TestSuccess_setupHeadersForUpdate_neighboringEpoch() {
3839
CurrentValidators: [][]byte{{1}},
3940
PreviousValidators: [][]byte{{1}},
4041
}
41-
neighborFn := func(height uint64, _ uint64) (core.Header, error) {
42+
neighborFn := func(_ context.Context, height uint64, _ uint64) (core.Header, error) {
4243
h, e := newETHHeader(&types2.Header{
4344
Number: big.NewInt(int64(height)),
4445
})
@@ -53,7 +54,7 @@ func (ts *SetupTestSuite) TestSuccess_setupHeadersForUpdate_neighboringEpoch() {
5354
}, nil
5455
}
5556

56-
targets, err := setupHeadersForUpdate(neighborFn, headerFn, clientStateLatestHeight, latestFinalizedHeader, clienttypes.NewHeight(0, 100000))
57+
targets, err := setupHeadersForUpdate(context.Background(), neighborFn, headerFn, clientStateLatestHeight, latestFinalizedHeader, clienttypes.NewHeight(0, 100000))
5758
ts.Require().NoError(err)
5859
ts.Require().Len(targets, expected)
5960
for i, h := range targets {
@@ -101,7 +102,7 @@ func (ts *SetupTestSuite) TestSuccess_setupHeadersForUpdate_allEmpty() {
101102
latestFinalizedHeader := &Header{
102103
Headers: []*ETHHeader{target},
103104
}
104-
neighboringEpochFn := func(height uint64, _ uint64) (core.Header, error) {
105+
neighboringEpochFn := func(_ context.Context, height uint64, _ uint64) (core.Header, error) {
105106
// insufficient vote attestation
106107
return nil, nil
107108
}
@@ -111,7 +112,7 @@ func (ts *SetupTestSuite) TestSuccess_setupHeadersForUpdate_allEmpty() {
111112
Extra: epochHeader().Extra,
112113
}, nil
113114
}
114-
targets, err := setupHeadersForUpdate(neighboringEpochFn, headerFn, clientStateLatestHeight, latestFinalizedHeader, clienttypes.NewHeight(0, 1000000))
115+
targets, err := setupHeadersForUpdate(context.Background(), neighboringEpochFn, headerFn, clientStateLatestHeight, latestFinalizedHeader, clienttypes.NewHeight(0, 1000000))
115116
ts.Require().NoError(err)
116117
ts.Require().Len(targets, expected)
117118
}
@@ -144,7 +145,7 @@ func (ts *SetupTestSuite) TestSuccess_setupNeighboringEpochHeader() {
144145
epochHeight := uint64(400)
145146
trustedEpochHeight := uint64(200)
146147

147-
neighboringEpochFn := func(height uint64, limit uint64) (core.Header, error) {
148+
neighboringEpochFn := func(_ context.Context, height uint64, limit uint64) (core.Header, error) {
148149
target, err := newETHHeader(&types2.Header{
149150
Number: big.NewInt(int64(limit)),
150151
})
@@ -156,7 +157,7 @@ func (ts *SetupTestSuite) TestSuccess_setupNeighboringEpochHeader() {
156157
headerFn := func(_ context.Context, height uint64) (*types2.Header, error) {
157158
return headerByHeight(int64(height)), nil
158159
}
159-
hs, err := setupNeighboringEpochHeader(headerFn, neighboringEpochFn, epochHeight, trustedEpochHeight, clienttypes.NewHeight(0, 10000))
160+
hs, err := setupNeighboringEpochHeader(context.Background(), headerFn, neighboringEpochFn, epochHeight, trustedEpochHeight, clienttypes.NewHeight(0, 10000))
160161
ts.Require().NoError(err)
161162
target, err := hs.(*Header).Target()
162163
ts.Require().NoError(err)

0 commit comments

Comments
 (0)