Skip to content

Commit

Permalink
refactor: update icahost to use runtime.Environment (#7600)
Browse files Browse the repository at this point in the history
  • Loading branch information
damiannolan authored Dec 17, 2024
1 parent d5e329c commit 7aae649
Show file tree
Hide file tree
Showing 15 changed files with 142 additions and 146 deletions.
2 changes: 1 addition & 1 deletion e2e/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ require (
github.com/avast/retry-go/v4 v4.5.1 // indirect
github.com/aws/aws-sdk-go v1.55.5 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/bgentry/speakeasy v0.2.0 // indirect
github.com/bits-and-blooms/bitset v1.12.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.4 // indirect
Expand Down Expand Up @@ -255,6 +254,7 @@ require (
cosmossdk.io/x/nft v0.0.0-00010101000000-000000000000 // indirect
github.com/FactomProject/basen v0.0.0-20150613233007-fe3947df716e // indirect
github.com/FactomProject/btcutilecc v0.0.0-20130527213604-d3a63a5752ec // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/bytedance/sonic v1.12.4 // indirect
github.com/bytedance/sonic/loader v0.2.1 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
Expand Down
15 changes: 10 additions & 5 deletions modules/apps/27-interchain-accounts/host/ibc_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,27 @@ func (im IBCModule) OnRecvPacket(
_ sdk.AccAddress,
) ibcexported.Acknowledgement {
if !im.keeper.GetParams(ctx).HostEnabled {
im.keeper.Logger(ctx).Info("host submodule is disabled")
keeper.EmitHostDisabledEvent(ctx, packet)
im.keeper.Logger.Info("host submodule is disabled")
if err := im.keeper.EmitHostDisabledEvent(ctx, packet); err != nil {
return channeltypes.NewErrorAcknowledgement(err)
}

return channeltypes.NewErrorAcknowledgement(types.ErrHostSubModuleDisabled)
}

txResponse, err := im.keeper.OnRecvPacket(ctx, packet)
ack := channeltypes.NewResultAcknowledgement(txResponse)
if err != nil {
ack = channeltypes.NewErrorAcknowledgement(err)
im.keeper.Logger(ctx).Error(fmt.Sprintf("%s sequence %d", err.Error(), packet.Sequence))
im.keeper.Logger.Error(fmt.Sprintf("%s sequence %d", err.Error(), packet.Sequence))
} else {
im.keeper.Logger(ctx).Info("successfully handled packet", "sequence", packet.Sequence)
im.keeper.Logger.Info("successfully handled packet", "sequence", packet.Sequence)
}

// Emit an event indicating a successful or failed acknowledgement.
keeper.EmitAcknowledgementEvent(ctx, packet, ack, err)
if err := im.keeper.EmitAcknowledgementEvent(ctx, packet, ack, err); err != nil {
return channeltypes.NewErrorAcknowledgement(err)
}

// NOTE: acknowledgement will be written synchronously during IBC handler execution.
return ack
Expand Down
41 changes: 19 additions & 22 deletions modules/apps/27-interchain-accounts/host/keeper/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"strconv"

"cosmossdk.io/core/event"

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

"github.com/cosmos/ibc-go/v9/modules/apps/27-interchain-accounts/host/types"
Expand All @@ -14,35 +16,30 @@ import (

// EmitAcknowledgementEvent emits an event signalling a successful or failed acknowledgement and including the error
// details if any.
func EmitAcknowledgementEvent(ctx context.Context, packet channeltypes.Packet, ack exported.Acknowledgement, err error) {
attributes := []sdk.Attribute{
sdk.NewAttribute(sdk.AttributeKeyModule, icatypes.ModuleName),
sdk.NewAttribute(icatypes.AttributeKeyHostChannelID, packet.GetDestChannel()),
sdk.NewAttribute(icatypes.AttributeKeyAckSuccess, strconv.FormatBool(ack.Success())),
func (k *Keeper) EmitAcknowledgementEvent(ctx context.Context, packet channeltypes.Packet, ack exported.Acknowledgement, err error) error {
attributes := []event.Attribute{
event.NewAttribute(sdk.AttributeKeyModule, icatypes.ModuleName),
event.NewAttribute(icatypes.AttributeKeyHostChannelID, packet.GetDestChannel()),
event.NewAttribute(icatypes.AttributeKeyAckSuccess, strconv.FormatBool(ack.Success())),
}

if err != nil {
attributes = append(attributes, sdk.NewAttribute(icatypes.AttributeKeyAckError, err.Error()))
attributes = append(attributes, event.NewAttribute(icatypes.AttributeKeyAckError, err.Error()))
}
sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/5917
sdkCtx.EventManager().EmitEvent(
sdk.NewEvent(
icatypes.EventTypePacket,
attributes...,
),

return k.EventService.EventManager(ctx).EmitKV(
icatypes.EventTypePacket,
attributes...,
)
}

// EmitHostDisabledEvent emits an event signalling that the host submodule is disabled.
func EmitHostDisabledEvent(ctx context.Context, packet channeltypes.Packet) {
sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/5917
sdkCtx.EventManager().EmitEvent(
sdk.NewEvent(
icatypes.EventTypePacket,
sdk.NewAttribute(sdk.AttributeKeyModule, icatypes.ModuleName),
sdk.NewAttribute(icatypes.AttributeKeyHostChannelID, packet.GetDestChannel()),
sdk.NewAttribute(icatypes.AttributeKeyAckError, types.ErrHostSubModuleDisabled.Error()),
sdk.NewAttribute(icatypes.AttributeKeyAckSuccess, "false"),
),
func (k *Keeper) EmitHostDisabledEvent(ctx context.Context, packet channeltypes.Packet) error {
return k.EventService.EventManager(ctx).EmitKV(
icatypes.EventTypePacket,
event.NewAttribute(sdk.AttributeKeyModule, icatypes.ModuleName),
event.NewAttribute(icatypes.AttributeKeyHostChannelID, packet.GetDestChannel()),
event.NewAttribute(icatypes.AttributeKeyAckError, types.ErrHostSubModuleDisabled.Error()),
event.NewAttribute(icatypes.AttributeKeyAckSuccess, "false"),
)
}
6 changes: 3 additions & 3 deletions modules/apps/27-interchain-accounts/host/keeper/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (k Keeper) OnChanOpenTry(
return "", errorsmod.Wrapf(err, "failed to retrieve connection %s", connectionHops[0])
}

k.Logger(ctx).Debug("counterparty version is invalid, proposing default metadata")
k.Logger.Debug("counterparty version is invalid, proposing default metadata")
metadata = icatypes.NewDefaultMetadata(connection.Counterparty.ConnectionId, connectionHops[0])
}

Expand Down Expand Up @@ -72,7 +72,7 @@ func (k Keeper) OnChanOpenTry(
interchainAccAddr, found := k.GetInterchainAccountAddress(ctx, metadata.HostConnectionId, counterparty.PortId)
if found {
// reopening an interchain account
k.Logger(ctx).Info("reopening existing interchain account", "address", interchainAccAddr)
k.Logger.Info("reopening existing interchain account", "address", interchainAccAddr)
accAddress = sdk.MustAccAddressFromBech32(interchainAccAddr)
if _, ok := k.authKeeper.GetAccount(ctx, accAddress).(*icatypes.InterchainAccount); !ok {
return "", errorsmod.Wrapf(icatypes.ErrInvalidAccountReopening, "existing account address %s, does not have interchain account type", accAddress)
Expand All @@ -83,7 +83,7 @@ func (k Keeper) OnChanOpenTry(
if err != nil {
return "", err
}
k.Logger(ctx).Info("successfully created new interchain account", "host-connection-id", metadata.HostConnectionId, "port-id", counterparty.PortId, "address", accAddress)
k.Logger.Info("successfully created new interchain account", "host-connection-id", metadata.HostConnectionId, "port-id", counterparty.PortId, "address", accAddress)
}

metadata.Address = accAddress.String()
Expand Down
45 changes: 16 additions & 29 deletions modules/apps/27-interchain-accounts/host/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,33 @@ import (

msgv1 "cosmossdk.io/api/cosmos/msg/v1"
queryv1 "cosmossdk.io/api/cosmos/query/v1"
corestore "cosmossdk.io/core/store"
"cosmossdk.io/core/appmodule"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"

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

genesistypes "github.com/cosmos/ibc-go/v9/modules/apps/27-interchain-accounts/genesis/types"
"github.com/cosmos/ibc-go/v9/modules/apps/27-interchain-accounts/host/types"
icatypes "github.com/cosmos/ibc-go/v9/modules/apps/27-interchain-accounts/types"
channeltypes "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
porttypes "github.com/cosmos/ibc-go/v9/modules/core/05-port/types"
ibcerrors "github.com/cosmos/ibc-go/v9/modules/core/errors"
"github.com/cosmos/ibc-go/v9/modules/core/exported"
coretypes "github.com/cosmos/ibc-go/v9/modules/core/types"
)

// Keeper defines the IBC interchain accounts host keeper
type Keeper struct {
storeService corestore.KVStoreService
appmodule.Environment

cdc codec.Codec
legacySubspace icatypes.ParamSubspace

ics4Wrapper porttypes.ICS4Wrapper
channelKeeper icatypes.ChannelKeeper
authKeeper icatypes.AuthKeeper

msgRouter icatypes.MessageRouter
queryRouter icatypes.QueryRouter

// mqsAllowList is a list of all module safe query paths
mqsAllowList []string

Expand All @@ -54,9 +49,9 @@ type Keeper struct {

// NewKeeper creates a new interchain accounts host Keeper instance
func NewKeeper(
cdc codec.Codec, storeService corestore.KVStoreService, legacySubspace icatypes.ParamSubspace,
cdc codec.Codec, env appmodule.Environment, legacySubspace icatypes.ParamSubspace,
ics4Wrapper porttypes.ICS4Wrapper, channelKeeper icatypes.ChannelKeeper,
authKeeper icatypes.AuthKeeper, msgRouter icatypes.MessageRouter, queryRouter icatypes.QueryRouter, authority string,
authKeeper icatypes.AuthKeeper, authority string,
) Keeper {
// ensure ibc interchain accounts module account is set
if addr := authKeeper.GetModuleAddress(icatypes.ModuleName); addr == nil {
Expand All @@ -68,14 +63,12 @@ func NewKeeper(
}

return Keeper{
storeService: storeService,
Environment: env,
cdc: cdc,
legacySubspace: legacySubspace,
ics4Wrapper: ics4Wrapper,
channelKeeper: channelKeeper,
authKeeper: authKeeper,
msgRouter: msgRouter,
queryRouter: queryRouter,
mqsAllowList: newModuleQuerySafeAllowList(),
authority: authority,
}
Expand All @@ -93,12 +86,6 @@ func (k Keeper) GetICS4Wrapper() porttypes.ICS4Wrapper {
return k.ics4Wrapper
}

// Logger returns the application logger, scoped to the associated module
func (Keeper) Logger(ctx context.Context) log.Logger {
sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: remove after context.Context is removed from core IBC
return sdkCtx.Logger().With("module", fmt.Sprintf("x/%s-%s", exported.ModuleName, icatypes.ModuleName))
}

// getConnectionID returns the connection id for the given port and channelIDs.
func (k Keeper) getConnectionID(ctx context.Context, portID, channelID string) (string, error) {
channel, found := k.channelKeeper.GetChannel(ctx, portID, channelID)
Expand All @@ -110,7 +97,7 @@ func (k Keeper) getConnectionID(ctx context.Context, portID, channelID string) (

// setPort sets the provided portID in state.
func (k Keeper) setPort(ctx context.Context, portID string) {
store := k.storeService.OpenKVStore(ctx)
store := k.KVStoreService.OpenKVStore(ctx)
if err := store.Set(icatypes.KeyPort(portID), []byte{0x01}); err != nil {
panic(err)
}
Expand All @@ -133,7 +120,7 @@ func (k Keeper) getAppMetadata(ctx context.Context, portID, channelID string) (i

// GetActiveChannelID retrieves the active channelID from the store keyed by the provided connectionID and portID
func (k Keeper) GetActiveChannelID(ctx context.Context, connectionID, portID string) (string, bool) {
store := k.storeService.OpenKVStore(ctx)
store := k.KVStoreService.OpenKVStore(ctx)
key := icatypes.KeyActiveChannel(portID, connectionID)

bz, err := store.Get(key)
Expand Down Expand Up @@ -165,9 +152,9 @@ func (k Keeper) GetOpenActiveChannel(ctx context.Context, connectionID, portID s

// GetAllActiveChannels returns a list of all active interchain accounts host channels and their associated connection and port identifiers
func (k Keeper) GetAllActiveChannels(ctx context.Context) []genesistypes.ActiveChannel {
store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := runtime.KVStoreAdapter(k.KVStoreService.OpenKVStore(ctx))
iterator := storetypes.KVStorePrefixIterator(store, []byte(icatypes.ActiveChannelKeyPrefix))
defer coretypes.LogDeferred(k.Logger(ctx), func() error { return iterator.Close() })
defer coretypes.LogDeferred(k.Logger, func() error { return iterator.Close() })

var activeChannels []genesistypes.ActiveChannel
for ; iterator.Valid(); iterator.Next() {
Expand All @@ -187,7 +174,7 @@ func (k Keeper) GetAllActiveChannels(ctx context.Context) []genesistypes.ActiveC

// SetActiveChannelID stores the active channelID, keyed by the provided connectionID and portID
func (k Keeper) SetActiveChannelID(ctx context.Context, connectionID, portID, channelID string) {
store := k.storeService.OpenKVStore(ctx)
store := k.KVStoreService.OpenKVStore(ctx)
if err := store.Set(icatypes.KeyActiveChannel(portID, connectionID), []byte(channelID)); err != nil {
panic(err)
}
Expand All @@ -201,7 +188,7 @@ func (k Keeper) IsActiveChannel(ctx context.Context, connectionID, portID string

// GetInterchainAccountAddress retrieves the InterchainAccount address from the store associated with the provided connectionID and portID
func (k Keeper) GetInterchainAccountAddress(ctx context.Context, connectionID, portID string) (string, bool) {
store := k.storeService.OpenKVStore(ctx)
store := k.KVStoreService.OpenKVStore(ctx)
key := icatypes.KeyOwnerAccount(portID, connectionID)

bz, err := store.Get(key)
Expand All @@ -217,7 +204,7 @@ func (k Keeper) GetInterchainAccountAddress(ctx context.Context, connectionID, p

// GetAllInterchainAccounts returns a list of all registered interchain account addresses and their associated connection and controller port identifiers
func (k Keeper) GetAllInterchainAccounts(ctx context.Context) []genesistypes.RegisteredInterchainAccount {
store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := runtime.KVStoreAdapter(k.KVStoreService.OpenKVStore(ctx))
iterator := storetypes.KVStorePrefixIterator(store, []byte(icatypes.OwnerKeyPrefix))

var interchainAccounts []genesistypes.RegisteredInterchainAccount
Expand All @@ -238,7 +225,7 @@ func (k Keeper) GetAllInterchainAccounts(ctx context.Context) []genesistypes.Reg

// SetInterchainAccountAddress stores the InterchainAccount address, keyed by the associated connectionID and portID
func (k Keeper) SetInterchainAccountAddress(ctx context.Context, connectionID, portID, address string) {
store := k.storeService.OpenKVStore(ctx)
store := k.KVStoreService.OpenKVStore(ctx)
if err := store.Set(icatypes.KeyOwnerAccount(portID, connectionID), []byte(address)); err != nil {
panic(err)
}
Expand All @@ -251,7 +238,7 @@ func (k Keeper) GetAuthority() string {

// GetParams returns the total set of the host submodule parameters.
func (k Keeper) GetParams(ctx context.Context) types.Params {
store := k.storeService.OpenKVStore(ctx)
store := k.KVStoreService.OpenKVStore(ctx)
bz, err := store.Get([]byte(types.ParamsKey))
if err != nil {
panic(err)
Expand All @@ -267,7 +254,7 @@ func (k Keeper) GetParams(ctx context.Context) types.Params {

// SetParams sets the total set of the host submodule parameters.
func (k Keeper) SetParams(ctx context.Context, params types.Params) {
store := k.storeService.OpenKVStore(ctx)
store := k.KVStoreService.OpenKVStore(ctx)
bz := k.cdc.MustMarshal(&params)
if err := store.Set([]byte(types.ParamsKey), bz); err != nil {
panic(err)
Expand Down
14 changes: 5 additions & 9 deletions modules/apps/27-interchain-accounts/host/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

testifysuite "github.com/stretchr/testify/suite"

"cosmossdk.io/log"

"github.com/cosmos/cosmos-sdk/runtime"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"

Expand Down Expand Up @@ -142,39 +144,33 @@ func (suite *KeeperTestSuite) TestNewKeeper() {
{"success", func() {
keeper.NewKeeper(
suite.chainA.GetSimApp().AppCodec(),
runtime.NewKVStoreService(suite.chainA.GetSimApp().GetKey(types.StoreKey)),
runtime.NewEnvironment(runtime.NewKVStoreService(suite.chainA.GetSimApp().GetKey(types.StoreKey)), log.NewNopLogger()),
suite.chainA.GetSimApp().GetSubspace(types.SubModuleName),
suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper,
suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper,
suite.chainA.GetSimApp().AuthKeeper,
suite.chainA.GetSimApp().MsgServiceRouter(),
suite.chainA.GetSimApp().GRPCQueryRouter(),
suite.chainA.GetSimApp().ICAHostKeeper.GetAuthority(),
)
}, ""},
{"failure: interchain accounts module account does not exist", func() {
keeper.NewKeeper(
suite.chainA.GetSimApp().AppCodec(),
runtime.NewKVStoreService(suite.chainA.GetSimApp().GetKey(types.StoreKey)),
runtime.NewEnvironment(runtime.NewKVStoreService(suite.chainA.GetSimApp().GetKey(types.StoreKey)), log.NewNopLogger()),
suite.chainA.GetSimApp().GetSubspace(types.SubModuleName),
suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper,
suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper,
authkeeper.AccountKeeper{}, // empty account keeper
suite.chainA.GetSimApp().MsgServiceRouter(),
suite.chainA.GetSimApp().GRPCQueryRouter(),
suite.chainA.GetSimApp().ICAHostKeeper.GetAuthority(),
)
}, "the Interchain Accounts module account has not been set"},
{"failure: empty mock staking keeper", func() {
keeper.NewKeeper(
suite.chainA.GetSimApp().AppCodec(),
runtime.NewKVStoreService(suite.chainA.GetSimApp().GetKey(types.StoreKey)),
runtime.NewEnvironment(runtime.NewKVStoreService(suite.chainA.GetSimApp().GetKey(types.StoreKey)), log.NewNopLogger()),
suite.chainA.GetSimApp().GetSubspace(types.SubModuleName),
suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper,
suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper,
suite.chainA.GetSimApp().AuthKeeper,
suite.chainA.GetSimApp().MsgServiceRouter(),
suite.chainA.GetSimApp().GRPCQueryRouter(),
"", // authority
)
}, "authority must be non-empty"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (m Migrator) MigrateParams(ctx context.Context) error {
return err
}
m.keeper.SetParams(ctx, params)
m.keeper.Logger(ctx).Info("successfully migrated ica/host submodule to self-manage params")
m.keeper.Logger.Info("successfully migrated ica/host submodule to self-manage params")
}
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper_test
import (
"fmt"

"cosmossdk.io/log"
govtypes "cosmossdk.io/x/gov/types"

"github.com/cosmos/cosmos-sdk/runtime"
Expand Down Expand Up @@ -32,13 +33,11 @@ func (suite *KeeperTestSuite) TestMigratorMigrateParams() {
func() {
suite.chainA.GetSimApp().ICAHostKeeper = icahostkeeper.NewKeeper(
suite.chainA.Codec,
runtime.NewKVStoreService(suite.chainA.GetSimApp().GetKey(icahosttypes.StoreKey)),
runtime.NewEnvironment(runtime.NewKVStoreService(suite.chainA.GetSimApp().GetKey(icahosttypes.StoreKey)), log.NewNopLogger()),
nil, // assign a nil legacy param subspace
suite.chainA.GetSimApp().IBCFeeKeeper,
suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper,
suite.chainA.GetSimApp().AuthKeeper,
suite.chainA.GetSimApp().MsgServiceRouter(),
suite.chainA.GetSimApp().GRPCQueryRouter(),
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
},
Expand Down
Loading

0 comments on commit 7aae649

Please sign in to comment.