From 390d97f80796675cbc3f78abd6073969d05fbe6b Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Wed, 9 Oct 2024 17:17:25 -0500 Subject: [PATCH] refactor(runtime/v2): rm singleton storebuilder scope --- runtime/v2/store.go | 22 ---------------------- simapp/v2/app_di.go | 10 +++++----- simapp/v2/app_test.go | 5 ++--- simapp/v2/simdv2/cmd/commands.go | 16 ++++++++-------- simapp/v2/simdv2/cmd/root_di.go | 4 ++-- 5 files changed, 17 insertions(+), 40 deletions(-) diff --git a/runtime/v2/store.go b/runtime/v2/store.go index 7f7b2135c809..40912ea41f48 100644 --- a/runtime/v2/store.go +++ b/runtime/v2/store.go @@ -3,35 +3,13 @@ package runtime import ( "errors" "fmt" - "sync" "cosmossdk.io/core/store" "cosmossdk.io/server/v2/stf" storev2 "cosmossdk.io/store/v2" "cosmossdk.io/store/v2/proof" - "cosmossdk.io/store/v2/root" ) -var ( - storeBuilderSingleton root.Builder - storeBuilderSingletonOnce sync.Once -) - -// ProvideSingletonScopedStoreBuilder returns a store builder that is a singleton -// in the scope of the process lifetime. -func ProvideSingletonScopedStoreBuilder() root.Builder { - storeBuilderSingletonOnce.Do(func() { - storeBuilderSingleton = root.NewBuilder() - }) - return storeBuilderSingleton -} - -// ResetSingletonScopedStoreBuilder resets the singleton store builder. Applications -// should not ever need to call this, but it may be useful in tests. -func ResetSingletonScopedStoreBuilder() { - storeBuilderSingletonOnce = sync.Once{} -} - // NewKVStoreService creates a new KVStoreService. // This wrapper is kept for backwards compatibility. // When migrating from runtime to runtime/v2, use runtimev2.NewKVStoreService(storeKey.Name()) instead of runtime.NewKVStoreService(storeKey). diff --git a/simapp/v2/app_di.go b/simapp/v2/app_di.go index 29f62defcde6..87a83b3f8e2e 100644 --- a/simapp/v2/app_di.go +++ b/simapp/v2/app_di.go @@ -66,7 +66,6 @@ func AppConfig() depinject.Config { codec.ProvideAddressCodec, codec.ProvideProtoCodec, codec.ProvideLegacyAmino, - runtime.ProvideSingletonScopedStoreBuilder, ), depinject.Invoke( std.RegisterInterfaces, @@ -79,12 +78,12 @@ func AppConfig() depinject.Config { func NewSimApp[T transaction.Tx]( logger log.Logger, viper *viper.Viper, + storeBuilder root.Builder, ) *SimApp[T] { var ( - app = &SimApp[T]{} - appBuilder *runtime.AppBuilder[T] - storeBuilder root.Builder - err error + app = &SimApp[T]{} + appBuilder *runtime.AppBuilder[T] + err error // merge the AppConfig and other configuration in one config appConfig = depinject.Configs( @@ -92,6 +91,7 @@ func NewSimApp[T transaction.Tx]( depinject.Supply( logger, viper, + storeBuilder, // ADVANCED CONFIGURATION diff --git a/simapp/v2/app_test.go b/simapp/v2/app_test.go index 8c705565956c..8990ead6d2a3 100644 --- a/simapp/v2/app_test.go +++ b/simapp/v2/app_test.go @@ -18,10 +18,10 @@ import ( "cosmossdk.io/core/transaction" "cosmossdk.io/log" sdkmath "cosmossdk.io/math" - "cosmossdk.io/runtime/v2" serverv2 "cosmossdk.io/server/v2" serverv2store "cosmossdk.io/server/v2/store" "cosmossdk.io/store/v2/db" + "cosmossdk.io/store/v2/root" banktypes "cosmossdk.io/x/bank/types" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" @@ -40,8 +40,7 @@ func NewTestApp(t *testing.T) (*SimApp[transaction.Tx], context.Context) { vp.Set(serverv2store.FlagAppDBBackend, string(db.DBTypeGoLevelDB)) vp.Set(serverv2.FlagHome, t.TempDir()) - runtime.ResetSingletonScopedStoreBuilder() - app := NewSimApp[transaction.Tx](logger, vp) + app := NewSimApp[transaction.Tx](logger, vp, root.NewBuilder()) genesis := app.ModuleManager().DefaultGenesis() privVal := mock.NewPV() diff --git a/simapp/v2/simdv2/cmd/commands.go b/simapp/v2/simdv2/cmd/commands.go index 6cc233cc9b19..b21185c5208d 100644 --- a/simapp/v2/simdv2/cmd/commands.go +++ b/simapp/v2/simdv2/cmd/commands.go @@ -36,11 +36,6 @@ import ( v2 "github.com/cosmos/cosmos-sdk/x/genutil/v2/cli" ) -func newApp[T transaction.Tx](logger log.Logger, viper *viper.Viper) serverv2.AppI[T] { - viper.Set(serverv2.FlagHome, simapp.DefaultNodeHome) - return serverv2.AppI[T](simapp.NewSimApp[T](logger, viper)) -} - func initRootCmd[T transaction.Tx]( rootCmd *cobra.Command, txConfig client.TxConfig, @@ -71,10 +66,15 @@ func initRootCmd[T transaction.Tx]( offchain.OffChain(), ) + appCreator := func(logger log.Logger, viper *viper.Viper) serverv2.AppI[T] { + viper.Set(serverv2.FlagHome, simapp.DefaultNodeHome) + return serverv2.AppI[T](simapp.NewSimApp[T](logger, viper, storeBuilder)) + } + // wire server commands if err = serverv2.AddCommands( rootCmd, - newApp, + appCreator, logger, initServerConfig(), cometbft.New( @@ -176,13 +176,13 @@ func appExport[T transaction.Tx]( var simApp *simapp.SimApp[T] if height != -1 { - simApp = simapp.NewSimApp[T](logger, viper) + simApp = simapp.NewSimApp[T](logger, viper, root.NewBuilder()) if err := simApp.LoadHeight(uint64(height)); err != nil { return genutilv2.ExportedApp{}, err } } else { - simApp = simapp.NewSimApp[T](logger, viper) + simApp = simapp.NewSimApp[T](logger, viper, root.NewBuilder()) } return simApp.ExportAppStateAndValidators(jailAllowedAddrs) diff --git a/simapp/v2/simdv2/cmd/root_di.go b/simapp/v2/simdv2/cmd/root_di.go index 30291b4ecdb3..7bfc9b29acff 100644 --- a/simapp/v2/simdv2/cmd/root_di.go +++ b/simapp/v2/simdv2/cmd/root_di.go @@ -32,14 +32,14 @@ func NewRootCmd[T transaction.Tx]() *cobra.Command { autoCliOpts autocli.AppOptions moduleManager *runtime.MM[T] clientCtx client.Context - storeBuilder root.Builder + storeBuilder = root.NewBuilder() ) if err := depinject.Inject( depinject.Configs( simapp.AppConfig(), depinject.Provide(ProvideClientContext), - depinject.Supply(log.NewNopLogger()), + depinject.Supply(log.NewNopLogger(), storeBuilder), ), &storeBuilder, &autoCliOpts,