Skip to content

Commit 0795530

Browse files
randygrokmergify[bot]
authored andcommitted
feat(runtime/v2): store loader on simappv2 (#21704)
Co-authored-by: marbar3778 <[email protected]> (cherry picked from commit 7d6ff0d) # Conflicts: # CHANGELOG.md # runtime/v2/app.go # runtime/v2/module.go # runtime/v2/store.go
1 parent 51b418a commit 0795530

File tree

8 files changed

+491
-28
lines changed

8 files changed

+491
-28
lines changed

Diff for: CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ Ref: https://keepachangelog.com/en/1.0.0/
4040

4141
Every module contains its own CHANGELOG.md. Please refer to the module you are interested in.
4242

43+
<<<<<<< HEAD
44+
=======
45+
### Features
46+
47+
* (baseapp) [#20291](https://github.com/cosmos/cosmos-sdk/pull/20291) Simulate nested messages.
48+
* (runtime) [#21704](https://github.com/cosmos/cosmos-sdk/pull/21704) Add StoreLoader in simappv2.
49+
50+
>>>>>>> 7d6ff0df5 (feat(runtime/v2): store loader on simappv2 (#21704))
4351
### Improvements
4452

4553
* (genutil) [#21701](https://github.com/cosmos/cosmos-sdk/pull/21701) Improved error messages for genesis validation.

Diff for: runtime/store.go

+21
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"cosmossdk.io/core/store"
1010
storetypes "cosmossdk.io/store/types"
1111

12+
"github.com/cosmos/cosmos-sdk/baseapp"
1213
sdk "github.com/cosmos/cosmos-sdk/types"
1314
)
1415

@@ -181,3 +182,23 @@ func (s kvStoreAdapter) ReverseIterator(start, end []byte) dbm.Iterator {
181182
func KVStoreAdapter(store store.KVStore) storetypes.KVStore {
182183
return &kvStoreAdapter{store}
183184
}
185+
186+
// UpgradeStoreLoader is used to prepare baseapp with a fixed StoreLoader
187+
// pattern. This is useful for custom upgrade loading logic.
188+
func UpgradeStoreLoader(upgradeHeight int64, storeUpgrades *store.StoreUpgrades) baseapp.StoreLoader {
189+
return func(ms storetypes.CommitMultiStore) error {
190+
if upgradeHeight == ms.LastCommitID().Version+1 {
191+
// Check if the current commit version and upgrade height matches
192+
if len(storeUpgrades.Deleted) > 0 || len(storeUpgrades.Added) > 0 {
193+
stup := &storetypes.StoreUpgrades{
194+
Added: storeUpgrades.Added,
195+
Deleted: storeUpgrades.Deleted,
196+
}
197+
return ms.LoadLatestVersionAndUpgrade(stup)
198+
}
199+
}
200+
201+
// Otherwise load default store loader
202+
return baseapp.DefaultStoreLoader(ms)
203+
}
204+
}

Diff for: runtime/v2/app.go

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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

Comments
 (0)