|
| 1 | +package runtime |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "slices" |
| 7 | + |
| 8 | + gogoproto "github.com/cosmos/gogoproto/proto" |
| 9 | + |
| 10 | + runtimev2 "cosmossdk.io/api/cosmos/app/runtime/v2" |
| 11 | + "cosmossdk.io/core/registry" |
| 12 | + "cosmossdk.io/core/transaction" |
| 13 | + "cosmossdk.io/log" |
| 14 | + "cosmossdk.io/server/v2/appmanager" |
| 15 | + "cosmossdk.io/server/v2/stf" |
| 16 | +) |
| 17 | + |
| 18 | +// App is a wrapper around AppManager and ModuleManager that can be used in hybrid |
| 19 | +// app.go/app config scenarios or directly as a servertypes.Application instance. |
| 20 | +// To get an instance of *App, *AppBuilder must be requested as a dependency |
| 21 | +// in a container which declares the runtime module and the AppBuilder.Build() |
| 22 | +// method must be called. |
| 23 | +// |
| 24 | +// App can be used to create a hybrid app.go setup where some configuration is |
| 25 | +// done declaratively with an app config and the rest of it is done the old way. |
| 26 | +// See simapp/app_v2.go for an example of this setup. |
| 27 | +type App[T transaction.Tx] struct { |
| 28 | + *appmanager.AppManager[T] |
| 29 | + |
| 30 | + // app manager dependencies |
| 31 | + stf *stf.STF[T] |
| 32 | + msgRouterBuilder *stf.MsgRouterBuilder |
| 33 | + queryRouterBuilder *stf.MsgRouterBuilder |
| 34 | + db Store |
| 35 | + |
| 36 | + // app configuration |
| 37 | + logger log.Logger |
| 38 | + config *runtimev2.Module |
| 39 | + |
| 40 | + // modules configuration |
| 41 | + storeKeys []string |
| 42 | + interfaceRegistrar registry.InterfaceRegistrar |
| 43 | + amino registry.AminoRegistrar |
| 44 | + moduleManager *MM[T] |
| 45 | + |
| 46 | + // GRPCMethodsToMessageMap maps gRPC method name to a function that decodes the request |
| 47 | + // bytes into a gogoproto.Message, which then can be passed to appmanager. |
| 48 | + GRPCMethodsToMessageMap map[string]func() gogoproto.Message |
| 49 | + |
| 50 | + storeLoader StoreLoader |
| 51 | +} |
| 52 | + |
| 53 | +// Name returns the app name. |
| 54 | +func (a *App[T]) Name() string { |
| 55 | + return a.config.AppName |
| 56 | +} |
| 57 | + |
| 58 | +// Logger returns the app logger. |
| 59 | +func (a *App[T]) Logger() log.Logger { |
| 60 | + return a.logger |
| 61 | +} |
| 62 | + |
| 63 | +// ModuleManager returns the module manager. |
| 64 | +func (a *App[T]) ModuleManager() *MM[T] { |
| 65 | + return a.moduleManager |
| 66 | +} |
| 67 | + |
| 68 | +// DefaultGenesis returns a default genesis from the registered modules. |
| 69 | +func (a *App[T]) DefaultGenesis() map[string]json.RawMessage { |
| 70 | + return a.moduleManager.DefaultGenesis() |
| 71 | +} |
| 72 | + |
| 73 | +// SetStoreLoader sets the store loader. |
| 74 | +func (a *App[T]) SetStoreLoader(loader StoreLoader) { |
| 75 | + a.storeLoader = loader |
| 76 | +} |
| 77 | + |
| 78 | +// LoadLatest loads the latest version. |
| 79 | +func (a *App[T]) LoadLatest() error { |
| 80 | + return a.storeLoader(a.db) |
| 81 | +} |
| 82 | + |
| 83 | +// LoadHeight loads a particular height |
| 84 | +func (a *App[T]) LoadHeight(height uint64) error { |
| 85 | + return a.db.LoadVersion(height) |
| 86 | +} |
| 87 | + |
| 88 | +// LoadLatestHeight loads the latest height. |
| 89 | +func (a *App[T]) LoadLatestHeight() (uint64, error) { |
| 90 | + return a.db.GetLatestVersion() |
| 91 | +} |
| 92 | + |
| 93 | +// Close is called in start cmd to gracefully cleanup resources. |
| 94 | +func (a *App[T]) Close() error { |
| 95 | + return nil |
| 96 | +} |
| 97 | + |
| 98 | +// GetStoreKeys returns all the app store keys. |
| 99 | +func (a *App[T]) GetStoreKeys() []string { |
| 100 | + return a.storeKeys |
| 101 | +} |
| 102 | + |
| 103 | +// UnsafeFindStoreKey fetches a registered StoreKey from the App in linear time. |
| 104 | +// NOTE: This should only be used in testing. |
| 105 | +func (a *App[T]) UnsafeFindStoreKey(storeKey string) (string, error) { |
| 106 | + i := slices.IndexFunc(a.storeKeys, func(s string) bool { return s == storeKey }) |
| 107 | + if i == -1 { |
| 108 | + return "", errors.New("store key not found") |
| 109 | + } |
| 110 | + |
| 111 | + return a.storeKeys[i], nil |
| 112 | +} |
| 113 | + |
| 114 | +// GetStore returns the app store. |
| 115 | +func (a *App[T]) GetStore() Store { |
| 116 | + return a.db |
| 117 | +} |
| 118 | + |
| 119 | +func (a *App[T]) GetAppManager() *appmanager.AppManager[T] { |
| 120 | + return a.AppManager |
| 121 | +} |
| 122 | + |
| 123 | +func (a *App[T]) GetGPRCMethodsToMessageMap() map[string]func() gogoproto.Message { |
| 124 | + return a.GRPCMethodsToMessageMap |
| 125 | +} |
0 commit comments