refactor: clarify usage of genesis.TxHandler in v1 vs v2 (#21213)

This commit is contained in:
Julien Robert 2024-08-08 13:23:41 +02:00 committed by GitHub
parent 22fb753099
commit 8400d9b426
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 39 additions and 53 deletions

View File

@ -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)

View File

@ -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)
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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

View File

@ -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{}
}

View File

@ -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)
}

View File

@ -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
}

View File

@ -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 {

View File

@ -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
}{
{

View File

@ -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
}