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

feat(server): add custom start handler (backport #19854) #19884

Merged
merged 5 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
39 changes: 24 additions & 15 deletions server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand All @@ -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",
Expand Down Expand Up @@ -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")
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
29 changes: 29 additions & 0 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 5 additions & 9 deletions simapp/simd/cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand All @@ -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(
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion simapp/simd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion simapp/simd/cmd/root_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading