From 09c2525aaa8637bde5f2dd3b113930f61a4b0d99 Mon Sep 17 00:00:00 2001 From: HuangYi Date: Wed, 18 Jan 2023 23:38:50 +0800 Subject: [PATCH 1/4] support customize db opener in `StartCmd` --- CHANGELOG.md | 4 ++++ cmd/ethermintd/root.go | 2 +- server/start.go | 33 +++++++++++++++++++++++++-------- server/util.go | 9 ++++----- 4 files changed, 34 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d09c67f4c..676c5ae366 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,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. +### Improvements + +* (cli) [#]() 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..32fdc6b218 100644 --- a/server/start.go +++ b/server/start.go @@ -68,9 +68,26 @@ import ( ethermint "github.com/evmos/ethermint/types" ) +type DBOpener func(rootDir string, backendType dbm.BackendType) (dbm.DB, error) + +// StartOptions defines options that can be customized in `StartCmd` +type StartOptions struct { + AppCreator types.AppCreator + DefaultNodeHome string + DBOpener DBOpener +} + +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 +136,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.AppCreator) } serverCtx.Logger.Info("Unlocking keyring") @@ -136,7 +153,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 +164,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") @@ -211,7 +228,7 @@ func startStandAlone(ctx *server.Context, appCreator types.AppCreator) error { transport := ctx.Viper.GetString(srvflags.Transport) home := ctx.Viper.GetString(flags.FlagHome) - db, err := openDB(home, server.GetAppDBBackend(ctx.Viper)) + db, err := opts.DBOpener(home, server.GetAppDBBackend(ctx.Viper)) if err != nil { return err } @@ -269,7 +286,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 +317,7 @@ func startInProcess(ctx *server.Context, clientCtx client.Context, appCreator ty }() } - db, err := openDB(home, server.GetAppDBBackend(ctx.Viper)) + db, err := opts.DBOpener(home, server.GetAppDBBackend(ctx.Viper)) if err != nil { logger.Error("failed to open DB", "error", err.Error()) return err @@ -330,7 +347,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 { 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(), From 2b7890bc637b934963434352e2e1361238d690c8 Mon Sep 17 00:00:00 2001 From: yihuang Date: Wed, 18 Jan 2023 23:42:24 +0800 Subject: [PATCH 2/4] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 676c5ae366..3a64ce06b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,7 +44,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements -* (cli) [#]() Support customize db opener in `StartCmd`. +* (cli) [#1615](https://github.com/evmos/ethermint/pull/1615) Support customize db opener in `StartCmd`. ## [v0.21.0-rc1] - 2022-1-13 From 83466fe42420a95f1be007505de1ee8bc5bc39b4 Mon Sep 17 00:00:00 2001 From: HuangYi Date: Fri, 20 Jan 2023 09:07:40 +0800 Subject: [PATCH 3/4] pass to dbOpener, so we can control different options through cli flags --- server/start.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/server/start.go b/server/start.go index 32fdc6b218..2c60a26754 100644 --- a/server/start.go +++ b/server/start.go @@ -68,7 +68,7 @@ import ( ethermint "github.com/evmos/ethermint/types" ) -type DBOpener func(rootDir string, backendType dbm.BackendType) (dbm.DB, error) +type DBOpener func(types.AppOptions, string, dbm.BackendType) (dbm.DB, error) // StartOptions defines options that can be customized in `StartCmd` type StartOptions struct { @@ -136,7 +136,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, opts.AppCreator) + return startStandAlone(serverCtx, opts) } serverCtx.Logger.Info("Unlocking keyring") @@ -223,12 +223,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 := opts.DBOpener(home, server.GetAppDBBackend(ctx.Viper)) + db, err := opts.DBOpener(ctx.Viper, home, server.GetAppDBBackend(ctx.Viper)) if err != nil { return err } @@ -245,7 +245,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 { @@ -317,7 +317,7 @@ func startInProcess(ctx *server.Context, clientCtx client.Context, opts StartOpt }() } - db, err := opts.DBOpener(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 @@ -642,7 +642,7 @@ func startInProcess(ctx *server.Context, clientCtx client.Context, opts StartOpt 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) } From a3c398ac17dde906129e7179f8b8f3ce7edb8908 Mon Sep 17 00:00:00 2001 From: HuangYi Date: Fri, 20 Jan 2023 22:31:50 +0800 Subject: [PATCH 4/4] add comments --- server/start.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/server/start.go b/server/start.go index 2c60a26754..6236dd9c7a 100644 --- a/server/start.go +++ b/server/start.go @@ -68,7 +68,8 @@ import ( ethermint "github.com/evmos/ethermint/types" ) -type DBOpener func(types.AppOptions, string, dbm.BackendType) (dbm.DB, error) +// 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 { @@ -77,6 +78,7 @@ type StartOptions struct { DBOpener DBOpener } +// NewDefaultStartOptions use the default db opener provided in tm-db. func NewDefaultStartOptions(appCreator types.AppCreator, defaultNodeHome string) StartOptions { return StartOptions{ AppCreator: appCreator,