diff --git a/.pending/improvements/keys/_4611-store-keys-in- b/.pending/improvements/keys/_4611-store-keys-in- new file mode 100644 index 0000000000..e319e241ac --- /dev/null +++ b/.pending/improvements/keys/_4611-store-keys-in- @@ -0,0 +1 @@ +#4611 store keys in simapp now use a map instead of using individual literal keys diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index b5778ecd2e..c4a81be94a 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -160,6 +160,28 @@ func (app *BaseApp) MountStores(keys ...sdk.StoreKey) { } } +// MountStores mounts all IAVL or DB stores to the provided keys in the BaseApp +// multistore. +func (app *BaseApp) MountKVStores(keys map[string]*sdk.KVStoreKey) { + for _, key := range keys { + if !app.fauxMerkleMode { + app.MountStore(key, sdk.StoreTypeIAVL) + } else { + // StoreTypeDB doesn't do anything upon commit, and it doesn't + // retain history, but it's useful for faster simulation. + app.MountStore(key, sdk.StoreTypeDB) + } + } +} + +// MountStores mounts all IAVL or DB stores to the provided keys in the BaseApp +// multistore. +func (app *BaseApp) MountTransientStores(keys map[string]*sdk.TransientStoreKey) { + for _, key := range keys { + app.MountStore(key, sdk.StoreTypeTransient) + } +} + // MountStoreWithDB mounts a store to the provided key in the BaseApp // multistore, using a specified DB. func (app *BaseApp) MountStoreWithDB(key sdk.StoreKey, typ sdk.StoreType, db dbm.DB) { diff --git a/simapp/app.go b/simapp/app.go index bc46e0cc24..58e7cf1865 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -18,7 +18,6 @@ import ( "github.com/cosmos/cosmos-sdk/x/bank" "github.com/cosmos/cosmos-sdk/x/crisis" distr "github.com/cosmos/cosmos-sdk/x/distribution" - distrclient "github.com/cosmos/cosmos-sdk/x/distribution/client" "github.com/cosmos/cosmos-sdk/x/genaccounts" "github.com/cosmos/cosmos-sdk/x/genutil" "github.com/cosmos/cosmos-sdk/x/gov" @@ -50,7 +49,7 @@ var ( staking.AppModuleBasic{}, mint.AppModuleBasic{}, distr.AppModuleBasic{}, - gov.NewAppModuleBasic(paramsclient.ProposalHandler, distrclient.ProposalHandler), + gov.NewAppModuleBasic(paramsclient.ProposalHandler, distr.ProposalHandler), params.AppModuleBasic{}, crisis.AppModuleBasic{}, slashing.AppModuleBasic{}, @@ -85,18 +84,8 @@ type SimApp struct { invCheckPeriod uint // keys to access the substores - keyMain *sdk.KVStoreKey - keyAccount *sdk.KVStoreKey - keySupply *sdk.KVStoreKey - keyStaking *sdk.KVStoreKey - tkeyStaking *sdk.TransientStoreKey - keySlashing *sdk.KVStoreKey - keyMint *sdk.KVStoreKey - keyDistr *sdk.KVStoreKey - tkeyDistr *sdk.TransientStoreKey - keyGov *sdk.KVStoreKey - keyParams *sdk.KVStoreKey - tkeyParams *sdk.TransientStoreKey + keys map[string]*sdk.KVStoreKey + tkeys map[string]*sdk.TransientStoreKey // keepers accountKeeper auth.AccountKeeper @@ -126,26 +115,21 @@ func NewSimApp( bApp.SetCommitMultiStoreTracer(traceStore) bApp.SetAppVersion(version.Version) + keys := sdk.NewKVStoreKeys(bam.MainStoreKey, auth.StoreKey, staking.StoreKey, + supply.StoreKey, mint.StoreKey, distr.StoreKey, slashing.StoreKey, + gov.StoreKey, params.StoreKey) + tkeys := sdk.NewTransientStoreKeys(staking.TStoreKey, params.TStoreKey) + app := &SimApp{ BaseApp: bApp, cdc: cdc, invCheckPeriod: invCheckPeriod, - keyMain: sdk.NewKVStoreKey(bam.MainStoreKey), - keyAccount: sdk.NewKVStoreKey(auth.StoreKey), - keyStaking: sdk.NewKVStoreKey(staking.StoreKey), - keySupply: sdk.NewKVStoreKey(supply.StoreKey), - tkeyStaking: sdk.NewTransientStoreKey(staking.TStoreKey), - keyMint: sdk.NewKVStoreKey(mint.StoreKey), - keyDistr: sdk.NewKVStoreKey(distr.StoreKey), - tkeyDistr: sdk.NewTransientStoreKey(distr.TStoreKey), - keySlashing: sdk.NewKVStoreKey(slashing.StoreKey), - keyGov: sdk.NewKVStoreKey(gov.StoreKey), - keyParams: sdk.NewKVStoreKey(params.StoreKey), - tkeyParams: sdk.NewTransientStoreKey(params.TStoreKey), + keys: keys, + tkeys: tkeys, } // init params keeper and subspaces - app.paramsKeeper = params.NewKeeper(app.cdc, app.keyParams, app.tkeyParams, params.DefaultCodespace) + app.paramsKeeper = params.NewKeeper(app.cdc, keys[params.StoreKey], tkeys[params.TStoreKey], params.DefaultCodespace) authSubspace := app.paramsKeeper.Subspace(auth.DefaultParamspace) bankSubspace := app.paramsKeeper.Subspace(bank.DefaultParamspace) stakingSubspace := app.paramsKeeper.Subspace(staking.DefaultParamspace) @@ -156,15 +140,15 @@ func NewSimApp( crisisSubspace := app.paramsKeeper.Subspace(crisis.DefaultParamspace) // add keepers - app.accountKeeper = auth.NewAccountKeeper(app.cdc, app.keyAccount, authSubspace, auth.ProtoBaseAccount) + app.accountKeeper = auth.NewAccountKeeper(app.cdc, keys[auth.StoreKey], authSubspace, auth.ProtoBaseAccount) app.bankKeeper = bank.NewBaseKeeper(app.accountKeeper, bankSubspace, bank.DefaultCodespace) - app.supplyKeeper = supply.NewKeeper(app.cdc, app.keySupply, app.accountKeeper, app.bankKeeper, supply.DefaultCodespace, maccPerms) - stakingKeeper := staking.NewKeeper(app.cdc, app.keyStaking, app.tkeyStaking, + app.supplyKeeper = supply.NewKeeper(app.cdc, keys[supply.StoreKey], app.accountKeeper, app.bankKeeper, supply.DefaultCodespace, maccPerms) + stakingKeeper := staking.NewKeeper(app.cdc, keys[staking.StoreKey], tkeys[staking.TStoreKey], app.supplyKeeper, stakingSubspace, staking.DefaultCodespace) - app.mintKeeper = mint.NewKeeper(app.cdc, app.keyMint, mintSubspace, &stakingKeeper, app.supplyKeeper, auth.FeeCollectorName) - app.distrKeeper = distr.NewKeeper(app.cdc, app.keyDistr, distrSubspace, &stakingKeeper, + app.mintKeeper = mint.NewKeeper(app.cdc, keys[mint.StoreKey], mintSubspace, &stakingKeeper, app.supplyKeeper, auth.FeeCollectorName) + app.distrKeeper = distr.NewKeeper(app.cdc, keys[distr.StoreKey], distrSubspace, &stakingKeeper, app.supplyKeeper, distr.DefaultCodespace, auth.FeeCollectorName) - app.slashingKeeper = slashing.NewKeeper(app.cdc, app.keySlashing, &stakingKeeper, + app.slashingKeeper = slashing.NewKeeper(app.cdc, keys[slashing.StoreKey], &stakingKeeper, slashingSubspace, slashing.DefaultCodespace) app.crisisKeeper = crisis.NewKeeper(crisisSubspace, invCheckPeriod, app.supplyKeeper, auth.FeeCollectorName) @@ -173,7 +157,7 @@ func NewSimApp( govRouter.AddRoute(gov.RouterKey, gov.ProposalHandler). AddRoute(params.RouterKey, params.NewParamChangeProposalHandler(app.paramsKeeper)). AddRoute(distr.RouterKey, distr.NewCommunityPoolSpendProposalHandler(app.distrKeeper)) - app.govKeeper = gov.NewKeeper(app.cdc, app.keyGov, app.paramsKeeper, govSubspace, + app.govKeeper = gov.NewKeeper(app.cdc, keys[gov.StoreKey], app.paramsKeeper, govSubspace, app.supplyKeeper, &stakingKeeper, gov.DefaultCodespace, govRouter) // register the staking hooks @@ -212,9 +196,8 @@ func NewSimApp( app.mm.RegisterRoutes(app.Router(), app.QueryRouter()) // initialize stores - app.MountStores(app.keyMain, app.keyAccount, app.keySupply, app.keyStaking, - app.keyMint, app.keyDistr, app.keySlashing, app.keyGov, app.keyParams, - app.tkeyParams, app.tkeyStaking, app.tkeyDistr) + app.MountKVStores(keys) + app.MountTransientStores(tkeys) // initialize BaseApp app.SetInitChainer(app.InitChainer) @@ -223,7 +206,7 @@ func NewSimApp( app.SetEndBlocker(app.EndBlocker) if loadLatest { - err := app.LoadLatestVersion(app.keyMain) + err := app.LoadLatestVersion(app.keys[bam.MainStoreKey]) if err != nil { cmn.Exit(err.Error()) } @@ -250,7 +233,7 @@ func (app *SimApp) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci. // load a particular height func (app *SimApp) LoadHeight(height int64) error { - return app.LoadVersion(height, app.keyMain) + return app.LoadVersion(height, app.keys[bam.MainStoreKey]) } // ModuleAccountAddrs returns all the app's module account addresses. diff --git a/simapp/export.go b/simapp/export.go index e1fe03e205..8d6273abdd 100644 --- a/simapp/export.go +++ b/simapp/export.go @@ -129,7 +129,7 @@ func (app *SimApp) prepForZeroHeightGenesis(ctx sdk.Context, jailWhiteList []str // Iterate through validators by power descending, reset bond heights, and // update bond intra-tx counters. - store := ctx.KVStore(app.keyStaking) + store := ctx.KVStore(app.keys[staking.StoreKey]) iter := sdk.KVStoreReversePrefixIterator(store, staking.ValidatorsKey) counter := int16(0) diff --git a/simapp/sim_test.go b/simapp/sim_test.go index 54b4e10340..0d1de926b5 100644 --- a/simapp/sim_test.go +++ b/simapp/sim_test.go @@ -19,15 +19,22 @@ import ( "github.com/cosmos/cosmos-sdk/baseapp" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" authsim "github.com/cosmos/cosmos-sdk/x/auth/simulation" "github.com/cosmos/cosmos-sdk/x/bank" + distr "github.com/cosmos/cosmos-sdk/x/distribution" distrsim "github.com/cosmos/cosmos-sdk/x/distribution/simulation" + "github.com/cosmos/cosmos-sdk/x/gov" govsim "github.com/cosmos/cosmos-sdk/x/gov/simulation" + "github.com/cosmos/cosmos-sdk/x/mint" + "github.com/cosmos/cosmos-sdk/x/params" paramsim "github.com/cosmos/cosmos-sdk/x/params/simulation" "github.com/cosmos/cosmos-sdk/x/simulation" + "github.com/cosmos/cosmos-sdk/x/slashing" slashingsim "github.com/cosmos/cosmos-sdk/x/slashing/simulation" "github.com/cosmos/cosmos-sdk/x/staking" stakingsim "github.com/cosmos/cosmos-sdk/x/staking/simulation" + "github.com/cosmos/cosmos-sdk/x/supply" ) func init() { @@ -503,9 +510,9 @@ func TestAppImportExport(t *testing.T) { require.Equal(t, "SimApp", app.Name()) // Run randomized simulation - _, params, simErr := simulation.SimulateFromSeed(getSimulateFromSeedInput(t, os.Stdout, app)) + _, simParams, simErr := simulation.SimulateFromSeed(getSimulateFromSeedInput(t, os.Stdout, app)) - // export state and params before the simulation error is checked + // export state and simParams before the simulation error is checked if exportStatePath != "" { fmt.Println("Exporting app state...") appState, _, err := app.ExportAppStateAndValidators(false, nil) @@ -517,10 +524,10 @@ func TestAppImportExport(t *testing.T) { if exportParamsPath != "" { fmt.Println("Exporting simulation params...") - paramsBz, err := json.MarshalIndent(params, "", " ") + simParamsBz, err := json.MarshalIndent(simParams, "", " ") require.NoError(t, err) - err = ioutil.WriteFile(exportParamsPath, paramsBz, 0644) + err = ioutil.WriteFile(exportParamsPath, simParamsBz, 0644) require.NoError(t, err) } @@ -570,16 +577,18 @@ func TestAppImportExport(t *testing.T) { } storeKeysPrefixes := []StoreKeysPrefixes{ - {app.keyMain, newApp.keyMain, [][]byte{}}, - {app.keyAccount, newApp.keyAccount, [][]byte{}}, - {app.keyStaking, newApp.keyStaking, [][]byte{staking.UnbondingQueueKey, - staking.RedelegationQueueKey, staking.ValidatorQueueKey}}, // ordering may change but it doesn't matter - {app.keySlashing, newApp.keySlashing, [][]byte{}}, - {app.keyMint, newApp.keyMint, [][]byte{}}, - {app.keyDistr, newApp.keyDistr, [][]byte{}}, - {app.keySupply, newApp.keySupply, [][]byte{}}, - {app.keyParams, newApp.keyParams, [][]byte{}}, - {app.keyGov, newApp.keyGov, [][]byte{}}, + {app.keys[baseapp.MainStoreKey], newApp.keys[baseapp.MainStoreKey], [][]byte{}}, + {app.keys[auth.StoreKey], newApp.keys[auth.StoreKey], [][]byte{}}, + {app.keys[staking.StoreKey], newApp.keys[staking.StoreKey], + [][]byte{ + staking.UnbondingQueueKey, staking.RedelegationQueueKey, staking.ValidatorQueueKey, + }}, // ordering may change but it doesn't matter + {app.keys[slashing.StoreKey], newApp.keys[slashing.StoreKey], [][]byte{}}, + {app.keys[mint.StoreKey], newApp.keys[mint.StoreKey], [][]byte{}}, + {app.keys[distr.StoreKey], newApp.keys[distr.StoreKey], [][]byte{}}, + {app.keys[supply.StoreKey], newApp.keys[supply.StoreKey], [][]byte{}}, + {app.keys[params.StoreKey], newApp.keys[params.StoreKey], [][]byte{}}, + {app.keys[gov.StoreKey], newApp.keys[gov.StoreKey], [][]byte{}}, } for _, storeKeysPrefix := range storeKeysPrefixes { diff --git a/simapp/utils.go b/simapp/utils.go index a97e0558d7..230ec2b17e 100644 --- a/simapp/utils.go +++ b/simapp/utils.go @@ -12,18 +12,14 @@ import ( "time" "github.com/tendermint/tendermint/crypto" + "github.com/tendermint/tendermint/crypto/secp256k1" cmn "github.com/tendermint/tendermint/libs/common" - dbm "github.com/tendermint/tendermint/libs/db" "github.com/tendermint/tendermint/libs/log" - - "github.com/cosmos/cosmos-sdk/codec" - - "github.com/tendermint/tendermint/crypto/secp256k1" tmtypes "github.com/tendermint/tendermint/types" "github.com/cosmos/cosmos-sdk/baseapp" - + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/bank" @@ -65,7 +61,7 @@ func NewSimAppUNSAFE(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLat ) (gapp *SimApp, keyMain, keyStaking *sdk.KVStoreKey, stakingKeeper staking.Keeper) { gapp = NewSimApp(logger, db, traceStore, loadLatest, invCheckPeriod, baseAppOptions...) - return gapp, gapp.keyMain, gapp.keyStaking, gapp.stakingKeeper + return gapp, gapp.keys[baseapp.MainStoreKey], gapp.keys[staking.StoreKey], gapp.stakingKeeper } // AppStateFromGenesisFileFn util function to generate the genesis AppState diff --git a/types/store.go b/types/store.go index c7e6576141..1ebeb62aed 100644 --- a/types/store.go +++ b/types/store.go @@ -73,12 +73,32 @@ func NewKVStoreKey(name string) *KVStoreKey { return types.NewKVStoreKey(name) } +// NewKVStoreKeys returns a map of new pointers to KVStoreKey's. +// Uses pointers so keys don't collide. +func NewKVStoreKeys(names ...string) map[string]*KVStoreKey { + keys := make(map[string]*KVStoreKey) + for _, name := range names { + keys[name] = NewKVStoreKey(name) + } + return keys +} + // Constructs new TransientStoreKey // Must return a pointer according to the ocap principle func NewTransientStoreKey(name string) *TransientStoreKey { return types.NewTransientStoreKey(name) } +// NewTransientStoreKeys constructs a new map of TransientStoreKey's +// Must return pointers according to the ocap principle +func NewTransientStoreKeys(names ...string) map[string]*TransientStoreKey { + keys := make(map[string]*TransientStoreKey) + for _, name := range names { + keys[name] = NewTransientStoreKey(name) + } + return keys +} + // PrefixEndBytes returns the []byte that would end a // range query for all []byte with a certain prefix // Deals with last byte of prefix being FF without overflowing diff --git a/x/distribution/alias.go b/x/distribution/alias.go index 165a8d6934..466674a492 100644 --- a/x/distribution/alias.go +++ b/x/distribution/alias.go @@ -2,11 +2,12 @@ // autogenerated code using github.com/rigelrozanski/multitool // aliases generated for the following subdirectories: // ALIASGEN: github.com/cosmos/cosmos-sdk/x/distribution/keeper -// ALIASGEN: github.com/cosmos/cosmos-sdk/x/distribution/tags // ALIASGEN: github.com/cosmos/cosmos-sdk/x/distribution/types +// ALIASGEN: github.com/cosmos/cosmos-sdk/x/distribution/client package distribution import ( + "github.com/cosmos/cosmos-sdk/x/distribution/client" "github.com/cosmos/cosmos-sdk/x/distribution/keeper" "github.com/cosmos/cosmos-sdk/x/distribution/types" ) @@ -20,7 +21,6 @@ const ( CodeSetWithdrawAddrDisabled = types.CodeSetWithdrawAddrDisabled ModuleName = types.ModuleName StoreKey = types.StoreKey - TStoreKey = types.TStoreKey RouterKey = types.RouterKey QuerierRoute = types.QuerierRoute ProposalTypeCommunityPoolSpend = types.ProposalTypeCommunityPoolSpend @@ -63,8 +63,8 @@ var ( GetValidatorCurrentRewardsKey = keeper.GetValidatorCurrentRewardsKey GetValidatorAccumulatedCommissionKey = keeper.GetValidatorAccumulatedCommissionKey GetValidatorSlashEventPrefix = keeper.GetValidatorSlashEventPrefix - GetValidatorSlashEventKey = keeper.GetValidatorSlashEventKey GetValidatorSlashEventKeyPrefix = keeper.GetValidatorSlashEventKeyPrefix + GetValidatorSlashEventKey = keeper.GetValidatorSlashEventKey ParamKeyTable = keeper.ParamKeyTable HandleCommunityPoolSpendProposal = keeper.HandleCommunityPoolSpendProposal NewQuerier = keeper.NewQuerier @@ -119,11 +119,17 @@ var ( ParamStoreKeyBonusProposerReward = keeper.ParamStoreKeyBonusProposerReward ParamStoreKeyWithdrawAddrEnabled = keeper.ParamStoreKeyWithdrawAddrEnabled TestAddrs = keeper.TestAddrs + ModuleCdc = types.ModuleCdc + EventTypeSetWithdrawAddress = types.EventTypeSetWithdrawAddress EventTypeRewards = types.EventTypeRewards EventTypeCommission = types.EventTypeCommission - AttributeValueCategory = types.AttributeValueCategory + EventTypeWithdrawRewards = types.EventTypeWithdrawRewards + EventTypeWithdrawCommission = types.EventTypeWithdrawCommission + EventTypeProposerReward = types.EventTypeProposerReward + AttributeKeyWithdrawAddress = types.AttributeKeyWithdrawAddress AttributeKeyValidator = types.AttributeKeyValidator - ModuleCdc = types.ModuleCdc + AttributeValueCategory = types.AttributeValueCategory + ProposalHandler = client.ProposalHandler ) type ( diff --git a/x/distribution/client/proposal_handler.go b/x/distribution/client/proposal_handler.go index 6514d8f5a2..c7fcc0bdce 100644 --- a/x/distribution/client/proposal_handler.go +++ b/x/distribution/client/proposal_handler.go @@ -7,4 +7,6 @@ import ( ) // param change proposal handler -var ProposalHandler = govclient.NewProposalHandler(cli.GetCmdSubmitProposal, rest.ProposalRESTHandler) +var ( + ProposalHandler = govclient.NewProposalHandler(cli.GetCmdSubmitProposal, rest.ProposalRESTHandler) +) diff --git a/x/distribution/types/keys.go b/x/distribution/types/keys.go index aeb1e4d3f7..5bdeaecc20 100644 --- a/x/distribution/types/keys.go +++ b/x/distribution/types/keys.go @@ -7,9 +7,6 @@ const ( // StoreKey is the store key string for distribution StoreKey = ModuleName - // TStoreKey is the transient store key for distribution - TStoreKey = "transient_" + ModuleName - // RouterKey is the message route for distribution RouterKey = ModuleName diff --git a/x/params/alias.go b/x/params/alias.go index 60ecdafdfa..47b89296de 100644 --- a/x/params/alias.go +++ b/x/params/alias.go @@ -40,6 +40,9 @@ var ( NewParamChange = types.NewParamChange NewParamChangeWithSubkey = types.NewParamChangeWithSubkey ValidateChanges = types.ValidateChanges + + // variable aliases + ModuleCdc = types.ModuleCdc ) type (