From dbfedd08f54f33aabd0d2ac417e7a16624ca871f Mon Sep 17 00:00:00 2001 From: frog power 4000 Date: Wed, 31 Jul 2019 09:59:16 -0400 Subject: [PATCH] Merge PR #4800: Organize store keys in app.go into a map --- app.go | 61 +++++++++++++++++++---------------------------------- export.go | 2 +- sim_test.go | 37 ++++++++++++++++++++------------ utils.go | 10 +++------ 4 files changed, 49 insertions(+), 61 deletions(-) diff --git a/app.go b/app.go index bc46e0cc2456..58e7cf18658b 100644 --- a/app.go +++ b/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/export.go b/export.go index e1fe03e20569..8d6273abdd4e 100644 --- a/export.go +++ b/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/sim_test.go b/sim_test.go index 54b4e10340c9..0d1de926b5fa 100644 --- a/sim_test.go +++ b/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/utils.go b/utils.go index a97e0558d77b..230ec2b17e36 100644 --- a/utils.go +++ b/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