From e4e44dc161a05dc7a50adea957cd29ccd68fa3be Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Thu, 25 Apr 2024 18:00:11 +0200 Subject: [PATCH] refactor(runtime/v2): remove store v1 dep --- runtime/v2/app.go | 17 +++++------ runtime/v2/go.mod | 2 +- runtime/v2/module.go | 70 ++++++++++++++++++-------------------------- runtime/v2/store.go | 6 ++-- simapp/v2/app_di.go | 7 +++-- 5 files changed, 44 insertions(+), 58 deletions(-) diff --git a/runtime/v2/app.go b/runtime/v2/app.go index 5baadfc89bd7..624aa9dd06d6 100644 --- a/runtime/v2/app.go +++ b/runtime/v2/app.go @@ -3,6 +3,7 @@ package runtime import ( "context" "encoding/json" + "errors" "golang.org/x/exp/slices" @@ -14,7 +15,6 @@ import ( "cosmossdk.io/log" "cosmossdk.io/server/v2/appmanager" "cosmossdk.io/server/v2/stf" - storetypes "cosmossdk.io/store/types" "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" @@ -59,7 +59,7 @@ type App struct { appConfig *appv1alpha1.Config // modules configuration - storeKeys []storetypes.StoreKey + storeKeys []string interfaceRegistry codectypes.InterfaceRegistry cdc codec.Codec amino *codec.LegacyAmino @@ -100,27 +100,26 @@ func (a *App) Close() error { // This method should only be used for registering extra stores // wiich is necessary for modules that not registered using the app config. // To be used in combination of RegisterModules. -func (a *App) RegisterStores(keys ...storetypes.StoreKey) error { +func (a *App) RegisterStores(keys ...string) error { a.storeKeys = append(a.storeKeys, keys...) - // a.MountStores(keys...) return nil } // GetStoreKeys returns all the stored store keys. -func (a *App) GetStoreKeys() []storetypes.StoreKey { +func (a *App) GetStoreKeys() []string { return a.storeKeys } // UnsafeFindStoreKey fetches a registered StoreKey from the App in linear time. // NOTE: This should only be used in testing. -func (a *App) UnsafeFindStoreKey(storeKey string) storetypes.StoreKey { - i := slices.IndexFunc(a.storeKeys, func(s storetypes.StoreKey) bool { return s.Name() == storeKey }) +func (a *App) UnsafeFindStoreKey(storeKey string) (string, error) { + i := slices.IndexFunc(a.storeKeys, func(s string) bool { return s == storeKey }) if i == -1 { - return nil + return "", errors.New("store key not found") } - return a.storeKeys[i] + return a.storeKeys[i], nil } func (a *App) GetStore() Store { diff --git a/runtime/v2/go.mod b/runtime/v2/go.mod index 97ce303732b4..42a6afb6579c 100644 --- a/runtime/v2/go.mod +++ b/runtime/v2/go.mod @@ -41,7 +41,6 @@ require ( cosmossdk.io/log v1.3.1 cosmossdk.io/server/v2/appmanager v0.0.0-00010101000000-000000000000 cosmossdk.io/server/v2/stf v0.0.0-00010101000000-000000000000 - cosmossdk.io/store v1.1.0 cosmossdk.io/store/v2 v2.0.0-00010101000000-000000000000 cosmossdk.io/x/tx v0.13.2 github.com/cosmos/cosmos-sdk v0.51.0 @@ -58,6 +57,7 @@ require ( cosmossdk.io/collections v0.4.0 // indirect cosmossdk.io/errors v1.0.1 // indirect cosmossdk.io/math v1.3.0 // indirect + cosmossdk.io/store v1.1.0 // indirect cosmossdk.io/x/auth v0.0.0-00010101000000-000000000000 // indirect filippo.io/edwards25519 v1.1.0 // indirect github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect diff --git a/runtime/v2/module.go b/runtime/v2/module.go index 0f18a8e457ed..b1985dffee1c 100644 --- a/runtime/v2/module.go +++ b/runtime/v2/module.go @@ -27,7 +27,6 @@ import ( "cosmossdk.io/log" "cosmossdk.io/runtime/v2/services" "cosmossdk.io/server/v2/stf" - storetypes "cosmossdk.io/store/types" rootstorev2 "cosmossdk.io/store/v2/root" "cosmossdk.io/x/tx/signing" @@ -106,10 +105,8 @@ func init() { appconfig.Provide( ProvideAppBuilder, ProvideInterfaceRegistry, - ProvideKVStoreKey, ProvideEnvironment, ProvideModuleManager, - ProvideMemoryStoreKey, ProvideAddressCodec, ProvideGenesisTxHandler, ProvideAppVersionModifier, @@ -182,7 +179,7 @@ func SetupAppBuilder(inputs AppInputs) { // registerStoreKey could instead set this on StoreOptions directly inputs.AppBuilder.storeOptions = inputs.StoreOptions for _, sk := range inputs.AppBuilder.app.storeKeys { - inputs.AppBuilder.storeOptions.StoreKeys = append(inputs.AppBuilder.storeOptions.StoreKeys, sk.String()) + inputs.AppBuilder.storeOptions.StoreKeys = append(inputs.AppBuilder.storeOptions.StoreKeys, sk) } } @@ -223,51 +220,26 @@ func ProvideInterfaceRegistry( return interfaceRegistry, nil } -func registerStoreKey(wrapper *AppBuilder, key storetypes.StoreKey) { - wrapper.app.storeKeys = append(wrapper.app.storeKeys, key) -} - -func storeKeyOverride(config *runtimev2.Module, moduleName string) *runtimev2.StoreKeyConfig { - for _, cfg := range config.OverrideStoreKeys { - if cfg.ModuleName == moduleName { - return cfg - } - } - return nil -} - -func ProvideKVStoreKey(config *runtimev2.Module, key depinject.ModuleKey, app *AppBuilder) *storetypes.KVStoreKey { - override := storeKeyOverride(config, key.Name()) - - var storeKeyName string - if override != nil { - storeKeyName = override.KvStoreKey - } else { - storeKeyName = key.Name() - } - - storeKey := storetypes.NewKVStoreKey(storeKeyName) - registerStoreKey(app, storeKey) - return storeKey -} - -func ProvideMemoryStoreKey(key depinject.ModuleKey, app *AppBuilder) *storetypes.MemoryStoreKey { - storeKey := storetypes.NewMemoryStoreKey(fmt.Sprintf("memory:%s", key.Name())) - registerStoreKey(app, storeKey) - return storeKey -} - // ProvideEnvironment provides the environment for keeper modules, while maintaining backward compatibility and provide services directly as well. func ProvideEnvironment(logger log.Logger, config *runtimev2.Module, key depinject.ModuleKey, app *AppBuilder) ( appmodulev2.Environment, store.KVStoreService, store.MemoryStoreService, ) { - kvStoreKey := ProvideKVStoreKey(config, key, app) - kvService := stf.NewKVStoreService([]byte(kvStoreKey.Name())) - memStoreKey := ProvideMemoryStoreKey(key, app) - memService := stf.NewMemoryStoreService([]byte(memStoreKey.Name())) + var kvStoreKey string + storeKeyOverride := storeKeyOverride(config, key.Name()) + if storeKeyOverride != nil { + kvStoreKey = storeKeyOverride.KvStoreKey + } else { + kvStoreKey = key.Name() + } + registerStoreKey(app, kvStoreKey) + kvService := stf.NewKVStoreService([]byte(kvStoreKey)) + + memStoreKey := fmt.Sprintf("memory:%s", key.Name()) + registerStoreKey(app, memStoreKey) + memService := stf.NewMemoryStoreService([]byte(memStoreKey)) env := appmodulev2.Environment{ Logger: logger, @@ -282,6 +254,20 @@ func ProvideEnvironment(logger log.Logger, config *runtimev2.Module, key depinje return env, kvService, memService } +func registerStoreKey(wrapper *AppBuilder, key string) { + wrapper.app.storeKeys = append(wrapper.app.storeKeys, key) +} + +func storeKeyOverride(config *runtimev2.Module, moduleName string) *runtimev2.StoreKeyConfig { + for _, cfg := range config.OverrideStoreKeys { + if cfg.ModuleName == moduleName { + return cfg + } + } + + return nil +} + type AddressCodecInputs struct { depinject.In diff --git a/runtime/v2/store.go b/runtime/v2/store.go index 02dd1257b044..73c3e3ded5f9 100644 --- a/runtime/v2/store.go +++ b/runtime/v2/store.go @@ -3,15 +3,15 @@ package runtime import ( "cosmossdk.io/core/store" "cosmossdk.io/server/v2/stf" - storetypes "cosmossdk.io/store/types" storev2 "cosmossdk.io/store/v2" "cosmossdk.io/store/v2/proof" ) // NewKVStoreService creates a new KVStoreService. // This wrapper is kept for backwards compatibility. -func NewKVStoreService(storeKey *storetypes.KVStoreKey) store.KVStoreService { - return stf.NewKVStoreService([]byte(storeKey.Name())) +// When migration from runtime to runtime/v2, use NewKVStoreService(storeKey.Name()) instead of NewKVStoreService(storeKey). +func NewKVStoreService(storeKey string) store.KVStoreService { + return stf.NewKVStoreService([]byte(storeKey)) } type Store interface { diff --git a/simapp/v2/app_di.go b/simapp/v2/app_di.go index 07bee6f82aca..8aae9d8efda2 100644 --- a/simapp/v2/app_di.go +++ b/simapp/v2/app_di.go @@ -1,14 +1,15 @@ package simapp import ( + _ "embed" + "os" + "path/filepath" + "cosmossdk.io/log" "cosmossdk.io/store/v2" "cosmossdk.io/store/v2/commitment/iavl" "cosmossdk.io/store/v2/db" "cosmossdk.io/store/v2/root" - _ "embed" - "os" - "path/filepath" "cosmossdk.io/depinject" authkeeper "cosmossdk.io/x/auth/keeper"