From f1337aa378e70f5ff9c4c4e676450505efbfa682 Mon Sep 17 00:00:00 2001 From: yihuang Date: Fri, 20 Jan 2023 23:53:53 +0800 Subject: [PATCH] support customize db opener in `StartCmd` (#1615) * support customize db opener in `StartCmd` * Update CHANGELOG.md * pass to dbOpener, so we can control different options through cli flags * add comments --- CHANGELOG.md | 4 ++++ cmd/ethermintd/root.go | 2 +- server/start.go | 41 ++++++++++++++++++++++++++++++----------- server/util.go | 9 ++++----- 4 files changed, 39 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c3911714d..97196f4efb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,10 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (rpc) [#1613](https://github.com/evmos/ethermint/pull/1613) Change the default json-rpc listen address to localhost. * (rpc) [#1611](https://github.com/evmos/ethermint/pull/1611) Add missing next fee in fee history, fix wrong oldestBlock and align earliest input as ethereum. +### Improvements + +* (cli) [#1615](https://github.com/evmos/ethermint/pull/1615) Support customize db opener in `StartCmd`. + ## [v0.21.0-rc1] - 2022-1-13 ### State Machine Breaking diff --git a/cmd/ethermintd/root.go b/cmd/ethermintd/root.go index 18478c06a4..5474ac57d2 100644 --- a/cmd/ethermintd/root.go +++ b/cmd/ethermintd/root.go @@ -130,7 +130,7 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) { ) a := appCreator{encodingConfig} - server.AddCommands(rootCmd, app.DefaultNodeHome, a.newApp, a.appExport, addModuleInitFlags) + server.AddCommands(rootCmd, server.NewDefaultStartOptions(a.newApp, app.DefaultNodeHome), a.appExport, addModuleInitFlags) // add keybase, auxiliary RPC, query, and tx child commands rootCmd.AddCommand( diff --git a/server/start.go b/server/start.go index 96c03d9632..6236dd9c7a 100644 --- a/server/start.go +++ b/server/start.go @@ -68,9 +68,28 @@ import ( ethermint "github.com/evmos/ethermint/types" ) +// DBOpener is a function to open `application.db`, potentially with customized options. +type DBOpener func(opts types.AppOptions, rootDir string, backend dbm.BackendType) (dbm.DB, error) + +// StartOptions defines options that can be customized in `StartCmd` +type StartOptions struct { + AppCreator types.AppCreator + DefaultNodeHome string + DBOpener DBOpener +} + +// NewDefaultStartOptions use the default db opener provided in tm-db. +func NewDefaultStartOptions(appCreator types.AppCreator, defaultNodeHome string) StartOptions { + return StartOptions{ + AppCreator: appCreator, + DefaultNodeHome: defaultNodeHome, + DBOpener: openDB, + } +} + // StartCmd runs the service passed in, either stand-alone or in-process with // Tendermint. -func StartCmd(appCreator types.AppCreator, defaultNodeHome string) *cobra.Command { +func StartCmd(opts StartOptions) *cobra.Command { cmd := &cobra.Command{ Use: "start", Short: "Run the full node", @@ -119,7 +138,7 @@ which accepts a path for the resulting pprof file. withTM, _ := cmd.Flags().GetBool(srvflags.WithTendermint) if !withTM { serverCtx.Logger.Info("starting ABCI without Tendermint") - return startStandAlone(serverCtx, appCreator) + return startStandAlone(serverCtx, opts) } serverCtx.Logger.Info("Unlocking keyring") @@ -136,7 +155,7 @@ which accepts a path for the resulting pprof file. serverCtx.Logger.Info("starting ABCI with Tendermint") // amino is needed here for backwards compatibility of REST routes - err = startInProcess(serverCtx, clientCtx, appCreator) + err = startInProcess(serverCtx, clientCtx, opts) errCode, ok := err.(server.ErrorCode) if !ok { return err @@ -147,7 +166,7 @@ which accepts a path for the resulting pprof file. }, } - cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory") + cmd.Flags().String(flags.FlagHome, opts.DefaultNodeHome, "The application home directory") cmd.Flags().Bool(srvflags.WithTendermint, true, "Run abci app embedded in-process with tendermint") cmd.Flags().String(srvflags.Address, "tcp://0.0.0.0:26658", "Listen address") cmd.Flags().String(srvflags.Transport, "socket", "Transport protocol: socket, grpc") @@ -206,12 +225,12 @@ which accepts a path for the resulting pprof file. return cmd } -func startStandAlone(ctx *server.Context, appCreator types.AppCreator) error { +func startStandAlone(ctx *server.Context, opts StartOptions) error { addr := ctx.Viper.GetString(srvflags.Address) transport := ctx.Viper.GetString(srvflags.Transport) home := ctx.Viper.GetString(flags.FlagHome) - db, err := openDB(home, server.GetAppDBBackend(ctx.Viper)) + db, err := opts.DBOpener(ctx.Viper, home, server.GetAppDBBackend(ctx.Viper)) if err != nil { return err } @@ -228,7 +247,7 @@ func startStandAlone(ctx *server.Context, appCreator types.AppCreator) error { return err } - app := appCreator(ctx.Logger, db, traceWriter, ctx.Viper) + app := opts.AppCreator(ctx.Logger, db, traceWriter, ctx.Viper) config, err := config.GetConfig(ctx.Viper) if err != nil { @@ -269,7 +288,7 @@ func startStandAlone(ctx *server.Context, appCreator types.AppCreator) error { } // legacyAminoCdc is used for the legacy REST API -func startInProcess(ctx *server.Context, clientCtx client.Context, appCreator types.AppCreator) (err error) { +func startInProcess(ctx *server.Context, clientCtx client.Context, opts StartOptions) (err error) { cfg := ctx.Config home := cfg.RootDir logger := ctx.Logger @@ -300,7 +319,7 @@ func startInProcess(ctx *server.Context, clientCtx client.Context, appCreator ty }() } - db, err := openDB(home, server.GetAppDBBackend(ctx.Viper)) + db, err := opts.DBOpener(ctx.Viper, home, server.GetAppDBBackend(ctx.Viper)) if err != nil { logger.Error("failed to open DB", "error", err.Error()) return err @@ -330,7 +349,7 @@ func startInProcess(ctx *server.Context, clientCtx client.Context, appCreator ty return err } - app := appCreator(ctx.Logger, db, traceWriter, ctx.Viper) + app := opts.AppCreator(ctx.Logger, db, traceWriter, ctx.Viper) nodeKey, err := p2p.LoadOrGenNodeKey(cfg.NodeKeyFile()) if err != nil { @@ -625,7 +644,7 @@ func startInProcess(ctx *server.Context, clientCtx client.Context, appCreator ty return server.WaitForQuitSignals() } -func openDB(rootDir string, backendType dbm.BackendType) (dbm.DB, error) { +func openDB(_ types.AppOptions, rootDir string, backendType dbm.BackendType) (dbm.DB, error) { dataDir := filepath.Join(rootDir, "data") return dbm.NewDB("application", backendType, dataDir) } diff --git a/server/util.go b/server/util.go index fa3a19e5b4..6657f9900a 100644 --- a/server/util.go +++ b/server/util.go @@ -38,8 +38,7 @@ import ( // AddCommands adds server commands func AddCommands( rootCmd *cobra.Command, - defaultNodeHome string, - appCreator types.AppCreator, + opts StartOptions, appExport types.AppExporter, addStartFlags types.ModuleInitFlags, ) { @@ -57,15 +56,15 @@ func AddCommands( tmcmd.ResetStateCmd, ) - startCmd := StartCmd(appCreator, defaultNodeHome) + startCmd := StartCmd(opts) addStartFlags(startCmd) rootCmd.AddCommand( startCmd, tendermintCmd, - sdkserver.ExportCmd(appExport, defaultNodeHome), + sdkserver.ExportCmd(appExport, opts.DefaultNodeHome), version.NewVersionCommand(), - sdkserver.NewRollbackCmd(appCreator, defaultNodeHome), + sdkserver.NewRollbackCmd(opts.AppCreator, opts.DefaultNodeHome), // custom tx indexer command NewIndexTxCmd(),