diff --git a/modules/apps/callbacks/testing/simapp/app.go b/modules/apps/callbacks/testing/simapp/app.go index aefbaf96bc6..69dba479e1e 100644 --- a/modules/apps/callbacks/testing/simapp/app.go +++ b/modules/apps/callbacks/testing/simapp/app.go @@ -404,7 +404,11 @@ func NewSimApp( app.UpgradeKeeper = upgradekeeper.NewKeeper(runtime.NewEnvironment(runtime.NewKVStoreService(keys[upgradetypes.StoreKey]), logger.With(log.ModuleKey, "x/upgrade"), runtime.EnvWithMsgRouterService(app.MsgServiceRouter()), runtime.EnvWithQueryRouterService(app.GRPCQueryRouter())), skipUpgradeHeights, appCodec, homePath, app.BaseApp, govModuleAddr, app.ConsensusParamsKeeper) app.IBCKeeper = ibckeeper.NewKeeper( - appCodec, runtime.NewKVStoreService(keys[ibcexported.StoreKey]), app.GetSubspace(ibcexported.ModuleName), app.UpgradeKeeper, authtypes.NewModuleAddress(govtypes.ModuleName).String(), + appCodec, + runtime.NewEnvironment(runtime.NewKVStoreService(keys[ibcexported.StoreKey]), logger.With(log.ModuleKey, "x/ibc")), + app.GetSubspace(ibcexported.ModuleName), + app.UpgradeKeeper, + govModuleAddr, ) // NOTE: The mock ContractKeeper is only created for testing. diff --git a/modules/core/02-client/abci.go b/modules/core/02-client/abci.go index 1b582a36e93..8117ee01910 100644 --- a/modules/core/02-client/abci.go +++ b/modules/core/02-client/abci.go @@ -1,6 +1,8 @@ package client import ( + "context" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/ibc-go/v9/modules/core/02-client/keeper" @@ -9,7 +11,11 @@ import ( ) // BeginBlocker is used to perform IBC client upgrades -func BeginBlocker(ctx sdk.Context, k *keeper.Keeper) { +func BeginBlocker(goCtx context.Context, k *keeper.Keeper) { + // TODO: In order to fully migrate away from sdk.Context here we will need to depend on comet service in order + // to consume the full block header as Env only contains header.Info (where we cannot access next vals hash) + ctx := sdk.UnwrapSDKContext(goCtx) + plan, err := k.GetUpgradePlan(ctx) if err == nil { // Once we are at the last block this chain will commit, set the upgraded consensus state @@ -29,7 +35,9 @@ func BeginBlocker(ctx sdk.Context, k *keeper.Keeper) { // SetUpgradedConsensusState always returns nil, hence the blank here. _ = k.SetUpgradedConsensusState(ctx, plan.Height, bz) - keeper.EmitUpgradeChainEvent(ctx, plan.Height) + if err := k.EmitUpgradeChainEvent(ctx, plan.Height); err != nil { + k.Logger.Error("error in events emission", "error", err.Error()) + } } } } diff --git a/modules/core/02-client/keeper/client.go b/modules/core/02-client/keeper/client.go index 422fca31ba3..99a14adb1a4 100644 --- a/modules/core/02-client/keeper/client.go +++ b/modules/core/02-client/keeper/client.go @@ -1,9 +1,9 @@ package keeper import ( - errorsmod "cosmossdk.io/errors" + "context" - sdk "github.com/cosmos/cosmos-sdk/types" + errorsmod "cosmossdk.io/errors" "github.com/cosmos/ibc-go/v9/modules/core/02-client/types" "github.com/cosmos/ibc-go/v9/modules/core/exported" @@ -15,7 +15,7 @@ import ( // client identifier. The light client module is responsible for setting any client-specific data in the store // via the Initialize method. This includes the client state, initial consensus state and any associated // metadata. The generated client identifier will be returned if a client was successfully initialized. -func (k *Keeper) CreateClient(ctx sdk.Context, clientType string, clientState, consensusState []byte) (string, error) { +func (k *Keeper) CreateClient(ctx context.Context, clientType string, clientState, consensusState []byte) (string, error) { if clientType == exported.Localhost { return "", errorsmod.Wrapf(types.ErrInvalidClientType, "cannot create client of type: %s", clientType) } @@ -36,16 +36,18 @@ func (k *Keeper) CreateClient(ctx sdk.Context, clientType string, clientState, c } initialHeight := clientModule.LatestHeight(ctx, clientID) - k.Logger(ctx).Info("client created at height", "client-id", clientID, "height", initialHeight.String()) + k.Logger.Info("client created at height", "client-id", clientID, "height", initialHeight.String()) defer telemetry.ReportCreateClient(clientType) - emitCreateClientEvent(ctx, clientID, clientType, initialHeight) + if err := k.emitCreateClientEvent(ctx, clientID, clientType, initialHeight); err != nil { + return "", err + } return clientID, nil } // UpdateClient updates the consensus state and the state root from a provided header. -func (k *Keeper) UpdateClient(ctx sdk.Context, clientID string, clientMsg exported.ClientMessage) error { +func (k *Keeper) UpdateClient(ctx context.Context, clientID string, clientMsg exported.ClientMessage) error { clientModule, err := k.Route(ctx, clientID) if err != nil { return err @@ -63,33 +65,27 @@ func (k *Keeper) UpdateClient(ctx sdk.Context, clientID string, clientMsg export if foundMisbehaviour { clientModule.UpdateStateOnMisbehaviour(ctx, clientID, clientMsg) - k.Logger(ctx).Info("client frozen due to misbehaviour", "client-id", clientID) + k.Logger.Info("client frozen due to misbehaviour", "client-id", clientID) clientType := types.MustParseClientIdentifier(clientID) defer telemetry.ReportUpdateClient(foundMisbehaviour, clientType, clientID) - emitSubmitMisbehaviourEvent(ctx, clientID, clientType) - return nil + return k.emitSubmitMisbehaviourEvent(ctx, clientID, clientType) } consensusHeights := clientModule.UpdateState(ctx, clientID, clientMsg) - k.Logger(ctx).Info("client state updated", "client-id", clientID, "heights", consensusHeights) + k.Logger.Info("client state updated", "client-id", clientID, "heights", consensusHeights) clientType := types.MustParseClientIdentifier(clientID) defer telemetry.ReportUpdateClient(foundMisbehaviour, clientType, clientID) - emitUpdateClientEvent(ctx, clientID, clientType, consensusHeights, k.cdc, clientMsg) - return nil + return k.emitUpdateClientEvent(ctx, clientID, clientType, consensusHeights, k.cdc, clientMsg) } // UpgradeClient upgrades the client to a new client state if this new client was committed to // by the old client at the specified upgrade height -func (k *Keeper) UpgradeClient( - ctx sdk.Context, - clientID string, - upgradedClient, upgradedConsState, upgradeClientProof, upgradeConsensusStateProof []byte, -) error { +func (k *Keeper) UpgradeClient(ctx context.Context, clientID string, upgradedClient, upgradedConsState, upgradeClientProof, upgradeConsensusStateProof []byte) error { clientModule, err := k.Route(ctx, clientID) if err != nil { return err @@ -104,13 +100,12 @@ func (k *Keeper) UpgradeClient( } latestHeight := clientModule.LatestHeight(ctx, clientID) - k.Logger(ctx).Info("client state upgraded", "client-id", clientID, "height", latestHeight.String()) + k.Logger.Info("client state upgraded", "client-id", clientID, "height", latestHeight.String()) clientType := types.MustParseClientIdentifier(clientID) defer telemetry.ReportUpgradeClient(clientType, clientID) - emitUpgradeClientEvent(ctx, clientID, clientType, latestHeight) - return nil + return k.emitUpgradeClientEvent(ctx, clientID, clientType, latestHeight) } // RecoverClient will invoke the light client module associated with the subject clientID requesting it to @@ -118,7 +113,7 @@ func (k *Keeper) UpgradeClient( // is responsible for validating the parameters of the substitute (ensuring they match the subject's parameters) // as well as copying the necessary consensus states from the substitute to the subject client store. // The substitute must be Active and the subject must not be Active. -func (k *Keeper) RecoverClient(ctx sdk.Context, subjectClientID, substituteClientID string) error { +func (k *Keeper) RecoverClient(ctx context.Context, subjectClientID, substituteClientID string) error { clientModule, err := k.Route(ctx, subjectClientID) if err != nil { return errorsmod.Wrap(types.ErrRouteNotFound, subjectClientID) @@ -142,11 +137,10 @@ func (k *Keeper) RecoverClient(ctx sdk.Context, subjectClientID, substituteClien return err } - k.Logger(ctx).Info("client recovered", "client-id", subjectClientID) + k.Logger.Info("client recovered", "client-id", subjectClientID) clientType := types.MustParseClientIdentifier(subjectClientID) defer telemetry.ReportRecoverClient(clientType, subjectClientID) - emitRecoverClientEvent(ctx, subjectClientID, clientType) - return nil + return k.emitRecoverClientEvent(ctx, subjectClientID, clientType) } diff --git a/modules/core/02-client/keeper/events.go b/modules/core/02-client/keeper/events.go index 6727ede5b7a..0db3e6cc915 100644 --- a/modules/core/02-client/keeper/events.go +++ b/modules/core/02-client/keeper/events.go @@ -1,10 +1,12 @@ package keeper import ( + "context" "fmt" "strconv" "strings" + "cosmossdk.io/core/event" upgradetypes "cosmossdk.io/x/upgrade/types" "github.com/cosmos/cosmos-sdk/codec" @@ -15,23 +17,24 @@ import ( ) // emitCreateClientEvent emits a create client event -func emitCreateClientEvent(ctx sdk.Context, clientID, clientType string, initialHeight exported.Height) { - ctx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeCreateClient, - sdk.NewAttribute(types.AttributeKeyClientID, clientID), - sdk.NewAttribute(types.AttributeKeyClientType, clientType), - sdk.NewAttribute(types.AttributeKeyConsensusHeight, initialHeight.String()), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func (k *Keeper) emitCreateClientEvent(ctx context.Context, clientID, clientType string, initialHeight exported.Height) error { + if err := k.EventService.EventManager(ctx).EmitKV( + types.EventTypeCreateClient, + event.NewAttribute(types.AttributeKeyClientID, clientID), + event.NewAttribute(types.AttributeKeyClientType, clientType), + event.NewAttribute(types.AttributeKeyConsensusHeight, initialHeight.String()), + ); err != nil { + return err + } + + return k.EventService.EventManager(ctx).EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } // emitUpdateClientEvent emits an update client event -func emitUpdateClientEvent(ctx sdk.Context, clientID string, clientType string, consensusHeights []exported.Height, _ codec.BinaryCodec, _ exported.ClientMessage) { +func (k *Keeper) emitUpdateClientEvent(ctx context.Context, clientID string, clientType string, consensusHeights []exported.Height, _ codec.BinaryCodec, _ exported.ClientMessage) error { var consensusHeightAttr string if len(consensusHeights) != 0 { consensusHeightAttr = consensusHeights[0].String() @@ -42,95 +45,101 @@ func emitUpdateClientEvent(ctx sdk.Context, clientID string, clientType string, consensusHeightsAttr[i] = height.String() } - ctx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeUpdateClient, - sdk.NewAttribute(types.AttributeKeyClientID, clientID), - sdk.NewAttribute(types.AttributeKeyClientType, clientType), - // Deprecated: AttributeKeyConsensusHeight is deprecated and will be removed in a future release. - // Please use AttributeKeyConsensusHeights instead. - sdk.NewAttribute(types.AttributeKeyConsensusHeight, consensusHeightAttr), - sdk.NewAttribute(types.AttributeKeyConsensusHeights, strings.Join(consensusHeightsAttr, ",")), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) + if err := k.EventService.EventManager(ctx).EmitKV( + types.EventTypeUpdateClient, + event.NewAttribute(types.AttributeKeyClientID, clientID), + event.NewAttribute(types.AttributeKeyClientType, clientType), + // Deprecated: AttributeKeyConsensusHeight is deprecated and will be removed in a future release. + // Please use AttributeKeyConsensusHeights instead. + event.NewAttribute(types.AttributeKeyConsensusHeight, consensusHeightAttr), + event.NewAttribute(types.AttributeKeyConsensusHeights, strings.Join(consensusHeightsAttr, ",")), + ); err != nil { + return err + } + + return k.EventService.EventManager(ctx).EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } // emitUpgradeClientEvent emits an upgrade client event -func emitUpgradeClientEvent(ctx sdk.Context, clientID, clientType string, latestHeight exported.Height) { - ctx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeUpgradeClient, - sdk.NewAttribute(types.AttributeKeyClientID, clientID), - sdk.NewAttribute(types.AttributeKeyClientType, clientType), - sdk.NewAttribute(types.AttributeKeyConsensusHeight, latestHeight.String()), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func (k *Keeper) emitUpgradeClientEvent(ctx context.Context, clientID, clientType string, latestHeight exported.Height) error { + if err := k.EventService.EventManager(ctx).EmitKV( + types.EventTypeUpgradeClient, + event.NewAttribute(types.AttributeKeyClientID, clientID), + event.NewAttribute(types.AttributeKeyClientType, clientType), + event.NewAttribute(types.AttributeKeyConsensusHeight, latestHeight.String()), + ); err != nil { + return err + } + + return k.EventService.EventManager(ctx).EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } // emitSubmitMisbehaviourEvent emits a client misbehaviour event -func emitSubmitMisbehaviourEvent(ctx sdk.Context, clientID string, clientType string) { - ctx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeSubmitMisbehaviour, - sdk.NewAttribute(types.AttributeKeyClientID, clientID), - sdk.NewAttribute(types.AttributeKeyClientType, clientType), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func (k *Keeper) emitSubmitMisbehaviourEvent(ctx context.Context, clientID string, clientType string) error { + if err := k.EventService.EventManager(ctx).EmitKV( + types.EventTypeSubmitMisbehaviour, + event.NewAttribute(types.AttributeKeyClientID, clientID), + event.NewAttribute(types.AttributeKeyClientType, clientType), + ); err != nil { + return err + } + + return k.EventService.EventManager(ctx).EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } // emitRecoverClientEvent emits a recover client event -func emitRecoverClientEvent(ctx sdk.Context, clientID, clientType string) { - ctx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeRecoverClient, - sdk.NewAttribute(types.AttributeKeySubjectClientID, clientID), - sdk.NewAttribute(types.AttributeKeyClientType, clientType), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func (k *Keeper) emitRecoverClientEvent(ctx context.Context, clientID, clientType string) error { + if err := k.EventService.EventManager(ctx).EmitKV( + types.EventTypeRecoverClient, + event.NewAttribute(types.AttributeKeySubjectClientID, clientID), + event.NewAttribute(types.AttributeKeyClientType, clientType), + ); err != nil { + return err + } + + return k.EventService.EventManager(ctx).EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } // emitScheduleIBCSoftwareUpgradeEvent emits a schedule IBC software upgrade event -func emitScheduleIBCSoftwareUpgradeEvent(ctx sdk.Context, title string, height int64) { - ctx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeScheduleIBCSoftwareUpgrade, - sdk.NewAttribute(types.AttributeKeyUpgradePlanTitle, title), - sdk.NewAttribute(types.AttributeKeyUpgradePlanHeight, fmt.Sprintf("%d", height)), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func (k *Keeper) emitScheduleIBCSoftwareUpgradeEvent(ctx context.Context, title string, height int64) error { + if err := k.EventService.EventManager(ctx).EmitKV( + types.EventTypeScheduleIBCSoftwareUpgrade, + event.NewAttribute(types.AttributeKeyUpgradePlanTitle, title), + event.NewAttribute(types.AttributeKeyUpgradePlanHeight, fmt.Sprintf("%d", height)), + ); err != nil { + return err + } + + return k.EventService.EventManager(ctx).EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } // EmitUpgradeChainEvent emits an upgrade chain event. -func EmitUpgradeChainEvent(ctx sdk.Context, height int64) { - ctx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeUpgradeChain, - sdk.NewAttribute(types.AttributeKeyUpgradePlanHeight, strconv.FormatInt(height, 10)), - sdk.NewAttribute(types.AttributeKeyUpgradeStore, upgradetypes.StoreKey), // which store to query proof of consensus state from - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func (k *Keeper) EmitUpgradeChainEvent(ctx context.Context, height int64) error { + if err := k.EventService.EventManager(ctx).EmitKV( + types.EventTypeUpgradeChain, + event.NewAttribute(types.AttributeKeyUpgradePlanHeight, strconv.FormatInt(height, 10)), + event.NewAttribute(types.AttributeKeyUpgradeStore, upgradetypes.StoreKey), // which store to query proof of consensus state from + ); err != nil { + return err + } + + return k.EventService.EventManager(ctx).EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } diff --git a/modules/core/02-client/keeper/grpc_query.go b/modules/core/02-client/keeper/grpc_query.go index bc075121731..3f7aa9ac3e6 100644 --- a/modules/core/02-client/keeper/grpc_query.go +++ b/modules/core/02-client/keeper/grpc_query.go @@ -75,7 +75,7 @@ func (q *queryServer) ClientStates(ctx context.Context, req *types.QueryClientSt } var clientStates types.IdentifiedClientStates - store := prefix.NewStore(runtime.KVStoreAdapter(q.storeService.OpenKVStore(ctx)), host.KeyClientStorePrefix) + store := prefix.NewStore(runtime.KVStoreAdapter(q.KVStoreService.OpenKVStore(ctx)), host.KeyClientStorePrefix) pageRes, err := query.FilteredPaginate(store, req.Pagination, func(key, value []byte, accumulate bool) (bool, error) { // filter any metadata stored under client state key @@ -166,7 +166,7 @@ func (q *queryServer) ConsensusStates(ctx context.Context, req *types.QueryConse } var consensusStates []types.ConsensusStateWithHeight - store := prefix.NewStore(runtime.KVStoreAdapter(q.storeService.OpenKVStore(ctx)), host.FullClientKey(req.ClientId, []byte(fmt.Sprintf("%s/", host.KeyConsensusStatePrefix)))) + store := prefix.NewStore(runtime.KVStoreAdapter(q.KVStoreService.OpenKVStore(ctx)), host.FullClientKey(req.ClientId, []byte(fmt.Sprintf("%s/", host.KeyConsensusStatePrefix)))) pageRes, err := query.FilteredPaginate(store, req.Pagination, func(key, value []byte, accumulate bool) (bool, error) { // filter any metadata stored under consensus state key @@ -208,7 +208,7 @@ func (q *queryServer) ConsensusStateHeights(ctx context.Context, req *types.Quer } var consensusStateHeights []types.Height - store := prefix.NewStore(runtime.KVStoreAdapter(q.storeService.OpenKVStore(ctx)), host.FullClientKey(req.ClientId, []byte(fmt.Sprintf("%s/", host.KeyConsensusStatePrefix)))) + store := prefix.NewStore(runtime.KVStoreAdapter(q.KVStoreService.OpenKVStore(ctx)), host.FullClientKey(req.ClientId, []byte(fmt.Sprintf("%s/", host.KeyConsensusStatePrefix)))) pageRes, err := query.FilteredPaginate(store, req.Pagination, func(key, _ []byte, accumulate bool) (bool, error) { // filter any metadata stored under consensus state key @@ -294,16 +294,15 @@ func (q *queryServer) UpgradedClientState(ctx context.Context, req *types.QueryU } // UpgradedConsensusState implements the Query/UpgradedConsensusState gRPC method -func (q *queryServer) UpgradedConsensusState(c context.Context, req *types.QueryUpgradedConsensusStateRequest) (*types.QueryUpgradedConsensusStateResponse, error) { +func (q *queryServer) UpgradedConsensusState(ctx context.Context, req *types.QueryUpgradedConsensusStateRequest) (*types.QueryUpgradedConsensusStateResponse, error) { if req == nil { return nil, status.Error(codes.InvalidArgument, "empty request") } - ctx := sdk.UnwrapSDKContext(c) - - bz, err := q.GetUpgradedConsensusState(ctx, ctx.BlockHeight()) + height := q.HeaderService.HeaderInfo(ctx).Height + bz, err := q.GetUpgradedConsensusState(ctx, height) if err != nil { - return nil, status.Errorf(codes.NotFound, "%s, height %d", err.Error(), ctx.BlockHeight()) + return nil, status.Errorf(codes.NotFound, "%s, height %d", err.Error(), height) } consensusState, err := types.UnmarshalConsensusState(q.cdc, bz) @@ -388,7 +387,7 @@ func (q *queryServer) VerifyMembership(c context.Context, req *types.QueryVerify ) if err := clientModule.VerifyMembership(cachedCtx, req.ClientId, req.ProofHeight, req.TimeDelay, req.BlockDelay, req.Proof, req.MerklePath, req.Value); err != nil { - q.Logger(ctx).Debug("proof verification failed", "key", req.MerklePath, "error", err) + q.Logger.Debug("proof verification failed", "key", req.MerklePath, "error", err) return &types.QueryVerifyMembershipResponse{ Success: false, }, nil diff --git a/modules/core/02-client/keeper/keeper.go b/modules/core/02-client/keeper/keeper.go index a921b8fc98f..9f843325d9a 100644 --- a/modules/core/02-client/keeper/keeper.go +++ b/modules/core/02-client/keeper/keeper.go @@ -6,9 +6,8 @@ import ( "fmt" "strings" - corestore "cosmossdk.io/core/store" + "cosmossdk.io/core/appmodule" errorsmod "cosmossdk.io/errors" - "cosmossdk.io/log" "cosmossdk.io/store/prefix" storetypes "cosmossdk.io/store/types" upgradetypes "cosmossdk.io/x/upgrade/types" @@ -28,7 +27,8 @@ import ( // Keeper represents a type that grants read and write permissions to any client // state information type Keeper struct { - storeService corestore.KVStoreService + appmodule.Environment + cdc codec.BinaryCodec router *types.Router legacySubspace types.ParamSubspace @@ -36,13 +36,13 @@ type Keeper struct { } // NewKeeper creates a new NewKeeper instance -func NewKeeper(cdc codec.BinaryCodec, storeService corestore.KVStoreService, legacySubspace types.ParamSubspace, uk types.UpgradeKeeper) *Keeper { +func NewKeeper(cdc codec.BinaryCodec, env appmodule.Environment, legacySubspace types.ParamSubspace, uk types.UpgradeKeeper) *Keeper { router := types.NewRouter() - localhostModule := localhost.NewLightClientModule(cdc, storeService) + localhostModule := localhost.NewLightClientModule(cdc, env) router.AddRoute(exported.Localhost, localhostModule) return &Keeper{ - storeService: storeService, + Environment: env, cdc: cdc, router: router, legacySubspace: legacySubspace, @@ -55,12 +55,6 @@ func (k *Keeper) Codec() codec.BinaryCodec { return k.cdc } -// Logger returns a module-specific logger. -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", "x/"+exported.ModuleName+"/"+types.SubModuleName) -} - // AddRoute adds a new route to the underlying router. func (k *Keeper) AddRoute(clientType string, module exported.LightClientModule) { k.router.AddRoute(clientType, module) @@ -68,7 +62,7 @@ func (k *Keeper) AddRoute(clientType string, module exported.LightClientModule) // GetStoreProvider returns the light client store provider. func (k *Keeper) GetStoreProvider() types.StoreProvider { - return types.NewStoreProvider(k.storeService) + return types.NewStoreProvider(k.KVStoreService) } // Route returns the light client module for the given client identifier. @@ -142,7 +136,7 @@ func (k *Keeper) SetClientConsensusState(ctx context.Context, clientID string, h // GetNextClientSequence gets the next client sequence from the store. func (k *Keeper) GetNextClientSequence(ctx context.Context) uint64 { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) bz, err := store.Get([]byte(types.KeyNextClientSequence)) if err != nil { panic(err) @@ -156,7 +150,7 @@ func (k *Keeper) GetNextClientSequence(ctx context.Context) uint64 { // SetNextClientSequence sets the next client sequence to the store. func (k *Keeper) SetNextClientSequence(ctx context.Context, sequence uint64) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) bz := sdk.Uint64ToBigEndian(sequence) if err := store.Set([]byte(types.KeyNextClientSequence), bz); err != nil { panic(err) @@ -167,10 +161,10 @@ func (k *Keeper) SetNextClientSequence(ctx context.Context, sequence uint64) { // objects. For each State object, cb will be called. If the cb returns true, // the iterator will close and stop. func (k *Keeper) IterateConsensusStates(ctx context.Context, cb func(clientID string, cs types.ConsensusStateWithHeight) bool) { - store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) + store := runtime.KVStoreAdapter(k.KVStoreService.OpenKVStore(ctx)) iterator := storetypes.KVStorePrefixIterator(store, host.KeyClientStorePrefix) - defer coretypes.LogDeferred(k.Logger(ctx), func() error { return iterator.Close() }) + defer coretypes.LogDeferred(k.Logger, func() error { return iterator.Close() }) for ; iterator.Valid(); iterator.Next() { keySplit := strings.Split(string(iterator.Key()), "/") // consensus key is in the format "clients//consensusStates/" @@ -192,10 +186,10 @@ func (k *Keeper) IterateConsensusStates(ctx context.Context, cb func(clientID st // iterateMetadata provides an iterator over all stored metadata keys in the client store. // For each metadata object, it will perform a callback. func (k *Keeper) iterateMetadata(ctx context.Context, cb func(clientID string, key, value []byte) bool) { - store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) + store := runtime.KVStoreAdapter(k.KVStoreService.OpenKVStore(ctx)) iterator := storetypes.KVStorePrefixIterator(store, host.KeyClientStorePrefix) - defer coretypes.LogDeferred(k.Logger(ctx), func() error { return iterator.Close() }) + defer coretypes.LogDeferred(k.Logger, func() error { return iterator.Close() }) for ; iterator.Valid(); iterator.Next() { split := strings.Split(string(iterator.Key()), "/") if len(split) == 3 && split[2] == string(host.KeyClientState) { @@ -358,10 +352,10 @@ func (k *Keeper) SetUpgradedConsensusState(ctx context.Context, planHeight int64 // objects using the provided store prefix. For each ClientState object, cb will be called. If the cb returns true, // the iterator will close and stop. func (k *Keeper) IterateClientStates(ctx context.Context, storePrefix []byte, cb func(clientID string, cs exported.ClientState) bool) { - store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) + store := runtime.KVStoreAdapter(k.KVStoreService.OpenKVStore(ctx)) iterator := storetypes.KVStorePrefixIterator(store, host.PrefixedClientStoreKey(storePrefix)) - defer coretypes.LogDeferred(k.Logger(ctx), func() error { return iterator.Close() }) + defer coretypes.LogDeferred(k.Logger, func() error { return iterator.Close() }) for ; iterator.Valid(); iterator.Next() { path := string(iterator.Key()) if !strings.Contains(path, host.KeyClientState) { @@ -393,7 +387,7 @@ func (k *Keeper) GetAllClients(ctx context.Context) []exported.ClientState { // namespace without being able to read/write other client's data func (k *Keeper) ClientStore(ctx context.Context, clientID string) storetypes.KVStore { clientPrefix := []byte(fmt.Sprintf("%s/%s/", host.KeyClientStorePrefix, clientID)) - store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) + store := runtime.KVStoreAdapter(k.KVStoreService.OpenKVStore(ctx)) return prefix.NewStore(store, clientPrefix) } @@ -436,7 +430,7 @@ func (k *Keeper) GetClientTimestampAtHeight(ctx context.Context, clientID string // GetParams returns the total set of ibc-client 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) @@ -452,7 +446,7 @@ func (k *Keeper) GetParams(ctx context.Context) types.Params { // SetParams sets the total set of ibc-client 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(¶ms) if err := store.Set([]byte(types.ParamsKey), bz); err != nil { panic(err) @@ -484,8 +478,5 @@ func (k *Keeper) ScheduleIBCSoftwareUpgrade(ctx context.Context, plan upgradetyp } // emitting an event for scheduling an upgrade plan - sdkContext := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/5917 - emitScheduleIBCSoftwareUpgradeEvent(sdkContext, plan.Name, plan.Height) - - return nil + return k.emitScheduleIBCSoftwareUpgradeEvent(ctx, plan.Name, plan.Height) } diff --git a/modules/core/02-client/keeper/migrations.go b/modules/core/02-client/keeper/migrations.go index d39a756ed93..a66b783f522 100644 --- a/modules/core/02-client/keeper/migrations.go +++ b/modules/core/02-client/keeper/migrations.go @@ -26,7 +26,7 @@ func NewMigrator(keeper *Keeper) Migrator { // - removes the localhost client // - asserts that existing tendermint clients are properly registered on the chain codec func (m Migrator) Migrate2to3(ctx sdk.Context) error { - return v7.MigrateStore(ctx, m.keeper.storeService, m.keeper.cdc, m.keeper) + return v7.MigrateStore(ctx, m.keeper.KVStoreService, m.keeper.cdc, m.keeper) } // MigrateParams migrates from consensus version 4 to 5. @@ -40,7 +40,7 @@ func (m Migrator) MigrateParams(ctx sdk.Context) error { } m.keeper.SetParams(ctx, params) - m.keeper.Logger(ctx).Info("successfully migrated client to self-manage params") + m.keeper.Logger.Info("successfully migrated client to self-manage params") return nil } diff --git a/modules/core/03-connection/keeper/events.go b/modules/core/03-connection/keeper/events.go index 04d0bb4851d..a05ca272741 100644 --- a/modules/core/03-connection/keeper/events.go +++ b/modules/core/03-connection/keeper/events.go @@ -3,78 +3,80 @@ package keeper import ( "context" + "cosmossdk.io/core/event" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/ibc-go/v9/modules/core/03-connection/types" ) // emitConnectionOpenInitEvent emits a connection open init event -func emitConnectionOpenInitEvent(ctx context.Context, connectionID string, clientID string, counterparty types.Counterparty) { - sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/7223 - sdkCtx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeConnectionOpenInit, - sdk.NewAttribute(types.AttributeKeyConnectionID, connectionID), - sdk.NewAttribute(types.AttributeKeyClientID, clientID), - sdk.NewAttribute(types.AttributeKeyCounterpartyClientID, counterparty.ClientId), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func (k *Keeper) emitConnectionOpenInitEvent(ctx context.Context, connectionID string, clientID string, counterparty types.Counterparty) error { + if err := k.EventService.EventManager(ctx).EmitKV( + types.EventTypeConnectionOpenInit, + event.NewAttribute(types.AttributeKeyConnectionID, connectionID), + event.NewAttribute(types.AttributeKeyClientID, clientID), + event.NewAttribute(types.AttributeKeyCounterpartyClientID, counterparty.ClientId), + ); err != nil { + return err + } + + return k.EventService.EventManager(ctx).EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } // emitConnectionOpenTryEvent emits a connection open try event -func emitConnectionOpenTryEvent(ctx context.Context, connectionID string, clientID string, counterparty types.Counterparty) { - sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/7223 - sdkCtx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeConnectionOpenTry, - sdk.NewAttribute(types.AttributeKeyConnectionID, connectionID), - sdk.NewAttribute(types.AttributeKeyClientID, clientID), - sdk.NewAttribute(types.AttributeKeyCounterpartyClientID, counterparty.ClientId), - sdk.NewAttribute(types.AttributeKeyCounterpartyConnectionID, counterparty.ConnectionId), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func (k *Keeper) emitConnectionOpenTryEvent(ctx context.Context, connectionID string, clientID string, counterparty types.Counterparty) error { + if err := k.EventService.EventManager(ctx).EmitKV( + types.EventTypeConnectionOpenTry, + event.NewAttribute(types.AttributeKeyConnectionID, connectionID), + event.NewAttribute(types.AttributeKeyClientID, clientID), + event.NewAttribute(types.AttributeKeyCounterpartyClientID, counterparty.ClientId), + event.NewAttribute(types.AttributeKeyCounterpartyConnectionID, counterparty.ConnectionId), + ); err != nil { + return err + } + + return k.EventService.EventManager(ctx).EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } // emitConnectionOpenAckEvent emits a connection open acknowledge event -func emitConnectionOpenAckEvent(ctx context.Context, connectionID string, connectionEnd types.ConnectionEnd) { - sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/7223 - sdkCtx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeConnectionOpenAck, - sdk.NewAttribute(types.AttributeKeyConnectionID, connectionID), - sdk.NewAttribute(types.AttributeKeyClientID, connectionEnd.ClientId), - sdk.NewAttribute(types.AttributeKeyCounterpartyClientID, connectionEnd.Counterparty.ClientId), - sdk.NewAttribute(types.AttributeKeyCounterpartyConnectionID, connectionEnd.Counterparty.ConnectionId), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func (k *Keeper) emitConnectionOpenAckEvent(ctx context.Context, connectionID string, connectionEnd types.ConnectionEnd) error { + if err := k.EventService.EventManager(ctx).EmitKV( + types.EventTypeConnectionOpenAck, + event.NewAttribute(types.AttributeKeyConnectionID, connectionID), + event.NewAttribute(types.AttributeKeyClientID, connectionEnd.ClientId), + event.NewAttribute(types.AttributeKeyCounterpartyClientID, connectionEnd.Counterparty.ClientId), + event.NewAttribute(types.AttributeKeyCounterpartyConnectionID, connectionEnd.Counterparty.ConnectionId), + ); err != nil { + return err + } + + return k.EventService.EventManager(ctx).EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } // emitConnectionOpenConfirmEvent emits a connection open confirm event -func emitConnectionOpenConfirmEvent(ctx context.Context, connectionID string, connectionEnd types.ConnectionEnd) { - sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/7223 - sdkCtx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeConnectionOpenConfirm, - sdk.NewAttribute(types.AttributeKeyConnectionID, connectionID), - sdk.NewAttribute(types.AttributeKeyClientID, connectionEnd.ClientId), - sdk.NewAttribute(types.AttributeKeyCounterpartyClientID, connectionEnd.Counterparty.ClientId), - sdk.NewAttribute(types.AttributeKeyCounterpartyConnectionID, connectionEnd.Counterparty.ConnectionId), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func (k *Keeper) emitConnectionOpenConfirmEvent(ctx context.Context, connectionID string, connectionEnd types.ConnectionEnd) error { + if err := k.EventService.EventManager(ctx).EmitKV( + types.EventTypeConnectionOpenConfirm, + event.NewAttribute(types.AttributeKeyConnectionID, connectionID), + event.NewAttribute(types.AttributeKeyClientID, connectionEnd.ClientId), + event.NewAttribute(types.AttributeKeyCounterpartyClientID, connectionEnd.Counterparty.ClientId), + event.NewAttribute(types.AttributeKeyCounterpartyConnectionID, connectionEnd.Counterparty.ConnectionId), + ); err != nil { + return err + } + + return k.EventService.EventManager(ctx).EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } diff --git a/modules/core/03-connection/keeper/grpc_query.go b/modules/core/03-connection/keeper/grpc_query.go index 859b6125332..c7d331f5f6e 100644 --- a/modules/core/03-connection/keeper/grpc_query.go +++ b/modules/core/03-connection/keeper/grpc_query.go @@ -10,7 +10,6 @@ import ( "cosmossdk.io/store/prefix" "github.com/cosmos/cosmos-sdk/runtime" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/query" clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types" @@ -34,7 +33,7 @@ func NewQueryServer(k *Keeper) types.QueryServer { } // Connection implements the Query/Connection gRPC method -func (q *queryServer) Connection(c context.Context, req *types.QueryConnectionRequest) (*types.QueryConnectionResponse, error) { +func (q *queryServer) Connection(ctx context.Context, req *types.QueryConnectionRequest) (*types.QueryConnectionResponse, error) { if req == nil { return nil, status.Error(codes.InvalidArgument, "empty request") } @@ -43,7 +42,6 @@ func (q *queryServer) Connection(c context.Context, req *types.QueryConnectionRe return nil, status.Error(codes.InvalidArgument, err.Error()) } - ctx := sdk.UnwrapSDKContext(c) connection, found := q.GetConnection(ctx, req.ConnectionId) if !found { return nil, status.Error( @@ -59,16 +57,14 @@ func (q *queryServer) Connection(c context.Context, req *types.QueryConnectionRe } // Connections implements the Query/Connections gRPC method -func (q *queryServer) Connections(c context.Context, req *types.QueryConnectionsRequest) (*types.QueryConnectionsResponse, error) { +func (q *queryServer) Connections(ctx context.Context, req *types.QueryConnectionsRequest) (*types.QueryConnectionsResponse, error) { if req == nil { return nil, status.Error(codes.InvalidArgument, "empty request") } - ctx := sdk.UnwrapSDKContext(c) - var connections []*types.IdentifiedConnection - store := prefix.NewStore(runtime.KVStoreAdapter(q.storeService.OpenKVStore(ctx)), []byte(host.KeyConnectionPrefix)) + store := prefix.NewStore(runtime.KVStoreAdapter(q.KVStoreService.OpenKVStore(ctx)), []byte(host.KeyConnectionPrefix)) pageRes, err := query.Paginate(store, req.Pagination, func(key, value []byte) error { var result types.ConnectionEnd @@ -97,7 +93,7 @@ func (q *queryServer) Connections(c context.Context, req *types.QueryConnections } // ClientConnections implements the Query/ClientConnections gRPC method -func (q *queryServer) ClientConnections(c context.Context, req *types.QueryClientConnectionsRequest) (*types.QueryClientConnectionsResponse, error) { +func (q *queryServer) ClientConnections(ctx context.Context, req *types.QueryClientConnectionsRequest) (*types.QueryClientConnectionsResponse, error) { if req == nil { return nil, status.Error(codes.InvalidArgument, "empty request") } @@ -106,7 +102,6 @@ func (q *queryServer) ClientConnections(c context.Context, req *types.QueryClien return nil, status.Error(codes.InvalidArgument, err.Error()) } - ctx := sdk.UnwrapSDKContext(c) clientConnectionPaths, found := q.GetClientConnectionPaths(ctx, req.ClientId) if !found { return nil, status.Error( @@ -122,7 +117,7 @@ func (q *queryServer) ClientConnections(c context.Context, req *types.QueryClien } // ConnectionClientState implements the Query/ConnectionClientState gRPC method -func (q *queryServer) ConnectionClientState(c context.Context, req *types.QueryConnectionClientStateRequest) (*types.QueryConnectionClientStateResponse, error) { +func (q *queryServer) ConnectionClientState(ctx context.Context, req *types.QueryConnectionClientStateRequest) (*types.QueryConnectionClientStateResponse, error) { if req == nil { return nil, status.Error(codes.InvalidArgument, "empty request") } @@ -131,8 +126,6 @@ func (q *queryServer) ConnectionClientState(c context.Context, req *types.QueryC return nil, status.Error(codes.InvalidArgument, err.Error()) } - ctx := sdk.UnwrapSDKContext(c) - connection, found := q.GetConnection(ctx, req.ConnectionId) if !found { return nil, status.Error( @@ -156,7 +149,7 @@ func (q *queryServer) ConnectionClientState(c context.Context, req *types.QueryC } // ConnectionConsensusState implements the Query/ConnectionConsensusState gRPC method -func (q *queryServer) ConnectionConsensusState(c context.Context, req *types.QueryConnectionConsensusStateRequest) (*types.QueryConnectionConsensusStateResponse, error) { +func (q *queryServer) ConnectionConsensusState(ctx context.Context, req *types.QueryConnectionConsensusStateRequest) (*types.QueryConnectionConsensusStateResponse, error) { if req == nil { return nil, status.Error(codes.InvalidArgument, "empty request") } @@ -165,8 +158,6 @@ func (q *queryServer) ConnectionConsensusState(c context.Context, req *types.Que return nil, status.Error(codes.InvalidArgument, err.Error()) } - ctx := sdk.UnwrapSDKContext(c) - connection, found := q.GetConnection(ctx, req.ConnectionId) if !found { return nil, status.Error( @@ -194,10 +185,8 @@ func (q *queryServer) ConnectionConsensusState(c context.Context, req *types.Que } // ConnectionParams implements the Query/ConnectionParams gRPC method. -func (q *queryServer) ConnectionParams(c context.Context, req *types.QueryConnectionParamsRequest) (*types.QueryConnectionParamsResponse, error) { - ctx := sdk.UnwrapSDKContext(c) +func (q *queryServer) ConnectionParams(ctx context.Context, req *types.QueryConnectionParamsRequest) (*types.QueryConnectionParamsResponse, error) { params := q.GetParams(ctx) - return &types.QueryConnectionParamsResponse{ Params: ¶ms, }, nil diff --git a/modules/core/03-connection/keeper/handshake.go b/modules/core/03-connection/keeper/handshake.go index 0b52d58991a..9700097dd47 100644 --- a/modules/core/03-connection/keeper/handshake.go +++ b/modules/core/03-connection/keeper/handshake.go @@ -47,11 +47,13 @@ func (k *Keeper) ConnOpenInit( connection := types.NewConnectionEnd(types.INIT, clientID, counterparty, versions, delayPeriod) k.SetConnection(ctx, connectionID, connection) - k.Logger(ctx).Info("connection state updated", "connection-id", connectionID, "previous-state", types.UNINITIALIZED, "new-state", types.INIT) + k.Logger.Info("connection state updated", "connection-id", connectionID, "previous-state", types.UNINITIALIZED, "new-state", types.INIT) defer telemetry.IncrCounter(1, "ibc", "connection", "open-init") - emitConnectionOpenInitEvent(ctx, connectionID, clientID, counterparty) + if err := k.emitConnectionOpenInitEvent(ctx, connectionID, clientID, counterparty); err != nil { + return "", err + } return connectionID, nil } @@ -106,11 +108,13 @@ func (k *Keeper) ConnOpenTry( } k.SetConnection(ctx, connectionID, connection) - k.Logger(ctx).Info("connection state updated", "connection-id", connectionID, "previous-state", types.UNINITIALIZED, "new-state", types.TRYOPEN) + k.Logger.Info("connection state updated", "connection-id", connectionID, "previous-state", types.UNINITIALIZED, "new-state", types.TRYOPEN) defer telemetry.IncrCounter(1, "ibc", "connection", "open-try") - emitConnectionOpenTryEvent(ctx, connectionID, clientID, counterparty) + if err := k.emitConnectionOpenTryEvent(ctx, connectionID, clientID, counterparty); err != nil { + return "", nil + } return connectionID, nil } @@ -161,7 +165,7 @@ func (k *Keeper) ConnOpenAck( return err } - k.Logger(ctx).Info("connection state updated", "connection-id", connectionID, "previous-state", types.INIT, "new-state", types.OPEN) + k.Logger.Info("connection state updated", "connection-id", connectionID, "previous-state", types.INIT, "new-state", types.OPEN) defer telemetry.IncrCounter(1, "ibc", "connection", "open-ack") @@ -171,9 +175,7 @@ func (k *Keeper) ConnOpenAck( connection.Counterparty.ConnectionId = counterpartyConnectionID k.SetConnection(ctx, connectionID, connection) - emitConnectionOpenAckEvent(ctx, connectionID, connection) - - return nil + return k.emitConnectionOpenAckEvent(ctx, connectionID, connection) } // ConnOpenConfirm confirms opening of a connection on chain A to chain B, after @@ -215,11 +217,9 @@ func (k *Keeper) ConnOpenConfirm( // Update ChainB's connection to Open connection.State = types.OPEN k.SetConnection(ctx, connectionID, connection) - k.Logger(ctx).Info("connection state updated", "connection-id", connectionID, "previous-state", types.TRYOPEN, "new-state", types.OPEN) + k.Logger.Info("connection state updated", "connection-id", connectionID, "previous-state", types.TRYOPEN, "new-state", types.OPEN) defer telemetry.IncrCounter(1, "ibc", "connection", "open-confirm") - emitConnectionOpenConfirmEvent(ctx, connectionID, connection) - - return nil + return k.emitConnectionOpenConfirmEvent(ctx, connectionID, connection) } diff --git a/modules/core/03-connection/keeper/keeper.go b/modules/core/03-connection/keeper/keeper.go index 328e6898420..916e685a337 100644 --- a/modules/core/03-connection/keeper/keeper.go +++ b/modules/core/03-connection/keeper/keeper.go @@ -4,9 +4,8 @@ import ( "context" "errors" - 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" @@ -23,31 +22,25 @@ import ( // Keeper defines the IBC connection keeper type Keeper struct { + appmodule.Environment // implements gRPC QueryServer interface types.QueryServer - storeService corestore.KVStoreService legacySubspace types.ParamSubspace cdc codec.BinaryCodec clientKeeper types.ClientKeeper } // NewKeeper creates a new IBC connection Keeper instance -func NewKeeper(cdc codec.BinaryCodec, storeService corestore.KVStoreService, legacySubspace types.ParamSubspace, ck types.ClientKeeper) *Keeper { +func NewKeeper(cdc codec.BinaryCodec, env appmodule.Environment, legacySubspace types.ParamSubspace, ck types.ClientKeeper) *Keeper { return &Keeper{ - storeService: storeService, + Environment: env, cdc: cdc, legacySubspace: legacySubspace, clientKeeper: ck, } } -// Logger returns a module-specific logger. -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", "x/"+exported.ModuleName+"/"+types.SubModuleName) -} - // GetCommitmentPrefix returns the IBC connection store prefix as a commitment // Prefix func (*Keeper) GetCommitmentPrefix() exported.Prefix { @@ -66,7 +59,7 @@ func (k *Keeper) GenerateConnectionIdentifier(ctx context.Context) string { // GetConnection returns a connection with a particular identifier func (k *Keeper) GetConnection(ctx context.Context, connectionID string) (types.ConnectionEnd, bool) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) bz, err := store.Get(host.ConnectionKey(connectionID)) if err != nil { panic(err) @@ -85,7 +78,7 @@ func (k *Keeper) GetConnection(ctx context.Context, connectionID string) (types. // HasConnection returns a true if the connection with the given identifier // exists in the store. func (k *Keeper) HasConnection(ctx context.Context, connectionID string) bool { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) has, err := store.Has(host.ConnectionKey(connectionID)) if err != nil { return false @@ -95,7 +88,7 @@ func (k *Keeper) HasConnection(ctx context.Context, connectionID string) bool { // SetConnection sets a connection to the store func (k *Keeper) SetConnection(ctx context.Context, connectionID string, connection types.ConnectionEnd) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) bz := k.cdc.MustMarshal(&connection) if err := store.Set(host.ConnectionKey(connectionID), bz); err != nil { panic(err) @@ -105,7 +98,7 @@ func (k *Keeper) SetConnection(ctx context.Context, connectionID string, connect // GetClientConnectionPaths returns all the connection paths stored under a // particular client func (k *Keeper) GetClientConnectionPaths(ctx context.Context, clientID string) ([]string, bool) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) bz, err := store.Get(host.ClientConnectionsKey(clientID)) if err != nil { panic(err) @@ -122,7 +115,7 @@ func (k *Keeper) GetClientConnectionPaths(ctx context.Context, clientID string) // SetClientConnectionPaths sets the connections paths for client func (k *Keeper) SetClientConnectionPaths(ctx context.Context, clientID string, paths []string) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) clientPaths := types.ClientPaths{Paths: paths} bz := k.cdc.MustMarshal(&clientPaths) if err := store.Set(host.ClientConnectionsKey(clientID), bz); err != nil { @@ -132,7 +125,7 @@ func (k *Keeper) SetClientConnectionPaths(ctx context.Context, clientID string, // GetNextConnectionSequence gets the next connection sequence from the store. func (k *Keeper) GetNextConnectionSequence(ctx context.Context) uint64 { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) bz, err := store.Get([]byte(types.KeyNextConnectionSequence)) if err != nil { panic(err) @@ -147,7 +140,7 @@ func (k *Keeper) GetNextConnectionSequence(ctx context.Context) uint64 { // SetNextConnectionSequence sets the next connection sequence to the store. func (k *Keeper) SetNextConnectionSequence(ctx context.Context, sequence uint64) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) bz := sdk.Uint64ToBigEndian(sequence) if err := store.Set([]byte(types.KeyNextConnectionSequence), bz); err != nil { panic(err) @@ -159,8 +152,7 @@ func (k *Keeper) SetNextConnectionSequence(ctx context.Context, sequence uint64) // no paths are stored. func (k *Keeper) GetAllClientConnectionPaths(ctx context.Context) []types.ConnectionPaths { var allConnectionPaths []types.ConnectionPaths - sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/5917 - k.clientKeeper.IterateClientStates(sdkCtx, nil, func(clientID string, cs exported.ClientState) bool { + k.clientKeeper.IterateClientStates(ctx, nil, func(clientID string, cs exported.ClientState) bool { paths, found := k.GetClientConnectionPaths(ctx, clientID) if !found { // continue when connection handshake is not initialized @@ -178,11 +170,11 @@ func (k *Keeper) GetAllClientConnectionPaths(ctx context.Context) []types.Connec // For each ConnectionEnd, cb will be called. If the cb returns true, the // iterator will close and stop. func (k *Keeper) IterateConnections(ctx context.Context, cb func(types.IdentifiedConnection) bool) { - store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) + store := runtime.KVStoreAdapter(k.KVStoreService.OpenKVStore(ctx)) iterator := storetypes.KVStorePrefixIterator(store, []byte(host.KeyConnectionPrefix)) - defer coretypes.LogDeferred(k.Logger(ctx), func() error { return iterator.Close() }) + defer coretypes.LogDeferred(k.Logger, func() error { return iterator.Close() }) for ; iterator.Valid(); iterator.Next() { var connection types.ConnectionEnd k.cdc.MustUnmarshal(iterator.Value(), &connection) @@ -232,7 +224,7 @@ func (k *Keeper) addConnectionToClient(ctx context.Context, clientID, connection // GetParams returns the total set of ibc-connection 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) @@ -249,7 +241,7 @@ func (k *Keeper) GetParams(ctx context.Context) types.Params { // SetParams sets the total set of ibc-connection 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(¶ms) if err := store.Set([]byte(types.ParamsKey), bz); err != nil { panic(err) diff --git a/modules/core/03-connection/keeper/migrations.go b/modules/core/03-connection/keeper/migrations.go index d668687d017..83503ebf971 100644 --- a/modules/core/03-connection/keeper/migrations.go +++ b/modules/core/03-connection/keeper/migrations.go @@ -35,6 +35,6 @@ func (m Migrator) MigrateParams(ctx sdk.Context) error { } m.keeper.SetParams(ctx, params) - m.keeper.Logger(ctx).Info("successfully migrated connection to self-manage params") + m.keeper.Logger.Info("successfully migrated connection to self-manage params") return nil } diff --git a/modules/core/04-channel/keeper/events.go b/modules/core/04-channel/keeper/events.go index fad33c9808b..fa8477ad387 100644 --- a/modules/core/04-channel/keeper/events.go +++ b/modules/core/04-channel/keeper/events.go @@ -5,6 +5,8 @@ import ( "encoding/hex" "fmt" + "cosmossdk.io/core/event" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types" @@ -12,437 +14,447 @@ import ( ) // emitChannelOpenInitEvent emits a channel open init event -func emitChannelOpenInitEvent(ctx context.Context, portID string, channelID string, channel types.Channel) { - sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/7223 - sdkCtx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeChannelOpenInit, - sdk.NewAttribute(types.AttributeKeyPortID, portID), - sdk.NewAttribute(types.AttributeKeyChannelID, channelID), - sdk.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), - sdk.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), - sdk.NewAttribute(types.AttributeKeyVersion, channel.Version), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func (k *Keeper) emitChannelOpenInitEvent(ctx context.Context, portID string, channelID string, channel types.Channel) error { + if err := k.EventService.EventManager(ctx).EmitKV( + types.EventTypeChannelOpenInit, + event.NewAttribute(types.AttributeKeyPortID, portID), + event.NewAttribute(types.AttributeKeyChannelID, channelID), + event.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), + event.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), + event.NewAttribute(types.AttributeKeyVersion, channel.Version), + ); err != nil { + return err + } + + return k.EventService.EventManager(ctx).EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } // emitChannelOpenTryEvent emits a channel open try event -func emitChannelOpenTryEvent(ctx context.Context, portID string, channelID string, channel types.Channel) { - sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/7223 - sdkCtx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeChannelOpenTry, - sdk.NewAttribute(types.AttributeKeyPortID, portID), - sdk.NewAttribute(types.AttributeKeyChannelID, channelID), - sdk.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), - sdk.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), - sdk.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), - sdk.NewAttribute(types.AttributeKeyVersion, channel.Version), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func (k *Keeper) emitChannelOpenTryEvent(ctx context.Context, portID string, channelID string, channel types.Channel) error { + if err := k.EventService.EventManager(ctx).EmitKV( + types.EventTypeChannelOpenTry, + event.NewAttribute(types.AttributeKeyPortID, portID), + event.NewAttribute(types.AttributeKeyChannelID, channelID), + event.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), + event.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), + event.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), + event.NewAttribute(types.AttributeKeyVersion, channel.Version), + ); err != nil { + return err + } + + return k.EventService.EventManager(ctx).EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } // emitChannelOpenAckEvent emits a channel open acknowledge event -func emitChannelOpenAckEvent(ctx context.Context, portID string, channelID string, channel types.Channel) { - sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/7223 - sdkCtx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeChannelOpenAck, - sdk.NewAttribute(types.AttributeKeyPortID, portID), - sdk.NewAttribute(types.AttributeKeyChannelID, channelID), - sdk.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), - sdk.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), - sdk.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func (k *Keeper) emitChannelOpenAckEvent(ctx context.Context, portID string, channelID string, channel types.Channel) error { + if err := k.EventService.EventManager(ctx).EmitKV( + types.EventTypeChannelOpenAck, + event.NewAttribute(types.AttributeKeyPortID, portID), + event.NewAttribute(types.AttributeKeyChannelID, channelID), + event.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), + event.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), + event.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), + ); err != nil { + return err + } + + return k.EventService.EventManager(ctx).EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } // emitChannelOpenConfirmEvent emits a channel open confirm event -func emitChannelOpenConfirmEvent(ctx context.Context, portID string, channelID string, channel types.Channel) { - sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/7223 - sdkCtx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeChannelOpenConfirm, - sdk.NewAttribute(types.AttributeKeyPortID, portID), - sdk.NewAttribute(types.AttributeKeyChannelID, channelID), - sdk.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), - sdk.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), - sdk.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func (k *Keeper) emitChannelOpenConfirmEvent(ctx context.Context, portID string, channelID string, channel types.Channel) error { + if err := k.EventService.EventManager(ctx).EmitKV( + types.EventTypeChannelOpenConfirm, + event.NewAttribute(types.AttributeKeyPortID, portID), + event.NewAttribute(types.AttributeKeyChannelID, channelID), + event.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), + event.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), + event.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), + ); err != nil { + return err + } + + return k.EventService.EventManager(ctx).EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } // emitChannelCloseInitEvent emits a channel close init event -func emitChannelCloseInitEvent(ctx context.Context, portID string, channelID string, channel types.Channel) { - sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/7223 - sdkCtx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeChannelCloseInit, - sdk.NewAttribute(types.AttributeKeyPortID, portID), - sdk.NewAttribute(types.AttributeKeyChannelID, channelID), - sdk.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), - sdk.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), - sdk.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func (k *Keeper) emitChannelCloseInitEvent(ctx context.Context, portID string, channelID string, channel types.Channel) error { + if err := k.EventService.EventManager(ctx).EmitKV( + types.EventTypeChannelCloseInit, + event.NewAttribute(types.AttributeKeyPortID, portID), + event.NewAttribute(types.AttributeKeyChannelID, channelID), + event.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), + event.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), + event.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), + ); err != nil { + return err + } + + return k.EventService.EventManager(ctx).EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } // emitChannelCloseConfirmEvent emits a channel close confirm event -func emitChannelCloseConfirmEvent(ctx context.Context, portID string, channelID string, channel types.Channel) { - sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/7223 - sdkCtx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeChannelCloseConfirm, - sdk.NewAttribute(types.AttributeKeyPortID, portID), - sdk.NewAttribute(types.AttributeKeyChannelID, channelID), - sdk.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), - sdk.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), - sdk.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func (k *Keeper) emitChannelCloseConfirmEvent(ctx context.Context, portID string, channelID string, channel types.Channel) error { + if err := k.EventService.EventManager(ctx).EmitKV( + types.EventTypeChannelCloseConfirm, + event.NewAttribute(types.AttributeKeyPortID, portID), + event.NewAttribute(types.AttributeKeyChannelID, channelID), + event.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), + event.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), + event.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), + ); err != nil { + return err + } + + return k.EventService.EventManager(ctx).EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } // emitSendPacketEvent emits an event with packet data along with other packet information for relayer // to pick up and relay to other chain -func emitSendPacketEvent(ctx sdk.Context, packet types.Packet, channel types.Channel, timeoutHeight exported.Height) { - ctx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeSendPacket, - sdk.NewAttribute(types.AttributeKeyDataHex, hex.EncodeToString(packet.GetData())), - sdk.NewAttribute(types.AttributeKeyTimeoutHeight, timeoutHeight.String()), - sdk.NewAttribute(types.AttributeKeyTimeoutTimestamp, fmt.Sprintf("%d", packet.GetTimeoutTimestamp())), - sdk.NewAttribute(types.AttributeKeySequence, fmt.Sprintf("%d", packet.GetSequence())), - sdk.NewAttribute(types.AttributeKeySrcPort, packet.GetSourcePort()), - sdk.NewAttribute(types.AttributeKeySrcChannel, packet.GetSourceChannel()), - sdk.NewAttribute(types.AttributeKeyDstPort, packet.GetDestPort()), - sdk.NewAttribute(types.AttributeKeyDstChannel, packet.GetDestChannel()), - sdk.NewAttribute(types.AttributeKeyChannelOrdering, channel.Ordering.String()), - // we only support 1-hop packets now, and that is the most important hop for a relayer - // (is it going to a chain I am connected to) - sdk.NewAttribute(types.AttributeKeyConnection, channel.ConnectionHops[0]), // DEPRECATED - sdk.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func (k *Keeper) emitSendPacketEvent(ctx context.Context, packet types.Packet, channel types.Channel, timeoutHeight exported.Height) error { + if err := k.EventService.EventManager(ctx).EmitKV( + types.EventTypeSendPacket, + event.NewAttribute(types.AttributeKeyDataHex, hex.EncodeToString(packet.GetData())), + event.NewAttribute(types.AttributeKeyTimeoutHeight, timeoutHeight.String()), + event.NewAttribute(types.AttributeKeyTimeoutTimestamp, fmt.Sprintf("%d", packet.GetTimeoutTimestamp())), + event.NewAttribute(types.AttributeKeySequence, fmt.Sprintf("%d", packet.GetSequence())), + event.NewAttribute(types.AttributeKeySrcPort, packet.GetSourcePort()), + event.NewAttribute(types.AttributeKeySrcChannel, packet.GetSourceChannel()), + event.NewAttribute(types.AttributeKeyDstPort, packet.GetDestPort()), + event.NewAttribute(types.AttributeKeyDstChannel, packet.GetDestChannel()), + event.NewAttribute(types.AttributeKeyChannelOrdering, channel.Ordering.String()), + // we only support 1-hop packets now, and that is the most important hop for a relayer + // (is it going to a chain I am connected to) + event.NewAttribute(types.AttributeKeyConnection, channel.ConnectionHops[0]), // DEPRECATED + event.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), + ); err != nil { + return err + } + + return k.EventService.EventManager(ctx).EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } // emitRecvPacketEvent emits a receive packet event. It will be emitted both the first time a packet // is received for a certain sequence and for all duplicate receives. -func emitRecvPacketEvent(ctx sdk.Context, packet types.Packet, channel types.Channel) { - ctx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeRecvPacket, - sdk.NewAttribute(types.AttributeKeyDataHex, hex.EncodeToString(packet.GetData())), - sdk.NewAttribute(types.AttributeKeyTimeoutHeight, packet.GetTimeoutHeight().String()), - sdk.NewAttribute(types.AttributeKeyTimeoutTimestamp, fmt.Sprintf("%d", packet.GetTimeoutTimestamp())), - sdk.NewAttribute(types.AttributeKeySequence, fmt.Sprintf("%d", packet.GetSequence())), - sdk.NewAttribute(types.AttributeKeySrcPort, packet.GetSourcePort()), - sdk.NewAttribute(types.AttributeKeySrcChannel, packet.GetSourceChannel()), - sdk.NewAttribute(types.AttributeKeyDstPort, packet.GetDestPort()), - sdk.NewAttribute(types.AttributeKeyDstChannel, packet.GetDestChannel()), - sdk.NewAttribute(types.AttributeKeyChannelOrdering, channel.Ordering.String()), - // we only support 1-hop packets now, and that is the most important hop for a relayer - // (is it going to a chain I am connected to) - sdk.NewAttribute(types.AttributeKeyConnection, channel.ConnectionHops[0]), // DEPRECATED - sdk.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func (k *Keeper) emitRecvPacketEvent(ctx context.Context, packet types.Packet, channel types.Channel) error { + if err := k.EventService.EventManager(ctx).EmitKV( + types.EventTypeRecvPacket, + event.NewAttribute(types.AttributeKeyDataHex, hex.EncodeToString(packet.GetData())), + event.NewAttribute(types.AttributeKeyTimeoutHeight, packet.GetTimeoutHeight().String()), + event.NewAttribute(types.AttributeKeyTimeoutTimestamp, fmt.Sprintf("%d", packet.GetTimeoutTimestamp())), + event.NewAttribute(types.AttributeKeySequence, fmt.Sprintf("%d", packet.GetSequence())), + event.NewAttribute(types.AttributeKeySrcPort, packet.GetSourcePort()), + event.NewAttribute(types.AttributeKeySrcChannel, packet.GetSourceChannel()), + event.NewAttribute(types.AttributeKeyDstPort, packet.GetDestPort()), + event.NewAttribute(types.AttributeKeyDstChannel, packet.GetDestChannel()), + event.NewAttribute(types.AttributeKeyChannelOrdering, channel.Ordering.String()), + // we only support 1-hop packets now, and that is the most important hop for a relayer + // (is it going to a chain I am connected to) + event.NewAttribute(types.AttributeKeyConnection, channel.ConnectionHops[0]), // DEPRECATED + event.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), + ); err != nil { + return err + } + + return k.EventService.EventManager(ctx).EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } // emitWriteAcknowledgementEvent emits an event that the relayer can query for -func emitWriteAcknowledgementEvent(ctx sdk.Context, packet types.Packet, channel types.Channel, acknowledgement []byte) { - ctx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeWriteAck, - sdk.NewAttribute(types.AttributeKeyDataHex, hex.EncodeToString(packet.GetData())), - sdk.NewAttribute(types.AttributeKeyTimeoutHeight, packet.GetTimeoutHeight().String()), - sdk.NewAttribute(types.AttributeKeyTimeoutTimestamp, fmt.Sprintf("%d", packet.GetTimeoutTimestamp())), - sdk.NewAttribute(types.AttributeKeySequence, fmt.Sprintf("%d", packet.GetSequence())), - sdk.NewAttribute(types.AttributeKeySrcPort, packet.GetSourcePort()), - sdk.NewAttribute(types.AttributeKeySrcChannel, packet.GetSourceChannel()), - sdk.NewAttribute(types.AttributeKeyDstPort, packet.GetDestPort()), - sdk.NewAttribute(types.AttributeKeyDstChannel, packet.GetDestChannel()), - sdk.NewAttribute(types.AttributeKeyAckHex, hex.EncodeToString(acknowledgement)), - sdk.NewAttribute(types.AttributeKeyChannelOrdering, channel.Ordering.String()), - // we only support 1-hop packets now, and that is the most important hop for a relayer - // (is it going to a chain I am connected to) - sdk.NewAttribute(types.AttributeKeyConnection, channel.ConnectionHops[0]), // DEPRECATED - sdk.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func (k *Keeper) emitWriteAcknowledgementEvent(ctx context.Context, packet types.Packet, channel types.Channel, acknowledgement []byte) error { + if err := k.EventService.EventManager(ctx).EmitKV( + types.EventTypeWriteAck, + event.NewAttribute(types.AttributeKeyDataHex, hex.EncodeToString(packet.GetData())), + event.NewAttribute(types.AttributeKeyTimeoutHeight, packet.GetTimeoutHeight().String()), + event.NewAttribute(types.AttributeKeyTimeoutTimestamp, fmt.Sprintf("%d", packet.GetTimeoutTimestamp())), + event.NewAttribute(types.AttributeKeySequence, fmt.Sprintf("%d", packet.GetSequence())), + event.NewAttribute(types.AttributeKeySrcPort, packet.GetSourcePort()), + event.NewAttribute(types.AttributeKeySrcChannel, packet.GetSourceChannel()), + event.NewAttribute(types.AttributeKeyDstPort, packet.GetDestPort()), + event.NewAttribute(types.AttributeKeyDstChannel, packet.GetDestChannel()), + event.NewAttribute(types.AttributeKeyAckHex, hex.EncodeToString(acknowledgement)), + event.NewAttribute(types.AttributeKeyChannelOrdering, channel.Ordering.String()), + // we only support 1-hop packets now, and that is the most important hop for a relayer + // (is it going to a chain I am connected to) + event.NewAttribute(types.AttributeKeyConnection, channel.ConnectionHops[0]), // DEPRECATED + event.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), + ); err != nil { + return err + } + + return k.EventService.EventManager(ctx).EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } // emitAcknowledgePacketEvent emits an acknowledge packet event. It will be emitted both the first time // a packet is acknowledged for a certain sequence and for all duplicate acknowledgements. -func emitAcknowledgePacketEvent(ctx context.Context, packet types.Packet, channel types.Channel) { - sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/7223 - sdkCtx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeAcknowledgePacket, - sdk.NewAttribute(types.AttributeKeyTimeoutHeight, packet.GetTimeoutHeight().String()), - sdk.NewAttribute(types.AttributeKeyTimeoutTimestamp, fmt.Sprintf("%d", packet.GetTimeoutTimestamp())), - sdk.NewAttribute(types.AttributeKeySequence, fmt.Sprintf("%d", packet.GetSequence())), - sdk.NewAttribute(types.AttributeKeySrcPort, packet.GetSourcePort()), - sdk.NewAttribute(types.AttributeKeySrcChannel, packet.GetSourceChannel()), - sdk.NewAttribute(types.AttributeKeyDstPort, packet.GetDestPort()), - sdk.NewAttribute(types.AttributeKeyDstChannel, packet.GetDestChannel()), - sdk.NewAttribute(types.AttributeKeyChannelOrdering, channel.Ordering.String()), - // we only support 1-hop packets now, and that is the most important hop for a relayer - // (is it going to a chain I am connected to) - sdk.NewAttribute(types.AttributeKeyConnection, channel.ConnectionHops[0]), // DEPRECATED - sdk.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func (k *Keeper) emitAcknowledgePacketEvent(ctx context.Context, packet types.Packet, channel types.Channel) error { + if err := k.EventService.EventManager(ctx).EmitKV( + types.EventTypeAcknowledgePacket, + event.NewAttribute(types.AttributeKeyTimeoutHeight, packet.GetTimeoutHeight().String()), + event.NewAttribute(types.AttributeKeyTimeoutTimestamp, fmt.Sprintf("%d", packet.GetTimeoutTimestamp())), + event.NewAttribute(types.AttributeKeySequence, fmt.Sprintf("%d", packet.GetSequence())), + event.NewAttribute(types.AttributeKeySrcPort, packet.GetSourcePort()), + event.NewAttribute(types.AttributeKeySrcChannel, packet.GetSourceChannel()), + event.NewAttribute(types.AttributeKeyDstPort, packet.GetDestPort()), + event.NewAttribute(types.AttributeKeyDstChannel, packet.GetDestChannel()), + event.NewAttribute(types.AttributeKeyChannelOrdering, channel.Ordering.String()), + // we only support 1-hop packets now, and that is the most important hop for a relayer + // (is it going to a chain I am connected to) + event.NewAttribute(types.AttributeKeyConnection, channel.ConnectionHops[0]), // DEPRECATED + event.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), + ); err != nil { + return err + } + + return k.EventService.EventManager(ctx).EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } // 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 context.Context, packet types.Packet, channel types.Channel) { - sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/7223 - sdkCtx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeTimeoutPacket, - sdk.NewAttribute(types.AttributeKeyTimeoutHeight, packet.GetTimeoutHeight().String()), - sdk.NewAttribute(types.AttributeKeyTimeoutTimestamp, fmt.Sprintf("%d", packet.GetTimeoutTimestamp())), - sdk.NewAttribute(types.AttributeKeySequence, fmt.Sprintf("%d", packet.GetSequence())), - sdk.NewAttribute(types.AttributeKeySrcPort, packet.GetSourcePort()), - sdk.NewAttribute(types.AttributeKeySrcChannel, packet.GetSourceChannel()), - sdk.NewAttribute(types.AttributeKeyDstPort, packet.GetDestPort()), - sdk.NewAttribute(types.AttributeKeyDstChannel, packet.GetDestChannel()), - sdk.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), - sdk.NewAttribute(types.AttributeKeyChannelOrdering, channel.Ordering.String()), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func (k *Keeper) emitTimeoutPacketEvent(ctx context.Context, packet types.Packet, channel types.Channel) error { + if err := k.EventService.EventManager(ctx).EmitKV( + types.EventTypeTimeoutPacket, + event.NewAttribute(types.AttributeKeyTimeoutHeight, packet.GetTimeoutHeight().String()), + event.NewAttribute(types.AttributeKeyTimeoutTimestamp, fmt.Sprintf("%d", packet.GetTimeoutTimestamp())), + event.NewAttribute(types.AttributeKeySequence, fmt.Sprintf("%d", packet.GetSequence())), + event.NewAttribute(types.AttributeKeySrcPort, packet.GetSourcePort()), + event.NewAttribute(types.AttributeKeySrcChannel, packet.GetSourceChannel()), + event.NewAttribute(types.AttributeKeyDstPort, packet.GetDestPort()), + event.NewAttribute(types.AttributeKeyDstChannel, packet.GetDestChannel()), + event.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), + event.NewAttribute(types.AttributeKeyChannelOrdering, channel.Ordering.String()), + ); err != nil { + return err + } + + return k.EventService.EventManager(ctx).EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } // emitChannelClosedEvent emits a channel closed event. -func emitChannelClosedEvent(ctx context.Context, packet types.Packet, channel types.Channel) { - sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/7223 - sdkCtx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeChannelClosed, - sdk.NewAttribute(types.AttributeKeyPortID, packet.GetSourcePort()), - sdk.NewAttribute(types.AttributeKeyChannelID, packet.GetSourceChannel()), - sdk.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), - sdk.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), - sdk.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), - sdk.NewAttribute(types.AttributeKeyChannelOrdering, channel.Ordering.String()), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func (k *Keeper) emitChannelClosedEvent(ctx context.Context, packet types.Packet, channel types.Channel) error { + if err := k.EventService.EventManager(ctx).EmitKV( + types.EventTypeChannelClosed, + event.NewAttribute(types.AttributeKeyPortID, packet.GetSourcePort()), + event.NewAttribute(types.AttributeKeyChannelID, packet.GetSourceChannel()), + event.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), + event.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), + event.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), + event.NewAttribute(types.AttributeKeyChannelOrdering, channel.Ordering.String()), + ); err != nil { + return err + } + + return k.EventService.EventManager(ctx).EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } // EmitChannelUpgradeInitEvent emits a channel upgrade init event -func EmitChannelUpgradeInitEvent(ctx sdk.Context, portID string, channelID string, channel types.Channel, upgrade types.Upgrade) { - ctx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeChannelUpgradeInit, - sdk.NewAttribute(types.AttributeKeyPortID, portID), - sdk.NewAttribute(types.AttributeKeyChannelID, channelID), - sdk.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), - sdk.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), - sdk.NewAttribute(types.AttributeKeyUpgradeSequence, fmt.Sprintf("%d", channel.UpgradeSequence)), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func (k *Keeper) EmitChannelUpgradeInitEvent(ctx context.Context, portID string, channelID string, channel types.Channel, upgrade types.Upgrade) error { + if err := k.EventService.EventManager(ctx).EmitKV( + types.EventTypeChannelUpgradeInit, + event.NewAttribute(types.AttributeKeyPortID, portID), + event.NewAttribute(types.AttributeKeyChannelID, channelID), + event.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), + event.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), + event.NewAttribute(types.AttributeKeyUpgradeSequence, fmt.Sprintf("%d", channel.UpgradeSequence)), + ); err != nil { + return err + } + + return k.EventService.EventManager(ctx).EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } // EmitChannelUpgradeTryEvent emits a channel upgrade try event -func EmitChannelUpgradeTryEvent(ctx sdk.Context, portID string, channelID string, channel types.Channel, upgrade types.Upgrade) { - ctx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeChannelUpgradeTry, - sdk.NewAttribute(types.AttributeKeyPortID, portID), - sdk.NewAttribute(types.AttributeKeyChannelID, channelID), - sdk.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), - sdk.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), - sdk.NewAttribute(types.AttributeKeyUpgradeSequence, fmt.Sprintf("%d", channel.UpgradeSequence)), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func (k *Keeper) EmitChannelUpgradeTryEvent(ctx context.Context, portID string, channelID string, channel types.Channel, upgrade types.Upgrade) error { + if err := k.EventService.EventManager(ctx).EmitKV( + types.EventTypeChannelUpgradeTry, + event.NewAttribute(types.AttributeKeyPortID, portID), + event.NewAttribute(types.AttributeKeyChannelID, channelID), + event.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), + event.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), + event.NewAttribute(types.AttributeKeyUpgradeSequence, fmt.Sprintf("%d", channel.UpgradeSequence)), + ); err != nil { + return err + } + + return k.EventService.EventManager(ctx).EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } // EmitChannelUpgradeAckEvent emits a channel upgrade ack event -func EmitChannelUpgradeAckEvent(ctx sdk.Context, portID string, channelID string, channel types.Channel, upgrade types.Upgrade) { - ctx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeChannelUpgradeAck, - sdk.NewAttribute(types.AttributeKeyPortID, portID), - sdk.NewAttribute(types.AttributeKeyChannelID, channelID), - sdk.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), - sdk.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), - sdk.NewAttribute(types.AttributeKeyUpgradeSequence, fmt.Sprintf("%d", channel.UpgradeSequence)), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func (k *Keeper) EmitChannelUpgradeAckEvent(ctx context.Context, portID string, channelID string, channel types.Channel, upgrade types.Upgrade) error { + if err := k.EventService.EventManager(ctx).EmitKV( + types.EventTypeChannelUpgradeAck, + event.NewAttribute(types.AttributeKeyPortID, portID), + event.NewAttribute(types.AttributeKeyChannelID, channelID), + event.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), + event.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), + event.NewAttribute(types.AttributeKeyUpgradeSequence, fmt.Sprintf("%d", channel.UpgradeSequence)), + ); err != nil { + return err + } + + return k.EventService.EventManager(ctx).EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } // EmitChannelUpgradeConfirmEvent emits a channel upgrade confirm event -func EmitChannelUpgradeConfirmEvent(ctx sdk.Context, portID, channelID string, channel types.Channel) { - ctx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeChannelUpgradeConfirm, - sdk.NewAttribute(types.AttributeKeyPortID, portID), - sdk.NewAttribute(types.AttributeKeyChannelID, channelID), - sdk.NewAttribute(types.AttributeKeyChannelState, channel.State.String()), - sdk.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), - sdk.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), - sdk.NewAttribute(types.AttributeKeyUpgradeSequence, fmt.Sprintf("%d", channel.UpgradeSequence)), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func (k *Keeper) EmitChannelUpgradeConfirmEvent(ctx context.Context, portID, channelID string, channel types.Channel) error { + if err := k.EventService.EventManager(ctx).EmitKV( + types.EventTypeChannelUpgradeConfirm, + event.NewAttribute(types.AttributeKeyPortID, portID), + event.NewAttribute(types.AttributeKeyChannelID, channelID), + event.NewAttribute(types.AttributeKeyChannelState, channel.State.String()), + event.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), + event.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), + event.NewAttribute(types.AttributeKeyUpgradeSequence, fmt.Sprintf("%d", channel.UpgradeSequence)), + ); err != nil { + return err + } + + return k.EventService.EventManager(ctx).EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } // EmitChannelUpgradeOpenEvent emits a channel upgrade open event -func EmitChannelUpgradeOpenEvent(ctx sdk.Context, portID string, channelID string, channel types.Channel) { - ctx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeChannelUpgradeOpen, - sdk.NewAttribute(types.AttributeKeyPortID, portID), - sdk.NewAttribute(types.AttributeKeyChannelID, channelID), - sdk.NewAttribute(types.AttributeKeyChannelState, channel.State.String()), - sdk.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), - sdk.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), - sdk.NewAttribute(types.AttributeKeyUpgradeSequence, fmt.Sprintf("%d", channel.UpgradeSequence)), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func (k *Keeper) EmitChannelUpgradeOpenEvent(ctx context.Context, portID string, channelID string, channel types.Channel) error { + if err := k.EventService.EventManager(ctx).EmitKV( + types.EventTypeChannelUpgradeOpen, + event.NewAttribute(types.AttributeKeyPortID, portID), + event.NewAttribute(types.AttributeKeyChannelID, channelID), + event.NewAttribute(types.AttributeKeyChannelState, channel.State.String()), + event.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), + event.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), + event.NewAttribute(types.AttributeKeyUpgradeSequence, fmt.Sprintf("%d", channel.UpgradeSequence)), + ); err != nil { + return err + } + + return k.EventService.EventManager(ctx).EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } // EmitChannelUpgradeTimeoutEvent emits an upgrade timeout event. -func EmitChannelUpgradeTimeoutEvent(ctx sdk.Context, portID string, channelID string, channel types.Channel, upgrade types.Upgrade) { - ctx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeChannelUpgradeTimeout, - sdk.NewAttribute(types.AttributeKeyPortID, portID), - sdk.NewAttribute(types.AttributeKeyChannelID, channelID), - sdk.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), - sdk.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), - sdk.NewAttribute(types.AttributeKeyUpgradeTimeoutHeight, upgrade.Timeout.Height.String()), - sdk.NewAttribute(types.AttributeKeyUpgradeTimeoutTimestamp, fmt.Sprintf("%d", upgrade.Timeout.Timestamp)), - sdk.NewAttribute(types.AttributeKeyUpgradeSequence, fmt.Sprintf("%d", channel.UpgradeSequence)), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func (k *Keeper) EmitChannelUpgradeTimeoutEvent(ctx context.Context, portID string, channelID string, channel types.Channel, upgrade types.Upgrade) error { + if err := k.EventService.EventManager(ctx).EmitKV( + types.EventTypeChannelUpgradeTimeout, + event.NewAttribute(types.AttributeKeyPortID, portID), + event.NewAttribute(types.AttributeKeyChannelID, channelID), + event.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), + event.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), + event.NewAttribute(types.AttributeKeyUpgradeTimeoutHeight, upgrade.Timeout.Height.String()), + event.NewAttribute(types.AttributeKeyUpgradeTimeoutTimestamp, fmt.Sprintf("%d", upgrade.Timeout.Timestamp)), + event.NewAttribute(types.AttributeKeyUpgradeSequence, fmt.Sprintf("%d", channel.UpgradeSequence)), + ); err != nil { + return err + } + + return k.EventService.EventManager(ctx).EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } // EmitErrorReceiptEvent emits an error receipt event -func EmitErrorReceiptEvent(ctx context.Context, portID string, channelID string, channel types.Channel, err error) { - sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/7223 - sdkCtx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeChannelUpgradeError, - sdk.NewAttribute(types.AttributeKeyPortID, portID), - sdk.NewAttribute(types.AttributeKeyChannelID, channelID), - sdk.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), - sdk.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), - sdk.NewAttribute(types.AttributeKeyUpgradeSequence, fmt.Sprintf("%d", channel.UpgradeSequence)), - sdk.NewAttribute(types.AttributeKeyErrorReceipt, err.Error()), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func (k *Keeper) EmitErrorReceiptEvent(ctx context.Context, portID string, channelID string, channel types.Channel, err error) error { + if err := k.EventService.EventManager(ctx).EmitKV( + types.EventTypeChannelUpgradeError, + event.NewAttribute(types.AttributeKeyPortID, portID), + event.NewAttribute(types.AttributeKeyChannelID, channelID), + event.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), + event.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), + event.NewAttribute(types.AttributeKeyUpgradeSequence, fmt.Sprintf("%d", channel.UpgradeSequence)), + event.NewAttribute(types.AttributeKeyErrorReceipt, err.Error()), + ); err != nil { + return err + } + + return k.EventService.EventManager(ctx).EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } // EmitChannelUpgradeCancelEvent emits an upgraded cancelled event. -func EmitChannelUpgradeCancelEvent(ctx sdk.Context, portID string, channelID string, channel types.Channel, upgrade types.Upgrade) { - ctx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeChannelUpgradeCancel, - sdk.NewAttribute(types.AttributeKeyPortID, portID), - sdk.NewAttribute(types.AttributeKeyChannelID, channelID), - sdk.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), - sdk.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), - sdk.NewAttribute(types.AttributeKeyUpgradeSequence, fmt.Sprintf("%d", channel.UpgradeSequence)), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func (k *Keeper) EmitChannelUpgradeCancelEvent(ctx context.Context, portID string, channelID string, channel types.Channel, upgrade types.Upgrade) error { + if err := k.EventService.EventManager(ctx).EmitKV( + types.EventTypeChannelUpgradeCancel, + event.NewAttribute(types.AttributeKeyPortID, portID), + event.NewAttribute(types.AttributeKeyChannelID, channelID), + event.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), + event.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), + event.NewAttribute(types.AttributeKeyUpgradeSequence, fmt.Sprintf("%d", channel.UpgradeSequence)), + ); err != nil { + return err + } + + return k.EventService.EventManager(ctx).EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } // emitChannelFlushCompleteEvent emits an flushing event. -func emitChannelFlushCompleteEvent(ctx context.Context, portID string, channelID string, channel types.Channel) { - sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/7223 - sdkCtx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeChannelFlushComplete, - sdk.NewAttribute(types.AttributeKeyPortID, portID), - sdk.NewAttribute(types.AttributeKeyChannelID, channelID), - sdk.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), - sdk.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), - sdk.NewAttribute(types.AttributeKeyChannelState, channel.State.String()), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func (k *Keeper) emitChannelFlushCompleteEvent(ctx context.Context, portID string, channelID string, channel types.Channel) error { + if err := k.EventService.EventManager(ctx).EmitKV( + types.EventTypeChannelFlushComplete, + event.NewAttribute(types.AttributeKeyPortID, portID), + event.NewAttribute(types.AttributeKeyChannelID, channelID), + event.NewAttribute(types.AttributeCounterpartyPortID, channel.Counterparty.PortId), + event.NewAttribute(types.AttributeCounterpartyChannelID, channel.Counterparty.ChannelId), + event.NewAttribute(types.AttributeKeyChannelState, channel.State.String()), + ); err != nil { + return err + } + + return k.EventService.EventManager(ctx).EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } diff --git a/modules/core/04-channel/keeper/grpc_query.go b/modules/core/04-channel/keeper/grpc_query.go index 648a8028534..d2ef8fed802 100644 --- a/modules/core/04-channel/keeper/grpc_query.go +++ b/modules/core/04-channel/keeper/grpc_query.go @@ -65,7 +65,7 @@ func (q *queryServer) Channels(ctx context.Context, req *types.QueryChannelsRequ } var channels []*types.IdentifiedChannel - store := prefix.NewStore(runtime.KVStoreAdapter(q.storeService.OpenKVStore(ctx)), []byte(host.KeyChannelEndPrefix)) + store := prefix.NewStore(runtime.KVStoreAdapter(q.KVStoreService.OpenKVStore(ctx)), []byte(host.KeyChannelEndPrefix)) pageRes, err := query.Paginate(store, req.Pagination, func(key, value []byte) error { var result types.Channel @@ -106,7 +106,7 @@ func (q *queryServer) ConnectionChannels(ctx context.Context, req *types.QueryCo var channels []*types.IdentifiedChannel - store := prefix.NewStore(runtime.KVStoreAdapter(q.storeService.OpenKVStore(ctx)), []byte(host.KeyChannelEndPrefix)) + store := prefix.NewStore(runtime.KVStoreAdapter(q.KVStoreService.OpenKVStore(ctx)), []byte(host.KeyChannelEndPrefix)) pageRes, err := query.FilteredPaginate(store, req.Pagination, func(key, value []byte, accumulate bool) (bool, error) { // filter any metadata stored under channel key @@ -254,7 +254,7 @@ func (q *queryServer) PacketCommitments(ctx context.Context, req *types.QueryPac ) } var commitments []*types.PacketState - store := prefix.NewStore(runtime.KVStoreAdapter(q.storeService.OpenKVStore(ctx)), host.PacketCommitmentPrefixKey(req.PortId, req.ChannelId)) + store := prefix.NewStore(runtime.KVStoreAdapter(q.KVStoreService.OpenKVStore(ctx)), host.PacketCommitmentPrefixKey(req.PortId, req.ChannelId)) pageRes, err := query.Paginate(store, req.Pagination, func(key, value []byte) error { keySplit := strings.Split(string(key), "/") @@ -352,7 +352,7 @@ func (q *queryServer) PacketAcknowledgements(ctx context.Context, req *types.Que ) } var acks []*types.PacketState - store := prefix.NewStore(runtime.KVStoreAdapter(q.storeService.OpenKVStore(ctx)), host.PacketAcknowledgementPrefixKey(req.PortId, req.ChannelId)) + store := prefix.NewStore(runtime.KVStoreAdapter(q.KVStoreService.OpenKVStore(ctx)), host.PacketAcknowledgementPrefixKey(req.PortId, req.ChannelId)) // if a list of packet sequences is provided then query for each specific ack and return a list <= len(req.PacketCommitmentSequences) // otherwise, maintain previous behaviour and perform paginated query diff --git a/modules/core/04-channel/keeper/handshake.go b/modules/core/04-channel/keeper/handshake.go index 635b178e067..0c587c6e7b2 100644 --- a/modules/core/04-channel/keeper/handshake.go +++ b/modules/core/04-channel/keeper/handshake.go @@ -76,11 +76,11 @@ func (k *Keeper) WriteOpenInitChannel( k.SetNextSequenceRecv(ctx, portID, channelID, 1) k.SetNextSequenceAck(ctx, portID, channelID, 1) - k.Logger(ctx).Info("channel state updated", "port-id", portID, "channel-id", channelID, "previous-state", types.UNINITIALIZED, "new-state", types.INIT) + k.Logger.Info("channel state updated", "port-id", portID, "channel-id", channelID, "previous-state", types.UNINITIALIZED, "new-state", types.INIT) defer telemetry.IncrCounter(1, "ibc", "channel", "open-init") - emitChannelOpenInitEvent(ctx, portID, channelID, channel) + k.emitChannelOpenInitEvent(ctx, portID, channelID, channel) } // ChanOpenTry is called by a module to accept the first step of a channel opening @@ -169,11 +169,11 @@ func (k *Keeper) WriteOpenTryChannel( k.SetChannel(ctx, portID, channelID, channel) - k.Logger(ctx).Info("channel state updated", "port-id", portID, "channel-id", channelID, "previous-state", types.UNINITIALIZED, "new-state", types.TRYOPEN) + k.Logger.Info("channel state updated", "port-id", portID, "channel-id", channelID, "previous-state", types.UNINITIALIZED, "new-state", types.TRYOPEN) defer telemetry.IncrCounter(1, "ibc", "channel", "open-try") - emitChannelOpenTryEvent(ctx, portID, channelID, channel) + k.emitChannelOpenTryEvent(ctx, portID, channelID, channel) } // ChanOpenAck is called by the handshake-originating module to acknowledge the @@ -239,11 +239,11 @@ func (k *Keeper) WriteOpenAckChannel( channel.Counterparty.ChannelId = counterpartyChannelID k.SetChannel(ctx, portID, channelID, channel) - k.Logger(ctx).Info("channel state updated", "port-id", portID, "channel-id", channelID, "previous-state", types.INIT, "new-state", types.OPEN) + k.Logger.Info("channel state updated", "port-id", portID, "channel-id", channelID, "previous-state", types.INIT, "new-state", types.OPEN) defer telemetry.IncrCounter(1, "ibc", "channel", "open-ack") - emitChannelOpenAckEvent(ctx, portID, channelID, channel) + k.emitChannelOpenAckEvent(ctx, portID, channelID, channel) } // ChanOpenConfirm is called by the handshake-accepting module to confirm the acknowledgement @@ -306,11 +306,11 @@ func (k *Keeper) WriteOpenConfirmChannel( channel.State = types.OPEN k.SetChannel(ctx, portID, channelID, channel) - k.Logger(ctx).Info("channel state updated", "port-id", portID, "channel-id", channelID, "previous-state", types.TRYOPEN, "new-state", types.OPEN) + k.Logger.Info("channel state updated", "port-id", portID, "channel-id", channelID, "previous-state", types.TRYOPEN, "new-state", types.OPEN) defer telemetry.IncrCounter(1, "ibc", "channel", "open-confirm") - emitChannelOpenConfirmEvent(ctx, portID, channelID, channel) + k.emitChannelOpenConfirmEvent(ctx, portID, channelID, channel) } // Closing Handshake @@ -347,16 +347,14 @@ func (k *Keeper) ChanCloseInit( return errorsmod.Wrapf(connectiontypes.ErrInvalidConnectionState, "connection state is not OPEN (got %s)", connectionEnd.State) } - k.Logger(ctx).Info("channel state updated", "port-id", portID, "channel-id", channelID, "previous-state", channel.State, "new-state", types.CLOSED) + k.Logger.Info("channel state updated", "port-id", portID, "channel-id", channelID, "previous-state", channel.State, "new-state", types.CLOSED) defer telemetry.IncrCounter(1, "ibc", "channel", "close-init") channel.State = types.CLOSED k.SetChannel(ctx, portID, channelID, channel) - emitChannelCloseInitEvent(ctx, portID, channelID, channel) - - return nil + return k.emitChannelCloseInitEvent(ctx, portID, channelID, channel) } // ChanCloseConfirm is called by the counterparty module to close their end of the @@ -410,7 +408,7 @@ func (k *Keeper) ChanCloseConfirm( // If the channel is closing during an upgrade, then we can delete all upgrade information. if k.hasUpgrade(ctx, portID, channelID) { k.deleteUpgradeInfo(ctx, portID, channelID) - k.Logger(ctx).Info( + k.Logger.Info( "upgrade info deleted", "port_id", portID, "channel_id", channelID, @@ -418,14 +416,12 @@ func (k *Keeper) ChanCloseConfirm( ) } - k.Logger(ctx).Info("channel state updated", "port-id", portID, "channel-id", channelID, "previous-state", channel.State, "new-state", types.CLOSED) + k.Logger.Info("channel state updated", "port-id", portID, "channel-id", channelID, "previous-state", channel.State, "new-state", types.CLOSED) defer telemetry.IncrCounter(1, "ibc", "channel", "close-confirm") channel.State = types.CLOSED k.SetChannel(ctx, portID, channelID, channel) - emitChannelCloseConfirmEvent(ctx, portID, channelID, channel) - - return nil + return k.emitChannelCloseConfirmEvent(ctx, portID, channelID, channel) } diff --git a/modules/core/04-channel/keeper/keeper.go b/modules/core/04-channel/keeper/keeper.go index 35ee0451afa..f0889bd0d2b 100644 --- a/modules/core/04-channel/keeper/keeper.go +++ b/modules/core/04-channel/keeper/keeper.go @@ -8,9 +8,8 @@ import ( db "github.com/cosmos/cosmos-db" - 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" @@ -30,10 +29,10 @@ var _ porttypes.ICS4Wrapper = (*Keeper)(nil) // Keeper defines the IBC channel keeper type Keeper struct { + appmodule.Environment // implements gRPC QueryServer interface types.QueryServer - storeService corestore.KVStoreService cdc codec.BinaryCodec clientKeeper types.ClientKeeper connectionKeeper types.ConnectionKeeper @@ -42,24 +41,18 @@ type Keeper struct { // NewKeeper creates a new IBC channel Keeper instance func NewKeeper( cdc codec.BinaryCodec, - storeService corestore.KVStoreService, + env appmodule.Environment, clientKeeper types.ClientKeeper, connectionKeeper types.ConnectionKeeper, ) *Keeper { return &Keeper{ - storeService: storeService, + Environment: env, cdc: cdc, clientKeeper: clientKeeper, connectionKeeper: connectionKeeper, } } -// Logger returns a module-specific logger. -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", "x/"+exported.ModuleName+"/"+types.SubModuleName) -} - // GenerateChannelIdentifier returns the next channel identifier. func (k *Keeper) GenerateChannelIdentifier(ctx context.Context) string { nextChannelSeq := k.GetNextChannelSequence(ctx) @@ -72,7 +65,7 @@ func (k *Keeper) GenerateChannelIdentifier(ctx context.Context) string { // HasChannel true if the channel with the given identifiers exists in state. func (k *Keeper) HasChannel(ctx context.Context, portID, channelID string) bool { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) has, err := store.Has(host.ChannelKey(portID, channelID)) if err != nil { panic(err) @@ -82,7 +75,7 @@ func (k *Keeper) HasChannel(ctx context.Context, portID, channelID string) bool // GetChannel returns a channel with a particular identifier binded to a specific port func (k *Keeper) GetChannel(ctx context.Context, portID, channelID string) (types.Channel, bool) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) bz, err := store.Get(host.ChannelKey(portID, channelID)) if err != nil { panic(err) @@ -98,7 +91,7 @@ func (k *Keeper) GetChannel(ctx context.Context, portID, channelID string) (type // SetChannel sets a channel to the store func (k *Keeper) SetChannel(ctx context.Context, portID, channelID string, channel types.Channel) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) bz := k.cdc.MustMarshal(&channel) if err := store.Set(host.ChannelKey(portID, channelID), bz); err != nil { panic(err) @@ -117,7 +110,7 @@ func (k *Keeper) GetAppVersion(ctx context.Context, portID, channelID string) (s // GetNextChannelSequence gets the next channel sequence from the store. func (k *Keeper) GetNextChannelSequence(ctx context.Context) uint64 { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) bz, err := store.Get([]byte(types.KeyNextChannelSequence)) if err != nil { panic(err) @@ -131,7 +124,7 @@ func (k *Keeper) GetNextChannelSequence(ctx context.Context) uint64 { // SetNextChannelSequence sets the next channel sequence to the store. func (k *Keeper) SetNextChannelSequence(ctx context.Context, sequence uint64) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) bz := sdk.Uint64ToBigEndian(sequence) if err := store.Set([]byte(types.KeyNextChannelSequence), bz); err != nil { panic(err) @@ -140,7 +133,7 @@ func (k *Keeper) SetNextChannelSequence(ctx context.Context, sequence uint64) { // GetNextSequenceSend gets a channel's next send sequence from the store func (k *Keeper) GetNextSequenceSend(ctx context.Context, portID, channelID string) (uint64, bool) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) bz, err := store.Get(host.NextSequenceSendKey(portID, channelID)) if err != nil { panic(err) @@ -154,7 +147,7 @@ func (k *Keeper) GetNextSequenceSend(ctx context.Context, portID, channelID stri // SetNextSequenceSend sets a channel's next send sequence to the store func (k *Keeper) SetNextSequenceSend(ctx context.Context, portID, channelID string, sequence uint64) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) bz := sdk.Uint64ToBigEndian(sequence) if err := store.Set(host.NextSequenceSendKey(portID, channelID), bz); err != nil { panic(err) @@ -163,7 +156,7 @@ func (k *Keeper) SetNextSequenceSend(ctx context.Context, portID, channelID stri // GetNextSequenceRecv gets a channel's next receive sequence from the store func (k *Keeper) GetNextSequenceRecv(ctx context.Context, portID, channelID string) (uint64, bool) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) bz, err := store.Get(host.NextSequenceRecvKey(portID, channelID)) if err != nil { panic(err) @@ -177,7 +170,7 @@ func (k *Keeper) GetNextSequenceRecv(ctx context.Context, portID, channelID stri // SetNextSequenceRecv sets a channel's next receive sequence to the store func (k *Keeper) SetNextSequenceRecv(ctx context.Context, portID, channelID string, sequence uint64) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) bz := sdk.Uint64ToBigEndian(sequence) if err := store.Set(host.NextSequenceRecvKey(portID, channelID), bz); err != nil { panic(err) @@ -186,7 +179,7 @@ func (k *Keeper) SetNextSequenceRecv(ctx context.Context, portID, channelID stri // GetNextSequenceAck gets a channel's next ack sequence from the store func (k *Keeper) GetNextSequenceAck(ctx context.Context, portID, channelID string) (uint64, bool) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) bz, err := store.Get(host.NextSequenceAckKey(portID, channelID)) if err != nil { panic(err) @@ -201,7 +194,7 @@ func (k *Keeper) GetNextSequenceAck(ctx context.Context, portID, channelID strin // SetNextSequenceAck sets a channel's next ack sequence to the store func (k *Keeper) SetNextSequenceAck(ctx context.Context, portID, channelID string, sequence uint64) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) bz := sdk.Uint64ToBigEndian(sequence) if err := store.Set(host.NextSequenceAckKey(portID, channelID), bz); err != nil { panic(err) @@ -210,7 +203,7 @@ func (k *Keeper) SetNextSequenceAck(ctx context.Context, portID, channelID strin // GetPacketReceipt gets a packet receipt from the store func (k *Keeper) GetPacketReceipt(ctx context.Context, portID, channelID string, sequence uint64) (string, bool) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) bz, err := store.Get(host.PacketReceiptKey(portID, channelID, sequence)) if err != nil { panic(err) @@ -225,7 +218,7 @@ func (k *Keeper) GetPacketReceipt(ctx context.Context, portID, channelID string, // SetPacketReceipt sets an empty packet receipt to the store func (k *Keeper) SetPacketReceipt(ctx context.Context, portID, channelID string, sequence uint64) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) if err := store.Set(host.PacketReceiptKey(portID, channelID, sequence), []byte{byte(1)}); err != nil { panic(err) } @@ -233,7 +226,7 @@ func (k *Keeper) SetPacketReceipt(ctx context.Context, portID, channelID string, // deletePacketReceipt deletes a packet receipt from the store func (k *Keeper) deletePacketReceipt(ctx context.Context, portID, channelID string, sequence uint64) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) if err := store.Delete(host.PacketReceiptKey(portID, channelID, sequence)); err != nil { panic(err) } @@ -241,7 +234,7 @@ func (k *Keeper) deletePacketReceipt(ctx context.Context, portID, channelID stri // GetPacketCommitment gets the packet commitment hash from the store func (k *Keeper) GetPacketCommitment(ctx context.Context, portID, channelID string, sequence uint64) []byte { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) bz, err := store.Get(host.PacketCommitmentKey(portID, channelID, sequence)) if err != nil { panic(err) @@ -252,7 +245,7 @@ func (k *Keeper) GetPacketCommitment(ctx context.Context, portID, channelID stri // HasPacketCommitment returns true if the packet commitment exists func (k *Keeper) HasPacketCommitment(ctx context.Context, portID, channelID string, sequence uint64) bool { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) has, err := store.Has(host.PacketCommitmentKey(portID, channelID, sequence)) if err != nil { panic(err) @@ -262,14 +255,14 @@ func (k *Keeper) HasPacketCommitment(ctx context.Context, portID, channelID stri // SetPacketCommitment sets the packet commitment hash to the store func (k *Keeper) SetPacketCommitment(ctx context.Context, portID, channelID string, sequence uint64, commitmentHash []byte) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) if err := store.Set(host.PacketCommitmentKey(portID, channelID, sequence), commitmentHash); err != nil { panic(err) } } func (k *Keeper) deletePacketCommitment(ctx context.Context, portID, channelID string, sequence uint64) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) if err := store.Delete(host.PacketCommitmentKey(portID, channelID, sequence)); err != nil { panic(err) } @@ -277,7 +270,7 @@ func (k *Keeper) deletePacketCommitment(ctx context.Context, portID, channelID s // SetPacketAcknowledgement sets the packet ack hash to the store func (k *Keeper) SetPacketAcknowledgement(ctx context.Context, portID, channelID string, sequence uint64, ackHash []byte) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) if err := store.Set(host.PacketAcknowledgementKey(portID, channelID, sequence), ackHash); err != nil { panic(err) } @@ -285,7 +278,7 @@ func (k *Keeper) SetPacketAcknowledgement(ctx context.Context, portID, channelID // GetPacketAcknowledgement gets the packet ack hash from the store func (k *Keeper) GetPacketAcknowledgement(ctx context.Context, portID, channelID string, sequence uint64) ([]byte, bool) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) bz, err := store.Get(host.PacketAcknowledgementKey(portID, channelID, sequence)) if err != nil { panic(err) @@ -300,7 +293,7 @@ func (k *Keeper) GetPacketAcknowledgement(ctx context.Context, portID, channelID // HasPacketAcknowledgement check if the packet ack hash is already on the store func (k *Keeper) HasPacketAcknowledgement(ctx context.Context, portID, channelID string, sequence uint64) bool { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) has, err := store.Has(host.PacketAcknowledgementKey(portID, channelID, sequence)) if err != nil { panic(err) @@ -310,7 +303,7 @@ func (k *Keeper) HasPacketAcknowledgement(ctx context.Context, portID, channelID // deletePacketAcknowledgement deletes the packet ack hash from the store func (k *Keeper) deletePacketAcknowledgement(ctx context.Context, portID, channelID string, sequence uint64) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) if err := store.Delete(host.PacketAcknowledgementKey(portID, channelID, sequence)); err != nil { panic(err) } @@ -320,7 +313,7 @@ func (k *Keeper) deletePacketAcknowledgement(ctx context.Context, portID, channe // For each sequence, cb will be called. If the cb returns true, the iterator // will close and stop. func (k *Keeper) IteratePacketSequence(ctx context.Context, iterator db.Iterator, cb func(portID, channelID string, sequence uint64) bool) { - defer coretypes.LogDeferred(k.Logger(ctx), func() error { return iterator.Close() }) + defer coretypes.LogDeferred(k.Logger, func() error { return iterator.Close() }) for ; iterator.Valid(); iterator.Next() { portID, channelID, err := host.ParseChannelPath(string(iterator.Key())) if err != nil { @@ -338,7 +331,7 @@ func (k *Keeper) IteratePacketSequence(ctx context.Context, iterator db.Iterator // GetAllPacketSendSeqs returns all stored next send sequences. func (k *Keeper) GetAllPacketSendSeqs(ctx context.Context) (seqs []types.PacketSequence) { - store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) + store := runtime.KVStoreAdapter(k.KVStoreService.OpenKVStore(ctx)) iterator := storetypes.KVStorePrefixIterator(store, []byte(host.KeyNextSeqSendPrefix)) k.IteratePacketSequence(ctx, iterator, func(portID, channelID string, nextSendSeq uint64) bool { ps := types.NewPacketSequence(portID, channelID, nextSendSeq) @@ -350,7 +343,7 @@ func (k *Keeper) GetAllPacketSendSeqs(ctx context.Context) (seqs []types.PacketS // GetAllPacketRecvSeqs returns all stored next recv sequences. func (k *Keeper) GetAllPacketRecvSeqs(ctx context.Context) (seqs []types.PacketSequence) { - store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) + store := runtime.KVStoreAdapter(k.KVStoreService.OpenKVStore(ctx)) iterator := storetypes.KVStorePrefixIterator(store, []byte(host.KeyNextSeqRecvPrefix)) k.IteratePacketSequence(ctx, iterator, func(portID, channelID string, nextRecvSeq uint64) bool { ps := types.NewPacketSequence(portID, channelID, nextRecvSeq) @@ -362,7 +355,7 @@ func (k *Keeper) GetAllPacketRecvSeqs(ctx context.Context) (seqs []types.PacketS // GetAllPacketAckSeqs returns all stored next acknowledgements sequences. func (k *Keeper) GetAllPacketAckSeqs(ctx context.Context) (seqs []types.PacketSequence) { - store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) + store := runtime.KVStoreAdapter(k.KVStoreService.OpenKVStore(ctx)) iterator := storetypes.KVStorePrefixIterator(store, []byte(host.KeyNextSeqAckPrefix)) k.IteratePacketSequence(ctx, iterator, func(portID, channelID string, nextAckSeq uint64) bool { ps := types.NewPacketSequence(portID, channelID, nextAckSeq) @@ -376,7 +369,7 @@ func (k *Keeper) GetAllPacketAckSeqs(ctx context.Context) (seqs []types.PacketSe // packet commitment, cb will be called. If the cb returns true, the iterator will close // and stop. func (k *Keeper) IteratePacketCommitment(ctx context.Context, cb func(portID, channelID string, sequence uint64, hash []byte) bool) { - store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) + store := runtime.KVStoreAdapter(k.KVStoreService.OpenKVStore(ctx)) iterator := storetypes.KVStorePrefixIterator(store, []byte(host.KeyPacketCommitmentPrefix)) k.iterateHashes(ctx, iterator, cb) } @@ -395,7 +388,7 @@ func (k *Keeper) GetAllPacketCommitments(ctx context.Context) (commitments []typ // at a specified channel. For each packet commitment, cb will be called. If the cb returns // true, the iterator will close and stop. func (k *Keeper) IteratePacketCommitmentAtChannel(ctx context.Context, portID, channelID string, cb func(_, _ string, sequence uint64, hash []byte) bool) { - store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) + store := runtime.KVStoreAdapter(k.KVStoreService.OpenKVStore(ctx)) iterator := storetypes.KVStorePrefixIterator(store, host.PacketCommitmentPrefixKey(portID, channelID)) k.iterateHashes(ctx, iterator, cb) } @@ -415,7 +408,7 @@ func (k *Keeper) GetAllPacketCommitmentsAtChannel(ctx context.Context, portID, c // receipt, cb will be called. If the cb returns true, the iterator will close // and stop. func (k *Keeper) IteratePacketReceipt(ctx context.Context, cb func(portID, channelID string, sequence uint64, receipt []byte) bool) { - store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) + store := runtime.KVStoreAdapter(k.KVStoreService.OpenKVStore(ctx)) iterator := storetypes.KVStorePrefixIterator(store, []byte(host.KeyPacketReceiptPrefix)) k.iterateHashes(ctx, iterator, cb) } @@ -434,7 +427,7 @@ func (k *Keeper) GetAllPacketReceipts(ctx context.Context) (receipts []types.Pac // acknowledgement, cb will be called. If the cb returns true, the iterator will close // and stop. func (k *Keeper) IteratePacketAcknowledgement(ctx context.Context, cb func(portID, channelID string, sequence uint64, hash []byte) bool) { - store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) + store := runtime.KVStoreAdapter(k.KVStoreService.OpenKVStore(ctx)) iterator := storetypes.KVStorePrefixIterator(store, []byte(host.KeyPacketAckPrefix)) k.iterateHashes(ctx, iterator, cb) } @@ -453,10 +446,10 @@ func (k *Keeper) GetAllPacketAcks(ctx context.Context) (acks []types.PacketState // Channel, cb will be called. If the cb returns true, the iterator will close // and stop. func (k *Keeper) IterateChannels(ctx context.Context, cb func(types.IdentifiedChannel) bool) { - store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) + store := runtime.KVStoreAdapter(k.KVStoreService.OpenKVStore(ctx)) iterator := storetypes.KVStorePrefixIterator(store, []byte(host.KeyChannelEndPrefix)) - defer coretypes.LogDeferred(k.Logger(ctx), func() error { return iterator.Close() }) + defer coretypes.LogDeferred(k.Logger, func() error { return iterator.Close() }) for ; iterator.Valid(); iterator.Next() { var channel types.Channel k.cdc.MustUnmarshal(iterator.Value(), &channel) @@ -475,9 +468,9 @@ func (k *Keeper) GetAllChannelsWithPortPrefix(ctx context.Context, portPrefix st if strings.TrimSpace(portPrefix) == "" { return k.GetAllChannels(ctx) } - store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) + store := runtime.KVStoreAdapter(k.KVStoreService.OpenKVStore(ctx)) iterator := storetypes.KVStorePrefixIterator(store, types.FilteredPortPrefix(portPrefix)) - defer coretypes.LogDeferred(k.Logger(ctx), func() error { return iterator.Close() }) + defer coretypes.LogDeferred(k.Logger, func() error { return iterator.Close() }) var filteredChannels []types.IdentifiedChannel for ; iterator.Valid(); iterator.Next() { @@ -549,7 +542,7 @@ func (k *Keeper) GetChannelConnection(ctx context.Context, portID, channelID str // GetUpgradeErrorReceipt returns the upgrade error receipt for the provided port and channel identifiers. func (k *Keeper) GetUpgradeErrorReceipt(ctx context.Context, portID, channelID string) (types.ErrorReceipt, bool) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) bz, err := store.Get(host.ChannelUpgradeErrorKey(portID, channelID)) if err != nil { panic(err) @@ -567,7 +560,7 @@ func (k *Keeper) GetUpgradeErrorReceipt(ctx context.Context, portID, channelID s // setUpgradeErrorReceipt sets the provided error receipt in store using the port and channel identifiers. func (k *Keeper) setUpgradeErrorReceipt(ctx context.Context, portID, channelID string, errorReceipt types.ErrorReceipt) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) bz := k.cdc.MustMarshal(&errorReceipt) if err := store.Set(host.ChannelUpgradeErrorKey(portID, channelID), bz); err != nil { panic(err) @@ -576,7 +569,7 @@ func (k *Keeper) setUpgradeErrorReceipt(ctx context.Context, portID, channelID s // hasUpgrade returns true if a proposed upgrade exists in store func (k *Keeper) hasUpgrade(ctx context.Context, portID, channelID string) bool { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) has, err := store.Has(host.ChannelUpgradeKey(portID, channelID)) if err != nil { panic(err) @@ -586,7 +579,7 @@ func (k *Keeper) hasUpgrade(ctx context.Context, portID, channelID string) bool // GetUpgrade returns the proposed upgrade for the provided port and channel identifiers. func (k *Keeper) GetUpgrade(ctx context.Context, portID, channelID string) (types.Upgrade, bool) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) bz, err := store.Get(host.ChannelUpgradeKey(portID, channelID)) if err != nil { panic(err) @@ -604,7 +597,7 @@ func (k *Keeper) GetUpgrade(ctx context.Context, portID, channelID string) (type // SetUpgrade sets the proposed upgrade using the provided port and channel identifiers. func (k *Keeper) SetUpgrade(ctx context.Context, portID, channelID string, upgrade types.Upgrade) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) bz := k.cdc.MustMarshal(&upgrade) if err := store.Set(host.ChannelUpgradeKey(portID, channelID), bz); err != nil { panic(err) @@ -613,7 +606,7 @@ func (k *Keeper) SetUpgrade(ctx context.Context, portID, channelID string, upgra // deleteUpgrade deletes the upgrade for the provided port and channel identifiers. func (k *Keeper) deleteUpgrade(ctx context.Context, portID, channelID string) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) if err := store.Delete(host.ChannelUpgradeKey(portID, channelID)); err != nil { panic(err) } @@ -621,7 +614,7 @@ func (k *Keeper) deleteUpgrade(ctx context.Context, portID, channelID string) { // hasCounterpartyUpgrade returns true if a counterparty upgrade exists in store func (k *Keeper) hasCounterpartyUpgrade(ctx context.Context, portID, channelID string) bool { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) has, err := store.Has(host.ChannelCounterpartyUpgradeKey(portID, channelID)) if err != nil { panic(err) @@ -631,7 +624,7 @@ func (k *Keeper) hasCounterpartyUpgrade(ctx context.Context, portID, channelID s // GetCounterpartyUpgrade gets the counterparty upgrade from the store. func (k *Keeper) GetCounterpartyUpgrade(ctx context.Context, portID, channelID string) (types.Upgrade, bool) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) bz, err := store.Get(host.ChannelCounterpartyUpgradeKey(portID, channelID)) if err != nil { panic(err) @@ -649,7 +642,7 @@ func (k *Keeper) GetCounterpartyUpgrade(ctx context.Context, portID, channelID s // SetCounterpartyUpgrade sets the counterparty upgrade in the store. func (k *Keeper) SetCounterpartyUpgrade(ctx context.Context, portID, channelID string, upgrade types.Upgrade) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) bz := k.cdc.MustMarshal(&upgrade) if err := store.Set(host.ChannelCounterpartyUpgradeKey(portID, channelID), bz); err != nil { panic(err) @@ -658,7 +651,7 @@ func (k *Keeper) SetCounterpartyUpgrade(ctx context.Context, portID, channelID s // deleteCounterpartyUpgrade deletes the counterparty upgrade in the store. func (k *Keeper) deleteCounterpartyUpgrade(ctx context.Context, portID, channelID string) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) if err := store.Delete(host.ChannelCounterpartyUpgradeKey(portID, channelID)); err != nil { panic(err) } @@ -672,7 +665,7 @@ func (k *Keeper) deleteUpgradeInfo(ctx context.Context, portID, channelID string // SetParams sets the channel 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(¶ms) if err := store.Set([]byte(types.ParamsKey), bz); err != nil { panic(err) @@ -681,7 +674,7 @@ func (k *Keeper) SetParams(ctx context.Context, params types.Params) { // GetParams returns the total set of the channel 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) @@ -698,7 +691,7 @@ func (k *Keeper) GetParams(ctx context.Context) types.Params { // common functionality for IteratePacketCommitment and IteratePacketAcknowledgement func (k *Keeper) iterateHashes(ctx context.Context, iterator db.Iterator, cb func(portID, channelID string, sequence uint64, hash []byte) bool) { - defer coretypes.LogDeferred(k.Logger(ctx), func() error { return iterator.Close() }) + defer coretypes.LogDeferred(k.Logger, func() error { return iterator.Close() }) for ; iterator.Valid(); iterator.Next() { keySplit := strings.Split(string(iterator.Key()), "/") @@ -719,16 +712,16 @@ func (k *Keeper) iterateHashes(ctx context.Context, iterator db.Iterator, cb fun // HasInflightPackets returns true if there are packet commitments stored at the specified // port and channel, and false otherwise. func (k *Keeper) HasInflightPackets(ctx context.Context, portID, channelID string) bool { - store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) + store := runtime.KVStoreAdapter(k.KVStoreService.OpenKVStore(ctx)) iterator := storetypes.KVStorePrefixIterator(store, host.PacketCommitmentPrefixKey(portID, channelID)) - defer coretypes.LogDeferred(k.Logger(ctx), func() error { return iterator.Close() }) + defer coretypes.LogDeferred(k.Logger, func() error { return iterator.Close() }) return iterator.Valid() } // setRecvStartSequence sets the channel's recv start sequence to the store. func (k *Keeper) setRecvStartSequence(ctx context.Context, portID, channelID string, sequence uint64) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) bz := sdk.Uint64ToBigEndian(sequence) if err := store.Set(host.RecvStartSequenceKey(portID, channelID), bz); err != nil { panic(err) @@ -740,7 +733,7 @@ func (k *Keeper) setRecvStartSequence(ctx context.Context, portID, channelID str // upon a successful channel upgrade. It will be used for replay protection of // historical packets and as the upper bound for pruning stale packet receives. func (k *Keeper) GetRecvStartSequence(ctx context.Context, portID, channelID string) (uint64, bool) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) bz, err := store.Get(host.RecvStartSequenceKey(portID, channelID)) if err != nil { panic(err) @@ -755,7 +748,7 @@ func (k *Keeper) GetRecvStartSequence(ctx context.Context, portID, channelID str // SetPruningSequenceStart sets a channel's pruning sequence start to the store. func (k *Keeper) SetPruningSequenceStart(ctx context.Context, portID, channelID string, sequence uint64) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) bz := sdk.Uint64ToBigEndian(sequence) if err := store.Set(host.PruningSequenceStartKey(portID, channelID), bz); err != nil { panic(err) @@ -764,7 +757,7 @@ func (k *Keeper) SetPruningSequenceStart(ctx context.Context, portID, channelID // GetPruningSequenceStart gets a channel's pruning sequence start from the store. func (k *Keeper) GetPruningSequenceStart(ctx context.Context, portID, channelID string) (uint64, bool) { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) bz, err := store.Get(host.PruningSequenceStartKey(portID, channelID)) if err != nil { panic(err) @@ -779,7 +772,7 @@ func (k *Keeper) GetPruningSequenceStart(ctx context.Context, portID, channelID // HasPruningSequenceStart returns true if the pruning sequence start is set for the specified channel. func (k *Keeper) HasPruningSequenceStart(ctx context.Context, portID, channelID string) bool { - store := k.storeService.OpenKVStore(ctx) + store := k.KVStoreService.OpenKVStore(ctx) has, err := store.Has(host.PruningSequenceStartKey(portID, channelID)) if err != nil { panic(err) diff --git a/modules/core/04-channel/keeper/migrations.go b/modules/core/04-channel/keeper/migrations.go index 9a382d26265..c83cf865022 100644 --- a/modules/core/04-channel/keeper/migrations.go +++ b/modules/core/04-channel/keeper/migrations.go @@ -20,6 +20,6 @@ func NewMigrator(keeper *Keeper) Migrator { func (m Migrator) MigrateParams(ctx sdk.Context) error { params := channeltypes.DefaultParams() m.keeper.SetParams(ctx, params) - m.keeper.Logger(ctx).Info("successfully migrated ibc channel params") + m.keeper.Logger.Info("successfully migrated ibc channel params") return nil } diff --git a/modules/core/04-channel/keeper/packet.go b/modules/core/04-channel/keeper/packet.go index 65410bcd11b..262ba79e44f 100644 --- a/modules/core/04-channel/keeper/packet.go +++ b/modules/core/04-channel/keeper/packet.go @@ -36,8 +36,6 @@ func (k *Keeper) SendPacket( return 0, errorsmod.Wrapf(types.ErrInvalidChannelState, "channel is not OPEN (got %s)", channel.State) } - sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/5917 - sequence, found := k.GetNextSequenceSend(ctx, sourcePort, sourceChannel) if !found { return 0, errorsmod.Wrapf( @@ -85,9 +83,11 @@ func (k *Keeper) SendPacket( k.SetNextSequenceSend(ctx, sourcePort, sourceChannel, sequence+1) k.SetPacketCommitment(ctx, sourcePort, sourceChannel, packet.GetSequence(), commitment) - emitSendPacketEvent(sdkCtx, packet, channel, timeoutHeight) + if err := k.emitSendPacketEvent(ctx, packet, channel, timeoutHeight); err != nil { + return 0, err + } - k.Logger(ctx).Info( + k.Logger.Info( "packet sent", "sequence", strconv.FormatUint(packet.GetSequence(), 10), "src_port", sourcePort, @@ -180,7 +180,7 @@ func (k *Keeper) RecvPacket( } // log that a packet has been received & executed - k.Logger(ctx).Info( + k.Logger.Info( "packet received", "sequence", strconv.FormatUint(packet.GetSequence(), 10), "src_port", packet.GetSourcePort(), @@ -190,7 +190,9 @@ func (k *Keeper) RecvPacket( ) // emit an event that the relayer can query for - emitRecvPacketEvent(sdkCtx, packet, channel) + if err := k.emitRecvPacketEvent(ctx, packet, channel); err != nil { + return "", err + } return channel.Version, nil } @@ -207,7 +209,6 @@ func (k *Keeper) applyReplayProtection(ctx context.Context, packet types.Packet, return errorsmod.Wrap(types.ErrPacketReceived, "packet already processed in previous channel upgrade") } - sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/7223 switch channel.Ordering { case types.UNORDERED: // REPLAY PROTECTION: Packet receipts will indicate that a packet has already been received @@ -215,7 +216,9 @@ func (k *Keeper) applyReplayProtection(ctx context.Context, packet types.Packet, // by the increase of the recvStartSequence. _, found := k.GetPacketReceipt(ctx, packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence()) if found { - emitRecvPacketEvent(sdkCtx, packet, channel) + if err := k.emitRecvPacketEvent(ctx, packet, channel); err != nil { + return err + } // 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. @@ -239,7 +242,9 @@ func (k *Keeper) applyReplayProtection(ctx context.Context, packet types.Packet, } if packet.GetSequence() < nextSequenceRecv { - emitRecvPacketEvent(sdkCtx, packet, channel) + if err := k.emitRecvPacketEvent(ctx, packet, channel); err != nil { + return err + } // 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. @@ -325,7 +330,7 @@ func (k *Keeper) WriteAcknowledgement( ) // log that a packet acknowledgement has been written - k.Logger(ctx).Info( + k.Logger.Info( "acknowledgement written", "sequence", strconv.FormatUint(packet.GetSequence(), 10), "src_port", packet.GetSourcePort(), @@ -334,10 +339,7 @@ func (k *Keeper) WriteAcknowledgement( "dst_channel", packet.GetDestChannel(), ) - sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/7223 - emitWriteAcknowledgementEvent(sdkCtx, packet.(types.Packet), channel, bz) - - return nil + return k.emitWriteAcknowledgementEvent(ctx, packet.(types.Packet), channel, bz) } // AcknowledgePacket is called by a module to process the acknowledgement of a @@ -392,7 +394,9 @@ func (k *Keeper) AcknowledgePacket( commitment := k.GetPacketCommitment(ctx, packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) if len(commitment) == 0 { - emitAcknowledgePacketEvent(ctx, packet, channel) + if err := k.emitAcknowledgePacketEvent(ctx, packet, channel); err != nil { + return "", err + } // This error indicates that the acknowledgement has already been relayed // or there is a misconfigured relayer attempting to prove an acknowledgement // for a packet never sent. Core IBC will treat this error as a no-op in order to @@ -444,7 +448,7 @@ func (k *Keeper) AcknowledgePacket( k.deletePacketCommitment(ctx, packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) // log that a packet has been acknowledged - k.Logger(ctx).Info( + k.Logger.Info( "packet acknowledged", "sequence", strconv.FormatUint(packet.GetSequence(), 10), "src_port", packet.GetSourcePort(), @@ -454,7 +458,9 @@ func (k *Keeper) AcknowledgePacket( ) // emit an event marking that we have processed the acknowledgement - emitAcknowledgePacketEvent(ctx, packet, channel) + if err := k.emitAcknowledgePacketEvent(ctx, packet, channel); err != nil { + return "", err + } // if an upgrade is in progress, handling packet flushing and update channel state appropriately if channel.State == types.FLUSHING { @@ -477,13 +483,15 @@ func (k *Keeper) handleFlushState(ctx context.Context, packet types.Packet, chan if timeout.Elapsed(selfHeight, selfTimestamp) { // packet flushing timeout has expired, abort the upgrade // committing an error receipt to state, deleting upgrade information and restoring the channel. - k.Logger(ctx).Info("upgrade aborted", "port_id", packet.GetSourcePort(), "channel_id", packet.GetSourceChannel(), "upgrade_sequence", channel.UpgradeSequence) + k.Logger.Info("upgrade aborted", "port_id", packet.GetSourcePort(), "channel_id", packet.GetSourceChannel(), "upgrade_sequence", channel.UpgradeSequence) k.MustAbortUpgrade(ctx, packet.GetSourcePort(), packet.GetSourceChannel(), timeout.ErrTimeoutElapsed(selfHeight, selfTimestamp)) } else if !k.HasInflightPackets(ctx, packet.GetSourcePort(), packet.GetSourceChannel()) { // set the channel state to flush complete if all packets have been acknowledged/flushed. channel.State = types.FLUSHCOMPLETE k.SetChannel(ctx, packet.GetSourcePort(), packet.GetSourceChannel(), channel) - emitChannelFlushCompleteEvent(ctx, packet.GetSourcePort(), packet.GetSourceChannel(), channel) + if err := k.emitChannelFlushCompleteEvent(ctx, packet.GetSourcePort(), packet.GetSourceChannel(), channel); err != nil { + panic(err) + } } } } diff --git a/modules/core/04-channel/keeper/timeout.go b/modules/core/04-channel/keeper/timeout.go index 5b548605700..04dc96a9daf 100644 --- a/modules/core/04-channel/keeper/timeout.go +++ b/modules/core/04-channel/keeper/timeout.go @@ -70,7 +70,9 @@ func (k *Keeper) TimeoutPacket( commitment := k.GetPacketCommitment(ctx, packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) if len(commitment) == 0 { - emitTimeoutPacketEvent(ctx, packet, channel) + if err := k.emitTimeoutPacketEvent(ctx, packet, channel); err != nil { + return "", err + } // This error indicates that the timeout has already been relayed // or there is a misconfigured relayer attempting to prove a timeout // for a packet never sent. Core IBC will treat this error as a no-op in order to @@ -145,7 +147,7 @@ func (k *Keeper) TimeoutExecuted( // all upgrade information is deleted and the channel is set to CLOSED. if channel.State == types.FLUSHING { k.deleteUpgradeInfo(ctx, packet.GetSourcePort(), packet.GetSourceChannel()) - k.Logger(ctx).Info( + k.Logger.Info( "upgrade info deleted", "port_id", packet.GetSourcePort(), "channel_id", packet.GetSourceChannel(), @@ -155,10 +157,12 @@ func (k *Keeper) TimeoutExecuted( channel.State = types.CLOSED k.SetChannel(ctx, packet.GetSourcePort(), packet.GetSourceChannel(), channel) - emitChannelClosedEvent(ctx, packet, channel) + if err := k.emitChannelClosedEvent(ctx, packet, channel); err != nil { + return err + } } - k.Logger(ctx).Info( + k.Logger.Info( "packet timed-out", "sequence", strconv.FormatUint(packet.GetSequence(), 10), "src_port", packet.GetSourcePort(), @@ -168,9 +172,7 @@ func (k *Keeper) TimeoutExecuted( ) // emit an event marking that we have processed the timeout - emitTimeoutPacketEvent(ctx, packet, channel) - - return nil + return k.emitTimeoutPacketEvent(ctx, packet, channel) } // TimeoutOnClose is called by a module in order to prove that the channel to @@ -212,7 +214,9 @@ func (k *Keeper) TimeoutOnClose( commitment := k.GetPacketCommitment(ctx, packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) if len(commitment) == 0 { - emitTimeoutPacketEvent(ctx, packet, channel) + if err := k.emitTimeoutPacketEvent(ctx, packet, channel); err != nil { + return "", err + } // This error indicates that the timeout has already been relayed // or there is a misconfigured relayer attempting to prove a timeout // for a packet never sent. Core IBC will treat this error as a no-op in order to diff --git a/modules/core/04-channel/keeper/upgrade.go b/modules/core/04-channel/keeper/upgrade.go index cee41cbf4f0..a842b92865e 100644 --- a/modules/core/04-channel/keeper/upgrade.go +++ b/modules/core/04-channel/keeper/upgrade.go @@ -67,7 +67,7 @@ func (k *Keeper) WriteUpgradeInitChannel(ctx context.Context, portID, channelID k.SetChannel(ctx, portID, channelID, channel) k.SetUpgrade(ctx, portID, channelID, upgrade) - k.Logger(ctx).Info("channel state updated", "port-id", portID, "channel-id", channelID, "state", channel.State, "upgrade-sequence", fmt.Sprintf("%d", channel.UpgradeSequence)) + k.Logger.Info("channel state updated", "port-id", portID, "channel-id", channelID, "state", channel.State, "upgrade-sequence", fmt.Sprintf("%d", channel.UpgradeSequence)) return channel, upgrade } @@ -235,7 +235,7 @@ func (k *Keeper) WriteUpgradeTryChannel(ctx context.Context, portID, channelID s upgrade.Fields.Version = upgradeVersion k.SetUpgrade(ctx, portID, channelID, upgrade) - k.Logger(ctx).Info("channel state updated", "port-id", portID, "channel-id", channelID, "previous-state", types.OPEN, "new-state", channel.State) + k.Logger.Info("channel state updated", "port-id", portID, "channel-id", channelID, "previous-state", types.OPEN, "new-state", channel.State) return channel, upgrade } @@ -366,7 +366,7 @@ func (k *Keeper) WriteUpgradeAckChannel(ctx context.Context, portID, channelID s previousState := channel.State channel.State = types.FLUSHCOMPLETE k.SetChannel(ctx, portID, channelID, channel) - k.Logger(ctx).Info("channel state updated", "port-id", portID, "channel-id", channelID, "previous-state", previousState, "new-state", channel.State) + k.Logger.Info("channel state updated", "port-id", portID, "channel-id", channelID, "previous-state", previousState, "new-state", channel.State) } upgrade, found := k.GetUpgrade(ctx, portID, channelID) @@ -490,7 +490,7 @@ func (k *Keeper) WriteUpgradeConfirmChannel(ctx context.Context, portID, channel previousState := channel.State channel.State = types.FLUSHCOMPLETE k.SetChannel(ctx, portID, channelID, channel) - k.Logger(ctx).Info("channel state updated", "port-id", portID, "channel-id", channelID, "previous-state", previousState, "new-state", channel.State) + k.Logger.Info("channel state updated", "port-id", portID, "channel-id", channelID, "previous-state", previousState, "new-state", channel.State) } k.SetCounterpartyUpgrade(ctx, portID, channelID, counterpartyUpgrade) @@ -647,7 +647,7 @@ func (k *Keeper) WriteUpgradeOpenChannel(ctx context.Context, portID, channelID // delete state associated with upgrade which is no longer required. k.deleteUpgradeInfo(ctx, portID, channelID) - k.Logger(ctx).Info("channel state updated", "port-id", portID, "channel-id", channelID, "previous-state", previousState, "new-state", types.OPEN) + k.Logger.Info("channel state updated", "port-id", portID, "channel-id", channelID, "previous-state", previousState, "new-state", types.OPEN) return channel } @@ -725,7 +725,7 @@ func (k *Keeper) WriteUpgradeCancelChannel(ctx context.Context, portID, channelI channel = k.restoreChannel(ctx, portID, channelID, sequence, channel) k.WriteErrorReceipt(ctx, portID, channelID, types.NewUpgradeError(sequence, types.ErrInvalidUpgrade)) - k.Logger(ctx).Info("channel state updated", "port-id", portID, "channel-id", channelID, "previous-state", previousState, "new-state", types.OPEN) + k.Logger.Info("channel state updated", "port-id", portID, "channel-id", channelID, "previous-state", previousState, "new-state", types.OPEN) } // ChanUpgradeTimeout times out an outstanding upgrade. @@ -845,7 +845,7 @@ func (k *Keeper) WriteUpgradeTimeoutChannel( channel = k.restoreChannel(ctx, portID, channelID, channel.UpgradeSequence, channel) k.WriteErrorReceipt(ctx, portID, channelID, types.NewUpgradeError(channel.UpgradeSequence, types.ErrUpgradeTimeout)) - k.Logger(ctx).Info("channel state restored", "port-id", portID, "channel-id", channelID) + k.Logger.Info("channel state restored", "port-id", portID, "channel-id", channelID) return channel, upgrade } @@ -1053,5 +1053,5 @@ func (k *Keeper) WriteErrorReceipt(ctx context.Context, portID, channelID string } k.setUpgradeErrorReceipt(ctx, portID, channelID, errorReceiptToWrite) - EmitErrorReceiptEvent(ctx, portID, channelID, channel, upgradeError) + k.EmitErrorReceiptEvent(ctx, portID, channelID, channel, upgradeError) } diff --git a/modules/core/04-channel/types/errors.go b/modules/core/04-channel/types/errors.go index 9e13edef03c..7d9407a32a6 100644 --- a/modules/core/04-channel/types/errors.go +++ b/modules/core/04-channel/types/errors.go @@ -59,4 +59,5 @@ var ( ErrPruningSequenceStartNotFound = errorsmod.Register(SubModuleName, 41, "pruning sequence start not found") ErrRecvStartSequenceNotFound = errorsmod.Register(SubModuleName, 42, "recv start sequence not found") ErrInvalidCommitment = errorsmod.Register(SubModuleName, 43, "invalid commitment") + ErrFailedAcknowledgement = errorsmod.Register(SubModuleName, 44, "acknowledgement error") ) diff --git a/modules/core/keeper/keeper.go b/modules/core/keeper/keeper.go index ccb1abe974d..9c9350071ec 100644 --- a/modules/core/keeper/keeper.go +++ b/modules/core/keeper/keeper.go @@ -5,7 +5,7 @@ import ( "reflect" "strings" - corestore "cosmossdk.io/core/store" + "cosmossdk.io/core/appmodule" "github.com/cosmos/cosmos-sdk/codec" @@ -20,6 +20,8 @@ import ( // Keeper defines each ICS keeper for IBC type Keeper struct { + appmodule.Environment + ClientKeeper *clientkeeper.Keeper ConnectionKeeper *connectionkeeper.Keeper ChannelKeeper *channelkeeper.Keeper @@ -32,7 +34,7 @@ type Keeper struct { // NewKeeper creates a new ibc Keeper func NewKeeper( - cdc codec.BinaryCodec, storeService corestore.KVStoreService, paramSpace types.ParamSubspace, + cdc codec.BinaryCodec, env appmodule.Environment, paramSpace types.ParamSubspace, upgradeKeeper clienttypes.UpgradeKeeper, authority string, ) *Keeper { // panic if any of the keepers passed in is empty @@ -44,12 +46,13 @@ func NewKeeper( panic(errors.New("authority must be non-empty")) } - clientKeeper := clientkeeper.NewKeeper(cdc, storeService, paramSpace, upgradeKeeper) - connectionKeeper := connectionkeeper.NewKeeper(cdc, storeService, paramSpace, clientKeeper) + clientKeeper := clientkeeper.NewKeeper(cdc, env, paramSpace, upgradeKeeper) + connectionKeeper := connectionkeeper.NewKeeper(cdc, env, paramSpace, clientKeeper) portKeeper := portkeeper.NewKeeper() - channelKeeper := channelkeeper.NewKeeper(cdc, storeService, clientKeeper, connectionKeeper) + channelKeeper := channelkeeper.NewKeeper(cdc, env, clientKeeper, connectionKeeper) return &Keeper{ + Environment: env, cdc: cdc, ClientKeeper: clientKeeper, ConnectionKeeper: connectionKeeper, diff --git a/modules/core/keeper/keeper_test.go b/modules/core/keeper/keeper_test.go index 77d11f82c70..fa4e01238c3 100644 --- a/modules/core/keeper/keeper_test.go +++ b/modules/core/keeper/keeper_test.go @@ -5,6 +5,7 @@ import ( testifysuite "github.com/stretchr/testify/suite" + "cosmossdk.io/log" upgradekeeper "cosmossdk.io/x/upgrade/keeper" "github.com/cosmos/cosmos-sdk/runtime" @@ -75,7 +76,7 @@ func (suite *KeeperTestSuite) TestNewKeeper() { newIBCKeeperFn = func() { ibckeeper.NewKeeper( suite.chainA.GetSimApp().AppCodec(), - runtime.NewKVStoreService(suite.chainA.GetSimApp().GetKey(ibcexported.StoreKey)), + runtime.NewEnvironment(runtime.NewKVStoreService(suite.chainA.GetSimApp().GetKey(ibcexported.StoreKey)), log.NewNopLogger()), suite.chainA.GetSimApp().GetSubspace(ibcexported.ModuleName), upgradeKeeper, "", // authority @@ -95,7 +96,7 @@ func (suite *KeeperTestSuite) TestNewKeeper() { newIBCKeeperFn = func() { ibckeeper.NewKeeper( suite.chainA.GetSimApp().AppCodec(), - runtime.NewKVStoreService(suite.chainA.GetSimApp().GetKey(ibcexported.StoreKey)), + runtime.NewEnvironment(runtime.NewKVStoreService(suite.chainA.GetSimApp().GetKey(ibcexported.StoreKey)), log.NewNopLogger()), suite.chainA.GetSimApp().GetSubspace(ibcexported.ModuleName), upgradeKeeper, suite.chainA.App.GetIBCKeeper().GetAuthority(), diff --git a/modules/core/keeper/msg_server.go b/modules/core/keeper/msg_server.go index b8dabeb8bb2..d0c7359bf7d 100644 --- a/modules/core/keeper/msg_server.go +++ b/modules/core/keeper/msg_server.go @@ -10,10 +10,10 @@ import ( clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types" connectiontypes "github.com/cosmos/ibc-go/v9/modules/core/03-connection/types" - "github.com/cosmos/ibc-go/v9/modules/core/04-channel/keeper" 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" "github.com/cosmos/ibc-go/v9/modules/core/internal/telemetry" coretypes "github.com/cosmos/ibc-go/v9/modules/core/types" ) @@ -30,9 +30,7 @@ var ( // of the light client module to unmarshal and interpret the proto encoded bytes. // Backwards compatibility with older versions of ibc-go is maintained through the light client module reconstructing and encoding // the expected concrete type to the protobuf.Any for proof verification. -func (k *Keeper) CreateClient(goCtx context.Context, msg *clienttypes.MsgCreateClient) (*clienttypes.MsgCreateClientResponse, error) { - ctx := sdk.UnwrapSDKContext(goCtx) - +func (k *Keeper) CreateClient(ctx context.Context, msg *clienttypes.MsgCreateClient) (*clienttypes.MsgCreateClientResponse, error) { clientState, err := clienttypes.UnpackClientState(msg.ClientState) if err != nil { return nil, err @@ -47,9 +45,7 @@ func (k *Keeper) CreateClient(goCtx context.Context, msg *clienttypes.MsgCreateC } // UpdateClient defines a rpc handler method for MsgUpdateClient. -func (k *Keeper) UpdateClient(goCtx context.Context, msg *clienttypes.MsgUpdateClient) (*clienttypes.MsgUpdateClientResponse, error) { - ctx := sdk.UnwrapSDKContext(goCtx) - +func (k *Keeper) UpdateClient(ctx context.Context, msg *clienttypes.MsgUpdateClient) (*clienttypes.MsgUpdateClientResponse, error) { clientMsg, err := clienttypes.UnpackClientMessage(msg.ClientMessage) if err != nil { return nil, err @@ -68,11 +64,10 @@ func (k *Keeper) UpdateClient(goCtx context.Context, msg *clienttypes.MsgUpdateC // of the light client module to unmarshal and interpret the proto encoded bytes. // Backwards compatibility with older versions of ibc-go is maintained through the light client module reconstructing and encoding // the expected concrete type to the protobuf.Any for proof verification. -func (k *Keeper) UpgradeClient(goCtx context.Context, msg *clienttypes.MsgUpgradeClient) (*clienttypes.MsgUpgradeClientResponse, error) { - ctx := sdk.UnwrapSDKContext(goCtx) - +func (k *Keeper) UpgradeClient(ctx context.Context, msg *clienttypes.MsgUpgradeClient) (*clienttypes.MsgUpgradeClientResponse, error) { if err := k.ClientKeeper.UpgradeClient( - ctx, msg.ClientId, + ctx, + msg.ClientId, msg.ClientState.Value, msg.ConsensusState.Value, msg.ProofUpgradeClient, @@ -87,9 +82,7 @@ func (k *Keeper) UpgradeClient(goCtx context.Context, msg *clienttypes.MsgUpgrad // SubmitMisbehaviour defines a rpc handler method for MsgSubmitMisbehaviour. // Warning: DEPRECATED // This handler is redundant as `MsgUpdateClient` is now capable of handling both a Header and a Misbehaviour -func (k *Keeper) SubmitMisbehaviour(goCtx context.Context, msg *clienttypes.MsgSubmitMisbehaviour) (*clienttypes.MsgSubmitMisbehaviourResponse, error) { //nolint:staticcheck // for now, we're using msgsubmitmisbehaviour. - ctx := sdk.UnwrapSDKContext(goCtx) - +func (k *Keeper) SubmitMisbehaviour(ctx context.Context, msg *clienttypes.MsgSubmitMisbehaviour) (*clienttypes.MsgSubmitMisbehaviourResponse, error) { //nolint:staticcheck // for now, we're using msgsubmitmisbehaviour. misbehaviour, err := clienttypes.UnpackClientMessage(msg.Misbehaviour) if err != nil { return nil, err @@ -103,12 +96,11 @@ func (k *Keeper) SubmitMisbehaviour(goCtx context.Context, msg *clienttypes.MsgS } // RecoverClient defines a rpc handler method for MsgRecoverClient. -func (k *Keeper) RecoverClient(goCtx context.Context, msg *clienttypes.MsgRecoverClient) (*clienttypes.MsgRecoverClientResponse, error) { +func (k *Keeper) RecoverClient(ctx context.Context, msg *clienttypes.MsgRecoverClient) (*clienttypes.MsgRecoverClientResponse, error) { if k.GetAuthority() != msg.Signer { return nil, errorsmod.Wrapf(ibcerrors.ErrUnauthorized, "expected %s, got %s", k.GetAuthority(), msg.Signer) } - ctx := sdk.UnwrapSDKContext(goCtx) if err := k.ClientKeeper.RecoverClient(ctx, msg.SubjectClientId, msg.SubstituteClientId); err != nil { return nil, errorsmod.Wrap(err, "client recovery failed") } @@ -117,7 +109,7 @@ func (k *Keeper) RecoverClient(goCtx context.Context, msg *clienttypes.MsgRecove } // IBCSoftwareUpgrade defines a rpc handler method for MsgIBCSoftwareUpgrade. -func (k *Keeper) IBCSoftwareUpgrade(goCtx context.Context, msg *clienttypes.MsgIBCSoftwareUpgrade) (*clienttypes.MsgIBCSoftwareUpgradeResponse, error) { +func (k *Keeper) IBCSoftwareUpgrade(ctx context.Context, msg *clienttypes.MsgIBCSoftwareUpgrade) (*clienttypes.MsgIBCSoftwareUpgradeResponse, error) { if k.GetAuthority() != msg.Signer { return nil, errorsmod.Wrapf(ibcerrors.ErrUnauthorized, "expected %s, got %s", k.GetAuthority(), msg.Signer) } @@ -127,7 +119,7 @@ func (k *Keeper) IBCSoftwareUpgrade(goCtx context.Context, msg *clienttypes.MsgI return nil, errorsmod.Wrapf(clienttypes.ErrInvalidClientType, "cannot unpack client state: %s", err) } - if err := k.ClientKeeper.ScheduleIBCSoftwareUpgrade(goCtx, msg.Plan, upgradedClientState); err != nil { + if err := k.ClientKeeper.ScheduleIBCSoftwareUpgrade(ctx, msg.Plan, upgradedClientState); err != nil { return nil, errorsmod.Wrap(err, "failed to schedule upgrade") } @@ -135,9 +127,7 @@ func (k *Keeper) IBCSoftwareUpgrade(goCtx context.Context, msg *clienttypes.MsgI } // ConnectionOpenInit defines a rpc handler method for MsgConnectionOpenInit. -func (k *Keeper) ConnectionOpenInit(goCtx context.Context, msg *connectiontypes.MsgConnectionOpenInit) (*connectiontypes.MsgConnectionOpenInitResponse, error) { - ctx := sdk.UnwrapSDKContext(goCtx) - +func (k *Keeper) ConnectionOpenInit(ctx context.Context, msg *connectiontypes.MsgConnectionOpenInit) (*connectiontypes.MsgConnectionOpenInitResponse, error) { if _, err := k.ConnectionKeeper.ConnOpenInit(ctx, msg.ClientId, msg.Counterparty, msg.Version, msg.DelayPeriod); err != nil { return nil, errorsmod.Wrap(err, "connection handshake open init failed") } @@ -146,9 +136,7 @@ func (k *Keeper) ConnectionOpenInit(goCtx context.Context, msg *connectiontypes. } // ConnectionOpenTry defines a rpc handler method for MsgConnectionOpenTry. -func (k *Keeper) ConnectionOpenTry(goCtx context.Context, msg *connectiontypes.MsgConnectionOpenTry) (*connectiontypes.MsgConnectionOpenTryResponse, error) { - ctx := sdk.UnwrapSDKContext(goCtx) - +func (k *Keeper) ConnectionOpenTry(ctx context.Context, msg *connectiontypes.MsgConnectionOpenTry) (*connectiontypes.MsgConnectionOpenTryResponse, error) { if _, err := k.ConnectionKeeper.ConnOpenTry( ctx, msg.Counterparty, msg.DelayPeriod, msg.ClientId, msg.CounterpartyVersions, msg.ProofInit, msg.ProofHeight, @@ -160,9 +148,7 @@ func (k *Keeper) ConnectionOpenTry(goCtx context.Context, msg *connectiontypes.M } // ConnectionOpenAck defines a rpc handler method for MsgConnectionOpenAck. -func (k *Keeper) ConnectionOpenAck(goCtx context.Context, msg *connectiontypes.MsgConnectionOpenAck) (*connectiontypes.MsgConnectionOpenAckResponse, error) { - ctx := sdk.UnwrapSDKContext(goCtx) - +func (k *Keeper) ConnectionOpenAck(ctx context.Context, msg *connectiontypes.MsgConnectionOpenAck) (*connectiontypes.MsgConnectionOpenAckResponse, error) { if err := k.ConnectionKeeper.ConnOpenAck( ctx, msg.ConnectionId, msg.Version, msg.CounterpartyConnectionId, msg.ProofTry, msg.ProofHeight, @@ -174,9 +160,7 @@ func (k *Keeper) ConnectionOpenAck(goCtx context.Context, msg *connectiontypes.M } // ConnectionOpenConfirm defines a rpc handler method for MsgConnectionOpenConfirm. -func (k *Keeper) ConnectionOpenConfirm(goCtx context.Context, msg *connectiontypes.MsgConnectionOpenConfirm) (*connectiontypes.MsgConnectionOpenConfirmResponse, error) { - ctx := sdk.UnwrapSDKContext(goCtx) - +func (k *Keeper) ConnectionOpenConfirm(ctx context.Context, msg *connectiontypes.MsgConnectionOpenConfirm) (*connectiontypes.MsgConnectionOpenConfirmResponse, error) { if err := k.ConnectionKeeper.ConnOpenConfirm( ctx, msg.ConnectionId, msg.ProofAck, msg.ProofHeight, ); err != nil { @@ -189,13 +173,11 @@ func (k *Keeper) ConnectionOpenConfirm(goCtx context.Context, msg *connectiontyp // ChannelOpenInit defines a rpc handler method for MsgChannelOpenInit. // ChannelOpenInit will perform 04-channel checks, route to the application // callback, and write an OpenInit channel into state upon successful execution. -func (k *Keeper) ChannelOpenInit(goCtx context.Context, msg *channeltypes.MsgChannelOpenInit) (*channeltypes.MsgChannelOpenInitResponse, error) { - ctx := sdk.UnwrapSDKContext(goCtx) - +func (k *Keeper) ChannelOpenInit(ctx context.Context, msg *channeltypes.MsgChannelOpenInit) (*channeltypes.MsgChannelOpenInitResponse, error) { // Retrieve application callbacks from router cbs, ok := k.PortKeeper.Route(msg.PortId) if !ok { - ctx.Logger().Error("channel open init failed", "port-id", msg.PortId, "error", errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.PortId)) + k.Logger.Error("channel open init failed", "port-id", msg.PortId, "error", errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.PortId)) return nil, errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.PortId) } @@ -204,21 +186,21 @@ func (k *Keeper) ChannelOpenInit(goCtx context.Context, msg *channeltypes.MsgCha ctx, msg.Channel.Ordering, msg.Channel.ConnectionHops, msg.PortId, msg.Channel.Counterparty, msg.Channel.Version, ) if err != nil { - ctx.Logger().Error("channel open init failed", "error", errorsmod.Wrap(err, "channel handshake open init failed")) + k.Logger.Error("channel open init failed", "error", errorsmod.Wrap(err, "channel handshake open init failed")) return nil, errorsmod.Wrap(err, "channel handshake open init failed") } // Perform application logic callback version, err := cbs.OnChanOpenInit(ctx, msg.Channel.Ordering, msg.Channel.ConnectionHops, msg.PortId, channelID, msg.Channel.Counterparty, msg.Channel.Version) if err != nil { - ctx.Logger().Error("channel open init failed", "port-id", msg.PortId, "channel-id", channelID, "error", errorsmod.Wrap(err, "channel open init callback failed")) + k.Logger.Error("channel open init failed", "port-id", msg.PortId, "channel-id", channelID, "error", errorsmod.Wrap(err, "channel open init callback failed")) return nil, errorsmod.Wrapf(err, "channel open init callback failed for port ID: %s, channel ID: %s", msg.PortId, channelID) } // Write channel into state k.ChannelKeeper.WriteOpenInitChannel(ctx, msg.PortId, channelID, msg.Channel.Ordering, msg.Channel.ConnectionHops, msg.Channel.Counterparty, version) - ctx.Logger().Info("channel open init succeeded", "channel-id", channelID, "version", version) + k.Logger.Info("channel open init succeeded", "channel-id", channelID, "version", version) return &channeltypes.MsgChannelOpenInitResponse{ ChannelId: channelID, @@ -229,34 +211,32 @@ func (k *Keeper) ChannelOpenInit(goCtx context.Context, msg *channeltypes.MsgCha // ChannelOpenTry defines a rpc handler method for MsgChannelOpenTry. // ChannelOpenTry will perform 04-channel checks, route to the application // callback, and write an OpenTry channel into state upon successful execution. -func (k *Keeper) ChannelOpenTry(goCtx context.Context, msg *channeltypes.MsgChannelOpenTry) (*channeltypes.MsgChannelOpenTryResponse, error) { - ctx := sdk.UnwrapSDKContext(goCtx) - +func (k *Keeper) ChannelOpenTry(ctx context.Context, msg *channeltypes.MsgChannelOpenTry) (*channeltypes.MsgChannelOpenTryResponse, error) { // Retrieve application callbacks from router cbs, ok := k.PortKeeper.Route(msg.PortId) if !ok { - ctx.Logger().Error("channel open try failed", "port-id", msg.PortId, "error", errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.PortId)) + k.Logger.Error("channel open try failed", "port-id", msg.PortId, "error", errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.PortId)) return nil, errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.PortId) } // Perform 04-channel verification channelID, err := k.ChannelKeeper.ChanOpenTry(ctx, msg.Channel.Ordering, msg.Channel.ConnectionHops, msg.PortId, msg.Channel.Counterparty, msg.CounterpartyVersion, msg.ProofInit, msg.ProofHeight) if err != nil { - ctx.Logger().Error("channel open try failed", "error", errorsmod.Wrap(err, "channel handshake open try failed")) + k.Logger.Error("channel open try failed", "error", errorsmod.Wrap(err, "channel handshake open try failed")) return nil, errorsmod.Wrap(err, "channel handshake open try failed") } // Perform application logic callback version, err := cbs.OnChanOpenTry(ctx, msg.Channel.Ordering, msg.Channel.ConnectionHops, msg.PortId, channelID, msg.Channel.Counterparty, msg.CounterpartyVersion) if err != nil { - ctx.Logger().Error("channel open try failed", "port-id", msg.PortId, "channel-id", channelID, "error", errorsmod.Wrap(err, "channel open try callback failed")) + k.Logger.Error("channel open try failed", "port-id", msg.PortId, "channel-id", channelID, "error", errorsmod.Wrap(err, "channel open try callback failed")) return nil, errorsmod.Wrapf(err, "channel open try callback failed for port ID: %s, channel ID: %s", msg.PortId, channelID) } // Write channel into state k.ChannelKeeper.WriteOpenTryChannel(ctx, msg.PortId, channelID, msg.Channel.Ordering, msg.Channel.ConnectionHops, msg.Channel.Counterparty, version) - ctx.Logger().Info("channel open try succeeded", "channel-id", channelID, "port-id", msg.PortId, "version", version) + k.Logger.Info("channel open try succeeded", "channel-id", channelID, "port-id", msg.PortId, "version", version) return &channeltypes.MsgChannelOpenTryResponse{ ChannelId: channelID, @@ -267,13 +247,11 @@ func (k *Keeper) ChannelOpenTry(goCtx context.Context, msg *channeltypes.MsgChan // ChannelOpenAck defines a rpc handler method for MsgChannelOpenAck. // ChannelOpenAck will perform 04-channel checks, route to the application // callback, and write an OpenAck channel into state upon successful execution. -func (k *Keeper) ChannelOpenAck(goCtx context.Context, msg *channeltypes.MsgChannelOpenAck) (*channeltypes.MsgChannelOpenAckResponse, error) { - ctx := sdk.UnwrapSDKContext(goCtx) - +func (k *Keeper) ChannelOpenAck(ctx context.Context, msg *channeltypes.MsgChannelOpenAck) (*channeltypes.MsgChannelOpenAckResponse, error) { // Retrieve application callbacks from router cbs, ok := k.PortKeeper.Route(msg.PortId) if !ok { - ctx.Logger().Error("channel open ack failed", "port-id", msg.PortId, "error", errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.PortId)) + k.Logger.Error("channel open ack failed", "port-id", msg.PortId, "error", errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.PortId)) return nil, errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.PortId) } @@ -281,7 +259,7 @@ func (k *Keeper) ChannelOpenAck(goCtx context.Context, msg *channeltypes.MsgChan if err := k.ChannelKeeper.ChanOpenAck( ctx, msg.PortId, msg.ChannelId, msg.CounterpartyVersion, msg.CounterpartyChannelId, msg.ProofTry, msg.ProofHeight, ); err != nil { - ctx.Logger().Error("channel open ack failed", "error", err.Error()) + k.Logger.Error("channel open ack failed", "error", err.Error()) return nil, errorsmod.Wrap(err, "channel handshake open ack failed") } @@ -290,11 +268,11 @@ func (k *Keeper) ChannelOpenAck(goCtx context.Context, msg *channeltypes.MsgChan // Perform application logic callback if err := cbs.OnChanOpenAck(ctx, msg.PortId, msg.ChannelId, msg.CounterpartyChannelId, msg.CounterpartyVersion); err != nil { - ctx.Logger().Error("channel open ack failed", "port-id", msg.PortId, "channel-id", msg.ChannelId, "error", errorsmod.Wrap(err, "channel open ack callback failed")) + k.Logger.Error("channel open ack failed", "port-id", msg.PortId, "channel-id", msg.ChannelId, "error", errorsmod.Wrap(err, "channel open ack callback failed")) return nil, errorsmod.Wrapf(err, "channel open ack callback failed for port ID: %s, channel ID: %s", msg.PortId, msg.ChannelId) } - ctx.Logger().Info("channel open ack succeeded", "channel-id", msg.ChannelId, "port-id", msg.PortId) + k.Logger.Info("channel open ack succeeded", "channel-id", msg.ChannelId, "port-id", msg.PortId) return &channeltypes.MsgChannelOpenAckResponse{}, nil } @@ -302,19 +280,17 @@ func (k *Keeper) ChannelOpenAck(goCtx context.Context, msg *channeltypes.MsgChan // ChannelOpenConfirm defines a rpc handler method for MsgChannelOpenConfirm. // ChannelOpenConfirm will perform 04-channel checks, route to the application // callback, and write an OpenConfirm channel into state upon successful execution. -func (k *Keeper) ChannelOpenConfirm(goCtx context.Context, msg *channeltypes.MsgChannelOpenConfirm) (*channeltypes.MsgChannelOpenConfirmResponse, error) { - ctx := sdk.UnwrapSDKContext(goCtx) - +func (k *Keeper) ChannelOpenConfirm(ctx context.Context, msg *channeltypes.MsgChannelOpenConfirm) (*channeltypes.MsgChannelOpenConfirmResponse, error) { // Retrieve application callbacks from router cbs, ok := k.PortKeeper.Route(msg.PortId) if !ok { - ctx.Logger().Error("channel open confirm failed", "port-id", msg.PortId, "error", errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.PortId)) + k.Logger.Error("channel open confirm failed", "port-id", msg.PortId, "error", errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.PortId)) return nil, errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.PortId) } // Perform 04-channel verification if err := k.ChannelKeeper.ChanOpenConfirm(ctx, msg.PortId, msg.ChannelId, msg.ProofAck, msg.ProofHeight); err != nil { - ctx.Logger().Error("channel open confirm failed", "error", errorsmod.Wrap(err, "channel handshake open confirm failed")) + k.Logger.Error("channel open confirm failed", "error", errorsmod.Wrap(err, "channel handshake open confirm failed")) return nil, errorsmod.Wrap(err, "channel handshake open confirm failed") } @@ -323,116 +299,119 @@ func (k *Keeper) ChannelOpenConfirm(goCtx context.Context, msg *channeltypes.Msg // Perform application logic callback if err := cbs.OnChanOpenConfirm(ctx, msg.PortId, msg.ChannelId); err != nil { - ctx.Logger().Error("channel open confirm failed", "port-id", msg.PortId, "channel-id", msg.ChannelId, "error", errorsmod.Wrap(err, "channel open confirm callback failed")) + k.Logger.Error("channel open confirm failed", "port-id", msg.PortId, "channel-id", msg.ChannelId, "error", errorsmod.Wrap(err, "channel open confirm callback failed")) return nil, errorsmod.Wrapf(err, "channel open confirm callback failed for port ID: %s, channel ID: %s", msg.PortId, msg.ChannelId) } - ctx.Logger().Info("channel open confirm succeeded", "channel-id", msg.ChannelId, "port-id", msg.PortId) + k.Logger.Info("channel open confirm succeeded", "channel-id", msg.ChannelId, "port-id", msg.PortId) return &channeltypes.MsgChannelOpenConfirmResponse{}, nil } // ChannelCloseInit defines a rpc handler method for MsgChannelCloseInit. -func (k *Keeper) ChannelCloseInit(goCtx context.Context, msg *channeltypes.MsgChannelCloseInit) (*channeltypes.MsgChannelCloseInitResponse, error) { - ctx := sdk.UnwrapSDKContext(goCtx) - +func (k *Keeper) ChannelCloseInit(ctx context.Context, msg *channeltypes.MsgChannelCloseInit) (*channeltypes.MsgChannelCloseInitResponse, error) { // Retrieve callbacks from router cbs, ok := k.PortKeeper.Route(msg.PortId) if !ok { - ctx.Logger().Error("channel close init failed", "port-id", msg.PortId, "error", errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.PortId)) + k.Logger.Error("channel close init failed", "port-id", msg.PortId, "error", errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.PortId)) return nil, errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.PortId) } if err := cbs.OnChanCloseInit(ctx, msg.PortId, msg.ChannelId); err != nil { - ctx.Logger().Error("channel close init failed", "port-id", msg.PortId, "channel-id", msg.ChannelId, "error", errorsmod.Wrap(err, "channel close init callback failed")) + k.Logger.Error("channel close init failed", "port-id", msg.PortId, "channel-id", msg.ChannelId, "error", errorsmod.Wrap(err, "channel close init callback failed")) return nil, errorsmod.Wrapf(err, "channel close init callback failed for port ID: %s, channel ID: %s", msg.PortId, msg.ChannelId) } err := k.ChannelKeeper.ChanCloseInit(ctx, msg.PortId, msg.ChannelId) if err != nil { - ctx.Logger().Error("channel close init failed", "port-id", msg.PortId, "channel-id", msg.ChannelId, "error", err.Error()) + k.Logger.Error("channel close init failed", "port-id", msg.PortId, "channel-id", msg.ChannelId, "error", err.Error()) return nil, errorsmod.Wrap(err, "channel handshake close init failed") } - ctx.Logger().Info("channel close init succeeded", "channel-id", msg.ChannelId, "port-id", msg.PortId) + k.Logger.Info("channel close init succeeded", "channel-id", msg.ChannelId, "port-id", msg.PortId) return &channeltypes.MsgChannelCloseInitResponse{}, nil } // ChannelCloseConfirm defines a rpc handler method for MsgChannelCloseConfirm. -func (k *Keeper) ChannelCloseConfirm(goCtx context.Context, msg *channeltypes.MsgChannelCloseConfirm) (*channeltypes.MsgChannelCloseConfirmResponse, error) { - ctx := sdk.UnwrapSDKContext(goCtx) - +func (k *Keeper) ChannelCloseConfirm(ctx context.Context, msg *channeltypes.MsgChannelCloseConfirm) (*channeltypes.MsgChannelCloseConfirmResponse, error) { // Retrieve callbacks from router cbs, ok := k.PortKeeper.Route(msg.PortId) if !ok { - ctx.Logger().Error("channel close confirm failed", "port-id", msg.PortId, "error", errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.PortId)) + k.Logger.Error("channel close confirm failed", "port-id", msg.PortId, "error", errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.PortId)) return nil, errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.PortId) } if err := cbs.OnChanCloseConfirm(ctx, msg.PortId, msg.ChannelId); err != nil { - ctx.Logger().Error("channel close confirm failed", "port-id", msg.PortId, "channel-id", msg.ChannelId, "error", errorsmod.Wrap(err, "channel close confirm callback failed")) + k.Logger.Error("channel close confirm failed", "port-id", msg.PortId, "channel-id", msg.ChannelId, "error", errorsmod.Wrap(err, "channel close confirm callback failed")) return nil, errorsmod.Wrapf(err, "channel close confirm callback failed for port ID: %s, channel ID: %s", msg.PortId, msg.ChannelId) } err := k.ChannelKeeper.ChanCloseConfirm(ctx, msg.PortId, msg.ChannelId, msg.ProofInit, msg.ProofHeight, msg.CounterpartyUpgradeSequence) if err != nil { - ctx.Logger().Error("channel close confirm failed", "port-id", msg.PortId, "channel-id", msg.ChannelId, "error", err.Error()) + k.Logger.Error("channel close confirm failed", "port-id", msg.PortId, "channel-id", msg.ChannelId, "error", err.Error()) return nil, errorsmod.Wrap(err, "channel handshake close confirm failed") } - ctx.Logger().Info("channel close confirm succeeded", "channel-id", msg.ChannelId, "port-id", msg.PortId) + k.Logger.Info("channel close confirm succeeded", "channel-id", msg.ChannelId, "port-id", msg.PortId) return &channeltypes.MsgChannelCloseConfirmResponse{}, nil } // RecvPacket defines a rpc handler method for MsgRecvPacket. -func (k *Keeper) RecvPacket(goCtx context.Context, msg *channeltypes.MsgRecvPacket) (*channeltypes.MsgRecvPacketResponse, error) { - ctx := sdk.UnwrapSDKContext(goCtx) - +func (k *Keeper) RecvPacket(ctx context.Context, msg *channeltypes.MsgRecvPacket) (*channeltypes.MsgRecvPacketResponse, error) { relayer, err := sdk.AccAddressFromBech32(msg.Signer) if err != nil { - ctx.Logger().Error("receive packet failed", "error", errorsmod.Wrap(err, "Invalid address for msg Signer")) + k.Logger.Error("receive packet failed", "error", errorsmod.Wrap(err, "Invalid address for msg Signer")) return nil, errorsmod.Wrap(err, "Invalid address for msg Signer") } // Retrieve callbacks from router cbs, ok := k.PortKeeper.Route(msg.Packet.DestinationPort) if !ok { - ctx.Logger().Error("receive packet failed", "port-id", msg.Packet.SourcePort, "error", errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.Packet.DestinationPort)) + k.Logger.Error("receive packet failed", "port-id", msg.Packet.SourcePort, "error", errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.Packet.DestinationPort)) return nil, errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.Packet.DestinationPort) } - // Perform TAO verification - // - // If the packet was already received, perform a no-op - // Use a cached context to prevent accidental state changes - cacheCtx, writeFn := ctx.CacheContext() - channelVersion, err := k.ChannelKeeper.RecvPacket(cacheCtx, msg.Packet, msg.ProofCommitment, msg.ProofHeight) + var channelVersion string + if err := k.BranchService.Execute(ctx, func(ctx context.Context) error { + // Perform TAO verification + channelVersion, err = k.ChannelKeeper.RecvPacket(ctx, msg.Packet, msg.ProofCommitment, msg.ProofHeight) + if err != nil { + return err + } - switch err { - case nil: - writeFn() - case channeltypes.ErrNoOpMsg: - // no-ops do not need event emission as they will be ignored - ctx.Logger().Debug("no-op on redundant relay", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel) - return &channeltypes.MsgRecvPacketResponse{Result: channeltypes.NOOP}, nil - default: - ctx.Logger().Error("receive packet failed", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel, "error", errorsmod.Wrap(err, "receive packet verification failed")) + return nil + }); err != nil { + if errors.Is(err, channeltypes.ErrNoOpMsg) { + // no-ops do not need event emission as they will be ignored + k.Logger.Debug("no-op on redundant relay", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel) + return &channeltypes.MsgRecvPacketResponse{Result: channeltypes.NOOP}, nil + } + + k.Logger.Error("receive packet failed", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel, "error", errorsmod.Wrap(err, "receive packet verification failed")) return nil, errorsmod.Wrap(err, "receive packet verification failed") } - // Perform application logic callback - // - // Cache context so that we may discard state changes from callback if the acknowledgement is unsuccessful. - cacheCtx, writeFn = ctx.CacheContext() - ack := cbs.OnRecvPacket(cacheCtx, channelVersion, msg.Packet, relayer) - if ack == nil || ack.Success() { - // write application state changes for asynchronous and successful acknowledgements - writeFn() - } else { - // Modify events in cached context to reflect unsuccessful acknowledgement - ctx.EventManager().EmitEvents(convertToErrorEvents(cacheCtx.EventManager().Events())) + var ack exported.Acknowledgement + if err := k.BranchService.Execute(ctx, func(ctx context.Context) error { + // Perform application logic callback + ack = cbs.OnRecvPacket(ctx, channelVersion, msg.Packet, relayer) + if ack == nil || ack.Success() { + // write application state changes for asynchronous and successful acknowledgements + return nil + } + + // we must return an error here so that false positive events are not emitted + return channeltypes.ErrFailedAcknowledgement + }); err != nil { + if errors.Is(err, channeltypes.ErrFailedAcknowledgement) { + // k.EventService.EventManager(ctx).EmitKV() + // Modify events in cached context to reflect unsuccessful acknowledgement + + // We lost apis to propagate and err prefix events from a branched multistore ctx + // ctx.EventManager().EmitEvents(convertToErrorEvents(cacheCtx.EventManager().Events())) + } } // Set packet acknowledgement only if the acknowledgement is not nil. @@ -446,7 +425,7 @@ func (k *Keeper) RecvPacket(goCtx context.Context, msg *channeltypes.MsgRecvPack defer telemetry.ReportRecvPacket(msg.Packet) - ctx.Logger().Info("receive packet callback succeeded", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel, "result", channeltypes.SUCCESS.String()) + k.Logger.Info("receive packet callback succeeded", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel, "result", channeltypes.SUCCESS.String()) return &channeltypes.MsgRecvPacketResponse{Result: channeltypes.SUCCESS}, nil } @@ -457,14 +436,14 @@ func (k *Keeper) Timeout(goCtx context.Context, msg *channeltypes.MsgTimeout) (* relayer, err := sdk.AccAddressFromBech32(msg.Signer) if err != nil { - ctx.Logger().Error("timeout failed", "error", errorsmod.Wrap(err, "Invalid address for msg Signer")) + k.Logger.Error("timeout failed", "error", errorsmod.Wrap(err, "Invalid address for msg Signer")) return nil, errorsmod.Wrap(err, "Invalid address for msg Signer") } // Retrieve callbacks from router cbs, ok := k.PortKeeper.Route(msg.Packet.SourcePort) if !ok { - ctx.Logger().Error("timeout failed", "port-id", msg.Packet.SourcePort, "error", errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.Packet.SourcePort)) + k.Logger.Error("timeout failed", "port-id", msg.Packet.SourcePort, "error", errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.Packet.SourcePort)) return nil, errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.Packet.SourcePort) } @@ -480,10 +459,10 @@ func (k *Keeper) Timeout(goCtx context.Context, msg *channeltypes.MsgTimeout) (* writeFn() case channeltypes.ErrNoOpMsg: // no-ops do not need event emission as they will be ignored - ctx.Logger().Debug("no-op on redundant relay", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel) + k.Logger.Debug("no-op on redundant relay", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel) return &channeltypes.MsgTimeoutResponse{Result: channeltypes.NOOP}, nil default: - ctx.Logger().Error("timeout failed", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel, "error", errorsmod.Wrap(err, "timeout packet verification failed")) + k.Logger.Error("timeout failed", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel, "error", errorsmod.Wrap(err, "timeout packet verification failed")) return nil, errorsmod.Wrap(err, "timeout packet verification failed") } @@ -495,13 +474,13 @@ func (k *Keeper) Timeout(goCtx context.Context, msg *channeltypes.MsgTimeout) (* // Perform application logic callback err = cbs.OnTimeoutPacket(ctx, channelVersion, msg.Packet, relayer) if err != nil { - ctx.Logger().Error("timeout failed", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel, "error", errorsmod.Wrap(err, "timeout packet callback failed")) + k.Logger.Error("timeout failed", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel, "error", errorsmod.Wrap(err, "timeout packet callback failed")) return nil, errorsmod.Wrap(err, "timeout packet callback failed") } defer telemetry.ReportTimeoutPacket(msg.Packet, "height") - ctx.Logger().Info("timeout packet callback succeeded", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel, "result", channeltypes.SUCCESS.String()) + k.Logger.Info("timeout packet callback succeeded", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel, "result", channeltypes.SUCCESS.String()) return &channeltypes.MsgTimeoutResponse{Result: channeltypes.SUCCESS}, nil } @@ -512,13 +491,13 @@ func (k *Keeper) TimeoutOnClose(goCtx context.Context, msg *channeltypes.MsgTime relayer, err := sdk.AccAddressFromBech32(msg.Signer) if err != nil { - ctx.Logger().Error("timeout on close failed", "error", errorsmod.Wrap(err, "Invalid address for msg Signer")) + k.Logger.Error("timeout on close failed", "error", errorsmod.Wrap(err, "Invalid address for msg Signer")) return nil, errorsmod.Wrap(err, "Invalid address for msg Signer") } cbs, ok := k.PortKeeper.Route(msg.Packet.SourcePort) if !ok { - ctx.Logger().Error("timeout on close failed", "port-id", msg.Packet.SourcePort, "error", errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.Packet.SourcePort)) + k.Logger.Error("timeout on close failed", "port-id", msg.Packet.SourcePort, "error", errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.Packet.SourcePort)) return nil, errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.Packet.SourcePort) } @@ -534,10 +513,10 @@ func (k *Keeper) TimeoutOnClose(goCtx context.Context, msg *channeltypes.MsgTime writeFn() case channeltypes.ErrNoOpMsg: // no-ops do not need event emission as they will be ignored - ctx.Logger().Debug("no-op on redundant relay", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel) + k.Logger.Debug("no-op on redundant relay", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel) return &channeltypes.MsgTimeoutOnCloseResponse{Result: channeltypes.NOOP}, nil default: - ctx.Logger().Error("timeout on close failed", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel, "error", errorsmod.Wrap(err, "timeout on close packet verification failed")) + k.Logger.Error("timeout on close failed", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel, "error", errorsmod.Wrap(err, "timeout on close packet verification failed")) return nil, errorsmod.Wrap(err, "timeout on close packet verification failed") } @@ -552,13 +531,13 @@ func (k *Keeper) TimeoutOnClose(goCtx context.Context, msg *channeltypes.MsgTime // application logic callback. err = cbs.OnTimeoutPacket(ctx, channelVersion, msg.Packet, relayer) if err != nil { - ctx.Logger().Error("timeout on close failed", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel, "error", errorsmod.Wrap(err, "timeout on close callback failed")) + k.Logger.Error("timeout on close failed", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel, "error", errorsmod.Wrap(err, "timeout on close callback failed")) return nil, errorsmod.Wrap(err, "timeout on close callback failed") } defer telemetry.ReportTimeoutPacket(msg.Packet, "channel-closed") - ctx.Logger().Info("timeout on close callback succeeded", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel, "result", channeltypes.SUCCESS.String()) + k.Logger.Info("timeout on close callback succeeded", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel, "result", channeltypes.SUCCESS.String()) return &channeltypes.MsgTimeoutOnCloseResponse{Result: channeltypes.SUCCESS}, nil } @@ -569,13 +548,13 @@ func (k *Keeper) Acknowledgement(goCtx context.Context, msg *channeltypes.MsgAck relayer, err := sdk.AccAddressFromBech32(msg.Signer) if err != nil { - ctx.Logger().Error("acknowledgement failed", "error", errorsmod.Wrap(err, "Invalid address for msg Signer")) + k.Logger.Error("acknowledgement failed", "error", errorsmod.Wrap(err, "Invalid address for msg Signer")) return nil, errorsmod.Wrap(err, "Invalid address for msg Signer") } cbs, ok := k.PortKeeper.Route(msg.Packet.SourcePort) if !ok { - ctx.Logger().Error("acknowledgement failed", "port-id", msg.Packet.SourcePort, "error", errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.Packet.SourcePort)) + k.Logger.Error("acknowledgement failed", "port-id", msg.Packet.SourcePort, "error", errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.Packet.SourcePort)) return nil, errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.Packet.SourcePort) } @@ -591,23 +570,23 @@ func (k *Keeper) Acknowledgement(goCtx context.Context, msg *channeltypes.MsgAck writeFn() case channeltypes.ErrNoOpMsg: // no-ops do not need event emission as they will be ignored - ctx.Logger().Debug("no-op on redundant relay", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel) + k.Logger.Debug("no-op on redundant relay", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel) return &channeltypes.MsgAcknowledgementResponse{Result: channeltypes.NOOP}, nil default: - ctx.Logger().Error("acknowledgement failed", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel, "error", errorsmod.Wrap(err, "acknowledge packet verification failed")) + k.Logger.Error("acknowledgement failed", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel, "error", errorsmod.Wrap(err, "acknowledge packet verification failed")) return nil, errorsmod.Wrap(err, "acknowledge packet verification failed") } // Perform application logic callback err = cbs.OnAcknowledgementPacket(ctx, channelVersion, msg.Packet, msg.Acknowledgement, relayer) if err != nil { - ctx.Logger().Error("acknowledgement failed", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel, "error", errorsmod.Wrap(err, "acknowledge packet callback failed")) + k.Logger.Error("acknowledgement failed", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel, "error", errorsmod.Wrap(err, "acknowledge packet callback failed")) return nil, errorsmod.Wrap(err, "acknowledge packet callback failed") } defer telemetry.ReportAcknowledgePacket(msg.Packet) - ctx.Logger().Info("acknowledgement succeeded", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel, "result", channeltypes.SUCCESS.String()) + k.Logger.Info("acknowledgement succeeded", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel, "result", channeltypes.SUCCESS.String()) return &channeltypes.MsgAcknowledgementResponse{Result: channeltypes.SUCCESS}, nil } @@ -622,19 +601,19 @@ func (k *Keeper) ChannelUpgradeInit(goCtx context.Context, msg *channeltypes.Msg app, ok := k.PortKeeper.Route(msg.PortId) if !ok { - ctx.Logger().Error("channel upgrade init failed", "port-id", msg.PortId, "error", errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.PortId)) + k.Logger.Error("channel upgrade init failed", "port-id", msg.PortId, "error", errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.PortId)) return nil, errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.PortId) } cbs, ok := app.(porttypes.UpgradableModule) if !ok { - ctx.Logger().Error("channel upgrade init failed", "port-id", msg.PortId, "error", errorsmod.Wrapf(porttypes.ErrInvalidRoute, "upgrade route not found to portID: %s", msg.PortId)) + k.Logger.Error("channel upgrade init failed", "port-id", msg.PortId, "error", errorsmod.Wrapf(porttypes.ErrInvalidRoute, "upgrade route not found to portID: %s", msg.PortId)) return nil, errorsmod.Wrapf(porttypes.ErrInvalidRoute, "upgrade route not found to portID: %s", msg.PortId) } upgrade, err := k.ChannelKeeper.ChanUpgradeInit(ctx, msg.PortId, msg.ChannelId, msg.Fields) if err != nil { - ctx.Logger().Error("channel upgrade init failed", "error", errorsmod.Wrap(err, "channel upgrade init failed")) + k.Logger.Error("channel upgrade init failed", "error", errorsmod.Wrap(err, "channel upgrade init failed")) return nil, errorsmod.Wrap(err, "channel upgrade init failed") } @@ -643,14 +622,16 @@ func (k *Keeper) ChannelUpgradeInit(goCtx context.Context, msg *channeltypes.Msg cacheCtx, _ := ctx.CacheContext() upgradeVersion, err := cbs.OnChanUpgradeInit(cacheCtx, msg.PortId, msg.ChannelId, upgrade.Fields.Ordering, upgrade.Fields.ConnectionHops, upgrade.Fields.Version) if err != nil { - ctx.Logger().Error("channel upgrade init callback failed", "port-id", msg.PortId, "channel-id", msg.ChannelId, "error", err.Error()) + k.Logger.Error("channel upgrade init callback failed", "port-id", msg.PortId, "channel-id", msg.ChannelId, "error", err.Error()) return nil, errorsmod.Wrapf(err, "channel upgrade init callback failed for port ID: %s, channel ID: %s", msg.PortId, msg.ChannelId) } channel, upgrade := k.ChannelKeeper.WriteUpgradeInitChannel(ctx, msg.PortId, msg.ChannelId, upgrade, upgradeVersion) - ctx.Logger().Info("channel upgrade init succeeded", "channel-id", msg.ChannelId, "version", upgradeVersion) - keeper.EmitChannelUpgradeInitEvent(ctx, msg.PortId, msg.ChannelId, channel, upgrade) + k.Logger.Info("channel upgrade init succeeded", "channel-id", msg.ChannelId, "version", upgradeVersion) + if err := k.ChannelKeeper.EmitChannelUpgradeInitEvent(ctx, msg.PortId, msg.ChannelId, channel, upgrade); err != nil { + return nil, errorsmod.Wrap(err, "event emission failed") + } return &channeltypes.MsgChannelUpgradeInitResponse{ Upgrade: upgrade, @@ -664,19 +645,19 @@ func (k *Keeper) ChannelUpgradeTry(goCtx context.Context, msg *channeltypes.MsgC app, ok := k.PortKeeper.Route(msg.PortId) if !ok { - ctx.Logger().Error("channel upgrade try failed", "port-id", msg.PortId, "error", errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.PortId)) + k.Logger.Error("channel upgrade try failed", "port-id", msg.PortId, "error", errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.PortId)) return nil, errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.PortId) } cbs, ok := app.(porttypes.UpgradableModule) if !ok { - ctx.Logger().Error("channel upgrade try failed", "port-id", msg.PortId, "error", errorsmod.Wrapf(porttypes.ErrInvalidRoute, "upgrade route not found to portID: %s", msg.PortId)) + k.Logger.Error("channel upgrade try failed", "port-id", msg.PortId, "error", errorsmod.Wrapf(porttypes.ErrInvalidRoute, "upgrade route not found to portID: %s", msg.PortId)) return nil, errorsmod.Wrapf(porttypes.ErrInvalidRoute, "upgrade route not found to portID: %s", msg.PortId) } channel, upgrade, err := k.ChannelKeeper.ChanUpgradeTry(ctx, msg.PortId, msg.ChannelId, msg.ProposedUpgradeConnectionHops, msg.CounterpartyUpgradeFields, msg.CounterpartyUpgradeSequence, msg.ProofChannel, msg.ProofUpgrade, msg.ProofHeight) if err != nil { - ctx.Logger().Error("channel upgrade try failed", "error", errorsmod.Wrap(err, "channel upgrade try failed")) + k.Logger.Error("channel upgrade try failed", "error", errorsmod.Wrap(err, "channel upgrade try failed")) if channeltypes.IsUpgradeError(err) { // In case the error is a wrapped upgrade error, we need to extract the inner error else process as normal var upgradeErr *channeltypes.UpgradeError @@ -697,14 +678,16 @@ func (k *Keeper) ChannelUpgradeTry(goCtx context.Context, msg *channeltypes.MsgC cacheCtx, _ := ctx.CacheContext() upgradeVersion, err := cbs.OnChanUpgradeTry(cacheCtx, msg.PortId, msg.ChannelId, upgrade.Fields.Ordering, upgrade.Fields.ConnectionHops, upgrade.Fields.Version) if err != nil { - ctx.Logger().Error("channel upgrade try callback failed", "port-id", msg.PortId, "channel-id", msg.ChannelId, "error", err.Error()) + k.Logger.Error("channel upgrade try callback failed", "port-id", msg.PortId, "channel-id", msg.ChannelId, "error", err.Error()) return nil, errorsmod.Wrapf(err, "channel upgrade try callback failed for port ID: %s, channel ID: %s", msg.PortId, msg.ChannelId) } channel, upgrade = k.ChannelKeeper.WriteUpgradeTryChannel(ctx, msg.PortId, msg.ChannelId, upgrade, upgradeVersion) - ctx.Logger().Info("channel upgrade try succeeded", "port-id", msg.PortId, "channel-id", msg.ChannelId) - keeper.EmitChannelUpgradeTryEvent(ctx, msg.PortId, msg.ChannelId, channel, upgrade) + k.Logger.Info("channel upgrade try succeeded", "port-id", msg.PortId, "channel-id", msg.ChannelId) + if err := k.ChannelKeeper.EmitChannelUpgradeTryEvent(ctx, msg.PortId, msg.ChannelId, channel, upgrade); err != nil { + return nil, errorsmod.Wrap(err, "event emission failed") + } return &channeltypes.MsgChannelUpgradeTryResponse{ Result: channeltypes.SUCCESS, @@ -720,20 +703,20 @@ func (k *Keeper) ChannelUpgradeAck(goCtx context.Context, msg *channeltypes.MsgC app, ok := k.PortKeeper.Route(msg.PortId) if !ok { err := errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.PortId) - ctx.Logger().Error("channel upgrade ack failed", "port-id", msg.PortId, "error", err) + k.Logger.Error("channel upgrade ack failed", "port-id", msg.PortId, "error", err) return nil, err } cbs, ok := app.(porttypes.UpgradableModule) if !ok { err := errorsmod.Wrapf(porttypes.ErrInvalidRoute, "upgrade route not found to portID: %s", msg.PortId) - ctx.Logger().Error("channel upgrade ack failed", "port-id", msg.PortId, "error", err) + k.Logger.Error("channel upgrade ack failed", "port-id", msg.PortId, "error", err) return nil, err } err := k.ChannelKeeper.ChanUpgradeAck(ctx, msg.PortId, msg.ChannelId, msg.CounterpartyUpgrade, msg.ProofChannel, msg.ProofUpgrade, msg.ProofHeight) if err != nil { - ctx.Logger().Error("channel upgrade ack failed", "error", errorsmod.Wrap(err, "channel upgrade ack failed")) + k.Logger.Error("channel upgrade ack failed", "error", errorsmod.Wrap(err, "channel upgrade ack failed")) if channeltypes.IsUpgradeError(err) { k.ChannelKeeper.MustAbortUpgrade(ctx, msg.PortId, msg.ChannelId, err) @@ -756,7 +739,7 @@ func (k *Keeper) ChannelUpgradeAck(goCtx context.Context, msg *channeltypes.MsgC return nil, errorsmod.Wrapf(channeltypes.ErrChannelNotFound, "channel not found for port ID (%s) channel ID (%s)", msg.PortId, msg.ChannelId) } - ctx.Logger().Error("channel upgrade ack callback failed", "port-id", msg.PortId, "channel-id", msg.ChannelId, "error", err.Error()) + k.Logger.Error("channel upgrade ack callback failed", "port-id", msg.PortId, "channel-id", msg.ChannelId, "error", err.Error()) // explicitly wrap the application callback in an upgrade error with the correct upgrade sequence. // this prevents any errors caused from the application returning an UpgradeError with an incorrect sequence. @@ -767,33 +750,33 @@ func (k *Keeper) ChannelUpgradeAck(goCtx context.Context, msg *channeltypes.MsgC channel, upgrade := k.ChannelKeeper.WriteUpgradeAckChannel(ctx, msg.PortId, msg.ChannelId, msg.CounterpartyUpgrade) - ctx.Logger().Info("channel upgrade ack succeeded", "port-id", msg.PortId, "channel-id", msg.ChannelId) - keeper.EmitChannelUpgradeAckEvent(ctx, msg.PortId, msg.ChannelId, channel, upgrade) + k.Logger.Info("channel upgrade ack succeeded", "port-id", msg.PortId, "channel-id", msg.ChannelId) + if err := k.ChannelKeeper.EmitChannelUpgradeAckEvent(ctx, msg.PortId, msg.ChannelId, channel, upgrade); err != nil { + return nil, errorsmod.Wrap(err, "event emission failed") + } return &channeltypes.MsgChannelUpgradeAckResponse{Result: channeltypes.SUCCESS}, nil } // ChannelUpgradeConfirm defines a rpc handler method for MsgChannelUpgradeConfirm. -func (k *Keeper) ChannelUpgradeConfirm(goCtx context.Context, msg *channeltypes.MsgChannelUpgradeConfirm) (*channeltypes.MsgChannelUpgradeConfirmResponse, error) { - ctx := sdk.UnwrapSDKContext(goCtx) - +func (k *Keeper) ChannelUpgradeConfirm(ctx context.Context, msg *channeltypes.MsgChannelUpgradeConfirm) (*channeltypes.MsgChannelUpgradeConfirmResponse, error) { app, ok := k.PortKeeper.Route(msg.PortId) if !ok { err := errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.PortId) - ctx.Logger().Error("channel upgrade confirm failed", "port-id", msg.PortId, "error", err) + k.Logger.Error("channel upgrade confirm failed", "port-id", msg.PortId, "error", err) return nil, err } cbs, ok := app.(porttypes.UpgradableModule) if !ok { err := errorsmod.Wrapf(porttypes.ErrInvalidRoute, "upgrade route not found to portID: %s", msg.PortId) - ctx.Logger().Error("channel upgrade confirm failed", "port-id", msg.PortId, "error", err) + k.Logger.Error("channel upgrade confirm failed", "port-id", msg.PortId, "error", err) return nil, err } err := k.ChannelKeeper.ChanUpgradeConfirm(ctx, msg.PortId, msg.ChannelId, msg.CounterpartyChannelState, msg.CounterpartyUpgrade, msg.ProofChannel, msg.ProofUpgrade, msg.ProofHeight) if err != nil { - ctx.Logger().Error("channel upgrade confirm failed", "error", errorsmod.Wrap(err, "channel upgrade confirm failed")) + k.Logger.Error("channel upgrade confirm failed", "error", errorsmod.Wrap(err, "channel upgrade confirm failed")) if channeltypes.IsUpgradeError(err) { k.ChannelKeeper.MustAbortUpgrade(ctx, msg.PortId, msg.ChannelId, err) @@ -807,8 +790,10 @@ func (k *Keeper) ChannelUpgradeConfirm(goCtx context.Context, msg *channeltypes. } channel := k.ChannelKeeper.WriteUpgradeConfirmChannel(ctx, msg.PortId, msg.ChannelId, msg.CounterpartyUpgrade) - ctx.Logger().Info("channel upgrade confirm succeeded", "port-id", msg.PortId, "channel-id", msg.ChannelId) - keeper.EmitChannelUpgradeConfirmEvent(ctx, msg.PortId, msg.ChannelId, channel) + k.Logger.Info("channel upgrade confirm succeeded", "port-id", msg.PortId, "channel-id", msg.ChannelId) + if err := k.ChannelKeeper.EmitChannelUpgradeConfirmEvent(ctx, msg.PortId, msg.ChannelId, channel); err != nil { + return nil, errorsmod.Wrap(err, "event emission failed") + } // Move channel to OPEN state if both chains have finished flushing in-flight packets. // Counterparty channel state has been verified in ChanUpgradeConfirm. @@ -821,33 +806,33 @@ func (k *Keeper) ChannelUpgradeConfirm(goCtx context.Context, msg *channeltypes. cbs.OnChanUpgradeOpen(ctx, msg.PortId, msg.ChannelId, upgrade.Fields.Ordering, upgrade.Fields.ConnectionHops, upgrade.Fields.Version) channel := k.ChannelKeeper.WriteUpgradeOpenChannel(ctx, msg.PortId, msg.ChannelId) - ctx.Logger().Info("channel upgrade open succeeded", "port-id", msg.PortId, "channel-id", msg.ChannelId) - keeper.EmitChannelUpgradeOpenEvent(ctx, msg.PortId, msg.ChannelId, channel) + k.Logger.Info("channel upgrade open succeeded", "port-id", msg.PortId, "channel-id", msg.ChannelId) + if err := k.ChannelKeeper.EmitChannelUpgradeOpenEvent(ctx, msg.PortId, msg.ChannelId, channel); err != nil { + return nil, errorsmod.Wrap(err, "event emission failed") + } } return &channeltypes.MsgChannelUpgradeConfirmResponse{Result: channeltypes.SUCCESS}, nil } // ChannelUpgradeOpen defines a rpc handler method for MsgChannelUpgradeOpen. -func (k *Keeper) ChannelUpgradeOpen(goCtx context.Context, msg *channeltypes.MsgChannelUpgradeOpen) (*channeltypes.MsgChannelUpgradeOpenResponse, error) { - ctx := sdk.UnwrapSDKContext(goCtx) - +func (k *Keeper) ChannelUpgradeOpen(ctx context.Context, msg *channeltypes.MsgChannelUpgradeOpen) (*channeltypes.MsgChannelUpgradeOpenResponse, error) { app, ok := k.PortKeeper.Route(msg.PortId) if !ok { err := errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to portID: %s", msg.PortId) - ctx.Logger().Error("channel upgrade open failed", "port-id", msg.PortId, "error", err) + k.Logger.Error("channel upgrade open failed", "port-id", msg.PortId, "error", err) return nil, err } cbs, ok := app.(porttypes.UpgradableModule) if !ok { err := errorsmod.Wrapf(porttypes.ErrInvalidRoute, "upgrade route not found to portID: %s", msg.PortId) - ctx.Logger().Error("channel upgrade open failed", "port-id", msg.PortId, "error", err) + k.Logger.Error("channel upgrade open failed", "port-id", msg.PortId, "error", err) return nil, err } if err := k.ChannelKeeper.ChanUpgradeOpen(ctx, msg.PortId, msg.ChannelId, msg.CounterpartyChannelState, msg.CounterpartyUpgradeSequence, msg.ProofChannel, msg.ProofHeight); err != nil { - ctx.Logger().Error("channel upgrade open failed", "error", errorsmod.Wrap(err, "channel upgrade open failed")) + k.Logger.Error("channel upgrade open failed", "error", errorsmod.Wrap(err, "channel upgrade open failed")) return nil, errorsmod.Wrap(err, "channel upgrade open failed") } @@ -859,32 +844,32 @@ func (k *Keeper) ChannelUpgradeOpen(goCtx context.Context, msg *channeltypes.Msg cbs.OnChanUpgradeOpen(ctx, msg.PortId, msg.ChannelId, upgrade.Fields.Ordering, upgrade.Fields.ConnectionHops, upgrade.Fields.Version) channel := k.ChannelKeeper.WriteUpgradeOpenChannel(ctx, msg.PortId, msg.ChannelId) - ctx.Logger().Info("channel upgrade open succeeded", "port-id", msg.PortId, "channel-id", msg.ChannelId) - keeper.EmitChannelUpgradeOpenEvent(ctx, msg.PortId, msg.ChannelId, channel) + k.Logger.Info("channel upgrade open succeeded", "port-id", msg.PortId, "channel-id", msg.ChannelId) + if err := k.ChannelKeeper.EmitChannelUpgradeOpenEvent(ctx, msg.PortId, msg.ChannelId, channel); err != nil { + return nil, errorsmod.Wrap(err, "event emission failed") + } return &channeltypes.MsgChannelUpgradeOpenResponse{}, nil } // ChannelUpgradeTimeout defines a rpc handler method for MsgChannelUpgradeTimeout. -func (k *Keeper) ChannelUpgradeTimeout(goCtx context.Context, msg *channeltypes.MsgChannelUpgradeTimeout) (*channeltypes.MsgChannelUpgradeTimeoutResponse, error) { - ctx := sdk.UnwrapSDKContext(goCtx) - +func (k *Keeper) ChannelUpgradeTimeout(ctx context.Context, msg *channeltypes.MsgChannelUpgradeTimeout) (*channeltypes.MsgChannelUpgradeTimeoutResponse, error) { if err := k.ChannelKeeper.ChanUpgradeTimeout(ctx, msg.PortId, msg.ChannelId, msg.CounterpartyChannel, msg.ProofChannel, msg.ProofHeight); err != nil { return nil, errorsmod.Wrapf(err, "could not timeout upgrade for channel: %s", msg.ChannelId) } channel, upgrade := k.ChannelKeeper.WriteUpgradeTimeoutChannel(ctx, msg.PortId, msg.ChannelId) - ctx.Logger().Info("channel upgrade timeout callback succeeded: portID %s, channelID %s", msg.PortId, msg.ChannelId) - keeper.EmitChannelUpgradeTimeoutEvent(ctx, msg.PortId, msg.ChannelId, channel, upgrade) + k.Logger.Info("channel upgrade timeout callback succeeded: portID %s, channelID %s", msg.PortId, msg.ChannelId) + if err := k.ChannelKeeper.EmitChannelUpgradeTimeoutEvent(ctx, msg.PortId, msg.ChannelId, channel, upgrade); err != nil { + return nil, errorsmod.Wrap(err, "event emission failed") + } return &channeltypes.MsgChannelUpgradeTimeoutResponse{}, nil } // ChannelUpgradeCancel defines a rpc handler method for MsgChannelUpgradeCancel. -func (k *Keeper) ChannelUpgradeCancel(goCtx context.Context, msg *channeltypes.MsgChannelUpgradeCancel) (*channeltypes.MsgChannelUpgradeCancelResponse, error) { - ctx := sdk.UnwrapSDKContext(goCtx) - +func (k *Keeper) ChannelUpgradeCancel(ctx context.Context, msg *channeltypes.MsgChannelUpgradeCancel) (*channeltypes.MsgChannelUpgradeCancelResponse, error) { channel, found := k.ChannelKeeper.GetChannel(ctx, msg.PortId, msg.ChannelId) if !found { return nil, errorsmod.Wrapf(channeltypes.ErrChannelNotFound, "port ID (%s) channel ID (%s)", msg.PortId, msg.ChannelId) @@ -901,15 +886,17 @@ func (k *Keeper) ChannelUpgradeCancel(goCtx context.Context, msg *channeltypes.M k.ChannelKeeper.WriteUpgradeCancelChannel(ctx, msg.PortId, msg.ChannelId, channel.UpgradeSequence) - ctx.Logger().Info("channel upgrade cancel succeeded", "port-id", msg.PortId, "channel-id", msg.ChannelId) + k.Logger.Info("channel upgrade cancel succeeded", "port-id", msg.PortId, "channel-id", msg.ChannelId) - keeper.EmitChannelUpgradeCancelEvent(ctx, msg.PortId, msg.ChannelId, channel, upgrade) + if err := k.ChannelKeeper.EmitChannelUpgradeCancelEvent(ctx, msg.PortId, msg.ChannelId, channel, upgrade); err != nil { + return nil, errorsmod.Wrap(err, "event emission failed") + } return &channeltypes.MsgChannelUpgradeCancelResponse{}, nil } if err := k.ChannelKeeper.ChanUpgradeCancel(ctx, msg.PortId, msg.ChannelId, msg.ErrorReceipt, msg.ProofErrorReceipt, msg.ProofHeight); err != nil { - ctx.Logger().Error("channel upgrade cancel failed", "port-id", msg.PortId, "error", err.Error()) + k.Logger.Error("channel upgrade cancel failed", "port-id", msg.PortId, "error", err.Error()) return nil, errorsmod.Wrap(err, "channel upgrade cancel failed") } @@ -921,22 +908,23 @@ func (k *Keeper) ChannelUpgradeCancel(goCtx context.Context, msg *channeltypes.M k.ChannelKeeper.WriteUpgradeCancelChannel(ctx, msg.PortId, msg.ChannelId, msg.ErrorReceipt.Sequence) - ctx.Logger().Info("channel upgrade cancel succeeded", "port-id", msg.PortId, "channel-id", msg.ChannelId) + k.Logger.Info("channel upgrade cancel succeeded", "port-id", msg.PortId, "channel-id", msg.ChannelId) // get channel here again to get latest state after write channel, found = k.ChannelKeeper.GetChannel(ctx, msg.PortId, msg.ChannelId) if !found { return nil, errorsmod.Wrapf(channeltypes.ErrChannelNotFound, "port ID (%s) channel ID (%s)", msg.PortId, msg.ChannelId) } - keeper.EmitChannelUpgradeCancelEvent(ctx, msg.PortId, msg.ChannelId, channel, upgrade) + + if err := k.ChannelKeeper.EmitChannelUpgradeCancelEvent(ctx, msg.PortId, msg.ChannelId, channel, upgrade); err != nil { + return nil, errorsmod.Wrap(err, "event emission failed") + } return &channeltypes.MsgChannelUpgradeCancelResponse{}, nil } // PruneAcknowledgements defines a rpc handler method for MsgPruneAcknowledgements. -func (k *Keeper) PruneAcknowledgements(goCtx context.Context, msg *channeltypes.MsgPruneAcknowledgements) (*channeltypes.MsgPruneAcknowledgementsResponse, error) { - ctx := sdk.UnwrapSDKContext(goCtx) - +func (k *Keeper) PruneAcknowledgements(ctx context.Context, msg *channeltypes.MsgPruneAcknowledgements) (*channeltypes.MsgPruneAcknowledgementsResponse, error) { pruned, remaining, err := k.ChannelKeeper.PruneAcknowledgements(ctx, msg.PortId, msg.ChannelId, msg.Limit) if err != nil { return nil, err @@ -949,36 +937,33 @@ func (k *Keeper) PruneAcknowledgements(goCtx context.Context, msg *channeltypes. } // UpdateClientParams defines a rpc handler method for MsgUpdateParams. -func (k *Keeper) UpdateClientParams(goCtx context.Context, msg *clienttypes.MsgUpdateParams) (*clienttypes.MsgUpdateParamsResponse, error) { +func (k *Keeper) UpdateClientParams(ctx context.Context, msg *clienttypes.MsgUpdateParams) (*clienttypes.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.ClientKeeper.SetParams(ctx, msg.Params) return &clienttypes.MsgUpdateParamsResponse{}, nil } // UpdateConnectionParams defines a rpc handler method for MsgUpdateParams for the 03-connection submodule. -func (k *Keeper) UpdateConnectionParams(goCtx context.Context, msg *connectiontypes.MsgUpdateParams) (*connectiontypes.MsgUpdateParamsResponse, error) { +func (k *Keeper) UpdateConnectionParams(ctx context.Context, msg *connectiontypes.MsgUpdateParams) (*connectiontypes.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.ConnectionKeeper.SetParams(ctx, msg.Params) return &connectiontypes.MsgUpdateParamsResponse{}, nil } // UpdateChannelParams defines a rpc handler method for MsgUpdateParams. -func (k *Keeper) UpdateChannelParams(goCtx context.Context, msg *channeltypes.MsgUpdateParams) (*channeltypes.MsgUpdateParamsResponse, error) { +func (k *Keeper) UpdateChannelParams(ctx context.Context, msg *channeltypes.MsgUpdateParams) (*channeltypes.MsgUpdateParamsResponse, error) { if k.GetAuthority() != msg.Authority { return nil, errorsmod.Wrapf(ibcerrors.ErrUnauthorized, "expected %s, got %s", k.GetAuthority(), msg.Authority) } - ctx := sdk.UnwrapSDKContext(goCtx) k.ChannelKeeper.SetParams(ctx, msg.Params) return &channeltypes.MsgUpdateParamsResponse{}, nil diff --git a/modules/core/module.go b/modules/core/module.go index 04cde49d313..7f350ed69b6 100644 --- a/modules/core/module.go +++ b/modules/core/module.go @@ -182,7 +182,7 @@ func (AppModule) ConsensusVersion() uint64 { return 7 } // BeginBlock returns the begin blocker for the ibc module. func (am AppModule) BeginBlock(ctx context.Context) error { - ibcclient.BeginBlocker(sdk.UnwrapSDKContext(ctx), am.keeper.ClientKeeper) + ibcclient.BeginBlocker(ctx, am.keeper.ClientKeeper) return nil } diff --git a/modules/light-clients/07-tendermint/migrations/expected_keepers.go b/modules/light-clients/07-tendermint/migrations/expected_keepers.go index 48ff97e416d..b56b5c4171f 100644 --- a/modules/light-clients/07-tendermint/migrations/expected_keepers.go +++ b/modules/light-clients/07-tendermint/migrations/expected_keepers.go @@ -3,7 +3,6 @@ package migrations import ( "context" - "cosmossdk.io/log" storetypes "cosmossdk.io/store/types" "github.com/cosmos/ibc-go/v9/modules/core/exported" @@ -14,5 +13,4 @@ type ClientKeeper interface { GetClientState(ctx context.Context, clientID string) (exported.ClientState, bool) IterateClientStates(ctx context.Context, prefix []byte, cb func(string, exported.ClientState) bool) ClientStore(ctx context.Context, clientID string) storetypes.KVStore - Logger(ctx context.Context) log.Logger } diff --git a/modules/light-clients/07-tendermint/migrations/migrations.go b/modules/light-clients/07-tendermint/migrations/migrations.go index c5457f6e684..c5705f7a90c 100644 --- a/modules/light-clients/07-tendermint/migrations/migrations.go +++ b/modules/light-clients/07-tendermint/migrations/migrations.go @@ -6,6 +6,7 @@ import ( 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" @@ -41,7 +42,7 @@ func PruneExpiredConsensusStates(ctx context.Context, cdc codec.BinaryCodec, cli totalPruned += ibctm.PruneAllExpiredConsensusStates(ctx, clientStore, cdc, tmClientState) } - clientKeeper.Logger(ctx).Info("pruned expired tendermint consensus states", "total", totalPruned) + sdk.UnwrapSDKContext(ctx).Logger().Info("pruned expired tendermint consensus states", "total", totalPruned) return totalPruned, nil } diff --git a/modules/light-clients/08-wasm/testing/simapp/app.go b/modules/light-clients/08-wasm/testing/simapp/app.go index 360f7f91077..9e3022605c7 100644 --- a/modules/light-clients/08-wasm/testing/simapp/app.go +++ b/modules/light-clients/08-wasm/testing/simapp/app.go @@ -470,7 +470,11 @@ func NewSimApp( app.UpgradeKeeper = upgradekeeper.NewKeeper(runtime.NewEnvironment(runtime.NewKVStoreService(keys[upgradetypes.StoreKey]), logger.With(log.ModuleKey, "x/upgrade"), runtime.EnvWithMsgRouterService(app.MsgServiceRouter()), runtime.EnvWithQueryRouterService(app.GRPCQueryRouter())), skipUpgradeHeights, appCodec, homePath, app.BaseApp, govModuleAddr, app.ConsensusParamsKeeper) app.IBCKeeper = ibckeeper.NewKeeper( - appCodec, runtime.NewKVStoreService(keys[ibcexported.StoreKey]), app.GetSubspace(ibcexported.ModuleName), app.UpgradeKeeper, authtypes.NewModuleAddress(govtypes.ModuleName).String(), + appCodec, + runtime.NewEnvironment(runtime.NewKVStoreService(keys[ibcexported.StoreKey]), logger.With(log.ModuleKey, "x/ibc")), + app.GetSubspace(ibcexported.ModuleName), + app.UpgradeKeeper, + govModuleAddr, ) govRouter := govv1beta1.NewRouter() diff --git a/modules/light-clients/09-localhost/light_client_module.go b/modules/light-clients/09-localhost/light_client_module.go index d18cbce8eef..cadfa805896 100644 --- a/modules/light-clients/09-localhost/light_client_module.go +++ b/modules/light-clients/09-localhost/light_client_module.go @@ -4,7 +4,7 @@ import ( "bytes" "context" - corestore "cosmossdk.io/core/store" + "cosmossdk.io/core/appmodule" errorsmod "cosmossdk.io/errors" "github.com/cosmos/cosmos-sdk/codec" @@ -33,15 +33,15 @@ var _ exported.LightClientModule = (*LightClientModule)(nil) // LightClientModule implements the core IBC api.LightClientModule interface. type LightClientModule struct { - cdc codec.BinaryCodec - storeService corestore.KVStoreService + cdc codec.BinaryCodec + appmodule.Environment } // NewLightClientModule creates and returns a new 09-localhost LightClientModule. -func NewLightClientModule(cdc codec.BinaryCodec, storeService corestore.KVStoreService) *LightClientModule { +func NewLightClientModule(cdc codec.BinaryCodec, env appmodule.Environment) *LightClientModule { return &LightClientModule{ - cdc: cdc, - storeService: storeService, + cdc: cdc, + Environment: env, } } @@ -83,7 +83,7 @@ func (l LightClientModule) VerifyMembership( path exported.Path, value []byte, ) error { - ibcStore := l.storeService.OpenKVStore(ctx) + ibcStore := l.KVStoreService.OpenKVStore(ctx) // ensure the proof provided is the expected sentinel localhost client proof if !bytes.Equal(proof, SentinelProof) { @@ -127,7 +127,7 @@ func (l LightClientModule) VerifyNonMembership( proof []byte, path exported.Path, ) error { - ibcStore := l.storeService.OpenKVStore(ctx) + ibcStore := l.KVStoreService.OpenKVStore(ctx) // ensure the proof provided is the expected sentinel localhost client proof if !bytes.Equal(proof, SentinelProof) { diff --git a/simapp/app.go b/simapp/app.go index ff443f26b6f..bfbc247cc4a 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -486,7 +486,11 @@ func NewSimApp( app.UpgradeKeeper = upgradekeeper.NewKeeper(runtime.NewEnvironment(runtime.NewKVStoreService(keys[upgradetypes.StoreKey]), logger.With(log.ModuleKey, "x/upgrade"), runtime.EnvWithMsgRouterService(app.MsgServiceRouter()), runtime.EnvWithQueryRouterService(app.GRPCQueryRouter())), skipUpgradeHeights, appCodec, homePath, app.BaseApp, govModuleAddr, app.ConsensusParamsKeeper) app.IBCKeeper = ibckeeper.NewKeeper( - appCodec, runtime.NewKVStoreService(keys[ibcexported.StoreKey]), app.GetSubspace(ibcexported.ModuleName), app.UpgradeKeeper, govModuleAddr, + appCodec, + runtime.NewEnvironment(runtime.NewKVStoreService(keys[ibcexported.StoreKey]), logger.With(log.ModuleKey, "x/ibc")), + app.GetSubspace(ibcexported.ModuleName), + app.UpgradeKeeper, + govModuleAddr, ) govRouter := govv1beta1.NewRouter() diff --git a/testing/simapp/app.go b/testing/simapp/app.go index fb1c447346f..62e9095d4cf 100644 --- a/testing/simapp/app.go +++ b/testing/simapp/app.go @@ -456,7 +456,11 @@ func NewSimApp( app.UpgradeKeeper = upgradekeeper.NewKeeper(runtime.NewEnvironment(runtime.NewKVStoreService(keys[upgradetypes.StoreKey]), logger.With(log.ModuleKey, "x/upgrade"), runtime.EnvWithMsgRouterService(app.MsgServiceRouter()), runtime.EnvWithQueryRouterService(app.GRPCQueryRouter())), skipUpgradeHeights, appCodec, homePath, app.BaseApp, govModuleAddr, app.ConsensusParamsKeeper) app.IBCKeeper = ibckeeper.NewKeeper( - appCodec, runtime.NewKVStoreService(keys[ibcexported.StoreKey]), app.GetSubspace(ibcexported.ModuleName), app.UpgradeKeeper, govModuleAddr, + appCodec, + runtime.NewEnvironment(runtime.NewKVStoreService(keys[ibcexported.StoreKey]), logger.With(log.ModuleKey, "x/ibc")), + app.GetSubspace(ibcexported.ModuleName), + app.UpgradeKeeper, + govModuleAddr, ) govRouter := govv1beta1.NewRouter()