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: introduce abciGenesis interface #17554

Merged
merged 21 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions core/appmodule/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

// HasGenesis is the extension interface that modules should implement to handle
// genesis data and state initialization.
// WARNING: This interface is experimental and may change at any time.
type HasGenesis interface {
AppModule

Expand Down
2 changes: 1 addition & 1 deletion tools/hubl/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21
require (
cosmossdk.io/api v0.7.0
cosmossdk.io/client/v2 v2.0.0-20230815130322-dded2e9921f0
cosmossdk.io/core v0.10.0
cosmossdk.io/errors v1.0.0
github.com/cockroachdb/errors v1.10.0
github.com/cosmos/cosmos-sdk v0.46.0-beta2.0.20230823104347-c6b0bb62ac70
Expand All @@ -17,7 +18,6 @@ require (

require (
cosmossdk.io/collections v0.4.0 // indirect
cosmossdk.io/core v0.10.0 // indirect
cosmossdk.io/depinject v1.0.0-alpha.4 // indirect
cosmossdk.io/log v1.2.0 // indirect
cosmossdk.io/math v1.1.2 // indirect
Expand Down
7 changes: 5 additions & 2 deletions types/module/core_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

var (
_ AppModuleBasic = coreAppModuleBasicAdaptor{}
_ HasGenesis = coreAppModuleBasicAdaptor{}
_ HasABCIGenesis = coreAppModuleBasicAdaptor{}
_ HasServices = coreAppModuleBasicAdaptor{}
)

Expand Down Expand Up @@ -122,9 +122,12 @@ func (c coreAppModuleBasicAdaptor) InitGenesis(ctx sdk.Context, cdc codec.JSONCo
}

if mod, ok := c.module.(HasGenesis); ok {
mod.InitGenesis(ctx, cdc, bz)
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved
return nil
}
if mod, ok := c.module.(HasABCIGenesis); ok {
return mod.InitGenesis(ctx, cdc, bz)
}

return nil
}

Expand Down
52 changes: 29 additions & 23 deletions types/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,15 @@ func (bm BasicManager) AddQueryCommands(rootQueryCmd *cobra.Command) {
}

// AppModuleGenesis is the standard form for an application module genesis functions
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
type AppModuleGenesis interface {
AppModuleBasic
HasGenesis
}

// HasGenesis is the extension interface for stateful genesis methods.
type HasGenesis interface {
HasGenesisBasics
InitGenesis(sdk.Context, codec.JSONCodec, json.RawMessage)
ExportGenesis(sdk.Context, codec.JSONCodec) json.RawMessage
}

type HasABCIGenesis interface {
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
HasGenesisBasics
InitGenesis(sdk.Context, codec.JSONCodec, json.RawMessage) []abci.ValidatorUpdate
ExportGenesis(sdk.Context, codec.JSONCodec) json.RawMessage
Expand Down Expand Up @@ -229,13 +231,14 @@ type HasABCIEndblock interface {

// GenesisOnlyAppModule is an AppModule that only has import/export functionality
type GenesisOnlyAppModule struct {
AppModuleGenesis
AppModuleBasic
HasABCIGenesis
}

// NewGenesisOnlyAppModule creates a new GenesisOnlyAppModule object
func NewGenesisOnlyAppModule(amg AppModuleGenesis) GenesisOnlyAppModule {
func NewGenesisOnlyAppModule(amg HasABCIGenesis) GenesisOnlyAppModule {
return GenesisOnlyAppModule{
AppModuleGenesis: amg,
HasABCIGenesis: amg,
}
}

Expand All @@ -245,26 +248,12 @@ func (GenesisOnlyAppModule) IsOnePerModuleType() {}
// IsAppModule implements the appmodule.AppModule interface.
func (GenesisOnlyAppModule) IsAppModule() {}

// RegisterInvariants is a placeholder function register no invariants
func (GenesisOnlyAppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}

// QuerierRoute returns an empty module querier route
func (GenesisOnlyAppModule) QuerierRoute() string { return "" }

// RegisterServices registers all services.
func (gam GenesisOnlyAppModule) RegisterServices(Configurator) {}

// ConsensusVersion implements AppModule/ConsensusVersion.
func (gam GenesisOnlyAppModule) ConsensusVersion() uint64 { return 1 }

// BeginBlock returns an empty module begin-block
func (gam GenesisOnlyAppModule) BeginBlock(ctx sdk.Context) error { return nil }

// EndBlock returns an empty module end-block
func (GenesisOnlyAppModule) EndBlock(sdk.Context) ([]abci.ValidatorUpdate, error) {
return []abci.ValidatorUpdate{}, nil
}

// Manager defines a module manager that provides the high level utility for managing and executing
// operations for a group of modules
type Manager struct {
Expand Down Expand Up @@ -344,6 +333,10 @@ func (m *Manager) SetOrderExportGenesis(moduleNames ...string) {
return !hasGenesis
}

if _, hasGenesis := module.(HasABCIGenesis); hasGenesis {
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
return !hasGenesis
}

_, hasGenesis := module.(HasGenesis)
return !hasGenesis
})
Expand Down Expand Up @@ -462,6 +455,9 @@ func (m *Manager) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, genesisData
return &abci.ResponseInitChain{}, err
}
} else if module, ok := mod.(HasGenesis); ok {
ctx.Logger().Debug("running initialization for module", "module", moduleName)
module.InitGenesis(ctx, cdc, genesisData[moduleName])
} else if module, ok := mod.(HasABCIGenesis); ok {
ctx.Logger().Debug("running initialization for module", "module", moduleName)
moduleValUpdates := module.InitGenesis(ctx, cdc, genesisData[moduleName])

Expand Down Expand Up @@ -535,6 +531,12 @@ func (m *Manager) ExportGenesisForModules(ctx sdk.Context, cdc codec.JSONCodec,
ctx := ctx.WithGasMeter(storetypes.NewInfiniteGasMeter()) // avoid race conditions
ch <- genesisResult{module.ExportGenesis(ctx, cdc), nil}
}(module, channels[moduleName])
} else if module, ok := mod.(HasABCIGenesis); ok {
channels[moduleName] = make(chan genesisResult)
go func(module HasABCIGenesis, ch chan genesisResult) {
ctx := ctx.WithGasMeter(storetypes.NewInfiniteGasMeter()) // avoid race conditions
ch <- genesisResult{module.ExportGenesis(ctx, cdc), nil}
}(module, channels[moduleName])
Comment on lines +548 to +551

Check notice

Code scanning / CodeQL

Spawning a Go routine

Spawning a Go routine may be a possible source of non-determinism
}
}

Expand Down Expand Up @@ -680,8 +682,12 @@ func (m Manager) RunMigrations(ctx context.Context, cfg Configurator, fromVM Ver
}
} else {
sdkCtx.Logger().Info(fmt.Sprintf("adding a new module: %s", moduleName))
if module, ok := m.Modules[moduleName].(HasGenesis); ok {
moduleValUpdates := module.InitGenesis(sdkCtx, c.cdc, module.DefaultGenesis(c.cdc))
module1, ok := m.Modules[moduleName].(HasGenesis)
if ok {
module1.InitGenesis(sdkCtx, c.cdc, module1.DefaultGenesis(c.cdc))
}
if module2, ok := m.Modules[moduleName].(HasABCIGenesis); ok {
moduleValUpdates := module2.InitGenesis(sdkCtx, c.cdc, module1.DefaultGenesis(c.cdc))
// The module manager assumes only one module will update the
// validator set, and it can't be a new module.
if len(moduleValUpdates) > 0 {
Expand Down
12 changes: 0 additions & 12 deletions types/module/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,6 @@ func TestBasicManager(t *testing.T) {
require.Nil(t, module.NewBasicManager().ValidateGenesis(cdc, nil, expDefaultGenesis))
}

func TestGenesisOnlyAppModule(t *testing.T) {
mockCtrl := gomock.NewController(t)
t.Cleanup(mockCtrl.Finish)

mockModule := mock.NewMockAppModuleGenesis(mockCtrl)
mockInvariantRegistry := mock.NewMockInvariantRegistry(mockCtrl)
goam := module.NewGenesisOnlyAppModule(mockModule)

// no-op
goam.RegisterInvariants(mockInvariantRegistry)
}

func TestAssertNoForgottenModules(t *testing.T) {
mockCtrl := gomock.NewController(t)
t.Cleanup(mockCtrl.Finish)
Expand Down
5 changes: 2 additions & 3 deletions x/authz/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"

abci "github.com/cometbft/cometbft/abci/types"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -118,6 +117,7 @@ func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak authz.AccountKeeper,
var (
_ appmodule.AppModule = AppModule{}
_ appmodule.HasBeginBlocker = AppModule{}
_ module.HasGenesis = AppModule{}
)

// IsOnePerModuleType implements the depinject.OnePerModuleType interface.
Expand All @@ -133,11 +133,10 @@ func (AppModule) Name() string {

// InitGenesis performs genesis initialization for the authz module. It returns
// no validator updates.
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate {
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) {
var genesisState authz.GenesisState
cdc.MustUnmarshalJSON(data, &genesisState)
am.keeper.InitGenesis(ctx, &genesisState)
return []abci.ValidatorUpdate{}
}

// ExportGenesis returns the exported genesis state as raw bytes for the authz
Expand Down
5 changes: 2 additions & 3 deletions x/bank/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"time"

abci "github.com/cometbft/cometbft/abci/types"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -40,6 +39,7 @@ var (
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ module.AppModuleSimulation = AppModule{}
_ module.HasGenesis = AppModule{}
)

// AppModuleBasic defines the basic application module used by the bank module.
Expand Down Expand Up @@ -153,14 +153,13 @@ func (AppModule) QuerierRoute() string { return types.RouterKey }

// InitGenesis performs genesis initialization for the bank module. It returns
// no validator updates.
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate {
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) {
start := time.Now()
var genesisState types.GenesisState
cdc.MustUnmarshalJSON(data, &genesisState)
telemetry.MeasureSince(start, "InitGenesis", "crisis", "unmarshal")

am.keeper.InitGenesis(ctx, &genesisState)
return []abci.ValidatorUpdate{}
}

// ExportGenesis returns the exported genesis state as raw bytes for the bank
Expand Down
5 changes: 2 additions & 3 deletions x/circuit/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"time"

abci "github.com/cometbft/cometbft/abci/types"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -36,6 +35,7 @@ const ConsensusVersion = 1
var (
_ module.AppModuleGenesis = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ module.HasGenesis = AppModule{}
)

// AppModuleBasic defines the basic application module used by the circuit module.
Expand Down Expand Up @@ -118,14 +118,13 @@ func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion }

// InitGenesis performs genesis initialization for the circuit module. It returns
// no validator updates.
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate {
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) {
start := time.Now()
var genesisState types.GenesisState
cdc.MustUnmarshalJSON(data, &genesisState)
telemetry.MeasureSince(start, "InitGenesis", "crisis", "unmarshal")

am.keeper.InitGenesis(ctx, &genesisState)
return []abci.ValidatorUpdate{}
}

// ExportGenesis returns the exported genesis state as raw bytes for the circuit
Expand Down
5 changes: 2 additions & 3 deletions x/crisis/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"time"

abci "github.com/cometbft/cometbft/abci/types"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cast"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -118,6 +117,7 @@ func NewAppModule(keeper *keeper.Keeper, skipGenesisInvariants bool, ss exported
var (
_ appmodule.AppModule = AppModule{}
_ appmodule.HasEndBlocker = AppModule{}
_ module.HasGenesis = AppModule{}
)

// IsOnePerModuleType implements the depinject.OnePerModuleType interface.
Expand Down Expand Up @@ -148,7 +148,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {

// InitGenesis performs genesis initialization for the crisis module. It returns
// no validator updates.
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate {
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) {
start := time.Now()
var genesisState types.GenesisState
cdc.MustUnmarshalJSON(data, &genesisState)
Expand All @@ -158,7 +158,6 @@ func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.
if !am.skipGenesisInvariants {
am.keeper.AssertInvariants(ctx)
}
return []abci.ValidatorUpdate{}
}

// ExportGenesis returns the exported genesis state as raw bytes for the crisis
Expand Down
5 changes: 2 additions & 3 deletions x/distribution/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"

abci "github.com/cometbft/cometbft/abci/types"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -118,6 +117,7 @@ func NewAppModule(
var (
_ appmodule.AppModule = AppModule{}
_ appmodule.HasBeginBlocker = AppModule{}
_ module.HasGenesis = AppModule{}
)

// IsOnePerModuleType implements the depinject.OnePerModuleType interface.
Expand Down Expand Up @@ -157,11 +157,10 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {

// InitGenesis performs genesis initialization for the distribution module. It returns
// no validator updates.
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate {
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) {
var genesisState types.GenesisState
cdc.MustUnmarshalJSON(data, &genesisState)
am.keeper.InitGenesis(ctx, genesisState)
return []abci.ValidatorUpdate{}
}

// ExportGenesis returns the exported genesis state as raw bytes for the distribution
Expand Down
5 changes: 2 additions & 3 deletions x/evidence/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"

abci "github.com/cometbft/cometbft/abci/types"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
"google.golang.org/grpc"
Expand Down Expand Up @@ -122,6 +121,7 @@ var (
_ appmodule.AppModule = AppModule{}
_ appmodule.HasServices = AppModule{}
_ appmodule.HasBeginBlocker = AppModule{}
_ module.HasGenesis = AppModule{}
)

// IsOnePerModuleType implements the depinject.OnePerModuleType interface.
Expand All @@ -144,15 +144,14 @@ func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error {

// InitGenesis performs the evidence module's genesis initialization It returns
// no validator updates.
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, bz json.RawMessage) []abci.ValidatorUpdate {
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, bz json.RawMessage) {
var gs types.GenesisState
err := cdc.UnmarshalJSON(bz, &gs)
if err != nil {
panic(fmt.Sprintf("failed to unmarshal %s genesis state: %s", types.ModuleName, err))
}

InitGenesis(ctx, am.keeper, &gs)
return []abci.ValidatorUpdate{}
}

// ExportGenesis returns the evidence module's exported genesis state as raw JSON bytes.
Expand Down
Loading