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

This commit is contained in:
Julien Robert
2024-03-27 07:12:49 +00:00
committed by GitHub
parent 815c9c5652
commit def211d868
9 changed files with 41 additions and 36 deletions
+16 -15
View File
@@ -124,6 +124,8 @@ type StartCmdOptions[T types.Application] struct {
PostSetupStandalone func(app T, 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[T], inProcessConsensus bool, opts StartCmdOptions[T]) error
}
// StartCmd runs the service passed in, either stand-alone or in-process with
@@ -139,6 +141,10 @@ func StartCmdWithOptions[T types.Application](appCreator types.AppCreator[T], op
opts.DBOpener = OpenDB
}
if opts.StartCommandHandler == nil {
opts.StartCommandHandler = start
}
cmd := &cobra.Command{
Use: "start",
Short: "Run the full node",
@@ -187,7 +193,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")
@@ -270,10 +276,7 @@ func startStandAlone[T types.Application](svrCtx *Context, svrCfg serverconfig.C
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
}
@@ -304,8 +307,6 @@ func startInProcess[T types.Application](svrCtx *Context, svrCfg serverconfig.Co
metrics *telemetry.Metrics, opts StartCmdOptions[T],
) error {
cmtCfg := svrCtx.Config
home := cmtCfg.RootDir
gRPCOnly := svrCtx.Viper.GetBool(flagGRPCOnly)
g, ctx := getCtx(svrCtx, true)
@@ -341,7 +342,7 @@ func startInProcess[T types.Application](svrCtx *Context, svrCfg serverconfig.Co
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
}
@@ -504,7 +505,6 @@ func startGrpcServer(
func startAPIServer(
ctx context.Context,
g *errgroup.Group,
cmtCfg *cmtcfg.Config,
svrCfg serverconfig.Config,
clientCtx client.Context,
svrCtx *Context,
@@ -612,7 +612,7 @@ func startApp[T types.Application](svrCtx *Context, appCreator types.AppCreator[
if isTestnet, ok := svrCtx.Viper.Get(KeyIsTestnet).(bool); ok && isTestnet {
var appPtr *T
appPtr, err = testnetify[T](svrCtx, home, appCreator, db, traceWriter)
appPtr, err = testnetify[T](svrCtx, appCreator, db, traceWriter)
if err != nil {
return app, traceCleanupFn, err
}
@@ -639,6 +639,10 @@ func InPlaceTestnetCreator[T types.Application](testnetAppCreator types.AppCreat
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",
@@ -703,7 +707,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")
@@ -726,7 +730,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[T types.Application](ctx *Context, home string, testnetAppCreator types.AppCreator[T], db dbm.DB, traceWriter io.WriteCloser) (*T, error) {
func testnetify[T types.Application](ctx *Context, testnetAppCreator types.AppCreator[T], db dbm.DB, traceWriter io.WriteCloser) (*T, error) {
config := ctx.Config
newChainID, ok := ctx.Viper.Get(KeyNewChainID).(string)
@@ -772,9 +776,6 @@ func testnetify[T types.Application](ctx *Context, home string, testnetAppCreato
return nil, err
}
validatorAddress := userPubKey.Address()
if err != nil {
return nil, err
}
stateStore := sm.NewStore(stateDB, sm.StoreOptions{
DiscardABCIResponses: config.Storage.DiscardABCIResponses,
-4
View File
@@ -8,7 +8,6 @@ import (
cmttypes "github.com/cometbft/cometbft/types"
dbm "github.com/cosmos/cosmos-db"
"github.com/cosmos/gogoproto/grpc"
"github.com/spf13/cobra"
"cosmossdk.io/log"
"cosmossdk.io/store/snapshots"
@@ -68,9 +67,6 @@ type (
// application using various configurations.
AppCreator[T Application] func(log.Logger, dbm.DB, io.Writer, AppOptions) T
// ModuleInitFlags takes a start command and adds modules specific init flags.
ModuleInitFlags func(startCmd *cobra.Command)
// ExportedApp represents an exported app state, along with
// validators, consensus params and latest app height.
ExportedApp struct {
+9 -8
View File
@@ -324,7 +324,7 @@ func interceptConfigs(rootViper *viper.Viper, customAppTemplate string, customCo
}
// AddCommands add server commands
func AddCommands[T types.Application](rootCmd *cobra.Command, appCreator types.AppCreator[T], addStartFlags types.ModuleInitFlags) {
func AddCommands[T types.Application](rootCmd *cobra.Command, appCreator types.AppCreator[T], opts StartCmdOptions[T]) {
cometCmd := &cobra.Command{
Use: "comet",
Aliases: []string{"cometbft", "tendermint"},
@@ -341,11 +341,7 @@ func AddCommands[T types.Application](rootCmd *cobra.Command, appCreator types.A
BootstrapStateCmd(appCreator),
)
startCmd := StartCmd(appCreator)
if addStartFlags != nil {
addStartFlags(startCmd)
}
startCmd := StartCmdWithOptions(appCreator, opts)
rootCmd.AddCommand(
startCmd,
cometCmd,
@@ -354,10 +350,15 @@ func AddCommands[T types.Application](rootCmd *cobra.Command, appCreator types.A
)
}
// AddCommandsWithStartCmdOptions adds server commands with the provided StartCmdOptions.
// Deprecated: Use AddCommands directly instead.
func AddCommandsWithStartCmdOptions[T types.Application](rootCmd *cobra.Command, appCreator types.AppCreator[T], opts StartCmdOptions[T]) {
AddCommands(rootCmd, appCreator, opts)
}
// AddTestnetCreatorCommand allows chains to create a testnet from the state existing in their node's data directory.
func AddTestnetCreatorCommand[T types.Application](rootCmd *cobra.Command, appCreator types.AppCreator[T], addStartFlags types.ModuleInitFlags) {
func AddTestnetCreatorCommand[T types.Application](rootCmd *cobra.Command, appCreator types.AppCreator[T]) {
testnetCreateCmd := InPlaceTestnetCreator(appCreator)
addStartFlags(testnetCreateCmd)
rootCmd.AddCommand(testnetCreateCmd)
}