Skip to content

Commit

Permalink
[1760]: Remove some args from app.New that should already be provided…
Browse files Browse the repository at this point in the history
… in the app options.
  • Loading branch information
SpicyLemon committed May 30, 2024
1 parent b4e0782 commit d0b5422
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 114 deletions.
20 changes: 14 additions & 6 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ import (
dbm "github.com/cosmos/cosmos-db"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/grpc/cmtservice"
nodeservice "github.com/cosmos/cosmos-sdk/client/grpc/node"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/runtime"
runtimeservices "github.com/cosmos/cosmos-sdk/runtime/services"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/server/api"
serverconfig "github.com/cosmos/cosmos-sdk/server/config"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
Expand Down Expand Up @@ -238,8 +240,6 @@ type App struct {
interfaceRegistry types.InterfaceRegistry
txConfig client.TxConfig

invCheckPeriod uint

// keys to access the substores
keys map[string]*storetypes.KVStoreKey
tkeys map[string]*storetypes.TransientStoreKey
Expand Down Expand Up @@ -326,8 +326,7 @@ func init() {

// New returns a reference to an initialized Provenance Blockchain App.
func New(
logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest bool, skipUpgradeHeights map[int64]bool,
homePath string, invCheckPeriod uint,
logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest bool,
appOpts servertypes.AppOptions, baseAppOptions ...func(*baseapp.BaseApp),
) *App {
signingOptions := signing.Options{
Expand Down Expand Up @@ -395,7 +394,6 @@ func New(
legacyAmino: legacyAmino,
appCodec: appCodec,
interfaceRegistry: interfaceRegistry,
invCheckPeriod: invCheckPeriod,
keys: keys,
tkeys: tkeys,
memKeys: memKeys,
Expand Down Expand Up @@ -476,11 +474,21 @@ func New(
appCodec, legacyAmino, runtime.NewKVStoreService(keys[slashingtypes.StoreKey]), app.StakingKeeper, govAuthority,
)

app.CrisisKeeper = crisiskeeper.NewKeeper(appCodec, runtime.NewKVStoreService(keys[crisistypes.StoreKey]), app.invCheckPeriod,
invCheckPeriod := cast.ToUint(appOpts.Get(server.FlagInvCheckPeriod))
app.CrisisKeeper = crisiskeeper.NewKeeper(appCodec, runtime.NewKVStoreService(keys[crisistypes.StoreKey]), invCheckPeriod,
app.BankKeeper, authtypes.FeeCollectorName, govAuthority, app.AccountKeeper.AddressCodec())

app.FeeGrantKeeper = feegrantkeeper.NewKeeper(appCodec, runtime.NewKVStoreService(keys[feegrant.StoreKey]), app.AccountKeeper)

// get skipUpgradeHeights from the app options
skipUpgradeHeights := make(map[int64]bool)
for _, h := range cast.ToIntSlice(appOpts.Get(server.FlagUnsafeSkipUpgrades)) {
skipUpgradeHeights[int64(h)] = true
}
homePath := cast.ToString(appOpts.Get(flags.FlagHome))
if len(homePath) == 0 {
homePath = DefaultNodeHome
}
app.UpgradeKeeper = upgradekeeper.NewKeeper(skipUpgradeHeights, runtime.NewKVStoreService(keys[upgradetypes.StoreKey]), appCodec, homePath, app.BaseApp, govAuthority)

app.MsgFeesKeeper = msgfeeskeeper.NewKeeper(
Expand Down
21 changes: 7 additions & 14 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,9 @@ import (

func TestSimAppExportAndBlockedAddrs(t *testing.T) {
opts := SetupOptions{
Logger: log.NewTestLogger(t),
DB: dbm.NewMemDB(),
InvCheckPeriod: 0,
HomePath: t.TempDir(),
SkipUpgradeHeights: map[int64]bool{},
AppOpts: simtestutil.EmptyAppOptions{},
Logger: log.NewTestLogger(t),
DB: dbm.NewMemDB(),
AppOpts: simtestutil.NewAppOptionsWithFlagHome(t.TempDir()),
}
app := NewAppWithCustomOptions(t, false, opts)

Expand All @@ -54,8 +51,7 @@ func TestSimAppExportAndBlockedAddrs(t *testing.T) {
app.Commit()

// Making a new app object with the db, so that initchain hasn't been called
app2 := New(log.NewTestLogger(t), opts.DB, nil, true,
map[int64]bool{}, opts.HomePath, 0, simtestutil.EmptyAppOptions{})
app2 := New(log.NewTestLogger(t), opts.DB, nil, true, opts.AppOpts)
require.NotPanics(t, func() {
_, err = app2.ExportAppStateAndValidators(false, nil, nil)
}, "exporting app state at current height")
Expand All @@ -74,12 +70,9 @@ func TestGetMaccPerms(t *testing.T) {

func TestExportAppStateAndValidators(t *testing.T) {
opts := SetupOptions{
Logger: log.NewTestLogger(t),
DB: dbm.NewMemDB(),
InvCheckPeriod: 0,
HomePath: t.TempDir(),
SkipUpgradeHeights: map[int64]bool{},
AppOpts: simtestutil.EmptyAppOptions{},
Logger: log.NewTestLogger(t),
DB: dbm.NewMemDB(),
AppOpts: simtestutil.NewAppOptionsWithFlagHome(t.TempDir()),
}
app := NewAppWithCustomOptions(t, false, opts)
ctx := app.BaseApp.NewContext(false)
Expand Down
5 changes: 2 additions & 3 deletions app/sim_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
simcli "github.com/cosmos/cosmos-sdk/x/simulation/client/cli"
)

// Profile with:
Expand All @@ -37,7 +36,7 @@ func BenchmarkFullAppSimulation(b *testing.B) {
}
}()

app := New(logger, db, nil, true, map[int64]bool{}, b.TempDir(), simcli.FlagPeriodValue, simtestutil.EmptyAppOptions{}, interBlockCacheOpt())
app := New(logger, db, nil, true, newSimAppOpts(b), interBlockCacheOpt())

// run randomized simulation
_, simParams, simErr := simulation.SimulateFromSeed(
Expand Down Expand Up @@ -86,7 +85,7 @@ func BenchmarkInvariants(b *testing.B) {
}
}()

app := New(logger, db, nil, true, map[int64]bool{}, b.TempDir(), simcli.FlagPeriodValue, simtestutil.EmptyAppOptions{}, interBlockCacheOpt())
app := New(logger, db, nil, true, newSimAppOpts(b), interBlockCacheOpt())

// run randomized simulation
_, lastBlockTime, simParams, simErr := simulation.SimulateFromSeedProv(
Expand Down
51 changes: 29 additions & 22 deletions app/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"time"

wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -178,6 +177,14 @@ func printDBInfo(db dbm.DB) {
fmt.Println("-^^^- Database Info -^^^-")
}

// newSimAppOpts creates a new set of AppOptions with a temp dir for home, and the desired invariant check period.
func newSimAppOpts(t testing.TB) simtestutil.AppOptionsMap {
return simtestutil.AppOptionsMap{
flags.FlagHome: t.TempDir(),
server.FlagInvCheckPeriod: simcli.FlagPeriodValue,
}
}

func TestFullAppSimulation(t *testing.T) {
config, db, dir, logger, skip, err := setupSimulation("leveldb-app-sim", "Simulation")
if skip {
Expand All @@ -191,7 +198,7 @@ func TestFullAppSimulation(t *testing.T) {
require.NoError(t, os.RemoveAll(dir))
}()

app := New(logger, db, nil, true, map[int64]bool{}, t.TempDir(), simcli.FlagPeriodValue, simtestutil.EmptyAppOptions{}, fauxMerkleModeOpt)
app := New(logger, db, nil, true, newSimAppOpts(t), fauxMerkleModeOpt)
require.Equal(t, "provenanced", app.Name())

fmt.Printf("running provenance full app simulation\n")
Expand Down Expand Up @@ -230,7 +237,7 @@ func TestSimple(t *testing.T) {
require.NoError(t, os.RemoveAll(dir))
}()

app := New(logger, db, nil, true, map[int64]bool{}, t.TempDir(), simcli.FlagPeriodValue, simtestutil.EmptyAppOptions{}, fauxMerkleModeOpt)
app := New(logger, db, nil, true, newSimAppOpts(t), fauxMerkleModeOpt)
require.Equal(t, "provenanced", app.Name())

// run randomized simulation
Expand Down Expand Up @@ -268,12 +275,12 @@ func TestAppImportExport(t *testing.T) {
require.NoError(t, os.RemoveAll(dir))
}()

home := t.TempDir()
appOptions := make(simtestutil.AppOptionsMap)
appOptions[flags.FlagHome] = home
appOptions[server.FlagInvCheckPeriod] = simcli.FlagPeriodValue

app := New(logger, db, nil, true, map[int64]bool{}, home, simcli.FlagPeriodValue, appOptions, fauxMerkleModeOpt, baseapp.SetChainID(config.ChainID))
appOpts := newSimAppOpts(t)
baseAppOpts := []func(*baseapp.BaseApp){
fauxMerkleModeOpt,
baseapp.SetChainID(config.ChainID),
}
app := New(logger, db, nil, true, appOpts, baseAppOpts...)
require.Equal(t, "provenanced", app.Name())

fmt.Printf("running provenance test import export\n")
Expand Down Expand Up @@ -305,15 +312,15 @@ func TestAppImportExport(t *testing.T) {

fmt.Printf("importing genesis...\n")

newDB, newDir, _, _, err := simtestutil.SetupSimulation(config, "leveldb-app-sim-2", "Simulation-2", simcli.FlagVerboseValue, simcli.FlagEnabledValue)
newDB, newDir, newLogger, _, err := simtestutil.SetupSimulation(config, "leveldb-app-sim-2", "Simulation-2", simcli.FlagVerboseValue, simcli.FlagEnabledValue)
require.NoError(t, err, "simulation setup failed")

defer func() {
require.NoError(t, newDB.Close())
require.NoError(t, os.RemoveAll(newDir))
}()

newApp := New(log.NewNopLogger(), newDB, nil, true, map[int64]bool{}, home, simcli.FlagPeriodValue, appOptions, fauxMerkleModeOpt, baseapp.SetChainID(config.ChainID))
newApp := New(newLogger, newDB, nil, true, appOpts, baseAppOpts...)

var genesisState map[string]json.RawMessage
err = json.Unmarshal(exported.AppState, &genesisState)
Expand Down Expand Up @@ -396,8 +403,12 @@ func TestAppSimulationAfterImport(t *testing.T) {
require.NoError(t, os.RemoveAll(dir))
}()

home := t.TempDir()
app := New(logger, db, nil, true, map[int64]bool{}, home, simcli.FlagPeriodValue, simtestutil.EmptyAppOptions{}, fauxMerkleModeOpt)
appOpts := newSimAppOpts(t)
baseAppOpts := []func(*baseapp.BaseApp){
fauxMerkleModeOpt,
baseapp.SetChainID(config.ChainID),
}
app := New(logger, db, nil, true, appOpts, baseAppOpts...)

// Run randomized simulation
stopEarly, lastBlockTime, simParams, simErr := simulation.SimulateFromSeedProv(
Expand Down Expand Up @@ -431,15 +442,15 @@ func TestAppSimulationAfterImport(t *testing.T) {

fmt.Printf("importing genesis...\n")

_, newDB, newDir, _, _, err := setupSimulation("leveldb-app-sim-2", "Simulation-2")
_, newDB, newDir, newLogger, _, err := setupSimulation("leveldb-app-sim-2", "Simulation-2")
require.NoError(t, err, "simulation setup failed")

defer func() {
newDB.Close()
require.NoError(t, os.RemoveAll(newDir))
}()

newApp := New(log.NewNopLogger(), newDB, nil, true, map[int64]bool{}, home, simcli.FlagPeriodValue, simtestutil.EmptyAppOptions{}, fauxMerkleModeOpt)
newApp := New(newLogger, newDB, nil, true, appOpts, baseAppOpts...)

_, err = newApp.InitChain(&abci.RequestInitChain{
AppStateBytes: exported.AppState,
Expand Down Expand Up @@ -500,13 +511,9 @@ func TestAppStateDeterminism(t *testing.T) {
}
}

home := t.TempDir()

appOptions := viper.New()
appOptions.SetDefault(flags.FlagHome, home)
appOptions.SetDefault(server.FlagInvCheckPeriod, simcli.FlagPeriodValue)
appOpts := newSimAppOpts(t)
if simcli.FlagVerboseValue {
appOptions.SetDefault(flags.FlagLogLevel, "debug")
appOpts[flags.FlagLogLevel] = "debug"
}

for i, seed := range seeds {
Expand All @@ -522,7 +529,7 @@ func TestAppStateDeterminism(t *testing.T) {
}

db := dbm.NewMemDB()
app := New(logger, db, nil, true, map[int64]bool{}, home, simcli.FlagPeriodValue, appOptions, interBlockCacheOpt(), baseapp.SetChainID(config.ChainID))
app := New(logger, db, nil, true, appOpts, interBlockCacheOpt(), baseapp.SetChainID(config.ChainID))

fmt.Printf(
"running provenance non-determinism simulation; seed %d: %d/%d, attempt: %d/%d\n",
Expand Down
21 changes: 7 additions & 14 deletions app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,10 @@ var DefaultConsensusParams = &cmttmtypes.ConsensusParams{

// SetupOptions defines arguments that are passed into `Simapp` constructor.
type SetupOptions struct {
Logger log.Logger
DB *dbm.MemDB
InvCheckPeriod uint
HomePath string
SkipUpgradeHeights map[int64]bool
AppOpts servertypes.AppOptions
ChainID string
Logger log.Logger
DB *dbm.MemDB
AppOpts servertypes.AppOptions
ChainID string
}

func setup(t *testing.T, withGenesis bool, invCheckPeriod uint, chainID string) (*App, GenesisState) {
Expand All @@ -81,7 +78,7 @@ func setup(t *testing.T, withGenesis bool, invCheckPeriod uint, chainID string)
pioconfig.SetProvenanceConfig("", 0)
}

app := New(loggerMaker(), db, nil, true, map[int64]bool{}, t.TempDir(), invCheckPeriod, simtestutil.EmptyAppOptions{}, baseapp.SetChainID(chainID))
app := New(loggerMaker(), db, nil, true, simtestutil.NewAppOptionsWithFlagHome(t.TempDir()), baseapp.SetChainID(chainID))
if withGenesis {
return app, app.DefaultGenesis()
}
Expand Down Expand Up @@ -157,7 +154,7 @@ func NewAppWithCustomOptions(t *testing.T, isCheckTx bool, options SetupOptions)
Coins: sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 100_000_000_000_000)),
}

app := New(options.Logger, options.DB, nil, true, options.SkipUpgradeHeights, options.HomePath, options.InvCheckPeriod, options.AppOpts)
app := New(options.Logger, options.DB, nil, true, options.AppOpts, baseapp.SetChainID(options.ChainID))
genesisState := app.DefaultGenesis()
genesisState = genesisStateWithValSet(t, app, genesisState, valSet, []authtypes.GenesisAccount{acc}, balance)

Expand Down Expand Up @@ -524,10 +521,6 @@ func MakeTestEncodingConfig(t *testing.T) params.EncodingConfig {
}
defer os.RemoveAll(tempDir)

tempApp := New(log.NewNopLogger(), dbm.NewMemDB(), nil, true, nil,
tempDir,
0,
simtestutil.EmptyAppOptions{},
)
tempApp := New(log.NewNopLogger(), dbm.NewMemDB(), nil, true, simtestutil.NewAppOptionsWithFlagHome(tempDir))
return tempApp.GetEncodingConfig()
}
26 changes: 4 additions & 22 deletions cmd/provenanced/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"context"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -75,11 +74,7 @@ func NewRootCmd(sealConfig bool) (*cobra.Command, params.EncodingConfig) {
// We initially set the config as testnet so commands that run before start work for testing such as gentx.
app.SetConfig(true, false)

tempApp := app.New(log.NewNopLogger(), dbm.NewMemDB(), nil, true, nil,
tempDir,
0,
simtestutil.EmptyAppOptions{},
)
tempApp := app.New(log.NewNopLogger(), dbm.NewMemDB(), nil, true, simtestutil.NewAppOptionsWithFlagHome(tempDir))
encodingConfig := tempApp.GetEncodingConfig()

initClientCtx := client.Context{}.
Expand Down Expand Up @@ -275,11 +270,6 @@ func newApp(logger log.Logger, db dbm.DB, traceStore io.Writer, appOpts serverty
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)
Expand Down Expand Up @@ -310,10 +300,7 @@ func newApp(logger log.Logger, db dbm.DB, traceStore io.Writer, appOpts serverty
chainID := cast.ToString(appOpts.Get(flags.FlagChainID))

return app.New(
logger, db, traceStore, true, skipUpgradeHeights,
cast.ToString(appOpts.Get(flags.FlagHome)),
cast.ToUint(appOpts.Get(server.FlagInvCheckPeriod)),
appOpts,
logger, db, traceStore, true, appOpts,
baseapp.SetPruning(pruningOpts),
baseapp.SetMinGasPrices(cast.ToString(appOpts.Get(server.FlagMinGasPrices))),
baseapp.SetMinRetainBlocks(cast.ToUint64(appOpts.Get(server.FlagMinRetainBlocks))),
Expand Down Expand Up @@ -362,19 +349,14 @@ func createAppAndExport(
) (servertypes.ExportedApp, error) {
var a *app.App

homePath, ok := appOpts.Get(flags.FlagHome).(string)
if !ok || homePath == "" {
return servertypes.ExportedApp{}, errors.New("application home not set")
}

if height != -1 {
a = app.New(logger, db, traceStore, false, map[int64]bool{}, homePath, cast.ToUint(appOpts.Get(server.FlagInvCheckPeriod)), appOpts)
a = app.New(logger, db, traceStore, false, appOpts)

if err := a.LoadHeight(height); err != nil {
return servertypes.ExportedApp{}, err
}
} else {
a = app.New(logger, db, traceStore, true, map[int64]bool{}, homePath, cast.ToUint(appOpts.Get(server.FlagInvCheckPeriod)), appOpts)
a = app.New(logger, db, traceStore, true, appOpts)
}

return a.ExportAppStateAndValidators(forZeroHeight, jailAllowedAddrs, modulesToExport)
Expand Down
6 changes: 1 addition & 5 deletions cmd/provenanced/cmd/testnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ func Test_TestnetCmd(t *testing.T) {
pioconfig.SetProvenanceConfig("", 0)
logger := log.NewNopLogger()
cfg, err := genutiltest.CreateDefaultCometConfig(home)
tempApp := app.New(log.NewNopLogger(), dbm.NewMemDB(), nil, true, nil,
home,
0,
simtestutil.EmptyAppOptions{},
)
tempApp := app.New(log.NewNopLogger(), dbm.NewMemDB(), nil, true, simtestutil.NewAppOptionsWithFlagHome(home))
encodingConfig := tempApp.GetEncodingConfig()

require.NoError(t, err)
Expand Down
Loading

0 comments on commit d0b5422

Please sign in to comment.