refactor(runtime/v2): simplify app manager (#22300)
This commit is contained in:
parent
97d37ae243
commit
681366e346
@ -23,26 +23,24 @@ import (
|
||||
// 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
|
||||
appmanager.AppManager[T]
|
||||
|
||||
// app configuration
|
||||
logger log.Logger
|
||||
config *runtimev2.Module
|
||||
|
||||
// state
|
||||
stf *stf.STF[T]
|
||||
msgRouterBuilder *stf.MsgRouterBuilder
|
||||
queryRouterBuilder *stf.MsgRouterBuilder
|
||||
db Store
|
||||
storeLoader StoreLoader
|
||||
|
||||
// modules
|
||||
interfaceRegistrar registry.InterfaceRegistrar
|
||||
amino registry.AminoRegistrar
|
||||
moduleManager *MM[T]
|
||||
|
||||
// QueryHandlers defines the query handlers
|
||||
QueryHandlers map[string]appmodulev2.Handler
|
||||
|
||||
storeLoader StoreLoader
|
||||
queryHandlers map[string]appmodulev2.Handler // queryHandlers defines the query handlers
|
||||
}
|
||||
|
||||
// Name returns the app name.
|
||||
@ -85,24 +83,21 @@ func (a *App[T]) LoadLatestHeight() (uint64, error) {
|
||||
return a.db.GetLatestVersion()
|
||||
}
|
||||
|
||||
// Close is called in start cmd to gracefully cleanup resources.
|
||||
func (a *App[T]) Close() error {
|
||||
return nil
|
||||
// GetQueryHandlers returns the query handlers.
|
||||
func (a *App[T]) QueryHandlers() map[string]appmodulev2.Handler {
|
||||
return a.queryHandlers
|
||||
}
|
||||
|
||||
func (a *App[T]) GetAppManager() *appmanager.AppManager[T] {
|
||||
return a.AppManager
|
||||
}
|
||||
|
||||
func (a *App[T]) GetQueryHandlers() map[string]appmodulev2.Handler {
|
||||
return a.QueryHandlers
|
||||
}
|
||||
|
||||
// GetSchemaDecoderResolver returns the module schema resolver.
|
||||
func (a *App[T]) GetSchemaDecoderResolver() decoding.DecoderResolver {
|
||||
// SchemaDecoderResolver returns the module schema resolver.
|
||||
func (a *App[T]) SchemaDecoderResolver() decoding.DecoderResolver {
|
||||
moduleSet := map[string]any{}
|
||||
for moduleName, module := range a.moduleManager.Modules() {
|
||||
moduleSet[moduleName] = module
|
||||
}
|
||||
return decoding.ModuleSetDecoderResolver(moduleSet)
|
||||
}
|
||||
|
||||
// Close is called in start cmd to gracefully cleanup resources.
|
||||
func (a *App[T]) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -31,11 +31,6 @@ type AppBuilder[T transaction.Tx] struct {
|
||||
postTxExec func(ctx context.Context, tx T, success bool) error
|
||||
}
|
||||
|
||||
// DefaultGenesis returns a default genesis from the registered AppModule's.
|
||||
func (a *AppBuilder[T]) DefaultGenesis() map[string]json.RawMessage {
|
||||
return a.app.moduleManager.DefaultGenesis()
|
||||
}
|
||||
|
||||
// RegisterModules registers the provided modules with the module manager.
|
||||
// This is the primary hook for integrating with modules which are not registered using the app config.
|
||||
func (a *AppBuilder[T]) RegisterModules(modules map[string]appmodulev2.AppModule) error {
|
||||
@ -98,7 +93,7 @@ func (a *AppBuilder[T]) Build(opts ...AppBuilderOption[T]) (*App[T], error) {
|
||||
|
||||
endBlocker, valUpdate := a.app.moduleManager.EndBlock()
|
||||
|
||||
stf, err := stf.NewSTF[T](
|
||||
stf, err := stf.New[T](
|
||||
a.app.logger.With("module", "stf"),
|
||||
a.app.msgRouterBuilder,
|
||||
a.app.queryRouterBuilder,
|
||||
@ -115,79 +110,77 @@ func (a *AppBuilder[T]) Build(opts ...AppBuilderOption[T]) (*App[T], error) {
|
||||
}
|
||||
a.app.stf = stf
|
||||
|
||||
appManagerBuilder := appmanager.Builder[T]{
|
||||
STF: a.app.stf,
|
||||
DB: a.app.db,
|
||||
ValidateTxGasLimit: a.app.config.GasConfig.ValidateTxGasLimit,
|
||||
QueryGasLimit: a.app.config.GasConfig.QueryGasLimit,
|
||||
SimulationGasLimit: a.app.config.GasConfig.SimulationGasLimit,
|
||||
InitGenesis: func(
|
||||
ctx context.Context,
|
||||
src io.Reader,
|
||||
txHandler func(json.RawMessage) error,
|
||||
) (store.WriterMap, error) {
|
||||
// this implementation assumes that the state is a JSON object
|
||||
bz, err := io.ReadAll(src)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read import state: %w", err)
|
||||
}
|
||||
var genesisJSON map[string]json.RawMessage
|
||||
if err = json.Unmarshal(bz, &genesisJSON); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
v, zeroState, err := a.app.db.StateLatest()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to get latest state: %w", err)
|
||||
}
|
||||
if v != 0 { // TODO: genesis state may be > 0, we need to set version on store
|
||||
return nil, errors.New("cannot init genesis on non-zero state")
|
||||
}
|
||||
genesisCtx := services.NewGenesisContext(a.branch(zeroState))
|
||||
genesisState, err := genesisCtx.Mutate(ctx, func(ctx context.Context) error {
|
||||
err = a.app.moduleManager.InitGenesisJSON(ctx, genesisJSON, txHandler)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to init genesis: %w", err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
return genesisState, err
|
||||
a.app.AppManager = appmanager.New[T](
|
||||
appmanager.Config{
|
||||
ValidateTxGasLimit: a.app.config.GasConfig.ValidateTxGasLimit,
|
||||
QueryGasLimit: a.app.config.GasConfig.QueryGasLimit,
|
||||
SimulationGasLimit: a.app.config.GasConfig.SimulationGasLimit,
|
||||
},
|
||||
ExportGenesis: func(ctx context.Context, version uint64) ([]byte, error) {
|
||||
state, err := a.app.db.StateAt(version)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to get state at given version: %w", err)
|
||||
}
|
||||
|
||||
genesisJson, err := a.app.moduleManager.ExportGenesisForModules(
|
||||
ctx,
|
||||
func() store.WriterMap {
|
||||
return a.branch(state)
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to export genesis: %w", err)
|
||||
}
|
||||
|
||||
bz, err := json.Marshal(genesisJson)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal genesis: %w", err)
|
||||
}
|
||||
|
||||
return bz, nil
|
||||
},
|
||||
}
|
||||
|
||||
appManager, err := appManagerBuilder.Build()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to build app manager: %w", err)
|
||||
}
|
||||
a.app.AppManager = appManager
|
||||
a.app.db,
|
||||
a.app.stf,
|
||||
a.initGenesis,
|
||||
a.exportGenesis,
|
||||
)
|
||||
|
||||
return a.app, nil
|
||||
}
|
||||
|
||||
// initGenesis returns the app initialization genesis for modules
|
||||
func (a *AppBuilder[T]) initGenesis(ctx context.Context, src io.Reader, txHandler func(json.RawMessage) error) (store.WriterMap, error) {
|
||||
// this implementation assumes that the state is a JSON object
|
||||
bz, err := io.ReadAll(src)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read import state: %w", err)
|
||||
}
|
||||
var genesisJSON map[string]json.RawMessage
|
||||
if err = json.Unmarshal(bz, &genesisJSON); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
v, zeroState, err := a.app.db.StateLatest()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to get latest state: %w", err)
|
||||
}
|
||||
if v != 0 { // TODO: genesis state may be > 0, we need to set version on store
|
||||
return nil, errors.New("cannot init genesis on non-zero state")
|
||||
}
|
||||
genesisCtx := services.NewGenesisContext(a.branch(zeroState))
|
||||
genesisState, err := genesisCtx.Mutate(ctx, func(ctx context.Context) error {
|
||||
err = a.app.moduleManager.InitGenesisJSON(ctx, genesisJSON, txHandler)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to init genesis: %w", err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
return genesisState, err
|
||||
}
|
||||
|
||||
// exportGenesis returns the app export genesis logic for modules
|
||||
func (a *AppBuilder[T]) exportGenesis(ctx context.Context, version uint64) ([]byte, error) {
|
||||
state, err := a.app.db.StateAt(version)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to get state at given version: %w", err)
|
||||
}
|
||||
|
||||
genesisJson, err := a.app.moduleManager.ExportGenesisForModules(
|
||||
ctx,
|
||||
func() store.WriterMap {
|
||||
return a.branch(state)
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to export genesis: %w", err)
|
||||
}
|
||||
|
||||
bz, err := json.Marshal(genesisJson)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal genesis: %w", err)
|
||||
}
|
||||
|
||||
return bz, nil
|
||||
}
|
||||
|
||||
// AppBuilderOption is a function that can be passed to AppBuilder.Build to customize the resulting app.
|
||||
type AppBuilderOption[T transaction.Tx] func(*AppBuilder[T])
|
||||
|
||||
|
||||
@ -636,7 +636,7 @@ func registerServices[T transaction.Tx](s appmodulev2.AppModule, app *App[T], re
|
||||
|
||||
// merge maps
|
||||
for path, decoder := range c.queryHandlers {
|
||||
app.QueryHandlers[path] = decoder
|
||||
app.queryHandlers[path] = decoder
|
||||
}
|
||||
}
|
||||
|
||||
@ -655,7 +655,7 @@ func registerServices[T transaction.Tx](s appmodulev2.AppModule, app *App[T], re
|
||||
module.RegisterQueryHandlers(&wrapper)
|
||||
|
||||
for path, handler := range wrapper.handlers {
|
||||
app.QueryHandlers[path] = handler
|
||||
app.queryHandlers[path] = handler
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -131,7 +131,7 @@ func ProvideAppBuilder[T transaction.Tx](
|
||||
amino: amino,
|
||||
msgRouterBuilder: msgRouterBuilder,
|
||||
queryRouterBuilder: stf.NewMsgRouterBuilder(), // TODO dedicated query router
|
||||
QueryHandlers: map[string]appmodulev2.Handler{},
|
||||
queryHandlers: map[string]appmodulev2.Handler{},
|
||||
storeLoader: DefaultStoreLoader,
|
||||
}
|
||||
appBuilder := &AppBuilder[T]{app: app, storeBuilder: storeBuilder}
|
||||
|
||||
@ -57,14 +57,14 @@ func (s *Server[T]) Init(appI serverv2.AppI[T], cfg map[string]any, logger log.L
|
||||
return fmt.Errorf("failed to unmarshal config: %w", err)
|
||||
}
|
||||
}
|
||||
methodsMap := appI.GetQueryHandlers()
|
||||
methodsMap := appI.QueryHandlers()
|
||||
|
||||
grpcSrv := grpc.NewServer(
|
||||
grpc.ForceServerCodec(newProtoCodec(appI.InterfaceRegistry()).GRPCCodec()),
|
||||
grpc.MaxSendMsgSize(serverCfg.MaxSendMsgSize),
|
||||
grpc.MaxRecvMsgSize(serverCfg.MaxRecvMsgSize),
|
||||
grpc.UnknownServiceHandler(
|
||||
makeUnknownServiceHandler(methodsMap, appI.GetAppManager()),
|
||||
makeUnknownServiceHandler(methodsMap, appI),
|
||||
),
|
||||
)
|
||||
|
||||
@ -85,7 +85,7 @@ func (s *Server[T]) StartCmdFlags() *pflag.FlagSet {
|
||||
}
|
||||
|
||||
func makeUnknownServiceHandler(handlers map[string]appmodulev2.Handler, querier interface {
|
||||
Query(ctx context.Context, version uint64, msg gogoproto.Message) (gogoproto.Message, error)
|
||||
Query(ctx context.Context, version uint64, msg transaction.Msg) (transaction.Msg, error)
|
||||
},
|
||||
) grpc.StreamHandler {
|
||||
getRegistry := sync.OnceValues(gogoproto.MergedRegistry)
|
||||
|
||||
@ -20,12 +20,12 @@ const (
|
||||
MaxBodySize = 1 << 20 // 1 MB
|
||||
)
|
||||
|
||||
func NewDefaultHandler[T transaction.Tx](appManager *appmanager.AppManager[T]) http.Handler {
|
||||
func NewDefaultHandler[T transaction.Tx](appManager appmanager.AppManager[T]) http.Handler {
|
||||
return &DefaultHandler[T]{appManager: appManager}
|
||||
}
|
||||
|
||||
type DefaultHandler[T transaction.Tx] struct {
|
||||
appManager *appmanager.AppManager[T]
|
||||
appManager appmanager.AppManager[T]
|
||||
}
|
||||
|
||||
func (h *DefaultHandler[T]) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
@ -45,7 +45,7 @@ func (s *Server[T]) Init(appI serverv2.AppI[T], cfg map[string]any, logger log.L
|
||||
}
|
||||
|
||||
s.router = http.NewServeMux()
|
||||
s.router.Handle("/", NewDefaultHandler(appI.GetAppManager()))
|
||||
s.router.Handle("/", NewDefaultHandler(appI))
|
||||
s.config = serverCfg
|
||||
|
||||
return nil
|
||||
|
||||
@ -12,6 +12,45 @@ import (
|
||||
"cosmossdk.io/core/transaction"
|
||||
)
|
||||
|
||||
// AppManager is a coordinator for all things related to an application
|
||||
// It is responsible for interacting with stf and store.
|
||||
// Runtime/v2 is an extension of this interface.
|
||||
type AppManager[T transaction.Tx] interface {
|
||||
// InitGenesis initializes the genesis state of the application.
|
||||
InitGenesis(
|
||||
ctx context.Context,
|
||||
blockRequest *server.BlockRequest[T],
|
||||
initGenesisJSON []byte,
|
||||
txDecoder transaction.Codec[T],
|
||||
) (*server.BlockResponse, corestore.WriterMap, error)
|
||||
|
||||
// ExportGenesis exports the genesis state of the application.
|
||||
ExportGenesis(ctx context.Context, version uint64) ([]byte, error)
|
||||
|
||||
// DeliverBlock executes a block of transactions.
|
||||
DeliverBlock(
|
||||
ctx context.Context,
|
||||
block *server.BlockRequest[T],
|
||||
) (*server.BlockResponse, corestore.WriterMap, error)
|
||||
|
||||
// ValidateTx will validate the tx against the latest storage state. This means that
|
||||
// only the stateful validation will be run, not the execution portion of the tx.
|
||||
// If full execution is needed, Simulate must be used.
|
||||
ValidateTx(ctx context.Context, tx T) (server.TxResult, error)
|
||||
|
||||
// Simulate runs validation and execution flow of a Tx.
|
||||
Simulate(ctx context.Context, tx T) (server.TxResult, corestore.WriterMap, error)
|
||||
|
||||
// Query queries the application at the provided version.
|
||||
// CONTRACT: Version must always be provided, if 0, get latest
|
||||
Query(ctx context.Context, version uint64, request transaction.Msg) (transaction.Msg, error)
|
||||
|
||||
// QueryWithState executes a query with the provided state. This allows to process a query
|
||||
// independently of the db state. For example, it can be used to process a query with temporary
|
||||
// and uncommitted state
|
||||
QueryWithState(ctx context.Context, state corestore.ReaderMap, request transaction.Msg) (transaction.Msg, error)
|
||||
}
|
||||
|
||||
// Store defines the underlying storage behavior needed by AppManager.
|
||||
type Store interface {
|
||||
// StateLatest returns a readonly view over the latest
|
||||
@ -24,20 +63,40 @@ type Store interface {
|
||||
StateAt(version uint64) (corestore.ReaderMap, error)
|
||||
}
|
||||
|
||||
// AppManager is a coordinator for all things related to an application
|
||||
type AppManager[T transaction.Tx] struct {
|
||||
// appManager is a coordinator for all things related to an application
|
||||
type appManager[T transaction.Tx] struct {
|
||||
// Gas limits for validating, querying, and simulating transactions.
|
||||
config Config
|
||||
|
||||
db Store
|
||||
|
||||
initGenesis InitGenesis
|
||||
// InitGenesis is a function that initializes the application state from a genesis file.
|
||||
// It takes a context, a source reader for the genesis file, and a transaction handler function.
|
||||
initGenesis InitGenesis
|
||||
// ExportGenesis is a function that exports the application state to a genesis file.
|
||||
// It takes a context and a version number for the genesis file.
|
||||
exportGenesis ExportGenesis
|
||||
|
||||
// The database for storing application data.
|
||||
db Store
|
||||
// The state transition function for processing transactions.
|
||||
stf StateTransitionFunction[T]
|
||||
}
|
||||
|
||||
func New[T transaction.Tx](
|
||||
config Config,
|
||||
db Store,
|
||||
stf StateTransitionFunction[T],
|
||||
initGenesisImpl InitGenesis,
|
||||
exportGenesisImpl ExportGenesis,
|
||||
) AppManager[T] {
|
||||
return &appManager[T]{
|
||||
config: config,
|
||||
db: db,
|
||||
stf: stf,
|
||||
initGenesis: initGenesisImpl,
|
||||
exportGenesis: exportGenesisImpl,
|
||||
}
|
||||
}
|
||||
|
||||
// InitGenesis initializes the genesis state of the application.
|
||||
func (a AppManager[T]) InitGenesis(
|
||||
func (a appManager[T]) InitGenesis(
|
||||
ctx context.Context,
|
||||
blockRequest *server.BlockRequest[T],
|
||||
initGenesisJSON []byte,
|
||||
@ -82,7 +141,7 @@ func (a AppManager[T]) InitGenesis(
|
||||
}
|
||||
|
||||
// ExportGenesis exports the genesis state of the application.
|
||||
func (a AppManager[T]) ExportGenesis(ctx context.Context, version uint64) ([]byte, error) {
|
||||
func (a appManager[T]) ExportGenesis(ctx context.Context, version uint64) ([]byte, error) {
|
||||
if a.exportGenesis == nil {
|
||||
return nil, errors.New("export genesis function not set")
|
||||
}
|
||||
@ -90,7 +149,8 @@ func (a AppManager[T]) ExportGenesis(ctx context.Context, version uint64) ([]byt
|
||||
return a.exportGenesis(ctx, version)
|
||||
}
|
||||
|
||||
func (a AppManager[T]) DeliverBlock(
|
||||
// DeliverBlock executes a block of transactions.
|
||||
func (a appManager[T]) DeliverBlock(
|
||||
ctx context.Context,
|
||||
block *server.BlockRequest[T],
|
||||
) (*server.BlockResponse, corestore.WriterMap, error) {
|
||||
@ -114,7 +174,7 @@ func (a AppManager[T]) DeliverBlock(
|
||||
// ValidateTx will validate the tx against the latest storage state. This means that
|
||||
// only the stateful validation will be run, not the execution portion of the tx.
|
||||
// If full execution is needed, Simulate must be used.
|
||||
func (a AppManager[T]) ValidateTx(ctx context.Context, tx T) (server.TxResult, error) {
|
||||
func (a appManager[T]) ValidateTx(ctx context.Context, tx T) (server.TxResult, error) {
|
||||
_, latestState, err := a.db.StateLatest()
|
||||
if err != nil {
|
||||
return server.TxResult{}, err
|
||||
@ -124,7 +184,7 @@ func (a AppManager[T]) ValidateTx(ctx context.Context, tx T) (server.TxResult, e
|
||||
}
|
||||
|
||||
// Simulate runs validation and execution flow of a Tx.
|
||||
func (a AppManager[T]) Simulate(ctx context.Context, tx T) (server.TxResult, corestore.WriterMap, error) {
|
||||
func (a appManager[T]) Simulate(ctx context.Context, tx T) (server.TxResult, corestore.WriterMap, error) {
|
||||
_, state, err := a.db.StateLatest()
|
||||
if err != nil {
|
||||
return server.TxResult{}, nil, err
|
||||
@ -135,7 +195,7 @@ func (a AppManager[T]) Simulate(ctx context.Context, tx T) (server.TxResult, cor
|
||||
|
||||
// Query queries the application at the provided version.
|
||||
// CONTRACT: Version must always be provided, if 0, get latest
|
||||
func (a AppManager[T]) Query(ctx context.Context, version uint64, request transaction.Msg) (transaction.Msg, error) {
|
||||
func (a appManager[T]) Query(ctx context.Context, version uint64, request transaction.Msg) (transaction.Msg, error) {
|
||||
// if version is provided attempt to do a height query.
|
||||
if version != 0 {
|
||||
queryState, err := a.db.StateAt(version)
|
||||
@ -156,6 +216,6 @@ func (a AppManager[T]) Query(ctx context.Context, version uint64, request transa
|
||||
// QueryWithState executes a query with the provided state. This allows to process a query
|
||||
// independently of the db state. For example, it can be used to process a query with temporary
|
||||
// and uncommitted state
|
||||
func (a AppManager[T]) QueryWithState(ctx context.Context, state corestore.ReaderMap, request transaction.Msg) (transaction.Msg, error) {
|
||||
func (a appManager[T]) QueryWithState(ctx context.Context, state corestore.ReaderMap, request transaction.Msg) (transaction.Msg, error) {
|
||||
return a.stf.Query(ctx, state, a.config.QueryGasLimit, request)
|
||||
}
|
||||
|
||||
@ -1,40 +0,0 @@
|
||||
package appmanager
|
||||
|
||||
import (
|
||||
"cosmossdk.io/core/transaction"
|
||||
)
|
||||
|
||||
// Builder is a struct that represents the application builder for managing transactions.
|
||||
// It contains various fields and methods for initializing the application and handling transactions.
|
||||
type Builder[T transaction.Tx] struct {
|
||||
STF StateTransitionFunction[T] // The state transition function for processing transactions.
|
||||
DB Store // The database for storing application data.
|
||||
|
||||
// Gas limits for validating, querying, and simulating transactions.
|
||||
ValidateTxGasLimit uint64
|
||||
QueryGasLimit uint64
|
||||
SimulationGasLimit uint64
|
||||
|
||||
// InitGenesis is a function that initializes the application state from a genesis file.
|
||||
// It takes a context, a source reader for the genesis file, and a transaction handler function.
|
||||
InitGenesis InitGenesis
|
||||
// ExportGenesis is a function that exports the application state to a genesis file.
|
||||
// It takes a context and a version number for the genesis file.
|
||||
ExportGenesis ExportGenesis
|
||||
}
|
||||
|
||||
// Build creates a new instance of AppManager with the provided configuration and returns it.
|
||||
// It initializes the AppManager with the given database, export state, import state, initGenesis function, and state transition function.
|
||||
func (b Builder[T]) Build() (*AppManager[T], error) {
|
||||
return &AppManager[T]{
|
||||
config: Config{
|
||||
ValidateTxGasLimit: b.ValidateTxGasLimit,
|
||||
QueryGasLimit: b.QueryGasLimit,
|
||||
SimulationGasLimit: b.SimulationGasLimit,
|
||||
},
|
||||
db: b.DB,
|
||||
initGenesis: b.InitGenesis,
|
||||
exportGenesis: b.ExportGenesis,
|
||||
stf: b.STF,
|
||||
}, nil
|
||||
}
|
||||
@ -1,7 +1,6 @@
|
||||
package appmanager
|
||||
|
||||
// Config represents the configuration options for the app manager.
|
||||
// TODO: implement comments for toml
|
||||
type Config struct {
|
||||
ValidateTxGasLimit uint64 `mapstructure:"validate-tx-gas-limit"` // TODO: check how this works on app mempool
|
||||
QueryGasLimit uint64 `mapstructure:"query-gas-limit"`
|
||||
|
||||
@ -9,9 +9,6 @@ import (
|
||||
)
|
||||
|
||||
type (
|
||||
// ExportGenesis is a function type that represents the export of the genesis state.
|
||||
ExportGenesis func(ctx context.Context, version uint64) ([]byte, error)
|
||||
|
||||
// InitGenesis is a function that will run at application genesis, it will be called with
|
||||
// the following arguments:
|
||||
// - ctx: the context of the genesis operation
|
||||
@ -25,4 +22,7 @@ type (
|
||||
src io.Reader,
|
||||
txHandler func(json.RawMessage) error,
|
||||
) (store.WriterMap, error)
|
||||
|
||||
// ExportGenesis is a function type that represents the export of the genesis state.
|
||||
ExportGenesis func(ctx context.Context, version uint64) ([]byte, error)
|
||||
)
|
||||
|
||||
@ -42,7 +42,7 @@ var _ abci.Application = (*Consensus[transaction.Tx])(nil)
|
||||
type Consensus[T transaction.Tx] struct {
|
||||
logger log.Logger
|
||||
appName, version string
|
||||
app *appmanager.AppManager[T]
|
||||
app appmanager.AppManager[T]
|
||||
appCloser func() error
|
||||
txCodec transaction.Codec[T]
|
||||
store types.Store
|
||||
@ -77,7 +77,7 @@ type Consensus[T transaction.Tx] struct {
|
||||
func NewConsensus[T transaction.Tx](
|
||||
logger log.Logger,
|
||||
appName string,
|
||||
app *appmanager.AppManager[T],
|
||||
app appmanager.AppManager[T],
|
||||
appCloser func() error,
|
||||
mp mempool.Mempool[T],
|
||||
indexedEvents map[string]struct{},
|
||||
|
||||
@ -646,7 +646,7 @@ func setUpConsensus(t *testing.T, gasLimit uint64, mempool mempool.Mempool[mock.
|
||||
}, nil
|
||||
})
|
||||
|
||||
s, err := stf.NewSTF(
|
||||
s, err := stf.New(
|
||||
log.NewNopLogger().With("module", "stf"),
|
||||
msgRouterBuilder,
|
||||
queryRouterBuilder,
|
||||
@ -672,21 +672,20 @@ func setUpConsensus(t *testing.T, gasLimit uint64, mempool mempool.Mempool[mock.
|
||||
sc := cometmock.NewMockCommiter(log.NewNopLogger(), string(actorName), "stf")
|
||||
mockStore := cometmock.NewMockStore(ss, sc)
|
||||
|
||||
b := appmanager.Builder[mock.Tx]{
|
||||
STF: s,
|
||||
DB: mockStore,
|
||||
am := appmanager.New(appmanager.Config{
|
||||
ValidateTxGasLimit: gasLimit,
|
||||
QueryGasLimit: gasLimit,
|
||||
SimulationGasLimit: gasLimit,
|
||||
InitGenesis: func(ctx context.Context, src io.Reader, txHandler func(json.RawMessage) error) (store.WriterMap, error) {
|
||||
},
|
||||
mockStore,
|
||||
s,
|
||||
func(ctx context.Context, src io.Reader, txHandler func(json.RawMessage) error) (store.WriterMap, error) {
|
||||
_, st, err := mockStore.StateLatest()
|
||||
require.NoError(t, err)
|
||||
return branch.DefaultNewWriterMap(st), nil
|
||||
},
|
||||
}
|
||||
|
||||
am, err := b.Build()
|
||||
require.NoError(t, err)
|
||||
nil,
|
||||
)
|
||||
|
||||
return NewConsensus[mock.Tx](log.NewNopLogger(), "testing-app", am, func() error { return nil }, mempool, map[string]struct{}{}, nil, mockStore, Config{AppTomlConfig: DefaultAppTomlConfig()}, mock.TxCodec{}, "test")
|
||||
}
|
||||
|
||||
@ -102,15 +102,15 @@ func (s *CometBFTServer[T]) Init(appI serverv2.AppI[T], cfg map[string]any, logg
|
||||
}
|
||||
|
||||
s.logger = logger.With(log.ModuleKey, s.Name())
|
||||
rs := appI.GetStore()
|
||||
rs := appI.Store()
|
||||
consensus := NewConsensus(
|
||||
s.logger,
|
||||
appI.Name(),
|
||||
appI.GetAppManager(),
|
||||
appI,
|
||||
appI.Close,
|
||||
s.serverOptions.Mempool(cfg),
|
||||
indexEvents,
|
||||
appI.GetQueryHandlers(),
|
||||
appI.QueryHandlers(),
|
||||
rs,
|
||||
s.config,
|
||||
s.initTxCodec,
|
||||
@ -137,7 +137,7 @@ func (s *CometBFTServer[T]) Init(appI serverv2.AppI[T], cfg map[string]any, logg
|
||||
if indexerCfg := s.config.AppTomlConfig.Indexer; len(indexerCfg.Target) > 0 {
|
||||
listener, err := indexer.StartIndexing(indexer.IndexingOptions{
|
||||
Config: indexerCfg,
|
||||
Resolver: appI.GetSchemaDecoderResolver(),
|
||||
Resolver: appI.SchemaDecoderResolver(),
|
||||
Logger: s.logger.With(log.ModuleKey, "indexer"),
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@ -17,7 +17,6 @@ import (
|
||||
"cosmossdk.io/log"
|
||||
serverv2 "cosmossdk.io/server/v2"
|
||||
grpc "cosmossdk.io/server/v2/api/grpc"
|
||||
"cosmossdk.io/server/v2/appmanager"
|
||||
"cosmossdk.io/server/v2/store"
|
||||
storev2 "cosmossdk.io/store/v2"
|
||||
)
|
||||
@ -37,19 +36,15 @@ type mockApp[T transaction.Tx] struct {
|
||||
serverv2.AppI[T]
|
||||
}
|
||||
|
||||
func (*mockApp[T]) GetQueryHandlers() map[string]appmodulev2.Handler {
|
||||
func (*mockApp[T]) QueryHandlers() map[string]appmodulev2.Handler {
|
||||
return map[string]appmodulev2.Handler{}
|
||||
}
|
||||
|
||||
func (*mockApp[T]) GetAppManager() *appmanager.AppManager[T] {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (*mockApp[T]) InterfaceRegistry() coreserver.InterfaceRegistry {
|
||||
return &mockInterfaceRegistry{}
|
||||
}
|
||||
|
||||
func (*mockApp[T]) GetStore() storev2.RootStore {
|
||||
func (*mockApp[T]) Store() storev2.RootStore {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@ -44,8 +44,8 @@ type STF[T transaction.Tx] struct {
|
||||
makeGasMeteredState makeGasMeteredStateFn
|
||||
}
|
||||
|
||||
// NewSTF returns a new STF instance.
|
||||
func NewSTF[T transaction.Tx](
|
||||
// New returns a new STF instance.
|
||||
func New[T transaction.Tx](
|
||||
logger log.Logger,
|
||||
msgRouterBuilder *MsgRouterBuilder,
|
||||
queryRouterBuilder *MsgRouterBuilder,
|
||||
|
||||
@ -32,7 +32,7 @@ func New[T transaction.Tx]() *Server[T] {
|
||||
}
|
||||
|
||||
func (s *Server[T]) Init(app serverv2.AppI[T], v map[string]any, _ log.Logger) (err error) {
|
||||
s.backend = app.GetStore()
|
||||
s.backend = app.Store()
|
||||
s.config, err = UnmarshalConfig(v)
|
||||
return err
|
||||
}
|
||||
|
||||
@ -15,11 +15,12 @@ import (
|
||||
type AppCreator[T transaction.Tx] func(log.Logger, *viper.Viper) AppI[T]
|
||||
|
||||
type AppI[T transaction.Tx] interface {
|
||||
appmanager.AppManager[T]
|
||||
|
||||
Name() string
|
||||
InterfaceRegistry() server.InterfaceRegistry
|
||||
GetAppManager() *appmanager.AppManager[T]
|
||||
GetQueryHandlers() map[string]appmodulev2.Handler
|
||||
GetStore() store.RootStore
|
||||
GetSchemaDecoderResolver() decoding.DecoderResolver
|
||||
QueryHandlers() map[string]appmodulev2.Handler
|
||||
Store() store.RootStore
|
||||
SchemaDecoderResolver() decoding.DecoderResolver
|
||||
Close() error
|
||||
}
|
||||
|
||||
@ -218,8 +218,8 @@ func (app *SimApp[T]) TxConfig() client.TxConfig {
|
||||
return app.txConfig
|
||||
}
|
||||
|
||||
// GetStore returns the root store.
|
||||
func (app *SimApp[T]) GetStore() store.RootStore {
|
||||
// Store returns the root store.
|
||||
func (app *SimApp[T]) Store() store.RootStore {
|
||||
return app.store
|
||||
}
|
||||
|
||||
|
||||
@ -72,7 +72,7 @@ func NewTestApp(t *testing.T) (*SimApp[transaction.Tx], context.Context) {
|
||||
genesisBytes, err := json.Marshal(genesis)
|
||||
require.NoError(t, err)
|
||||
|
||||
st := app.GetStore()
|
||||
st := app.Store()
|
||||
ci, err := st.LastCommitID()
|
||||
require.NoError(t, err)
|
||||
|
||||
@ -108,7 +108,7 @@ func MoveNextBlock(t *testing.T, app *SimApp[transaction.Tx], ctx context.Contex
|
||||
|
||||
bz := sha256.Sum256([]byte{})
|
||||
|
||||
st := app.GetStore()
|
||||
st := app.Store()
|
||||
ci, err := st.LastCommitID()
|
||||
require.NoError(t, err)
|
||||
|
||||
|
||||
@ -29,7 +29,7 @@ func (app *SimApp[T]) ExportAppStateAndValidators(
|
||||
return exportedApp, err
|
||||
}
|
||||
|
||||
readerMap, err := app.GetStore().StateAt(latestHeight)
|
||||
readerMap, err := app.Store().StateAt(latestHeight)
|
||||
if err != nil {
|
||||
return exportedApp, err
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user