Skip to content

Commit

Permalink
Sim refactor 3: move weighted operations to modules (#4869)
Browse files Browse the repository at this point in the history
* move GenesisState generators to modules

* minor change on slashing genState generator

* move simulation params back to modules (#4839)

move simulation params back to modules (#4839)

* cleanup params

* various fixes

* move store decoders to modules

* fix

* module pattern

* split generators for param change

* param changes

* revert util pkg

* banksim

* compile

* update Decoders params

* fix

* address @colin-axner comments

* move weighted operations to modules

* cleanup

* cleanup

* Update cmd_test.go

* simulation manager

* mino fixes

* cleanup

* add GenerateGenesisState to simulation manager

* Apply suggestions from code review

Co-Authored-By: frog power 4000 <[email protected]>

* address @rigelrozanski comments

* changelog

* Apply suggestions from code review

Co-Authored-By: colin axner <[email protected]>

* restructure modules simulation pkgs

* remove cycle deps

* rename funcs and add missing params

* modularize simulator param changes

* build

* fix params keys

* make format

* various fixes

* fix tests

* minor updates to sim_test

* cleanup

* more cleanup

* modularize genesis generators

* minor cleanup

* remove cdc from generators

* remove cdc

* add get or generate

* fix non-determinism in simulation

* changelog and x/simulation godoc

* cleanup operations

* update operations to use baseapp

* updates and cleanup operations

* update operations

* restructure sim ops params

* rename sim /operations/msg.go to /operations.go

* move GenTx to a helper pkg to avoid circle deps

* rm msg.ValidateBasic

* changelog

* random fees; delete auth's DeductFees sim operation

* add chain-id for sig verification

* Update x/simulation/account.go

Co-Authored-By: colin axner <[email protected]>

* fix bank, gov and distr errors

* fix staking and slashing errors; increase prob for send enabled

* increase gas x10

* make format

* fix some distr and staking edge cases

* fix all edge cases

* golang ci

* rename acc vars; default no fees to 0stake

* cleanup; check for exchange rate and skip invalid ops

* fixes

* check for max entries

* add pubkey to genaccounts

* fix gov bug

* update staking sim ops

* fix small redelegation error

* fix small self delegation on unjail

* rm inf loop on random val/accs

* copy array

* add ok boolean to RandomValidator return values

* format

* build

* add WeightedOperations to AppModuleSimulation

* define each module proposals content as part of the module pattern

* Update x/bank/simulation/operations.go

Co-Authored-By: colin axner <[email protected]>

* Update simapp/helpers/test_helpers.go

Co-Authored-By: colin axner <[email protected]>

* address @colin-axner comments

* add genaccount pubkey validation

* fix test

* update operations and move RandomFees to x/simulation

* update gov ops

* address @alexanderbez comments

* avoid modifications to config

* reorder params

* modularized sim operations working

* changelog

* Update types/module/simulation.go

Co-Authored-By: frog power 4000 <[email protected]>

* Update x/simulation/params.go

Co-Authored-By: frog power 4000 <[email protected]>

* Update x/simulation/params.go

Co-Authored-By: frog power 4000 <[email protected]>

* update /types/module

* Update x/distribution/simulation/genesis.go

Co-Authored-By: Alexander Bezobchuk <[email protected]>

* remove named return values

* ensure all operations are simulated

* golangci

* add nolint

* disable whitespace and funlen linter

* disable godox

* add TODO on unjail

* update ops weights

* remove dup

* update godoc

* remove unused func

* build fixes

* move weights to the same file

* scopelint

* changelog

* address @AdityaSripal comments

* address @alexanderbez comments
  • Loading branch information
fedekunze authored Dec 5, 2019
1 parent 12ff486 commit 722a633
Show file tree
Hide file tree
Showing 30 changed files with 880 additions and 519 deletions.
9 changes: 6 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,15 @@ generalized genesis accounts through the `GenesisAccount` interface.
* (simulation) [\#4824](https://github.com/cosmos/cosmos-sdk/issues/4824) PrintAllInvariants flag will print all failed invariants
* (simulation) [\#4490](https://github.com/cosmos/cosmos-sdk/issues/4490) add `InitialBlockHeight` flag to resume a simulation from a given block
* Support exporting the simulation stats to a given JSON file
* (simulation) [\#4847](https://github.com/cosmos/cosmos-sdk/issues/4847), [\#4838](https://github.com/cosmos/cosmos-sdk/pull/4838) `SimApp` and simulation refactors
* (simulation) [\#4847](https://github.com/cosmos/cosmos-sdk/issues/4847), [\#4838](https://github.com/cosmos/cosmos-sdk/pull/4838) and [\#4869](https://github.com/cosmos/cosmos-sdk/pull/4869) `SimApp` and simulation refactors:
* Implement `SimulationManager` for executing modules' simulation functionalities in a modularized way
* Add `RegisterStoreDecoders` to the `SimulationManager` for decoding each module's types
* Add `GenerateGenesisStates` to the `SimulationManager` to generate a randomized `GenState` for each module
* Add `RandomizedParams` to the `SimulationManager` that registers each modules' parameters `Content` to simulate
`ParamChangeProposal`s
* Add `RandomizedParams` to the `SimulationManager` that registers each modules' parameters in order to
simulate `ParamChangeProposal`s' `Content`s
* Add `WeightedOperations` to the `SimulationManager` that define simulation operations (modules' `Msg`s) with their
respective weights (i.e chance of being simulated).
* Add `ProposalContents` to the `SimulationManager` to register each module's governance proposal `Content`s.
* (simulation) [\#4893](https://github.com/cosmos/cosmos-sdk/issues/4893) Change SimApp keepers to be public and add getter functions for keys and codec
* (simulation) [\#4906](https://github.com/cosmos/cosmos-sdk/issues/4906) Add simulation `Config` struct that wraps simulation flags
* (simulation) [\#4935](https://github.com/cosmos/cosmos-sdk/issues/4935) Update simulation to reflect a proper `ABCI` application without bypassing `BaseApp` semantics
Expand Down
13 changes: 7 additions & 6 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,10 @@ func NewSimApp(
bank.NewAppModule(app.BankKeeper, app.AccountKeeper),
crisis.NewAppModule(&app.CrisisKeeper),
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),
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),
Expand Down Expand Up @@ -260,11 +260,12 @@ func NewSimApp(
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),
staking.NewAppModule(app.StakingKeeper, app.AccountKeeper, app.SupplyKeeper),
slashing.NewAppModule(app.SlashingKeeper, app.StakingKeeper),
distr.NewAppModule(app.DistrKeeper, app.AccountKeeper, app.SupplyKeeper, app.StakingKeeper),
slashing.NewAppModule(app.SlashingKeeper, app.AccountKeeper, app.StakingKeeper),
params.NewAppModule(), // NOTE: only used for simulation to generate randomized param change proposals
)

app.sm.RegisterStoreDecoders()
Expand Down
21 changes: 2 additions & 19 deletions simapp/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,6 @@ package simapp

// Simulation parameter constants
const (
StakePerAccount = "stake_per_account"
InitiallyBondedValidators = "initially_bonded_validators"
OpWeightDeductFee = "op_weight_deduct_fee"
OpWeightMsgSend = "op_weight_msg_send"
OpWeightMsgMultiSend = "op_weight_msg_multisend"
OpWeightMsgSetWithdrawAddress = "op_weight_msg_set_withdraw_address"
OpWeightMsgWithdrawDelegationReward = "op_weight_msg_withdraw_delegation_reward"
OpWeightMsgWithdrawValidatorCommission = "op_weight_msg_withdraw_validator_commission"
OpWeightSubmitTextProposal = "op_weight_submit_text_proposal"
OpWeightSubmitCommunitySpendProposal = "op_weight_submit_community_spend_proposal"
OpWeightSubmitParamChangeProposal = "op_weight_submit_param_change_proposal"
OpWeightMsgDeposit = "op_weight_msg_deposit"
OpWeightMsgVote = "op_weight_msg_vote"
OpWeightMsgCreateValidator = "op_weight_msg_create_validator"
OpWeightMsgEditValidator = "op_weight_msg_edit_validator"
OpWeightMsgDelegate = "op_weight_msg_delegate"
OpWeightMsgUndelegate = "op_weight_msg_undelegate"
OpWeightMsgBeginRedelegate = "op_weight_msg_begin_redelegate"
OpWeightMsgUnjail = "op_weight_msg_unjail"
StakePerAccount = "stake_per_account"
InitiallyBondedValidators = "initially_bonded_validators"
)
19 changes: 19 additions & 0 deletions simapp/params/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
Package params defines the simulation parameters in the simapp.
It contains the default weights used for each transaction used on the module's
simulation. These weights define the chance for a transaction to be simulated at
any gived operation.
You can repace the default values for the weights by providing a params.json
file with the weights defined for each of the transaction operations:
{
"op_weight_msg_send": 60,
"op_weight_msg_delegate": 100,
}
In the example above, the `MsgSend` has 60% chance to be simulated, while the
`MsgDelegate` will always be simulated.
*/
package params
22 changes: 22 additions & 0 deletions simapp/params/weights.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package params

// Default simulation operation weights for messages and gov proposals
const (
DefaultWeightMsgSend int = 100
DefaultWeightMsgMultiSend int = 10
DefaultWeightMsgSetWithdrawAddress int = 50
DefaultWeightMsgWithdrawDelegationReward int = 50
DefaultWeightMsgWithdrawValidatorCommission int = 50
DefaultWeightMsgDeposit int = 100
DefaultWeightMsgVote int = 67
DefaultWeightMsgUnjail int = 100
DefaultWeightMsgCreateValidator int = 100
DefaultWeightMsgEditValidator int = 5
DefaultWeightMsgDelegate int = 100
DefaultWeightMsgUndelegate int = 100
DefaultWeightMsgBeginRedelegate int = 100

DefaultWeightCommunitySpendProposal int = 5
DefaultWeightTextProposal int = 5
DefaultWeightParamChangeProposal int = 5
)
6 changes: 4 additions & 2 deletions simapp/sim_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func BenchmarkFullAppSimulation(b *testing.B) {
// TODO: parameterize numbers, save for a later PR
_, simParams, simErr := simulation.SimulateFromSeed(
b, os.Stdout, app.BaseApp, AppStateFn(app.Codec(), app.sm),
testAndRunTxs(app, config), app.ModuleAccountAddrs(), config,
SimulationOperations(app, app.Codec(), config),
app.ModuleAccountAddrs(), config,
)

// export state and params before the simulation error is checked
Expand Down Expand Up @@ -86,7 +87,8 @@ func BenchmarkInvariants(b *testing.B) {
// 2. Run parameterized simulation (w/o invariants)
_, simParams, simErr := simulation.SimulateFromSeed(
b, ioutil.Discard, app.BaseApp, AppStateFn(app.Codec(), app.sm),
testAndRunTxs(app, config), app.ModuleAccountAddrs(), config,
SimulationOperations(app, app.Codec(), config),
app.ModuleAccountAddrs(), config,
)

// export state and params before the simulation error is checked
Expand Down
Loading

0 comments on commit 722a633

Please sign in to comment.