From 1b6192fec0ed1b13f1714be97a77ac79a75d6ee1 Mon Sep 17 00:00:00 2001 From: Marko Date: Tue, 6 Dec 2022 14:28:18 +0000 Subject: [PATCH] refactor: provide a helper for baseapp options (#14175) * provide a helper for baseapp options * rename * changelog entry * fix spelling --- CHANGELOG.md | 1 + server/mock/app_test.go | 2 +- server/util.go | 50 +++++++++++++++++++++++++++++++++++++++++ simapp/simd/cmd/root.go | 50 ++--------------------------------------- 4 files changed, 54 insertions(+), 49 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 925c39afb996..cb9ea9cb8a8b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -99,6 +99,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [#13794](https://github.com/cosmos/cosmos-sdk/pull/13794) `types/module.Manager` now supports the `cosmossdk.io/core/appmodule.AppModule` API via the new `NewManagerFromMap` constructor. * [#14019](https://github.com/cosmos/cosmos-sdk/issues/14019) Remove the interface casting to allow other implementations of a `CommitMultiStore`. +* [#14175](https://github.com/cosmos/cosmos-sdk/pull/14175) Add `server.DefaultBaseappOptions(appopts)` function to reduce boiler plate in root.go. ### State Machine Breaking diff --git a/server/mock/app_test.go b/server/mock/app_test.go index 6ae13632d8ea..5e21cdd58393 100644 --- a/server/mock/app_test.go +++ b/server/mock/app_test.go @@ -2,10 +2,10 @@ package mock import ( "math/rand" + "testing" "time" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" - "testing" "github.com/stretchr/testify/require" abci "github.com/tendermint/tendermint/abci/types" diff --git a/server/util.go b/server/util.go index 56db69f289cf..ce253c0d9c0c 100644 --- a/server/util.go +++ b/server/util.go @@ -25,10 +25,15 @@ import ( tmlog "github.com/tendermint/tendermint/libs/log" dbm "github.com/tendermint/tm-db" + "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/server/config" "github.com/cosmos/cosmos-sdk/server/types" + "github.com/cosmos/cosmos-sdk/store" + "github.com/cosmos/cosmos-sdk/store/snapshots" + snapshottypes "github.com/cosmos/cosmos-sdk/store/snapshots/types" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/mempool" "github.com/cosmos/cosmos-sdk/version" ) @@ -406,3 +411,48 @@ func openTraceWriter(traceWriterFile string) (w io.WriteCloser, err error) { 0o666, ) } + +// DefaultBaseappOptions returns the default baseapp options provided by the Cosmos SDK +func DefaultBaseappOptions(appOpts types.AppOptions) []func(*baseapp.BaseApp) { + var cache sdk.MultiStorePersistentCache + + if cast.ToBool(appOpts.Get(FlagInterBlockCache)) { + cache = store.NewCommitKVStoreCacheManager() + } + + pruningOpts, err := GetPruningOptionsFromFlags(appOpts) + if err != nil { + panic(err) + } + + snapshotDir := filepath.Join(cast.ToString(appOpts.Get(flags.FlagHome)), "data", "snapshots") + snapshotDB, err := dbm.NewDB("metadata", GetAppDBBackend(appOpts), snapshotDir) + if err != nil { + panic(err) + } + snapshotStore, err := snapshots.NewStore(snapshotDB, snapshotDir) + if err != nil { + panic(err) + } + + snapshotOptions := snapshottypes.NewSnapshotOptions( + cast.ToUint64(appOpts.Get(FlagStateSyncSnapshotInterval)), + cast.ToUint32(appOpts.Get(FlagStateSyncSnapshotKeepRecent)), + ) + + return []func(*baseapp.BaseApp){ + baseapp.SetPruning(pruningOpts), + baseapp.SetMinGasPrices(cast.ToString(appOpts.Get(FlagMinGasPrices))), + baseapp.SetHaltHeight(cast.ToUint64(appOpts.Get(FlagHaltHeight))), + baseapp.SetHaltTime(cast.ToUint64(appOpts.Get(FlagHaltTime))), + baseapp.SetMinRetainBlocks(cast.ToUint64(appOpts.Get(FlagMinRetainBlocks))), + baseapp.SetInterBlockCache(cache), + baseapp.SetTrace(cast.ToBool(appOpts.Get(FlagTrace))), + baseapp.SetIndexEvents(cast.ToStringSlice(appOpts.Get(FlagIndexEvents))), + baseapp.SetSnapshot(snapshotStore, snapshotOptions), + baseapp.SetIAVLCacheSize(cast.ToInt(appOpts.Get(FlagIAVLCacheSize))), + baseapp.SetIAVLDisableFastNode(cast.ToBool(appOpts.Get(FlagDisableIAVLFastNode))), + baseapp.SetMempool(mempool.NewSenderNonceMempool( + mempool.SenderNonceMaxTxOpt(cast.ToInt(appOpts.Get(FlagMempoolMaxTxs))))), + } +} diff --git a/simapp/simd/cmd/root.go b/simapp/simd/cmd/root.go index 859c53dba360..eafc26cd2ffb 100644 --- a/simapp/simd/cmd/root.go +++ b/simapp/simd/cmd/root.go @@ -4,11 +4,9 @@ import ( "errors" "io" "os" - "path/filepath" rosettaCmd "cosmossdk.io/tools/rosetta/cmd" - "github.com/spf13/cast" "github.com/spf13/cobra" "github.com/spf13/viper" tmcfg "github.com/tendermint/tendermint/config" @@ -18,7 +16,6 @@ import ( "cosmossdk.io/simapp" "cosmossdk.io/simapp/params" - "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/config" "github.com/cosmos/cosmos-sdk/client/debug" @@ -29,12 +26,8 @@ import ( "github.com/cosmos/cosmos-sdk/server" serverconfig "github.com/cosmos/cosmos-sdk/server/config" servertypes "github.com/cosmos/cosmos-sdk/server/types" - "github.com/cosmos/cosmos-sdk/store" - "github.com/cosmos/cosmos-sdk/store/snapshots" - snapshottypes "github.com/cosmos/cosmos-sdk/store/snapshots/types" simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/mempool" authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" "github.com/cosmos/cosmos-sdk/x/auth/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" @@ -263,52 +256,13 @@ func newApp( traceStore io.Writer, appOpts servertypes.AppOptions, ) servertypes.Application { - var cache sdk.MultiStorePersistentCache - if cast.ToBool(appOpts.Get(server.FlagInterBlockCache)) { - cache = store.NewCommitKVStoreCacheManager() - } - - skipUpgradeHeights := make(map[int64]bool) - for _, h := range cast.ToIntSlice(appOpts.Get(server.FlagUnsafeSkipUpgrades)) { - skipUpgradeHeights[int64(h)] = true - } - - pruningOpts, err := server.GetPruningOptionsFromFlags(appOpts) - if err != nil { - panic(err) - } - - snapshotDir := filepath.Join(cast.ToString(appOpts.Get(flags.FlagHome)), "data", "snapshots") - snapshotDB, err := dbm.NewDB("metadata", server.GetAppDBBackend(appOpts), snapshotDir) - if err != nil { - panic(err) - } - snapshotStore, err := snapshots.NewStore(snapshotDB, snapshotDir) - if err != nil { - panic(err) - } - - snapshotOptions := snapshottypes.NewSnapshotOptions( - cast.ToUint64(appOpts.Get(server.FlagStateSyncSnapshotInterval)), - cast.ToUint32(appOpts.Get(server.FlagStateSyncSnapshotKeepRecent)), - ) + baseappOptions := server.DefaultBaseappOptions(appOpts) return simapp.NewSimApp( logger, db, traceStore, true, appOpts, - baseapp.SetPruning(pruningOpts), - baseapp.SetMinGasPrices(cast.ToString(appOpts.Get(server.FlagMinGasPrices))), - baseapp.SetHaltHeight(cast.ToUint64(appOpts.Get(server.FlagHaltHeight))), - baseapp.SetHaltTime(cast.ToUint64(appOpts.Get(server.FlagHaltTime))), - baseapp.SetMinRetainBlocks(cast.ToUint64(appOpts.Get(server.FlagMinRetainBlocks))), - baseapp.SetInterBlockCache(cache), - baseapp.SetTrace(cast.ToBool(appOpts.Get(server.FlagTrace))), - baseapp.SetIndexEvents(cast.ToStringSlice(appOpts.Get(server.FlagIndexEvents))), - baseapp.SetSnapshot(snapshotStore, snapshotOptions), - baseapp.SetIAVLCacheSize(cast.ToInt(appOpts.Get(server.FlagIAVLCacheSize))), - baseapp.SetIAVLDisableFastNode(cast.ToBool(appOpts.Get(server.FlagDisableIAVLFastNode))), - baseapp.SetMempool(mempool.NewSenderNonceMempool(mempool.SenderNonceMaxTxOpt(cast.ToInt(appOpts.Get(server.FlagMempoolMaxTxs))))), + baseappOptions..., ) }