Skip to content

Commit 5962607

Browse files
authored
Merge pull request #153 from abicky/add-context
Add context.Context to methods defined in chain, prover, and signer module interfaces
2 parents f0347c7 + 708cf66 commit 5962607

File tree

21 files changed

+85
-80
lines changed

21 files changed

+85
-80
lines changed

chains/tendermint/chain.go

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

145145
// LatestHeight queries the chain for the latest height and returns it
146-
func (c *Chain) LatestHeight() (ibcexported.Height, error) {
146+
func (c *Chain) LatestHeight(ctx context.Context) (ibcexported.Height, error) {
147147
res, err := c.Client.Status(context.Background())
148148
if err != nil {
149149
return nil, err
@@ -154,7 +154,7 @@ func (c *Chain) LatestHeight() (ibcexported.Height, error) {
154154
return clienttypes.NewHeight(version, uint64(res.SyncInfo.LatestBlockHeight)), nil
155155
}
156156

157-
func (c *Chain) Timestamp(height ibcexported.Height) (time.Time, error) {
157+
func (c *Chain) Timestamp(ctx context.Context, height ibcexported.Height) (time.Time, error) {
158158
ht := int64(height.GetRevisionHeight())
159159
if header, err := c.Client.Header(context.TODO(), &ht); err != nil {
160160
return time.Time{}, err
@@ -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(msgs); err != nil {
196+
if err := c.msgEventListener.OnSentMsg(context.TODO(), msgs); err != nil {
197197
logger.Error("failed to OnSendMsg call", err)
198198
return res, nil
199199
}
@@ -280,7 +280,7 @@ func (c *Chain) waitForCommit(txHash string) (*coretypes.ResultTx, error) {
280280
// proofs of states updated up to height N are available.
281281
// In order to make the proof of the state updated by a tx available just after `sendMsgs`,
282282
// `waitForCommit` must wait until the latest height is greater than the tx height.
283-
if height, err := c.LatestHeight(); err != nil {
283+
if height, err := c.LatestHeight(context.TODO()); err != nil {
284284
return fmt.Errorf("failed to obtain latest height: %v", err)
285285
} else if height.GetRevisionHeight() <= uint64(resTx.Height) {
286286
return fmt.Errorf("latest_height(%v) is less than or equal to tx_height(%v) yet", height, resTx.Height)
@@ -404,7 +404,7 @@ func CalculateGas(
404404
return simRes, uint64(txf.GasAdjustment() * float64(simRes.GasInfo.GasUsed)), nil
405405
}
406406

407-
func (c *Chain) SendMsgs(msgs []sdk.Msg) ([]core.MsgID, error) {
407+
func (c *Chain) SendMsgs(ctx context.Context, msgs []sdk.Msg) ([]core.MsgID, error) {
408408
// Broadcast those bytes
409409
res, err := c.sendMsgs(msgs)
410410
if err != nil {
@@ -420,7 +420,7 @@ func (c *Chain) SendMsgs(msgs []sdk.Msg) ([]core.MsgID, error) {
420420
return msgIDs, nil
421421
}
422422

423-
func (c *Chain) GetMsgResult(id core.MsgID) (core.MsgResult, error) {
423+
func (c *Chain) GetMsgResult(ctx context.Context, id core.MsgID) (core.MsgResult, error) {
424424
msgID, ok := id.(*MsgID)
425425
if !ok {
426426
return nil, fmt.Errorf("unexpected message id type: %T", id)

chains/tendermint/light.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,14 @@ func (pr *Prover) LightClientWithoutTrust(db dbm.DB) (*light.Client, error) {
108108
prov := pr.LightHTTP()
109109

110110
if err := retry.Do(func() error {
111-
h, err := pr.chain.LatestHeight()
111+
h, err := pr.chain.LatestHeight(context.TODO())
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(h)
118+
t, err := pr.chain.Timestamp(context.TODO(), h)
119119
if err != nil {
120120
return err
121121
}

chains/tendermint/prover.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (pr *Prover) ProveHostConsensusState(ctx core.QueryContext, height ibcexpor
6363
/* LightClient implementation */
6464

6565
// CreateInitialLightClientState creates a pair of ClientState and ConsensusState submitted to the counterparty chain as MsgCreateClient
66-
func (pr *Prover) CreateInitialLightClientState(height ibcexported.Height) (ibcexported.ClientState, ibcexported.ConsensusState, error) {
66+
func (pr *Prover) CreateInitialLightClientState(ctx context.Context, height ibcexported.Height) (ibcexported.ClientState, ibcexported.ConsensusState, error) {
6767
var tmHeight int64
6868
if height != nil {
6969
tmHeight = int64(height.GetRevisionHeight())
@@ -90,13 +90,13 @@ func (pr *Prover) CreateInitialLightClientState(height ibcexported.Height) (ibce
9090
}
9191

9292
// SetupHeadersForUpdate returns the finalized header and any intermediate headers needed to apply it to the client on the counterpaty chain
93-
func (pr *Prover) SetupHeadersForUpdate(counterparty core.FinalityAwareChain, latestFinalizedHeader core.Header) ([]core.Header, error) {
93+
func (pr *Prover) SetupHeadersForUpdate(ctx context.Context, counterparty core.FinalityAwareChain, latestFinalizedHeader core.Header) ([]core.Header, error) {
9494
self := pr.chain
9595
// make copy of header stored in mop
9696
tmp := latestFinalizedHeader.(*tmclient.Header)
9797
h := *tmp
9898

99-
cph, err := counterparty.LatestHeight()
99+
cph, err := counterparty.LatestHeight(context.TODO())
100100
if err != nil {
101101
return nil, err
102102
}
@@ -127,12 +127,12 @@ func (pr *Prover) SetupHeadersForUpdate(counterparty core.FinalityAwareChain, la
127127
}
128128

129129
// GetLatestFinalizedHeader returns the latest finalized header
130-
func (pr *Prover) GetLatestFinalizedHeader() (core.Header, error) {
130+
func (pr *Prover) GetLatestFinalizedHeader(ctx context.Context) (core.Header, error) {
131131
return pr.UpdateLightClient(0)
132132
}
133133

134-
func (pr *Prover) CheckRefreshRequired(counterparty core.ChainInfoICS02Querier) (bool, error) {
135-
cpQueryHeight, err := counterparty.LatestHeight()
134+
func (pr *Prover) CheckRefreshRequired(ctx context.Context, counterparty core.ChainInfoICS02Querier) (bool, error) {
135+
cpQueryHeight, err := counterparty.LatestHeight(context.TODO())
136136
if err != nil {
137137
return false, fmt.Errorf("failed to get the latest height of the counterparty chain: %v", err)
138138
}
@@ -159,12 +159,12 @@ func (pr *Prover) CheckRefreshRequired(counterparty core.ChainInfoICS02Querier)
159159
}
160160
lcLastTimestamp := time.Unix(0, int64(cons.GetTimestamp()))
161161

162-
selfQueryHeight, err := pr.chain.LatestHeight()
162+
selfQueryHeight, err := pr.chain.LatestHeight(context.TODO())
163163
if err != nil {
164164
return false, fmt.Errorf("failed to get the latest height of the self chain: %v", err)
165165
}
166166

167-
selfTimestamp, err := pr.chain.Timestamp(selfQueryHeight)
167+
selfTimestamp, err := pr.chain.Timestamp(context.TODO(), selfQueryHeight)
168168
if err != nil {
169169
return false, fmt.Errorf("failed to get timestamp of the self chain: %v", err)
170170
}

chains/tendermint/query.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ func (c *Chain) QueryUnfinalizedRelayPackets(ctx core.QueryContext, counterparty
211211
}
212212

213213
var counterpartyCtx core.QueryContext
214-
if counterpartyH, err := counterparty.GetLatestFinalizedHeader(); err != nil {
214+
if counterpartyH, err := counterparty.GetLatestFinalizedHeader(context.TODO()); err != nil {
215215
return nil, fmt.Errorf("failed to get latest finalized header: error=%w height=%v", err, ctx.Height())
216216
} else {
217217
counterpartyCtx = core.NewQueryContext(context.TODO(), counterpartyH.GetHeight())
@@ -264,7 +264,7 @@ func (c *Chain) QueryUnfinalizedRelayAcknowledgements(ctx core.QueryContext, cou
264264
}
265265

266266
var counterpartyCtx core.QueryContext
267-
if counterpartyH, err := counterparty.GetLatestFinalizedHeader(); err != nil {
267+
if counterpartyH, err := counterparty.GetLatestFinalizedHeader(context.TODO()); err != nil {
268268
return nil, fmt.Errorf("failed to get latest finalized header: error=%w height=%v", err, ctx.Height())
269269
} else {
270270
counterpartyCtx = core.NewQueryContext(context.TODO(), counterpartyH.GetHeight())

cmd/query.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func queryClientCmd(ctx *config.Context) *cobra.Command {
5656
if err != nil {
5757
return err
5858
}
59-
latestHeight, err := c.LatestHeight()
59+
latestHeight, err := c.LatestHeight(context.TODO())
6060
if err != nil {
6161
return err
6262
}
@@ -98,7 +98,7 @@ func queryConnection(ctx *config.Context) *cobra.Command {
9898
if err != nil {
9999
return err
100100
}
101-
latestHeight, err := c.LatestHeight()
101+
latestHeight, err := c.LatestHeight(context.TODO())
102102
if err != nil {
103103
return err
104104
}
@@ -136,7 +136,7 @@ func queryChannel(ctx *config.Context) *cobra.Command {
136136
if err != nil {
137137
return err
138138
}
139-
latestHeight, err := c.LatestHeight()
139+
latestHeight, err := c.LatestHeight(context.TODO())
140140
if err != nil {
141141
return err
142142
}
@@ -175,7 +175,7 @@ func queryChannelUpgrade(ctx *config.Context) *cobra.Command {
175175
if err != nil {
176176
return err
177177
}
178-
latestHeight, err := c.LatestHeight()
178+
latestHeight, err := c.LatestHeight(context.TODO())
179179
if err != nil {
180180
return err
181181
}
@@ -221,7 +221,7 @@ func queryBalanceCmd(ctx *config.Context) *cobra.Command {
221221
return err
222222
}
223223

224-
h, err := chain.LatestHeight()
224+
h, err := chain.LatestHeight(context.TODO())
225225
if err != nil {
226226
return err
227227
}

cmd/tx.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func createClientsCmd(ctx *config.Context) *cobra.Command {
8181
return err
8282
} else if height == 0 {
8383
srcHeight = nil
84-
} else if latestHeight, err := c[src].LatestHeight(); err != nil {
84+
} else if latestHeight, err := c[src].LatestHeight(context.TODO()); err != nil {
8585
return fmt.Errorf("failed to get the latest height of src chain: %v", err)
8686
} else {
8787
srcHeight = clienttypes.NewHeight(latestHeight.GetRevisionNumber(), height)
@@ -93,7 +93,7 @@ func createClientsCmd(ctx *config.Context) *cobra.Command {
9393
return err
9494
} else if height == 0 {
9595
dstHeight = nil
96-
} else if latestHeight, err := c[dst].LatestHeight(); err != nil {
96+
} else if latestHeight, err := c[dst].LatestHeight(context.TODO()); err != nil {
9797
return fmt.Errorf("failed to get the latest height of dst chain: %v", err)
9898
} else {
9999
dstHeight = clienttypes.NewHeight(latestHeight.GetRevisionNumber(), height)

core/chain.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ type Chain interface {
7878
// SendMsgs sends msgs to the chain and waits for them to be included in blocks.
7979
// This function returns err=nil only if all the msgs executed successfully at the blocks.
8080
// It should be noted that the block is not finalized at that point and can be reverted afterwards.
81-
SendMsgs(msgs []sdk.Msg) ([]MsgID, error)
81+
SendMsgs(ctx context.Context, msgs []sdk.Msg) ([]MsgID, error)
8282

8383
// GetMsgResult returns the execution result of `sdk.Msg` specified by `MsgID`
8484
// If the msg is not included in any block, this function waits for inclusion.
85-
GetMsgResult(id MsgID) (MsgResult, error)
85+
GetMsgResult(ctx context.Context, id MsgID) (MsgResult, error)
8686

8787
// RegisterMsgEventListener registers a given EventListener to the chain
8888
RegisterMsgEventListener(MsgEventListener)
@@ -101,10 +101,10 @@ type ChainInfo interface {
101101
//
102102
// NOTE: The returned height does not have to be finalized.
103103
// If a finalized height/header is required, the `Prover`'s `GetLatestFinalizedHeader` function should be called instead.
104-
LatestHeight() (ibcexported.Height, error)
104+
LatestHeight(ctx context.Context) (ibcexported.Height, error)
105105

106106
// Timestamp returns the block timestamp
107-
Timestamp(ibcexported.Height) (time.Time, error)
107+
Timestamp(ctx context.Context, height ibcexported.Height) (time.Time, error)
108108

109109
// AverageBlockTime returns the average time required for each new block to be committed
110110
AverageBlockTime() time.Duration
@@ -113,7 +113,7 @@ type ChainInfo interface {
113113
// MsgEventListener is a listener that listens a msg send to the chain
114114
type MsgEventListener interface {
115115
// OnSentMsg is a callback functoin that is called when a msg send to the chain
116-
OnSentMsg(msgs []sdk.Msg) error
116+
OnSentMsg(ctx context.Context, msgs []sdk.Msg) error
117117
}
118118

119119
// IBCQuerier is an interface to the state of IBC

core/channel-upgrade.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ func InitChannelUpgrade(chain, cp *ProvableChain, upgradeFields chantypes.Upgrad
7979
logger := GetChannelLogger(chain.Chain)
8080
defer logger.TimeTrack(time.Now(), "InitChannelUpgrade")
8181

82-
if h, err := chain.LatestHeight(); err != nil {
82+
if h, err := chain.LatestHeight(context.TODO()); err != nil {
8383
logger.Error("failed to get the latest height", err)
8484
return err
85-
} else if cpH, err := cp.LatestHeight(); err != nil {
85+
} else if cpH, err := cp.LatestHeight(context.TODO()); err != nil {
8686
logger.Error("failed to get the latest height of the counterparty chain", err)
8787
return err
8888
} else if chann, cpChann, err := QueryChannelPair(
@@ -117,7 +117,7 @@ func InitChannelUpgrade(chain, cp *ProvableChain, upgradeFields chantypes.Upgrad
117117

118118
msg := chain.Path().ChanUpgradeInit(upgradeFields, addr)
119119

120-
if _, err := chain.SendMsgs([]sdk.Msg{msg}); err != nil {
120+
if _, err := chain.SendMsgs(context.TODO(), []sdk.Msg{msg}); err != nil {
121121
logger.Error("failed to send MsgChannelUpgradeInit", err)
122122
return err
123123
} else {
@@ -219,7 +219,7 @@ func CancelChannelUpgrade(chain, cp *ProvableChain, settlementInterval time.Dura
219219
continue
220220
}
221221

222-
cpHeaders, err := cp.SetupHeadersForUpdate(chain, sh.GetLatestFinalizedHeader(cp.ChainID()))
222+
cpHeaders, err := cp.SetupHeadersForUpdate(context.TODO(), chain, sh.GetLatestFinalizedHeader(cp.ChainID()))
223223
if err != nil {
224224
logger.Error("failed to set up headers for LC update", err)
225225
return err
@@ -260,7 +260,7 @@ func CancelChannelUpgrade(chain, cp *ProvableChain, settlementInterval time.Dura
260260
// NOTE: A call of SendMsgs for each msg is executed separately to avoid using multicall for eth.
261261
// This is just a workaround and should be fixed in the future.
262262
for _, msg := range msgs {
263-
if _, err := chain.SendMsgs([]sdk.Msg{msg}); err != nil {
263+
if _, err := chain.SendMsgs(context.TODO(), []sdk.Msg{msg}); err != nil {
264264
logger.Error("failed to send a msg to cancel the channel upgrade", err)
265265
return err
266266
}
@@ -617,7 +617,7 @@ func queryCanTransitionToFlushComplete(chain interface {
617617
ChainInfo
618618
ICS04Querier
619619
}) (bool, error) {
620-
if h, err := chain.LatestHeight(); err != nil {
620+
if h, err := chain.LatestHeight(context.TODO()); err != nil {
621621
return false, err
622622
} else {
623623
return chain.QueryCanTransitionToFlushComplete(NewQueryContext(context.TODO(), h))
@@ -648,13 +648,13 @@ func querySettledChannelUpgradePair(
648648

649649
// prepare QueryContext's based on the latest heights
650650
var srcLatestCtx, dstLatestCtx QueryContext
651-
if h, err := src.LatestHeight(); err != nil {
651+
if h, err := src.LatestHeight(context.TODO()); err != nil {
652652
logger.Error("failed to get the latest height of the src chain", err)
653653
return nil, nil, false, err
654654
} else {
655655
srcLatestCtx = NewQueryContext(context.TODO(), h)
656656
}
657-
if h, err := dst.LatestHeight(); err != nil {
657+
if h, err := dst.LatestHeight(context.TODO()); err != nil {
658658
logger.Error("failed to get the latest height of the dst chain", err)
659659
return nil, nil, false, err
660660
} else {
@@ -712,7 +712,7 @@ func upgradeAlreadyTimedOut(
712712
cpChanUpg *chantypes.QueryUpgradeResponse,
713713
) (bool, error) {
714714
height := ctx.Height().(clienttypes.Height)
715-
timestamp, err := chain.Timestamp(height)
715+
timestamp, err := chain.Timestamp(context.TODO(), height)
716716
if err != nil {
717717
return false, err
718718
}

core/channel.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func checkChannelCreateReady(src, dst *ProvableChain, logger *log.RelayLogger) (
8787
return chantypes.UNINITIALIZED, nil
8888
}
8989

90-
latestHeight, err := pc.LatestHeight()
90+
latestHeight, err := pc.LatestHeight(context.TODO())
9191
if err != nil {
9292
return chantypes.UNINITIALIZED, err
9393
}
@@ -268,13 +268,13 @@ func querySettledChannelPair(
268268
}
269269

270270
var srcLatestCtx, dstLatestCtx QueryContext
271-
if h, err := src.LatestHeight(); err != nil {
271+
if h, err := src.LatestHeight(context.TODO()); err != nil {
272272
logger.Error("failed to get the latest height of the src chain", err)
273273
return nil, nil, false, err
274274
} else {
275275
srcLatestCtx = NewQueryContext(context.TODO(), h)
276276
}
277-
if h, err := dst.LatestHeight(); err != nil {
277+
if h, err := dst.LatestHeight(context.TODO()); err != nil {
278278
logger.Error("failed to get the latest height of the dst chain", err)
279279
return nil, nil, false, err
280280
} else {

core/client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func checkCreateClientsReady(src, dst *ProvableChain, logger *log.RelayLogger) (
2020
}
2121

2222
getState := func(pc *ProvableChain) (*clienttypes.QueryClientStateResponse, error) {
23-
latestHeight, err := pc.LatestHeight()
23+
latestHeight, err := pc.LatestHeight(context.TODO())
2424
if err != nil {
2525
return nil, err
2626
}
@@ -85,7 +85,7 @@ func CreateClients(pathName string, src, dst *ProvableChain, srcHeight, dstHeigh
8585
return err
8686
}
8787

88-
cs, cons, err := dst.CreateInitialLightClientState(dstHeight)
88+
cs, cons, err := dst.CreateInitialLightClientState(context.TODO(), dstHeight)
8989
if err != nil {
9090
logger.Error("failed to create initial light client state", err)
9191
return err
@@ -107,7 +107,7 @@ func CreateClients(pathName string, src, dst *ProvableChain, srcHeight, dstHeigh
107107
return err
108108
}
109109

110-
cs, cons, err := src.CreateInitialLightClientState(srcHeight)
110+
cs, cons, err := src.CreateInitialLightClientState(context.TODO(), srcHeight)
111111
if err != nil {
112112
logger.Error("failed to create initial light client state", err)
113113
return err

0 commit comments

Comments
 (0)