Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions baseapp/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions core/appmodule/v2/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
6 changes: 0 additions & 6 deletions core/genesis/txhandler.go

This file was deleted.

4 changes: 2 additions & 2 deletions runtime/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 0 additions & 4 deletions runtime/v2/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/v2/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions runtime/v2/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -99,7 +98,6 @@ func init() {
ProvideAppBuilder[transaction.Tx],
ProvideEnvironment[transaction.Tx],
ProvideModuleManager[transaction.Tx],
ProvideGenesisTxHandler[transaction.Tx],
ProvideCometService,
ProvideAppVersionModifier[transaction.Tx],
),
Expand Down Expand Up @@ -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{}
}
Expand Down
6 changes: 2 additions & 4 deletions x/genutil/depinject.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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"`
}

Expand All @@ -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)
}
21 changes: 13 additions & 8 deletions x/genutil/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions x/genutil/gentx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"strings"

"cosmossdk.io/core/genesis"
bankexported "cosmossdk.io/x/bank/exported"
stakingtypes "cosmossdk.io/x/staking/types"

Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions x/genutil/gentx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -264,7 +263,7 @@ func (suite *GenTxTestSuite) TestDeliverGenTxs() {
testCases := []struct {
msg string
malleate func()
deliverTxFn genesis.TxHandler
deliverTxFn genutil.TxHandler
expPass bool
}{
{
Expand Down
27 changes: 15 additions & 12 deletions x/genutil/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}
Loading