diff --git a/app/app.go b/app/app.go index 9c71e41849a..baec887dfe7 100644 --- a/app/app.go +++ b/app/app.go @@ -209,15 +209,14 @@ func NewGaiaApp( bank.NewAppModule(app.bankKeeper, app.accountKeeper), crisis.NewAppModule(&app.crisisKeeper), supply.NewAppModule(app.supplyKeeper, app.accountKeeper), - distr.NewAppModule(app.distrKeeper, app.supplyKeeper), - gov.NewAppModule(app.govKeeper, app.supplyKeeper), + gov.NewAppModule(app.govKeeper, app.accountKeeper, app.supplyKeeper), mint.NewAppModule(app.mintKeeper), - slashing.NewAppModule(app.slashingKeeper, app.stakingKeeper), + slashing.NewAppModule(app.slashingKeeper, app.accountKeeper, app.stakingKeeper), + distr.NewAppModule(app.distrKeeper, app.accountKeeper, app.supplyKeeper, app.stakingKeeper), staking.NewAppModule(app.stakingKeeper, app.accountKeeper, app.supplyKeeper), upgrade.NewAppModule(app.upgradeKeeper), evidence.NewAppModule(app.evidenceKeeper), ) - // During begin block slashing happens after distr.BeginBlocker so that // there is nothing left over in the validator fee pool, so as to keep the // CanWithdrawInvariant invariant. @@ -244,11 +243,11 @@ func NewGaiaApp( auth.NewAppModule(app.accountKeeper), bank.NewAppModule(app.bankKeeper, app.accountKeeper), supply.NewAppModule(app.supplyKeeper, app.accountKeeper), - gov.NewAppModule(app.govKeeper, app.supplyKeeper), + gov.NewAppModule(app.govKeeper, app.accountKeeper, app.supplyKeeper), mint.NewAppModule(app.mintKeeper), - distr.NewAppModule(app.distrKeeper, app.supplyKeeper), + distr.NewAppModule(app.distrKeeper, app.accountKeeper, app.supplyKeeper, app.stakingKeeper), staking.NewAppModule(app.stakingKeeper, app.accountKeeper, app.supplyKeeper), - slashing.NewAppModule(app.slashingKeeper, app.stakingKeeper), + slashing.NewAppModule(app.slashingKeeper, app.accountKeeper, app.stakingKeeper), ) app.sm.RegisterStoreDecoders() @@ -273,17 +272,17 @@ func NewGaiaApp( return app } -// application updates every begin block +// BeginBlocker application updates every begin block func (app *GaiaApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock { return app.mm.BeginBlock(ctx, req) } -// application updates every end block +// EndBlocker application updates every end block func (app *GaiaApp) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock { return app.mm.EndBlock(ctx, req) } -// application update at chain initialization +// InitChainer application update at chain initialization func (app *GaiaApp) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain { var genesisState simapp.GenesisState app.cdc.MustUnmarshalJSON(req.AppStateBytes, &genesisState) @@ -291,7 +290,7 @@ func (app *GaiaApp) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci return app.mm.InitGenesis(ctx, genesisState) } -// load a particular height +// LoadHeight loads a particular height func (app *GaiaApp) LoadHeight(height int64) error { return app.LoadVersion(height, app.keys[bam.MainStoreKey]) } diff --git a/app/sim_test.go b/app/sim_test.go index 0ade11ea044..282549cad81 100644 --- a/app/sim_test.go +++ b/app/sim_test.go @@ -18,19 +18,13 @@ import ( "github.com/cosmos/cosmos-sdk/store" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" - banksim "github.com/cosmos/cosmos-sdk/x/bank/simulation" 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" ) @@ -38,201 +32,6 @@ func init() { simapp.GetSimulatorFlags() } -func testAndRunTxs(app *GaiaApp, config simulation.Config) []simulation.WeightedOperation { - ap := make(simulation.AppParams) - - paramChanges := app.sm.GenerateParamChanges(config.Seed) - - if config.ParamsFile != "" { - bz, err := ioutil.ReadFile(config.ParamsFile) - if err != nil { - panic(err) - } - - app.cdc.MustUnmarshalJSON(bz, &ap) - } - - // nolint: govet - return []simulation.WeightedOperation{ - { - func(_ *rand.Rand) int { - var v int - ap.GetOrGenerate(app.cdc, OpWeightMsgSend, &v, nil, - func(_ *rand.Rand) { - v = 100 - }) - return v - }(nil), - banksim.SimulateMsgSend(app.accountKeeper, app.bankKeeper), - }, - { - func(_ *rand.Rand) int { - var v int - ap.GetOrGenerate(app.cdc, OpWeightMsgMultiSend, &v, nil, - func(_ *rand.Rand) { - v = 40 - }) - return v - }(nil), - banksim.SimulateMsgMultiSend(app.accountKeeper, app.bankKeeper), - }, - { - func(_ *rand.Rand) int { - var v int - ap.GetOrGenerate(app.cdc, OpWeightMsgSetWithdrawAddress, &v, nil, - func(_ *rand.Rand) { - v = 50 - }) - return v - }(nil), - distrsim.SimulateMsgSetWithdrawAddress(app.accountKeeper, app.distrKeeper), - }, - { - func(_ *rand.Rand) int { - var v int - ap.GetOrGenerate(app.cdc, OpWeightMsgWithdrawDelegationReward, &v, nil, - func(_ *rand.Rand) { - v = 50 - }) - return v - }(nil), - distrsim.SimulateMsgWithdrawDelegatorReward(app.accountKeeper, app.distrKeeper, app.stakingKeeper), - }, - { - func(_ *rand.Rand) int { - var v int - ap.GetOrGenerate(app.cdc, OpWeightMsgWithdrawValidatorCommission, &v, nil, - func(_ *rand.Rand) { - v = 50 - }) - return v - }(nil), - distrsim.SimulateMsgWithdrawValidatorCommission(app.accountKeeper, app.distrKeeper, app.stakingKeeper), - }, - { - func(_ *rand.Rand) int { - var v int - ap.GetOrGenerate(app.cdc, OpWeightSubmitTextProposal, &v, nil, - func(_ *rand.Rand) { - v = 20 - }) - return v - }(nil), - govsim.SimulateSubmitProposal(app.accountKeeper, app.govKeeper, govsim.SimulateTextProposalContent), - }, - { - func(_ *rand.Rand) int { - var v int - ap.GetOrGenerate(app.cdc, OpWeightSubmitCommunitySpendProposal, &v, nil, - func(_ *rand.Rand) { - v = 20 - }) - return v - }(nil), - govsim.SimulateSubmitProposal(app.accountKeeper, app.govKeeper, distrsim.SimulateCommunityPoolSpendProposalContent(app.distrKeeper)), - }, - { - func(_ *rand.Rand) int { - var v int - ap.GetOrGenerate(app.cdc, OpWeightSubmitParamChangeProposal, &v, nil, - func(_ *rand.Rand) { - v = 20 - }) - return v - }(nil), - govsim.SimulateSubmitProposal(app.accountKeeper, app.govKeeper, paramsim.SimulateParamChangeProposalContent(paramChanges)), - }, - { - func(_ *rand.Rand) int { - var v int - ap.GetOrGenerate(app.cdc, OpWeightMsgDeposit, &v, nil, - func(_ *rand.Rand) { - v = 100 - }) - return v - }(nil), - govsim.SimulateMsgDeposit(app.accountKeeper, app.govKeeper), - }, - { - func(_ *rand.Rand) int { - var v int - ap.GetOrGenerate(app.cdc, OpWeightMsgVote, &v, nil, - func(_ *rand.Rand) { - v = 100 - }) - return v - }(nil), - govsim.SimulateMsgVote(app.accountKeeper, app.govKeeper), - }, - { - func(_ *rand.Rand) int { - var v int - ap.GetOrGenerate(app.cdc, OpWeightMsgCreateValidator, &v, nil, - func(_ *rand.Rand) { - v = 100 - }) - return v - }(nil), - stakingsim.SimulateMsgCreateValidator(app.accountKeeper, app.stakingKeeper), - }, - { - func(_ *rand.Rand) int { - var v int - ap.GetOrGenerate(app.cdc, OpWeightMsgEditValidator, &v, nil, - func(_ *rand.Rand) { - v = 20 - }) - return v - }(nil), - stakingsim.SimulateMsgEditValidator(app.accountKeeper, app.stakingKeeper), - }, - { - func(_ *rand.Rand) int { - var v int - ap.GetOrGenerate(app.cdc, OpWeightMsgDelegate, &v, nil, - func(_ *rand.Rand) { - v = 100 - }) - return v - }(nil), - stakingsim.SimulateMsgDelegate(app.accountKeeper, app.stakingKeeper), - }, - { - func(_ *rand.Rand) int { - var v int - ap.GetOrGenerate(app.cdc, OpWeightMsgUndelegate, &v, nil, - func(_ *rand.Rand) { - v = 100 - }) - return v - }(nil), - stakingsim.SimulateMsgUndelegate(app.accountKeeper, app.stakingKeeper), - }, - { - func(_ *rand.Rand) int { - var v int - ap.GetOrGenerate(app.cdc, OpWeightMsgBeginRedelegate, &v, nil, - func(_ *rand.Rand) { - v = 100 - }) - return v - }(nil), - stakingsim.SimulateMsgBeginRedelegate(app.accountKeeper, app.stakingKeeper), - }, - { - func(_ *rand.Rand) int { - var v int - ap.GetOrGenerate(app.cdc, OpWeightMsgUnjail, &v, nil, - func(_ *rand.Rand) { - v = 100 - }) - return v - }(nil), - slashingsim.SimulateMsgUnjail(app.accountKeeper, app.slashingKeeper, app.stakingKeeper), - }, - } -} - // fauxMerkleModeOpt returns a BaseApp option to use a dbStoreAdapter instead of // an IAVLStore for faster simulation speed. func fauxMerkleModeOpt(bapp *baseapp.BaseApp) { @@ -267,18 +66,19 @@ func BenchmarkFullAppSimulation(b *testing.B) { _ = os.RemoveAll(dir) }() - gapp := NewGaiaApp(logger, db, nil, true, simapp.FlagPeriodValue, interBlockCacheOpt()) + app := NewGaiaApp(logger, db, nil, true, simapp.FlagPeriodValue, interBlockCacheOpt()) // Run randomized simulation // TODO: parameterize numbers, save for a later PR _, simParams, simErr := simulation.SimulateFromSeed( - b, os.Stdout, gapp.BaseApp, simapp.AppStateFn(gapp.Codec(), gapp.sm), - testAndRunTxs(gapp, config), gapp.ModuleAccountAddrs(), config, + b, os.Stdout, app.BaseApp, simapp.AppStateFn(app.Codec(), app.sm), + SimulationOperations(app, app.Codec(), config), + app.ModuleAccountAddrs(), config, ) // export state and params before the simulation error is checked if config.ExportStatePath != "" { - if err := ExportStateToJSON(gapp, config.ExportStatePath); err != nil { + if err := ExportStateToJSON(app, config.ExportStatePath); err != nil { fmt.Println(err) b.Fail() } @@ -328,18 +128,19 @@ func TestFullAppSimulation(t *testing.T) { _ = os.RemoveAll(dir) }() - gapp := NewGaiaApp(logger, db, nil, true, simapp.FlagPeriodValue, fauxMerkleModeOpt) - require.Equal(t, "GaiaApp", gapp.Name()) + app := NewGaiaApp(logger, db, nil, true, simapp.FlagPeriodValue, fauxMerkleModeOpt) + require.Equal(t, "GaiaApp", app.Name()) // Run randomized simulation _, simParams, simErr := simulation.SimulateFromSeed( - t, os.Stdout, gapp.BaseApp, simapp.AppStateFn(gapp.Codec(), gapp.sm), - testAndRunTxs(gapp, config), gapp.ModuleAccountAddrs(), config, + t, os.Stdout, app.BaseApp, simapp.AppStateFn(app.Codec(), app.sm), + SimulationOperations(app, app.Codec(), config), + app.ModuleAccountAddrs(), config, ) // export state and params before the simulation error is checked if config.ExportStatePath != "" { - err := ExportStateToJSON(gapp, config.ExportStatePath) + err := ExportStateToJSON(app, config.ExportStatePath) require.NoError(t, err) } @@ -390,7 +191,8 @@ func TestAppImportExport(t *testing.T) { // Run randomized simulation _, simParams, simErr := simulation.SimulateFromSeed( t, os.Stdout, app.BaseApp, simapp.AppStateFn(app.Codec(), app.sm), - testAndRunTxs(app, config), app.ModuleAccountAddrs(), config, + SimulationOperations(app, app.Codec(), config), + app.ModuleAccountAddrs(), config, ) // export state and simParams before the simulation error is checked @@ -504,19 +306,20 @@ func TestAppSimulationAfterImport(t *testing.T) { _ = os.RemoveAll(dir) }() - gapp := NewGaiaApp(logger, db, nil, true, simapp.FlagPeriodValue, fauxMerkleModeOpt) - require.Equal(t, "GaiaApp", gapp.Name()) + app := NewGaiaApp(logger, db, nil, true, simapp.FlagPeriodValue, fauxMerkleModeOpt) + require.Equal(t, "GaiaApp", app.Name()) // Run randomized simulation // Run randomized simulation stopEarly, simParams, simErr := simulation.SimulateFromSeed( - t, os.Stdout, gapp.BaseApp, simapp.AppStateFn(gapp.Codec(), gapp.sm), - testAndRunTxs(gapp, config), gapp.ModuleAccountAddrs(), config, + t, os.Stdout, app.BaseApp, simapp.AppStateFn(app.Codec(), app.sm), + SimulationOperations(app, app.Codec(), config), + app.ModuleAccountAddrs(), config, ) // export state and params before the simulation error is checked if config.ExportStatePath != "" { - err := ExportStateToJSON(gapp, config.ExportStatePath) + err := ExportStateToJSON(app, config.ExportStatePath) require.NoError(t, err) } @@ -543,7 +346,7 @@ func TestAppSimulationAfterImport(t *testing.T) { fmt.Printf("Exporting genesis...\n") - appState, _, err := gapp.ExportAppStateAndValidators(true, []string{}) + appState, _, err := app.ExportAppStateAndValidators(true, []string{}) if err != nil { panic(err) } @@ -569,8 +372,9 @@ func TestAppSimulationAfterImport(t *testing.T) { // Run randomized simulation on imported app _, _, err = simulation.SimulateFromSeed( - t, os.Stdout, newApp.BaseApp, simapp.AppStateFn(gapp.Codec(), gapp.sm), - testAndRunTxs(newApp, config), newApp.ModuleAccountAddrs(), config, + t, os.Stdout, newApp.BaseApp, simapp.AppStateFn(app.Codec(), app.sm), + SimulationOperations(app, app.Codec(), config), + newApp.ModuleAccountAddrs(), config, ) require.NoError(t, err) @@ -608,7 +412,8 @@ func TestAppStateDeterminism(t *testing.T) { _, _, err := simulation.SimulateFromSeed( t, os.Stdout, app.BaseApp, simapp.AppStateFn(app.Codec(), app.sm), - testAndRunTxs(app, config), app.ModuleAccountAddrs(), config, + SimulationOperations(app, app.Codec(), config), + app.ModuleAccountAddrs(), config, ) require.NoError(t, err) @@ -647,17 +452,18 @@ func BenchmarkInvariants(b *testing.B) { os.RemoveAll(dir) }() - gapp := NewGaiaApp(logger, db, nil, true, simapp.FlagPeriodValue, interBlockCacheOpt()) + app := NewGaiaApp(logger, db, nil, true, simapp.FlagPeriodValue, interBlockCacheOpt()) // 2. Run parameterized simulation (w/o invariants) _, simParams, simErr := simulation.SimulateFromSeed( - b, ioutil.Discard, gapp.BaseApp, simapp.AppStateFn(gapp.Codec(), gapp.sm), - testAndRunTxs(gapp, config), gapp.ModuleAccountAddrs(), config, + b, ioutil.Discard, app.BaseApp, simapp.AppStateFn(app.Codec(), app.sm), + SimulationOperations(app, app.Codec(), config), + app.ModuleAccountAddrs(), config, ) // export state and params before the simulation error is checked if config.ExportStatePath != "" { - if err := ExportStateToJSON(gapp, config.ExportStatePath); err != nil { + if err := ExportStateToJSON(app, config.ExportStatePath); err != nil { fmt.Println(err) b.Fail() } @@ -675,13 +481,13 @@ func BenchmarkInvariants(b *testing.B) { b.FailNow() } - ctx := gapp.NewContext(true, abci.Header{Height: gapp.LastBlockHeight() + 1}) + ctx := app.NewContext(true, abci.Header{Height: app.LastBlockHeight() + 1}) // 3. Benchmark each invariant separately // // NOTE: We use the crisis keeper as it has all the invariants registered with // their respective metadata which makes it useful for testing/benchmarking. - for _, cr := range gapp.crisisKeeper.Routes() { + for _, cr := range app.crisisKeeper.Routes() { cr := cr b.Run(fmt.Sprintf("%s/%s", cr.ModuleName, cr.Route), func(b *testing.B) { if res, stop := cr.Invar(ctx); stop { diff --git a/app/utils.go b/app/utils.go index 6f9350e1b7f..f960b2d0c3f 100644 --- a/app/utils.go +++ b/app/utils.go @@ -1,20 +1,36 @@ -//nolint package app import ( "fmt" - "io" "io/ioutil" - "github.com/tendermint/tendermint/libs/log" - dbm "github.com/tendermint/tm-db" - - "github.com/cosmos/cosmos-sdk/baseapp" - bam "github.com/cosmos/cosmos-sdk/baseapp" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/staking" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/types/module" + "github.com/cosmos/cosmos-sdk/x/simulation" ) +// SimulationOperations retrieves the simulation params from the provided file path +// and returns all the modules weighted operations +func SimulationOperations(app *GaiaApp, cdc *codec.Codec, config simulation.Config) []simulation.WeightedOperation { + simState := module.SimulationState{ + AppParams: make(simulation.AppParams), + Cdc: cdc, + } + + if config.ParamsFile != "" { + bz, err := ioutil.ReadFile(config.ParamsFile) + if err != nil { + panic(err) + } + + app.cdc.MustUnmarshalJSON(bz, &simState.AppParams) + } + + simState.ParamChanges = app.sm.GenerateParamChanges(config.Seed) + simState.Contents = app.sm.GetProposalContents(simState) + return app.sm.WeightedOperations(simState) +} + // ExportStateToJSON util function to export the app state to JSON func ExportStateToJSON(app *GaiaApp, path string) error { fmt.Println("exporting app state...") @@ -25,14 +41,3 @@ func ExportStateToJSON(app *GaiaApp, path string) error { return ioutil.WriteFile(path, []byte(appState), 0644) } - -// NewGaiaAppUNSAFE is used for debugging purposes only. -// -// NOTE: to not use this function with non-test code -func NewGaiaAppUNSAFE(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest bool, - invCheckPeriod uint, baseAppOptions ...func(*baseapp.BaseApp), -) (gapp *GaiaApp, keyMain, keyStaking *sdk.KVStoreKey, stakingKeeper staking.Keeper) { - - gapp = NewGaiaApp(logger, db, traceStore, loadLatest, invCheckPeriod, baseAppOptions...) - return gapp, gapp.keys[bam.MainStoreKey], gapp.keys[staking.StoreKey], gapp.stakingKeeper -} diff --git a/go.mod b/go.mod index 69887ba4861..fb57292638e 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.13 require ( github.com/btcsuite/btcd v0.0.0-20190807005414-4063feeff79a // indirect - github.com/cosmos/cosmos-sdk v0.34.4-0.20191204151802-202cb9be0bc3 + github.com/cosmos/cosmos-sdk v0.34.4-0.20191205092954-722a633f5478 github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d // indirect github.com/golang/mock v1.3.1 // indirect github.com/onsi/ginkgo v1.8.0 // indirect diff --git a/go.sum b/go.sum index b3005eda520..467b87ac419 100644 --- a/go.sum +++ b/go.sum @@ -43,6 +43,10 @@ github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7 github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= github.com/cosmos/cosmos-sdk v0.34.4-0.20191204151802-202cb9be0bc3 h1:8kzgZVydSDaSbK2BbvR+D9ccrMN/VmV7WVvIEYjvMJo= github.com/cosmos/cosmos-sdk v0.34.4-0.20191204151802-202cb9be0bc3/go.mod h1:JWuSAxZmMgNmNsZBCTuFfMHeeAAJZDxTnAKrQeSJOdk= +github.com/cosmos/cosmos-sdk v0.34.4-0.20191204213837-40a38faed681 h1:muUv2wUIjMzJAF2XRpyoBxWsOasrOjzTvdXLChJHzjE= +github.com/cosmos/cosmos-sdk v0.34.4-0.20191204213837-40a38faed681/go.mod h1:JWuSAxZmMgNmNsZBCTuFfMHeeAAJZDxTnAKrQeSJOdk= +github.com/cosmos/cosmos-sdk v0.34.4-0.20191205092954-722a633f5478 h1:pf3kPFlxOfsXxATPFIwPoZ5N5IJ/VmZ2ebz7maE4CGc= +github.com/cosmos/cosmos-sdk v0.34.4-0.20191205092954-722a633f5478/go.mod h1:JWuSAxZmMgNmNsZBCTuFfMHeeAAJZDxTnAKrQeSJOdk= github.com/cosmos/go-bip39 v0.0.0-20180618194314-52158e4697b8/go.mod h1:tSxLoYXyBmiFeKpvmq4dzayMdCjCnu8uqmCysIGBT2Y= github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d h1:49RLWk1j44Xu4fjHb6JFYmeUnDORVwHNkDxaQ0ctCVU= github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d/go.mod h1:tSxLoYXyBmiFeKpvmq4dzayMdCjCnu8uqmCysIGBT2Y=