From 03a3153520e087a5e391b4344c17f8ef7be0c4f6 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Thu, 8 Aug 2024 09:37:46 +0200 Subject: [PATCH 1/4] refactor,docs: clarify usage of `genesis.TxHandler` in v1 vs v2 --- core/appmodule/v2/genesis.go | 2 ++ runtime/v2/app.go | 4 ---- runtime/v2/manager.go | 2 +- runtime/v2/module.go | 6 ------ x/genutil/depinject.go | 5 ++--- x/genutil/genesis.go | 12 +++++++----- x/genutil/gentx.go | 1 + x/genutil/module.go | 20 ++++++++++++-------- 8 files changed, 25 insertions(+), 27 deletions(-) diff --git a/core/appmodule/v2/genesis.go b/core/appmodule/v2/genesis.go index 967618772f6e..629ac02ac6de 100644 --- a/core/appmodule/v2/genesis.go +++ b/core/appmodule/v2/genesis.go @@ -30,6 +30,8 @@ type HasABCIGenesis interface { ExportGenesis(ctx context.Context) (json.RawMessage, error) } +// GenesisDecoder is an alternative to the InitGenesis method. +// It is implemented by the genutil module to decode genTxs. type GenesisDecoder interface { DecodeGenesisJSON(data json.RawMessage) ([]json.RawMessage, error) } diff --git a/runtime/v2/app.go b/runtime/v2/app.go index 0e28d8e3b5a5..ba46e001fcb3 100644 --- a/runtime/v2/app.go +++ b/runtime/v2/app.go @@ -110,10 +110,6 @@ func (a *App[T]) GetLogger() log.Logger { return a.logger } -func (a *App[T]) ExecuteGenesisTx(_ []byte) error { - panic("App.ExecuteGenesisTx not supported in runtime/v2") -} - func (a *App[T]) GetAppManager() *appmanager.AppManager[T] { return a.AppManager } diff --git a/runtime/v2/manager.go b/runtime/v2/manager.go index d1261966a1fb..dbd68260bd05 100644 --- a/runtime/v2/manager.go +++ b/runtime/v2/manager.go @@ -153,7 +153,7 @@ func (m *MM[T]) InitGenesisJSON( switch module := mod.(type) { case appmodule.HasGenesisAuto: panic(fmt.Sprintf("module %s isn't server/v2 compatible", moduleName)) - case appmodulev2.GenesisDecoder: + case appmodulev2.GenesisDecoder: // GenesisDecoder needs to supersede HasGenesis and HasABCIGenesis. genTxs, err := module.DecodeGenesisJSON(genesisData[moduleName]) if err != nil { return err diff --git a/runtime/v2/module.go b/runtime/v2/module.go index d4a99f49a0d2..a3fc7e87a98e 100644 --- a/runtime/v2/module.go +++ b/runtime/v2/module.go @@ -18,7 +18,6 @@ import ( "cosmossdk.io/core/app" appmodulev2 "cosmossdk.io/core/appmodule/v2" "cosmossdk.io/core/comet" - "cosmossdk.io/core/genesis" "cosmossdk.io/core/legacy" "cosmossdk.io/core/registry" "cosmossdk.io/core/store" @@ -99,7 +98,6 @@ func init() { ProvideAppBuilder[transaction.Tx], ProvideEnvironment[transaction.Tx], ProvideModuleManager[transaction.Tx], - ProvideGenesisTxHandler[transaction.Tx], ProvideCometService, ProvideAppVersionModifier[transaction.Tx], ), @@ -238,10 +236,6 @@ func storeKeyOverride(config *runtimev2.Module, moduleName string) *runtimev2.St return nil } -func ProvideGenesisTxHandler[T transaction.Tx](appBuilder *AppBuilder[T]) genesis.TxHandler { - return appBuilder.app -} - func ProvideCometService() comet.Service { return &services.ContextAwareCometInfoService{} } diff --git a/x/genutil/depinject.go b/x/genutil/depinject.go index 6b6f2beabfa0..99aec1a38c0e 100644 --- a/x/genutil/depinject.go +++ b/x/genutil/depinject.go @@ -29,9 +29,9 @@ type ModuleInputs struct { AccountKeeper types.AccountKeeper StakingKeeper types.StakingKeeper - DeliverTx genesis.TxHandler Config client.TxConfig Cdc codec.Codec + DeliverTx genesis.TxHandler `optional:"true"` // Only used in server v0 applications GenTxValidator types.MessageValidator `optional:"true"` } @@ -40,6 +40,5 @@ func ProvideModule(in ModuleInputs) appmodule.AppModule { in.GenTxValidator = types.DefaultMessageValidator } - m := NewAppModule(in.Cdc, in.AccountKeeper, in.StakingKeeper, in.DeliverTx, in.Config, in.GenTxValidator) - return m + return NewAppModule(in.Cdc, in.AccountKeeper, in.StakingKeeper, in.DeliverTx, in.Config, in.GenTxValidator) } diff --git a/x/genutil/genesis.go b/x/genutil/genesis.go index 545f14010982..f16d97a307ef 100644 --- a/x/genutil/genesis.go +++ b/x/genutil/genesis.go @@ -2,6 +2,7 @@ package genutil import ( "context" + "fmt" "cosmossdk.io/core/genesis" @@ -11,17 +12,18 @@ import ( ) // InitGenesis - initialize accounts and deliver genesis transactions +// NOTE: It isn't used in server/v2 applications. func InitGenesis( ctx context.Context, stakingKeeper types.StakingKeeper, deliverTx genesis.TxHandler, genesisState types.GenesisState, txEncodingConfig client.TxEncodingConfig, ) (validatorUpdates []module.ValidatorUpdate, err error) { + if deliverTx == nil { + return nil, fmt.Errorf("deliverTx (genesis.TxHandler) not defined, verify x/genutil wiring") + } + if len(genesisState.GenTxs) > 0 { - moduleValidatorUpdates, err := DeliverGenTxs(ctx, genesisState.GenTxs, stakingKeeper, deliverTx, txEncodingConfig) - if err != nil { - return nil, err - } - return moduleValidatorUpdates, nil + return DeliverGenTxs(ctx, genesisState.GenTxs, stakingKeeper, deliverTx, txEncodingConfig) } return } diff --git a/x/genutil/gentx.go b/x/genutil/gentx.go index 714e68b2dd08..9385bcf22b04 100644 --- a/x/genutil/gentx.go +++ b/x/genutil/gentx.go @@ -89,6 +89,7 @@ func ValidateAccountInGenesis( // DeliverGenTxs iterates over all genesis txs, decodes each into a Tx and // invokes the provided deliverTxfn with the decoded Tx. It returns the result // of the staking module's ApplyAndReturnValidatorSetUpdates. +// NOTE: This isn't used in server/v2 applications. func DeliverGenTxs( ctx context.Context, genTxs []json.RawMessage, stakingKeeper types.StakingKeeper, deliverTx genesis.TxHandler, diff --git a/x/genutil/module.go b/x/genutil/module.go index df3b8d85dd51..40b7eb4dec0e 100644 --- a/x/genutil/module.go +++ b/x/genutil/module.go @@ -76,12 +76,24 @@ func (am AppModule) ValidateGenesis(bz json.RawMessage) error { } // InitGenesis performs genesis initialization for the genutil module. +// InitGenesis is skipped in a server/v2 application as DecodeGenesisJSON takes precedence. func (am AppModule) InitGenesis(ctx context.Context, data json.RawMessage) ([]module.ValidatorUpdate, error) { var genesisState types.GenesisState am.cdc.MustUnmarshalJSON(data, &genesisState) return InitGenesis(ctx, am.stakingKeeper, am.deliverTx, genesisState, am.txEncodingConfig) } +// DecodeGenesisJSON returns the genesis transactions for the genutil module. +// It is an alternative to InitGenesis and used in server/v2 applications. +func (am AppModule) DecodeGenesisJSON(data json.RawMessage) ([]json.RawMessage, error) { + var genesisState types.GenesisState + if err := am.cdc.UnmarshalJSON(data, &genesisState); err != nil { + return nil, err + } + + return genesisState.GenTxs, nil +} + // ExportGenesis returns the exported genesis state as raw bytes for the genutil module. func (am AppModule) ExportGenesis(_ context.Context) (json.RawMessage, error) { return am.DefaultGenesis(), nil @@ -94,11 +106,3 @@ func (am AppModule) GenTxValidator() types.MessageValidator { // ConsensusVersion implements HasConsensusVersion func (AppModule) ConsensusVersion() uint64 { return 1 } - -func (am AppModule) DecodeGenesisJSON(data json.RawMessage) ([]json.RawMessage, error) { - var genesisState types.GenesisState - if err := am.cdc.UnmarshalJSON(data, &genesisState); err != nil { - return nil, err - } - return genesisState.GenTxs, nil -} From 56a758b51f4eb1db322df3f6856a3bbe329ef27c Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Thu, 8 Aug 2024 10:01:29 +0200 Subject: [PATCH 2/4] Remove TxHandler from core --- baseapp/genesis.go | 13 ++++++++----- core/genesis/txhandler.go | 6 ------ runtime/module.go | 3 +-- x/genutil/depinject.go | 3 +-- x/genutil/genesis.go | 9 ++++++--- x/genutil/gentx.go | 3 +-- x/genutil/gentx_test.go | 3 +-- x/genutil/module.go | 7 +++---- 8 files changed, 21 insertions(+), 26 deletions(-) delete mode 100644 core/genesis/txhandler.go diff --git a/baseapp/genesis.go b/baseapp/genesis.go index 4662d1187b4a..4a5b08ba6a8d 100644 --- a/baseapp/genesis.go +++ b/baseapp/genesis.go @@ -4,14 +4,17 @@ import ( "errors" "github.com/cometbft/cometbft/abci/types" - - "cosmossdk.io/core/genesis" ) -var _ genesis.TxHandler = (*BaseApp)(nil) +var _ TxHandler = (*BaseApp)(nil) + +// TxHandler is an interface that defines how genesis txs are handled. +// By default, BaseApp handles them using the deliverTx method. +type TxHandler interface { + ExecuteGenesisTx([]byte) error +} -// ExecuteGenesisTx implements genesis.GenesisState from -// cosmossdk.io/core/genesis to set initial state in genesis +// ExecuteGenesis implements TxHandler. It executes a genesis tx. func (ba *BaseApp) ExecuteGenesisTx(tx []byte) error { res := ba.deliverTx(tx) diff --git a/core/genesis/txhandler.go b/core/genesis/txhandler.go deleted file mode 100644 index cd76517476a3..000000000000 --- a/core/genesis/txhandler.go +++ /dev/null @@ -1,6 +0,0 @@ -package genesis - -// TxHandler is an interface that modules can implement to provide genesis state transitions -type TxHandler interface { - ExecuteGenesisTx([]byte) error -} diff --git a/runtime/module.go b/runtime/module.go index e806e2156761..6d94fe849f19 100644 --- a/runtime/module.go +++ b/runtime/module.go @@ -15,7 +15,6 @@ import ( "cosmossdk.io/core/app" "cosmossdk.io/core/appmodule" "cosmossdk.io/core/comet" - "cosmossdk.io/core/genesis" "cosmossdk.io/core/legacy" "cosmossdk.io/core/store" "cosmossdk.io/depinject" @@ -240,7 +239,7 @@ func ProvideModuleManager(modules map[string]appmodule.AppModule) *module.Manage return module.NewManagerFromMap(modules) } -func ProvideGenesisTxHandler(appBuilder *AppBuilder) genesis.TxHandler { +func ProvideGenesisTxHandler(appBuilder *AppBuilder) baseapp.TxHandler { return appBuilder.app } diff --git a/x/genutil/depinject.go b/x/genutil/depinject.go index 99aec1a38c0e..c23aba5bf74b 100644 --- a/x/genutil/depinject.go +++ b/x/genutil/depinject.go @@ -3,7 +3,6 @@ package genutil import ( modulev1 "cosmossdk.io/api/cosmos/genutil/module/v1" "cosmossdk.io/core/appmodule" - "cosmossdk.io/core/genesis" "cosmossdk.io/depinject" "cosmossdk.io/depinject/appconfig" @@ -31,7 +30,7 @@ type ModuleInputs struct { StakingKeeper types.StakingKeeper Config client.TxConfig Cdc codec.Codec - DeliverTx genesis.TxHandler `optional:"true"` // Only used in server v0 applications + DeliverTx TxHandler `optional:"true"` // Only used in server v0 applications GenTxValidator types.MessageValidator `optional:"true"` } diff --git a/x/genutil/genesis.go b/x/genutil/genesis.go index f16d97a307ef..2008f28fe19d 100644 --- a/x/genutil/genesis.go +++ b/x/genutil/genesis.go @@ -4,18 +4,21 @@ import ( "context" "fmt" - "cosmossdk.io/core/genesis" - "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/x/genutil/types" ) +// TxHandler is an interface that defines how genesis txs are handled. +type TxHandler interface { + ExecuteGenesisTx([]byte) error +} + // InitGenesis - initialize accounts and deliver genesis transactions // NOTE: It isn't used in server/v2 applications. func InitGenesis( ctx context.Context, stakingKeeper types.StakingKeeper, - deliverTx genesis.TxHandler, genesisState types.GenesisState, + deliverTx TxHandler, genesisState types.GenesisState, txEncodingConfig client.TxEncodingConfig, ) (validatorUpdates []module.ValidatorUpdate, err error) { if deliverTx == nil { diff --git a/x/genutil/gentx.go b/x/genutil/gentx.go index 9385bcf22b04..7e4808b0ce15 100644 --- a/x/genutil/gentx.go +++ b/x/genutil/gentx.go @@ -6,7 +6,6 @@ import ( "fmt" "strings" - "cosmossdk.io/core/genesis" bankexported "cosmossdk.io/x/bank/exported" stakingtypes "cosmossdk.io/x/staking/types" @@ -92,7 +91,7 @@ func ValidateAccountInGenesis( // NOTE: This isn't used in server/v2 applications. func DeliverGenTxs( ctx context.Context, genTxs []json.RawMessage, - stakingKeeper types.StakingKeeper, deliverTx genesis.TxHandler, + stakingKeeper types.StakingKeeper, deliverTx TxHandler, txEncodingConfig client.TxEncodingConfig, ) ([]module.ValidatorUpdate, error) { for _, genTx := range genTxs { diff --git a/x/genutil/gentx_test.go b/x/genutil/gentx_test.go index 324ce95c6093..10721c819e12 100644 --- a/x/genutil/gentx_test.go +++ b/x/genutil/gentx_test.go @@ -12,7 +12,6 @@ import ( "github.com/stretchr/testify/suite" _ "cosmossdk.io/api/cosmos/crypto/secp256k1" - "cosmossdk.io/core/genesis" "cosmossdk.io/math" storetypes "cosmossdk.io/store/types" banktypes "cosmossdk.io/x/bank/types" @@ -264,7 +263,7 @@ func (suite *GenTxTestSuite) TestDeliverGenTxs() { testCases := []struct { msg string malleate func() - deliverTxFn genesis.TxHandler + deliverTxFn genutil.TxHandler expPass bool }{ { diff --git a/x/genutil/module.go b/x/genutil/module.go index 40b7eb4dec0e..a5db429bee5f 100644 --- a/x/genutil/module.go +++ b/x/genutil/module.go @@ -7,7 +7,6 @@ import ( "cosmossdk.io/core/appmodule" appmodulev2 "cosmossdk.io/core/appmodule/v2" - "cosmossdk.io/core/genesis" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" @@ -27,7 +26,7 @@ type AppModule struct { cdc codec.Codec accountKeeper types.AccountKeeper stakingKeeper types.StakingKeeper - deliverTx genesis.TxHandler + deliverTx TxHandler // Unecessary in server/v2 applications txEncodingConfig client.TxEncodingConfig genTxValidator types.MessageValidator } @@ -37,7 +36,7 @@ func NewAppModule( cdc codec.Codec, accountKeeper types.AccountKeeper, stakingKeeper types.StakingKeeper, - deliverTx genesis.TxHandler, + deliverTx TxHandler, txEncodingConfig client.TxEncodingConfig, genTxValidator types.MessageValidator, ) module.AppModule { @@ -95,7 +94,7 @@ func (am AppModule) DecodeGenesisJSON(data json.RawMessage) ([]json.RawMessage, } // ExportGenesis returns the exported genesis state as raw bytes for the genutil module. -func (am AppModule) ExportGenesis(_ context.Context) (json.RawMessage, error) { +func (am AppModule) ExportGenesis(context.Context) (json.RawMessage, error) { return am.DefaultGenesis(), nil } From 0fe630d92f7a73d267bf1792c2bd8370340bf281 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Thu, 8 Aug 2024 11:20:57 +0200 Subject: [PATCH 3/4] lint --- x/genutil/module.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/genutil/module.go b/x/genutil/module.go index a5db429bee5f..ba33eab26b1f 100644 --- a/x/genutil/module.go +++ b/x/genutil/module.go @@ -26,7 +26,7 @@ type AppModule struct { cdc codec.Codec accountKeeper types.AccountKeeper stakingKeeper types.StakingKeeper - deliverTx TxHandler // Unecessary in server/v2 applications + deliverTx TxHandler // Unnecessary in server/v2 applications txEncodingConfig client.TxEncodingConfig genTxValidator types.MessageValidator } From 9f5df39ce82f71551a5253350bdff9ea1db87590 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Thu, 8 Aug 2024 13:08:12 +0200 Subject: [PATCH 4/4] fix --- baseapp/genesis.go | 10 +--------- runtime/module.go | 3 ++- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/baseapp/genesis.go b/baseapp/genesis.go index 4a5b08ba6a8d..ef3499462472 100644 --- a/baseapp/genesis.go +++ b/baseapp/genesis.go @@ -6,15 +6,7 @@ import ( "github.com/cometbft/cometbft/abci/types" ) -var _ TxHandler = (*BaseApp)(nil) - -// TxHandler is an interface that defines how genesis txs are handled. -// By default, BaseApp handles them using the deliverTx method. -type TxHandler interface { - ExecuteGenesisTx([]byte) error -} - -// ExecuteGenesis implements TxHandler. It executes a genesis tx. +// ExecuteGenesis implements a genesis TxHandler used to execute a genTxs (from genutil). func (ba *BaseApp) ExecuteGenesisTx(tx []byte) error { res := ba.deliverTx(tx) diff --git a/runtime/module.go b/runtime/module.go index 6d94fe849f19..43da40efb237 100644 --- a/runtime/module.go +++ b/runtime/module.go @@ -28,6 +28,7 @@ import ( "github.com/cosmos/cosmos-sdk/std" "github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/types/msgservice" + "github.com/cosmos/cosmos-sdk/x/genutil" ) // appModule defines runtime as an AppModule @@ -239,7 +240,7 @@ func ProvideModuleManager(modules map[string]appmodule.AppModule) *module.Manage return module.NewManagerFromMap(modules) } -func ProvideGenesisTxHandler(appBuilder *AppBuilder) baseapp.TxHandler { +func ProvideGenesisTxHandler(appBuilder *AppBuilder) genutil.TxHandler { return appBuilder.app }