Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R4R: Simulation framework, including staking simulation #1620

Merged
merged 45 commits into from
Jul 19, 2018

Conversation

cwgoes
Copy link
Contributor

@cwgoes cwgoes commented Jul 10, 2018

Closes #1647

How to run the simulation: make test_sim. Added to CI as test_sim.

Not yet implemented (for a future PR):

  • Simulate Tendermint behavior (validator set updates, block signing in ABCI, evidence)
  • Simulated slashing (messages / tick function)
  • Simulated time and configuration parameters (e.g. unbonding period) to match
  • Updated all relevant documentation in docs
  • Updated all code comments where relevant
  • Wrote tests
  • Updated CHANGELOG.md
  • Updated Gaia/Examples
  • Squashed all commits, uses message "Merge pull request #XYZ: [title]" (coding standards)
  • Added appropriate labels to PR (ex. wip, ready-for-review, docs)

@rigelrozanski
Copy link
Contributor

excited

@codecov
Copy link

codecov bot commented Jul 10, 2018

Codecov Report

Merging #1620 into develop will decrease coverage by 1.38%.
The diff coverage is 10.71%.

@@             Coverage Diff             @@
##           develop    #1620      +/-   ##
===========================================
- Coverage    64.16%   62.77%   -1.39%     
===========================================
  Files          115      122       +7     
  Lines         6837     7125     +288     
===========================================
+ Hits          4387     4473      +86     
- Misses        2196     2390     +194     
- Partials       254      262       +8

@ebuchman ebuchman changed the base branch from develop to master July 11, 2018 00:29
@cwgoes
Copy link
Contributor Author

cwgoes commented Jul 11, 2018

Caught a bug already! #1630

@cwgoes cwgoes changed the title WIP: Randomized testing for staking/slashing WIP: Simulation framework, including staking/slashing simulation Jul 11, 2018
@rigelrozanski
Copy link
Contributor

p.s. merging rigel/remove-global-shares seems to fix tests.

@@ -0,0 +1,21 @@
package simulation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++ on this as a new package

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately, turns out we need to access keepers, which are unexported

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm - clarify the problem?

}

// SupplyInvariants checks that the total supply reflects all held loose tokens, bonded tokens, and unbonding delegations
func SupplyInvariants(ck bank.Keeper, k stake.Keeper) mock.Invariant {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this file could be broken out a bit from sim_test.go into Invariants.go, msgs.go, simulation_test.go?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@cwgoes cwgoes force-pushed the cwgoes/a-random-walk-down-proof-of-stake branch from d833c2d to 47b805d Compare July 18, 2018 08:00
@cwgoes cwgoes force-pushed the cwgoes/a-random-walk-down-proof-of-stake branch from 47b805d to 2c0cd73 Compare July 18, 2018 08:00
@cwgoes cwgoes changed the title WIP: Simulation framework, including staking/slashing simulation Simulation framework, including staking/slashing simulation Jul 18, 2018
@cwgoes
Copy link
Contributor Author

cwgoes commented Jul 18, 2018

Not the finalized version of randomized simulation (see wishlist in first comment), but this can be reviewed & merged now to avoid blocking any other x/mock refactors.

@cwgoes cwgoes changed the title Simulation framework, including staking/slashing simulation Simulation framework, including staking simulation Jul 18, 2018
@cwgoes cwgoes added the T: API Breaking Breaking changes that impact APIs and the SDK only (not state machine). label Jul 18, 2018
@cwgoes
Copy link
Contributor Author

cwgoes commented Jul 18, 2018

This is technically breaking as it includes a quick fix for #1402.

@cwgoes cwgoes changed the title Simulation framework, including staking simulation R4R: Simulation framework, including staking simulation Jul 18, 2018
@rigelrozanski
Copy link
Contributor

@cwgoes can you highlight the changes made for #1402? didn't see any changes to the keys.go

@cwgoes
Copy link
Contributor Author

cwgoes commented Jul 18, 2018

@cwgoes can you highlight the changes made for #1402? didn't see any changes to the keys.go

I didn't change keys.go, just blocked having more than one unbonding delegation to the same validator.

https://github.com/cosmos/cosmos-sdk/pull/1620/files#diff-b905817409b4c0b71e9adffb102682b4R317

simulationEnvSeed = "GAIA_SIMULATION_SEED"
simulationEnvKeys = "GAIA_SIMULATION_KEYS"
simulationEnvBlocks = "GAIA_SIMULATION_BLOCKS"
simulationEnvBlockSize = "GAIA_SIMULATION_BLOCK_SIZE"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the thinking of using environment variables to pass information to the tests?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, flags seem slightly cleaner - switched.

[]simulation.RandSetup{},
[]simulation.Invariant{
banksim.NonnegativeBalanceInvariant(app.accountMapper),
stakesim.AllInvariants(app.coinKeeper, app.stakeKeeper, app.accountMapper),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

COOL!

description := stake.Description{
Moniker: simulation.RandStringOfLength(r, 10),
}
key := keys[r.Intn(len(keys))]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm bonkers, but I think due to the nature of this type of compact statement, our code would be more clear if we expanded this to a function to separate out concerns a bit.

key := RandomKey(r, keys) 

func RandomKey(r *rand.Rand, keys []crypto.PrivKey) key {
    return keys[r.Intn(
        len(keys)
    )]
}

I've always found it a bit irritating reading tests with the rand function because of lines like this, to separate out would make my mind and heart at ease

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(obv. applies to all statements like this in this PR)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, that's cleaner. Updated.

address := sdk.AccAddress(pubkey.Address())
amount := m.GetAccount(ctx, address).GetCoins().AmountOf(denom)
if amount.GT(sdk.ZeroInt()) {
amount = sdk.NewInt(int64(r.Intn(int(amount.Int64()))))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here how about:

key := RandomAmount(r, amount) 

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(obv. applies to all statements like this in this PR)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, updated.

denom := params.BondDenom
loose := sdk.ZeroInt()
mapp.AccountMapper.IterateAccounts(ctx, func(acc auth.Account) bool {
balance := sdk.NewInt(int64(r.Intn(1000000)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would use RandomAmount as outlined above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, updated.

Copy link
Contributor

@rigelrozanski rigelrozanski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Old comments addressed

Lookin' good, just a few final points to address before I think we should merge. See comments

@cwgoes cwgoes force-pushed the cwgoes/a-random-walk-down-proof-of-stake branch from ddd8d84 to ee29e10 Compare July 19, 2018 06:48
@cwgoes cwgoes dismissed rigelrozanski’s stale review July 19, 2018 06:49

Comments addressed.

Copy link
Contributor

@rigelrozanski rigelrozanski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woot!

@rigelrozanski rigelrozanski merged commit a054532 into develop Jul 19, 2018
@rigelrozanski rigelrozanski deleted the cwgoes/a-random-walk-down-proof-of-stake branch July 19, 2018 22:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: API Breaking Breaking changes that impact APIs and the SDK only (not state machine).
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants