|
| 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