Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
support customize db opener in StartCmd (backport #1615) (#1622)
Browse files Browse the repository at this point in the history
* 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

(cherry picked from commit f1337aa)

# Conflicts:
#	CHANGELOG.md

* conflicts

Co-authored-by: yihuang <[email protected]>
Co-authored-by: Federico Kunze Küllmer <[email protected]>
  • Loading branch information
3 people authored Jan 20, 2023
1 parent 949674e commit 64e80be
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
8 changes: 4 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ Ref: https://keepachangelog.com/en/1.0.0/

* (feemarket) [#1509](https://github.com/evmos/ethermint/pull/1509) Deprecate usage of x/params in x/feemarket
* (evm) [#1472](https://github.com/evmos/ethermint/pull/1472) Deprecate x/params usage in x/evm
* (deps) [#1575](https://github.com/evmos/ethermint/pull/1575) bump ibc-go to [`v6.1.0`]
* (deps) [#1168](https://github.com/evmos/ethermint/pull/1168) Upgrade Cosmos SDK to [`v0.46.6`]
* (deps) [#1575](https://github.com/evmos/ethermint/pull/1575) bump ibc-go to [`v6.1.0`](https://github.com/cosmos/ibc-go/releases/tag/v6.1.0)

### Improvements

* (evm) [#1582](https://github.com/evmos/ethermint/pull/1582) cleanup `evm` files
* (evm) [#1544](https://github.com/evmos/ethermint/pull/1544) Migrate deprecated event emitting to new TypedEvent
* (cli) [#1615](https://github.com/evmos/ethermint/pull/1615) Support customize db opener in `StartCmd`.
* (evm) [#1582](https://github.com/evmos/ethermint/pull/1582) Cleanup `evm` files
* (evm) [#1544](https://github.com/evmos/ethermint/pull/1544) Migrate deprecated event emitting to new `TypedEvent`
* (deps) [#1532](https://github.com/evmos/ethermint/pull/1532) Upgrade Go-Ethereum version to [`v1.10.26`](https://github.com/ethereum/go-ethereum/releases/tag/v1.10.26).

### Bug Fixes
Expand Down
2 changes: 1 addition & 1 deletion cmd/ethermintd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
41 changes: 30 additions & 11 deletions server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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")
Expand All @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
9 changes: 4 additions & 5 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
Expand All @@ -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(),
Expand Down

0 comments on commit 64e80be

Please sign in to comment.