Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

chore: Keeper cleanup #7262

Draft
wants to merge 4 commits into
base: marko/gomod_change
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,15 @@ func (im IBCMiddleware) OnChanCloseConfirm(
}

// OnRecvPacket implements the IBCMiddleware interface
func (IBCMiddleware) OnRecvPacket(
func (im IBCMiddleware) OnRecvPacket(
ctx context.Context,
_ string,
packet channeltypes.Packet,
_ sdk.AccAddress,
) ibcexported.Acknowledgement {
err := errorsmod.Wrapf(icatypes.ErrInvalidChannelFlow, "cannot receive packet on controller chain")
ack := channeltypes.NewErrorAcknowledgement(err)
keeper.EmitAcknowledgementEvent(ctx, packet, ack, err)
keeper.EmitAcknowledgementEvent(ctx, packet, ack, err, im.keeper)
return ack
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func (k Keeper) registerInterchainAccount(ctx context.Context, connectionID, por
return "", err
}

events := sdkCtx.EventManager().Events()
k.Logger(ctx).Debug("emitting interchain account registration events", logging.SdkEventsToLogArguments(events))
k.EventService.EventManager(ctx).Events()
k.Logger.Debug("emitting interchain account registration events", logging.SdkEventsToLogArguments(events))

// NOTE: The sdk msg handler creates a new EventManager, so events must be correctly propagated back to the current context
sdkCtx.EventManager().EmitEvents(events)
Expand Down
25 changes: 12 additions & 13 deletions modules/apps/27-interchain-accounts/controller/keeper/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package keeper

import (
"context"
"strconv"

"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/event"
sdk "github.com/cosmos/cosmos-sdk/types"

icatypes "github.com/cosmos/ibc-go/v9/modules/apps/27-interchain-accounts/types"
Expand All @@ -13,22 +14,20 @@ 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) {
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()),
sdk.NewAttribute(icatypes.AttributeKeyAckSuccess, strconv.FormatBool(ack.Success())),
func EmitAcknowledgementEvent(ctx context.Context, packet channeltypes.Packet, ack exported.Acknowledgement, err error, env appmodule.Environment) {

attributes := []event.Attribute{
{sdk.AttributeKeyModule, icatypes.ModuleName},
{icatypes.AttributeKeyControllerChannelID, packet.GetDestChannel()},
{icatypes.AttributeKeyControllerChannelID, packet.GetDestChannel()},
}

if err != nil {
attributes = append(attributes, sdk.NewAttribute(icatypes.AttributeKeyAckError, err.Error()))
attributes = append(attributes, event.Attribute{icatypes.AttributeKeyAckError, err.Error()})
}

sdkCtx.EventManager().EmitEvent(
sdk.NewEvent(
icatypes.EventTypePacket,
attributes...,
),
env.EventService.EventManager(ctx).EmitKV(
icatypes.EventTypePacket,
attributes...,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,18 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

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

"github.com/cosmos/ibc-go/v9/modules/apps/27-interchain-accounts/controller/types"
icatypes "github.com/cosmos/ibc-go/v9/modules/apps/27-interchain-accounts/types"
)

var _ types.QueryServer = (*Keeper)(nil)

// InterchainAccount implements the Query/InterchainAccount gRPC method
func (k Keeper) InterchainAccount(goCtx context.Context, req *types.QueryInterchainAccountRequest) (*types.QueryInterchainAccountResponse, error) {
func (k Keeper) InterchainAccount(ctx context.Context, req *types.QueryInterchainAccountRequest) (*types.QueryInterchainAccountResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

ctx := sdk.UnwrapSDKContext(goCtx)

portID, err := icatypes.NewControllerPortID(req.Owner)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "failed to generate portID from owner address: %s", err)
Expand All @@ -38,8 +34,7 @@ func (k Keeper) InterchainAccount(goCtx context.Context, req *types.QueryInterch
}

// Params implements the Query/Params gRPC method
func (k Keeper) Params(c context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
func (k Keeper) Params(ctx context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
params := k.GetParams(ctx)

return &types.QueryParamsResponse{
Expand Down
51 changes: 21 additions & 30 deletions modules/apps/27-interchain-accounts/controller/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ import (
"bytes"
"context"
"errors"
"fmt"
"strings"

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"

capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
"github.com/cosmos/ibc-go/v9/modules/apps/27-interchain-accounts/controller/types"
Expand All @@ -30,7 +27,7 @@ import (

// Keeper defines the IBC interchain accounts controller keeper
type Keeper struct {
storeService corestore.KVStoreService
appmodule.Environment
cdc codec.Codec
legacySubspace icatypes.ParamSubspace
ics4Wrapper porttypes.ICS4Wrapper
Expand All @@ -48,7 +45,7 @@ type Keeper struct {

// NewKeeper creates a new interchain accounts controller 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, portKeeper icatypes.PortKeeper,
scopedKeeper exported.ScopedKeeper, msgRouter icatypes.MessageRouter, authority string,
) Keeper {
Expand All @@ -57,7 +54,7 @@ func NewKeeper(
}

return Keeper{
storeService: storeService,
Environment: env,
cdc: cdc,
legacySubspace: legacySubspace,
ics4Wrapper: ics4Wrapper,
Expand All @@ -81,12 +78,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: https://github.com/cosmos/ibc-go/issues/5917
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 @@ -98,9 +89,9 @@ func (k Keeper) GetConnectionID(ctx context.Context, portID, channelID string) (

// GetAllPorts returns all ports to which the interchain accounts controller module is bound. Used in ExportGenesis
func (k Keeper) GetAllPorts(ctx context.Context) []string {
store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := runtime.KVStoreAdapter(k.KVStoreService.OpenKVStore(ctx))
iterator := storetypes.KVStorePrefixIterator(store, []byte(icatypes.PortKeyPrefix))
defer coretypes.LogDeferred(k.Logger(ctx), func() error { return iterator.Close() })
defer coretypes.LogDeferred(k.Logger, func() error { return iterator.Close() })

var ports []string
for ; iterator.Valid(); iterator.Next() {
Expand All @@ -114,7 +105,7 @@ func (k Keeper) GetAllPorts(ctx context.Context) []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 Down Expand Up @@ -143,7 +134,7 @@ func (k Keeper) GetAppVersion(ctx context.Context, portID, channelID string) (st

// 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 @@ -186,9 +177,9 @@ func (k Keeper) IsActiveChannelClosed(ctx context.Context, connectionID, portID

// GetAllActiveChannels returns a list of all active interchain accounts controller 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 @@ -213,7 +204,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 @@ -227,7 +218,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 @@ -243,7 +234,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 @@ -264,15 +255,15 @@ 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)
}
}

// IsMiddlewareEnabled returns true if the underlying application callbacks are enabled for given port and connection identifier pair, otherwise false
func (k Keeper) IsMiddlewareEnabled(ctx context.Context, portID, connectionID string) bool {
store := k.storeService.OpenKVStore(ctx)
store := k.KVStoreService.OpenKVStore(ctx)
bz, err := store.Get(icatypes.KeyIsMiddlewareEnabled(portID, connectionID))
if err != nil {
panic(err)
Expand All @@ -282,7 +273,7 @@ func (k Keeper) IsMiddlewareEnabled(ctx context.Context, portID, connectionID st

// IsMiddlewareDisabled returns true if the underlying application callbacks are disabled for the given port and connection identifier pair, otherwise false
func (k Keeper) IsMiddlewareDisabled(ctx context.Context, portID, connectionID string) bool {
store := k.storeService.OpenKVStore(ctx)
store := k.KVStoreService.OpenKVStore(ctx)
bz, err := store.Get(icatypes.KeyIsMiddlewareEnabled(portID, connectionID))
if err != nil {
panic(err)
Expand All @@ -292,23 +283,23 @@ func (k Keeper) IsMiddlewareDisabled(ctx context.Context, portID, connectionID s

// SetMiddlewareEnabled stores a flag to indicate that the underlying application callbacks should be enabled for the given port and connection identifier pair
func (k Keeper) SetMiddlewareEnabled(ctx context.Context, portID, connectionID string) {
store := k.storeService.OpenKVStore(ctx)
store := k.KVStoreService.OpenKVStore(ctx)
if err := store.Set(icatypes.KeyIsMiddlewareEnabled(portID, connectionID), icatypes.MiddlewareEnabled); err != nil {
panic(err)
}
}

// SetMiddlewareDisabled stores a flag to indicate that the underlying application callbacks should be disabled for the given port and connection identifier pair
func (k Keeper) SetMiddlewareDisabled(ctx context.Context, portID, connectionID string) {
store := k.storeService.OpenKVStore(ctx)
store := k.KVStoreService.OpenKVStore(ctx)
if err := store.Set(icatypes.KeyIsMiddlewareEnabled(portID, connectionID), icatypes.MiddlewareDisabled); err != nil {
panic(err)
}
}

// DeleteMiddlewareEnabled deletes the middleware enabled flag stored in state
func (k Keeper) DeleteMiddlewareEnabled(ctx context.Context, portID, connectionID string) {
store := k.storeService.OpenKVStore(ctx)
store := k.KVStoreService.OpenKVStore(ctx)
if err := store.Delete(icatypes.KeyIsMiddlewareEnabled(portID, connectionID)); err != nil {
panic(err)
}
Expand All @@ -331,7 +322,7 @@ func (k Keeper) getAppMetadata(ctx context.Context, portID, channelID string) (i

// GetParams returns the current ica/controller 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 @@ -347,7 +338,7 @@ func (k Keeper) GetParams(ctx context.Context) types.Params {

// SetParams sets the ica/controller 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
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (m Migrator) MigrateParams(ctx context.Context) error {
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")
m.keeper.Logger.Info("successfully migrated ica/controller submodule to self-manage params")
}
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (

errorsmod "cosmossdk.io/errors"

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

"github.com/cosmos/ibc-go/v9/modules/apps/27-interchain-accounts/controller/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"
Expand All @@ -26,8 +24,7 @@ func NewMsgServerImpl(keeper *Keeper) types.MsgServer {
}

// RegisterInterchainAccount defines a rpc handler for MsgRegisterInterchainAccount
func (s msgServer) RegisterInterchainAccount(goCtx context.Context, msg *types.MsgRegisterInterchainAccount) (*types.MsgRegisterInterchainAccountResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
func (s msgServer) RegisterInterchainAccount(ctx context.Context, msg *types.MsgRegisterInterchainAccount) (*types.MsgRegisterInterchainAccountResponse, error) {

portID, err := icatypes.NewControllerPortID(msg.Owner)
if err != nil {
Expand All @@ -48,11 +45,11 @@ func (s msgServer) RegisterInterchainAccount(goCtx context.Context, msg *types.M

channelID, err := s.registerInterchainAccount(ctx, msg.ConnectionId, portID, msg.Version, order)
if err != nil {
s.Logger(ctx).Error("error registering interchain account", "error", err.Error())
s.Logger.Error("error registering interchain account", "error", err.Error())
return nil, err
}

s.Logger(ctx).Info("successfully registered interchain account", "channel-id", channelID)
s.Logger.Info("successfully registered interchain account", "channel-id", channelID)

return &types.MsgRegisterInterchainAccountResponse{
ChannelId: channelID,
Expand All @@ -61,8 +58,7 @@ func (s msgServer) RegisterInterchainAccount(goCtx context.Context, msg *types.M
}

// SendTx defines a rpc handler for MsgSendTx
func (s msgServer) SendTx(goCtx context.Context, msg *types.MsgSendTx) (*types.MsgSendTxResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
func (s msgServer) SendTx(ctx context.Context, msg *types.MsgSendTx) (*types.MsgSendTxResponse, error) {

portID, err := icatypes.NewControllerPortID(msg.Owner)
if err != nil {
Expand All @@ -71,7 +67,7 @@ func (s msgServer) SendTx(goCtx context.Context, msg *types.MsgSendTx) (*types.M

// the absolute timeout value is calculated using the controller chain block time + the relative timeout value
// this assumes time synchrony to a certain degree between the controller and counterparty host chain
absoluteTimeout := uint64(ctx.BlockTime().UnixNano()) + msg.RelativeTimeout
absoluteTimeout := uint64(s.HeaderService.HeaderInfo(ctx).Time.UnixNano()) + msg.RelativeTimeout
seq, err := s.sendTx(ctx, msg.ConnectionId, portID, msg.PacketData, absoluteTimeout)
if err != nil {
return nil, err
Expand All @@ -81,12 +77,11 @@ func (s msgServer) SendTx(goCtx context.Context, msg *types.MsgSendTx) (*types.M
}

// UpdateParams defines an rpc handler method for MsgUpdateParams. Updates the ica/controller submodule's parameters.
func (k Keeper) UpdateParams(goCtx context.Context, msg *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
func (k Keeper) UpdateParams(ctx context.Context, msg *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
if k.GetAuthority() != msg.Signer {
return nil, errorsmod.Wrapf(ibcerrors.ErrUnauthorized, "expected %s, got %s", k.GetAuthority(), msg.Signer)
}

ctx := sdk.UnwrapSDKContext(goCtx)
k.SetParams(ctx, msg.Params)

return &types.MsgUpdateParamsResponse{}, nil
Expand Down
Loading
Loading