From 44f709ace2eb52f530166a20d62741c27d67ea11 Mon Sep 17 00:00:00 2001 From: Marko Baricevic Date: Wed, 28 Aug 2024 15:52:04 +0200 Subject: [PATCH] more context cleanup --- .../controller/keeper/export_test.go | 4 +- .../controller/keeper/migrations.go | 8 +-- .../controller/keeper/relay.go | 7 +-- .../host/ibc_module_test.go | 2 +- .../host/keeper/export_test.go | 4 +- .../host/keeper/genesis.go | 7 ++- .../host/keeper/migrations.go | 8 +-- .../host/keeper/relay.go | 2 +- .../host/keeper/relay_test.go | 3 +- modules/apps/29-fee/keeper/genesis.go | 6 +-- modules/apps/29-fee/keeper/migrations.go | 2 +- modules/core/04-channel/keeper/events.go | 21 +++++--- modules/core/04-channel/keeper/migrations.go | 1 - modules/core/04-channel/keeper/packet.go | 19 ++++--- modules/core/04-channel/keeper/timeout.go | 9 ++-- modules/core/04-channel/keeper/upgrade.go | 54 ++++++++++--------- .../07-tendermint/migrations/migrations.go | 5 +- modules/light-clients/07-tendermint/store.go | 6 +-- 18 files changed, 92 insertions(+), 76 deletions(-) diff --git a/modules/apps/27-interchain-accounts/controller/keeper/export_test.go b/modules/apps/27-interchain-accounts/controller/keeper/export_test.go index 154d8d3aee6..c580aa61005 100644 --- a/modules/apps/27-interchain-accounts/controller/keeper/export_test.go +++ b/modules/apps/27-interchain-accounts/controller/keeper/export_test.go @@ -5,12 +5,12 @@ package keeper */ import ( - sdk "github.com/cosmos/cosmos-sdk/types" + "context" icatypes "github.com/cosmos/ibc-go/v9/modules/apps/27-interchain-accounts/types" ) // GetAppMetadata is a wrapper around getAppMetadata to allow the function to be directly called in tests. -func (k Keeper) GetAppMetadata(ctx sdk.Context, portID, channelID string) (icatypes.Metadata, error) { +func (k Keeper) GetAppMetadata(ctx context.Context, portID, channelID string) (icatypes.Metadata, error) { return k.getAppMetadata(ctx, portID, channelID) } diff --git a/modules/apps/27-interchain-accounts/controller/keeper/migrations.go b/modules/apps/27-interchain-accounts/controller/keeper/migrations.go index ad69ceacc65..4ca20081e22 100644 --- a/modules/apps/27-interchain-accounts/controller/keeper/migrations.go +++ b/modules/apps/27-interchain-accounts/controller/keeper/migrations.go @@ -1,8 +1,9 @@ package keeper import ( - sdk "github.com/cosmos/cosmos-sdk/types" + "context" + sdk "github.com/cosmos/cosmos-sdk/types" controllertypes "github.com/cosmos/ibc-go/v9/modules/apps/27-interchain-accounts/controller/types" ) @@ -19,11 +20,12 @@ func NewMigrator(k *Keeper) Migrator { } // MigrateParams migrates the controller submodule's parameters from the x/params to self store. -func (m Migrator) MigrateParams(ctx sdk.Context) error { +func (m Migrator) MigrateParams(ctx context.Context) error { if m.keeper != nil { params := controllertypes.DefaultParams() if m.keeper.legacySubspace != nil { - m.keeper.legacySubspace.GetParamSetIfExists(ctx, ¶ms) + sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: remove when Upgrading to 52 + m.keeper.legacySubspace.GetParamSetIfExists(sdkCtx, ¶ms) } m.keeper.SetParams(ctx, params) m.keeper.Logger(ctx).Info("successfully migrated ica/controller submodule to self-manage params") diff --git a/modules/apps/27-interchain-accounts/controller/keeper/relay.go b/modules/apps/27-interchain-accounts/controller/keeper/relay.go index 280328727b0..09d84a38847 100644 --- a/modules/apps/27-interchain-accounts/controller/keeper/relay.go +++ b/modules/apps/27-interchain-accounts/controller/keeper/relay.go @@ -25,11 +25,11 @@ import ( // Prior to v6.x.x of ibc-go, the controller module was only functional as middleware, with authentication performed // by the underlying application. For a full summary of the changes in v6.x.x, please see ADR009. // This API will be removed in later releases. -func (k Keeper) SendTx(ctx sdk.Context, _ *capabilitytypes.Capability, connectionID, portID string, icaPacketData icatypes.InterchainAccountPacketData, timeoutTimestamp uint64) (uint64, error) { +func (k Keeper) SendTx(ctx context.Context, _ *capabilitytypes.Capability, connectionID, portID string, icaPacketData icatypes.InterchainAccountPacketData, timeoutTimestamp uint64) (uint64, error) { return k.sendTx(ctx, connectionID, portID, icaPacketData, timeoutTimestamp) } -func (k Keeper) sendTx(ctx sdk.Context, connectionID, portID string, icaPacketData icatypes.InterchainAccountPacketData, timeoutTimestamp uint64) (uint64, error) { +func (k Keeper) sendTx(ctx context.Context, connectionID, portID string, icaPacketData icatypes.InterchainAccountPacketData, timeoutTimestamp uint64) (uint64, error) { if !k.GetParams(ctx).ControllerEnabled { return 0, types.ErrControllerSubModuleDisabled } @@ -44,7 +44,8 @@ func (k Keeper) sendTx(ctx sdk.Context, connectionID, portID string, icaPacketDa return 0, errorsmod.Wrapf(capabilitytypes.ErrCapabilityNotFound, "failed to find capability: %s", host.ChannelCapabilityPath(portID, activeChannelID)) } - if uint64(ctx.BlockTime().UnixNano()) >= timeoutTimestamp { + sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/5917 + if uint64(sdkCtx.BlockTime().UnixNano()) >= timeoutTimestamp { return 0, icatypes.ErrInvalidTimeoutTimestamp } diff --git a/modules/apps/27-interchain-accounts/host/ibc_module_test.go b/modules/apps/27-interchain-accounts/host/ibc_module_test.go index ba88fbdd3fa..7ef5894ad4d 100644 --- a/modules/apps/27-interchain-accounts/host/ibc_module_test.go +++ b/modules/apps/27-interchain-accounts/host/ibc_module_test.go @@ -785,7 +785,7 @@ func (suite *InterchainAccountsTestSuite) TestOnChanUpgradeAck() { } } -func (suite *InterchainAccountsTestSuite) fundICAWallet(ctx sdk.Context, portID string, amount sdk.Coins) { +func (suite *InterchainAccountsTestSuite) fundICAWallet(ctx context.Context, portID string, amount sdk.Coins) { interchainAccountAddr, found := suite.chainB.GetSimApp().ICAHostKeeper.GetInterchainAccountAddress(ctx, ibctesting.FirstConnectionID, portID) suite.Require().True(found) diff --git a/modules/apps/27-interchain-accounts/host/keeper/export_test.go b/modules/apps/27-interchain-accounts/host/keeper/export_test.go index 0d33f9e5006..c9916e1b3b4 100644 --- a/modules/apps/27-interchain-accounts/host/keeper/export_test.go +++ b/modules/apps/27-interchain-accounts/host/keeper/export_test.go @@ -5,13 +5,13 @@ package keeper */ import ( - sdk "github.com/cosmos/cosmos-sdk/types" + "context" icatypes "github.com/cosmos/ibc-go/v9/modules/apps/27-interchain-accounts/types" ) // GetAppMetadata is a wrapper around getAppMetadata to allow the function to be directly called in tests. -func (k Keeper) GetAppMetadata(ctx sdk.Context, portID, channelID string) (icatypes.Metadata, error) { +func (k Keeper) GetAppMetadata(ctx context.Context, portID, channelID string) (icatypes.Metadata, error) { return k.getAppMetadata(ctx, portID, channelID) } diff --git a/modules/apps/27-interchain-accounts/host/keeper/genesis.go b/modules/apps/27-interchain-accounts/host/keeper/genesis.go index 4af3cbe7f39..9fc935907a4 100644 --- a/modules/apps/27-interchain-accounts/host/keeper/genesis.go +++ b/modules/apps/27-interchain-accounts/host/keeper/genesis.go @@ -1,17 +1,16 @@ package keeper import ( + "context" "fmt" - sdk "github.com/cosmos/cosmos-sdk/types" - genesistypes "github.com/cosmos/ibc-go/v9/modules/apps/27-interchain-accounts/genesis/types" icatypes "github.com/cosmos/ibc-go/v9/modules/apps/27-interchain-accounts/types" host "github.com/cosmos/ibc-go/v9/modules/core/24-host" ) // InitGenesis initializes the interchain accounts host application state from a provided genesis state -func InitGenesis(ctx sdk.Context, keeper Keeper, state genesistypes.HostGenesisState) { +func InitGenesis(ctx context.Context, keeper Keeper, state genesistypes.HostGenesisState) { keeper.setPort(ctx, state.Port) // generate port capability if it does not already exist @@ -40,7 +39,7 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, state genesistypes.HostGenesisS } // ExportGenesis returns the interchain accounts host exported genesis -func ExportGenesis(ctx sdk.Context, keeper Keeper) genesistypes.HostGenesisState { +func ExportGenesis(ctx context.Context, keeper Keeper) genesistypes.HostGenesisState { return genesistypes.NewHostGenesisState( keeper.GetAllActiveChannels(ctx), keeper.GetAllInterchainAccounts(ctx), diff --git a/modules/apps/27-interchain-accounts/host/keeper/migrations.go b/modules/apps/27-interchain-accounts/host/keeper/migrations.go index 1dcc95fd71c..fed0aa02648 100644 --- a/modules/apps/27-interchain-accounts/host/keeper/migrations.go +++ b/modules/apps/27-interchain-accounts/host/keeper/migrations.go @@ -1,8 +1,9 @@ package keeper import ( - sdk "github.com/cosmos/cosmos-sdk/types" + "context" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/ibc-go/v9/modules/apps/27-interchain-accounts/host/types" ) @@ -19,11 +20,12 @@ func NewMigrator(k *Keeper) Migrator { } // MigrateParams migrates the host submodule's parameters from the x/params to self store. -func (m Migrator) MigrateParams(ctx sdk.Context) error { +func (m Migrator) MigrateParams(ctx context.Context) error { if m.keeper != nil { params := types.DefaultParams() if m.keeper.legacySubspace != nil { - m.keeper.legacySubspace.GetParamSetIfExists(ctx, ¶ms) + sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: should we remove legacy migrations? + m.keeper.legacySubspace.GetParamSetIfExists(sdkCtx, ¶ms) } if err := params.Validate(); err != nil { return err diff --git a/modules/apps/27-interchain-accounts/host/keeper/relay.go b/modules/apps/27-interchain-accounts/host/keeper/relay.go index cc395a1197a..7d31d382570 100644 --- a/modules/apps/27-interchain-accounts/host/keeper/relay.go +++ b/modules/apps/27-interchain-accounts/host/keeper/relay.go @@ -131,7 +131,7 @@ func (k Keeper) authenticateTx(ctx context.Context, msgs []sdk.Msg, connectionID // Attempts to get the message handler from the router and if found will then execute the message. // If the message execution is successful, the proto marshaled message response will be returned. -func (k Keeper) executeMsg(ctx sdk.Context, msg sdk.Msg) (*codectypes.Any, error) { +func (k Keeper) executeMsg(ctx sdk.Context, msg sdk.Msg) (*codectypes.Any, error) { // TODO: remove sdk.COntext when migrating to 52 handler := k.msgRouter.Handler(msg) if handler == nil { return nil, icatypes.ErrInvalidRoute diff --git a/modules/apps/27-interchain-accounts/host/keeper/relay_test.go b/modules/apps/27-interchain-accounts/host/keeper/relay_test.go index b4cbacfcf01..75e5962854a 100644 --- a/modules/apps/27-interchain-accounts/host/keeper/relay_test.go +++ b/modules/apps/27-interchain-accounts/host/keeper/relay_test.go @@ -1,6 +1,7 @@ package keeper_test import ( + "context" "fmt" "strings" "time" @@ -886,7 +887,7 @@ func (suite *KeeperTestSuite) TestJSONOnRecvPacket() { } } -func (suite *KeeperTestSuite) fundICAWallet(ctx sdk.Context, portID string, amount sdk.Coins) { +func (suite *KeeperTestSuite) fundICAWallet(ctx context.Context, portID string, amount sdk.Coins) { interchainAccountAddr, found := suite.chainB.GetSimApp().ICAHostKeeper.GetInterchainAccountAddress(ctx, ibctesting.FirstConnectionID, portID) suite.Require().True(found) diff --git a/modules/apps/29-fee/keeper/genesis.go b/modules/apps/29-fee/keeper/genesis.go index 913f24c72fd..c407238f772 100644 --- a/modules/apps/29-fee/keeper/genesis.go +++ b/modules/apps/29-fee/keeper/genesis.go @@ -1,13 +1,13 @@ package keeper import ( - sdk "github.com/cosmos/cosmos-sdk/types" + "context" "github.com/cosmos/ibc-go/v9/modules/apps/29-fee/types" ) // InitGenesis initializes the fee middleware application state from a provided genesis state -func (k Keeper) InitGenesis(ctx sdk.Context, state types.GenesisState) { +func (k Keeper) InitGenesis(ctx context.Context, state types.GenesisState) { for _, identifiedFees := range state.IdentifiedFees { k.SetFeesInEscrow(ctx, identifiedFees.PacketId, types.NewPacketFees(identifiedFees.PacketFees)) } @@ -30,7 +30,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, state types.GenesisState) { } // ExportGenesis returns the fee middleware application exported genesis -func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { +func (k Keeper) ExportGenesis(ctx context.Context) *types.GenesisState { return &types.GenesisState{ IdentifiedFees: k.GetAllIdentifiedPacketFees(ctx), FeeEnabledChannels: k.GetAllFeeEnabledChannels(ctx), diff --git a/modules/apps/29-fee/keeper/migrations.go b/modules/apps/29-fee/keeper/migrations.go index 25408c95954..5e538a3a3ca 100644 --- a/modules/apps/29-fee/keeper/migrations.go +++ b/modules/apps/29-fee/keeper/migrations.go @@ -23,7 +23,7 @@ func NewMigrator(keeper Keeper) Migrator { // Migrate1to2 migrates ibc-fee module from ConsensusVersion 1 to 2 // by refunding leftover fees to the refund address. -func (m Migrator) Migrate1to2(ctx sdk.Context) error { +func (m Migrator) Migrate1to2(ctx sdk.Context) error { //TODO: should this be removed? store := runtime.KVStoreAdapter(m.keeper.storeService.OpenKVStore(ctx)) iterator := storetypes.KVStorePrefixIterator(store, []byte(types.FeesInEscrowPrefix)) defer sdk.LogDeferred(ctx.Logger(), func() error { return iterator.Close() }) diff --git a/modules/core/04-channel/keeper/events.go b/modules/core/04-channel/keeper/events.go index 6b7de9e973f..abb52101145 100644 --- a/modules/core/04-channel/keeper/events.go +++ b/modules/core/04-channel/keeper/events.go @@ -1,6 +1,7 @@ package keeper import ( + "context" "encoding/hex" "fmt" @@ -228,8 +229,9 @@ func emitAcknowledgePacketEvent(ctx sdk.Context, packet types.Packet, channel ty // emitTimeoutPacketEvent emits a timeout packet event. It will be emitted both the first time a packet // is timed out for a certain sequence and for all duplicate timeouts. -func emitTimeoutPacketEvent(ctx sdk.Context, packet types.Packet, channel types.Channel) { - ctx.EventManager().EmitEvents(sdk.Events{ +func emitTimeoutPacketEvent(ctx context.Context, packet types.Packet, channel types.Channel) { + sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: remove when Upgrading to 52 + sdkCtx.EventManager().EmitEvents(sdk.Events{ sdk.NewEvent( types.EventTypeTimeoutPacket, sdk.NewAttribute(types.AttributeKeyTimeoutHeight, packet.GetTimeoutHeight().String()), @@ -250,8 +252,9 @@ func emitTimeoutPacketEvent(ctx sdk.Context, packet types.Packet, channel types. } // emitChannelClosedEvent emits a channel closed event. -func emitChannelClosedEvent(ctx sdk.Context, packet types.Packet, channel types.Channel) { - ctx.EventManager().EmitEvents(sdk.Events{ +func emitChannelClosedEvent(ctx context.Context, packet types.Packet, channel types.Channel) { + sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: remove when Upgrading to 52 + sdkCtx.EventManager().EmitEvents(sdk.Events{ sdk.NewEvent( types.EventTypeChannelClosed, sdk.NewAttribute(types.AttributeKeyPortID, packet.GetSourcePort()), @@ -381,8 +384,9 @@ func EmitChannelUpgradeTimeoutEvent(ctx sdk.Context, portID string, channelID st } // EmitErrorReceiptEvent emits an error receipt event -func EmitErrorReceiptEvent(ctx sdk.Context, portID string, channelID string, channel types.Channel, err error) { - ctx.EventManager().EmitEvents(sdk.Events{ +func EmitErrorReceiptEvent(ctx context.Context, portID string, channelID string, channel types.Channel, err error) { + sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: remove when Upgrading to 52 + sdkCtx.EventManager().EmitEvents(sdk.Events{ sdk.NewEvent( types.EventTypeChannelUpgradeError, sdk.NewAttribute(types.AttributeKeyPortID, portID), @@ -418,8 +422,9 @@ func EmitChannelUpgradeCancelEvent(ctx sdk.Context, portID string, channelID str } // emitChannelFlushCompleteEvent emits an flushing event. -func emitChannelFlushCompleteEvent(ctx sdk.Context, portID string, channelID string, channel types.Channel) { - ctx.EventManager().EmitEvents(sdk.Events{ +func emitChannelFlushCompleteEvent(ctx context.Context, portID string, channelID string, channel types.Channel) { + sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: remove when Upgrading to 52 + sdkCtx.EventManager().EmitEvents(sdk.Events{ sdk.NewEvent( types.EventTypeChannelFlushComplete, sdk.NewAttribute(types.AttributeKeyPortID, portID), diff --git a/modules/core/04-channel/keeper/migrations.go b/modules/core/04-channel/keeper/migrations.go index 9a382d26265..2103a220d4e 100644 --- a/modules/core/04-channel/keeper/migrations.go +++ b/modules/core/04-channel/keeper/migrations.go @@ -2,7 +2,6 @@ package keeper import ( sdk "github.com/cosmos/cosmos-sdk/types" - channeltypes "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types" ) diff --git a/modules/core/04-channel/keeper/packet.go b/modules/core/04-channel/keeper/packet.go index 3c444200249..5b8136dfb6e 100644 --- a/modules/core/04-channel/keeper/packet.go +++ b/modules/core/04-channel/keeper/packet.go @@ -108,7 +108,7 @@ func (k *Keeper) SendPacket( // RecvPacket is called by a module in order to receive & process an IBC packet // sent on the corresponding channel end on the counterparty chain. func (k *Keeper) RecvPacket( - ctx sdk.Context, + ctx context.Context, chanCap *capabilitytypes.Capability, packet types.Packet, proof []byte, @@ -173,7 +173,8 @@ func (k *Keeper) RecvPacket( } // check if packet timed out by comparing it with the latest height of the chain - selfHeight, selfTimestamp := clienttypes.GetSelfHeight(ctx), uint64(ctx.BlockTime().UnixNano()) + sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: remove when Upgrading to 52 + selfHeight, selfTimestamp := clienttypes.GetSelfHeight(sdkCtx), uint64(sdkCtx.BlockTime().UnixNano()) timeout := types.NewTimeout(packet.GetTimeoutHeight().(clienttypes.Height), packet.GetTimeoutTimestamp()) if timeout.Elapsed(selfHeight, selfTimestamp) { return "", errorsmod.Wrap(timeout.ErrTimeoutElapsed(selfHeight, selfTimestamp), "packet timeout elapsed") @@ -205,14 +206,14 @@ func (k *Keeper) RecvPacket( ) // emit an event that the relayer can query for - emitRecvPacketEvent(ctx, packet, channel) + emitRecvPacketEvent(sdkCtx, packet, channel) return channel.Version, nil } // applyReplayProtection ensures a packet has not already been received // and performs the necessary state changes to ensure it cannot be received again. -func (k *Keeper) applyReplayProtection(ctx sdk.Context, packet types.Packet, channel types.Channel) error { +func (k *Keeper) applyReplayProtection(ctx context.Context, packet types.Packet, channel types.Channel) error { // REPLAY PROTECTION: The recvStartSequence will prevent historical proofs from allowing replay // attacks on packets processed in previous lifecycles of a channel. After a successful channel // upgrade all packets under the recvStartSequence will have been processed and thus should be @@ -222,6 +223,7 @@ func (k *Keeper) applyReplayProtection(ctx sdk.Context, packet types.Packet, cha return errorsmod.Wrap(types.ErrPacketReceived, "packet already processed in previous channel upgrade") } + sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: remove when Upgrading to 52 switch channel.Ordering { case types.UNORDERED: // REPLAY PROTECTION: Packet receipts will indicate that a packet has already been received @@ -229,7 +231,7 @@ func (k *Keeper) applyReplayProtection(ctx sdk.Context, packet types.Packet, cha // by the increase of the recvStartSequence. _, found := k.GetPacketReceipt(ctx, packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence()) if found { - emitRecvPacketEvent(ctx, packet, channel) + emitRecvPacketEvent(sdkCtx, packet, channel) // This error indicates that the packet has already been relayed. Core IBC will // treat this error as a no-op in order to prevent an entire relay transaction // from failing and consuming unnecessary fees. @@ -253,7 +255,7 @@ func (k *Keeper) applyReplayProtection(ctx sdk.Context, packet types.Packet, cha } if packet.GetSequence() < nextSequenceRecv { - emitRecvPacketEvent(ctx, packet, channel) + emitRecvPacketEvent(sdkCtx, packet, channel) // This error indicates that the packet has already been relayed. Core IBC will // treat this error as a no-op in order to prevent an entire relay transaction // from failing and consuming unnecessary fees. @@ -502,10 +504,11 @@ func (k *Keeper) AcknowledgePacket( // FLUSHING state. It checks if the upgrade has timed out and if so, aborts the upgrade. If all // packets have completed their lifecycle, it sets the channel state to FLUSHCOMPLETE and // emits a channel_flush_complete event. Returns true if the upgrade was aborted, false otherwise. -func (k *Keeper) handleFlushState(ctx sdk.Context, packet types.Packet, channel types.Channel) { +func (k *Keeper) handleFlushState(ctx context.Context, packet types.Packet, channel types.Channel) { if counterpartyUpgrade, found := k.GetCounterpartyUpgrade(ctx, packet.GetSourcePort(), packet.GetSourceChannel()); found { timeout := counterpartyUpgrade.Timeout - selfHeight, selfTimestamp := clienttypes.GetSelfHeight(ctx), uint64(ctx.BlockTime().UnixNano()) + sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: remove when Upgrading to 52 + selfHeight, selfTimestamp := clienttypes.GetSelfHeight(sdkCtx), uint64(sdkCtx.BlockTime().UnixNano()) if timeout.Elapsed(selfHeight, selfTimestamp) { // packet flushing timeout has expired, abort the upgrade diff --git a/modules/core/04-channel/keeper/timeout.go b/modules/core/04-channel/keeper/timeout.go index 99d38a9657e..06f93974e98 100644 --- a/modules/core/04-channel/keeper/timeout.go +++ b/modules/core/04-channel/keeper/timeout.go @@ -2,12 +2,11 @@ package keeper import ( "bytes" + "context" "strconv" errorsmod "cosmossdk.io/errors" - sdk "github.com/cosmos/cosmos-sdk/types" - capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types" clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types" connectiontypes "github.com/cosmos/ibc-go/v9/modules/core/03-connection/types" @@ -23,7 +22,7 @@ import ( // perform appropriate state transitions. Its intended usage is within the // ante handler. func (k *Keeper) TimeoutPacket( - ctx sdk.Context, + ctx context.Context, packet types.Packet, proof []byte, proofHeight exported.Height, @@ -131,7 +130,7 @@ func (k *Keeper) TimeoutPacket( // // CONTRACT: this function must be called in the IBC handler func (k *Keeper) TimeoutExecuted( - ctx sdk.Context, + ctx context.Context, chanCap *capabilitytypes.Capability, packet types.Packet, ) error { @@ -192,7 +191,7 @@ func (k *Keeper) TimeoutExecuted( // which an unreceived packet was addressed has been closed, so the packet will // never be received (even if the timeoutHeight has not yet been reached). func (k *Keeper) TimeoutOnClose( - ctx sdk.Context, + ctx context.Context, chanCap *capabilitytypes.Capability, packet types.Packet, proof, diff --git a/modules/core/04-channel/keeper/upgrade.go b/modules/core/04-channel/keeper/upgrade.go index 2fd44ca10b5..bee91fd8679 100644 --- a/modules/core/04-channel/keeper/upgrade.go +++ b/modules/core/04-channel/keeper/upgrade.go @@ -1,6 +1,7 @@ package keeper import ( + "context" "fmt" "reflect" "slices" @@ -20,7 +21,7 @@ import ( // ChanUpgradeInit is called by a module to initiate a channel upgrade handshake with // a module on another chain. func (k *Keeper) ChanUpgradeInit( - ctx sdk.Context, + ctx context.Context, portID string, channelID string, upgradeFields types.UpgradeFields, @@ -45,7 +46,7 @@ func (k *Keeper) ChanUpgradeInit( // WriteUpgradeInitChannel writes a channel which has successfully passed the UpgradeInit handshake step. // An event is emitted for the handshake step. -func (k *Keeper) WriteUpgradeInitChannel(ctx sdk.Context, portID, channelID string, upgrade types.Upgrade, upgradeVersion string) (types.Channel, types.Upgrade) { +func (k *Keeper) WriteUpgradeInitChannel(ctx context.Context, portID, channelID string, upgrade types.Upgrade, upgradeVersion string) (types.Channel, types.Upgrade) { defer telemetry.IncrCounter(1, "ibc", "channel", "upgrade-init") channel, found := k.GetChannel(ctx, portID, channelID) @@ -74,7 +75,7 @@ func (k *Keeper) WriteUpgradeInitChannel(ctx sdk.Context, portID, channelID stri // ChanUpgradeTry is called by a module to accept the first step of a channel upgrade handshake initiated by // a module on another chain. If this function is successful, the proposed upgrade will be returned. If the upgrade fails, the upgrade sequence will still be incremented but an error will be returned. func (k *Keeper) ChanUpgradeTry( - ctx sdk.Context, + ctx context.Context, portID, channelID string, proposedConnectionHops []string, @@ -223,7 +224,7 @@ func (k *Keeper) ChanUpgradeTry( // WriteUpgradeTryChannel writes the channel end and upgrade to state after successfully passing the UpgradeTry handshake step. // An event is emitted for the handshake step. -func (k *Keeper) WriteUpgradeTryChannel(ctx sdk.Context, portID, channelID string, upgrade types.Upgrade, upgradeVersion string) (types.Channel, types.Upgrade) { +func (k *Keeper) WriteUpgradeTryChannel(ctx context.Context, portID, channelID string, upgrade types.Upgrade, upgradeVersion string) (types.Channel, types.Upgrade) { defer telemetry.IncrCounter(1, "ibc", "channel", "upgrade-try") channel, found := k.GetChannel(ctx, portID, channelID) @@ -252,7 +253,7 @@ func (k *Keeper) WriteUpgradeTryChannel(ctx sdk.Context, portID, channelID strin // // A -> Init (OPEN), B -> Init (OPEN) -> A -> Try (FLUSHING), B -> Try (FLUSHING), A -> Ack (begins in FLUSHING) func (k *Keeper) ChanUpgradeAck( - ctx sdk.Context, + ctx context.Context, portID, channelID string, counterpartyUpgrade types.Upgrade, @@ -340,7 +341,8 @@ func (k *Keeper) ChanUpgradeAck( } timeout := counterpartyUpgrade.Timeout - selfHeight, selfTimestamp := clienttypes.GetSelfHeight(ctx), uint64(ctx.BlockTime().UnixNano()) + sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: remove when Upgrading to 52 + selfHeight, selfTimestamp := clienttypes.GetSelfHeight(sdkCtx), uint64(sdkCtx.BlockTime().UnixNano()) if timeout.Elapsed(selfHeight, selfTimestamp) { return types.NewUpgradeError(channel.UpgradeSequence, errorsmod.Wrap(timeout.ErrTimeoutElapsed(selfHeight, selfTimestamp), "counterparty upgrade timeout elapsed")) @@ -352,7 +354,7 @@ func (k *Keeper) ChanUpgradeAck( // WriteUpgradeAckChannel writes a channel which has successfully passed the UpgradeAck handshake step as well as // setting the upgrade for that channel. // An event is emitted for the handshake step. -func (k *Keeper) WriteUpgradeAckChannel(ctx sdk.Context, portID, channelID string, counterpartyUpgrade types.Upgrade) (types.Channel, types.Upgrade) { +func (k *Keeper) WriteUpgradeAckChannel(ctx context.Context, portID, channelID string, counterpartyUpgrade types.Upgrade) (types.Channel, types.Upgrade) { defer telemetry.IncrCounter(1, "ibc", "channel", "upgrade-ack") channel, found := k.GetChannel(ctx, portID, channelID) @@ -383,7 +385,7 @@ func (k *Keeper) WriteUpgradeAckChannel(ctx sdk.Context, portID, channelID strin // ChanUpgradeConfirm is called on the chain which is on FLUSHING after chanUpgradeAck is called on the counterparty. // This will inform the TRY chain of the timeout set on ACK by the counterparty. If the timeout has already exceeded, we will write an error receipt and restore. func (k *Keeper) ChanUpgradeConfirm( - ctx sdk.Context, + ctx context.Context, portID, channelID string, counterpartyChannelState types.State, @@ -462,7 +464,8 @@ func (k *Keeper) ChanUpgradeConfirm( } timeout := counterpartyUpgrade.Timeout - selfHeight, selfTimestamp := clienttypes.GetSelfHeight(ctx), uint64(ctx.BlockTime().UnixNano()) + sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: remove when Upgrading to 52 + selfHeight, selfTimestamp := clienttypes.GetSelfHeight(sdkCtx), uint64(sdkCtx.BlockTime().UnixNano()) if timeout.Elapsed(selfHeight, selfTimestamp) { return types.NewUpgradeError(channel.UpgradeSequence, errorsmod.Wrap(timeout.ErrTimeoutElapsed(selfHeight, selfTimestamp), "counterparty upgrade timeout elapsed")) @@ -475,7 +478,7 @@ func (k *Keeper) ChanUpgradeConfirm( // If the channel has no in-flight packets, its state is updated to indicate that flushing has completed. Otherwise, the counterparty upgrade is set // and the channel state is left unchanged. // An event is emitted for the handshake step. -func (k *Keeper) WriteUpgradeConfirmChannel(ctx sdk.Context, portID, channelID string, counterpartyUpgrade types.Upgrade) types.Channel { +func (k *Keeper) WriteUpgradeConfirmChannel(ctx context.Context, portID, channelID string, counterpartyUpgrade types.Upgrade) types.Channel { defer telemetry.IncrCounter(1, "ibc", "channel", "upgrade-confirm") channel, found := k.GetChannel(ctx, portID, channelID) @@ -498,7 +501,7 @@ func (k *Keeper) WriteUpgradeConfirmChannel(ctx sdk.Context, portID, channelID s // This method should only be called after both channels have flushed any in-flight packets. // This method should only be called directly by the core IBC message server. func (k *Keeper) ChanUpgradeOpen( - ctx sdk.Context, + ctx context.Context, portID, channelID string, counterpartyChannelState types.State, @@ -589,7 +592,7 @@ func (k *Keeper) ChanUpgradeOpen( // WriteUpgradeOpenChannel writes the agreed upon upgrade fields to the channel, and sets the channel state back to OPEN. This can be called in one of two cases: // - In the UpgradeConfirm step of the handshake if both sides have already flushed all in-flight packets. // - In the UpgradeOpen step of the handshake. -func (k *Keeper) WriteUpgradeOpenChannel(ctx sdk.Context, portID, channelID string) types.Channel { +func (k *Keeper) WriteUpgradeOpenChannel(ctx context.Context, portID, channelID string) types.Channel { channel, found := k.GetChannel(ctx, portID, channelID) if !found { panic(fmt.Errorf("could not find existing channel when updating channel state, channelID: %s, portID: %s", channelID, portID)) @@ -651,7 +654,7 @@ func (k *Keeper) WriteUpgradeOpenChannel(ctx sdk.Context, portID, channelID stri // ChanUpgradeCancel is called by the msg server to prove that an error receipt was written on the counterparty // which constitutes a valid situation where the upgrade should be cancelled. An error is returned if sufficient evidence // for cancelling the upgrade has not been provided. -func (k *Keeper) ChanUpgradeCancel(ctx sdk.Context, portID, channelID string, errorReceipt types.ErrorReceipt, errorReceiptProof []byte, proofHeight clienttypes.Height) error { +func (k *Keeper) ChanUpgradeCancel(ctx context.Context, portID, channelID string, errorReceipt types.ErrorReceipt, errorReceiptProof []byte, proofHeight clienttypes.Height) error { channel, found := k.GetChannel(ctx, portID, channelID) if !found { return errorsmod.Wrapf(types.ErrChannelNotFound, "port ID (%s) channel ID (%s)", portID, channelID) @@ -709,7 +712,7 @@ func (k *Keeper) ChanUpgradeCancel(ctx sdk.Context, portID, channelID string, er // WriteUpgradeCancelChannel writes a channel which has canceled the upgrade process.Auxiliary upgrade state is // also deleted. -func (k *Keeper) WriteUpgradeCancelChannel(ctx sdk.Context, portID, channelID string, sequence uint64) { +func (k *Keeper) WriteUpgradeCancelChannel(ctx context.Context, portID, channelID string, sequence uint64) { defer telemetry.IncrCounter(1, "ibc", "channel", "upgrade-cancel") channel, found := k.GetChannel(ctx, portID, channelID) @@ -728,7 +731,7 @@ func (k *Keeper) WriteUpgradeCancelChannel(ctx sdk.Context, portID, channelID st // ChanUpgradeTimeout times out an outstanding upgrade. // This should be used by the initialising chain when the counterparty chain has not responded to an upgrade proposal within the specified timeout period. func (k *Keeper) ChanUpgradeTimeout( - ctx sdk.Context, + ctx context.Context, portID, channelID string, counterpartyChannel types.Channel, counterpartyChannelProof []byte, @@ -824,7 +827,7 @@ func (k *Keeper) ChanUpgradeTimeout( // Auxiliary upgrade state is also deleted. // An event is emitted for the handshake step. func (k *Keeper) WriteUpgradeTimeoutChannel( - ctx sdk.Context, + ctx context.Context, portID, channelID string, ) (types.Channel, types.Upgrade) { defer telemetry.IncrCounter(1, "ibc", "channel", "upgrade-timeout") @@ -849,7 +852,7 @@ func (k *Keeper) WriteUpgradeTimeoutChannel( // startFlushing will set the upgrade last packet send and continue blocking the upgrade from continuing until all // in-flight packets have been flushed. -func (k *Keeper) startFlushing(ctx sdk.Context, portID, channelID string, upgrade *types.Upgrade) error { +func (k *Keeper) startFlushing(ctx context.Context, portID, channelID string, upgrade *types.Upgrade) error { channel, found := k.GetChannel(ctx, portID, channelID) if !found { return errorsmod.Wrapf(types.ErrChannelNotFound, "port ID (%s) channel ID (%s)", portID, channelID) @@ -880,13 +883,14 @@ func (k *Keeper) startFlushing(ctx sdk.Context, portID, channelID string, upgrad } // getAbsoluteUpgradeTimeout returns the absolute timeout for the given upgrade. -func (k *Keeper) getAbsoluteUpgradeTimeout(ctx sdk.Context) types.Timeout { +func (k *Keeper) getAbsoluteUpgradeTimeout(ctx context.Context) types.Timeout { upgradeTimeout := k.GetParams(ctx).UpgradeTimeout - return types.NewTimeout(clienttypes.ZeroHeight(), uint64(ctx.BlockTime().UnixNano())+upgradeTimeout.Timestamp) + sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: remove when Upgrading to 52 + return types.NewTimeout(clienttypes.ZeroHeight(), uint64(sdkCtx.BlockTime().UnixNano())+upgradeTimeout.Timestamp) } // checkForUpgradeCompatibility checks performs stateful validation of self upgrade fields relative to counterparty upgrade. -func (k *Keeper) checkForUpgradeCompatibility(ctx sdk.Context, upgradeFields, counterpartyUpgradeFields types.UpgradeFields) error { +func (k *Keeper) checkForUpgradeCompatibility(ctx context.Context, upgradeFields, counterpartyUpgradeFields types.UpgradeFields) error { // assert that both sides propose the same channel ordering if upgradeFields.Ordering != counterpartyUpgradeFields.Ordering { return errorsmod.Wrapf(types.ErrIncompatibleCounterpartyUpgrade, "expected upgrade ordering (%s) to match counterparty upgrade ordering (%s)", upgradeFields.Ordering, counterpartyUpgradeFields.Ordering) @@ -924,7 +928,7 @@ func (k *Keeper) checkForUpgradeCompatibility(ctx sdk.Context, upgradeFields, co // - the proposed connection hops do not exist // - the proposed version is non-empty (checked in UpgradeFields.ValidateBasic()) // - the proposed connection hops are not open -func (k *Keeper) validateSelfUpgradeFields(ctx sdk.Context, proposedUpgrade types.UpgradeFields, channel types.Channel) error { +func (k *Keeper) validateSelfUpgradeFields(ctx context.Context, proposedUpgrade types.UpgradeFields, channel types.Channel) error { currentFields := extractUpgradeFields(channel) if reflect.DeepEqual(proposedUpgrade, currentFields) { @@ -973,7 +977,7 @@ func extractUpgradeFields(channel types.Channel) types.UpgradeFields { // MustAbortUpgrade will restore the channel state to its pre-upgrade state so that upgrade is aborted. // Any unnecessary state is deleted and an error receipt is written. // This function is expected to always succeed, a panic will occur if an error occurs. -func (k *Keeper) MustAbortUpgrade(ctx sdk.Context, portID, channelID string, err error) { +func (k *Keeper) MustAbortUpgrade(ctx context.Context, portID, channelID string, err error) { if err := k.abortUpgrade(ctx, portID, channelID, err); err != nil { panic(err) } @@ -983,7 +987,7 @@ func (k *Keeper) MustAbortUpgrade(ctx sdk.Context, portID, channelID string, err // All upgrade information associated with the upgrade attempt is deleted and an upgrade error // receipt is written for that upgrade attempt. This prevents the upgrade handshake from continuing // on our side and provides proof for the counterparty to safely abort the upgrade. -func (k *Keeper) abortUpgrade(ctx sdk.Context, portID, channelID string, err error) error { +func (k *Keeper) abortUpgrade(ctx context.Context, portID, channelID string, err error) error { if err == nil { return errorsmod.Wrap(types.ErrInvalidUpgradeError, "cannot abort upgrade handshake with nil error") } @@ -1011,7 +1015,7 @@ func (k *Keeper) abortUpgrade(ctx sdk.Context, portID, channelID string, err err // restoreChannel will restore the channel state to its pre-upgrade state so that upgrade is aborted. // When an upgrade attempt is aborted, the upgrade information must be deleted. This prevents us // from continuing an upgrade handshake after we cancel an upgrade attempt. -func (k *Keeper) restoreChannel(ctx sdk.Context, portID, channelID string, upgradeSequence uint64, channel types.Channel) types.Channel { +func (k *Keeper) restoreChannel(ctx context.Context, portID, channelID string, upgradeSequence uint64, channel types.Channel) types.Channel { channel.State = types.OPEN channel.UpgradeSequence = upgradeSequence @@ -1024,7 +1028,7 @@ func (k *Keeper) restoreChannel(ctx sdk.Context, portID, channelID string, upgra } // WriteErrorReceipt will write an error receipt from the provided UpgradeError. -func (k *Keeper) WriteErrorReceipt(ctx sdk.Context, portID, channelID string, upgradeError *types.UpgradeError) { +func (k *Keeper) WriteErrorReceipt(ctx context.Context, portID, channelID string, upgradeError *types.UpgradeError) { channel, found := k.GetChannel(ctx, portID, channelID) if !found { panic(errorsmod.Wrapf(types.ErrChannelNotFound, "port ID (%s) channel ID (%s)", portID, channelID)) diff --git a/modules/light-clients/07-tendermint/migrations/migrations.go b/modules/light-clients/07-tendermint/migrations/migrations.go index b1ab4f0eea6..c5457f6e684 100644 --- a/modules/light-clients/07-tendermint/migrations/migrations.go +++ b/modules/light-clients/07-tendermint/migrations/migrations.go @@ -1,10 +1,11 @@ package migrations import ( + "context" + errorsmod "cosmossdk.io/errors" "github.com/cosmos/cosmos-sdk/codec" - sdk "github.com/cosmos/cosmos-sdk/types" clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types" "github.com/cosmos/ibc-go/v9/modules/core/exported" @@ -13,7 +14,7 @@ import ( // PruneExpiredConsensusStates prunes all expired tendermint consensus states. This function // may optionally be called during in-place store migrations. The ibc store key must be provided. -func PruneExpiredConsensusStates(ctx sdk.Context, cdc codec.BinaryCodec, clientKeeper ClientKeeper) (int, error) { +func PruneExpiredConsensusStates(ctx context.Context, cdc codec.BinaryCodec, clientKeeper ClientKeeper) (int, error) { var clientIDs []string clientKeeper.IterateClientStates(ctx, []byte(exported.Tendermint), func(clientID string, _ exported.ClientState) bool { clientIDs = append(clientIDs, clientID) diff --git a/modules/light-clients/07-tendermint/store.go b/modules/light-clients/07-tendermint/store.go index db6dcc64bb0..e7eeb651f7b 100644 --- a/modules/light-clients/07-tendermint/store.go +++ b/modules/light-clients/07-tendermint/store.go @@ -265,7 +265,7 @@ func GetPreviousConsensusState(clientStore storetypes.KVStore, cdc codec.BinaryC // client store. If a consensus state is expired, it is deleted and its metadata // is deleted. The number of consensus states pruned is returned. func PruneAllExpiredConsensusStates( - ctx sdk.Context, clientStore storetypes.KVStore, + ctx context.Context, clientStore storetypes.KVStore, cdc codec.BinaryCodec, clientState *ClientState, ) int { var heights []exported.Height @@ -275,8 +275,8 @@ func PruneAllExpiredConsensusStates( if !found { // consensus state should always be found return true } - - if clientState.IsExpired(consState.Timestamp, ctx.BlockTime()) { + sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: remove when Upgrading to 52 + if clientState.IsExpired(consState.Timestamp, sdkCtx.BlockTime()) { heights = append(heights, height) }