Skip to content

Commit

Permalink
refactor(simdv2): allow non-comet server components
Browse files Browse the repository at this point in the history
This renames NewRootCmd to NewCometBFTRootCmd, to make it clear that it
uses CometBFT for the consensus layer. Callers who want to use a
different consensus-providing server component can instead call
NewRootCmdWithConsensusComponent, passing in a callback to be evaluated
with a client context in order to produce the component.

This pattern is working for Gordian-Cosmos integration.

I'm not especially tied to the new function names. I'm also open to more
clear naming for "consensus component" which occurs a few times.
  • Loading branch information
mark-rushakoff committed Oct 23, 2024
1 parent bf12702 commit 39778d7
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 15 deletions.
9 changes: 2 additions & 7 deletions simapp/v2/simdv2/cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"cosmossdk.io/server/v2/api/grpc"
"cosmossdk.io/server/v2/api/rest"
"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"
Expand All @@ -43,8 +42,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()
Expand All @@ -70,11 +69,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](),
Expand Down
27 changes: 23 additions & 4 deletions simapp/v2/simdv2/cmd/root_di.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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]
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions simapp/v2/simdv2/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion simapp/v2/simdv2/cmd/testnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion simapp/v2/simdv2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 39778d7

Please sign in to comment.