-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
R4R: Simulation framework, including staking simulation #1620
Conversation
excited |
Codecov Report
@@ 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 |
Caught a bug already! #1630 |
p.s. merging |
cmd/gaia/simulation/sim_test.go
Outdated
@@ -0,0 +1,21 @@ | |||
package simulation |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm - clarify the problem?
x/stake/simulation/sim_test.go
Outdated
} | ||
|
||
// 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 { |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
d833c2d
to
47b805d
Compare
47b805d
to
2c0cd73
Compare
Not the finalized version of randomized simulation (see wishlist in first comment), but this can be reviewed & merged now to avoid blocking any other |
This is technically breaking as it includes a quick fix for #1402. |
…lk-down-proof-of-stake
cmd/gaia/app/sim_test.go
Outdated
simulationEnvSeed = "GAIA_SIMULATION_SEED" | ||
simulationEnvKeys = "GAIA_SIMULATION_KEYS" | ||
simulationEnvBlocks = "GAIA_SIMULATION_BLOCKS" | ||
simulationEnvBlockSize = "GAIA_SIMULATION_BLOCK_SIZE" |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm probably more into using test flags (https://stackoverflow.com/questions/47045445/idiomatic-way-to-pass-variables-to-test-cases-in-golang)
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
COOL!
x/stake/simulation/msgs.go
Outdated
description := stake.Description{ | ||
Moniker: simulation.RandStringOfLength(r, 10), | ||
} | ||
key := keys[r.Intn(len(keys))] |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
x/stake/simulation/msgs.go
Outdated
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())))) |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, updated.
x/stake/simulation/msgs.go
Outdated
denom := params.BondDenom | ||
loose := sdk.ZeroInt() | ||
mapp.AccountMapper.IterateAccounts(ctx, func(acc auth.Account) bool { | ||
balance := sdk.NewInt(int64(r.Intn(1000000))) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, updated.
There was a problem hiding this 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
ddd8d84
to
ee29e10
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Woot!
Closes #1647
How to run the simulation:
make test_sim
. Added to CI astest_sim
.Not yet implemented (for a future PR):