Skip to content

Commit

Permalink
refactor: migrate from sdk.Context to Context.Context (2/n) (#7081)
Browse files Browse the repository at this point in the history
* migrate context part 1

* ++

* ++

* ++

* ++

* ++fafo

* p2

* fix todos

* error checks

* fix build

* fix build for wasm

* ignore linting

* fix(test): reorder logic to fix failing OnChanOpenInit ica tests

* chore(tests): use valid id for channel.

* chore(linter): fix linter madness

---------

Co-authored-by: Damian Nolan <[email protected]>
Co-authored-by: DimitrisJim <[email protected]>
  • Loading branch information
3 people authored Aug 27, 2024
1 parent 5db55dd commit 30371fa
Show file tree
Hide file tree
Showing 43 changed files with 610 additions and 454 deletions.
26 changes: 13 additions & 13 deletions modules/apps/27-interchain-accounts/controller/ibc_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func NewIBCMiddlewareWithAuth(app porttypes.IBCModule, k keeper.Keeper) IBCMiddl
// version. They will be allowed to perform custom logic without changing
// the parameters stored within a channel struct.
func (im IBCMiddleware) OnChanOpenInit(
ctx sdk.Context,
ctx context.Context,
order channeltypes.Order,
connectionHops []string,
portID string,
Expand Down Expand Up @@ -94,7 +94,7 @@ func (im IBCMiddleware) OnChanOpenInit(

// OnChanOpenTry implements the IBCMiddleware interface
func (IBCMiddleware) OnChanOpenTry(
ctx sdk.Context,
ctx context.Context,
order channeltypes.Order,
connectionHops []string,
portID,
Expand All @@ -113,7 +113,7 @@ func (IBCMiddleware) OnChanOpenTry(
// version. They will be allowed to perform custom logic without changing
// the parameters stored within a channel struct.
func (im IBCMiddleware) OnChanOpenAck(
ctx sdk.Context,
ctx context.Context,
portID,
channelID string,
counterpartyChannelID string,
Expand Down Expand Up @@ -142,7 +142,7 @@ func (im IBCMiddleware) OnChanOpenAck(

// OnChanOpenConfirm implements the IBCMiddleware interface
func (IBCMiddleware) OnChanOpenConfirm(
ctx sdk.Context,
ctx context.Context,
portID,
channelID string,
) error {
Expand All @@ -151,7 +151,7 @@ func (IBCMiddleware) OnChanOpenConfirm(

// OnChanCloseInit implements the IBCMiddleware interface
func (IBCMiddleware) OnChanCloseInit(
ctx sdk.Context,
ctx context.Context,
portID,
channelID string,
) error {
Expand All @@ -161,7 +161,7 @@ func (IBCMiddleware) OnChanCloseInit(

// OnChanCloseConfirm implements the IBCMiddleware interface
func (im IBCMiddleware) OnChanCloseConfirm(
ctx sdk.Context,
ctx context.Context,
portID,
channelID string,
) error {
Expand All @@ -183,7 +183,7 @@ func (im IBCMiddleware) OnChanCloseConfirm(

// OnRecvPacket implements the IBCMiddleware interface
func (IBCMiddleware) OnRecvPacket(
ctx sdk.Context,
ctx context.Context,
_ string,
packet channeltypes.Packet,
_ sdk.AccAddress,
Expand All @@ -196,7 +196,7 @@ func (IBCMiddleware) OnRecvPacket(

// OnAcknowledgementPacket implements the IBCMiddleware interface
func (im IBCMiddleware) OnAcknowledgementPacket(
ctx sdk.Context,
ctx context.Context,
channelVersion string,
packet channeltypes.Packet,
acknowledgement []byte,
Expand All @@ -221,7 +221,7 @@ func (im IBCMiddleware) OnAcknowledgementPacket(

// OnTimeoutPacket implements the IBCMiddleware interface
func (im IBCMiddleware) OnTimeoutPacket(
ctx sdk.Context,
ctx context.Context,
channelVersion string,
packet channeltypes.Packet,
relayer sdk.AccAddress,
Expand All @@ -247,7 +247,7 @@ func (im IBCMiddleware) OnTimeoutPacket(
}

// OnChanUpgradeInit implements the IBCModule interface
func (im IBCMiddleware) OnChanUpgradeInit(ctx sdk.Context, portID, channelID string, proposedOrder channeltypes.Order, proposedConnectionHops []string, proposedVersion string) (string, error) {
func (im IBCMiddleware) OnChanUpgradeInit(ctx context.Context, portID, channelID string, proposedOrder channeltypes.Order, proposedConnectionHops []string, proposedVersion string) (string, error) {
if !im.keeper.GetParams(ctx).ControllerEnabled {
return "", types.ErrControllerSubModuleDisabled
}
Expand Down Expand Up @@ -275,12 +275,12 @@ func (im IBCMiddleware) OnChanUpgradeInit(ctx sdk.Context, portID, channelID str
}

// OnChanUpgradeTry implements the IBCModule interface
func (IBCMiddleware) OnChanUpgradeTry(_ sdk.Context, portID, channelID string, proposedOrder channeltypes.Order, proposedConnectionHops []string, counterpartyVersion string) (string, error) {
func (IBCMiddleware) OnChanUpgradeTry(_ context.Context, portID, channelID string, proposedOrder channeltypes.Order, proposedConnectionHops []string, counterpartyVersion string) (string, error) {
return "", errorsmod.Wrap(icatypes.ErrInvalidChannelFlow, "channel upgrade handshake must be initiated by controller chain")
}

// OnChanUpgradeAck implements the IBCModule interface
func (im IBCMiddleware) OnChanUpgradeAck(ctx sdk.Context, portID, channelID, counterpartyVersion string) error {
func (im IBCMiddleware) OnChanUpgradeAck(ctx context.Context, portID, channelID, counterpartyVersion string) error {
if !im.keeper.GetParams(ctx).ControllerEnabled {
return types.ErrControllerSubModuleDisabled
}
Expand All @@ -307,7 +307,7 @@ func (im IBCMiddleware) OnChanUpgradeAck(ctx sdk.Context, portID, channelID, cou
}

// OnChanUpgradeOpen implements the IBCModule interface
func (im IBCMiddleware) OnChanUpgradeOpen(ctx sdk.Context, portID, channelID string, proposedOrder channeltypes.Order, proposedConnectionHops []string, proposedVersion string) {
func (im IBCMiddleware) OnChanUpgradeOpen(ctx context.Context, portID, channelID string, proposedOrder channeltypes.Order, proposedConnectionHops []string, proposedVersion string) {
connectionID, err := im.keeper.GetConnectionID(ctx, portID, channelID)
if err != nil {
panic(err)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controller_test

import (
"context"
"fmt"
"strconv"
"testing"
Expand Down Expand Up @@ -129,7 +130,7 @@ func (suite *InterchainAccountsTestSuite) TestOnChanOpenInit() {
},
{
"ICA auth module does not claim channel capability", func() {
suite.chainA.GetSimApp().ICAAuthModule.IBCApp.OnChanOpenInit = func(ctx sdk.Context, order channeltypes.Order, connectionHops []string,
suite.chainA.GetSimApp().ICAAuthModule.IBCApp.OnChanOpenInit = func(ctx context.Context, order channeltypes.Order, connectionHops []string,
portID, channelID string, chanCap *capabilitytypes.Capability,
counterparty channeltypes.Counterparty, version string,
) (string, error) {
Expand All @@ -145,7 +146,7 @@ func (suite *InterchainAccountsTestSuite) TestOnChanOpenInit() {
"ICA auth module modification of channel version is ignored", func() {
// NOTE: explicitly modify the channel version via the auth module callback,
// ensuring the expected JSON encoded metadata is not modified upon return
suite.chainA.GetSimApp().ICAAuthModule.IBCApp.OnChanOpenInit = func(ctx sdk.Context, order channeltypes.Order, connectionHops []string,
suite.chainA.GetSimApp().ICAAuthModule.IBCApp.OnChanOpenInit = func(ctx context.Context, order channeltypes.Order, connectionHops []string,
portID, channelID string, chanCap *capabilitytypes.Capability,
counterparty channeltypes.Counterparty, version string,
) (string, error) {
Expand All @@ -160,7 +161,7 @@ func (suite *InterchainAccountsTestSuite) TestOnChanOpenInit() {
},
{
"ICA auth module callback fails", func() {
suite.chainA.GetSimApp().ICAAuthModule.IBCApp.OnChanOpenInit = func(ctx sdk.Context, order channeltypes.Order, connectionHops []string,
suite.chainA.GetSimApp().ICAAuthModule.IBCApp.OnChanOpenInit = func(ctx context.Context, order channeltypes.Order, connectionHops []string,
portID, channelID string, chanCap *capabilitytypes.Capability,
counterparty channeltypes.Counterparty, version string,
) (string, error) {
Expand All @@ -177,7 +178,7 @@ func (suite *InterchainAccountsTestSuite) TestOnChanOpenInit() {
"middleware disabled", func() {
suite.chainA.GetSimApp().ICAControllerKeeper.DeleteMiddlewareEnabled(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ConnectionID)

suite.chainA.GetSimApp().ICAAuthModule.IBCApp.OnChanOpenInit = func(ctx sdk.Context, order channeltypes.Order, connectionHops []string,
suite.chainA.GetSimApp().ICAAuthModule.IBCApp.OnChanOpenInit = func(ctx context.Context, order channeltypes.Order, connectionHops []string,
portID, channelID string, chanCap *capabilitytypes.Capability,
counterparty channeltypes.Counterparty, version string,
) (string, error) {
Expand Down Expand Up @@ -333,7 +334,7 @@ func (suite *InterchainAccountsTestSuite) TestOnChanOpenAck() {
{
"ICA auth module callback fails", func() {
suite.chainA.GetSimApp().ICAAuthModule.IBCApp.OnChanOpenAck = func(
ctx sdk.Context, portID, channelID string, counterpartyChannelID string, counterpartyVersion string,
ctx context.Context, portID, channelID string, counterpartyChannelID string, counterpartyVersion string,
) error {
return fmt.Errorf("mock ica auth fails")
}
Expand All @@ -349,7 +350,7 @@ func (suite *InterchainAccountsTestSuite) TestOnChanOpenAck() {
suite.chainA.GetSimApp().ICAControllerKeeper.DeleteMiddlewareEnabled(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ConnectionID)

suite.chainA.GetSimApp().ICAAuthModule.IBCApp.OnChanOpenAck = func(
ctx sdk.Context, portID, channelID string, counterpartyChannelID string, counterpartyVersion string,
ctx context.Context, portID, channelID string, counterpartyChannelID string, counterpartyVersion string,
) error {
return fmt.Errorf("error should be unreachable")
}
Expand Down Expand Up @@ -622,7 +623,7 @@ func (suite *InterchainAccountsTestSuite) TestOnAcknowledgementPacket() {
{
"ICA auth module callback fails", func() {
suite.chainA.GetSimApp().ICAAuthModule.IBCApp.OnAcknowledgementPacket = func(
ctx sdk.Context, channelVersion string, packet channeltypes.Packet, acknowledgement []byte, relayer sdk.AccAddress,
ctx context.Context, channelVersion string, packet channeltypes.Packet, acknowledgement []byte, relayer sdk.AccAddress,
) error {
return fmt.Errorf("mock ica auth fails")
}
Expand All @@ -638,7 +639,7 @@ func (suite *InterchainAccountsTestSuite) TestOnAcknowledgementPacket() {
suite.chainA.GetSimApp().ICAControllerKeeper.DeleteMiddlewareEnabled(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ConnectionID)

suite.chainA.GetSimApp().ICAAuthModule.IBCApp.OnAcknowledgementPacket = func(
ctx sdk.Context, channelVersion string, packet channeltypes.Packet, acknowledgement []byte, relayer sdk.AccAddress,
ctx context.Context, channelVersion string, packet channeltypes.Packet, acknowledgement []byte, relayer sdk.AccAddress,
) error {
return fmt.Errorf("error should be unreachable")
}
Expand Down Expand Up @@ -719,7 +720,7 @@ func (suite *InterchainAccountsTestSuite) TestOnTimeoutPacket() {
{
"ICA auth module callback fails", func() {
suite.chainA.GetSimApp().ICAAuthModule.IBCApp.OnTimeoutPacket = func(
ctx sdk.Context, channelVersion string, packet channeltypes.Packet, relayer sdk.AccAddress,
ctx context.Context, channelVersion string, packet channeltypes.Packet, relayer sdk.AccAddress,
) error {
return fmt.Errorf("mock ica auth fails")
}
Expand All @@ -735,7 +736,7 @@ func (suite *InterchainAccountsTestSuite) TestOnTimeoutPacket() {
suite.chainA.GetSimApp().ICAControllerKeeper.DeleteMiddlewareEnabled(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ConnectionID)

suite.chainA.GetSimApp().ICAAuthModule.IBCApp.OnTimeoutPacket = func(
ctx sdk.Context, channelVersion string, packet channeltypes.Packet, relayer sdk.AccAddress,
ctx context.Context, channelVersion string, packet channeltypes.Packet, relayer sdk.AccAddress,
) error {
return fmt.Errorf("error should be unreachable")
}
Expand Down Expand Up @@ -826,15 +827,15 @@ func (suite *InterchainAccountsTestSuite) TestOnChanUpgradeInit() {
},
{
"ICA auth module callback fails", func() {
suite.chainA.GetSimApp().ICAAuthModule.IBCApp.OnChanUpgradeInit = func(ctx sdk.Context, portID, channelID string, order channeltypes.Order, connectionHops []string, version string) (string, error) {
suite.chainA.GetSimApp().ICAAuthModule.IBCApp.OnChanUpgradeInit = func(ctx context.Context, portID, channelID string, order channeltypes.Order, connectionHops []string, version string) (string, error) {
return "", ibcmock.MockApplicationCallbackError
}
}, ibcmock.MockApplicationCallbackError,
},
{
"middleware disabled", func() {
suite.chainA.GetSimApp().ICAControllerKeeper.DeleteMiddlewareEnabled(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ConnectionID)
suite.chainA.GetSimApp().ICAAuthModule.IBCApp.OnChanUpgradeInit = func(ctx sdk.Context, portID, channelID string, order channeltypes.Order, connectionHops []string, version string) (string, error) {
suite.chainA.GetSimApp().ICAAuthModule.IBCApp.OnChanUpgradeInit = func(ctx context.Context, portID, channelID string, order channeltypes.Order, connectionHops []string, version string) (string, error) {
return "", ibcmock.MockApplicationCallbackError
}
}, nil,
Expand Down Expand Up @@ -954,15 +955,15 @@ func (suite *InterchainAccountsTestSuite) TestOnChanUpgradeAck() {
},
{
"ICA auth module callback fails", func() {
suite.chainA.GetSimApp().ICAAuthModule.IBCApp.OnChanUpgradeAck = func(ctx sdk.Context, portID, channelID string, counterpartyVersion string) error {
suite.chainA.GetSimApp().ICAAuthModule.IBCApp.OnChanUpgradeAck = func(ctx context.Context, portID, channelID string, counterpartyVersion string) error {
return ibcmock.MockApplicationCallbackError
}
}, ibcmock.MockApplicationCallbackError,
},
{
"middleware disabled", func() {
suite.chainA.GetSimApp().ICAControllerKeeper.DeleteMiddlewareEnabled(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ConnectionID)
suite.chainA.GetSimApp().ICAAuthModule.IBCApp.OnChanUpgradeAck = func(ctx sdk.Context, portID, channelID string, counterpartyVersion string) error {
suite.chainA.GetSimApp().ICAAuthModule.IBCApp.OnChanUpgradeAck = func(ctx context.Context, portID, channelID string, counterpartyVersion string) error {
return ibcmock.MockApplicationCallbackError
}
}, nil,
Expand Down Expand Up @@ -1039,7 +1040,7 @@ func (suite *InterchainAccountsTestSuite) TestOnChanUpgradeOpen() {
{
"middleware disabled", func() {
suite.chainA.GetSimApp().ICAControllerKeeper.DeleteMiddlewareEnabled(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ConnectionID)
suite.chainA.GetSimApp().ICAAuthModule.IBCApp.OnChanUpgradeAck = func(ctx sdk.Context, portID, channelID string, counterpartyVersion string) error {
suite.chainA.GetSimApp().ICAAuthModule.IBCApp.OnChanUpgradeAck = func(ctx context.Context, portID, channelID string, counterpartyVersion string) error {
return ibcmock.MockApplicationCallbackError
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ func (suite *KeeperTestSuite) TestRegisterInterchainAccount() {
portID, err := icatypes.NewControllerPortID(TestOwnerAddress)
suite.Require().NoError(err)

channelID := channeltypes.FormatChannelIdentifier(suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper.GetNextChannelSequence(suite.chainA.GetContext()))
path.EndpointA.ChannelID = channelID

suite.chainA.GetSimApp().ICAControllerKeeper.SetActiveChannelID(suite.chainA.GetContext(), ibctesting.FirstConnectionID, portID, path.EndpointA.ChannelID)

counterparty := channeltypes.NewCounterparty(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"context"
"strconv"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -12,7 +13,8 @@ import (

// EmitAcknowledgementEvent emits an event signalling a successful or failed acknowledgement and including the error
// details if any.
func EmitAcknowledgementEvent(ctx sdk.Context, packet channeltypes.Packet, ack exported.Acknowledgement, err error) {
func EmitAcknowledgementEvent(ctx context.Context, packet channeltypes.Packet, ack exported.Acknowledgement, err error) {
sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/5917
attributes := []sdk.Attribute{
sdk.NewAttribute(sdk.AttributeKeyModule, icatypes.ModuleName),
sdk.NewAttribute(icatypes.AttributeKeyControllerChannelID, packet.GetDestChannel()),
Expand All @@ -23,7 +25,7 @@ func EmitAcknowledgementEvent(ctx sdk.Context, packet channeltypes.Packet, ack e
attributes = append(attributes, sdk.NewAttribute(icatypes.AttributeKeyAckError, err.Error()))
}

ctx.EventManager().EmitEvent(
sdkCtx.EventManager().EmitEvent(
sdk.NewEvent(
icatypes.EventTypePacket,
attributes...,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package keeper

import (
"context"
"fmt"
"strings"

errorsmod "cosmossdk.io/errors"

sdk "github.com/cosmos/cosmos-sdk/types"

capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
icatypes "github.com/cosmos/ibc-go/v9/modules/apps/27-interchain-accounts/types"
connectiontypes "github.com/cosmos/ibc-go/v9/modules/core/03-connection/types"
Expand All @@ -21,7 +20,7 @@ import (
// and the interchain accounts module must be able to claim the channel
// capability.
func (k Keeper) OnChanOpenInit(
ctx sdk.Context,
ctx context.Context,
order channeltypes.Order,
connectionHops []string,
portID string,
Expand Down Expand Up @@ -91,7 +90,7 @@ func (k Keeper) OnChanOpenInit(
// OnChanOpenAck sets the active channel for the interchain account/owner pair
// and stores the associated interchain account address in state keyed by it's corresponding port identifier
func (k Keeper) OnChanOpenAck(
ctx sdk.Context,
ctx context.Context,
portID,
channelID string,
counterpartyVersion string,
Expand Down Expand Up @@ -133,7 +132,7 @@ func (k Keeper) OnChanOpenAck(

// OnChanCloseConfirm removes the active channel stored in state
func (Keeper) OnChanCloseConfirm(
ctx sdk.Context,
ctx context.Context,
portID,
channelID string,
) error {
Expand All @@ -154,7 +153,7 @@ func (Keeper) OnChanCloseConfirm(
// - connectionHops (and subsequently host/controller connectionIDs)
// - interchain account address
// - ICS27 protocol version
func (k Keeper) OnChanUpgradeInit(ctx sdk.Context, portID, channelID string, proposedOrder channeltypes.Order, proposedConnectionHops []string, proposedversion string) (string, error) {
func (k Keeper) OnChanUpgradeInit(ctx context.Context, portID, channelID string, proposedOrder channeltypes.Order, proposedConnectionHops []string, proposedversion string) (string, error) {
// verify connection hops has not changed
connectionID, err := k.GetConnectionID(ctx, portID, channelID)
if err != nil {
Expand Down Expand Up @@ -217,7 +216,7 @@ func (k Keeper) OnChanUpgradeInit(ctx sdk.Context, portID, channelID string, pro
// - host connectionID
// - interchain account address
// - ICS27 protocol version
func (k Keeper) OnChanUpgradeAck(ctx sdk.Context, portID, channelID, counterpartyVersion string) error {
func (k Keeper) OnChanUpgradeAck(ctx context.Context, portID, channelID, counterpartyVersion string) error {
if strings.TrimSpace(counterpartyVersion) == "" {
return errorsmod.Wrap(channeltypes.ErrInvalidChannelVersion, "counterparty version cannot be empty")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,14 @@ func (suite *KeeperTestSuite) TestOnChanOpenInit() {
Version: string(versionBytes),
}

chanCap, err = suite.chainA.App.GetScopedIBCKeeper().NewCapability(suite.chainA.GetContext(), host.ChannelCapabilityPath(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID))
suite.Require().NoError(err)
channelID := channeltypes.FormatChannelIdentifier(suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper.GetNextChannelSequence(suite.chainA.GetContext()))
path.EndpointA.ChannelID = channelID

tc.malleate() // malleate mutates test data

chanCap, err = suite.chainA.App.GetScopedIBCKeeper().NewCapability(suite.chainA.GetContext(), host.ChannelCapabilityPath(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID))
suite.Require().NoError(err)

version, err := suite.chainA.GetSimApp().ICAControllerKeeper.OnChanOpenInit(suite.chainA.GetContext(), channel.Ordering, channel.ConnectionHops,
path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, chanCap, channel.Counterparty, channel.Version,
)
Expand Down
Loading

0 comments on commit 30371fa

Please sign in to comment.