diff --git a/CHANGELOG.md b/CHANGELOG.md index 703883327cf5..02a4c381ab8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements +* (server) [#19884](https://github.com/cosmos/cosmos-sdk/pull/19884) Add start customizability to start command options. * (x/gov) [#19853](https://github.com/cosmos/cosmos-sdk/pull/19853) Emit `depositor` in `EventTypeProposalDeposit`. * (x/gov) [#19844](https://github.com/cosmos/cosmos-sdk/pull/19844) Emit the proposer of governance proposals. diff --git a/server/start.go b/server/start.go index 71874cd4269f..c86c1cfc2dc6 100644 --- a/server/start.go +++ b/server/start.go @@ -115,8 +115,12 @@ type StartCmdOptions struct { // PostSetup can be used to setup extra services under the same cancellable context, // it's not called in stand-alone mode, only for in-process mode. PostSetup func(svrCtx *Context, clientCtx client.Context, ctx context.Context, g *errgroup.Group) error + // PostSetupStandalone can be used to setup extra services under the same cancellable context, + PostSetupStandalone func(svrCtx *Context, clientCtx client.Context, ctx context.Context, g *errgroup.Group) error // AddFlags add custom flags to start cmd AddFlags func(cmd *cobra.Command) + // StartCommandHanlder can be used to customize the start command handler + StartCommandHandler func(svrCtx *Context, clientCtx client.Context, appCreator types.AppCreator, inProcessConsensus bool, opts StartCmdOptions) error } // StartCmd runs the service passed in, either stand-alone or in-process with @@ -132,6 +136,10 @@ func StartCmdWithOptions(appCreator types.AppCreator, defaultNodeHome string, op opts.DBOpener = openDB } + if opts.StartCommandHandler == nil { + opts.StartCommandHandler = start + } + cmd := &cobra.Command{ Use: "start", Short: "Run the full node", @@ -187,7 +195,7 @@ is performed. Note, when enabled, gRPC will also be automatically enabled. } err = wrapCPUProfile(serverCtx, func() error { - return start(serverCtx, clientCtx, appCreator, withCMT, opts) + return opts.StartCommandHandler(serverCtx, clientCtx, appCreator, withCMT, opts) }) serverCtx.Logger.Debug("received quit signal") @@ -271,14 +279,17 @@ func startStandAlone(svrCtx *Context, svrCfg serverconfig.Config, clientCtx clie return err } - cmtCfg := svrCtx.Config - home := cmtCfg.RootDir - - err = startAPIServer(ctx, g, cmtCfg, svrCfg, clientCtx, svrCtx, app, home, grpcSrv, metrics) + err = startAPIServer(ctx, g, svrCfg, clientCtx, svrCtx, app, svrCtx.Config.RootDir, grpcSrv, metrics) if err != nil { return err } + if opts.PostSetupStandalone != nil { + if err := opts.PostSetupStandalone(svrCtx, clientCtx, ctx, g); err != nil { + return err + } + } + g.Go(func() error { if err := svr.Start(); err != nil { svrCtx.Logger.Error("failed to start out-of-process ABCI server", "err", err) @@ -299,8 +310,6 @@ func startInProcess(svrCtx *Context, svrCfg serverconfig.Config, clientCtx clien metrics *telemetry.Metrics, opts StartCmdOptions, ) error { cmtCfg := svrCtx.Config - home := cmtCfg.RootDir - gRPCOnly := svrCtx.Viper.GetBool(flagGRPCOnly) g, ctx := getCtx(svrCtx, true) @@ -336,7 +345,7 @@ func startInProcess(svrCtx *Context, svrCfg serverconfig.Config, clientCtx clien return err } - err = startAPIServer(ctx, g, cmtCfg, svrCfg, clientCtx, svrCtx, app, home, grpcSrv, metrics) + err = startAPIServer(ctx, g, svrCfg, clientCtx, svrCtx, app, cmtCfg.RootDir, grpcSrv, metrics) if err != nil { return err } @@ -499,7 +508,6 @@ func startGrpcServer( func startAPIServer( ctx context.Context, g *errgroup.Group, - cmtCfg *cmtcfg.Config, svrCfg serverconfig.Config, clientCtx client.Context, svrCtx *Context, @@ -606,7 +614,7 @@ func startApp(svrCtx *Context, appCreator types.AppCreator, opts StartCmdOptions } if isTestnet, ok := svrCtx.Viper.Get(KeyIsTestnet).(bool); ok && isTestnet { - app, err = testnetify(svrCtx, home, appCreator, db, traceWriter) + app, err = testnetify(svrCtx, appCreator, db, traceWriter) if err != nil { return app, traceCleanupFn, err } @@ -632,6 +640,10 @@ func InPlaceTestnetCreator(testnetAppCreator types.AppCreator) *cobra.Command { opts.DBOpener = openDB } + if opts.StartCommandHandler == nil { + opts.StartCommandHandler = start + } + cmd := &cobra.Command{ Use: "in-place-testnet [newChainID] [newOperatorAddress]", Short: "Create and start a testnet from current local state", @@ -696,7 +708,7 @@ you want to test the upgrade handler itself. serverCtx.Viper.Set(KeyNewOpAddr, newOperatorAddress) err = wrapCPUProfile(serverCtx, func() error { - return start(serverCtx, clientCtx, testnetAppCreator, withCMT, opts) + return opts.StartCommandHandler(serverCtx, clientCtx, testnetAppCreator, withCMT, opts) }) serverCtx.Logger.Debug("received quit signal") @@ -719,7 +731,7 @@ you want to test the upgrade handler itself. // testnetify modifies both state and blockStore, allowing the provided operator address and local validator key to control the network // that the state in the data folder represents. The chainID of the local genesis file is modified to match the provided chainID. -func testnetify(ctx *Context, home string, testnetAppCreator types.AppCreator, db dbm.DB, traceWriter io.WriteCloser) (types.Application, error) { +func testnetify(ctx *Context, testnetAppCreator types.AppCreator, db dbm.DB, traceWriter io.WriteCloser) (types.Application, error) { config := ctx.Config newChainID, ok := ctx.Viper.Get(KeyNewChainID).(string) @@ -765,9 +777,6 @@ func testnetify(ctx *Context, home string, testnetAppCreator types.AppCreator, d return nil, err } validatorAddress := userPubKey.Address() - if err != nil { - return nil, err - } stateStore := sm.NewStore(stateDB, sm.StoreOptions{ DiscardABCIResponses: config.Storage.DiscardABCIResponses, diff --git a/server/util.go b/server/util.go index b1cbec13f751..de656d949001 100644 --- a/server/util.go +++ b/server/util.go @@ -341,6 +341,35 @@ func AddCommands(rootCmd *cobra.Command, defaultNodeHome string, appCreator type ) } +// AddCommandsWithStartCmdOptions adds server commands with the provided StartCmdOptions. +func AddCommandsWithStartCmdOptions(rootCmd *cobra.Command, defaultNodeHome string, appCreator types.AppCreator, appExport types.AppExporter, opts StartCmdOptions) { + cometCmd := &cobra.Command{ + Use: "comet", + Aliases: []string{"cometbft", "tendermint"}, + Short: "CometBFT subcommands", + } + + cometCmd.AddCommand( + ShowNodeIDCmd(), + ShowValidatorCmd(), + ShowAddressCmd(), + VersionCmd(), + cmtcmd.ResetAllCmd, + cmtcmd.ResetStateCmd, + BootstrapStateCmd(appCreator), + ) + + startCmd := StartCmdWithOptions(appCreator, defaultNodeHome, opts) + + rootCmd.AddCommand( + startCmd, + cometCmd, + ExportCmd(appExport, defaultNodeHome), + version.NewVersionCommand(), + NewRollbackCmd(appCreator, defaultNodeHome), + ) +} + // AddTestnetCreatorCommand allows chains to create a testnet from the state existing in their node's data directory. func AddTestnetCreatorCommand(rootCmd *cobra.Command, appCreator types.AppCreator, addStartFlags types.ModuleInitFlags) { testnetCreateCmd := InPlaceTestnetCreator(appCreator) diff --git a/simapp/simd/cmd/commands.go b/simapp/simd/cmd/commands.go index e8f59e5bc123..7540dd7009a3 100644 --- a/simapp/simd/cmd/commands.go +++ b/simapp/simd/cmd/commands.go @@ -21,8 +21,6 @@ import ( "github.com/cosmos/cosmos-sdk/client/pruning" "github.com/cosmos/cosmos-sdk/client/rpc" "github.com/cosmos/cosmos-sdk/client/snapshot" - "github.com/cosmos/cosmos-sdk/codec" - codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/server" serverconfig "github.com/cosmos/cosmos-sdk/server/config" servertypes "github.com/cosmos/cosmos-sdk/server/types" @@ -106,8 +104,6 @@ custom-field = "{{ .Custom.CustomField }}"` func initRootCmd( rootCmd *cobra.Command, txConfig client.TxConfig, - interfaceRegistry codectypes.InterfaceRegistry, - appCodec codec.Codec, basicManager module.BasicManager, ) { cfg := sdk.GetConfig() @@ -122,7 +118,11 @@ func initRootCmd( snapshot.Cmd(newApp), ) - server.AddCommands(rootCmd, simapp.DefaultNodeHome, newApp, appExport, addModuleInitFlags) + server.AddCommandsWithStartCmdOptions(rootCmd, simapp.DefaultNodeHome, newApp, appExport, server.StartCmdOptions{ + AddFlags: func(startCmd *cobra.Command) { + crisis.AddModuleInitFlags(startCmd) + }, + }) // add keybase, auxiliary RPC, query, genesis, and tx child commands rootCmd.AddCommand( @@ -134,10 +134,6 @@ func initRootCmd( ) } -func addModuleInitFlags(startCmd *cobra.Command) { - crisis.AddModuleInitFlags(startCmd) -} - // genesisCommand builds genesis-related `simd genesis` command. Users may provide application specific commands as a parameter func genesisCommand(txConfig client.TxConfig, basicManager module.BasicManager, cmds ...*cobra.Command) *cobra.Command { cmd := genutilcli.Commands(txConfig, basicManager, simapp.DefaultNodeHome) diff --git a/simapp/simd/cmd/root.go b/simapp/simd/cmd/root.go index ee8d7e089892..f56b849a9c56 100644 --- a/simapp/simd/cmd/root.go +++ b/simapp/simd/cmd/root.go @@ -96,7 +96,7 @@ func NewRootCmd() *cobra.Command { }, } - initRootCmd(rootCmd, encodingConfig.TxConfig, encodingConfig.InterfaceRegistry, encodingConfig.Codec, tempApp.BasicModuleManager) + initRootCmd(rootCmd, encodingConfig.TxConfig, tempApp.BasicModuleManager) // add keyring to autocli opts autoCliOpts := tempApp.AutoCliOpts() diff --git a/simapp/simd/cmd/root_v2.go b/simapp/simd/cmd/root_v2.go index 934ee3584925..94ef4cd9d17b 100644 --- a/simapp/simd/cmd/root_v2.go +++ b/simapp/simd/cmd/root_v2.go @@ -84,7 +84,7 @@ func NewRootCmd() *cobra.Command { }, } - initRootCmd(rootCmd, clientCtx.TxConfig, clientCtx.InterfaceRegistry, clientCtx.Codec, moduleBasicManager) + initRootCmd(rootCmd, clientCtx.TxConfig, moduleBasicManager) if err := autoCliOpts.EnhanceRootCommand(rootCmd); err != nil { panic(err)