feat: make StartCmd more customizable (#16209)

This commit is contained in:
yihuang 2023-05-19 17:41:29 +08:00 committed by GitHub
parent 89eee12fc8
commit de75e568b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 6 deletions

View File

@ -67,6 +67,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (core) [#14860](https://github.com/cosmos/cosmos-sdk/pull/14860) Add `Precommit` and `PrepareCheckState` AppModule callbacks.
* (tx) [#15992](https://github.com/cosmos/cosmos-sdk/pull/15992) Add `WithExtensionOptions` in tx Factory to allow `SetExtensionOptions` with given extension options.
* (types/simulation) [#16074](https://github.com/cosmos/cosmos-sdk/pull/16074) Add generic SimulationStoreDecoder for modules using collections.
* (cli) [#16209](https://github.com/cosmos/cosmos-sdk/pull/16209) Make `StartCmd` more customizable.
### Improvements
@ -212,6 +213,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* `simulation.NewOperationMsg` now marshals the operation msg as proto bytes instead of legacy amino JSON bytes.
* `simulation.NewOperationMsg` is now 2-arity instead of 3-arity with the obsolete argument `codec.ProtoCodec` removed.
* The field `OperationMsg.Msg` is now of type `[]byte` instead of `json.RawMessage`.
* (cli) [#16209](https://github.com/cosmos/cosmos-sdk/pull/16209) Add API `StartCmdWithOptions` to create customized start command.
### Client Breaking Changes

View File

@ -20,6 +20,7 @@ import (
"github.com/cometbft/cometbft/proxy"
"github.com/cometbft/cometbft/rpc/client/local"
cmttypes "github.com/cometbft/cometbft/types"
dbm "github.com/cosmos/cosmos-db"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"golang.org/x/sync/errgroup"
@ -88,9 +89,31 @@ const (
FlagMempoolMaxTxs = "mempool.max-txs"
)
// StartCmdOptions defines options that can be customized in `StartCmdWithOptions`,
type StartCmdOptions struct {
// DBOpener can be used to customize db opening, for example customize db options or support different db backends,
// default to the builtin db opener.
DBOpener func(rootDir string, backendType dbm.BackendType) (dbm.DB, error)
// 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
// AddFlags add custom flags to start cmd
AddFlags func(cmd *cobra.Command)
}
// StartCmd runs the service passed in, either stand-alone or in-process with
// CometBFT.
func StartCmd(appCreator types.AppCreator, defaultNodeHome string) *cobra.Command {
return StartCmdWithOptions(appCreator, defaultNodeHome, StartCmdOptions{})
}
// StartCmdWithOptions runs the service passed in, either stand-alone or in-process with
// CometBFT.
func StartCmdWithOptions(appCreator types.AppCreator, defaultNodeHome string, opts StartCmdOptions) *cobra.Command {
if opts.DBOpener == nil {
opts.DBOpener = openDB
}
cmd := &cobra.Command{
Use: "start",
Short: "Run the full node",
@ -145,12 +168,12 @@ is performed. Note, when enabled, gRPC will also be automatically enabled.
serverCtx.Logger.Info("starting ABCI without CometBFT")
return wrapCPUProfile(serverCtx, func() error {
return startStandAlone(serverCtx, appCreator)
return startStandAlone(serverCtx, appCreator, opts)
})
}
return wrapCPUProfile(serverCtx, func() error {
return startInProcess(serverCtx, clientCtx, appCreator)
return startInProcess(serverCtx, clientCtx, appCreator, opts)
})
},
}
@ -200,15 +223,19 @@ is performed. Note, when enabled, gRPC will also be automatically enabled.
// add support for all CometBFT-specific command line options
cmtcmd.AddNodeFlags(cmd)
if opts.AddFlags != nil {
opts.AddFlags(cmd)
}
return cmd
}
func startStandAlone(svrCtx *Context, appCreator types.AppCreator) error {
func startStandAlone(svrCtx *Context, appCreator types.AppCreator, opts StartCmdOptions) error {
addr := svrCtx.Viper.GetString(flagAddress)
transport := svrCtx.Viper.GetString(flagTransport)
home := svrCtx.Viper.GetString(flags.FlagHome)
db, err := openDB(home, GetAppDBBackend(svrCtx.Viper))
db, err := opts.DBOpener(home, GetAppDBBackend(svrCtx.Viper))
if err != nil {
return err
}
@ -267,11 +294,11 @@ func startStandAlone(svrCtx *Context, appCreator types.AppCreator) error {
return g.Wait()
}
func startInProcess(svrCtx *Context, clientCtx client.Context, appCreator types.AppCreator) error {
func startInProcess(svrCtx *Context, clientCtx client.Context, appCreator types.AppCreator, opts StartCmdOptions) error {
cmtCfg := svrCtx.Config
home := cmtCfg.RootDir
db, err := openDB(home, GetAppDBBackend(svrCtx.Viper))
db, err := opts.DBOpener(home, GetAppDBBackend(svrCtx.Viper))
if err != nil {
return err
}
@ -346,6 +373,12 @@ func startInProcess(svrCtx *Context, clientCtx client.Context, appCreator types.
return err
}
if opts.PostSetup != nil {
if err := opts.PostSetup(svrCtx, clientCtx, ctx, g); err != nil {
return err
}
}
// At this point it is safe to block the process if we're in gRPC-only mode as
// we do not need to handle any CometBFT related processes.
if gRPCOnly {