Skip to content

Commit

Permalink
refactor: clarify usage of genesis.TxHandler in v1 vs v2 (#21213)
Browse files Browse the repository at this point in the history
(cherry picked from commit 8400d9b)

# Conflicts:
#	core/appmodule/v2/genesis.go
#	runtime/v2/app.go
#	runtime/v2/manager.go
#	runtime/v2/module.go
  • Loading branch information
julienrbrt authored and mergify[bot] committed Aug 8, 2024
1 parent 8de0315 commit 730ac9d
Show file tree
Hide file tree
Showing 11 changed files with 1,157 additions and 36 deletions.
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
37 changes: 37 additions & 0 deletions core/appmodule/v2/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package appmodule

import (
"context"
"encoding/json"
)

// HasGenesis defines a custom genesis handling API implementation.
// WARNING: this API is meant as a short-term solution to allow for the
// migration of existing modules to the new app module API.
// It is intended to be replaced by an automatic genesis with collections/orm.
type HasGenesis interface {
AppModule

Check failure on line 13 in core/appmodule/v2/genesis.go

View workflow job for this annotation

GitHub Actions / dependency-review

undefined: AppModule

Check failure on line 13 in core/appmodule/v2/genesis.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: AppModule

DefaultGenesis() json.RawMessage
ValidateGenesis(data json.RawMessage) error
InitGenesis(ctx context.Context, data json.RawMessage) error
ExportGenesis(ctx context.Context) (json.RawMessage, error)
}

// HasABCIGenesis defines a custom genesis handling API implementation for ABCI.
// (stateful genesis methods which returns validator updates)
// Most modules should not implement this interface.
type HasABCIGenesis interface {
AppModule

Check failure on line 25 in core/appmodule/v2/genesis.go

View workflow job for this annotation

GitHub Actions / dependency-review

undefined: AppModule

Check failure on line 25 in core/appmodule/v2/genesis.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: AppModule

DefaultGenesis() json.RawMessage
ValidateGenesis(data json.RawMessage) error
InitGenesis(ctx context.Context, data json.RawMessage) ([]ValidatorUpdate, error)

Check failure on line 29 in core/appmodule/v2/genesis.go

View workflow job for this annotation

GitHub Actions / dependency-review

undefined: ValidatorUpdate

Check failure on line 29 in core/appmodule/v2/genesis.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: ValidatorUpdate (typecheck)
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)
}
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
119 changes: 119 additions & 0 deletions runtime/v2/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package runtime

import (
"encoding/json"
"errors"

gogoproto "github.com/cosmos/gogoproto/proto"
"golang.org/x/exp/slices"

runtimev2 "cosmossdk.io/api/cosmos/app/runtime/v2"
"cosmossdk.io/core/legacy"
"cosmossdk.io/core/registry"
"cosmossdk.io/core/transaction"
"cosmossdk.io/log"
"cosmossdk.io/server/v2/appmanager"

Check failure on line 15 in runtime/v2/app.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/server/v2/appmanager; to add it:

Check failure on line 15 in runtime/v2/app.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/server/v2/appmanager; to add it:

Check failure on line 15 in runtime/v2/app.go

View workflow job for this annotation

GitHub Actions / dependency-review

could not import cosmossdk.io/server/v2/appmanager (invalid package name: "")

Check failure on line 15 in runtime/v2/app.go

View workflow job for this annotation

GitHub Actions / split-test-files

no required module provides package cosmossdk.io/server/v2/appmanager; to add it:
"cosmossdk.io/server/v2/stf"

Check failure on line 16 in runtime/v2/app.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/server/v2/stf; to add it:

Check failure on line 16 in runtime/v2/app.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/server/v2/stf; to add it:

Check failure on line 16 in runtime/v2/app.go

View workflow job for this annotation

GitHub Actions / dependency-review

could not import cosmossdk.io/server/v2/stf (invalid package name: "")

Check failure on line 16 in runtime/v2/app.go

View workflow job for this annotation

GitHub Actions / split-test-files

no required module provides package cosmossdk.io/server/v2/stf; to add it:
)

// App is a wrapper around AppManager and ModuleManager that can be used in hybrid
// app.go/app config scenarios or directly as a servertypes.Application instance.
// To get an instance of *App, *AppBuilder must be requested as a dependency
// in a container which declares the runtime module and the AppBuilder.Build()
// method must be called.
//
// App can be used to create a hybrid app.go setup where some configuration is
// done declaratively with an app config and the rest of it is done the old way.
// See simapp/app_v2.go for an example of this setup.
type App[T transaction.Tx] struct {
*appmanager.AppManager[T]

// app manager dependencies
stf *stf.STF[T]
msgRouterBuilder *stf.MsgRouterBuilder
queryRouterBuilder *stf.MsgRouterBuilder
db Store

Check failure on line 35 in runtime/v2/app.go

View workflow job for this annotation

GitHub Actions / dependency-review

undefined: Store

Check failure on line 35 in runtime/v2/app.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: Store (typecheck)

// app configuration
logger log.Logger
config *runtimev2.Module

// modules configuration
storeKeys []string
interfaceRegistrar registry.InterfaceRegistrar
amino legacy.Amino
moduleManager *MM[T]

// GRPCMethodsToMessageMap maps gRPC method name to a function that decodes the request
// bytes into a gogoproto.Message, which then can be passed to appmanager.
GRPCMethodsToMessageMap map[string]func() gogoproto.Message
}

// Name returns the app name.
func (a *App[T]) Name() string {
return a.config.AppName
}

// Logger returns the app logger.
func (a *App[T]) Logger() log.Logger {
return a.logger
}

// ModuleManager returns the module manager.
func (a *App[T]) ModuleManager() *MM[T] {
return a.moduleManager
}

// DefaultGenesis returns a default genesis from the registered modules.
func (a *App[T]) DefaultGenesis() map[string]json.RawMessage {
return a.moduleManager.DefaultGenesis()
}

// LoadLatest loads the latest version.
func (a *App[T]) LoadLatest() error {
return a.db.LoadLatestVersion()
}

// LoadHeight loads a particular height
func (a *App[T]) LoadHeight(height uint64) error {
return a.db.LoadVersion(height)
}

// Close is called in start cmd to gracefully cleanup resources.
func (a *App[T]) Close() error {
return nil
}

// GetStoreKeys returns all the app store keys.
func (a *App[T]) GetStoreKeys() []string {
return a.storeKeys
}

// UnsafeFindStoreKey fetches a registered StoreKey from the App in linear time.
// NOTE: This should only be used in testing.
func (a *App[T]) UnsafeFindStoreKey(storeKey string) (string, error) {
i := slices.IndexFunc(a.storeKeys, func(s string) bool { return s == storeKey })
if i == -1 {
return "", errors.New("store key not found")
}

return a.storeKeys[i], nil
}

// GetStore returns the app store.
func (a *App[T]) GetStore() Store {

Check failure on line 104 in runtime/v2/app.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: Store (typecheck)
return a.db
}

// GetLogger returns the app logger.
func (a *App[T]) GetLogger() log.Logger {
return a.logger
}

func (a *App[T]) GetAppManager() *appmanager.AppManager[T] {
return a.AppManager
}

func (a *App[T]) GetGPRCMethodsToMessageMap() map[string]func() gogoproto.Message {
return a.GRPCMethodsToMessageMap
}
Loading

0 comments on commit 730ac9d

Please sign in to comment.