From 63a234ed4f6e69fc09a02e1380c4031944188b0b Mon Sep 17 00:00:00 2001 From: Mark Rushakoff Date: Thu, 24 Oct 2024 04:32:38 -0400 Subject: [PATCH] refactor(simdv2): allow non-comet server components (#22351) (cherry picked from commit 6e6255df1f559826761d9f462d5b44fac81dde1e) --- simapp/v2/simdv2/cmd/commands.go | 9 ++------- simapp/v2/simdv2/cmd/root_di.go | 27 +++++++++++++++++++++++---- simapp/v2/simdv2/cmd/root_test.go | 4 ++-- simapp/v2/simdv2/cmd/testnet_test.go | 2 +- simapp/v2/simdv2/main.go | 2 +- 5 files changed, 29 insertions(+), 15 deletions(-) diff --git a/simapp/v2/simdv2/cmd/commands.go b/simapp/v2/simdv2/cmd/commands.go index b8721f3917de..affa87ad87f3 100644 --- a/simapp/v2/simdv2/cmd/commands.go +++ b/simapp/v2/simdv2/cmd/commands.go @@ -16,7 +16,6 @@ import ( serverv2 "cosmossdk.io/server/v2" "cosmossdk.io/server/v2/api/grpc" "cosmossdk.io/server/v2/api/telemetry" - "cosmossdk.io/server/v2/cometbft" serverstore "cosmossdk.io/server/v2/store" "cosmossdk.io/simapp/v2" confixcmd "cosmossdk.io/tools/confix/cmd" @@ -42,8 +41,8 @@ func newApp[T transaction.Tx](logger log.Logger, viper *viper.Viper) serverv2.Ap func initRootCmd[T transaction.Tx]( rootCmd *cobra.Command, - txConfig client.TxConfig, moduleManager *runtimev2.MM[T], + consensusComponent serverv2.ServerComponent[T], ) { cfg := sdk.GetConfig() cfg.Seal() @@ -69,11 +68,7 @@ func initRootCmd[T transaction.Tx]( rootCmd, newApp, initServerConfig(), - cometbft.New( - &genericTxDecoder[T]{txConfig}, - initCometOptions[T](), - initCometConfig(), - ), + consensusComponent, grpc.New[T](), serverstore.New[T](), telemetry.New[T](), diff --git a/simapp/v2/simdv2/cmd/root_di.go b/simapp/v2/simdv2/cmd/root_di.go index a43ce3bca8c3..da1bcf77061a 100644 --- a/simapp/v2/simdv2/cmd/root_di.go +++ b/simapp/v2/simdv2/cmd/root_di.go @@ -13,6 +13,8 @@ import ( "cosmossdk.io/depinject" "cosmossdk.io/log" "cosmossdk.io/runtime/v2" + serverv2 "cosmossdk.io/server/v2" + "cosmossdk.io/server/v2/cometbft" "cosmossdk.io/simapp/v2" basedepinject "cosmossdk.io/x/accounts/defaults/base/depinject" lockupdepinject "cosmossdk.io/x/accounts/defaults/lockup/depinject" @@ -28,8 +30,25 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth/types" ) -// NewRootCmd creates a new root command for simd. It is called once in the main function. -func NewRootCmd[T transaction.Tx]() *cobra.Command { +// NewCometBFTRootCmd creates a new root command for simd, +// using the CometBFT server component for consensus. +// It is called once in the main function. +func NewCometBFTRootCmd[T transaction.Tx]() *cobra.Command { + return NewRootCmdWithConsensusComponent(func(cc client.Context) serverv2.ServerComponent[T] { + return cometbft.New[T]( + &genericTxDecoder[T]{cc.TxConfig}, + initCometOptions[T](), + initCometConfig(), + ) + }) +} + +// NewRootCmdWithConsensusComponent returns a new root command, +// using the provided callback to instantiate the server component for the consensus layer. +// Callers who want to use CometBFT should call [NewCometBFTRootCmd] directly. +func NewRootCmdWithConsensusComponent[T transaction.Tx]( + makeConsensusComponent func(cc client.Context) serverv2.ServerComponent[T], +) *cobra.Command { var ( autoCliOpts autocli.AppOptions moduleManager *runtime.MM[T] @@ -82,12 +101,12 @@ func NewRootCmd[T transaction.Tx]() *cobra.Command { }, } - initRootCmd(rootCmd, clientCtx.TxConfig, moduleManager) + consensusComponent := makeConsensusComponent(clientCtx) + initRootCmd(rootCmd, moduleManager, consensusComponent) nodeCmds := nodeservice.NewNodeCommands() autoCliOpts.ModuleOptions = make(map[string]*autocliv1.ModuleOptions) autoCliOpts.ModuleOptions[nodeCmds.Name()] = nodeCmds.AutoCLIOptions() - if err := autoCliOpts.EnhanceRootCommand(rootCmd); err != nil { panic(err) } diff --git a/simapp/v2/simdv2/cmd/root_test.go b/simapp/v2/simdv2/cmd/root_test.go index 7c51b1b21170..87678bc4b909 100644 --- a/simapp/v2/simdv2/cmd/root_test.go +++ b/simapp/v2/simdv2/cmd/root_test.go @@ -16,7 +16,7 @@ import ( ) func TestInitCmd(t *testing.T) { - rootCmd := cmd.NewRootCmd[transaction.Tx]() + rootCmd := cmd.NewCometBFTRootCmd[transaction.Tx]() rootCmd.SetArgs([]string{ "init", // Test the init cmd "simapp-test", // Moniker @@ -29,7 +29,7 @@ func TestInitCmd(t *testing.T) { func TestHomeFlagRegistration(t *testing.T) { homeDir := "/tmp/foo" - rootCmd := cmd.NewRootCmd[transaction.Tx]() + rootCmd := cmd.NewCometBFTRootCmd[transaction.Tx]() rootCmd.SetArgs([]string{ "query", fmt.Sprintf("--%s", flags.FlagHome), diff --git a/simapp/v2/simdv2/cmd/testnet_test.go b/simapp/v2/simdv2/cmd/testnet_test.go index 145a32608e60..3c7769d912e8 100644 --- a/simapp/v2/simdv2/cmd/testnet_test.go +++ b/simapp/v2/simdv2/cmd/testnet_test.go @@ -16,7 +16,7 @@ import ( ) func TestInitTestFilesCmd(t *testing.T) { - rootCmd := cmd.NewRootCmd[transaction.Tx]() + rootCmd := cmd.NewCometBFTRootCmd[transaction.Tx]() rootCmd.SetArgs([]string{ "testnet", // Test the testnet init-files command "init-files", diff --git a/simapp/v2/simdv2/main.go b/simapp/v2/simdv2/main.go index eb7d9a1b005b..84248ceed0a6 100644 --- a/simapp/v2/simdv2/main.go +++ b/simapp/v2/simdv2/main.go @@ -12,7 +12,7 @@ import ( ) func main() { - rootCmd := cmd.NewRootCmd[transaction.Tx]() + rootCmd := cmd.NewCometBFTRootCmd[transaction.Tx]() if err := serverv2.Execute(rootCmd, clientv2helpers.EnvPrefix, simapp.DefaultNodeHome); err != nil { fmt.Fprintln(rootCmd.OutOrStderr(), err) os.Exit(1)