From a056ce064524a4bdb5daf8a5769bc43036c78b94 Mon Sep 17 00:00:00 2001 From: Tyler <48813565+technicallyty@users.noreply.github.com> Date: Tue, 21 Jan 2025 20:50:05 -0800 Subject: [PATCH 01/24] core modules --- modules/apps/27-interchain-accounts/module.go | 38 ++++++++------- modules/apps/29-fee/keeper/migrations.go | 6 ++- modules/apps/29-fee/module.go | 22 +++++---- modules/apps/transfer/keeper/migrations.go | 12 ++--- modules/apps/transfer/module.go | 33 ++++++++----- modules/core/02-client/keeper/migrations.go | 12 +++-- modules/core/02-client/migrations/v7/store.go | 27 ++++++----- .../core/03-connection/keeper/migrations.go | 8 ++-- modules/core/04-channel/keeper/migrations.go | 4 +- modules/core/module.go | 48 +++++++++++-------- 10 files changed, 121 insertions(+), 89 deletions(-) diff --git a/modules/apps/27-interchain-accounts/module.go b/modules/apps/27-interchain-accounts/module.go index 6e7b838184c..3f019f3588a 100644 --- a/modules/apps/27-interchain-accounts/module.go +++ b/modules/apps/27-interchain-accounts/module.go @@ -7,13 +7,14 @@ import ( "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" + "google.golang.org/grpc" "cosmossdk.io/core/appmodule" + appmodulev2 "cosmossdk.io/core/appmodule/v2" coreregistry "cosmossdk.io/core/registry" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" @@ -34,8 +35,8 @@ var ( _ module.AppModuleBasic = (*AppModule)(nil) _ module.AppModuleSimulation = (*AppModule)(nil) _ module.HasGenesis = (*AppModule)(nil) - _ module.HasServices = (*AppModule)(nil) _ appmodule.AppModule = (*AppModule)(nil) + _ appmodule.HasMigrations = (*AppModule)(nil) _ porttypes.IBCModule = (*host.IBCModule)(nil) ) @@ -116,28 +117,33 @@ func (AppModule) GetQueryCmd() *cobra.Command { return cli.GetQueryCmd() } -// RegisterServices registers module services -func (am AppModule) RegisterServices(cfg module.Configurator) { - if am.controllerKeeper != nil { - controllertypes.RegisterMsgServer(cfg.MsgServer(), controllerkeeper.NewMsgServerImpl(am.controllerKeeper)) - controllertypes.RegisterQueryServer(cfg.QueryServer(), am.controllerKeeper) - } - - if am.hostKeeper != nil { - hosttypes.RegisterMsgServer(cfg.MsgServer(), hostkeeper.NewMsgServerImpl(am.hostKeeper)) - hosttypes.RegisterQueryServer(cfg.QueryServer(), am.hostKeeper) - } - +func (am AppModule) RegisterMigrations(registrar appmodulev2.MigrationRegistrar) error { controllerMigrator := controllerkeeper.NewMigrator(am.controllerKeeper) hostMigrator := hostkeeper.NewMigrator(am.hostKeeper) - if err := cfg.RegisterMigration(types.ModuleName, 2, func(ctx sdk.Context) error { + if err := registrar.Register(types.ModuleName, 2, func(ctx context.Context) error { if err := hostMigrator.MigrateParams(ctx); err != nil { return err } return controllerMigrator.MigrateParams(ctx) }); err != nil { - panic(fmt.Errorf("failed to migrate interchainaccounts app from version 2 to 3 (self-managed params migration): %v", err)) + return fmt.Errorf("failed to migrate interchainaccounts app from version 2 to 3 (self-managed params migration): %w", err) + } + return nil +} + +// RegisterServices registers module services +func (am AppModule) RegisterServices(cfg grpc.ServiceRegistrar) error { + if am.controllerKeeper != nil { + controllertypes.RegisterMsgServer(cfg, controllerkeeper.NewMsgServerImpl(am.controllerKeeper)) + controllertypes.RegisterQueryServer(cfg, am.controllerKeeper) } + + if am.hostKeeper != nil { + hosttypes.RegisterMsgServer(cfg, hostkeeper.NewMsgServerImpl(am.hostKeeper)) + hosttypes.RegisterQueryServer(cfg, am.hostKeeper) + } + + return nil } // InitGenesis performs genesis initialization for the interchain accounts module. diff --git a/modules/apps/29-fee/keeper/migrations.go b/modules/apps/29-fee/keeper/migrations.go index 6bb05548796..0b9477b569d 100644 --- a/modules/apps/29-fee/keeper/migrations.go +++ b/modules/apps/29-fee/keeper/migrations.go @@ -1,6 +1,8 @@ package keeper import ( + "context" + storetypes "cosmossdk.io/store/types" "github.com/cosmos/cosmos-sdk/runtime" @@ -24,10 +26,10 @@ func NewMigrator(keeper Keeper) Migrator { // Migrate1to2 migrates ibc-fee module from ConsensusVersion 1 to 2 // by refunding leftover fees to the refund address. -func (m Migrator) Migrate1to2(ctx sdk.Context) error { +func (m Migrator) Migrate1to2(ctx context.Context) error { store := runtime.KVStoreAdapter(m.keeper.KVStoreService.OpenKVStore(ctx)) iterator := storetypes.KVStorePrefixIterator(store, []byte(types.FeesInEscrowPrefix)) - defer coretypes.LogDeferred(ctx.Logger(), func() error { return iterator.Close() }) + defer coretypes.LogDeferred(m.keeper.Logger, func() error { return iterator.Close() }) for ; iterator.Valid(); iterator.Next() { feesInEscrow := m.keeper.MustUnmarshalFees(iterator.Value()) diff --git a/modules/apps/29-fee/module.go b/modules/apps/29-fee/module.go index 058078cdc11..984810bc4c3 100644 --- a/modules/apps/29-fee/module.go +++ b/modules/apps/29-fee/module.go @@ -7,8 +7,10 @@ import ( "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" + "google.golang.org/grpc" "cosmossdk.io/core/appmodule" + appmodulev2 "cosmossdk.io/core/appmodule/v2" coreregistry "cosmossdk.io/core/registry" "github.com/cosmos/cosmos-sdk/client" @@ -27,7 +29,7 @@ var ( _ module.AppModuleSimulation = (*AppModule)(nil) _ module.HasGenesis = (*AppModule)(nil) _ appmodule.HasConsensusVersion = (*AppModule)(nil) - _ module.HasServices = (*AppModule)(nil) + _ appmodule.HasMigrations = (*AppModule)(nil) _ appmodule.AppModule = (*AppModule)(nil) ) @@ -100,15 +102,19 @@ func (AppModule) GetQueryCmd() *cobra.Command { return cli.GetQueryCmd() } -// RegisterServices registers module services. -func (am AppModule) RegisterServices(cfg module.Configurator) { - types.RegisterMsgServer(cfg.MsgServer(), am.keeper) - types.RegisterQueryServer(cfg.QueryServer(), am.keeper) - +func (am AppModule) RegisterMigrations(registrar appmodulev2.MigrationRegistrar) error { m := keeper.NewMigrator(am.keeper) - if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil { - panic(fmt.Errorf("failed to migrate ibc-fee module from version 1 to 2 (refund leftover fees): %v", err)) + if err := registrar.Register(types.ModuleName, 1, m.Migrate1to2); err != nil { + return fmt.Errorf("failed to migrate ibc-fee module from version 1 to 2 (refund leftover fees): %v", err) } + return nil +} + +// RegisterServices registers module services. +func (am AppModule) RegisterServices(cfg grpc.ServiceRegistrar) error { + types.RegisterMsgServer(cfg, am.keeper) + types.RegisterQueryServer(cfg, am.keeper) + return nil } // InitGenesis performs genesis initialization for the ibc-29-fee module. It returns diff --git a/modules/apps/transfer/keeper/migrations.go b/modules/apps/transfer/keeper/migrations.go index d276aef8091..9d793f33635 100644 --- a/modules/apps/transfer/keeper/migrations.go +++ b/modules/apps/transfer/keeper/migrations.go @@ -30,9 +30,9 @@ func NewMigrator(keeper Keeper) Migrator { } // MigrateParams migrates the transfer module's parameters from the x/params to self store. -func (m Migrator) MigrateParams(ctx sdk.Context) error { +func (m Migrator) MigrateParams(ctx context.Context) error { var params types.Params - m.keeper.legacySubspace.GetParamSet(ctx, ¶ms) + m.keeper.legacySubspace.GetParamSet(sdk.UnwrapSDKContext(ctx), ¶ms) m.keeper.SetParams(ctx, params) m.keeper.Logger.Info("successfully migrated transfer app self-manage params") @@ -40,7 +40,7 @@ func (m Migrator) MigrateParams(ctx sdk.Context) error { } // MigrateDenomMetadata sets token metadata for all the IBC denom traces -func (m Migrator) MigrateDenomMetadata(ctx sdk.Context) error { +func (m Migrator) MigrateDenomMetadata(ctx context.Context) error { m.keeper.iterateDenomTraces(ctx, func(dt internaltypes.DenomTrace) (stop bool) { // check if the metadata for the given denom trace does not already exist @@ -55,7 +55,7 @@ func (m Migrator) MigrateDenomMetadata(ctx sdk.Context) error { } // MigrateTotalEscrowForDenom migrates the total amount of source chain tokens in escrow. -func (m Migrator) MigrateTotalEscrowForDenom(ctx sdk.Context) error { +func (m Migrator) MigrateTotalEscrowForDenom(ctx context.Context) error { var totalEscrowed sdk.Coins portID := m.keeper.GetPort(ctx) @@ -76,7 +76,7 @@ func (m Migrator) MigrateTotalEscrowForDenom(ctx sdk.Context) error { } // MigrateDenomTraceToDenom migrates storage from using DenomTrace to Denom. -func (m Migrator) MigrateDenomTraceToDenom(ctx sdk.Context) error { +func (m Migrator) MigrateDenomTraceToDenom(ctx context.Context) error { var ( denoms []types.Denom denomTraces []internaltypes.DenomTrace @@ -147,7 +147,7 @@ func (k Keeper) iterateDenomTraces(ctx context.Context, cb func(denomTrace inter } // setDenomMetadataWithDenomTrace sets an IBC token's denomination metadata -func (k Keeper) setDenomMetadataWithDenomTrace(ctx sdk.Context, denomTrace internaltypes.DenomTrace) { +func (k Keeper) setDenomMetadataWithDenomTrace(ctx context.Context, denomTrace internaltypes.DenomTrace) { metadata := banktypes.Metadata{ Description: fmt.Sprintf("IBC token from %s", denomTrace.GetFullDenomPath()), DenomUnits: []*banktypes.DenomUnit{ diff --git a/modules/apps/transfer/module.go b/modules/apps/transfer/module.go index 7b1225a873f..9ff555d6532 100644 --- a/modules/apps/transfer/module.go +++ b/modules/apps/transfer/module.go @@ -7,8 +7,10 @@ import ( "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" + "google.golang.org/grpc" "cosmossdk.io/core/appmodule" + appmodulev2 "cosmossdk.io/core/appmodule/v2" coreregistry "cosmossdk.io/core/registry" "github.com/cosmos/cosmos-sdk/client" @@ -31,8 +33,8 @@ var ( _ module.HasGenesis = (*AppModule)(nil) _ appmodule.HasConsensusVersion = (*AppModule)(nil) _ module.HasInvariants = (*AppModule)(nil) - _ module.HasServices = (*AppModule)(nil) _ appmodule.AppModule = (*AppModule)(nil) + _ appmodule.HasMigrations = (*AppModule)(nil) _ porttypes.IBCModule = (*IBCModule)(nil) ) @@ -115,27 +117,32 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { } // RegisterServices registers module services. -func (am AppModule) RegisterServices(cfg module.Configurator) { - types.RegisterMsgServer(cfg.MsgServer(), am.keeper) - types.RegisterQueryServer(cfg.QueryServer(), am.keeper) - types.RegisterQueryV2Server(cfg.QueryServer(), am.keeper) +func (am AppModule) RegisterServices(cfg grpc.ServiceRegistrar) error { + types.RegisterMsgServer(cfg, am.keeper) + types.RegisterQueryServer(cfg, am.keeper) + types.RegisterQueryV2Server(cfg, am.keeper) + return nil +} +func (am AppModule) RegisterMigrations(registrar appmodulev2.MigrationRegistrar) error { m := keeper.NewMigrator(am.keeper) - if err := cfg.RegisterMigration(types.ModuleName, 2, m.MigrateTotalEscrowForDenom); err != nil { - panic(fmt.Errorf("failed to migrate transfer app from version 2 to 3 (total escrow entry migration): %v", err)) + if err := registrar.Register(types.ModuleName, 2, m.MigrateTotalEscrowForDenom); err != nil { + return fmt.Errorf("failed to migrate transfer app from version 2 to 3 (total escrow entry migration): %w", err) } - if err := cfg.RegisterMigration(types.ModuleName, 3, m.MigrateParams); err != nil { - panic(fmt.Errorf("failed to migrate transfer app version 3 to 4 (self-managed params migration): %v", err)) + if err := registrar.Register(types.ModuleName, 3, m.MigrateParams); err != nil { + return fmt.Errorf("failed to migrate transfer app version 3 to 4 (self-managed params migration): %w", err) } - if err := cfg.RegisterMigration(types.ModuleName, 4, m.MigrateDenomMetadata); err != nil { - panic(fmt.Errorf("failed to migrate transfer app from version 4 to 5 (set denom metadata migration): %v", err)) + if err := registrar.Register(types.ModuleName, 4, m.MigrateDenomMetadata); err != nil { + return fmt.Errorf("failed to migrate transfer app from version 4 to 5 (set denom metadata migration): %w", err) } - if err := cfg.RegisterMigration(types.ModuleName, 5, m.MigrateDenomTraceToDenom); err != nil { - panic(fmt.Errorf("failed to migrate transfer app from version 5 to 6 (migrate DenomTrace to Denom): %v", err)) + if err := registrar.Register(types.ModuleName, 5, m.MigrateDenomTraceToDenom); err != nil { + return fmt.Errorf("failed to migrate transfer app from version 5 to 6 (migrate DenomTrace to Denom): %w", err) } + + return nil } // InitGenesis performs genesis initialization for the ibc-transfer module. It returns diff --git a/modules/core/02-client/keeper/migrations.go b/modules/core/02-client/keeper/migrations.go index a66b783f522..5fae9dc8fdd 100644 --- a/modules/core/02-client/keeper/migrations.go +++ b/modules/core/02-client/keeper/migrations.go @@ -1,6 +1,8 @@ package keeper import ( + "context" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/ibc-go/v9/modules/core/02-client/migrations/v7" @@ -25,16 +27,16 @@ func NewMigrator(keeper *Keeper) Migrator { // - prunes solo machine consensus states // - 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.KVStoreService, m.keeper.cdc, m.keeper) +func (m Migrator) Migrate2to3(ctx context.Context) error { + return v7.MigrateStore(ctx, m.keeper.Logger, m.keeper.KVStoreService, m.keeper.cdc, m.keeper) } // MigrateParams migrates from consensus version 4 to 5. // This migration takes the parameters that are currently stored and managed by x/params // and stores them directly in the ibc module's state. -func (m Migrator) MigrateParams(ctx sdk.Context) error { +func (m Migrator) MigrateParams(ctx context.Context) error { var params types.Params - m.keeper.legacySubspace.GetParamSet(ctx, ¶ms) + m.keeper.legacySubspace.GetParamSet(sdk.UnwrapSDKContext(ctx), ¶ms) if err := params.Validate(); err != nil { return err } @@ -46,7 +48,7 @@ func (m Migrator) MigrateParams(ctx sdk.Context) error { // MigrateToStatelessLocalhost deletes the localhost client state. The localhost // implementation is now stateless. -func (m Migrator) MigrateToStatelessLocalhost(ctx sdk.Context) error { +func (m Migrator) MigrateToStatelessLocalhost(ctx context.Context) error { clientStore := m.keeper.ClientStore(ctx, exported.LocalhostClientID) // delete the client state diff --git a/modules/core/02-client/migrations/v7/store.go b/modules/core/02-client/migrations/v7/store.go index bf9e44656b9..4ab12059fa5 100644 --- a/modules/core/02-client/migrations/v7/store.go +++ b/modules/core/02-client/migrations/v7/store.go @@ -1,8 +1,10 @@ package v7 import ( + "context" "strings" + "cosmossdk.io/core/log" corestore "cosmossdk.io/core/store" errorsmod "cosmossdk.io/errors" storetypes "cosmossdk.io/store/types" @@ -10,7 +12,6 @@ import ( "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/runtime" - sdk "github.com/cosmos/cosmos-sdk/types" clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types" host "github.com/cosmos/ibc-go/v9/modules/core/24-host" @@ -32,24 +33,24 @@ const Localhost string = "09-localhost" // - Pruning all solo machine consensus states // - Removing the localhost client // - Asserting existing tendermint clients are properly registered on the chain codec -func MigrateStore(ctx sdk.Context, storeService corestore.KVStoreService, cdc codec.BinaryCodec, clientKeeper ClientKeeper) error { +func MigrateStore(ctx context.Context, logger log.Logger, storeService corestore.KVStoreService, cdc codec.BinaryCodec, clientKeeper ClientKeeper) error { store := runtime.KVStoreAdapter(storeService.OpenKVStore(ctx)) - if err := handleSolomachineMigration(ctx, store, cdc, clientKeeper); err != nil { + if err := handleSolomachineMigration(ctx, logger, store, cdc, clientKeeper); err != nil { return err } - if err := handleTendermintMigration(ctx, store, clientKeeper); err != nil { + if err := handleTendermintMigration(ctx, logger, store, clientKeeper); err != nil { return err } - return handleLocalhostMigration(ctx, store, clientKeeper) + return handleLocalhostMigration(ctx, logger, store, clientKeeper) } // handleSolomachineMigration iterates over the solo machine clients and migrates client state from // protobuf definition v2 to v3. All consensus states stored outside of the client state are pruned. -func handleSolomachineMigration(ctx sdk.Context, store storetypes.KVStore, cdc codec.BinaryCodec, clientKeeper ClientKeeper) error { - clients, err := collectClients(ctx, store, exported.Solomachine) +func handleSolomachineMigration(ctx context.Context, logger log.Logger, store storetypes.KVStore, cdc codec.BinaryCodec, clientKeeper ClientKeeper) error { + clients, err := collectClients(logger, store, exported.Solomachine) if err != nil { return err } @@ -85,8 +86,8 @@ func handleSolomachineMigration(ctx sdk.Context, store storetypes.KVStore, cdc c // handleTendermintMigration asserts that the tendermint client in state can be decoded properly. // This ensures the upgrading chain properly registered the tendermint client types on the chain codec. -func handleTendermintMigration(ctx sdk.Context, store storetypes.KVStore, clientKeeper ClientKeeper) error { - clients, err := collectClients(ctx, store, exported.Tendermint) +func handleTendermintMigration(ctx context.Context, logger log.Logger, store storetypes.KVStore, clientKeeper ClientKeeper) error { + clients, err := collectClients(logger, store, exported.Tendermint) if err != nil { return err } @@ -117,8 +118,8 @@ func handleTendermintMigration(ctx sdk.Context, store storetypes.KVStore, client } // handleLocalhostMigration removes all client and consensus states associated with the localhost client type. -func handleLocalhostMigration(ctx sdk.Context, store storetypes.KVStore, clientKeeper ClientKeeper) error { - clients, err := collectClients(ctx, store, Localhost) +func handleLocalhostMigration(ctx context.Context, logger log.Logger, store storetypes.KVStore, clientKeeper ClientKeeper) error { + clients, err := collectClients(logger, store, Localhost) if err != nil { return err } @@ -140,11 +141,11 @@ func handleLocalhostMigration(ctx sdk.Context, store storetypes.KVStore, clientK // avoid state corruption as modifying state during iteration is unsafe. A special case // for tendermint clients is included as only one tendermint clientID is required for // v7 migrations. -func collectClients(ctx sdk.Context, store storetypes.KVStore, clientType string) (clients []string, err error) { +func collectClients(logger log.Logger, store storetypes.KVStore, clientType string) (clients []string, err error) { clientPrefix := host.PrefixedClientStoreKey([]byte(clientType)) iterator := storetypes.KVStorePrefixIterator(store, clientPrefix) - defer coretypes.LogDeferred(ctx.Logger(), func() error { return iterator.Close() }) + defer coretypes.LogDeferred(logger, func() error { return iterator.Close() }) for ; iterator.Valid(); iterator.Next() { path := string(iterator.Key()) if !strings.Contains(path, host.KeyClientState) { diff --git a/modules/core/03-connection/keeper/migrations.go b/modules/core/03-connection/keeper/migrations.go index 83503ebf971..5220d00669f 100644 --- a/modules/core/03-connection/keeper/migrations.go +++ b/modules/core/03-connection/keeper/migrations.go @@ -1,6 +1,8 @@ package keeper import ( + "context" + sdk "github.com/cosmos/cosmos-sdk/types" connectionv7 "github.com/cosmos/ibc-go/v9/modules/core/03-connection/migrations/v7" @@ -19,7 +21,7 @@ func NewMigrator(keeper *Keeper) Migrator { // Migrate3to4 migrates from version 3 to 4. // This migration writes the sentinel localhost connection end to state. -func (m Migrator) Migrate3to4(ctx sdk.Context) error { +func (m Migrator) Migrate3to4(ctx context.Context) error { connectionv7.MigrateLocalhostConnection(ctx, m.keeper) return nil } @@ -27,9 +29,9 @@ func (m Migrator) Migrate3to4(ctx sdk.Context) error { // MigrateParams migrates from consensus version 4 to 5. // This migration takes the parameters that are currently stored and managed by x/params // and stores them directly in the ibc module's state. -func (m Migrator) MigrateParams(ctx sdk.Context) error { +func (m Migrator) MigrateParams(ctx context.Context) error { var params types.Params - m.keeper.legacySubspace.GetParamSet(ctx, ¶ms) + m.keeper.legacySubspace.GetParamSet(sdk.UnwrapSDKContext(ctx), ¶ms) if err := params.Validate(); err != nil { return err } diff --git a/modules/core/04-channel/keeper/migrations.go b/modules/core/04-channel/keeper/migrations.go index c83cf865022..23d9cc1f231 100644 --- a/modules/core/04-channel/keeper/migrations.go +++ b/modules/core/04-channel/keeper/migrations.go @@ -1,7 +1,7 @@ package keeper import ( - sdk "github.com/cosmos/cosmos-sdk/types" + "context" channeltypes "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types" ) @@ -17,7 +17,7 @@ func NewMigrator(keeper *Keeper) Migrator { } // MigrateParams migrates params to the default channel params. -func (m Migrator) MigrateParams(ctx sdk.Context) error { +func (m Migrator) MigrateParams(ctx context.Context) error { params := channeltypes.DefaultParams() m.keeper.SetParams(ctx, params) m.keeper.Logger.Info("successfully migrated ibc channel params") diff --git a/modules/core/module.go b/modules/core/module.go index 7f350ed69b6..ae89813824c 100644 --- a/modules/core/module.go +++ b/modules/core/module.go @@ -7,13 +7,14 @@ import ( "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" + "google.golang.org/grpc" "cosmossdk.io/core/appmodule" + appmodulev2 "cosmossdk.io/core/appmodule/v2" coreregistry "cosmossdk.io/core/registry" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" @@ -37,9 +38,9 @@ var ( _ module.AppModuleSimulation = (*AppModule)(nil) _ module.HasGenesis = (*AppModule)(nil) _ appmodule.HasConsensusVersion = (*AppModule)(nil) - _ module.HasServices = (*AppModule)(nil) _ appmodule.AppModule = (*AppModule)(nil) _ appmodule.HasBeginBlocker = (*AppModule)(nil) + _ appmodule.HasMigrations = (*AppModule)(nil) ) // AppModule implements an application module for the ibc module. @@ -117,43 +118,48 @@ func (AppModule) RegisterInterfaces(registry coreregistry.InterfaceRegistrar) { types.RegisterInterfaces(registry) } -// RegisterServices registers module services. -func (am AppModule) RegisterServices(cfg module.Configurator) { - clienttypes.RegisterMsgServer(cfg.MsgServer(), am.keeper) - connectiontypes.RegisterMsgServer(cfg.MsgServer(), am.keeper) - channeltypes.RegisterMsgServer(cfg.MsgServer(), am.keeper) - clienttypes.RegisterQueryServer(cfg.QueryServer(), clientkeeper.NewQueryServer(am.keeper.ClientKeeper)) - connectiontypes.RegisterQueryServer(cfg.QueryServer(), connectionkeeper.NewQueryServer(am.keeper.ConnectionKeeper)) - channeltypes.RegisterQueryServer(cfg.QueryServer(), channelkeeper.NewQueryServer(am.keeper.ChannelKeeper)) - +func (am AppModule) RegisterMigrations(registrar appmodulev2.MigrationRegistrar) error { clientMigrator := clientkeeper.NewMigrator(am.keeper.ClientKeeper) - if err := cfg.RegisterMigration(exported.ModuleName, 2, clientMigrator.Migrate2to3); err != nil { - panic(err) + if err := registrar.Register(exported.ModuleName, 2, clientMigrator.Migrate2to3); err != nil { + return err } connectionMigrator := connectionkeeper.NewMigrator(am.keeper.ConnectionKeeper) - if err := cfg.RegisterMigration(exported.ModuleName, 3, connectionMigrator.Migrate3to4); err != nil { - panic(err) + if err := registrar.Register(exported.ModuleName, 3, connectionMigrator.Migrate3to4); err != nil { + return err } - if err := cfg.RegisterMigration(exported.ModuleName, 4, func(ctx sdk.Context) error { + if err := registrar.Register(exported.ModuleName, 4, func(ctx context.Context) error { if err := clientMigrator.MigrateParams(ctx); err != nil { return err } return connectionMigrator.MigrateParams(ctx) }); err != nil { - panic(err) + return err } channelMigrator := channelkeeper.NewMigrator(am.keeper.ChannelKeeper) - if err := cfg.RegisterMigration(exported.ModuleName, 5, channelMigrator.MigrateParams); err != nil { - panic(err) + if err := registrar.Register(exported.ModuleName, 5, channelMigrator.MigrateParams); err != nil { + return err } - if err := cfg.RegisterMigration(exported.ModuleName, 6, clientMigrator.MigrateToStatelessLocalhost); err != nil { - panic(err) + if err := registrar.Register(exported.ModuleName, 6, clientMigrator.MigrateToStatelessLocalhost); err != nil { + return err } + + return nil +} + +// RegisterServices registers module services. +func (am AppModule) RegisterServices(cfg grpc.ServiceRegistrar) error { + clienttypes.RegisterMsgServer(cfg, am.keeper) + connectiontypes.RegisterMsgServer(cfg, am.keeper) + channeltypes.RegisterMsgServer(cfg, am.keeper) + clienttypes.RegisterQueryServer(cfg, clientkeeper.NewQueryServer(am.keeper.ClientKeeper)) + connectiontypes.RegisterQueryServer(cfg, connectionkeeper.NewQueryServer(am.keeper.ConnectionKeeper)) + channeltypes.RegisterQueryServer(cfg, channelkeeper.NewQueryServer(am.keeper.ChannelKeeper)) + return nil } // InitGenesis performs genesis initialization for the ibc module. It returns From 0953f74c819806a56c424814c2d28fdbc9626e5a Mon Sep 17 00:00:00 2001 From: Tyler <48813565+technicallyty@users.noreply.github.com> Date: Wed, 22 Jan 2025 09:21:42 -0800 Subject: [PATCH 02/24] fmt --- modules/apps/27-interchain-accounts/module.go | 3 +-- modules/apps/29-fee/module.go | 7 +++---- modules/apps/transfer/module.go | 3 +-- modules/core/module.go | 2 +- 4 files changed, 6 insertions(+), 9 deletions(-) diff --git a/modules/apps/27-interchain-accounts/module.go b/modules/apps/27-interchain-accounts/module.go index 2f2837fbea2..3b1c8af290c 100644 --- a/modules/apps/27-interchain-accounts/module.go +++ b/modules/apps/27-interchain-accounts/module.go @@ -35,13 +35,12 @@ var ( _ appmodule.AppModule = (*AppModule)(nil) _ appmodule.HasConsensusVersion = (*AppModule)(nil) _ appmodule.HasRegisterInterfaces = (*AppModule)(nil) - _ appmodule.HasMigrations = (*AppModule)(nil) + _ appmodule.HasMigrations = (*AppModule)(nil) _ module.AppModule = (*AppModule)(nil) _ module.HasGenesis = (*AppModule)(nil) _ module.HasGRPCGateway = (*AppModule)(nil) - // Sims _ module.AppModuleSimulation = (*AppModule)(nil) _ module.HasLegacyProposalMsgs = (*AppModule)(nil) diff --git a/modules/apps/29-fee/module.go b/modules/apps/29-fee/module.go index bf9651a8a92..1c43cf08b43 100644 --- a/modules/apps/29-fee/module.go +++ b/modules/apps/29-fee/module.go @@ -27,11 +27,10 @@ var ( _ appmodule.HasConsensusVersion = (*AppModule)(nil) _ appmodule.HasAminoCodec = (*AppModule)(nil) _ appmodule.HasRegisterInterfaces = (*AppModule)(nil) - _ appmodule.HasMigrations = (*AppModule)(nil) + _ appmodule.HasMigrations = (*AppModule)(nil) - - _ module.AppModule = (*AppModule)(nil) - _ module.HasGenesis = (*AppModule)(nil) + _ module.AppModule = (*AppModule)(nil) + _ module.HasGenesis = (*AppModule)(nil) _ autocli.HasCustomTxCommand = (*AppModule)(nil) _ autocli.HasCustomQueryCommand = (*AppModule)(nil) diff --git a/modules/apps/transfer/module.go b/modules/apps/transfer/module.go index bb2de1946f6..ac035ae72d4 100644 --- a/modules/apps/transfer/module.go +++ b/modules/apps/transfer/module.go @@ -30,8 +30,7 @@ var ( _ appmodule.AppModule = (*AppModule)(nil) _ appmodule.HasConsensusVersion = (*AppModule)(nil) _ appmodule.HasRegisterInterfaces = (*AppModule)(nil) - _ appmodule.HasMigrations = (*AppModule)(nil) - + _ appmodule.HasMigrations = (*AppModule)(nil) _ module.AppModule = (*AppModule)(nil) _ module.HasGRPCGateway = (*AppModule)(nil) diff --git a/modules/core/module.go b/modules/core/module.go index 721dbe4f68e..0f3c75e7a49 100644 --- a/modules/core/module.go +++ b/modules/core/module.go @@ -38,7 +38,7 @@ var ( _ appmodule.HasBeginBlocker = (*AppModule)(nil) _ appmodule.HasConsensusVersion = (*AppModule)(nil) _ appmodule.HasRegisterInterfaces = (*AppModule)(nil) - _ appmodule.HasMigrations = (*AppModule)(nil) + _ appmodule.HasMigrations = (*AppModule)(nil) _ module.AppModule = (*AppModule)(nil) _ module.HasGRPCGateway = (*AppModule)(nil) From 09fac25a34600cd3e1e3daab0856966435fdf4e8 Mon Sep 17 00:00:00 2001 From: Tyler <48813565+technicallyty@users.noreply.github.com> Date: Thu, 23 Jan 2025 10:13:13 -0800 Subject: [PATCH 03/24] fix broken tests --- modules/core/02-client/migrations/v7/genesis_test.go | 4 +++- modules/core/02-client/migrations/v7/store_test.go | 6 ++++-- modules/core/migrations/v7/genesis_test.go | 4 +++- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/modules/core/02-client/migrations/v7/genesis_test.go b/modules/core/02-client/migrations/v7/genesis_test.go index c674e7d9680..e063b742dc5 100644 --- a/modules/core/02-client/migrations/v7/genesis_test.go +++ b/modules/core/02-client/migrations/v7/genesis_test.go @@ -12,6 +12,8 @@ import ( host "github.com/cosmos/ibc-go/v9/modules/core/24-host" ibcexported "github.com/cosmos/ibc-go/v9/modules/core/exported" ibctesting "github.com/cosmos/ibc-go/v9/testing" + + "cosmossdk.io/log" ) func (suite *MigrationsV7TestSuite) TestMigrateGenesisSolomachine() { @@ -107,7 +109,7 @@ func (suite *MigrationsV7TestSuite) TestMigrateGenesisSolomachine() { // migrate store get expected genesis // store migration and genesis migration should produce identical results // NOTE: tendermint clients are not pruned in genesis so the test should not have expired tendermint clients - err = v7.MigrateStore(suite.chainA.GetContext(), runtime.NewKVStoreService(suite.chainA.GetSimApp().GetKey(ibcexported.StoreKey)), suite.chainA.App.AppCodec(), suite.chainA.GetSimApp().IBCKeeper.ClientKeeper) + err = v7.MigrateStore(suite.chainA.GetContext(), log.NewTestLogger(suite.T()), runtime.NewKVStoreService(suite.chainA.GetSimApp().GetKey(ibcexported.StoreKey)), suite.chainA.App.AppCodec(), suite.chainA.GetSimApp().IBCKeeper.ClientKeeper) suite.Require().NoError(err) expectedClientGenState, err := ibcclient.ExportGenesis(suite.chainA.GetContext(), suite.chainA.App.GetIBCKeeper().ClientKeeper) suite.Require().NoError(err) diff --git a/modules/core/02-client/migrations/v7/store_test.go b/modules/core/02-client/migrations/v7/store_test.go index f9e104e823f..6682bada40d 100644 --- a/modules/core/02-client/migrations/v7/store_test.go +++ b/modules/core/02-client/migrations/v7/store_test.go @@ -14,6 +14,8 @@ import ( host "github.com/cosmos/ibc-go/v9/modules/core/24-host" ibcexported "github.com/cosmos/ibc-go/v9/modules/core/exported" ibctesting "github.com/cosmos/ibc-go/v9/testing" + + "cosmossdk.io/log" ) // numCreations is the number of clients/consensus states created for @@ -62,7 +64,7 @@ func (suite *MigrationsV7TestSuite) TestMigrateStore() { suite.createSolomachineClients(solomachines) suite.createLocalhostClients() - err := v7.MigrateStore(suite.chainA.GetContext(), runtime.NewKVStoreService(suite.chainA.GetSimApp().GetKey(ibcexported.StoreKey)), suite.chainA.App.AppCodec(), suite.chainA.GetSimApp().IBCKeeper.ClientKeeper) + err := v7.MigrateStore(suite.chainA.GetContext(), log.NewTestLogger(suite.T()), runtime.NewKVStoreService(suite.chainA.GetSimApp().GetKey(ibcexported.StoreKey)), suite.chainA.App.AppCodec(), suite.chainA.GetSimApp().IBCKeeper.ClientKeeper) suite.Require().NoError(err) suite.assertSolomachineClients(solomachines) @@ -78,7 +80,7 @@ func (suite *MigrationsV7TestSuite) TestMigrateStoreNoTendermintClients() { suite.createSolomachineClients(solomachines) suite.createLocalhostClients() - err := v7.MigrateStore(suite.chainA.GetContext(), runtime.NewKVStoreService(suite.chainA.GetSimApp().GetKey(ibcexported.StoreKey)), suite.chainA.App.AppCodec(), suite.chainA.GetSimApp().IBCKeeper.ClientKeeper) + err := v7.MigrateStore(suite.chainA.GetContext(), log.NewTestLogger(suite.T()), runtime.NewKVStoreService(suite.chainA.GetSimApp().GetKey(ibcexported.StoreKey)), suite.chainA.App.AppCodec(), suite.chainA.GetSimApp().IBCKeeper.ClientKeeper) suite.Require().NoError(err) suite.assertSolomachineClients(solomachines) diff --git a/modules/core/migrations/v7/genesis_test.go b/modules/core/migrations/v7/genesis_test.go index fd5d6add051..f893a395140 100644 --- a/modules/core/migrations/v7/genesis_test.go +++ b/modules/core/migrations/v7/genesis_test.go @@ -18,6 +18,8 @@ import ( v7 "github.com/cosmos/ibc-go/v9/modules/core/migrations/v7" "github.com/cosmos/ibc-go/v9/modules/core/types" ibctesting "github.com/cosmos/ibc-go/v9/testing" + + "cosmossdk.io/log" ) type MigrationsV7TestSuite struct { @@ -136,7 +138,7 @@ func (suite *MigrationsV7TestSuite) TestMigrateGenesisSolomachine() { // migrate store get expected genesis // store migration and genesis migration should produce identical results // NOTE: tendermint clients are not pruned in genesis so the test should not have expired tendermint clients - err = clientv7.MigrateStore(suite.chainA.GetContext(), runtime.NewKVStoreService(suite.chainA.GetSimApp().GetKey(ibcexported.StoreKey)), suite.chainA.App.AppCodec(), suite.chainA.GetSimApp().IBCKeeper.ClientKeeper) + err = clientv7.MigrateStore(suite.chainA.GetContext(), log.NewTestLogger(suite.T()), runtime.NewKVStoreService(suite.chainA.GetSimApp().GetKey(ibcexported.StoreKey)), suite.chainA.App.AppCodec(), suite.chainA.GetSimApp().IBCKeeper.ClientKeeper) suite.Require().NoError(err) expectedClientGenState, err := ibcclient.ExportGenesis(suite.chainA.GetContext(), suite.chainA.App.GetIBCKeeper().ClientKeeper) suite.Require().NoError(err) From 9c8ab60816114c8abd3640064ee5a6a917820e1b Mon Sep 17 00:00:00 2001 From: Tyler <48813565+technicallyty@users.noreply.github.com> Date: Thu, 23 Jan 2025 10:25:00 -0800 Subject: [PATCH 04/24] lint fix --- modules/apps/29-fee/module.go | 1 + modules/core/02-client/migrations/v7/genesis_test.go | 4 ++-- modules/core/02-client/migrations/v7/store_test.go | 4 ++-- modules/core/migrations/v7/genesis_test.go | 4 ++-- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/modules/apps/29-fee/module.go b/modules/apps/29-fee/module.go index 1c43cf08b43..aa162adf7d0 100644 --- a/modules/apps/29-fee/module.go +++ b/modules/apps/29-fee/module.go @@ -17,6 +17,7 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/types/module" + "github.com/cosmos/ibc-go/v9/modules/apps/29-fee/client/cli" "github.com/cosmos/ibc-go/v9/modules/apps/29-fee/keeper" "github.com/cosmos/ibc-go/v9/modules/apps/29-fee/types" diff --git a/modules/core/02-client/migrations/v7/genesis_test.go b/modules/core/02-client/migrations/v7/genesis_test.go index e063b742dc5..b0207d5cdf3 100644 --- a/modules/core/02-client/migrations/v7/genesis_test.go +++ b/modules/core/02-client/migrations/v7/genesis_test.go @@ -3,6 +3,8 @@ package v7_test import ( "encoding/json" + "cosmossdk.io/log" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/runtime" @@ -12,8 +14,6 @@ import ( host "github.com/cosmos/ibc-go/v9/modules/core/24-host" ibcexported "github.com/cosmos/ibc-go/v9/modules/core/exported" ibctesting "github.com/cosmos/ibc-go/v9/testing" - - "cosmossdk.io/log" ) func (suite *MigrationsV7TestSuite) TestMigrateGenesisSolomachine() { diff --git a/modules/core/02-client/migrations/v7/store_test.go b/modules/core/02-client/migrations/v7/store_test.go index 6682bada40d..223ad91987a 100644 --- a/modules/core/02-client/migrations/v7/store_test.go +++ b/modules/core/02-client/migrations/v7/store_test.go @@ -6,6 +6,8 @@ import ( testifysuite "github.com/stretchr/testify/suite" + "cosmossdk.io/log" + "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/runtime" @@ -14,8 +16,6 @@ import ( host "github.com/cosmos/ibc-go/v9/modules/core/24-host" ibcexported "github.com/cosmos/ibc-go/v9/modules/core/exported" ibctesting "github.com/cosmos/ibc-go/v9/testing" - - "cosmossdk.io/log" ) // numCreations is the number of clients/consensus states created for diff --git a/modules/core/migrations/v7/genesis_test.go b/modules/core/migrations/v7/genesis_test.go index f893a395140..860cd84a1a7 100644 --- a/modules/core/migrations/v7/genesis_test.go +++ b/modules/core/migrations/v7/genesis_test.go @@ -5,6 +5,8 @@ import ( testifysuite "github.com/stretchr/testify/suite" + "cosmossdk.io/log" + "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/runtime" @@ -18,8 +20,6 @@ import ( v7 "github.com/cosmos/ibc-go/v9/modules/core/migrations/v7" "github.com/cosmos/ibc-go/v9/modules/core/types" ibctesting "github.com/cosmos/ibc-go/v9/testing" - - "cosmossdk.io/log" ) type MigrationsV7TestSuite struct { From 1f838ac04e2dab063fb4624858aa1642a0e86812 Mon Sep 17 00:00:00 2001 From: Tyler <48813565+technicallyty@users.noreply.github.com> Date: Thu, 23 Jan 2025 11:57:16 -0800 Subject: [PATCH 05/24] actually, we can do wasm for now. this will work fine. --- .../light-clients/08-wasm/keeper/migrations.go | 5 ++++- modules/light-clients/08-wasm/module.go | 17 +++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/modules/light-clients/08-wasm/keeper/migrations.go b/modules/light-clients/08-wasm/keeper/migrations.go index bf0a1488494..45005d625df 100644 --- a/modules/light-clients/08-wasm/keeper/migrations.go +++ b/modules/light-clients/08-wasm/keeper/migrations.go @@ -1,6 +1,8 @@ package keeper import ( + "context" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types" @@ -24,7 +26,8 @@ func NewMigrator(keeper Keeper) Migrator { // It grabs the checksums stored previously under the old key and stores // them in the global KeySet collection. It then deletes the old key and // the checksums stored under it. -func (m Migrator) MigrateChecksums(ctx sdk.Context) error { +func (m Migrator) MigrateChecksums(goCtx context.Context) error { + ctx := sdk.UnwrapSDKContext(goCtx) checksums, err := m.getStoredChecksums(ctx) if err != nil { return err diff --git a/modules/light-clients/08-wasm/module.go b/modules/light-clients/08-wasm/module.go index bda17444165..6123b48d9cc 100644 --- a/modules/light-clients/08-wasm/module.go +++ b/modules/light-clients/08-wasm/module.go @@ -7,6 +7,7 @@ import ( "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" + "google.golang.org/grpc" "cosmossdk.io/client/v2/autocli" "cosmossdk.io/core/appmodule" @@ -25,11 +26,11 @@ var ( _ appmodule.AppModule = (*AppModule)(nil) _ appmodule.HasConsensusVersion = (*AppModule)(nil) _ appmodule.HasRegisterInterfaces = (*AppModule)(nil) + _ appmodule.HasMigrations = (*AppModule)(nil) _ module.AppModule = (*AppModule)(nil) _ module.HasGRPCGateway = (*AppModule)(nil) _ module.HasGenesis = (*AppModule)(nil) - _ module.HasServices = (*AppModule)(nil) _ autocli.HasCustomTxCommand = (*AppModule)(nil) _ autocli.HasCustomQueryCommand = (*AppModule)(nil) @@ -102,14 +103,18 @@ func (AppModule) GetQueryCmd() *cobra.Command { } // RegisterServices registers module services. -func (am AppModule) RegisterServices(cfg module.Configurator) { - types.RegisterMsgServer(cfg.MsgServer(), am.keeper) - types.RegisterQueryServer(cfg.QueryServer(), am.keeper) +func (am AppModule) RegisterServices(cfg grpc.ServiceRegistrar) error { + types.RegisterMsgServer(cfg, am.keeper) + types.RegisterQueryServer(cfg, am.keeper) + return nil +} +func (am AppModule) RegisterMigrations(registrar appmodule.MigrationRegistrar) error { wasmMigrator := keeper.NewMigrator(am.keeper) - if err := cfg.RegisterMigration(types.ModuleName, 1, wasmMigrator.MigrateChecksums); err != nil { - panic(fmt.Errorf("failed to migrate 08-wasm module from version 1 to 2 (checksums migration to collections): %v", err)) + if err := registrar.Register(types.ModuleName, 1, wasmMigrator.MigrateChecksums); err != nil { + return err } + return nil } // ConsensusVersion implements AppModule/ConsensusVersion. From 2132fe5e75799b83da370fc4f34dd78b5b75aea2 Mon Sep 17 00:00:00 2001 From: Tyler <48813565+technicallyty@users.noreply.github.com> Date: Thu, 23 Jan 2025 12:01:01 -0800 Subject: [PATCH 06/24] appmodulev2 -> appmodule --- modules/apps/27-interchain-accounts/module.go | 3 +-- modules/apps/29-fee/module.go | 3 +-- modules/apps/transfer/module.go | 3 +-- modules/core/module.go | 3 +-- 4 files changed, 4 insertions(+), 8 deletions(-) diff --git a/modules/apps/27-interchain-accounts/module.go b/modules/apps/27-interchain-accounts/module.go index 3b1c8af290c..1e9d4ceb522 100644 --- a/modules/apps/27-interchain-accounts/module.go +++ b/modules/apps/27-interchain-accounts/module.go @@ -11,7 +11,6 @@ import ( "cosmossdk.io/client/v2/autocli" "cosmossdk.io/core/appmodule" - appmodulev2 "cosmossdk.io/core/appmodule/v2" coreregistry "cosmossdk.io/core/registry" "github.com/cosmos/cosmos-sdk/client" @@ -124,7 +123,7 @@ func (AppModule) GetQueryCmd() *cobra.Command { return cli.GetQueryCmd() } -func (am AppModule) RegisterMigrations(registrar appmodulev2.MigrationRegistrar) error { +func (am AppModule) RegisterMigrations(registrar appmodule.MigrationRegistrar) error { controllerMigrator := controllerkeeper.NewMigrator(am.controllerKeeper) hostMigrator := hostkeeper.NewMigrator(am.hostKeeper) if err := registrar.Register(types.ModuleName, 2, func(ctx context.Context) error { diff --git a/modules/apps/29-fee/module.go b/modules/apps/29-fee/module.go index aa162adf7d0..14fad4319e5 100644 --- a/modules/apps/29-fee/module.go +++ b/modules/apps/29-fee/module.go @@ -11,7 +11,6 @@ import ( "cosmossdk.io/client/v2/autocli" "cosmossdk.io/core/appmodule" - appmodulev2 "cosmossdk.io/core/appmodule/v2" coreregistry "cosmossdk.io/core/registry" "github.com/cosmos/cosmos-sdk/client" @@ -106,7 +105,7 @@ func (AppModule) GetQueryCmd() *cobra.Command { return cli.GetQueryCmd() } -func (am AppModule) RegisterMigrations(registrar appmodulev2.MigrationRegistrar) error { +func (am AppModule) RegisterMigrations(registrar appmodule.MigrationRegistrar) error { m := keeper.NewMigrator(am.keeper) if err := registrar.Register(types.ModuleName, 1, m.Migrate1to2); err != nil { return fmt.Errorf("failed to migrate ibc-fee module from version 1 to 2 (refund leftover fees): %v", err) diff --git a/modules/apps/transfer/module.go b/modules/apps/transfer/module.go index ac035ae72d4..31245c739e0 100644 --- a/modules/apps/transfer/module.go +++ b/modules/apps/transfer/module.go @@ -11,7 +11,6 @@ import ( "cosmossdk.io/client/v2/autocli" "cosmossdk.io/core/appmodule" - appmodulev2 "cosmossdk.io/core/appmodule/v2" coreregistry "cosmossdk.io/core/registry" "github.com/cosmos/cosmos-sdk/client" @@ -127,7 +126,7 @@ func (am AppModule) RegisterServices(cfg grpc.ServiceRegistrar) error { return nil } -func (am AppModule) RegisterMigrations(registrar appmodulev2.MigrationRegistrar) error { +func (am AppModule) RegisterMigrations(registrar appmodule.MigrationRegistrar) error { m := keeper.NewMigrator(am.keeper) if err := registrar.Register(types.ModuleName, 2, m.MigrateTotalEscrowForDenom); err != nil { return fmt.Errorf("failed to migrate transfer app from version 2 to 3 (total escrow entry migration): %w", err) diff --git a/modules/core/module.go b/modules/core/module.go index 0f3c75e7a49..8e7d401a752 100644 --- a/modules/core/module.go +++ b/modules/core/module.go @@ -11,7 +11,6 @@ import ( "cosmossdk.io/client/v2/autocli" "cosmossdk.io/core/appmodule" - appmodulev2 "cosmossdk.io/core/appmodule/v2" coreregistry "cosmossdk.io/core/registry" "github.com/cosmos/cosmos-sdk/client" @@ -123,7 +122,7 @@ func (AppModule) RegisterInterfaces(registry coreregistry.InterfaceRegistrar) { types.RegisterInterfaces(registry) } -func (am AppModule) RegisterMigrations(registrar appmodulev2.MigrationRegistrar) error { +func (am AppModule) RegisterMigrations(registrar appmodule.MigrationRegistrar) error { clientMigrator := clientkeeper.NewMigrator(am.keeper.ClientKeeper) if err := registrar.Register(exported.ModuleName, 2, clientMigrator.Migrate2to3); err != nil { return err From 93b268513a7c4b36dbc8b9c5b536b889f33bc986 Mon Sep 17 00:00:00 2001 From: Tyler <48813565+technicallyty@users.noreply.github.com> Date: Thu, 23 Jan 2025 16:15:55 -0800 Subject: [PATCH 07/24] environment patch --- .../light-clients/08-wasm/keeper/keeper.go | 18 ++++++++--------- .../08-wasm/keeper/keeper_no_vm.go | 2 ++ .../08-wasm/keeper/keeper_test.go | 12 +++++------ .../light-clients/08-wasm/keeper/keeper_vm.go | 20 ++++++++----------- .../08-wasm/keeper/migrations.go | 4 ++-- .../08-wasm/keeper/options_test.go | 10 ++++++---- .../08-wasm/testing/simapp/app.go | 6 ++++-- 7 files changed, 37 insertions(+), 35 deletions(-) diff --git a/modules/light-clients/08-wasm/keeper/keeper.go b/modules/light-clients/08-wasm/keeper/keeper.go index 0be1bb9064c..edd1a1956be 100644 --- a/modules/light-clients/08-wasm/keeper/keeper.go +++ b/modules/light-clients/08-wasm/keeper/keeper.go @@ -8,9 +8,9 @@ import ( wasmvm "github.com/CosmWasm/wasmvm/v2" "cosmossdk.io/collections" - "cosmossdk.io/core/store" + "cosmossdk.io/core/appmodule" + "cosmossdk.io/core/log" errorsmod "cosmossdk.io/errors" - "cosmossdk.io/log" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" @@ -22,6 +22,7 @@ import ( // Keeper defines the 08-wasm keeper type Keeper struct { + appmodule.Environment // implements gRPC QueryServer interface types.QueryServer @@ -30,8 +31,7 @@ type Keeper struct { vm types.WasmEngine - checksums collections.KeySet[[]byte] - storeService store.KVStoreService + checksums collections.KeySet[[]byte] queryPlugins QueryPlugins @@ -49,8 +49,8 @@ func (k Keeper) GetAuthority() string { } // Logger returns a module-specific logger. -func (Keeper) Logger(ctx sdk.Context) log.Logger { - return moduleLogger(ctx) +func (k Keeper) Logger(_ context.Context) log.Logger { + return k.Environment.Logger } func moduleLogger(ctx sdk.Context) log.Logger { @@ -77,7 +77,7 @@ func (k *Keeper) setQueryPlugins(plugins QueryPlugins) { k.queryPlugins = plugins } -func (k Keeper) newQueryHandler(ctx sdk.Context, callerID string) *queryHandler { +func (k Keeper) newQueryHandler(ctx context.Context, callerID string) *queryHandler { return newQueryHandler(ctx, k.getQueryPlugins(), callerID) } @@ -186,7 +186,7 @@ func (k Keeper) migrateContractCode(ctx sdk.Context, clientID string, newChecksu } // GetWasmClientState returns the 08-wasm client state for the given client identifier. -func (k Keeper) GetWasmClientState(ctx sdk.Context, clientID string) (*types.ClientState, error) { +func (k Keeper) GetWasmClientState(ctx context.Context, clientID string) (*types.ClientState, error) { clientState, found := k.clientKeeper.GetClientState(ctx, clientID) if !found { return nil, errorsmod.Wrapf(clienttypes.ErrClientTypeNotFound, "clientID %s", clientID) @@ -233,7 +233,7 @@ func (k Keeper) HasChecksum(ctx context.Context, checksum types.Checksum) bool { } // InitializePinnedCodes updates wasmvm to pin to cache all contracts marked as pinned -func (k Keeper) InitializePinnedCodes(ctx sdk.Context) error { +func (k Keeper) InitializePinnedCodes(ctx context.Context) error { checksums, err := k.GetAllChecksums(ctx) if err != nil { return err diff --git a/modules/light-clients/08-wasm/keeper/keeper_no_vm.go b/modules/light-clients/08-wasm/keeper/keeper_no_vm.go index 5dcacf5a176..2767800a0c3 100644 --- a/modules/light-clients/08-wasm/keeper/keeper_no_vm.go +++ b/modules/light-clients/08-wasm/keeper/keeper_no_vm.go @@ -15,6 +15,7 @@ import ( // custom build directive: nolink_libwasmvm. // This function is intended to panic and notify users that 08-wasm keeper functionality is not available. func NewKeeperWithVM( + _ environment.Environment, _ codec.BinaryCodec, _ storetypes.KVStoreService, _ types.ClientKeeper, @@ -31,6 +32,7 @@ func NewKeeperWithVM( // custom build directive: nolink_libwasmvm. // This function is intended to panic and notify users that 08-wasm keeper functionality is not available. func NewKeeperWithConfig( + _ environment.Environment, _ codec.BinaryCodec, _ storetypes.KVStoreService, _ types.ClientKeeper, diff --git a/modules/light-clients/08-wasm/keeper/keeper_test.go b/modules/light-clients/08-wasm/keeper/keeper_test.go index 9974671d270..9dd17e958f3 100644 --- a/modules/light-clients/08-wasm/keeper/keeper_test.go +++ b/modules/light-clients/08-wasm/keeper/keeper_test.go @@ -152,8 +152,8 @@ func (suite *KeeperTestSuite) TestNewKeeper() { "success", func() { keeper.NewKeeperWithVM( + runtime.NewEnvironment(runtime.NewKVStoreService(GetSimApp(suite.chainA).GetKey(types.StoreKey)), log.NewNopLogger()), GetSimApp(suite.chainA).AppCodec(), - runtime.NewKVStoreService(GetSimApp(suite.chainA).GetKey(types.StoreKey)), GetSimApp(suite.chainA).IBCKeeper.ClientKeeper, GetSimApp(suite.chainA).WasmClientKeeper.GetAuthority(), GetSimApp(suite.chainA).WasmClientKeeper.GetVM(), @@ -167,8 +167,8 @@ func (suite *KeeperTestSuite) TestNewKeeper() { "failure: empty authority", func() { keeper.NewKeeperWithVM( + runtime.NewEnvironment(runtime.NewKVStoreService(GetSimApp(suite.chainA).GetKey(types.StoreKey)), log.NewNopLogger()), GetSimApp(suite.chainA).AppCodec(), - runtime.NewKVStoreService(GetSimApp(suite.chainA).GetKey(types.StoreKey)), GetSimApp(suite.chainA).IBCKeeper.ClientKeeper, "", // authority GetSimApp(suite.chainA).WasmClientKeeper.GetVM(), @@ -182,8 +182,8 @@ func (suite *KeeperTestSuite) TestNewKeeper() { "failure: nil client keeper", func() { keeper.NewKeeperWithVM( + runtime.NewEnvironment(runtime.NewKVStoreService(GetSimApp(suite.chainA).GetKey(types.StoreKey)), log.NewNopLogger()), GetSimApp(suite.chainA).AppCodec(), - runtime.NewKVStoreService(GetSimApp(suite.chainA).GetKey(types.StoreKey)), nil, // client keeper, GetSimApp(suite.chainA).WasmClientKeeper.GetAuthority(), GetSimApp(suite.chainA).WasmClientKeeper.GetVM(), @@ -197,8 +197,8 @@ func (suite *KeeperTestSuite) TestNewKeeper() { "failure: nil wasm VM", func() { keeper.NewKeeperWithVM( + runtime.NewEnvironment(runtime.NewKVStoreService(GetSimApp(suite.chainA).GetKey(types.StoreKey)), log.NewNopLogger()), GetSimApp(suite.chainA).AppCodec(), - runtime.NewKVStoreService(GetSimApp(suite.chainA).GetKey(types.StoreKey)), GetSimApp(suite.chainA).IBCKeeper.ClientKeeper, GetSimApp(suite.chainA).WasmClientKeeper.GetAuthority(), nil, @@ -212,8 +212,8 @@ func (suite *KeeperTestSuite) TestNewKeeper() { "failure: nil store service", func() { keeper.NewKeeperWithVM( + runtime.NewEnvironment(nil, log.NewNopLogger()), GetSimApp(suite.chainA).AppCodec(), - nil, GetSimApp(suite.chainA).IBCKeeper.ClientKeeper, GetSimApp(suite.chainA).WasmClientKeeper.GetAuthority(), GetSimApp(suite.chainA).WasmClientKeeper.GetVM(), @@ -227,8 +227,8 @@ func (suite *KeeperTestSuite) TestNewKeeper() { "failure: nil query router", func() { keeper.NewKeeperWithVM( + runtime.NewEnvironment(runtime.NewKVStoreService(GetSimApp(suite.chainA).GetKey(types.StoreKey)), log.NewNopLogger()), GetSimApp(suite.chainA).AppCodec(), - runtime.NewKVStoreService(GetSimApp(suite.chainA).GetKey(types.StoreKey)), GetSimApp(suite.chainA).IBCKeeper.ClientKeeper, GetSimApp(suite.chainA).WasmClientKeeper.GetAuthority(), GetSimApp(suite.chainA).WasmClientKeeper.GetVM(), diff --git a/modules/light-clients/08-wasm/keeper/keeper_vm.go b/modules/light-clients/08-wasm/keeper/keeper_vm.go index bcaf2fafa72..d6e4afa7e22 100644 --- a/modules/light-clients/08-wasm/keeper/keeper_vm.go +++ b/modules/light-clients/08-wasm/keeper/keeper_vm.go @@ -9,11 +9,11 @@ import ( wasmvm "github.com/CosmWasm/wasmvm/v2" - "cosmossdk.io/collections" - "cosmossdk.io/core/store" - "github.com/cosmos/cosmos-sdk/codec" + "cosmossdk.io/collections" + "cosmossdk.io/core/appmodule" + "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types" ) @@ -21,8 +21,8 @@ import ( // This constructor function is meant to be used when the chain uses x/wasm // and the same Wasm VM instance should be shared with it. func NewKeeperWithVM( + env appmodule.Environment, cdc codec.BinaryCodec, - storeService store.KVStoreService, clientKeeper types.ClientKeeper, authority string, vm types.WasmEngine, @@ -41,21 +41,17 @@ func NewKeeperWithVM( panic(errors.New("wasm VM must not be nil")) } - if storeService == nil { - panic(errors.New("store service must not be nil")) - } - if strings.TrimSpace(authority) == "" { panic(errors.New("authority must be non-empty")) } - sb := collections.NewSchemaBuilder(storeService) + sb := collections.NewSchemaBuilder(env.KVStoreService) keeper := &Keeper{ + Environment: env, cdc: cdc, vm: vm, checksums: collections.NewKeySet(sb, types.ChecksumsKey, "checksums", collections.BytesKey), - storeService: storeService, clientKeeper: clientKeeper, authority: authority, } @@ -80,8 +76,8 @@ func NewKeeperWithVM( // This constructor function is meant to be used when the chain does not use x/wasm // and a Wasm VM needs to be instantiated using the provided parameters. func NewKeeperWithConfig( + env appmodule.Environment, cdc codec.BinaryCodec, - storeService store.KVStoreService, clientKeeper types.ClientKeeper, authority string, wasmConfig types.WasmConfig, @@ -93,5 +89,5 @@ func NewKeeperWithConfig( panic(fmt.Errorf("failed to instantiate new Wasm VM instance: %v", err)) } - return NewKeeperWithVM(cdc, storeService, clientKeeper, authority, vm, queryRouter, opts...) + return NewKeeperWithVM(env, cdc, clientKeeper, authority, vm, queryRouter, opts...) } diff --git a/modules/light-clients/08-wasm/keeper/migrations.go b/modules/light-clients/08-wasm/keeper/migrations.go index 45005d625df..95aa755d439 100644 --- a/modules/light-clients/08-wasm/keeper/migrations.go +++ b/modules/light-clients/08-wasm/keeper/migrations.go @@ -50,7 +50,7 @@ func (m Migrator) MigrateChecksums(goCtx context.Context) error { // getStoredChecksums returns the checksums stored under the KeyChecksums key. func (m Migrator) getStoredChecksums(ctx sdk.Context) ([][]byte, error) { - store := m.keeper.storeService.OpenKVStore(ctx) + store := m.keeper.KVStoreService.OpenKVStore(ctx) bz, err := store.Get([]byte(types.KeyChecksums)) if err != nil { @@ -68,7 +68,7 @@ func (m Migrator) getStoredChecksums(ctx sdk.Context) ([][]byte, error) { // deleteChecksums deletes the checksums stored under the KeyChecksums key. func (m Migrator) deleteChecksums(ctx sdk.Context) error { - store := m.keeper.storeService.OpenKVStore(ctx) + store := m.keeper.KVStoreService.OpenKVStore(ctx) err := store.Delete([]byte(types.KeyChecksums)) return err diff --git a/modules/light-clients/08-wasm/keeper/options_test.go b/modules/light-clients/08-wasm/keeper/options_test.go index bec7233df21..2a64e900efb 100644 --- a/modules/light-clients/08-wasm/keeper/options_test.go +++ b/modules/light-clients/08-wasm/keeper/options_test.go @@ -4,6 +4,8 @@ import ( "encoding/json" "errors" + "cosmossdk.io/log" + wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types" "github.com/cosmos/cosmos-sdk/runtime" @@ -36,8 +38,8 @@ func (suite *KeeperTestSuite) TestNewKeeperWithOptions() { "success: no options", func() { k = keeper.NewKeeperWithVM( + runtime.NewEnvironment(runtime.NewKVStoreService(GetSimApp(suite.chainA).GetKey(types.StoreKey)), log.NewNopLogger()), GetSimApp(suite.chainA).AppCodec(), - runtime.NewKVStoreService(GetSimApp(suite.chainA).GetKey(types.StoreKey)), GetSimApp(suite.chainA).IBCKeeper.ClientKeeper, GetSimApp(suite.chainA).WasmClientKeeper.GetAuthority(), GetSimApp(suite.chainA).WasmClientKeeper.GetVM(), @@ -61,8 +63,8 @@ func (suite *KeeperTestSuite) TestNewKeeperWithOptions() { Custom: mockErrorCustomQuerier(), }) k = keeper.NewKeeperWithVM( + runtime.NewEnvironment(runtime.NewKVStoreService(GetSimApp(suite.chainA).GetKey(types.StoreKey)), log.NewNopLogger()), GetSimApp(suite.chainA).AppCodec(), - runtime.NewKVStoreService(GetSimApp(suite.chainA).GetKey(types.StoreKey)), GetSimApp(suite.chainA).IBCKeeper.ClientKeeper, GetSimApp(suite.chainA).WasmClientKeeper.GetAuthority(), GetSimApp(suite.chainA).WasmClientKeeper.GetVM(), @@ -87,8 +89,8 @@ func (suite *KeeperTestSuite) TestNewKeeperWithOptions() { Stargate: mockErrorStargateQuerier(), }) k = keeper.NewKeeperWithVM( + runtime.NewEnvironment(runtime.NewKVStoreService(GetSimApp(suite.chainA).GetKey(types.StoreKey)), log.NewNopLogger()), GetSimApp(suite.chainA).AppCodec(), - runtime.NewKVStoreService(GetSimApp(suite.chainA).GetKey(types.StoreKey)), GetSimApp(suite.chainA).IBCKeeper.ClientKeeper, GetSimApp(suite.chainA).WasmClientKeeper.GetAuthority(), GetSimApp(suite.chainA).WasmClientKeeper.GetVM(), @@ -114,8 +116,8 @@ func (suite *KeeperTestSuite) TestNewKeeperWithOptions() { Stargate: mockErrorStargateQuerier(), }) k = keeper.NewKeeperWithVM( + runtime.NewEnvironment(runtime.NewKVStoreService(GetSimApp(suite.chainA).GetKey(types.StoreKey)), log.NewNopLogger()), GetSimApp(suite.chainA).AppCodec(), - runtime.NewKVStoreService(GetSimApp(suite.chainA).GetKey(types.StoreKey)), GetSimApp(suite.chainA).IBCKeeper.ClientKeeper, GetSimApp(suite.chainA).WasmClientKeeper.GetAuthority(), GetSimApp(suite.chainA).WasmClientKeeper.GetVM(), diff --git a/modules/light-clients/08-wasm/testing/simapp/app.go b/modules/light-clients/08-wasm/testing/simapp/app.go index 09a99f2070b..118c340eb06 100644 --- a/modules/light-clients/08-wasm/testing/simapp/app.go +++ b/modules/light-clients/08-wasm/testing/simapp/app.go @@ -546,12 +546,14 @@ func NewSimApp( if mockVM != nil { // NOTE: mockVM is used for testing purposes only! app.WasmClientKeeper = wasmkeeper.NewKeeperWithVM( - appCodec, runtime.NewKVStoreService(keys[wasmtypes.StoreKey]), app.IBCKeeper.ClientKeeper, + runtime.NewEnvironment(runtime.NewKVStoreService(keys[wasmtypes.StoreKey]), logger.With(log.ModuleKey, "x/ibc-wasm")), + appCodec, app.IBCKeeper.ClientKeeper, authtypes.NewModuleAddress(govtypes.ModuleName).String(), mockVM, app.GRPCQueryRouter(), ) } else { app.WasmClientKeeper = wasmkeeper.NewKeeperWithConfig( - appCodec, runtime.NewKVStoreService(keys[wasmtypes.StoreKey]), app.IBCKeeper.ClientKeeper, + runtime.NewEnvironment(runtime.NewKVStoreService(keys[wasmtypes.StoreKey]), logger.With(log.ModuleKey, "x/ibc-wasm")), + appCodec, app.IBCKeeper.ClientKeeper, authtypes.NewModuleAddress(govtypes.ModuleName).String(), wasmConfig, app.GRPCQueryRouter(), ) } From 3dd1f3377cd318285767ccb816fdebae1f42c831 Mon Sep 17 00:00:00 2001 From: Tyler <48813565+technicallyty@users.noreply.github.com> Date: Thu, 23 Jan 2025 17:09:55 -0800 Subject: [PATCH 08/24] ok most migrated now --- .../08-wasm/keeper/contract_keeper.go | 43 +++++++++-------- .../light-clients/08-wasm/keeper/events.go | 46 +++++++++---------- .../light-clients/08-wasm/keeper/keeper.go | 11 ++--- .../light-clients/08-wasm/keeper/keeper_vm.go | 4 ++ .../08-wasm/keeper/msg_server.go | 6 +-- .../light-clients/08-wasm/keeper/querier.go | 10 ++-- 6 files changed, 61 insertions(+), 59 deletions(-) diff --git a/modules/light-clients/08-wasm/keeper/contract_keeper.go b/modules/light-clients/08-wasm/keeper/contract_keeper.go index e812f2570e4..80d2fdd8cb2 100644 --- a/modules/light-clients/08-wasm/keeper/contract_keeper.go +++ b/modules/light-clients/08-wasm/keeper/contract_keeper.go @@ -2,6 +2,7 @@ package keeper import ( "bytes" + "context" "encoding/hex" "encoding/json" "errors" @@ -9,6 +10,7 @@ import ( wasmvm "github.com/CosmWasm/wasmvm/v2" wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types" + "cosmossdk.io/core/header" errorsmod "cosmossdk.io/errors" storetypes "cosmossdk.io/store/types" @@ -39,7 +41,7 @@ func (k Keeper) instantiateContract(ctx sdk.Context, clientID string, clientStor multipliedGasMeter := types.NewMultipliedGasMeter(sdkGasMeter, types.VMGasRegister) gasLimit := VMGasRegister.RuntimeGasForContract(ctx) - env := getEnv(ctx, clientID) + env := getEnv(k.HeaderService.HeaderInfo(ctx), clientID) msgInfo := wasmvmtypes.MessageInfo{ Sender: "", @@ -58,7 +60,7 @@ func (k Keeper) callContract(ctx sdk.Context, clientID string, clientStore store multipliedGasMeter := types.NewMultipliedGasMeter(sdkGasMeter, VMGasRegister) gasLimit := VMGasRegister.RuntimeGasForContract(ctx) - env := getEnv(ctx, clientID) + env := getEnv(k.HeaderService.HeaderInfo(ctx), clientID) ctx.GasMeter().ConsumeGas(VMGasRegister.SetupContractCost(true, len(msg)), "Loading CosmWasm module: sudo") resp, gasUsed, err := k.GetVM().Sudo(checksum, env, msg, internaltypes.NewStoreAdapter(clientStore), wasmvmAPI, k.newQueryHandler(ctx, clientID), multipliedGasMeter, gasLimit, types.CostJSONDeserialization) @@ -72,7 +74,7 @@ func (k Keeper) queryContract(ctx sdk.Context, clientID string, clientStore stor multipliedGasMeter := types.NewMultipliedGasMeter(sdkGasMeter, VMGasRegister) gasLimit := VMGasRegister.RuntimeGasForContract(ctx) - env := getEnv(ctx, clientID) + env := getEnv(k.HeaderService.HeaderInfo(ctx), clientID) ctx.GasMeter().ConsumeGas(VMGasRegister.SetupContractCost(true, len(msg)), "Loading CosmWasm module: query") resp, gasUsed, err := k.GetVM().Query(checksum, env, msg, internaltypes.NewStoreAdapter(clientStore), wasmvmAPI, k.newQueryHandler(ctx, clientID), multipliedGasMeter, gasLimit, types.CostJSONDeserialization) @@ -82,29 +84,30 @@ func (k Keeper) queryContract(ctx sdk.Context, clientID string, clientStore stor } // migrateContract calls vm.Migrate with internally constructed gas meter and environment. -func (k Keeper) migrateContract(ctx sdk.Context, clientID string, clientStore storetypes.KVStore, checksum types.Checksum, msg []byte) (*wasmvmtypes.ContractResult, error) { - sdkGasMeter := ctx.GasMeter() +func (k Keeper) migrateContract(ctx context.Context, clientID string, clientStore storetypes.KVStore, checksum types.Checksum, msg []byte) (*wasmvmtypes.ContractResult, error) { + sdkCtx := sdk.UnwrapSDKContext(ctx) + sdkGasMeter := sdkCtx.GasMeter() multipliedGasMeter := types.NewMultipliedGasMeter(sdkGasMeter, VMGasRegister) - gasLimit := VMGasRegister.RuntimeGasForContract(ctx) + gasLimit := VMGasRegister.RuntimeGasForContract(sdkCtx) - env := getEnv(ctx, clientID) + env := getEnv(k.HeaderService.HeaderInfo(ctx), clientID) - ctx.GasMeter().ConsumeGas(VMGasRegister.SetupContractCost(true, len(msg)), "Loading CosmWasm module: migrate") + sdkCtx.GasMeter().ConsumeGas(VMGasRegister.SetupContractCost(true, len(msg)), "Loading CosmWasm module: migrate") resp, gasUsed, err := k.GetVM().Migrate(checksum, env, msg, internaltypes.NewStoreAdapter(clientStore), wasmvmAPI, k.newQueryHandler(ctx, clientID), multipliedGasMeter, gasLimit, types.CostJSONDeserialization) - VMGasRegister.ConsumeRuntimeGas(ctx, gasUsed) + VMGasRegister.ConsumeRuntimeGas(sdkCtx, gasUsed) return resp, err } // WasmInstantiate accepts a message to instantiate a wasm contract, JSON encodes it and calls instantiateContract. -func (k Keeper) WasmInstantiate(ctx sdk.Context, clientID string, clientStore storetypes.KVStore, cs *types.ClientState, payload types.InstantiateMessage) error { +func (k Keeper) WasmInstantiate(ctx context.Context, clientID string, clientStore storetypes.KVStore, cs *types.ClientState, payload types.InstantiateMessage) error { encodedData, err := json.Marshal(payload) if err != nil { return errorsmod.Wrap(err, "failed to marshal payload for wasm contract instantiation") } checksum := cs.Checksum - res, err := k.instantiateContract(ctx, clientID, clientStore, checksum, encodedData) + res, err := k.instantiateContract(sdk.UnwrapSDKContext(ctx), clientID, clientStore, checksum, encodedData) if err != nil { return errorsmod.Wrap(types.ErrVMError, err.Error()) } @@ -136,14 +139,14 @@ func (k Keeper) WasmInstantiate(ctx sdk.Context, clientID string, clientStore st // - the response of the contract call contains non-empty events // - the response of the contract call contains non-empty attributes // - the data bytes of the response cannot be unmarshaled into the result type -func (k Keeper) WasmSudo(ctx sdk.Context, clientID string, clientStore storetypes.KVStore, cs *types.ClientState, payload types.SudoMsg) ([]byte, error) { +func (k Keeper) WasmSudo(ctx context.Context, clientID string, clientStore storetypes.KVStore, cs *types.ClientState, payload types.SudoMsg) ([]byte, error) { encodedData, err := json.Marshal(payload) if err != nil { return nil, errorsmod.Wrap(err, "failed to marshal payload for wasm execution") } checksum := cs.Checksum - res, err := k.callContract(ctx, clientID, clientStore, checksum, encodedData) + res, err := k.callContract(sdk.UnwrapSDKContext(ctx), clientID, clientStore, checksum, encodedData) if err != nil { return nil, errorsmod.Wrap(types.ErrVMError, err.Error()) } @@ -171,7 +174,7 @@ func (k Keeper) WasmSudo(ctx sdk.Context, clientID string, clientStore storetype // WasmMigrate migrate calls the migrate entry point of the contract with the given payload and returns the result. // WasmMigrate returns an error if: // - the contract migration returns an error -func (k Keeper) WasmMigrate(ctx sdk.Context, clientStore storetypes.KVStore, cs *types.ClientState, clientID string, payload []byte) error { +func (k Keeper) WasmMigrate(ctx context.Context, clientStore storetypes.KVStore, cs *types.ClientState, clientID string, payload []byte) error { res, err := k.migrateContract(ctx, clientID, clientStore, cs.Checksum, payload) if err != nil { return errorsmod.Wrap(types.ErrVMError, err.Error()) @@ -192,13 +195,13 @@ func (k Keeper) WasmMigrate(ctx sdk.Context, clientStore storetypes.KVStore, cs // WasmQuery returns an error if: // - the contract query returns an error // - the data bytes of the response cannot be unmarshal into the result type -func (k Keeper) WasmQuery(ctx sdk.Context, clientID string, clientStore storetypes.KVStore, cs *types.ClientState, payload types.QueryMsg) ([]byte, error) { +func (k Keeper) WasmQuery(ctx context.Context, clientID string, clientStore storetypes.KVStore, cs *types.ClientState, payload types.QueryMsg) ([]byte, error) { encodedData, err := json.Marshal(payload) if err != nil { return nil, errorsmod.Wrap(err, "failed to marshal payload for wasm query") } - res, err := k.queryContract(ctx, clientID, clientStore, cs.Checksum, encodedData) + res, err := k.queryContract(sdk.UnwrapSDKContext(ctx), clientID, clientStore, cs.Checksum, encodedData) if err != nil { return nil, errorsmod.Wrap(types.ErrVMError, err.Error()) } @@ -250,15 +253,15 @@ func unmarshalClientState(cdc codec.BinaryCodec, bz []byte) (exported.ClientStat } // getEnv returns the state of the blockchain environment the contract is running on -func getEnv(ctx sdk.Context, contractAddr string) wasmvmtypes.Env { - chainID := ctx.BlockHeader().ChainID - height := ctx.BlockHeader().Height +func getEnv(header header.Info, contractAddr string) wasmvmtypes.Env { + chainID := header.ChainID + height := header.Height // safety checks before casting below if height < 0 { panic(errors.New("block height must never be negative")) } - nsec := ctx.BlockTime().UnixNano() + nsec := header.Time.UnixNano() if nsec < 0 { panic(errors.New("block (unix) time must never be negative ")) } diff --git a/modules/light-clients/08-wasm/keeper/events.go b/modules/light-clients/08-wasm/keeper/events.go index 8e9801b9e45..2262f4c4ee1 100644 --- a/modules/light-clients/08-wasm/keeper/events.go +++ b/modules/light-clients/08-wasm/keeper/events.go @@ -6,34 +6,32 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types" + + "cosmossdk.io/core/event" ) // emitStoreWasmCodeEvent emits a store wasm code event -func emitStoreWasmCodeEvent(ctx sdk.Context, checksum types.Checksum) { - ctx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeStoreWasmCode, - sdk.NewAttribute(types.AttributeKeyWasmChecksum, hex.EncodeToString(checksum)), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func emitStoreWasmCodeEvent(em event.Manager, checksum types.Checksum) { + em.EmitKV( + types.EventTypeStoreWasmCode, + event.NewAttribute(types.AttributeKeyWasmChecksum, hex.EncodeToString(checksum)), + ) + em.EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } // emitMigrateContractEvent emits a migrate contract event -func emitMigrateContractEvent(ctx sdk.Context, clientID string, checksum, newChecksum types.Checksum) { - ctx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeMigrateContract, - sdk.NewAttribute(types.AttributeKeyClientID, clientID), - sdk.NewAttribute(types.AttributeKeyWasmChecksum, hex.EncodeToString(checksum)), - sdk.NewAttribute(types.AttributeKeyNewChecksum, hex.EncodeToString(newChecksum)), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - }) +func emitMigrateContractEvent(em event.Manager, clientID string, checksum, newChecksum types.Checksum) { + em.EmitKV( + types.EventTypeMigrateContract, + event.NewAttribute(types.AttributeKeyClientID, clientID), + event.NewAttribute(types.AttributeKeyWasmChecksum, hex.EncodeToString(checksum)), + event.NewAttribute(types.AttributeKeyNewChecksum, hex.EncodeToString(newChecksum)), + ) + em.EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ) } diff --git a/modules/light-clients/08-wasm/keeper/keeper.go b/modules/light-clients/08-wasm/keeper/keeper.go index edd1a1956be..cbd731b49ec 100644 --- a/modules/light-clients/08-wasm/keeper/keeper.go +++ b/modules/light-clients/08-wasm/keeper/keeper.go @@ -17,7 +17,6 @@ import ( "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types" clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types" - "github.com/cosmos/ibc-go/v9/modules/core/exported" ) // Keeper defines the 08-wasm keeper @@ -53,10 +52,6 @@ func (k Keeper) Logger(_ context.Context) log.Logger { return k.Environment.Logger } -func moduleLogger(ctx sdk.Context) log.Logger { - return ctx.Logger().With("module", "x/"+exported.ModuleName+"-"+types.ModuleName) -} - // GetVM returns the keeper's vm engine. func (k Keeper) GetVM() types.WasmEngine { return k.vm @@ -78,7 +73,7 @@ func (k *Keeper) setQueryPlugins(plugins QueryPlugins) { } func (k Keeper) newQueryHandler(ctx context.Context, callerID string) *queryHandler { - return newQueryHandler(ctx, k.getQueryPlugins(), callerID) + return newQueryHandler(ctx, k.Environment.Logger, k.getQueryPlugins(), callerID) } // storeWasmCode stores the contract to the VM, pins the checksum in the VM's in memory cache and stores the checksum @@ -141,7 +136,7 @@ func (k Keeper) storeWasmCode(ctx context.Context, code []byte, storeFn func(cod // migrateContractCode migrates the contract for a given light client to one denoted by the given new checksum. The checksum we // are migrating to must first be stored using storeWasmCode and must not match the checksum currently stored for this light client. -func (k Keeper) migrateContractCode(ctx sdk.Context, clientID string, newChecksum, migrateMsg []byte) error { +func (k Keeper) migrateContractCode(ctx context.Context, clientID string, newChecksum, migrateMsg []byte) error { clientStore := k.clientKeeper.ClientStore(ctx, clientID) wasmClientState, found := types.GetClientState(clientStore, k.cdc) if !found { @@ -180,7 +175,7 @@ func (k Keeper) migrateContractCode(ctx sdk.Context, clientID string, newChecksu k.clientKeeper.SetClientState(ctx, clientID, wasmClientState) - emitMigrateContractEvent(ctx, clientID, oldChecksum, newChecksum) + emitMigrateContractEvent(k.EventService.EventManager(ctx), clientID, oldChecksum, newChecksum) return nil } diff --git a/modules/light-clients/08-wasm/keeper/keeper_vm.go b/modules/light-clients/08-wasm/keeper/keeper_vm.go index d6e4afa7e22..3f450a156b0 100644 --- a/modules/light-clients/08-wasm/keeper/keeper_vm.go +++ b/modules/light-clients/08-wasm/keeper/keeper_vm.go @@ -33,6 +33,10 @@ func NewKeeperWithVM( panic(errors.New("client keeper must not be nil")) } + if env.KVStoreService == nil { + panic(errors.New("store service must not be nil")) + } + if queryRouter == nil { panic(errors.New("query router must not be nil")) } diff --git a/modules/light-clients/08-wasm/keeper/msg_server.go b/modules/light-clients/08-wasm/keeper/msg_server.go index d08063d6630..b3e9afd63da 100644 --- a/modules/light-clients/08-wasm/keeper/msg_server.go +++ b/modules/light-clients/08-wasm/keeper/msg_server.go @@ -26,7 +26,7 @@ func (k Keeper) StoreCode(goCtx context.Context, msg *types.MsgStoreCode) (*type return nil, errorsmod.Wrap(err, "failed to store wasm bytecode") } - emitStoreWasmCodeEvent(ctx, checksum) + emitStoreWasmCodeEvent(k.EventService.EventManager(ctx), checksum) return &types.MsgStoreCodeResponse{ Checksum: checksum, @@ -57,13 +57,11 @@ func (k Keeper) RemoveChecksum(goCtx context.Context, msg *types.MsgRemoveChecks } // MigrateContract defines a rpc handler method for MsgMigrateContract -func (k Keeper) MigrateContract(goCtx context.Context, msg *types.MsgMigrateContract) (*types.MsgMigrateContractResponse, error) { +func (k Keeper) MigrateContract(ctx context.Context, msg *types.MsgMigrateContract) (*types.MsgMigrateContractResponse, error) { if k.GetAuthority() != msg.Signer { return nil, errorsmod.Wrapf(ibcerrors.ErrUnauthorized, "expected %s, got %s", k.GetAuthority(), msg.Signer) } - ctx := sdk.UnwrapSDKContext(goCtx) - err := k.migrateContractCode(ctx, msg.ClientId, msg.Checksum, msg.Msg) if err != nil { return nil, errorsmod.Wrap(err, "failed to migrate contract") diff --git a/modules/light-clients/08-wasm/keeper/querier.go b/modules/light-clients/08-wasm/keeper/querier.go index 20fa56b6ca0..9681439e378 100644 --- a/modules/light-clients/08-wasm/keeper/querier.go +++ b/modules/light-clients/08-wasm/keeper/querier.go @@ -1,12 +1,14 @@ package keeper import ( + "context" "encoding/json" "fmt" "slices" wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types" + "cosmossdk.io/core/log" errorsmod "cosmossdk.io/errors" storetypes "cosmossdk.io/store/types" @@ -39,15 +41,17 @@ var defaultAcceptList = []string{ // queryHandler is a wrapper around the sdk.Context and the CallerID that calls // into the query plugins. type queryHandler struct { + logger log.Logger Ctx sdk.Context Plugins QueryPlugins CallerID string } // newQueryHandler returns a default querier that can be used in the contract. -func newQueryHandler(ctx sdk.Context, plugins QueryPlugins, callerID string) *queryHandler { +func newQueryHandler(ctx context.Context, logger log.Logger, plugins QueryPlugins, callerID string) *queryHandler { return &queryHandler{ - Ctx: ctx, + Ctx: sdk.UnwrapSDKContext(ctx), + logger: logger, Plugins: plugins, CallerID: callerID, } @@ -75,7 +79,7 @@ func (q *queryHandler) Query(request wasmvmtypes.QueryRequest, gasLimit uint64) return res, nil } - moduleLogger(q.Ctx).Debug("Redacting query error", "cause", err) + q.logger.Debug("Redacting query error", "cause", err) return nil, redactError(err) } From b4bf84e3a69ab524e8fdfccede9919510cda8023 Mon Sep 17 00:00:00 2001 From: Tyler <48813565+technicallyty@users.noreply.github.com> Date: Thu, 23 Jan 2025 17:25:03 -0800 Subject: [PATCH 09/24] use branch service isntead of query contexts --- .../light-clients/08-wasm/keeper/keeper.go | 2 +- .../light-clients/08-wasm/keeper/querier.go | 51 +++++++++---------- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/modules/light-clients/08-wasm/keeper/keeper.go b/modules/light-clients/08-wasm/keeper/keeper.go index cbd731b49ec..03842252e84 100644 --- a/modules/light-clients/08-wasm/keeper/keeper.go +++ b/modules/light-clients/08-wasm/keeper/keeper.go @@ -73,7 +73,7 @@ func (k *Keeper) setQueryPlugins(plugins QueryPlugins) { } func (k Keeper) newQueryHandler(ctx context.Context, callerID string) *queryHandler { - return newQueryHandler(ctx, k.Environment.Logger, k.getQueryPlugins(), callerID) + return newQueryHandler(ctx, k.Environment, k.getQueryPlugins(), callerID) } // storeWasmCode stores the contract to the VM, pins the checksum in the VM's in memory cache and stores the checksum diff --git a/modules/light-clients/08-wasm/keeper/querier.go b/modules/light-clients/08-wasm/keeper/querier.go index 9681439e378..3aae7031afb 100644 --- a/modules/light-clients/08-wasm/keeper/querier.go +++ b/modules/light-clients/08-wasm/keeper/querier.go @@ -8,12 +8,11 @@ import ( wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types" - "cosmossdk.io/core/log" - errorsmod "cosmossdk.io/errors" - storetypes "cosmossdk.io/store/types" - sdk "github.com/cosmos/cosmos-sdk/types" + "cosmossdk.io/core/appmodule" + errorsmod "cosmossdk.io/errors" + abci "github.com/cometbft/cometbft/api/cometbft/abci/v1" "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types" @@ -41,46 +40,46 @@ var defaultAcceptList = []string{ // queryHandler is a wrapper around the sdk.Context and the CallerID that calls // into the query plugins. type queryHandler struct { - logger log.Logger - Ctx sdk.Context + appmodule.Environment + Ctx context.Context Plugins QueryPlugins CallerID string } // newQueryHandler returns a default querier that can be used in the contract. -func newQueryHandler(ctx context.Context, logger log.Logger, plugins QueryPlugins, callerID string) *queryHandler { +func newQueryHandler(ctx context.Context, env appmodule.Environment, plugins QueryPlugins, callerID string) *queryHandler { return &queryHandler{ - Ctx: sdk.UnwrapSDKContext(ctx), - logger: logger, - Plugins: plugins, - CallerID: callerID, + Ctx: ctx, + Environment: env, + Plugins: plugins, + CallerID: callerID, } } // GasConsumed implements the wasmvmtypes.Querier interface. func (q *queryHandler) GasConsumed() uint64 { - return VMGasRegister.ToWasmVMGas(q.Ctx.GasMeter().GasConsumed()) + return VMGasRegister.ToWasmVMGas(q.GasService.GasMeter(q.Ctx).Consumed()) } // Query implements the wasmvmtypes.Querier interface. func (q *queryHandler) Query(request wasmvmtypes.QueryRequest, gasLimit uint64) ([]byte, error) { - sdkGas := VMGasRegister.FromWasmVMGas(gasLimit) - + sdkGasLimit := VMGasRegister.FromWasmVMGas(gasLimit) // discard all changes/events in subCtx by not committing the cached context - subCtx, _ := q.Ctx.WithGasMeter(storetypes.NewGasMeter(sdkGas)).CacheContext() - - // make sure we charge the higher level context even on panic - defer func() { - q.Ctx.GasMeter().ConsumeGas(subCtx.GasMeter().GasConsumed(), "contract sub-query") - }() + var res []byte + _, err := q.BranchService.ExecuteWithGasLimit(q.Ctx, sdkGasLimit, func(ctx context.Context) error { + var err error + res, err = q.Plugins.HandleQuery(sdk.UnwrapSDKContext(ctx), q.CallerID, request) + if err == nil { + return nil + } - res, err := q.Plugins.HandleQuery(subCtx, q.CallerID, request) - if err == nil { - return res, nil + q.Logger.Debug("Redacting query error", "cause", err) + return err + }) + if err != nil { + return nil, redactError(err) } - - q.logger.Debug("Redacting query error", "cause", err) - return nil, redactError(err) + return res, nil } type ( From 2d759efe3a6c7decb7ec620d3c239f70cd7a3ece Mon Sep 17 00:00:00 2001 From: Tyler <48813565+technicallyty@users.noreply.github.com> Date: Thu, 23 Jan 2025 17:38:57 -0800 Subject: [PATCH 10/24] wip gas --- .../08-wasm/keeper/contract_keeper.go | 2 +- .../08-wasm/types/gas_register_custom.go | 21 +++++++++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/modules/light-clients/08-wasm/keeper/contract_keeper.go b/modules/light-clients/08-wasm/keeper/contract_keeper.go index 80d2fdd8cb2..13f3fb2adc0 100644 --- a/modules/light-clients/08-wasm/keeper/contract_keeper.go +++ b/modules/light-clients/08-wasm/keeper/contract_keeper.go @@ -37,7 +37,7 @@ var ( // instantiateContract calls vm.Instantiate with appropriate arguments. func (k Keeper) instantiateContract(ctx sdk.Context, clientID string, clientStore storetypes.KVStore, checksum types.Checksum, msg []byte) (*wasmvmtypes.ContractResult, error) { - sdkGasMeter := ctx.GasMeter() + sdkGasMeter := k.GasService.GasMeter(ctx) multipliedGasMeter := types.NewMultipliedGasMeter(sdkGasMeter, types.VMGasRegister) gasLimit := VMGasRegister.RuntimeGasForContract(ctx) diff --git a/modules/light-clients/08-wasm/types/gas_register_custom.go b/modules/light-clients/08-wasm/types/gas_register_custom.go index 991d8779c43..dd56b7647a9 100644 --- a/modules/light-clients/08-wasm/types/gas_register_custom.go +++ b/modules/light-clients/08-wasm/types/gas_register_custom.go @@ -6,6 +6,7 @@ import ( wasmvm "github.com/CosmWasm/wasmvm/v2" wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types" + "cosmossdk.io/core/gas" storetypes "cosmossdk.io/store/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -24,39 +25,41 @@ var CostJSONDeserialization = wasmvmtypes.UFraction{ Denominator: 1, } -func (g WasmGasRegister) RuntimeGasForContract(ctx sdk.Context) uint64 { - meter := ctx.GasMeter() - if meter.IsOutOfGas() { +func (g WasmGasRegister) RuntimeGasForContract(meter gas.Meter) uint64 { + if meter.Remaining() >= meter.Limit() { return 0 } // infinite gas meter with limit=0 or MaxUint64 if meter.Limit() == 0 || meter.Limit() == math.MaxUint64 { return math.MaxUint64 } + sdk.Context{}.GasMeter().GasConsumedToLimit() + var consumedToLimit return g.ToWasmVMGas(meter.Limit() - meter.GasConsumedToLimit()) } -func (g WasmGasRegister) ConsumeRuntimeGas(ctx sdk.Context, gas uint64) { +func (g WasmGasRegister) ConsumeRuntimeGas(meter gas.Meter, gas uint64) { consumed := g.FromWasmVMGas(gas) - ctx.GasMeter().ConsumeGas(consumed, "wasm contract") + meter.Consume(consumed, "wasm contract") + // throw OutOfGas error if we ran out (got exactly to zero due to better limit enforcing) - if ctx.GasMeter().IsOutOfGas() { + if meter.Remaining() >= meter.Limit() { panic(storetypes.ErrorOutOfGas{Descriptor: "Wasmer function execution"}) } } // MultipliedGasMeter wraps the GasMeter from context and multiplies all reads by out defined multiplier type MultipliedGasMeter struct { - originalMeter storetypes.GasMeter + originalMeter gas.Meter GasRegister GasRegister } -func NewMultipliedGasMeter(originalMeter storetypes.GasMeter, gr GasRegister) MultipliedGasMeter { +func NewMultipliedGasMeter(originalMeter gas.Meter, gr GasRegister) MultipliedGasMeter { return MultipliedGasMeter{originalMeter: originalMeter, GasRegister: gr} } var _ wasmvm.GasMeter = MultipliedGasMeter{} func (m MultipliedGasMeter) GasConsumed() storetypes.Gas { - return m.GasRegister.ToWasmVMGas(m.originalMeter.GasConsumed()) + return m.GasRegister.ToWasmVMGas(m.originalMeter.Consumed()) } From 15f51bd643e9a49f643fc254661e2d75bb80f7dc Mon Sep 17 00:00:00 2001 From: Tyler <48813565+technicallyty@users.noreply.github.com> Date: Fri, 24 Jan 2025 06:49:22 -0800 Subject: [PATCH 11/24] migration complete --- .../08-wasm/keeper/contract_keeper.go | 38 +++++++++---------- .../light-clients/08-wasm/keeper/keeper.go | 10 ++--- .../08-wasm/keeper/migrations.go | 4 +- .../08-wasm/keeper/snapshotter.go | 2 +- .../08-wasm/types/gas_register_custom.go | 20 ++++++---- 5 files changed, 39 insertions(+), 35 deletions(-) diff --git a/modules/light-clients/08-wasm/keeper/contract_keeper.go b/modules/light-clients/08-wasm/keeper/contract_keeper.go index 13f3fb2adc0..dfcc594d6a0 100644 --- a/modules/light-clients/08-wasm/keeper/contract_keeper.go +++ b/modules/light-clients/08-wasm/keeper/contract_keeper.go @@ -36,10 +36,10 @@ var ( ) // instantiateContract calls vm.Instantiate with appropriate arguments. -func (k Keeper) instantiateContract(ctx sdk.Context, clientID string, clientStore storetypes.KVStore, checksum types.Checksum, msg []byte) (*wasmvmtypes.ContractResult, error) { +func (k Keeper) instantiateContract(ctx context.Context, clientID string, clientStore storetypes.KVStore, checksum types.Checksum, msg []byte) (*wasmvmtypes.ContractResult, error) { sdkGasMeter := k.GasService.GasMeter(ctx) multipliedGasMeter := types.NewMultipliedGasMeter(sdkGasMeter, types.VMGasRegister) - gasLimit := VMGasRegister.RuntimeGasForContract(ctx) + gasLimit := VMGasRegister.RuntimeGasForContract(sdkGasMeter) env := getEnv(k.HeaderService.HeaderInfo(ctx), clientID) @@ -48,53 +48,53 @@ func (k Keeper) instantiateContract(ctx sdk.Context, clientID string, clientStor Funds: nil, } - ctx.GasMeter().ConsumeGas(types.VMGasRegister.SetupContractCost(true, len(msg)), "Loading CosmWasm module: instantiate") + sdkGasMeter.Consume(types.VMGasRegister.SetupContractCost(true, len(msg)), "Loading CosmWasm module: instantiate") + resp, gasUsed, err := k.GetVM().Instantiate(checksum, env, msgInfo, msg, internaltypes.NewStoreAdapter(clientStore), wasmvmAPI, k.newQueryHandler(ctx, clientID), multipliedGasMeter, gasLimit, types.CostJSONDeserialization) - types.VMGasRegister.ConsumeRuntimeGas(ctx, gasUsed) + types.VMGasRegister.ConsumeRuntimeGas(sdkGasMeter, gasUsed) return resp, err } // callContract calls vm.Sudo with internally constructed gas meter and environment. -func (k Keeper) callContract(ctx sdk.Context, clientID string, clientStore storetypes.KVStore, checksum types.Checksum, msg []byte) (*wasmvmtypes.ContractResult, error) { - sdkGasMeter := ctx.GasMeter() +func (k Keeper) callContract(ctx context.Context, clientID string, clientStore storetypes.KVStore, checksum types.Checksum, msg []byte) (*wasmvmtypes.ContractResult, error) { + sdkGasMeter := k.GasService.GasMeter(ctx) multipliedGasMeter := types.NewMultipliedGasMeter(sdkGasMeter, VMGasRegister) - gasLimit := VMGasRegister.RuntimeGasForContract(ctx) + gasLimit := VMGasRegister.RuntimeGasForContract(sdkGasMeter) env := getEnv(k.HeaderService.HeaderInfo(ctx), clientID) - ctx.GasMeter().ConsumeGas(VMGasRegister.SetupContractCost(true, len(msg)), "Loading CosmWasm module: sudo") + sdkGasMeter.Consume(VMGasRegister.SetupContractCost(true, len(msg)), "Loading CosmWasm module: sudo") resp, gasUsed, err := k.GetVM().Sudo(checksum, env, msg, internaltypes.NewStoreAdapter(clientStore), wasmvmAPI, k.newQueryHandler(ctx, clientID), multipliedGasMeter, gasLimit, types.CostJSONDeserialization) - VMGasRegister.ConsumeRuntimeGas(ctx, gasUsed) + VMGasRegister.ConsumeRuntimeGas(sdkGasMeter, gasUsed) return resp, err } // queryContract calls vm.Query. -func (k Keeper) queryContract(ctx sdk.Context, clientID string, clientStore storetypes.KVStore, checksum types.Checksum, msg []byte) (*wasmvmtypes.QueryResult, error) { - sdkGasMeter := ctx.GasMeter() +func (k Keeper) queryContract(ctx context.Context, clientID string, clientStore storetypes.KVStore, checksum types.Checksum, msg []byte) (*wasmvmtypes.QueryResult, error) { + sdkGasMeter := k.GasService.GasMeter(ctx) multipliedGasMeter := types.NewMultipliedGasMeter(sdkGasMeter, VMGasRegister) - gasLimit := VMGasRegister.RuntimeGasForContract(ctx) + gasLimit := VMGasRegister.RuntimeGasForContract(sdkGasMeter) env := getEnv(k.HeaderService.HeaderInfo(ctx), clientID) - ctx.GasMeter().ConsumeGas(VMGasRegister.SetupContractCost(true, len(msg)), "Loading CosmWasm module: query") + sdkGasMeter.Consume(VMGasRegister.SetupContractCost(true, len(msg)), "Loading CosmWasm module: query") resp, gasUsed, err := k.GetVM().Query(checksum, env, msg, internaltypes.NewStoreAdapter(clientStore), wasmvmAPI, k.newQueryHandler(ctx, clientID), multipliedGasMeter, gasLimit, types.CostJSONDeserialization) - VMGasRegister.ConsumeRuntimeGas(ctx, gasUsed) + VMGasRegister.ConsumeRuntimeGas(sdkGasMeter, gasUsed) return resp, err } // migrateContract calls vm.Migrate with internally constructed gas meter and environment. func (k Keeper) migrateContract(ctx context.Context, clientID string, clientStore storetypes.KVStore, checksum types.Checksum, msg []byte) (*wasmvmtypes.ContractResult, error) { - sdkCtx := sdk.UnwrapSDKContext(ctx) - sdkGasMeter := sdkCtx.GasMeter() + sdkGasMeter := k.GasService.GasMeter(ctx) multipliedGasMeter := types.NewMultipliedGasMeter(sdkGasMeter, VMGasRegister) - gasLimit := VMGasRegister.RuntimeGasForContract(sdkCtx) + gasLimit := VMGasRegister.RuntimeGasForContract(sdkGasMeter) env := getEnv(k.HeaderService.HeaderInfo(ctx), clientID) - sdkCtx.GasMeter().ConsumeGas(VMGasRegister.SetupContractCost(true, len(msg)), "Loading CosmWasm module: migrate") + sdkGasMeter.Consume(VMGasRegister.SetupContractCost(true, len(msg)), "Loading CosmWasm module: migrate") resp, gasUsed, err := k.GetVM().Migrate(checksum, env, msg, internaltypes.NewStoreAdapter(clientStore), wasmvmAPI, k.newQueryHandler(ctx, clientID), multipliedGasMeter, gasLimit, types.CostJSONDeserialization) - VMGasRegister.ConsumeRuntimeGas(sdkCtx, gasUsed) + VMGasRegister.ConsumeRuntimeGas(sdkGasMeter, gasUsed) return resp, err } diff --git a/modules/light-clients/08-wasm/keeper/keeper.go b/modules/light-clients/08-wasm/keeper/keeper.go index 03842252e84..fe317700507 100644 --- a/modules/light-clients/08-wasm/keeper/keeper.go +++ b/modules/light-clients/08-wasm/keeper/keeper.go @@ -13,8 +13,6 @@ import ( errorsmod "cosmossdk.io/errors" "github.com/cosmos/cosmos-sdk/codec" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types" clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types" ) @@ -82,10 +80,10 @@ func (k Keeper) newQueryHandler(ctx context.Context, callerID string) *queryHand // - Size bounds are checked. Contract length must not be 0 or exceed a specific size (maxWasmSize). // - The contract must not have already been stored in store. func (k Keeper) storeWasmCode(ctx context.Context, code []byte, storeFn func(code wasmvm.WasmCode, gasLimit uint64) (wasmvm.Checksum, uint64, error)) ([]byte, error) { - sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/5917 + meter := k.GasService.GasMeter(ctx) var err error if types.IsGzip(code) { - sdkCtx.GasMeter().ConsumeGas(types.VMGasRegister.UncompressCosts(len(code)), "Uncompress gzip bytecode") + meter.Consume(types.VMGasRegister.UncompressCosts(len(code)), "Uncompress gzip bytecode") code, err = types.Uncompress(code, types.MaxWasmSize) if err != nil { return nil, errorsmod.Wrap(err, "failed to store contract") @@ -108,9 +106,9 @@ func (k Keeper) storeWasmCode(ctx context.Context, code []byte, storeFn func(cod } // create the code in the vm - gasLeft := types.VMGasRegister.RuntimeGasForContract(sdkCtx) + gasLeft := types.VMGasRegister.RuntimeGasForContract(meter) vmChecksum, gasUsed, err := storeFn(code, gasLeft) - types.VMGasRegister.ConsumeRuntimeGas(sdkCtx, gasUsed) + types.VMGasRegister.ConsumeRuntimeGas(meter, gasUsed) if err != nil { return nil, errorsmod.Wrap(err, "failed to store contract") } diff --git a/modules/light-clients/08-wasm/keeper/migrations.go b/modules/light-clients/08-wasm/keeper/migrations.go index 95aa755d439..d4f29dc9fbe 100644 --- a/modules/light-clients/08-wasm/keeper/migrations.go +++ b/modules/light-clients/08-wasm/keeper/migrations.go @@ -49,7 +49,7 @@ func (m Migrator) MigrateChecksums(goCtx context.Context) error { } // getStoredChecksums returns the checksums stored under the KeyChecksums key. -func (m Migrator) getStoredChecksums(ctx sdk.Context) ([][]byte, error) { +func (m Migrator) getStoredChecksums(ctx context.Context) ([][]byte, error) { store := m.keeper.KVStoreService.OpenKVStore(ctx) bz, err := store.Get([]byte(types.KeyChecksums)) @@ -67,7 +67,7 @@ func (m Migrator) getStoredChecksums(ctx sdk.Context) ([][]byte, error) { } // deleteChecksums deletes the checksums stored under the KeyChecksums key. -func (m Migrator) deleteChecksums(ctx sdk.Context) error { +func (m Migrator) deleteChecksums(ctx context.Context) error { store := m.keeper.KVStoreService.OpenKVStore(ctx) err := store.Delete([]byte(types.KeyChecksums)) diff --git a/modules/light-clients/08-wasm/keeper/snapshotter.go b/modules/light-clients/08-wasm/keeper/snapshotter.go index fa8163c64c2..a400b7a8024 100644 --- a/modules/light-clients/08-wasm/keeper/snapshotter.go +++ b/modules/light-clients/08-wasm/keeper/snapshotter.go @@ -99,7 +99,7 @@ func (ws *WasmSnapshotter) RestoreExtension(height uint64, format uint32, payloa return errorsmod.Wrapf(snapshot.ErrUnknownFormat, "expected %d, got %d", ws.SnapshotFormat(), format) } -func restoreV1(ctx sdk.Context, k *Keeper, compressedCode []byte) error { +func restoreV1(_ sdk.Context, k *Keeper, compressedCode []byte) error { if !types.IsGzip(compressedCode) { return errorsmod.Wrap(types.ErrInvalidData, "expected wasm code is not gzip format") } diff --git a/modules/light-clients/08-wasm/types/gas_register_custom.go b/modules/light-clients/08-wasm/types/gas_register_custom.go index dd56b7647a9..1bd8a8a2373 100644 --- a/modules/light-clients/08-wasm/types/gas_register_custom.go +++ b/modules/light-clients/08-wasm/types/gas_register_custom.go @@ -8,8 +8,6 @@ import ( "cosmossdk.io/core/gas" storetypes "cosmossdk.io/store/types" - - sdk "github.com/cosmos/cosmos-sdk/types" ) // While gas_register.go is a direct copy of https://github.com/CosmWasm/wasmd/blob/main/x/wasm/types/gas_register.go @@ -33,17 +31,25 @@ func (g WasmGasRegister) RuntimeGasForContract(meter gas.Meter) uint64 { if meter.Limit() == 0 || meter.Limit() == math.MaxUint64 { return math.MaxUint64 } - sdk.Context{}.GasMeter().GasConsumedToLimit() - var consumedToLimit - return g.ToWasmVMGas(meter.Limit() - meter.GasConsumedToLimit()) + var consumedToLimit gas.Gas + if meter.Remaining() <= meter.Limit() { + consumedToLimit = meter.Limit() + } else { + consumedToLimit = meter.Consumed() + } + return g.ToWasmVMGas(meter.Limit() - consumedToLimit) } func (g WasmGasRegister) ConsumeRuntimeGas(meter gas.Meter, gas uint64) { consumed := g.FromWasmVMGas(gas) meter.Consume(consumed, "wasm contract") - // throw OutOfGas error if we ran out (got exactly to zero due to better limit enforcing) - if meter.Remaining() >= meter.Limit() { + // TODO(technicallyty): this used to be a meter.IsOutOfGas check, which has since been removed from the gas meter iface. + // We use the InfiniteGasMeter in tests, which would ALWAYS return false to IsOutOfGas. Now that we don't have that method, + // we have to include this weird hack that essentially checks if we're out of gas and we're not the infinite gas meter. + // Please don't replicate this if you can. It is ugly. + if meter.Remaining() >= meter.Limit() && (meter.Limit() != math.MaxUint64 && meter.Remaining() != math.MaxUint64) { + // throw OutOfGas error if we ran out (got exactly to zero due to better limit enforcing) panic(storetypes.ErrorOutOfGas{Descriptor: "Wasmer function execution"}) } } From c90669f1c34c50f445e18bb8db6e8a53c5cdb1e2 Mon Sep 17 00:00:00 2001 From: Tyler <48813565+technicallyty@users.noreply.github.com> Date: Fri, 24 Jan 2025 07:02:42 -0800 Subject: [PATCH 12/24] linter, error checks, etc etc --- .../08-wasm/keeper/contract_keeper.go | 25 +++++++---- .../light-clients/08-wasm/keeper/events.go | 45 ++++++++++--------- .../light-clients/08-wasm/keeper/keeper.go | 10 ++++- .../light-clients/08-wasm/keeper/keeper_vm.go | 4 +- .../08-wasm/keeper/msg_server.go | 5 ++- .../08-wasm/keeper/options_test.go | 4 +- .../light-clients/08-wasm/keeper/querier.go | 4 +- .../08-wasm/types/gas_register_custom.go | 9 ++-- 8 files changed, 66 insertions(+), 40 deletions(-) diff --git a/modules/light-clients/08-wasm/keeper/contract_keeper.go b/modules/light-clients/08-wasm/keeper/contract_keeper.go index dfcc594d6a0..f7d904b4563 100644 --- a/modules/light-clients/08-wasm/keeper/contract_keeper.go +++ b/modules/light-clients/08-wasm/keeper/contract_keeper.go @@ -6,6 +6,7 @@ import ( "encoding/hex" "encoding/json" "errors" + "fmt" wasmvm "github.com/CosmWasm/wasmvm/v2" wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types" @@ -48,7 +49,9 @@ func (k Keeper) instantiateContract(ctx context.Context, clientID string, client Funds: nil, } - sdkGasMeter.Consume(types.VMGasRegister.SetupContractCost(true, len(msg)), "Loading CosmWasm module: instantiate") + if err := sdkGasMeter.Consume(types.VMGasRegister.SetupContractCost(true, len(msg)), "Loading CosmWasm module: instantiate"); err != nil { + return nil, fmt.Errorf("failed to consume gas for Loading Cosmwasm module instantiate: %w", err) + } resp, gasUsed, err := k.GetVM().Instantiate(checksum, env, msgInfo, msg, internaltypes.NewStoreAdapter(clientStore), wasmvmAPI, k.newQueryHandler(ctx, clientID), multipliedGasMeter, gasLimit, types.CostJSONDeserialization) types.VMGasRegister.ConsumeRuntimeGas(sdkGasMeter, gasUsed) @@ -63,7 +66,9 @@ func (k Keeper) callContract(ctx context.Context, clientID string, clientStore s env := getEnv(k.HeaderService.HeaderInfo(ctx), clientID) - sdkGasMeter.Consume(VMGasRegister.SetupContractCost(true, len(msg)), "Loading CosmWasm module: sudo") + if err := sdkGasMeter.Consume(VMGasRegister.SetupContractCost(true, len(msg)), "Loading CosmWasm module: sudo"); err != nil { + return nil, fmt.Errorf("failed to consume gas for Loading Cosmwasm module sudo: %w", err) + } resp, gasUsed, err := k.GetVM().Sudo(checksum, env, msg, internaltypes.NewStoreAdapter(clientStore), wasmvmAPI, k.newQueryHandler(ctx, clientID), multipliedGasMeter, gasLimit, types.CostJSONDeserialization) VMGasRegister.ConsumeRuntimeGas(sdkGasMeter, gasUsed) return resp, err @@ -77,7 +82,9 @@ func (k Keeper) queryContract(ctx context.Context, clientID string, clientStore env := getEnv(k.HeaderService.HeaderInfo(ctx), clientID) - sdkGasMeter.Consume(VMGasRegister.SetupContractCost(true, len(msg)), "Loading CosmWasm module: query") + if err := sdkGasMeter.Consume(VMGasRegister.SetupContractCost(true, len(msg)), "Loading CosmWasm module: query"); err != nil { + return nil, fmt.Errorf("failed to consume gas for Loading Cosmwasm module query: %w", err) + } resp, gasUsed, err := k.GetVM().Query(checksum, env, msg, internaltypes.NewStoreAdapter(clientStore), wasmvmAPI, k.newQueryHandler(ctx, clientID), multipliedGasMeter, gasLimit, types.CostJSONDeserialization) VMGasRegister.ConsumeRuntimeGas(sdkGasMeter, gasUsed) @@ -92,7 +99,9 @@ func (k Keeper) migrateContract(ctx context.Context, clientID string, clientStor env := getEnv(k.HeaderService.HeaderInfo(ctx), clientID) - sdkGasMeter.Consume(VMGasRegister.SetupContractCost(true, len(msg)), "Loading CosmWasm module: migrate") + if err := sdkGasMeter.Consume(VMGasRegister.SetupContractCost(true, len(msg)), "Loading CosmWasm module: migrate"); err != nil { + return nil, fmt.Errorf("failed to consume gas for Loading Cosmwasm module migrate: %w", err) + } resp, gasUsed, err := k.GetVM().Migrate(checksum, env, msg, internaltypes.NewStoreAdapter(clientStore), wasmvmAPI, k.newQueryHandler(ctx, clientID), multipliedGasMeter, gasLimit, types.CostJSONDeserialization) VMGasRegister.ConsumeRuntimeGas(sdkGasMeter, gasUsed) @@ -253,15 +262,15 @@ func unmarshalClientState(cdc codec.BinaryCodec, bz []byte) (exported.ClientStat } // getEnv returns the state of the blockchain environment the contract is running on -func getEnv(header header.Info, contractAddr string) wasmvmtypes.Env { - chainID := header.ChainID - height := header.Height +func getEnv(headerInfo header.Info, contractAddr string) wasmvmtypes.Env { + chainID := headerInfo.ChainID + height := headerInfo.Height // safety checks before casting below if height < 0 { panic(errors.New("block height must never be negative")) } - nsec := header.Time.UnixNano() + nsec := headerInfo.Time.UnixNano() if nsec < 0 { panic(errors.New("block (unix) time must never be negative ")) } diff --git a/modules/light-clients/08-wasm/keeper/events.go b/modules/light-clients/08-wasm/keeper/events.go index 2262f4c4ee1..ce235c75805 100644 --- a/modules/light-clients/08-wasm/keeper/events.go +++ b/modules/light-clients/08-wasm/keeper/events.go @@ -2,36 +2,41 @@ package keeper import ( "encoding/hex" + "errors" + + "cosmossdk.io/core/event" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types" - - "cosmossdk.io/core/event" ) // emitStoreWasmCodeEvent emits a store wasm code event -func emitStoreWasmCodeEvent(em event.Manager, checksum types.Checksum) { - em.EmitKV( - types.EventTypeStoreWasmCode, - event.NewAttribute(types.AttributeKeyWasmChecksum, hex.EncodeToString(checksum)), - ) - em.EmitKV( - sdk.EventTypeMessage, - event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), +func emitStoreWasmCodeEvent(em event.Manager, checksum types.Checksum) error { + return errors.Join( + em.EmitKV( + types.EventTypeStoreWasmCode, + event.NewAttribute(types.AttributeKeyWasmChecksum, hex.EncodeToString(checksum)), + ), + em.EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ), ) } // emitMigrateContractEvent emits a migrate contract event -func emitMigrateContractEvent(em event.Manager, clientID string, checksum, newChecksum types.Checksum) { - em.EmitKV( - types.EventTypeMigrateContract, - event.NewAttribute(types.AttributeKeyClientID, clientID), - event.NewAttribute(types.AttributeKeyWasmChecksum, hex.EncodeToString(checksum)), - event.NewAttribute(types.AttributeKeyNewChecksum, hex.EncodeToString(newChecksum)), - ) - em.EmitKV( - sdk.EventTypeMessage, - event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), +func emitMigrateContractEvent(em event.Manager, clientID string, checksum, newChecksum types.Checksum) error { + return errors.Join( + em.EmitKV( + types.EventTypeMigrateContract, + event.NewAttribute(types.AttributeKeyClientID, clientID), + event.NewAttribute(types.AttributeKeyWasmChecksum, hex.EncodeToString(checksum)), + event.NewAttribute(types.AttributeKeyNewChecksum, hex.EncodeToString(newChecksum)), + ), + em.EmitKV( + sdk.EventTypeMessage, + event.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + ), ) } diff --git a/modules/light-clients/08-wasm/keeper/keeper.go b/modules/light-clients/08-wasm/keeper/keeper.go index fe317700507..1ed37ca49fa 100644 --- a/modules/light-clients/08-wasm/keeper/keeper.go +++ b/modules/light-clients/08-wasm/keeper/keeper.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/hex" + "fmt" wasmvm "github.com/CosmWasm/wasmvm/v2" @@ -13,6 +14,7 @@ import ( errorsmod "cosmossdk.io/errors" "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types" clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types" ) @@ -83,7 +85,9 @@ func (k Keeper) storeWasmCode(ctx context.Context, code []byte, storeFn func(cod meter := k.GasService.GasMeter(ctx) var err error if types.IsGzip(code) { - meter.Consume(types.VMGasRegister.UncompressCosts(len(code)), "Uncompress gzip bytecode") + if err := meter.Consume(types.VMGasRegister.UncompressCosts(len(code)), "Uncompress gzip bytecode"); err != nil { + return nil, fmt.Errorf("failed to consume gas for Uncompress gzip bytecode: %w", err) + } code, err = types.Uncompress(code, types.MaxWasmSize) if err != nil { return nil, errorsmod.Wrap(err, "failed to store contract") @@ -173,7 +177,9 @@ func (k Keeper) migrateContractCode(ctx context.Context, clientID string, newChe k.clientKeeper.SetClientState(ctx, clientID, wasmClientState) - emitMigrateContractEvent(k.EventService.EventManager(ctx), clientID, oldChecksum, newChecksum) + if err = emitMigrateContractEvent(k.EventService.EventManager(ctx), clientID, oldChecksum, newChecksum); err != nil { + return fmt.Errorf("failed to emit migrate contract events: %w", err) + } return nil } diff --git a/modules/light-clients/08-wasm/keeper/keeper_vm.go b/modules/light-clients/08-wasm/keeper/keeper_vm.go index 3f450a156b0..70334a32b74 100644 --- a/modules/light-clients/08-wasm/keeper/keeper_vm.go +++ b/modules/light-clients/08-wasm/keeper/keeper_vm.go @@ -9,11 +9,11 @@ import ( wasmvm "github.com/CosmWasm/wasmvm/v2" - "github.com/cosmos/cosmos-sdk/codec" - "cosmossdk.io/collections" "cosmossdk.io/core/appmodule" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types" ) diff --git a/modules/light-clients/08-wasm/keeper/msg_server.go b/modules/light-clients/08-wasm/keeper/msg_server.go index b3e9afd63da..ec341730f2c 100644 --- a/modules/light-clients/08-wasm/keeper/msg_server.go +++ b/modules/light-clients/08-wasm/keeper/msg_server.go @@ -3,6 +3,7 @@ package keeper import ( "context" "encoding/hex" + "fmt" errorsmod "cosmossdk.io/errors" @@ -26,7 +27,9 @@ func (k Keeper) StoreCode(goCtx context.Context, msg *types.MsgStoreCode) (*type return nil, errorsmod.Wrap(err, "failed to store wasm bytecode") } - emitStoreWasmCodeEvent(k.EventService.EventManager(ctx), checksum) + if err := emitStoreWasmCodeEvent(k.EventService.EventManager(ctx), checksum); err != nil { + return nil, fmt.Errorf("failed to emit store wasm code events: %w", err) + } return &types.MsgStoreCodeResponse{ Checksum: checksum, diff --git a/modules/light-clients/08-wasm/keeper/options_test.go b/modules/light-clients/08-wasm/keeper/options_test.go index 2a64e900efb..863894a893c 100644 --- a/modules/light-clients/08-wasm/keeper/options_test.go +++ b/modules/light-clients/08-wasm/keeper/options_test.go @@ -4,10 +4,10 @@ import ( "encoding/json" "errors" - "cosmossdk.io/log" - wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types" + "cosmossdk.io/log" + "github.com/cosmos/cosmos-sdk/runtime" sdk "github.com/cosmos/cosmos-sdk/types" diff --git a/modules/light-clients/08-wasm/keeper/querier.go b/modules/light-clients/08-wasm/keeper/querier.go index 3aae7031afb..389f4d04502 100644 --- a/modules/light-clients/08-wasm/keeper/querier.go +++ b/modules/light-clients/08-wasm/keeper/querier.go @@ -8,11 +8,11 @@ import ( wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types" - sdk "github.com/cosmos/cosmos-sdk/types" - "cosmossdk.io/core/appmodule" errorsmod "cosmossdk.io/errors" + sdk "github.com/cosmos/cosmos-sdk/types" + abci "github.com/cometbft/cometbft/api/cometbft/abci/v1" "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types" diff --git a/modules/light-clients/08-wasm/types/gas_register_custom.go b/modules/light-clients/08-wasm/types/gas_register_custom.go index 1bd8a8a2373..e121829a8a5 100644 --- a/modules/light-clients/08-wasm/types/gas_register_custom.go +++ b/modules/light-clients/08-wasm/types/gas_register_custom.go @@ -1,6 +1,7 @@ package types import ( + "fmt" "math" wasmvm "github.com/CosmWasm/wasmvm/v2" @@ -40,9 +41,11 @@ func (g WasmGasRegister) RuntimeGasForContract(meter gas.Meter) uint64 { return g.ToWasmVMGas(meter.Limit() - consumedToLimit) } -func (g WasmGasRegister) ConsumeRuntimeGas(meter gas.Meter, gas uint64) { - consumed := g.FromWasmVMGas(gas) - meter.Consume(consumed, "wasm contract") +func (g WasmGasRegister) ConsumeRuntimeGas(meter gas.Meter, gasAmt uint64) { + consumed := g.FromWasmVMGas(gasAmt) + if err := meter.Consume(consumed, "wasm contract"); err != nil { + panic(fmt.Errorf("ConsumeRuntimeGas: failed to consume gas: %w", err)) + } // TODO(technicallyty): this used to be a meter.IsOutOfGas check, which has since been removed from the gas meter iface. // We use the InfiniteGasMeter in tests, which would ALWAYS return false to IsOutOfGas. Now that we don't have that method, From 5b824009af47a16e9eb8cc5f5b1a08c5f8ba3cb6 Mon Sep 17 00:00:00 2001 From: Tyler <48813565+technicallyty@users.noreply.github.com> Date: Fri, 24 Jan 2025 10:46:15 -0800 Subject: [PATCH 13/24] import ordering --- modules/light-clients/08-wasm/keeper/options_test.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/modules/light-clients/08-wasm/keeper/options_test.go b/modules/light-clients/08-wasm/keeper/options_test.go index 863894a893c..5d27d4276a4 100644 --- a/modules/light-clients/08-wasm/keeper/options_test.go +++ b/modules/light-clients/08-wasm/keeper/options_test.go @@ -5,14 +5,12 @@ import ( "errors" wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types" - - "cosmossdk.io/log" - "github.com/cosmos/cosmos-sdk/runtime" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/keeper" "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types" + + "cosmossdk.io/log" ) func mockErrorCustomQuerier() func(sdk.Context, json.RawMessage) ([]byte, error) { From 5ba53b8edcf876a6b571b55c4e1265e74171df51 Mon Sep 17 00:00:00 2001 From: Tyler <48813565+technicallyty@users.noreply.github.com> Date: Fri, 24 Jan 2025 10:55:16 -0800 Subject: [PATCH 14/24] linter --- modules/light-clients/08-wasm/keeper/options_test.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/modules/light-clients/08-wasm/keeper/options_test.go b/modules/light-clients/08-wasm/keeper/options_test.go index 5d27d4276a4..863894a893c 100644 --- a/modules/light-clients/08-wasm/keeper/options_test.go +++ b/modules/light-clients/08-wasm/keeper/options_test.go @@ -5,12 +5,14 @@ import ( "errors" wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types" + + "cosmossdk.io/log" + "github.com/cosmos/cosmos-sdk/runtime" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/keeper" "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types" - - "cosmossdk.io/log" ) func mockErrorCustomQuerier() func(sdk.Context, json.RawMessage) ([]byte, error) { From 90c40f87021e5e531c6b4acc99ec3694cff3eccd Mon Sep 17 00:00:00 2001 From: Tyler <48813565+technicallyty@users.noreply.github.com> Date: Fri, 24 Jan 2025 11:09:02 -0800 Subject: [PATCH 15/24] update queryHandler commentg --- modules/light-clients/08-wasm/keeper/querier.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/light-clients/08-wasm/keeper/querier.go b/modules/light-clients/08-wasm/keeper/querier.go index 389f4d04502..e833f07b960 100644 --- a/modules/light-clients/08-wasm/keeper/querier.go +++ b/modules/light-clients/08-wasm/keeper/querier.go @@ -37,7 +37,7 @@ var defaultAcceptList = []string{ "/ibc.core.client.v1.Query/VerifyMembership", } -// queryHandler is a wrapper around the sdk.Context and the CallerID that calls +// queryHandler is a wrapper around the sdk Environment and the CallerID that calls // into the query plugins. type queryHandler struct { appmodule.Environment From e64bf5ea0d7bd91cf7828f5b3c71fe6a3c2d66a7 Mon Sep 17 00:00:00 2001 From: Tyler <48813565+technicallyty@users.noreply.github.com> Date: Fri, 24 Jan 2025 13:04:58 -0800 Subject: [PATCH 16/24] comment --- modules/light-clients/08-wasm/types/gas_register_custom.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/modules/light-clients/08-wasm/types/gas_register_custom.go b/modules/light-clients/08-wasm/types/gas_register_custom.go index e121829a8a5..74e84222b17 100644 --- a/modules/light-clients/08-wasm/types/gas_register_custom.go +++ b/modules/light-clients/08-wasm/types/gas_register_custom.go @@ -6,6 +6,7 @@ import ( wasmvm "github.com/CosmWasm/wasmvm/v2" wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types" + sdk "github.com/cosmos/cosmos-sdk/types" "cosmossdk.io/core/gas" storetypes "cosmossdk.io/store/types" @@ -47,7 +48,8 @@ func (g WasmGasRegister) ConsumeRuntimeGas(meter gas.Meter, gasAmt uint64) { panic(fmt.Errorf("ConsumeRuntimeGas: failed to consume gas: %w", err)) } - // TODO(technicallyty): this used to be a meter.IsOutOfGas check, which has since been removed from the gas meter iface. + sdk.Context{}.GasMeter().IsOutOfGas() + // This used to be a meter.IsOutOfGas check, which has since been removed from the gas meter iface. // We use the InfiniteGasMeter in tests, which would ALWAYS return false to IsOutOfGas. Now that we don't have that method, // we have to include this weird hack that essentially checks if we're out of gas and we're not the infinite gas meter. // Please don't replicate this if you can. It is ugly. From 54aa3b45b89ce98806564b42de515e78a2e0734d Mon Sep 17 00:00:00 2001 From: Tyler <48813565+technicallyty@users.noreply.github.com> Date: Fri, 24 Jan 2025 13:37:59 -0800 Subject: [PATCH 17/24] fix gas check --- .../light-clients/08-wasm/types/gas_register_custom.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/modules/light-clients/08-wasm/types/gas_register_custom.go b/modules/light-clients/08-wasm/types/gas_register_custom.go index 74e84222b17..524bae342d9 100644 --- a/modules/light-clients/08-wasm/types/gas_register_custom.go +++ b/modules/light-clients/08-wasm/types/gas_register_custom.go @@ -6,7 +6,6 @@ import ( wasmvm "github.com/CosmWasm/wasmvm/v2" wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types" - sdk "github.com/cosmos/cosmos-sdk/types" "cosmossdk.io/core/gas" storetypes "cosmossdk.io/store/types" @@ -48,12 +47,7 @@ func (g WasmGasRegister) ConsumeRuntimeGas(meter gas.Meter, gasAmt uint64) { panic(fmt.Errorf("ConsumeRuntimeGas: failed to consume gas: %w", err)) } - sdk.Context{}.GasMeter().IsOutOfGas() - // This used to be a meter.IsOutOfGas check, which has since been removed from the gas meter iface. - // We use the InfiniteGasMeter in tests, which would ALWAYS return false to IsOutOfGas. Now that we don't have that method, - // we have to include this weird hack that essentially checks if we're out of gas and we're not the infinite gas meter. - // Please don't replicate this if you can. It is ugly. - if meter.Remaining() >= meter.Limit() && (meter.Limit() != math.MaxUint64 && meter.Remaining() != math.MaxUint64) { + if meter.Consumed() >= meter.Limit() { // throw OutOfGas error if we ran out (got exactly to zero due to better limit enforcing) panic(storetypes.ErrorOutOfGas{Descriptor: "Wasmer function execution"}) } From 0d8f78bc31c4e199abca0af734bd59b7fbffc569 Mon Sep 17 00:00:00 2001 From: Tyler <48813565+technicallyty@users.noreply.github.com> Date: Mon, 27 Jan 2025 11:22:41 -0800 Subject: [PATCH 18/24] make event emission functions methods on keeper --- modules/light-clients/08-wasm/keeper/events.go | 7 +++++-- modules/light-clients/08-wasm/keeper/keeper.go | 2 +- modules/light-clients/08-wasm/keeper/msg_server.go | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/modules/light-clients/08-wasm/keeper/events.go b/modules/light-clients/08-wasm/keeper/events.go index ce235c75805..bd6de954342 100644 --- a/modules/light-clients/08-wasm/keeper/events.go +++ b/modules/light-clients/08-wasm/keeper/events.go @@ -1,6 +1,7 @@ package keeper import ( + "context" "encoding/hex" "errors" @@ -12,7 +13,8 @@ import ( ) // emitStoreWasmCodeEvent emits a store wasm code event -func emitStoreWasmCodeEvent(em event.Manager, checksum types.Checksum) error { +func (k Keeper) emitStoreWasmCodeEvent(ctx context.Context, checksum types.Checksum) error { + em := k.EventService.EventManager(ctx) return errors.Join( em.EmitKV( types.EventTypeStoreWasmCode, @@ -26,7 +28,8 @@ func emitStoreWasmCodeEvent(em event.Manager, checksum types.Checksum) error { } // emitMigrateContractEvent emits a migrate contract event -func emitMigrateContractEvent(em event.Manager, clientID string, checksum, newChecksum types.Checksum) error { +func (k Keeper) emitMigrateContractEvent(ctx context.Context, clientID string, checksum, newChecksum types.Checksum) error { + em := k.EventService.EventManager(ctx) return errors.Join( em.EmitKV( types.EventTypeMigrateContract, diff --git a/modules/light-clients/08-wasm/keeper/keeper.go b/modules/light-clients/08-wasm/keeper/keeper.go index 1ed37ca49fa..79e2bc3c62d 100644 --- a/modules/light-clients/08-wasm/keeper/keeper.go +++ b/modules/light-clients/08-wasm/keeper/keeper.go @@ -177,7 +177,7 @@ func (k Keeper) migrateContractCode(ctx context.Context, clientID string, newChe k.clientKeeper.SetClientState(ctx, clientID, wasmClientState) - if err = emitMigrateContractEvent(k.EventService.EventManager(ctx), clientID, oldChecksum, newChecksum); err != nil { + if err = k.emitMigrateContractEvent(ctx, clientID, oldChecksum, newChecksum); err != nil { return fmt.Errorf("failed to emit migrate contract events: %w", err) } diff --git a/modules/light-clients/08-wasm/keeper/msg_server.go b/modules/light-clients/08-wasm/keeper/msg_server.go index ec341730f2c..bc7d5a518a1 100644 --- a/modules/light-clients/08-wasm/keeper/msg_server.go +++ b/modules/light-clients/08-wasm/keeper/msg_server.go @@ -27,7 +27,7 @@ func (k Keeper) StoreCode(goCtx context.Context, msg *types.MsgStoreCode) (*type return nil, errorsmod.Wrap(err, "failed to store wasm bytecode") } - if err := emitStoreWasmCodeEvent(k.EventService.EventManager(ctx), checksum); err != nil { + if err := k.emitStoreWasmCodeEvent(ctx, checksum); err != nil { return nil, fmt.Errorf("failed to emit store wasm code events: %w", err) } From 0aaffc74d5de0b44c196d7a88663aa9d4b8f557b Mon Sep 17 00:00:00 2001 From: Tyler <48813565+technicallyty@users.noreply.github.com> Date: Mon, 27 Jan 2025 11:23:28 -0800 Subject: [PATCH 19/24] remove context argument from logger method --- modules/light-clients/08-wasm/keeper/keeper.go | 2 +- modules/light-clients/08-wasm/keeper/migrations.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/light-clients/08-wasm/keeper/keeper.go b/modules/light-clients/08-wasm/keeper/keeper.go index 79e2bc3c62d..414ce52e097 100644 --- a/modules/light-clients/08-wasm/keeper/keeper.go +++ b/modules/light-clients/08-wasm/keeper/keeper.go @@ -48,7 +48,7 @@ func (k Keeper) GetAuthority() string { } // Logger returns a module-specific logger. -func (k Keeper) Logger(_ context.Context) log.Logger { +func (k Keeper) Logger() log.Logger { return k.Environment.Logger } diff --git a/modules/light-clients/08-wasm/keeper/migrations.go b/modules/light-clients/08-wasm/keeper/migrations.go index d4f29dc9fbe..4165499a268 100644 --- a/modules/light-clients/08-wasm/keeper/migrations.go +++ b/modules/light-clients/08-wasm/keeper/migrations.go @@ -44,7 +44,7 @@ func (m Migrator) MigrateChecksums(goCtx context.Context) error { return err } - m.keeper.Logger(ctx).Info("successfully migrated Checksums to collections") + m.keeper.Logger().Info("successfully migrated Checksums to collections") return nil } From 830373922438c0da738ee97eeccf92a52eed5d32 Mon Sep 17 00:00:00 2001 From: Tyler <48813565+technicallyty@users.noreply.github.com> Date: Mon, 27 Jan 2025 15:05:06 -0800 Subject: [PATCH 20/24] remove unwraps where possible --- .../08-wasm/keeper/contract_keeper.go | 6 ++-- .../08-wasm/keeper/grpc_query.go | 4 +-- .../light-clients/08-wasm/keeper/keeper_vm.go | 8 ++--- .../08-wasm/keeper/migrations.go | 3 +- .../08-wasm/keeper/msg_server.go | 3 +- .../08-wasm/keeper/options_test.go | 9 ++--- .../light-clients/08-wasm/keeper/querier.go | 18 +++++----- .../08-wasm/keeper/querier_test.go | 6 +++- .../08-wasm/light_client_module.go | 33 +++++++------------ .../08-wasm/testing/simapp/app.go | 7 ++-- 10 files changed, 45 insertions(+), 52 deletions(-) diff --git a/modules/light-clients/08-wasm/keeper/contract_keeper.go b/modules/light-clients/08-wasm/keeper/contract_keeper.go index f7d904b4563..1df6760feba 100644 --- a/modules/light-clients/08-wasm/keeper/contract_keeper.go +++ b/modules/light-clients/08-wasm/keeper/contract_keeper.go @@ -116,7 +116,7 @@ func (k Keeper) WasmInstantiate(ctx context.Context, clientID string, clientStor } checksum := cs.Checksum - res, err := k.instantiateContract(sdk.UnwrapSDKContext(ctx), clientID, clientStore, checksum, encodedData) + res, err := k.instantiateContract(ctx, clientID, clientStore, checksum, encodedData) if err != nil { return errorsmod.Wrap(types.ErrVMError, err.Error()) } @@ -155,7 +155,7 @@ func (k Keeper) WasmSudo(ctx context.Context, clientID string, clientStore store } checksum := cs.Checksum - res, err := k.callContract(sdk.UnwrapSDKContext(ctx), clientID, clientStore, checksum, encodedData) + res, err := k.callContract(ctx, clientID, clientStore, checksum, encodedData) if err != nil { return nil, errorsmod.Wrap(types.ErrVMError, err.Error()) } @@ -210,7 +210,7 @@ func (k Keeper) WasmQuery(ctx context.Context, clientID string, clientStore stor return nil, errorsmod.Wrap(err, "failed to marshal payload for wasm query") } - res, err := k.queryContract(sdk.UnwrapSDKContext(ctx), clientID, clientStore, cs.Checksum, encodedData) + res, err := k.queryContract(ctx, clientID, clientStore, cs.Checksum, encodedData) if err != nil { return nil, errorsmod.Wrap(types.ErrVMError, err.Error()) } diff --git a/modules/light-clients/08-wasm/keeper/grpc_query.go b/modules/light-clients/08-wasm/keeper/grpc_query.go index 21b2a558894..12b08b669c3 100644 --- a/modules/light-clients/08-wasm/keeper/grpc_query.go +++ b/modules/light-clients/08-wasm/keeper/grpc_query.go @@ -19,7 +19,7 @@ import ( var _ types.QueryServer = (*Keeper)(nil) // Code implements the Query/Code gRPC method -func (k Keeper) Code(goCtx context.Context, req *types.QueryCodeRequest) (*types.QueryCodeResponse, error) { +func (k Keeper) Code(ctx context.Context, req *types.QueryCodeRequest) (*types.QueryCodeResponse, error) { if req == nil { return nil, status.Error(codes.InvalidArgument, "empty request") } @@ -30,7 +30,7 @@ func (k Keeper) Code(goCtx context.Context, req *types.QueryCodeRequest) (*types } // Only return checksums we previously stored, not arbitrary checksums that might be stored via e.g Wasmd. - if !k.HasChecksum(sdk.UnwrapSDKContext(goCtx), checksum) { + if !k.HasChecksum(ctx, checksum) { return nil, status.Error(codes.NotFound, errorsmod.Wrap(types.ErrWasmChecksumNotFound, req.Checksum).Error()) } diff --git a/modules/light-clients/08-wasm/keeper/keeper_vm.go b/modules/light-clients/08-wasm/keeper/keeper_vm.go index 70334a32b74..c544777615a 100644 --- a/modules/light-clients/08-wasm/keeper/keeper_vm.go +++ b/modules/light-clients/08-wasm/keeper/keeper_vm.go @@ -26,7 +26,6 @@ func NewKeeperWithVM( clientKeeper types.ClientKeeper, authority string, vm types.WasmEngine, - queryRouter types.QueryRouter, opts ...Option, ) Keeper { if clientKeeper == nil { @@ -37,7 +36,7 @@ func NewKeeperWithVM( panic(errors.New("store service must not be nil")) } - if queryRouter == nil { + if env.QueryRouterService == nil { panic(errors.New("query router must not be nil")) } @@ -67,7 +66,7 @@ func NewKeeperWithVM( // set query plugins to ensure there is a non-nil query plugin // regardless of what options the user provides - keeper.setQueryPlugins(NewDefaultQueryPlugins(queryRouter)) + keeper.setQueryPlugins(NewDefaultQueryPlugins(keeper.QueryRouterService)) for _, opt := range opts { opt.apply(keeper) @@ -85,7 +84,6 @@ func NewKeeperWithConfig( clientKeeper types.ClientKeeper, authority string, wasmConfig types.WasmConfig, - queryRouter types.QueryRouter, opts ...Option, ) Keeper { vm, err := wasmvm.NewVM(wasmConfig.DataDir, wasmConfig.SupportedCapabilities, types.ContractMemoryLimit, wasmConfig.ContractDebugMode, types.MemoryCacheSize) @@ -93,5 +91,5 @@ func NewKeeperWithConfig( panic(fmt.Errorf("failed to instantiate new Wasm VM instance: %v", err)) } - return NewKeeperWithVM(env, cdc, clientKeeper, authority, vm, queryRouter, opts...) + return NewKeeperWithVM(env, cdc, clientKeeper, authority, vm, opts...) } diff --git a/modules/light-clients/08-wasm/keeper/migrations.go b/modules/light-clients/08-wasm/keeper/migrations.go index 4165499a268..7e940cd93b3 100644 --- a/modules/light-clients/08-wasm/keeper/migrations.go +++ b/modules/light-clients/08-wasm/keeper/migrations.go @@ -26,8 +26,7 @@ func NewMigrator(keeper Keeper) Migrator { // It grabs the checksums stored previously under the old key and stores // them in the global KeySet collection. It then deletes the old key and // the checksums stored under it. -func (m Migrator) MigrateChecksums(goCtx context.Context) error { - ctx := sdk.UnwrapSDKContext(goCtx) +func (m Migrator) MigrateChecksums(ctx context.Context) error { checksums, err := m.getStoredChecksums(ctx) if err != nil { return err diff --git a/modules/light-clients/08-wasm/keeper/msg_server.go b/modules/light-clients/08-wasm/keeper/msg_server.go index bc7d5a518a1..6f0445b18ce 100644 --- a/modules/light-clients/08-wasm/keeper/msg_server.go +++ b/modules/light-clients/08-wasm/keeper/msg_server.go @@ -16,12 +16,11 @@ import ( var _ types.MsgServer = (*Keeper)(nil) // StoreCode defines a rpc handler method for MsgStoreCode -func (k Keeper) StoreCode(goCtx context.Context, msg *types.MsgStoreCode) (*types.MsgStoreCodeResponse, error) { +func (k Keeper) StoreCode(ctx context.Context, msg *types.MsgStoreCode) (*types.MsgStoreCodeResponse, error) { if k.GetAuthority() != msg.Signer { return nil, errorsmod.Wrapf(ibcerrors.ErrUnauthorized, "expected %s, got %s", k.GetAuthority(), msg.Signer) } - ctx := sdk.UnwrapSDKContext(goCtx) checksum, err := k.storeWasmCode(ctx, msg.WasmByteCode, k.GetVM().StoreCode) if err != nil { return nil, errorsmod.Wrap(err, "failed to store wasm bytecode") diff --git a/modules/light-clients/08-wasm/keeper/options_test.go b/modules/light-clients/08-wasm/keeper/options_test.go index 863894a893c..3256cba2c92 100644 --- a/modules/light-clients/08-wasm/keeper/options_test.go +++ b/modules/light-clients/08-wasm/keeper/options_test.go @@ -1,6 +1,7 @@ package keeper_test import ( + "context" "encoding/json" "errors" @@ -15,14 +16,14 @@ import ( "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types" ) -func mockErrorCustomQuerier() func(sdk.Context, json.RawMessage) ([]byte, error) { - return func(_ sdk.Context, _ json.RawMessage) ([]byte, error) { +func mockErrorCustomQuerier() func(context.Context, json.RawMessage) ([]byte, error) { + return func(_ context.Context, _ json.RawMessage) ([]byte, error) { return nil, errors.New("custom querier error for TestNewKeeperWithOptions") } } -func mockErrorStargateQuerier() func(sdk.Context, *wasmvmtypes.StargateQuery) ([]byte, error) { - return func(_ sdk.Context, _ *wasmvmtypes.StargateQuery) ([]byte, error) { +func mockErrorStargateQuerier() func(context.Context, *wasmvmtypes.StargateQuery) ([]byte, error) { + return func(_ context.Context, _ *wasmvmtypes.StargateQuery) ([]byte, error) { return nil, errors.New("stargate querier error for TestNewKeeperWithOptions") } } diff --git a/modules/light-clients/08-wasm/keeper/querier.go b/modules/light-clients/08-wasm/keeper/querier.go index e833f07b960..c167f44fd96 100644 --- a/modules/light-clients/08-wasm/keeper/querier.go +++ b/modules/light-clients/08-wasm/keeper/querier.go @@ -68,7 +68,7 @@ func (q *queryHandler) Query(request wasmvmtypes.QueryRequest, gasLimit uint64) var res []byte _, err := q.BranchService.ExecuteWithGasLimit(q.Ctx, sdkGasLimit, func(ctx context.Context) error { var err error - res, err = q.Plugins.HandleQuery(sdk.UnwrapSDKContext(ctx), q.CallerID, request) + res, err = q.Plugins.HandleQuery(ctx, q.CallerID, request) if err == nil { return nil } @@ -83,8 +83,8 @@ func (q *queryHandler) Query(request wasmvmtypes.QueryRequest, gasLimit uint64) } type ( - CustomQuerier func(ctx sdk.Context, request json.RawMessage) ([]byte, error) - StargateQuerier func(ctx sdk.Context, request *wasmvmtypes.StargateQuery) ([]byte, error) + CustomQuerier func(ctx context.Context, request json.RawMessage) ([]byte, error) + StargateQuerier func(ctx context.Context, request *wasmvmtypes.StargateQuery) ([]byte, error) // QueryPlugins is a list of queriers that can be used to extend the default querier. QueryPlugins struct { @@ -112,7 +112,7 @@ func (e QueryPlugins) Merge(x *QueryPlugins) QueryPlugins { } // HandleQuery implements the ibcwasm.QueryPluginsI interface. -func (e QueryPlugins) HandleQuery(ctx sdk.Context, caller string, request wasmvmtypes.QueryRequest) ([]byte, error) { +func (e QueryPlugins) HandleQuery(ctx context.Context, caller string, request wasmvmtypes.QueryRequest) ([]byte, error) { if request.Stargate != nil { return e.Stargate(ctx, request.Stargate) } @@ -134,8 +134,8 @@ func NewDefaultQueryPlugins(queryRouter types.QueryRouter) QueryPlugins { // AcceptListStargateQuerier allows all queries that are in the provided accept list. // This function returns protobuf encoded responses in bytes. -func AcceptListStargateQuerier(acceptedQueries []string, queryRouter types.QueryRouter) func(sdk.Context, *wasmvmtypes.StargateQuery) ([]byte, error) { - return func(ctx sdk.Context, request *wasmvmtypes.StargateQuery) ([]byte, error) { +func AcceptListStargateQuerier(acceptedQueries []string, queryRouter types.QueryRouter) func(context.Context, *wasmvmtypes.StargateQuery) ([]byte, error) { + return func(ctx context.Context, request *wasmvmtypes.StargateQuery) ([]byte, error) { // append user defined accepted queries to default list defined above. acceptedQueries = append(defaultAcceptList, acceptedQueries...) @@ -149,7 +149,7 @@ func AcceptListStargateQuerier(acceptedQueries []string, queryRouter types.Query return nil, wasmvmtypes.UnsupportedRequest{Kind: fmt.Sprintf("No route to query '%s'", request.Path)} } - res, err := route(ctx, &abci.QueryRequest{ + res, err := route(sdk.UnwrapSDKContext(ctx), &abci.QueryRequest{ Data: request.Data, Path: request.Path, }) @@ -165,8 +165,8 @@ func AcceptListStargateQuerier(acceptedQueries []string, queryRouter types.Query } // RejectCustomQuerier rejects all custom queries -func RejectCustomQuerier() func(sdk.Context, json.RawMessage) ([]byte, error) { - return func(ctx sdk.Context, request json.RawMessage) ([]byte, error) { +func RejectCustomQuerier() func(context.Context, json.RawMessage) ([]byte, error) { + return func(ctx context.Context, request json.RawMessage) ([]byte, error) { return nil, wasmvmtypes.UnsupportedRequest{Kind: "Custom queries are not allowed"} } } diff --git a/modules/light-clients/08-wasm/keeper/querier_test.go b/modules/light-clients/08-wasm/keeper/querier_test.go index e1b31efe032..5d0bf44e950 100644 --- a/modules/light-clients/08-wasm/keeper/querier_test.go +++ b/modules/light-clients/08-wasm/keeper/querier_test.go @@ -8,6 +8,7 @@ import ( wasmvm "github.com/CosmWasm/wasmvm/v2" wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types" + "github.com/cosmos/cosmos-sdk/runtime" sdk "github.com/cosmos/cosmos-sdk/types" @@ -17,6 +18,8 @@ import ( clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types" commitmenttypes "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types" "github.com/cosmos/ibc-go/v9/modules/core/exported" + + "cosmossdk.io/core/router" ) type CustomQuery struct { @@ -153,8 +156,9 @@ func (suite *KeeperTestSuite) TestStargateQuery() { { "success: custom query", func() { + rtr := runtime.NewQueryRouterService(GetSimApp(suite.chainA).GRPCQueryRouter()) querierPlugin := keeper.QueryPlugins{ - Stargate: keeper.AcceptListStargateQuerier([]string{typeURL}, GetSimApp(suite.chainA).GRPCQueryRouter()), + Stargate: keeper.AcceptListStargateQuerier([]string{typeURL}, rtr), } GetSimApp(suite.chainA).WasmClientKeeper.SetQueryPlugins(querierPlugin) diff --git a/modules/light-clients/08-wasm/light_client_module.go b/modules/light-clients/08-wasm/light_client_module.go index cd88887418e..4f34f15ea46 100644 --- a/modules/light-clients/08-wasm/light_client_module.go +++ b/modules/light-clients/08-wasm/light_client_module.go @@ -71,8 +71,7 @@ func (l LightClientModule) Initialize(ctx context.Context, clientID string, clie Checksum: clientState.Checksum, } - sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/5917 - return l.keeper.WasmInstantiate(sdkCtx, clientID, clientStore, &clientState, payload) + return l.keeper.WasmInstantiate(ctx, clientID, clientStore, &clientState, payload) } // VerifyClientMessage obtains the client state associated with the client identifier, it then must verify the ClientMessage. @@ -97,8 +96,7 @@ func (l LightClientModule) VerifyClientMessage(ctx context.Context, clientID str payload := types.QueryMsg{ VerifyClientMessage: &types.VerifyClientMessageMsg{ClientMessage: clientMessage.Data}, } - sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/5917 - _, err := l.keeper.WasmQuery(sdkCtx, clientID, clientStore, clientState, payload) + _, err := l.keeper.WasmQuery(ctx, clientID, clientStore, clientState, payload) return err } @@ -122,8 +120,7 @@ func (l LightClientModule) CheckForMisbehaviour(ctx context.Context, clientID st CheckForMisbehaviour: &types.CheckForMisbehaviourMsg{ClientMessage: clientMessage.Data}, } - sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/5917 - res, err := l.keeper.WasmQuery(sdkCtx, clientID, clientStore, clientState, payload) + res, err := l.keeper.WasmQuery(ctx, clientID, clientStore, clientState, payload) if err != nil { return false } @@ -157,8 +154,7 @@ func (l LightClientModule) UpdateStateOnMisbehaviour(ctx context.Context, client UpdateStateOnMisbehaviour: &types.UpdateStateOnMisbehaviourMsg{ClientMessage: clientMessage.Data}, } - sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/5917 - _, err := l.keeper.WasmSudo(sdkCtx, clientID, clientStore, clientState, payload) + _, err := l.keeper.WasmSudo(ctx, clientID, clientStore, clientState, payload) if err != nil { panic(err) } @@ -184,8 +180,7 @@ func (l LightClientModule) UpdateState(ctx context.Context, clientID string, cli UpdateState: &types.UpdateStateMsg{ClientMessage: clientMessage.Data}, } - sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/5917 - res, err := l.keeper.WasmSudo(sdkCtx, clientID, clientStore, clientState, payload) + res, err := l.keeper.WasmSudo(ctx, clientID, clientStore, clientState, payload) if err != nil { panic(err) } @@ -253,8 +248,7 @@ func (l LightClientModule) VerifyMembership( }, } - sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/5917 - _, err := l.keeper.WasmSudo(sdkCtx, clientID, clientStore, clientState, payload) + _, err := l.keeper.WasmSudo(ctx, clientID, clientStore, clientState, payload) return err } @@ -306,8 +300,7 @@ func (l LightClientModule) VerifyNonMembership( }, } - sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/5917 - _, err := l.keeper.WasmSudo(sdkCtx, clientID, clientStore, clientState, payload) + _, err := l.keeper.WasmSudo(ctx, clientID, clientStore, clientState, payload) return err } @@ -336,8 +329,7 @@ func (l LightClientModule) Status(ctx context.Context, clientID string) exported } payload := types.QueryMsg{Status: &types.StatusMsg{}} - sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/5917 - res, err := l.keeper.WasmQuery(sdkCtx, clientID, clientStore, clientState, payload) + res, err := l.keeper.WasmQuery(ctx, clientID, clientStore, clientState, payload) if err != nil { return exported.Unknown } @@ -386,8 +378,7 @@ func (l LightClientModule) TimestampAtHeight(ctx context.Context, clientID strin }, } - sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/5917 - res, err := l.keeper.WasmQuery(sdkCtx, clientID, clientStore, clientState, payload) + res, err := l.keeper.WasmQuery(ctx, clientID, clientStore, clientState, payload) if err != nil { return 0, errorsmod.Wrapf(err, "height (%s)", height) } @@ -440,8 +431,7 @@ func (l LightClientModule) RecoverClient(ctx context.Context, clientID, substitu MigrateClientStore: &types.MigrateClientStoreMsg{}, } - sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/5917 - _, err = l.keeper.WasmSudo(sdkCtx, clientID, store, subjectClientState, payload) + _, err = l.keeper.WasmSudo(ctx, clientID, store, subjectClientState, payload) return err } @@ -489,7 +479,6 @@ func (l LightClientModule) VerifyUpgradeAndUpdateState( }, } - sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/5917 - _, err := l.keeper.WasmSudo(sdkCtx, clientID, clientStore, clientState, payload) + _, err := l.keeper.WasmSudo(ctx, clientID, clientStore, clientState, payload) return err } diff --git a/modules/light-clients/08-wasm/testing/simapp/app.go b/modules/light-clients/08-wasm/testing/simapp/app.go index 1419831038d..1df19fc9c6f 100644 --- a/modules/light-clients/08-wasm/testing/simapp/app.go +++ b/modules/light-clients/08-wasm/testing/simapp/app.go @@ -552,9 +552,12 @@ func NewSimApp( ) } else { app.WasmClientKeeper = wasmkeeper.NewKeeperWithConfig( - runtime.NewEnvironment(runtime.NewKVStoreService(keys[wasmtypes.StoreKey]), logger.With(log.ModuleKey, "x/ibc-wasm")), + runtime.NewEnvironment( + runtime.NewKVStoreService(keys[wasmtypes.StoreKey]), logger.With(log.ModuleKey, "x/ibc-wasm"), + runtime.EnvWithQueryRouterService(app.GRPCQueryRouter()), + ), appCodec, app.IBCKeeper.ClientKeeper, - authtypes.NewModuleAddress(govtypes.ModuleName).String(), wasmConfig, app.GRPCQueryRouter(), + authtypes.NewModuleAddress(govtypes.ModuleName).String(), wasmConfig, ) } From 3ac42feb220ed8132d1909dba25e623749028b6e Mon Sep 17 00:00:00 2001 From: Tyler <48813565+technicallyty@users.noreply.github.com> Date: Mon, 27 Jan 2025 15:58:12 -0800 Subject: [PATCH 21/24] back to queyr router --- modules/light-clients/08-wasm/keeper/keeper.go | 2 ++ modules/light-clients/08-wasm/keeper/keeper_vm.go | 9 ++++++--- modules/light-clients/08-wasm/keeper/msg_server.go | 2 -- .../light-clients/08-wasm/keeper/querier_test.go | 14 ++++---------- .../light-clients/08-wasm/light_client_module.go | 2 -- .../light-clients/08-wasm/testing/simapp/app.go | 6 ++---- 6 files changed, 14 insertions(+), 21 deletions(-) diff --git a/modules/light-clients/08-wasm/keeper/keeper.go b/modules/light-clients/08-wasm/keeper/keeper.go index 414ce52e097..c4e9722c59b 100644 --- a/modules/light-clients/08-wasm/keeper/keeper.go +++ b/modules/light-clients/08-wasm/keeper/keeper.go @@ -34,6 +34,8 @@ type Keeper struct { queryPlugins QueryPlugins + queryRouter types.QueryRouter + authority string } diff --git a/modules/light-clients/08-wasm/keeper/keeper_vm.go b/modules/light-clients/08-wasm/keeper/keeper_vm.go index c544777615a..e5a07a82e41 100644 --- a/modules/light-clients/08-wasm/keeper/keeper_vm.go +++ b/modules/light-clients/08-wasm/keeper/keeper_vm.go @@ -26,6 +26,7 @@ func NewKeeperWithVM( clientKeeper types.ClientKeeper, authority string, vm types.WasmEngine, + queryRouter types.QueryRouter, opts ...Option, ) Keeper { if clientKeeper == nil { @@ -36,7 +37,7 @@ func NewKeeperWithVM( panic(errors.New("store service must not be nil")) } - if env.QueryRouterService == nil { + if queryRouter == nil { panic(errors.New("query router must not be nil")) } @@ -57,6 +58,7 @@ func NewKeeperWithVM( checksums: collections.NewKeySet(sb, types.ChecksumsKey, "checksums", collections.BytesKey), clientKeeper: clientKeeper, authority: authority, + queryRouter: queryRouter, } _, err := sb.Build() @@ -66,7 +68,7 @@ func NewKeeperWithVM( // set query plugins to ensure there is a non-nil query plugin // regardless of what options the user provides - keeper.setQueryPlugins(NewDefaultQueryPlugins(keeper.QueryRouterService)) + keeper.setQueryPlugins(NewDefaultQueryPlugins(keeper.queryRouter)) for _, opt := range opts { opt.apply(keeper) @@ -84,6 +86,7 @@ func NewKeeperWithConfig( clientKeeper types.ClientKeeper, authority string, wasmConfig types.WasmConfig, + queryRouter types.QueryRouter, opts ...Option, ) Keeper { vm, err := wasmvm.NewVM(wasmConfig.DataDir, wasmConfig.SupportedCapabilities, types.ContractMemoryLimit, wasmConfig.ContractDebugMode, types.MemoryCacheSize) @@ -91,5 +94,5 @@ func NewKeeperWithConfig( panic(fmt.Errorf("failed to instantiate new Wasm VM instance: %v", err)) } - return NewKeeperWithVM(env, cdc, clientKeeper, authority, vm, opts...) + return NewKeeperWithVM(env, cdc, clientKeeper, authority, vm, queryRouter, opts...) } diff --git a/modules/light-clients/08-wasm/keeper/msg_server.go b/modules/light-clients/08-wasm/keeper/msg_server.go index 6f0445b18ce..45a9422f355 100644 --- a/modules/light-clients/08-wasm/keeper/msg_server.go +++ b/modules/light-clients/08-wasm/keeper/msg_server.go @@ -7,8 +7,6 @@ import ( errorsmod "cosmossdk.io/errors" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types" ibcerrors "github.com/cosmos/ibc-go/v9/modules/core/errors" ) diff --git a/modules/light-clients/08-wasm/keeper/querier_test.go b/modules/light-clients/08-wasm/keeper/querier_test.go index 5d0bf44e950..d739e37b726 100644 --- a/modules/light-clients/08-wasm/keeper/querier_test.go +++ b/modules/light-clients/08-wasm/keeper/querier_test.go @@ -1,6 +1,7 @@ package keeper_test import ( + "context" "encoding/hex" "encoding/json" "fmt" @@ -8,18 +9,12 @@ import ( wasmvm "github.com/CosmWasm/wasmvm/v2" wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types" - "github.com/cosmos/cosmos-sdk/runtime" - - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/keeper" wasmtesting "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/testing" "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types" clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types" commitmenttypes "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types" "github.com/cosmos/ibc-go/v9/modules/core/exported" - - "cosmossdk.io/core/router" ) type CustomQuery struct { @@ -30,8 +25,8 @@ type QueryEcho struct { Data string `json:"data"` } -func mockCustomQuerier() func(sdk.Context, json.RawMessage) ([]byte, error) { - return func(ctx sdk.Context, request json.RawMessage) ([]byte, error) { +func mockCustomQuerier() func(context.Context, json.RawMessage) ([]byte, error) { + return func(ctx context.Context, request json.RawMessage) ([]byte, error) { var customQuery CustomQuery err := json.Unmarshal([]byte(request), &customQuery) if err != nil { @@ -156,9 +151,8 @@ func (suite *KeeperTestSuite) TestStargateQuery() { { "success: custom query", func() { - rtr := runtime.NewQueryRouterService(GetSimApp(suite.chainA).GRPCQueryRouter()) querierPlugin := keeper.QueryPlugins{ - Stargate: keeper.AcceptListStargateQuerier([]string{typeURL}, rtr), + Stargate: keeper.AcceptListStargateQuerier([]string{typeURL}, GetSimApp(suite.chainA).GRPCQueryRouter()), } GetSimApp(suite.chainA).WasmClientKeeper.SetQueryPlugins(querierPlugin) diff --git a/modules/light-clients/08-wasm/light_client_module.go b/modules/light-clients/08-wasm/light_client_module.go index 4f34f15ea46..5b0680e3b49 100644 --- a/modules/light-clients/08-wasm/light_client_module.go +++ b/modules/light-clients/08-wasm/light_client_module.go @@ -9,8 +9,6 @@ import ( errorsmod "cosmossdk.io/errors" - sdk "github.com/cosmos/cosmos-sdk/types" - internaltypes "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/internal/types" wasmkeeper "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/keeper" "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types" diff --git a/modules/light-clients/08-wasm/testing/simapp/app.go b/modules/light-clients/08-wasm/testing/simapp/app.go index 1df19fc9c6f..fd546472a59 100644 --- a/modules/light-clients/08-wasm/testing/simapp/app.go +++ b/modules/light-clients/08-wasm/testing/simapp/app.go @@ -553,11 +553,9 @@ func NewSimApp( } else { app.WasmClientKeeper = wasmkeeper.NewKeeperWithConfig( runtime.NewEnvironment( - runtime.NewKVStoreService(keys[wasmtypes.StoreKey]), logger.With(log.ModuleKey, "x/ibc-wasm"), - runtime.EnvWithQueryRouterService(app.GRPCQueryRouter()), - ), + runtime.NewKVStoreService(keys[wasmtypes.StoreKey]), logger.With(log.ModuleKey, "x/ibc-wasm")), appCodec, app.IBCKeeper.ClientKeeper, - authtypes.NewModuleAddress(govtypes.ModuleName).String(), wasmConfig, + authtypes.NewModuleAddress(govtypes.ModuleName).String(), wasmConfig, app.GRPCQueryRouter(), ) } From b0a7a381274eda66bb0c5cdd525ee0f7eab39054 Mon Sep 17 00:00:00 2001 From: Tyler <48813565+technicallyty@users.noreply.github.com> Date: Mon, 27 Jan 2025 16:01:16 -0800 Subject: [PATCH 22/24] fix --- modules/light-clients/08-wasm/keeper/contract_keeper.go | 1 - modules/light-clients/08-wasm/keeper/grpc_query.go | 1 - modules/light-clients/08-wasm/keeper/keeper.go | 2 -- modules/light-clients/08-wasm/keeper/keeper_vm.go | 3 +-- modules/light-clients/08-wasm/keeper/migrations.go | 2 -- modules/light-clients/08-wasm/keeper/querier_test.go | 1 + 6 files changed, 2 insertions(+), 8 deletions(-) diff --git a/modules/light-clients/08-wasm/keeper/contract_keeper.go b/modules/light-clients/08-wasm/keeper/contract_keeper.go index 1df6760feba..796e7341b81 100644 --- a/modules/light-clients/08-wasm/keeper/contract_keeper.go +++ b/modules/light-clients/08-wasm/keeper/contract_keeper.go @@ -16,7 +16,6 @@ import ( storetypes "cosmossdk.io/store/types" "github.com/cosmos/cosmos-sdk/codec" - sdk "github.com/cosmos/cosmos-sdk/types" internaltypes "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/internal/types" "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types" diff --git a/modules/light-clients/08-wasm/keeper/grpc_query.go b/modules/light-clients/08-wasm/keeper/grpc_query.go index 12b08b669c3..a2907499561 100644 --- a/modules/light-clients/08-wasm/keeper/grpc_query.go +++ b/modules/light-clients/08-wasm/keeper/grpc_query.go @@ -10,7 +10,6 @@ import ( "cosmossdk.io/collections" errorsmod "cosmossdk.io/errors" - sdk "github.com/cosmos/cosmos-sdk/types" sdkquery "github.com/cosmos/cosmos-sdk/types/query" "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types" diff --git a/modules/light-clients/08-wasm/keeper/keeper.go b/modules/light-clients/08-wasm/keeper/keeper.go index c4e9722c59b..414ce52e097 100644 --- a/modules/light-clients/08-wasm/keeper/keeper.go +++ b/modules/light-clients/08-wasm/keeper/keeper.go @@ -34,8 +34,6 @@ type Keeper struct { queryPlugins QueryPlugins - queryRouter types.QueryRouter - authority string } diff --git a/modules/light-clients/08-wasm/keeper/keeper_vm.go b/modules/light-clients/08-wasm/keeper/keeper_vm.go index e5a07a82e41..70334a32b74 100644 --- a/modules/light-clients/08-wasm/keeper/keeper_vm.go +++ b/modules/light-clients/08-wasm/keeper/keeper_vm.go @@ -58,7 +58,6 @@ func NewKeeperWithVM( checksums: collections.NewKeySet(sb, types.ChecksumsKey, "checksums", collections.BytesKey), clientKeeper: clientKeeper, authority: authority, - queryRouter: queryRouter, } _, err := sb.Build() @@ -68,7 +67,7 @@ func NewKeeperWithVM( // set query plugins to ensure there is a non-nil query plugin // regardless of what options the user provides - keeper.setQueryPlugins(NewDefaultQueryPlugins(keeper.queryRouter)) + keeper.setQueryPlugins(NewDefaultQueryPlugins(queryRouter)) for _, opt := range opts { opt.apply(keeper) diff --git a/modules/light-clients/08-wasm/keeper/migrations.go b/modules/light-clients/08-wasm/keeper/migrations.go index 7e940cd93b3..1888ae6af54 100644 --- a/modules/light-clients/08-wasm/keeper/migrations.go +++ b/modules/light-clients/08-wasm/keeper/migrations.go @@ -3,8 +3,6 @@ package keeper import ( "context" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types" ) diff --git a/modules/light-clients/08-wasm/keeper/querier_test.go b/modules/light-clients/08-wasm/keeper/querier_test.go index d739e37b726..252a1bd94b5 100644 --- a/modules/light-clients/08-wasm/keeper/querier_test.go +++ b/modules/light-clients/08-wasm/keeper/querier_test.go @@ -9,6 +9,7 @@ import ( wasmvm "github.com/CosmWasm/wasmvm/v2" wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types" + "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/keeper" wasmtesting "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/testing" "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types" From 862fe3f4be1f1d668aaaa1ac95677fb9d0960ea2 Mon Sep 17 00:00:00 2001 From: Tyler <48813565+technicallyty@users.noreply.github.com> Date: Wed, 29 Jan 2025 09:19:52 -0800 Subject: [PATCH 23/24] remove sdk.Context and height args from WasmSnapshotter functionality --- modules/light-clients/08-wasm/keeper/snapshotter.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/modules/light-clients/08-wasm/keeper/snapshotter.go b/modules/light-clients/08-wasm/keeper/snapshotter.go index a400b7a8024..f231aab0a8e 100644 --- a/modules/light-clients/08-wasm/keeper/snapshotter.go +++ b/modules/light-clients/08-wasm/keeper/snapshotter.go @@ -91,15 +91,15 @@ func (ws *WasmSnapshotter) SnapshotExtension(height uint64, payloadWriter snapsh // RestoreExtension implements the snapshot.ExtensionSnapshotter interface. // RestoreExtension is used to read data from an existing extension state snapshot into the 08-wasm module. // The payload reader returns io.EOF when it has reached the end of the extension state snapshot. -func (ws *WasmSnapshotter) RestoreExtension(height uint64, format uint32, payloadReader snapshot.ExtensionPayloadReader) error { +func (ws *WasmSnapshotter) RestoreExtension(format uint32, payloadReader snapshot.ExtensionPayloadReader) error { if format == ws.SnapshotFormat() { - return ws.processAllItems(height, payloadReader, restoreV1) + return ws.processAllItems(payloadReader, restoreV1) } return errorsmod.Wrapf(snapshot.ErrUnknownFormat, "expected %d, got %d", ws.SnapshotFormat(), format) } -func restoreV1(_ sdk.Context, k *Keeper, compressedCode []byte) error { +func restoreV1(k *Keeper, compressedCode []byte) error { if !types.IsGzip(compressedCode) { return errorsmod.Wrap(types.ErrInvalidData, "expected wasm code is not gzip format") } @@ -122,11 +122,9 @@ func restoreV1(_ sdk.Context, k *Keeper, compressedCode []byte) error { } func (ws *WasmSnapshotter) processAllItems( - height uint64, payloadReader snapshot.ExtensionPayloadReader, - cb func(sdk.Context, *Keeper, []byte) error, + cb func(*Keeper, []byte) error, ) error { - ctx := sdk.NewContext(ws.cms, false, nil).WithBlockHeight(int64(height)) for { payload, err := payloadReader() if err == io.EOF { @@ -135,7 +133,7 @@ func (ws *WasmSnapshotter) processAllItems( return err } - if err := cb(ctx, ws.keeper, payload); err != nil { + if err := cb(ws.keeper, payload); err != nil { return errorsmod.Wrap(err, "failure processing snapshot item") } } From b3c9914af3266fc4dfdd5f3f8d65fecc744bce69 Mon Sep 17 00:00:00 2001 From: Tyler <48813565+technicallyty@users.noreply.github.com> Date: Wed, 29 Jan 2025 09:23:49 -0800 Subject: [PATCH 24/24] restore height arg because thats an iface impl --- modules/light-clients/08-wasm/keeper/snapshotter.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/light-clients/08-wasm/keeper/snapshotter.go b/modules/light-clients/08-wasm/keeper/snapshotter.go index f231aab0a8e..af0b221cbbd 100644 --- a/modules/light-clients/08-wasm/keeper/snapshotter.go +++ b/modules/light-clients/08-wasm/keeper/snapshotter.go @@ -91,7 +91,7 @@ func (ws *WasmSnapshotter) SnapshotExtension(height uint64, payloadWriter snapsh // RestoreExtension implements the snapshot.ExtensionSnapshotter interface. // RestoreExtension is used to read data from an existing extension state snapshot into the 08-wasm module. // The payload reader returns io.EOF when it has reached the end of the extension state snapshot. -func (ws *WasmSnapshotter) RestoreExtension(format uint32, payloadReader snapshot.ExtensionPayloadReader) error { +func (ws *WasmSnapshotter) RestoreExtension(_ uint64, format uint32, payloadReader snapshot.ExtensionPayloadReader) error { if format == ws.SnapshotFormat() { return ws.processAllItems(payloadReader, restoreV1) }