@@ -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
146146func (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
157157func (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
407410func (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 }
0 commit comments