Skip to content

Commit 709a315

Browse files
authored
Merge pull request #154 from abicky/use-context-appropriately
Resolve context.TODO() and add context argument
2 parents 5962607 + 1df3281 commit 709a315

File tree

26 files changed

+430
-383
lines changed

26 files changed

+430
-383
lines changed

.github/workflows/test.yml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@ jobs:
1919
with:
2020
go-version: 1.21
2121
id: go
22-
- uses: actions/cache@v4
23-
with:
24-
path: |
25-
~/.cache/go-build
26-
~/go/pkg/mod
27-
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
28-
restore-keys: |
29-
${{ runner.os }}-go-
3022
- name: Run Unit test
3123
run: make test
3224
- name: Build

chains/tendermint/chain.go

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func (c *Chain) SetupForRelay(ctx context.Context) error {
144144

145145
// LatestHeight queries the chain for the latest height and returns it
146146
func (c *Chain) LatestHeight(ctx context.Context) (ibcexported.Height, error) {
147-
res, err := c.Client.Status(context.Background())
147+
res, err := c.Client.Status(ctx)
148148
if err != nil {
149149
return nil, err
150150
} else if res.SyncInfo.CatchingUp {
@@ -156,7 +156,7 @@ func (c *Chain) LatestHeight(ctx context.Context) (ibcexported.Height, error) {
156156

157157
func (c *Chain) Timestamp(ctx context.Context, height ibcexported.Height) (time.Time, error) {
158158
ht := int64(height.GetRevisionHeight())
159-
if header, err := c.Client.Header(context.TODO(), &ht); err != nil {
159+
if header, err := c.Client.Header(ctx, &ht); err != nil {
160160
return time.Time{}, err
161161
} else {
162162
return header.Header.Time, nil
@@ -172,10 +172,10 @@ func (c *Chain) RegisterMsgEventListener(listener core.MsgEventListener) {
172172
c.msgEventListener = listener
173173
}
174174

175-
func (c *Chain) sendMsgs(msgs []sdk.Msg) (*sdk.TxResponse, error) {
175+
func (c *Chain) sendMsgs(ctx context.Context, msgs []sdk.Msg) (*sdk.TxResponse, error) {
176176
logger := GetChainLogger()
177177
// broadcast tx
178-
res, _, err := c.rawSendMsgs(msgs)
178+
res, _, err := c.rawSendMsgs(ctx, msgs)
179179
if err != nil {
180180
return nil, err
181181
} else if res.Code != 0 {
@@ -184,7 +184,7 @@ func (c *Chain) sendMsgs(msgs []sdk.Msg) (*sdk.TxResponse, error) {
184184
}
185185

186186
// wait for tx being committed
187-
if resTx, err := c.waitForCommit(res.TxHash); err != nil {
187+
if resTx, err := c.waitForCommit(ctx, res.TxHash); err != nil {
188188
return nil, err
189189
} else if resTx.TxResult.IsErr() {
190190
// DeliverTx failed
@@ -193,7 +193,7 @@ func (c *Chain) sendMsgs(msgs []sdk.Msg) (*sdk.TxResponse, error) {
193193

194194
// call msgEventListener if needed
195195
if c.msgEventListener != nil {
196-
if err := c.msgEventListener.OnSentMsg(context.TODO(), msgs); err != nil {
196+
if err := c.msgEventListener.OnSentMsg(ctx, msgs); err != nil {
197197
logger.Error("failed to OnSendMsg call", err)
198198
return res, nil
199199
}
@@ -202,20 +202,23 @@ func (c *Chain) sendMsgs(msgs []sdk.Msg) (*sdk.TxResponse, error) {
202202
return res, nil
203203
}
204204

205-
func (c *Chain) rawSendMsgs(msgs []sdk.Msg) (*sdk.TxResponse, bool, error) {
205+
func (c *Chain) rawSendMsgs(ctx context.Context, msgs []sdk.Msg) (*sdk.TxResponse, bool, error) {
206206
// Instantiate the client context
207-
ctx := c.CLIContext(0)
207+
// NOTE: Although cosmos-sdk does not currently use CmdContext in Context.QueryWithData,
208+
// set ctx to clientCtx in case cosmos-sdk uses it in the future.
209+
// (cf. https://github.com/cosmos/cosmos-sdk/blob/v0.50.5/client/query.go#L98, https://github.com/cosmos/cosmos-sdk/blob/v0.50.5/x/auth/types/account_retriever.go#L39, etc.)
210+
clientCtx := c.CLIContext(0).WithCmdContext(ctx)
208211

209212
// Query account details
210-
txf, err := prepareFactory(ctx, c.TxFactory(0))
213+
txf, err := prepareFactory(clientCtx, c.TxFactory(0))
211214
if err != nil {
212215
return nil, false, err
213216
}
214217

215218
// TODO: Make this work with new CalculateGas method
216219
// https://github.com/cosmos/cosmos-sdk/blob/5725659684fc93790a63981c653feee33ecf3225/client/tx/tx.go#L297
217220
// If users pass gas adjustment, then calculate gas
218-
_, adjusted, err := CalculateGas(ctx.QueryWithData, txf, msgs...)
221+
_, adjusted, err := CalculateGas(clientCtx.QueryWithData, txf, msgs...)
219222
if err != nil {
220223
return nil, false, err
221224
}
@@ -230,19 +233,19 @@ func (c *Chain) rawSendMsgs(msgs []sdk.Msg) (*sdk.TxResponse, bool, error) {
230233
}
231234

232235
// Attach the signature to the transaction
233-
err = tx.Sign(context.TODO(), txf, c.config.Key, txb, false)
236+
err = tx.Sign(ctx, txf, c.config.Key, txb, false)
234237
if err != nil {
235238
return nil, false, err
236239
}
237240

238241
// Generate the transaction bytes
239-
txBytes, err := ctx.TxConfig.TxEncoder()(txb.GetTx())
242+
txBytes, err := clientCtx.TxConfig.TxEncoder()(txb.GetTx())
240243
if err != nil {
241244
return nil, false, err
242245
}
243246

244247
// Broadcast those bytes
245-
res, err := ctx.BroadcastTx(txBytes)
248+
res, err := clientCtx.BroadcastTx(txBytes)
246249
if err != nil {
247250
return nil, false, err
248251
}
@@ -259,7 +262,7 @@ func (c *Chain) rawSendMsgs(msgs []sdk.Msg) (*sdk.TxResponse, bool, error) {
259262
return res, true, nil
260263
}
261264

262-
func (c *Chain) waitForCommit(txHash string) (*coretypes.ResultTx, error) {
265+
func (c *Chain) waitForCommit(ctx context.Context, txHash string) (*coretypes.ResultTx, error) {
263266
var resTx *coretypes.ResultTx
264267

265268
retryInterval := c.AverageBlockTime()
@@ -268,7 +271,7 @@ func (c *Chain) waitForCommit(txHash string) (*coretypes.ResultTx, error) {
268271
if err := retry.Do(func() error {
269272
var err error
270273
var recoverable bool
271-
resTx, recoverable, err = c.rawQueryTx(txHash)
274+
resTx, recoverable, err = c.rawQueryTx(ctx, txHash)
272275
if err != nil {
273276
if recoverable {
274277
return err
@@ -280,13 +283,13 @@ func (c *Chain) waitForCommit(txHash string) (*coretypes.ResultTx, error) {
280283
// proofs of states updated up to height N are available.
281284
// In order to make the proof of the state updated by a tx available just after `sendMsgs`,
282285
// `waitForCommit` must wait until the latest height is greater than the tx height.
283-
if height, err := c.LatestHeight(context.TODO()); err != nil {
286+
if height, err := c.LatestHeight(ctx); err != nil {
284287
return fmt.Errorf("failed to obtain latest height: %v", err)
285288
} else if height.GetRevisionHeight() <= uint64(resTx.Height) {
286289
return fmt.Errorf("latest_height(%v) is less than or equal to tx_height(%v) yet", height, resTx.Height)
287290
}
288291
return nil
289-
}, retry.Attempts(maxRetry), retry.Delay(retryInterval), rtyErr); err != nil {
292+
}, retry.Context(ctx), retry.Attempts(maxRetry), retry.Delay(retryInterval), rtyErr); err != nil {
290293
return resTx, fmt.Errorf("failed to make sure that tx is committed: %v", err)
291294
}
292295

@@ -295,20 +298,20 @@ func (c *Chain) waitForCommit(txHash string) (*coretypes.ResultTx, error) {
295298

296299
// rawQueryTx returns a tx of which hash equals to `hexTxHash`.
297300
// If the RPC is successful but the tx is not found, this returns nil with nil error.
298-
func (c *Chain) rawQueryTx(hexTxHash string) (*coretypes.ResultTx, bool, error) {
299-
ctx := c.CLIContext(0)
301+
func (c *Chain) rawQueryTx(ctx context.Context, hexTxHash string) (*coretypes.ResultTx, bool, error) {
302+
clientCtx := c.CLIContext(0).WithCmdContext(ctx)
300303

301304
txHash, err := hex.DecodeString(hexTxHash)
302305
if err != nil {
303306
return nil, false, fmt.Errorf("failed to decode the hex string of tx hash: %v", err)
304307
}
305308

306-
node, err := ctx.GetNode()
309+
node, err := clientCtx.GetNode()
307310
if err != nil {
308311
return nil, false, fmt.Errorf("failed to get node: %v", err)
309312
}
310313

311-
resTx, err := node.Tx(context.TODO(), txHash, false)
314+
resTx, err := node.Tx(ctx, txHash, false)
312315
if err != nil {
313316
recoverable := !strings.Contains(err.Error(), "transaction indexing is disabled")
314317
return nil, recoverable, fmt.Errorf("failed to retrieve tx: %v", err)
@@ -406,7 +409,7 @@ func CalculateGas(
406409

407410
func (c *Chain) SendMsgs(ctx context.Context, msgs []sdk.Msg) ([]core.MsgID, error) {
408411
// Broadcast those bytes
409-
res, err := c.sendMsgs(msgs)
412+
res, err := c.sendMsgs(ctx, msgs)
410413
if err != nil {
411414
return nil, err
412415
}
@@ -427,7 +430,7 @@ func (c *Chain) GetMsgResult(ctx context.Context, id core.MsgID) (core.MsgResult
427430
}
428431

429432
// find tx
430-
resTx, err := c.waitForCommit(msgID.TxHash)
433+
resTx, err := c.waitForCommit(ctx, msgID.TxHash)
431434
if err != nil {
432435
return nil, fmt.Errorf("failed to query tx: %v", err)
433436
}

chains/tendermint/cmd/light.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func initLightCmd(ctx *config.Context) *cobra.Command {
4343
chain := c.Chain.(*tendermint.Chain)
4444
prover := c.Prover.(*tendermint.Prover)
4545

46-
db, df, err := prover.NewLightDB()
46+
db, df, err := prover.NewLightDB(cmd.Context())
4747
if err != nil {
4848
return err
4949
}
@@ -64,13 +64,13 @@ func initLightCmd(ctx *config.Context) *cobra.Command {
6464

6565
switch {
6666
case force: // force initialization from trusted node
67-
_, err := prover.LightClientWithoutTrust(db)
67+
_, err := prover.LightClientWithoutTrust(cmd.Context(), db)
6868
if err != nil {
6969
return err
7070
}
7171
fmt.Printf("successfully created light client for %s by trusting endpoint %s...\n", chain.ChainID(), chain.Config().RpcAddr)
7272
case height > 0 && len(hash) > 0: // height and hash are given
73-
_, err = prover.LightClientWithTrust(db, prover.TrustOptions(height, hash))
73+
_, err = prover.LightClientWithTrust(cmd.Context(), db, prover.TrustOptions(height, hash))
7474
if err != nil {
7575
return wrapInitFailed(err)
7676
}
@@ -98,12 +98,12 @@ func updateLightCmd(ctx *config.Context) *cobra.Command {
9898
}
9999
prover := c.Prover.(*tendermint.Prover)
100100

101-
bh, err := prover.GetLatestLightHeader()
101+
bh, err := prover.GetLatestLightHeader(cmd.Context())
102102
if err != nil {
103103
return err
104104
}
105105

106-
ah, err := prover.UpdateLightClient(0)
106+
ah, err := prover.UpdateLightClient(cmd.Context(), 0)
107107
if err != nil {
108108
return err
109109
}
@@ -136,7 +136,7 @@ func lightHeaderCmd(ctx *config.Context) *cobra.Command {
136136

137137
switch len(args) {
138138
case 1:
139-
header, err = prover.GetLatestLightHeader()
139+
header, err = prover.GetLatestLightHeader(cmd.Context())
140140
if err != nil {
141141
return err
142142
}
@@ -148,7 +148,7 @@ func lightHeaderCmd(ctx *config.Context) *cobra.Command {
148148
}
149149

150150
if height == 0 {
151-
height, err = prover.GetLatestLightHeight()
151+
height, err = prover.GetLatestLightHeight(cmd.Context())
152152
if err != nil {
153153
return err
154154
}
@@ -158,7 +158,7 @@ func lightHeaderCmd(ctx *config.Context) *cobra.Command {
158158
}
159159
}
160160

161-
header, err = prover.GetLightSignedHeaderAtHeight(height)
161+
header, err = prover.GetLightSignedHeaderAtHeight(cmd.Context(), height)
162162
if err != nil {
163163
return err
164164
}
@@ -181,7 +181,7 @@ func deleteLightCmd(ctx *config.Context) *cobra.Command {
181181
cmd := &cobra.Command{
182182
Use: "delete [chain-id]",
183183
Aliases: []string{"d"},
184-
Short: "wipe the light client database, forcing re-initialzation on the next run",
184+
Short: "wipe the light client database, forcing re-initialization on the next run",
185185
Args: cobra.ExactArgs(1),
186186
RunE: func(cmd *cobra.Command, args []string) error {
187187
c, err := ctx.Config.GetChain(args[0])

chains/tendermint/light.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ func (pr *Prover) LightHTTP() lightp.Provider {
5555
return cl
5656
}
5757

58-
func (pr *Prover) NewLightDB() (db *dbm.GoLevelDB, df func(), err error) {
58+
func (pr *Prover) NewLightDB(ctx context.Context) (db *dbm.GoLevelDB, df func(), err error) {
5959
c := pr.chain
6060
if err := retry.Do(func() error {
6161
db, err = dbm.NewGoLevelDB(c.config.ChainId, lightDir(c.HomePath))
6262
if err != nil {
6363
return fmt.Errorf("can't open light client database: %w", err)
6464
}
6565
return nil
66-
}, rtyAtt, rtyDel, rtyErr); err != nil {
66+
}, rtyAtt, rtyDel, rtyErr, retry.Context(ctx)); err != nil {
6767
return nil, nil, err
6868
}
6969

@@ -84,10 +84,10 @@ func (pr *Prover) DeleteLightDB() error {
8484

8585
// LightClientWithTrust takes a header from the chain and attempts to add that header to the light
8686
// database.
87-
func (pr *Prover) LightClientWithTrust(db dbm.DB, to light.TrustOptions) (*light.Client, error) {
87+
func (pr *Prover) LightClientWithTrust(ctx context.Context, db dbm.DB, to light.TrustOptions) (*light.Client, error) {
8888
prov := pr.LightHTTP()
8989
return light.NewClient(
90-
context.Background(),
90+
ctx,
9191
pr.chain.config.ChainId,
9292
to,
9393
prov,
@@ -98,24 +98,24 @@ func (pr *Prover) LightClientWithTrust(db dbm.DB, to light.TrustOptions) (*light
9898
logger)
9999
}
100100

101-
// LightClientWithoutTrust querys the latest header from the chain and initializes a new light client
101+
// LightClientWithoutTrust queries the latest header from the chain and initializes a new light client
102102
// database using that header. This should only be called when first initializing the light client
103-
func (pr *Prover) LightClientWithoutTrust(db dbm.DB) (*light.Client, error) {
103+
func (pr *Prover) LightClientWithoutTrust(ctx context.Context, db dbm.DB) (*light.Client, error) {
104104
var (
105105
height int64
106106
err error
107107
)
108108
prov := pr.LightHTTP()
109109

110110
if err := retry.Do(func() error {
111-
h, err := pr.chain.LatestHeight(context.TODO())
111+
h, err := pr.chain.LatestHeight(ctx)
112112
switch {
113113
case err != nil:
114114
return err
115115
case h.GetRevisionHeight() == 0:
116116
return fmt.Errorf("shouldn't be here")
117117
default:
118-
t, err := pr.chain.Timestamp(context.TODO(), h)
118+
t, err := pr.chain.Timestamp(ctx, h)
119119
if err != nil {
120120
return err
121121
}
@@ -125,16 +125,16 @@ func (pr *Prover) LightClientWithoutTrust(db dbm.DB) (*light.Client, error) {
125125
height = int64(h.GetRevisionHeight())
126126
return nil
127127
}
128-
}, rtyAtt, rtyDel, rtyErr); err != nil {
128+
}, rtyAtt, rtyDel, rtyErr, retry.Context(ctx)); err != nil {
129129
return nil, err
130130
}
131131

132-
lb, err := prov.LightBlock(context.Background(), height)
132+
lb, err := prov.LightBlock(ctx, height)
133133
if err != nil {
134134
return nil, err
135135
}
136136
return light.NewClient(
137-
context.Background(),
137+
ctx,
138138
pr.chain.config.ChainId,
139139
light.TrustOptions{
140140
Period: pr.getTrustingPeriod(),
@@ -150,14 +150,14 @@ func (pr *Prover) LightClientWithoutTrust(db dbm.DB) (*light.Client, error) {
150150
}
151151

152152
// GetLatestLightHeader returns the header to be used for client creation
153-
func (pr *Prover) GetLatestLightHeader() (*tmclient.Header, error) {
154-
return pr.GetLightSignedHeaderAtHeight(0)
153+
func (pr *Prover) GetLatestLightHeader(ctx context.Context) (*tmclient.Header, error) {
154+
return pr.GetLightSignedHeaderAtHeight(ctx, 0)
155155
}
156156

157157
// GetLightSignedHeaderAtHeight returns a signed header at a particular height.
158-
func (pr *Prover) GetLightSignedHeaderAtHeight(height int64) (*tmclient.Header, error) {
158+
func (pr *Prover) GetLightSignedHeaderAtHeight(ctx context.Context, height int64) (*tmclient.Header, error) {
159159
// create database connection
160-
db, df, err := pr.NewLightDB()
160+
db, df, err := pr.NewLightDB(ctx)
161161
if err != nil {
162162
return nil, err
163163
}

0 commit comments

Comments
 (0)