Skip to content

Add context.Context to methods defined in chain, prover, and signer module interfaces #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions chains/tendermint/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (c *Chain) SetupForRelay(ctx context.Context) error {
}

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

func (c *Chain) Timestamp(height ibcexported.Height) (time.Time, error) {
func (c *Chain) Timestamp(ctx context.Context, height ibcexported.Height) (time.Time, error) {
ht := int64(height.GetRevisionHeight())
if header, err := c.Client.Header(context.TODO(), &ht); err != nil {
return time.Time{}, err
Expand Down Expand Up @@ -193,7 +193,7 @@ func (c *Chain) sendMsgs(msgs []sdk.Msg) (*sdk.TxResponse, error) {

// call msgEventListener if needed
if c.msgEventListener != nil {
if err := c.msgEventListener.OnSentMsg(msgs); err != nil {
if err := c.msgEventListener.OnSentMsg(context.TODO(), msgs); err != nil {
logger.Error("failed to OnSendMsg call", err)
return res, nil
}
Expand Down Expand Up @@ -280,7 +280,7 @@ func (c *Chain) waitForCommit(txHash string) (*coretypes.ResultTx, error) {
// proofs of states updated up to height N are available.
// In order to make the proof of the state updated by a tx available just after `sendMsgs`,
// `waitForCommit` must wait until the latest height is greater than the tx height.
if height, err := c.LatestHeight(); err != nil {
if height, err := c.LatestHeight(context.TODO()); err != nil {
return fmt.Errorf("failed to obtain latest height: %v", err)
} else if height.GetRevisionHeight() <= uint64(resTx.Height) {
return fmt.Errorf("latest_height(%v) is less than or equal to tx_height(%v) yet", height, resTx.Height)
Expand Down Expand Up @@ -404,7 +404,7 @@ func CalculateGas(
return simRes, uint64(txf.GasAdjustment() * float64(simRes.GasInfo.GasUsed)), nil
}

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

func (c *Chain) GetMsgResult(id core.MsgID) (core.MsgResult, error) {
func (c *Chain) GetMsgResult(ctx context.Context, id core.MsgID) (core.MsgResult, error) {
msgID, ok := id.(*MsgID)
if !ok {
return nil, fmt.Errorf("unexpected message id type: %T", id)
Expand Down
4 changes: 2 additions & 2 deletions chains/tendermint/light.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ func (pr *Prover) LightClientWithoutTrust(db dbm.DB) (*light.Client, error) {
prov := pr.LightHTTP()

if err := retry.Do(func() error {
h, err := pr.chain.LatestHeight()
h, err := pr.chain.LatestHeight(context.TODO())
switch {
case err != nil:
return err
case h.GetRevisionHeight() == 0:
return fmt.Errorf("shouldn't be here")
default:
t, err := pr.chain.Timestamp(h)
t, err := pr.chain.Timestamp(context.TODO(), h)
if err != nil {
return err
}
Expand Down
16 changes: 8 additions & 8 deletions chains/tendermint/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (pr *Prover) ProveHostConsensusState(ctx core.QueryContext, height ibcexpor
/* LightClient implementation */

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

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

cph, err := counterparty.LatestHeight()
cph, err := counterparty.LatestHeight(context.TODO())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -127,12 +127,12 @@ func (pr *Prover) SetupHeadersForUpdate(counterparty core.FinalityAwareChain, la
}

// GetLatestFinalizedHeader returns the latest finalized header
func (pr *Prover) GetLatestFinalizedHeader() (core.Header, error) {
func (pr *Prover) GetLatestFinalizedHeader(ctx context.Context) (core.Header, error) {
return pr.UpdateLightClient(0)
}

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

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

selfTimestamp, err := pr.chain.Timestamp(selfQueryHeight)
selfTimestamp, err := pr.chain.Timestamp(context.TODO(), selfQueryHeight)
if err != nil {
return false, fmt.Errorf("failed to get timestamp of the self chain: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions chains/tendermint/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func (c *Chain) QueryUnfinalizedRelayPackets(ctx core.QueryContext, counterparty
}

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

var counterpartyCtx core.QueryContext
if counterpartyH, err := counterparty.GetLatestFinalizedHeader(); err != nil {
if counterpartyH, err := counterparty.GetLatestFinalizedHeader(context.TODO()); err != nil {
return nil, fmt.Errorf("failed to get latest finalized header: error=%w height=%v", err, ctx.Height())
} else {
counterpartyCtx = core.NewQueryContext(context.TODO(), counterpartyH.GetHeight())
Expand Down
10 changes: 5 additions & 5 deletions cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func queryClientCmd(ctx *config.Context) *cobra.Command {
if err != nil {
return err
}
latestHeight, err := c.LatestHeight()
latestHeight, err := c.LatestHeight(context.TODO())
if err != nil {
return err
}
Expand Down Expand Up @@ -98,7 +98,7 @@ func queryConnection(ctx *config.Context) *cobra.Command {
if err != nil {
return err
}
latestHeight, err := c.LatestHeight()
latestHeight, err := c.LatestHeight(context.TODO())
if err != nil {
return err
}
Expand Down Expand Up @@ -136,7 +136,7 @@ func queryChannel(ctx *config.Context) *cobra.Command {
if err != nil {
return err
}
latestHeight, err := c.LatestHeight()
latestHeight, err := c.LatestHeight(context.TODO())
if err != nil {
return err
}
Expand Down Expand Up @@ -175,7 +175,7 @@ func queryChannelUpgrade(ctx *config.Context) *cobra.Command {
if err != nil {
return err
}
latestHeight, err := c.LatestHeight()
latestHeight, err := c.LatestHeight(context.TODO())
if err != nil {
return err
}
Expand Down Expand Up @@ -221,7 +221,7 @@ func queryBalanceCmd(ctx *config.Context) *cobra.Command {
return err
}

h, err := chain.LatestHeight()
h, err := chain.LatestHeight(context.TODO())
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func createClientsCmd(ctx *config.Context) *cobra.Command {
return err
} else if height == 0 {
srcHeight = nil
} else if latestHeight, err := c[src].LatestHeight(); err != nil {
} else if latestHeight, err := c[src].LatestHeight(context.TODO()); err != nil {
return fmt.Errorf("failed to get the latest height of src chain: %v", err)
} else {
srcHeight = clienttypes.NewHeight(latestHeight.GetRevisionNumber(), height)
Expand All @@ -93,7 +93,7 @@ func createClientsCmd(ctx *config.Context) *cobra.Command {
return err
} else if height == 0 {
dstHeight = nil
} else if latestHeight, err := c[dst].LatestHeight(); err != nil {
} else if latestHeight, err := c[dst].LatestHeight(context.TODO()); err != nil {
return fmt.Errorf("failed to get the latest height of dst chain: %v", err)
} else {
dstHeight = clienttypes.NewHeight(latestHeight.GetRevisionNumber(), height)
Expand Down
10 changes: 5 additions & 5 deletions core/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ type Chain interface {
// SendMsgs sends msgs to the chain and waits for them to be included in blocks.
// This function returns err=nil only if all the msgs executed successfully at the blocks.
// It should be noted that the block is not finalized at that point and can be reverted afterwards.
SendMsgs(msgs []sdk.Msg) ([]MsgID, error)
SendMsgs(ctx context.Context, msgs []sdk.Msg) ([]MsgID, error)

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

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

// Timestamp returns the block timestamp
Timestamp(ibcexported.Height) (time.Time, error)
Timestamp(ctx context.Context, height ibcexported.Height) (time.Time, error)

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

// IBCQuerier is an interface to the state of IBC
Expand Down
18 changes: 9 additions & 9 deletions core/channel-upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ func InitChannelUpgrade(chain, cp *ProvableChain, upgradeFields chantypes.Upgrad
logger := GetChannelLogger(chain.Chain)
defer logger.TimeTrack(time.Now(), "InitChannelUpgrade")

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

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

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

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

// prepare QueryContext's based on the latest heights
var srcLatestCtx, dstLatestCtx QueryContext
if h, err := src.LatestHeight(); err != nil {
if h, err := src.LatestHeight(context.TODO()); err != nil {
logger.Error("failed to get the latest height of the src chain", err)
return nil, nil, false, err
} else {
srcLatestCtx = NewQueryContext(context.TODO(), h)
}
if h, err := dst.LatestHeight(); err != nil {
if h, err := dst.LatestHeight(context.TODO()); err != nil {
logger.Error("failed to get the latest height of the dst chain", err)
return nil, nil, false, err
} else {
Expand Down Expand Up @@ -712,7 +712,7 @@ func upgradeAlreadyTimedOut(
cpChanUpg *chantypes.QueryUpgradeResponse,
) (bool, error) {
height := ctx.Height().(clienttypes.Height)
timestamp, err := chain.Timestamp(height)
timestamp, err := chain.Timestamp(context.TODO(), height)
if err != nil {
return false, err
}
Expand Down
6 changes: 3 additions & 3 deletions core/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func checkChannelCreateReady(src, dst *ProvableChain, logger *log.RelayLogger) (
return chantypes.UNINITIALIZED, nil
}

latestHeight, err := pc.LatestHeight()
latestHeight, err := pc.LatestHeight(context.TODO())
if err != nil {
return chantypes.UNINITIALIZED, err
}
Expand Down Expand Up @@ -268,13 +268,13 @@ func querySettledChannelPair(
}

var srcLatestCtx, dstLatestCtx QueryContext
if h, err := src.LatestHeight(); err != nil {
if h, err := src.LatestHeight(context.TODO()); err != nil {
logger.Error("failed to get the latest height of the src chain", err)
return nil, nil, false, err
} else {
srcLatestCtx = NewQueryContext(context.TODO(), h)
}
if h, err := dst.LatestHeight(); err != nil {
if h, err := dst.LatestHeight(context.TODO()); err != nil {
logger.Error("failed to get the latest height of the dst chain", err)
return nil, nil, false, err
} else {
Expand Down
6 changes: 3 additions & 3 deletions core/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func checkCreateClientsReady(src, dst *ProvableChain, logger *log.RelayLogger) (
}

getState := func(pc *ProvableChain) (*clienttypes.QueryClientStateResponse, error) {
latestHeight, err := pc.LatestHeight()
latestHeight, err := pc.LatestHeight(context.TODO())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -85,7 +85,7 @@ func CreateClients(pathName string, src, dst *ProvableChain, srcHeight, dstHeigh
return err
}

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

cs, cons, err := src.CreateInitialLightClientState(srcHeight)
cs, cons, err := src.CreateInitialLightClientState(context.TODO(), srcHeight)
if err != nil {
logger.Error("failed to create initial light client state", err)
return err
Expand Down
2 changes: 1 addition & 1 deletion core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func SyncChainConfigFromEvents(pathName string, msgIDs []MsgID, chain *ProvableC
if msgID == nil {
continue
}
msgRes, err := chain.Chain.GetMsgResult(msgID)
msgRes, err := chain.Chain.GetMsgResult(context.TODO(), msgID)
if err != nil {
return fmt.Errorf("failed to get message result: %v", err)
} else if ok, failureReason := msgRes.Status(); !ok {
Expand Down
Loading