Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(runtime/v2): use AppI.GetStore() to fetch an initialized RootStore #22205

Merged
merged 10 commits into from
Oct 10, 2024
3 changes: 3 additions & 0 deletions runtime/v2/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ func ProvideEnvironment(
// - header.Service
// - comet.Service
// - event.Service
// - store/v2/root.Builder
//
// They are all required. For most use cases these default services bindings should be sufficient.
// Power users (or tests) may wish to provide their own services bindings, in which case they must
Expand All @@ -244,11 +245,13 @@ func DefaultServiceBindings() depinject.Config {
cometService comet.Service = &services.ContextAwareCometInfoService{}
headerService = services.NewGenesisHeaderService(stf.HeaderService{})
eventService = services.NewGenesisEventService(stf.NewEventService())
storeBuilder = root.NewBuilder()
)
return depinject.Supply(
kvServiceFactory,
headerService,
cometService,
eventService,
storeBuilder,
)
}
22 changes: 0 additions & 22 deletions runtime/v2/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
16 changes: 1 addition & 15 deletions server/v2/cometbft/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import (
"os"
"path/filepath"

"cosmossdk.io/server/v2/store"
"cosmossdk.io/store/v2/root"

abciserver "github.com/cometbft/cometbft/abci/server"
cmtcmd "github.com/cometbft/cometbft/cmd/cometbft/commands"
cmtcfg "github.com/cometbft/cometbft/config"
Expand Down Expand Up @@ -45,21 +42,18 @@ type CometBFTServer[T transaction.Tx] struct {

initTxCodec transaction.Codec[T]
logger log.Logger
storeBuilder root.Builder
serverOptions ServerOptions[T]
config Config
cfgOptions []CfgOption
}

func New[T transaction.Tx](
txCodec transaction.Codec[T],
storeBuilder root.Builder,
serverOptions ServerOptions[T],
cfgOptions ...CfgOption,
) *CometBFTServer[T] {
return &CometBFTServer[T]{
initTxCodec: txCodec,
storeBuilder: storeBuilder,
serverOptions: serverOptions,
cfgOptions: cfgOptions,
}
Expand Down Expand Up @@ -106,16 +100,8 @@ func (s *CometBFTServer[T]) Init(appI serverv2.AppI[T], cfg map[string]any, logg
indexEvents[e] = struct{}{}
}

storeCfg, err := store.UnmarshalConfig(cfg)
if err != nil {
return err
}
rs, err := s.storeBuilder.Build(logger, storeCfg)
if err != nil {
return err
}

s.logger = logger.With(log.ModuleKey, s.Name())
rs := appI.GetStore()
consensus := NewConsensus(
s.logger,
appI.Name(),
Expand Down
15 changes: 3 additions & 12 deletions server/v2/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"cosmossdk.io/server/v2/appmanager"
"cosmossdk.io/server/v2/store"
storev2 "cosmossdk.io/store/v2"
"cosmossdk.io/store/v2/root"
)

type mockInterfaceRegistry struct{}
Expand Down Expand Up @@ -50,18 +49,10 @@ func (*mockApp[T]) InterfaceRegistry() coreserver.InterfaceRegistry {
return &mockInterfaceRegistry{}
}

var _ root.Builder = &mockStoreBuilder{}

type mockStoreBuilder struct{}

func (m mockStoreBuilder) Build(logger log.Logger, config *root.Config) (storev2.RootStore, error) {
return nil, nil
func (*mockApp[T]) GetStore() storev2.RootStore {
return nil
}

func (m mockStoreBuilder) RegisterKey(string) {}

func (m mockStoreBuilder) Get() storev2.RootStore { return nil }

func TestServer(t *testing.T) {
currentDir, err := os.Getwd()
require.NoError(t, err)
Expand All @@ -78,7 +69,7 @@ func TestServer(t *testing.T) {
err = grpcServer.Init(&mockApp[transaction.Tx]{}, cfg, logger)
require.NoError(t, err)

storeServer := store.New[transaction.Tx](&mockStoreBuilder{})
storeServer := store.New[transaction.Tx]()
err = storeServer.Init(&mockApp[transaction.Tx]{}, cfg, logger)
require.NoError(t, err)

Expand Down
23 changes: 7 additions & 16 deletions server/v2/store/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,17 @@ const ServerName = "store"
// Server manages store config and contains prune & snapshot commands
type Server[T transaction.Tx] struct {
config *root.Config
builder root.Builder
backend storev2.Backend
}

func New[T transaction.Tx](builder root.Builder) *Server[T] {
return &Server[T]{builder: builder}
func New[T transaction.Tx]() *Server[T] {
return &Server[T]{}
}

func (s *Server[T]) Init(_ serverv2.AppI[T], cfg map[string]any, log log.Logger) error {
var err error
s.config, err = UnmarshalConfig(cfg)
if err != nil {
return fmt.Errorf("failed to unmarshal config: %w", err)
}
s.backend, err = s.builder.Build(log, s.config)
if err != nil {
return fmt.Errorf("failed to create store backend: %w", err)
}

return nil
func (s *Server[T]) Init(app serverv2.AppI[T], v map[string]any, _ log.Logger) (err error) {
s.backend = app.GetStore()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

init should still set the config.

s.config, err = UnmarshalConfig(v)
return err
}

func (s *Server[T]) Name() string {
Expand Down Expand Up @@ -90,7 +81,7 @@ func UnmarshalConfig(cfg map[string]any) (*root.Config, error) {
Options: root.DefaultStoreOptions(),
}
if err := serverv2.UnmarshalSubConfig(cfg, ServerName, config); err != nil {
return nil, fmt.Errorf("failed to unmarshal config: %w", err)
return nil, fmt.Errorf("failed to unmarshal store config: %w", err)
}
home := cfg[serverv2.FlagHome]
if home != nil {
Expand Down
2 changes: 2 additions & 0 deletions server/v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"cosmossdk.io/core/transaction"
"cosmossdk.io/log"
"cosmossdk.io/server/v2/appmanager"
"cosmossdk.io/store/v2"
)

type AppCreator[T transaction.Tx] func(log.Logger, *viper.Viper) AppI[T]
Expand All @@ -17,4 +18,5 @@ type AppI[T transaction.Tx] interface {
InterfaceRegistry() server.InterfaceRegistry
GetAppManager() *appmanager.AppManager[T]
GetQueryHandlers() map[string]appmodulev2.Handler
GetStore() store.RootStore
}
3 changes: 1 addition & 2 deletions simapp/v2/app_di.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ func AppConfig() depinject.Config {
codec.ProvideAddressCodec,
codec.ProvideProtoCodec,
codec.ProvideLegacyAmino,
runtime.ProvideSingletonScopedStoreBuilder,
),
depinject.Invoke(
std.RegisterInterfaces,
Expand All @@ -83,8 +82,8 @@ func NewSimApp[T transaction.Tx](
var (
app = &SimApp[T]{}
appBuilder *runtime.AppBuilder[T]
storeBuilder root.Builder
err error
storeBuilder root.Builder

// merge the AppConfig and other configuration in one config
appConfig = depinject.Configs(
Expand Down
2 changes: 0 additions & 2 deletions simapp/v2/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ 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"
Expand All @@ -40,7 +39,6 @@ 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)
genesis := app.ModuleManager().DefaultGenesis()

Expand Down
7 changes: 2 additions & 5 deletions simapp/v2/simdv2/cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"cosmossdk.io/server/v2/cometbft"
serverstore "cosmossdk.io/server/v2/store"
"cosmossdk.io/simapp/v2"
"cosmossdk.io/store/v2/root"
confixcmd "cosmossdk.io/tools/confix/cmd"

"github.com/cosmos/cosmos-sdk/client"
Expand All @@ -44,7 +43,6 @@ func newApp[T transaction.Tx](logger log.Logger, viper *viper.Viper) serverv2.Ap
func initRootCmd[T transaction.Tx](
rootCmd *cobra.Command,
txConfig client.TxConfig,
storeBuilder root.Builder,
moduleManager *runtimev2.MM[T],
) {
cfg := sdk.GetConfig()
Expand All @@ -54,7 +52,7 @@ func initRootCmd[T transaction.Tx](
genutilcli.InitCmd(moduleManager),
debug.Cmd(),
confixcmd.ConfigCommand(),
NewTestnetCmd(storeBuilder, moduleManager),
NewTestnetCmd(moduleManager),
)

logger, err := serverv2.NewLogger(viper.New(), rootCmd.OutOrStdout())
Expand All @@ -79,12 +77,11 @@ func initRootCmd[T transaction.Tx](
initServerConfig(),
cometbft.New(
&genericTxDecoder[T]{txConfig},
storeBuilder,
initCometOptions[T](),
initCometConfig(),
),
grpc.New[T](),
serverstore.New[T](storeBuilder),
serverstore.New[T](),
telemetry.New[T](),
); err != nil {
panic(err)
Expand Down
5 changes: 1 addition & 4 deletions simapp/v2/simdv2/cmd/root_di.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"cosmossdk.io/log"
"cosmossdk.io/runtime/v2"
"cosmossdk.io/simapp/v2"
"cosmossdk.io/store/v2/root"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/config"
Expand All @@ -32,7 +31,6 @@ func NewRootCmd[T transaction.Tx]() *cobra.Command {
autoCliOpts autocli.AppOptions
moduleManager *runtime.MM[T]
clientCtx client.Context
storeBuilder root.Builder
)

if err := depinject.Inject(
Expand All @@ -41,7 +39,6 @@ func NewRootCmd[T transaction.Tx]() *cobra.Command {
depinject.Provide(ProvideClientContext),
depinject.Supply(log.NewNopLogger()),
),
&storeBuilder,
&autoCliOpts,
&moduleManager,
&clientCtx,
Expand Down Expand Up @@ -74,7 +71,7 @@ func NewRootCmd[T transaction.Tx]() *cobra.Command {
},
}

initRootCmd(rootCmd, clientCtx.TxConfig, storeBuilder, moduleManager)
initRootCmd(rootCmd, clientCtx.TxConfig, moduleManager)

nodeCmds := nodeservice.NewNodeCommands()
autoCliOpts.ModuleOptions = make(map[string]*autocliv1.ModuleOptions)
Expand Down
13 changes: 5 additions & 8 deletions simapp/v2/simdv2/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"cosmossdk.io/server/v2/api/grpc"
"cosmossdk.io/server/v2/cometbft"
"cosmossdk.io/server/v2/store"
"cosmossdk.io/store/v2/root"
banktypes "cosmossdk.io/x/bank/types"
bankv2types "cosmossdk.io/x/bank/v2/types"
stakingtypes "cosmossdk.io/x/staking/types"
Expand Down Expand Up @@ -88,7 +87,7 @@ func addTestnetFlagsToCmd(cmd *cobra.Command) {

// NewTestnetCmd creates a root testnet command with subcommands to run an in-process testnet or initialize
// validator configuration files for running a multi-validator testnet in a separate process
func NewTestnetCmd[T transaction.Tx](sb root.Builder, mm *runtimev2.MM[T]) *cobra.Command {
func NewTestnetCmd[T transaction.Tx](mm *runtimev2.MM[T]) *cobra.Command {
testnetCmd := &cobra.Command{
Use: "testnet",
Short: "subcommands for starting or configuring local testnets",
Expand All @@ -97,13 +96,13 @@ func NewTestnetCmd[T transaction.Tx](sb root.Builder, mm *runtimev2.MM[T]) *cobr
RunE: client.ValidateCmd,
}

testnetCmd.AddCommand(testnetInitFilesCmd(sb, mm))
testnetCmd.AddCommand(testnetInitFilesCmd(mm))

return testnetCmd
}

// testnetInitFilesCmd returns a cmd to initialize all files for CometBFT testnet and application
func testnetInitFilesCmd[T transaction.Tx](sb root.Builder, mm *runtimev2.MM[T]) *cobra.Command {
func testnetInitFilesCmd[T transaction.Tx](mm *runtimev2.MM[T]) *cobra.Command {
cmd := &cobra.Command{
Use: "init-files",
Short: "Initialize config directories & files for a multi-validator testnet running locally via separate processes (e.g. Docker Compose or similar)",
Expand Down Expand Up @@ -144,7 +143,7 @@ Example:
return err
}

return initTestnetFiles(clientCtx, sb, cmd, config, mm, args)
return initTestnetFiles(clientCtx, cmd, config, mm, args)
},
}

Expand All @@ -166,7 +165,6 @@ const nodeDirPerm = 0o755
// initTestnetFiles initializes testnet files for a testnet to be run in a separate process
func initTestnetFiles[T transaction.Tx](
clientCtx client.Context,
sb root.Builder,
cmd *cobra.Command,
nodeConfig *cmtconfig.Config,
mm *runtimev2.MM[T],
Expand Down Expand Up @@ -341,11 +339,10 @@ func initTestnetFiles[T transaction.Tx](
// Write server config
cometServer := cometbft.New[T](
&genericTxDecoder[T]{clientCtx.TxConfig},
sb,
cometbft.ServerOptions[T]{},
cometbft.OverwriteDefaultConfigTomlConfig(nodeConfig),
)
storeServer := store.New[T](sb)
storeServer := store.New[T]()
grpcServer := grpc.New[T](grpc.OverwriteDefaultConfig(grpcConfig))
server := serverv2.NewServer[T](log.NewNopLogger(), serverCfg, cometServer, grpcServer, storeServer)
err = server.WriteConfig(filepath.Join(nodeDir, "config"))
Expand Down
Loading