Skip to content

Commit

Permalink
more context cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle committed Aug 28, 2024
1 parent 1288323 commit 44f709a
Show file tree
Hide file tree
Showing 18 changed files with 92 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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, &params)
sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: remove when Upgrading to 52
m.keeper.legacySubspace.GetParamSetIfExists(sdkCtx, &params)
}
m.keeper.SetParams(ctx, params)
m.keeper.Logger(ctx).Info("successfully migrated ica/controller submodule to self-manage params")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
7 changes: 3 additions & 4 deletions modules/apps/27-interchain-accounts/host/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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),
Expand Down
8 changes: 5 additions & 3 deletions modules/apps/27-interchain-accounts/host/keeper/migrations.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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, &params)
sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: should we remove legacy migrations?
m.keeper.legacySubspace.GetParamSetIfExists(sdkCtx, &params)
}
if err := params.Validate(); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion modules/apps/27-interchain-accounts/host/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
"context"
"fmt"
"strings"
"time"
Expand Down Expand Up @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions modules/apps/29-fee/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -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))
}
Expand All @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion modules/apps/29-fee/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() })
Expand Down
21 changes: 13 additions & 8 deletions modules/core/04-channel/keeper/events.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"context"
"encoding/hex"
"fmt"

Expand Down Expand Up @@ -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()),
Expand All @@ -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()),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down
1 change: 0 additions & 1 deletion modules/core/04-channel/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
19 changes: 11 additions & 8 deletions modules/core/04-channel/keeper/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand All @@ -222,14 +223,15 @@ 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
// on unordered channels. Packet receipts must not be pruned, unless it has been marked stale
// 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.
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions modules/core/04-channel/keeper/timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
Loading

0 comments on commit 44f709a

Please sign in to comment.