Skip to content

Commit

Permalink
refactor(runtime/v2): remove store v1 dep (#20189)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt authored Apr 25, 2024
1 parent 9d5fba3 commit 16c171a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 58 deletions.
17 changes: 8 additions & 9 deletions runtime/v2/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package runtime
import (
"context"
"encoding/json"
"errors"

"golang.org/x/exp/slices"

Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion runtime/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
70 changes: 28 additions & 42 deletions runtime/v2/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -106,10 +105,8 @@ func init() {
appconfig.Provide(
ProvideAppBuilder,
ProvideInterfaceRegistry,
ProvideKVStoreKey,
ProvideEnvironment,
ProvideModuleManager,
ProvideMemoryStoreKey,
ProvideAddressCodec,
ProvideGenesisTxHandler,
ProvideAppVersionModifier,
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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,
Expand All @@ -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

Expand Down
6 changes: 3 additions & 3 deletions runtime/v2/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions simapp/v2/app_di.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit 16c171a

Please sign in to comment.