diff --git a/baseapp/genesis.go b/baseapp/genesis.go index 4662d1187b..ef34994624 100644 --- a/baseapp/genesis.go +++ b/baseapp/genesis.go @@ -4,14 +4,9 @@ import ( "errors" "github.com/cometbft/cometbft/abci/types" - - "cosmossdk.io/core/genesis" ) -var _ genesis.TxHandler = (*BaseApp)(nil) - -// ExecuteGenesisTx implements genesis.GenesisState from -// cosmossdk.io/core/genesis to set initial state in genesis +// 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/core/appmodule/v2/genesis.go b/core/appmodule/v2/genesis.go index 967618772f..629ac02ac6 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/core/genesis/txhandler.go b/core/genesis/txhandler.go deleted file mode 100644 index cd76517476..0000000000 --- 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 e806e21567..43da40efb2 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" @@ -29,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 @@ -240,7 +240,7 @@ func ProvideModuleManager(modules map[string]appmodule.AppModule) *module.Manage return module.NewManagerFromMap(modules) } -func ProvideGenesisTxHandler(appBuilder *AppBuilder) genesis.TxHandler { +func ProvideGenesisTxHandler(appBuilder *AppBuilder) genutil.TxHandler { return appBuilder.app } diff --git a/runtime/v2/app.go b/runtime/v2/app.go index 0e28d8e3b5..ba46e001fc 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 d1261966a1..dbd68260bd 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 d4a99f49a0..a3fc7e87a9 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 6b6f2beabf..c23aba5bf7 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" @@ -29,9 +28,9 @@ type ModuleInputs struct { AccountKeeper types.AccountKeeper StakingKeeper types.StakingKeeper - DeliverTx genesis.TxHandler Config client.TxConfig Cdc codec.Codec + DeliverTx TxHandler `optional:"true"` // Only used in server v0 applications GenTxValidator types.MessageValidator `optional:"true"` } @@ -40,6 +39,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 545f140109..2008f28fe1 100644 --- a/x/genutil/genesis.go +++ b/x/genutil/genesis.go @@ -2,26 +2,31 @@ package genutil import ( "context" - - "cosmossdk.io/core/genesis" + "fmt" "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 { + 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 714e68b2dd..7e4808b0ce 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" @@ -89,9 +88,10 @@ 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, + 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 324ce95c60..10721c819e 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 df3b8d85dd..ba33eab26b 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 // Unnecessary 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 { @@ -76,14 +75,26 @@ 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) { +func (am AppModule) ExportGenesis(context.Context) (json.RawMessage, error) { return am.DefaultGenesis(), nil } @@ -94,11 +105,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 -}