Skip to content

Commit 1b6192f

Browse files
authored
refactor: provide a helper for baseapp options (#14175)
* provide a helper for baseapp options * rename * changelog entry * fix spelling
1 parent 8a2c93f commit 1b6192f

File tree

4 files changed

+54
-49
lines changed

4 files changed

+54
-49
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
9999
* [#13794](https://github.com/cosmos/cosmos-sdk/pull/13794) `types/module.Manager` now supports the
100100
`cosmossdk.io/core/appmodule.AppModule` API via the new `NewManagerFromMap` constructor.
101101
* [#14019](https://github.com/cosmos/cosmos-sdk/issues/14019) Remove the interface casting to allow other implementations of a `CommitMultiStore`.
102+
* [#14175](https://github.com/cosmos/cosmos-sdk/pull/14175) Add `server.DefaultBaseappOptions(appopts)` function to reduce boiler plate in root.go.
102103

103104
### State Machine Breaking
104105

server/mock/app_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package mock
22

33
import (
44
"math/rand"
5+
"testing"
56
"time"
67

78
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
8-
"testing"
99

1010
"github.com/stretchr/testify/require"
1111
abci "github.com/tendermint/tendermint/abci/types"

server/util.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,15 @@ import (
2525
tmlog "github.com/tendermint/tendermint/libs/log"
2626
dbm "github.com/tendermint/tm-db"
2727

28+
"github.com/cosmos/cosmos-sdk/baseapp"
2829
"github.com/cosmos/cosmos-sdk/client/flags"
2930
"github.com/cosmos/cosmos-sdk/server/config"
3031
"github.com/cosmos/cosmos-sdk/server/types"
32+
"github.com/cosmos/cosmos-sdk/store"
33+
"github.com/cosmos/cosmos-sdk/store/snapshots"
34+
snapshottypes "github.com/cosmos/cosmos-sdk/store/snapshots/types"
3135
sdk "github.com/cosmos/cosmos-sdk/types"
36+
"github.com/cosmos/cosmos-sdk/types/mempool"
3237
"github.com/cosmos/cosmos-sdk/version"
3338
)
3439

@@ -406,3 +411,48 @@ func openTraceWriter(traceWriterFile string) (w io.WriteCloser, err error) {
406411
0o666,
407412
)
408413
}
414+
415+
// DefaultBaseappOptions returns the default baseapp options provided by the Cosmos SDK
416+
func DefaultBaseappOptions(appOpts types.AppOptions) []func(*baseapp.BaseApp) {
417+
var cache sdk.MultiStorePersistentCache
418+
419+
if cast.ToBool(appOpts.Get(FlagInterBlockCache)) {
420+
cache = store.NewCommitKVStoreCacheManager()
421+
}
422+
423+
pruningOpts, err := GetPruningOptionsFromFlags(appOpts)
424+
if err != nil {
425+
panic(err)
426+
}
427+
428+
snapshotDir := filepath.Join(cast.ToString(appOpts.Get(flags.FlagHome)), "data", "snapshots")
429+
snapshotDB, err := dbm.NewDB("metadata", GetAppDBBackend(appOpts), snapshotDir)
430+
if err != nil {
431+
panic(err)
432+
}
433+
snapshotStore, err := snapshots.NewStore(snapshotDB, snapshotDir)
434+
if err != nil {
435+
panic(err)
436+
}
437+
438+
snapshotOptions := snapshottypes.NewSnapshotOptions(
439+
cast.ToUint64(appOpts.Get(FlagStateSyncSnapshotInterval)),
440+
cast.ToUint32(appOpts.Get(FlagStateSyncSnapshotKeepRecent)),
441+
)
442+
443+
return []func(*baseapp.BaseApp){
444+
baseapp.SetPruning(pruningOpts),
445+
baseapp.SetMinGasPrices(cast.ToString(appOpts.Get(FlagMinGasPrices))),
446+
baseapp.SetHaltHeight(cast.ToUint64(appOpts.Get(FlagHaltHeight))),
447+
baseapp.SetHaltTime(cast.ToUint64(appOpts.Get(FlagHaltTime))),
448+
baseapp.SetMinRetainBlocks(cast.ToUint64(appOpts.Get(FlagMinRetainBlocks))),
449+
baseapp.SetInterBlockCache(cache),
450+
baseapp.SetTrace(cast.ToBool(appOpts.Get(FlagTrace))),
451+
baseapp.SetIndexEvents(cast.ToStringSlice(appOpts.Get(FlagIndexEvents))),
452+
baseapp.SetSnapshot(snapshotStore, snapshotOptions),
453+
baseapp.SetIAVLCacheSize(cast.ToInt(appOpts.Get(FlagIAVLCacheSize))),
454+
baseapp.SetIAVLDisableFastNode(cast.ToBool(appOpts.Get(FlagDisableIAVLFastNode))),
455+
baseapp.SetMempool(mempool.NewSenderNonceMempool(
456+
mempool.SenderNonceMaxTxOpt(cast.ToInt(appOpts.Get(FlagMempoolMaxTxs))))),
457+
}
458+
}

simapp/simd/cmd/root.go

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ import (
44
"errors"
55
"io"
66
"os"
7-
"path/filepath"
87

98
rosettaCmd "cosmossdk.io/tools/rosetta/cmd"
109

11-
"github.com/spf13/cast"
1210
"github.com/spf13/cobra"
1311
"github.com/spf13/viper"
1412
tmcfg "github.com/tendermint/tendermint/config"
@@ -18,7 +16,6 @@ import (
1816
"cosmossdk.io/simapp"
1917
"cosmossdk.io/simapp/params"
2018

21-
"github.com/cosmos/cosmos-sdk/baseapp"
2219
"github.com/cosmos/cosmos-sdk/client"
2320
"github.com/cosmos/cosmos-sdk/client/config"
2421
"github.com/cosmos/cosmos-sdk/client/debug"
@@ -29,12 +26,8 @@ import (
2926
"github.com/cosmos/cosmos-sdk/server"
3027
serverconfig "github.com/cosmos/cosmos-sdk/server/config"
3128
servertypes "github.com/cosmos/cosmos-sdk/server/types"
32-
"github.com/cosmos/cosmos-sdk/store"
33-
"github.com/cosmos/cosmos-sdk/store/snapshots"
34-
snapshottypes "github.com/cosmos/cosmos-sdk/store/snapshots/types"
3529
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
3630
sdk "github.com/cosmos/cosmos-sdk/types"
37-
"github.com/cosmos/cosmos-sdk/types/mempool"
3831
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
3932
"github.com/cosmos/cosmos-sdk/x/auth/types"
4033
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
@@ -263,52 +256,13 @@ func newApp(
263256
traceStore io.Writer,
264257
appOpts servertypes.AppOptions,
265258
) servertypes.Application {
266-
var cache sdk.MultiStorePersistentCache
267259

268-
if cast.ToBool(appOpts.Get(server.FlagInterBlockCache)) {
269-
cache = store.NewCommitKVStoreCacheManager()
270-
}
271-
272-
skipUpgradeHeights := make(map[int64]bool)
273-
for _, h := range cast.ToIntSlice(appOpts.Get(server.FlagUnsafeSkipUpgrades)) {
274-
skipUpgradeHeights[int64(h)] = true
275-
}
276-
277-
pruningOpts, err := server.GetPruningOptionsFromFlags(appOpts)
278-
if err != nil {
279-
panic(err)
280-
}
281-
282-
snapshotDir := filepath.Join(cast.ToString(appOpts.Get(flags.FlagHome)), "data", "snapshots")
283-
snapshotDB, err := dbm.NewDB("metadata", server.GetAppDBBackend(appOpts), snapshotDir)
284-
if err != nil {
285-
panic(err)
286-
}
287-
snapshotStore, err := snapshots.NewStore(snapshotDB, snapshotDir)
288-
if err != nil {
289-
panic(err)
290-
}
291-
292-
snapshotOptions := snapshottypes.NewSnapshotOptions(
293-
cast.ToUint64(appOpts.Get(server.FlagStateSyncSnapshotInterval)),
294-
cast.ToUint32(appOpts.Get(server.FlagStateSyncSnapshotKeepRecent)),
295-
)
260+
baseappOptions := server.DefaultBaseappOptions(appOpts)
296261

297262
return simapp.NewSimApp(
298263
logger, db, traceStore, true,
299264
appOpts,
300-
baseapp.SetPruning(pruningOpts),
301-
baseapp.SetMinGasPrices(cast.ToString(appOpts.Get(server.FlagMinGasPrices))),
302-
baseapp.SetHaltHeight(cast.ToUint64(appOpts.Get(server.FlagHaltHeight))),
303-
baseapp.SetHaltTime(cast.ToUint64(appOpts.Get(server.FlagHaltTime))),
304-
baseapp.SetMinRetainBlocks(cast.ToUint64(appOpts.Get(server.FlagMinRetainBlocks))),
305-
baseapp.SetInterBlockCache(cache),
306-
baseapp.SetTrace(cast.ToBool(appOpts.Get(server.FlagTrace))),
307-
baseapp.SetIndexEvents(cast.ToStringSlice(appOpts.Get(server.FlagIndexEvents))),
308-
baseapp.SetSnapshot(snapshotStore, snapshotOptions),
309-
baseapp.SetIAVLCacheSize(cast.ToInt(appOpts.Get(server.FlagIAVLCacheSize))),
310-
baseapp.SetIAVLDisableFastNode(cast.ToBool(appOpts.Get(server.FlagDisableIAVLFastNode))),
311-
baseapp.SetMempool(mempool.NewSenderNonceMempool(mempool.SenderNonceMaxTxOpt(cast.ToInt(appOpts.Get(server.FlagMempoolMaxTxs))))),
265+
baseappOptions...,
312266
)
313267
}
314268

0 commit comments

Comments
 (0)