Skip to content

Commit

Permalink
Merge branch 'main' into aka/TestValidateHostGenesisState
Browse files Browse the repository at this point in the history
  • Loading branch information
damiannolan authored Dec 20, 2024
2 parents 0f80c15 + f5e1a4c commit 70f8c15
Show file tree
Hide file tree
Showing 33 changed files with 1,027 additions and 1,038 deletions.
6 changes: 5 additions & 1 deletion modules/apps/callbacks/testing/simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 10 additions & 2 deletions modules/core/02-client/abci.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand All @@ -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())
}
}
}
}
42 changes: 18 additions & 24 deletions modules/core/02-client/keeper/client.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
}
Expand All @@ -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
Expand All @@ -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
Expand All @@ -104,21 +100,20 @@ 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
// recover the subject client given a substitute client identifier. The light client implementation
// 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)
Expand All @@ -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)
}
189 changes: 99 additions & 90 deletions modules/core/02-client/keeper/events.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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()
Expand All @@ -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),
)
}
Loading

0 comments on commit 70f8c15

Please sign in to comment.