Skip to content

Commit 4755da2

Browse files
julienrbrtmergify[bot]
authored andcommitted
refactor(runtime/v2): simplify app manager (#22300)
(cherry picked from commit 681366e) # Conflicts: # runtime/v2/app.go # runtime/v2/builder.go # runtime/v2/manager.go # runtime/v2/module.go # server/v2/api/grpc/server.go # server/v2/api/rest/handler.go # server/v2/api/rest/server.go # server/v2/appmanager/appmanager.go # server/v2/appmanager/config.go # server/v2/appmanager/genesis.go # server/v2/appmanager/stf.go # server/v2/cometbft/server.go # server/v2/server_test.go # server/v2/stf/stf.go # server/v2/store/server.go # server/v2/types.go
1 parent 89219a3 commit 4755da2

21 files changed

+3049
-19
lines changed

runtime/v2/app.go

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package runtime
2+
3+
import (
4+
"encoding/json"
5+
6+
runtimev2 "cosmossdk.io/api/cosmos/app/runtime/v2"
7+
appmodulev2 "cosmossdk.io/core/appmodule/v2"
8+
"cosmossdk.io/core/registry"
9+
"cosmossdk.io/core/transaction"
10+
"cosmossdk.io/log"
11+
"cosmossdk.io/schema/decoding"
12+
"cosmossdk.io/server/v2/appmanager"
13+
"cosmossdk.io/server/v2/stf"
14+
)
15+
16+
// App is a wrapper around AppManager and ModuleManager that can be used in hybrid
17+
// app.go/app config scenarios or directly as a servertypes.Application instance.
18+
// To get an instance of *App, *AppBuilder must be requested as a dependency
19+
// in a container which declares the runtime module and the AppBuilder.Build()
20+
// method must be called.
21+
//
22+
// App can be used to create a hybrid app.go setup where some configuration is
23+
// done declaratively with an app config and the rest of it is done the old way.
24+
// See simapp/app_v2.go for an example of this setup.
25+
type App[T transaction.Tx] struct {
26+
appmanager.AppManager[T]
27+
28+
// app configuration
29+
logger log.Logger
30+
config *runtimev2.Module
31+
32+
// state
33+
stf *stf.STF[T]
34+
msgRouterBuilder *stf.MsgRouterBuilder
35+
queryRouterBuilder *stf.MsgRouterBuilder
36+
db Store
37+
storeLoader StoreLoader
38+
39+
// modules
40+
interfaceRegistrar registry.InterfaceRegistrar
41+
amino registry.AminoRegistrar
42+
moduleManager *MM[T]
43+
queryHandlers map[string]appmodulev2.Handler // queryHandlers defines the query handlers
44+
}
45+
46+
// Name returns the app name.
47+
func (a *App[T]) Name() string {
48+
return a.config.AppName
49+
}
50+
51+
// Logger returns the app logger.
52+
func (a *App[T]) Logger() log.Logger {
53+
return a.logger
54+
}
55+
56+
// ModuleManager returns the module manager.
57+
func (a *App[T]) ModuleManager() *MM[T] {
58+
return a.moduleManager
59+
}
60+
61+
// DefaultGenesis returns a default genesis from the registered modules.
62+
func (a *App[T]) DefaultGenesis() map[string]json.RawMessage {
63+
return a.moduleManager.DefaultGenesis()
64+
}
65+
66+
// SetStoreLoader sets the store loader.
67+
func (a *App[T]) SetStoreLoader(loader StoreLoader) {
68+
a.storeLoader = loader
69+
}
70+
71+
// LoadLatest loads the latest version.
72+
func (a *App[T]) LoadLatest() error {
73+
return a.storeLoader(a.db)
74+
}
75+
76+
// LoadHeight loads a particular height
77+
func (a *App[T]) LoadHeight(height uint64) error {
78+
return a.db.LoadVersion(height)
79+
}
80+
81+
// LoadLatestHeight loads the latest height.
82+
func (a *App[T]) LoadLatestHeight() (uint64, error) {
83+
return a.db.GetLatestVersion()
84+
}
85+
86+
// GetQueryHandlers returns the query handlers.
87+
func (a *App[T]) QueryHandlers() map[string]appmodulev2.Handler {
88+
return a.queryHandlers
89+
}
90+
91+
// SchemaDecoderResolver returns the module schema resolver.
92+
func (a *App[T]) SchemaDecoderResolver() decoding.DecoderResolver {
93+
moduleSet := map[string]any{}
94+
for moduleName, module := range a.moduleManager.Modules() {
95+
moduleSet[moduleName] = module
96+
}
97+
return decoding.ModuleSetDecoderResolver(moduleSet)
98+
}
99+
100+
// Close is called in start cmd to gracefully cleanup resources.
101+
func (a *App[T]) Close() error {
102+
return nil
103+
}

runtime/v2/builder.go

+216
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
package runtime
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"errors"
7+
"fmt"
8+
"io"
9+
10+
"cosmossdk.io/core/appmodule"
11+
appmodulev2 "cosmossdk.io/core/appmodule/v2"
12+
"cosmossdk.io/core/store"
13+
"cosmossdk.io/core/transaction"
14+
"cosmossdk.io/runtime/v2/services"
15+
"cosmossdk.io/server/v2/appmanager"
16+
"cosmossdk.io/server/v2/stf"
17+
"cosmossdk.io/server/v2/stf/branch"
18+
"cosmossdk.io/store/v2/root"
19+
)
20+
21+
// AppBuilder is a type that is injected into a container by the runtime/v2 module
22+
// (as *AppBuilder) which can be used to create an app which is compatible with
23+
// the existing app.go initialization conventions.
24+
type AppBuilder[T transaction.Tx] struct {
25+
app *App[T]
26+
storeBuilder root.Builder
27+
28+
// the following fields are used to overwrite the default
29+
branch func(state store.ReaderMap) store.WriterMap
30+
txValidator func(ctx context.Context, tx T) error
31+
postTxExec func(ctx context.Context, tx T, success bool) error
32+
}
33+
34+
// RegisterModules registers the provided modules with the module manager.
35+
// This is the primary hook for integrating with modules which are not registered using the app config.
36+
func (a *AppBuilder[T]) RegisterModules(modules map[string]appmodulev2.AppModule) error {
37+
for name, appModule := range modules {
38+
// if a (legacy) module implements the HasName interface, check that the name matches
39+
if mod, ok := appModule.(interface{ Name() string }); ok {
40+
if name != mod.Name() {
41+
a.app.logger.Warn(fmt.Sprintf("module name %q does not match name returned by HasName: %q", name, mod.Name()))
42+
}
43+
}
44+
45+
if _, ok := a.app.moduleManager.modules[name]; ok {
46+
return fmt.Errorf("module named %q already exists", name)
47+
}
48+
a.app.moduleManager.modules[name] = appModule
49+
50+
if mod, ok := appModule.(appmodulev2.HasRegisterInterfaces); ok {
51+
mod.RegisterInterfaces(a.app.interfaceRegistrar)
52+
}
53+
54+
if mod, ok := appModule.(appmodule.HasAminoCodec); ok {
55+
mod.RegisterLegacyAminoCodec(a.app.amino)
56+
}
57+
}
58+
59+
return nil
60+
}
61+
62+
// Build builds an *App instance.
63+
func (a *AppBuilder[T]) Build(opts ...AppBuilderOption[T]) (*App[T], error) {
64+
for _, opt := range opts {
65+
opt(a)
66+
}
67+
68+
// default branch
69+
if a.branch == nil {
70+
a.branch = branch.DefaultNewWriterMap
71+
}
72+
73+
// default tx validator
74+
if a.txValidator == nil {
75+
a.txValidator = a.app.moduleManager.TxValidators()
76+
}
77+
78+
// default post tx exec
79+
if a.postTxExec == nil {
80+
a.postTxExec = func(ctx context.Context, tx T, success bool) error {
81+
return nil
82+
}
83+
}
84+
85+
a.app.db = a.storeBuilder.Get()
86+
if a.app.db == nil {
87+
return nil, fmt.Errorf("storeBuilder did not return a db")
88+
}
89+
90+
if err := a.app.moduleManager.RegisterServices(a.app); err != nil {
91+
return nil, err
92+
}
93+
94+
endBlocker, valUpdate := a.app.moduleManager.EndBlock()
95+
96+
stf, err := stf.New[T](
97+
a.app.logger.With("module", "stf"),
98+
a.app.msgRouterBuilder,
99+
a.app.queryRouterBuilder,
100+
a.app.moduleManager.PreBlocker(),
101+
a.app.moduleManager.BeginBlock(),
102+
endBlocker,
103+
a.txValidator,
104+
valUpdate,
105+
a.postTxExec,
106+
a.branch,
107+
)
108+
if err != nil {
109+
return nil, fmt.Errorf("failed to create STF: %w", err)
110+
}
111+
a.app.stf = stf
112+
113+
a.app.AppManager = appmanager.New[T](
114+
appmanager.Config{
115+
ValidateTxGasLimit: a.app.config.GasConfig.ValidateTxGasLimit,
116+
QueryGasLimit: a.app.config.GasConfig.QueryGasLimit,
117+
SimulationGasLimit: a.app.config.GasConfig.SimulationGasLimit,
118+
},
119+
a.app.db,
120+
a.app.stf,
121+
a.initGenesis,
122+
a.exportGenesis,
123+
)
124+
125+
return a.app, nil
126+
}
127+
128+
// initGenesis returns the app initialization genesis for modules
129+
func (a *AppBuilder[T]) initGenesis(ctx context.Context, src io.Reader, txHandler func(json.RawMessage) error) (store.WriterMap, error) {
130+
// this implementation assumes that the state is a JSON object
131+
bz, err := io.ReadAll(src)
132+
if err != nil {
133+
return nil, fmt.Errorf("failed to read import state: %w", err)
134+
}
135+
var genesisJSON map[string]json.RawMessage
136+
if err = json.Unmarshal(bz, &genesisJSON); err != nil {
137+
return nil, err
138+
}
139+
140+
v, zeroState, err := a.app.db.StateLatest()
141+
if err != nil {
142+
return nil, fmt.Errorf("unable to get latest state: %w", err)
143+
}
144+
if v != 0 { // TODO: genesis state may be > 0, we need to set version on store
145+
return nil, errors.New("cannot init genesis on non-zero state")
146+
}
147+
genesisCtx := services.NewGenesisContext(a.branch(zeroState))
148+
genesisState, err := genesisCtx.Mutate(ctx, func(ctx context.Context) error {
149+
err = a.app.moduleManager.InitGenesisJSON(ctx, genesisJSON, txHandler)
150+
if err != nil {
151+
return fmt.Errorf("failed to init genesis: %w", err)
152+
}
153+
return nil
154+
})
155+
156+
return genesisState, err
157+
}
158+
159+
// exportGenesis returns the app export genesis logic for modules
160+
func (a *AppBuilder[T]) exportGenesis(ctx context.Context, version uint64) ([]byte, error) {
161+
state, err := a.app.db.StateAt(version)
162+
if err != nil {
163+
return nil, fmt.Errorf("unable to get state at given version: %w", err)
164+
}
165+
166+
genesisJson, err := a.app.moduleManager.ExportGenesisForModules(
167+
ctx,
168+
func() store.WriterMap {
169+
return a.branch(state)
170+
},
171+
)
172+
if err != nil {
173+
return nil, fmt.Errorf("failed to export genesis: %w", err)
174+
}
175+
176+
bz, err := json.Marshal(genesisJson)
177+
if err != nil {
178+
return nil, fmt.Errorf("failed to marshal genesis: %w", err)
179+
}
180+
181+
return bz, nil
182+
}
183+
184+
// AppBuilderOption is a function that can be passed to AppBuilder.Build to customize the resulting app.
185+
type AppBuilderOption[T transaction.Tx] func(*AppBuilder[T])
186+
187+
// AppBuilderWithBranch sets a custom branch implementation for the app.
188+
func AppBuilderWithBranch[T transaction.Tx](branch func(state store.ReaderMap) store.WriterMap) AppBuilderOption[T] {
189+
return func(a *AppBuilder[T]) {
190+
a.branch = branch
191+
}
192+
}
193+
194+
// AppBuilderWithTxValidator sets the tx validator for the app.
195+
// It overrides all default tx validators defined by modules.
196+
func AppBuilderWithTxValidator[T transaction.Tx](
197+
txValidators func(
198+
ctx context.Context, tx T,
199+
) error,
200+
) AppBuilderOption[T] {
201+
return func(a *AppBuilder[T]) {
202+
a.txValidator = txValidators
203+
}
204+
}
205+
206+
// AppBuilderWithPostTxExec sets logic that will be executed after each transaction.
207+
// When not provided, a no-op function will be used.
208+
func AppBuilderWithPostTxExec[T transaction.Tx](
209+
postTxExec func(
210+
ctx context.Context, tx T, success bool,
211+
) error,
212+
) AppBuilderOption[T] {
213+
return func(a *AppBuilder[T]) {
214+
a.postTxExec = postTxExec
215+
}
216+
}

0 commit comments

Comments
 (0)