Merge PR #2660: simulation: Make validator choice use validator set
* simulation: Make validator choice use validator set This also had to change the default seed, since with the previous one it actually got into a state where there were no validators left bonded, lol. This also changes Unbond msgs from failing with almost 100% probability to now only failing with 33% probability. Thus more of the state machine is getting tested!
This commit is contained in:
commit
f4d3e65ef0
2
Makefile
2
Makefile
|
@ -169,7 +169,7 @@ test_sim_gaia_nondeterminism:
|
||||||
|
|
||||||
test_sim_gaia_fast:
|
test_sim_gaia_fast:
|
||||||
@echo "Running quick Gaia simulation. This may take several minutes..."
|
@echo "Running quick Gaia simulation. This may take several minutes..."
|
||||||
@go test ./cmd/gaia/app -run TestFullGaiaSimulation -SimulationEnabled=true -SimulationNumBlocks=500 -SimulationBlockSize=200 -SimulationCommit=true -SimulationSeed=9 -v -timeout 24h
|
@go test ./cmd/gaia/app -run TestFullGaiaSimulation -SimulationEnabled=true -SimulationNumBlocks=500 -SimulationBlockSize=200 -SimulationCommit=true -SimulationSeed=10 -v -timeout 24h
|
||||||
|
|
||||||
test_sim_gaia_multi_seed:
|
test_sim_gaia_multi_seed:
|
||||||
@echo "Running multi-seed Gaia simulation. This may take awhile!"
|
@echo "Running multi-seed Gaia simulation. This may take awhile!"
|
||||||
|
|
|
@ -41,7 +41,8 @@ IMPROVEMENTS
|
||||||
|
|
||||||
* SDK
|
* SDK
|
||||||
- #2573 [x/distribution] add accum invariance
|
- #2573 [x/distribution] add accum invariance
|
||||||
- \#1924 [simulation] Use a transition matrix for block size
|
- \#1924 [x/mock/simulation] Use a transition matrix for block size
|
||||||
|
- \#2660 [x/mock/simulation] Staking transactions get tested far more frequently
|
||||||
- #2610 [x/stake] Block redelegation to and from the same validator
|
- #2610 [x/stake] Block redelegation to and from the same validator
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ package simulation
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math/big"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
|
@ -71,6 +72,12 @@ func RandomAmount(r *rand.Rand, max sdk.Int) sdk.Int {
|
||||||
return sdk.NewInt(int64(r.Intn(int(max.Int64()))))
|
return sdk.NewInt(int64(r.Intn(int(max.Int64()))))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RandomDecAmount generates a random decimal amount
|
||||||
|
func RandomDecAmount(r *rand.Rand, max sdk.Dec) sdk.Dec {
|
||||||
|
randInt := big.NewInt(0).Rand(r, max.Int)
|
||||||
|
return sdk.NewDecFromBigIntWithPrec(randInt, sdk.Precision)
|
||||||
|
}
|
||||||
|
|
||||||
// RandomAccounts generates n random accounts
|
// RandomAccounts generates n random accounts
|
||||||
func RandomAccounts(r *rand.Rand, n int) []Account {
|
func RandomAccounts(r *rand.Rand, n int) []Account {
|
||||||
accs := make([]Account, n)
|
accs := make([]Account, n)
|
||||||
|
|
|
@ -3,6 +3,7 @@ package keeper
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"math/rand"
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
@ -215,3 +216,17 @@ func validatorByPowerIndexExists(k Keeper, ctx sdk.Context, power []byte) bool {
|
||||||
store := ctx.KVStore(k.storeKey)
|
store := ctx.KVStore(k.storeKey)
|
||||||
return store.Has(power)
|
return store.Has(power)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RandomValidator returns a random validator given access to the keeper and ctx
|
||||||
|
func RandomValidator(r *rand.Rand, keeper Keeper, ctx sdk.Context) types.Validator {
|
||||||
|
vals := keeper.GetAllValidators(ctx)
|
||||||
|
i := r.Intn(len(vals))
|
||||||
|
return vals[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
// RandomBondedValidator returns a random bonded validator given access to the keeper and ctx
|
||||||
|
func RandomBondedValidator(r *rand.Rand, keeper Keeper, ctx sdk.Context) types.Validator {
|
||||||
|
vals := keeper.GetBondedValidatorsByPower(ctx)
|
||||||
|
i := r.Intn(len(vals))
|
||||||
|
return vals[i]
|
||||||
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"github.com/cosmos/cosmos-sdk/x/mock"
|
"github.com/cosmos/cosmos-sdk/x/mock"
|
||||||
"github.com/cosmos/cosmos-sdk/x/mock/simulation"
|
"github.com/cosmos/cosmos-sdk/x/mock/simulation"
|
||||||
"github.com/cosmos/cosmos-sdk/x/stake"
|
"github.com/cosmos/cosmos-sdk/x/stake"
|
||||||
|
"github.com/cosmos/cosmos-sdk/x/stake/keeper"
|
||||||
abci "github.com/tendermint/tendermint/abci/types"
|
abci "github.com/tendermint/tendermint/abci/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -87,8 +88,8 @@ func SimulateMsgEditValidator(k stake.Keeper) simulation.Operation {
|
||||||
maxCommission := sdk.NewInt(10)
|
maxCommission := sdk.NewInt(10)
|
||||||
newCommissionRate := sdk.NewDecWithPrec(simulation.RandomAmount(r, maxCommission).Int64(), 1)
|
newCommissionRate := sdk.NewDecWithPrec(simulation.RandomAmount(r, maxCommission).Int64(), 1)
|
||||||
|
|
||||||
acc := simulation.RandomAcc(r, accs)
|
val := keeper.RandomValidator(r, k, ctx)
|
||||||
address := sdk.ValAddress(acc.Address)
|
address := val.GetOperator()
|
||||||
msg := stake.MsgEditValidator{
|
msg := stake.MsgEditValidator{
|
||||||
Description: description,
|
Description: description,
|
||||||
ValidatorAddr: address,
|
ValidatorAddr: address,
|
||||||
|
@ -118,8 +119,8 @@ func SimulateMsgDelegate(m auth.AccountKeeper, k stake.Keeper) simulation.Operat
|
||||||
action string, fOp []simulation.FutureOperation, err error) {
|
action string, fOp []simulation.FutureOperation, err error) {
|
||||||
|
|
||||||
denom := k.GetParams(ctx).BondDenom
|
denom := k.GetParams(ctx).BondDenom
|
||||||
validatorAcc := simulation.RandomAcc(r, accs)
|
val := keeper.RandomValidator(r, k, ctx)
|
||||||
validatorAddress := sdk.ValAddress(validatorAcc.Address)
|
validatorAddress := val.GetOperator()
|
||||||
delegatorAcc := simulation.RandomAcc(r, accs)
|
delegatorAcc := simulation.RandomAcc(r, accs)
|
||||||
delegatorAddress := delegatorAcc.Address
|
delegatorAddress := delegatorAcc.Address
|
||||||
amount := m.GetAccount(ctx, delegatorAddress).GetCoins().AmountOf(denom)
|
amount := m.GetAccount(ctx, delegatorAddress).GetCoins().AmountOf(denom)
|
||||||
|
@ -155,25 +156,26 @@ func SimulateMsgBeginUnbonding(m auth.AccountKeeper, k stake.Keeper) simulation.
|
||||||
accs []simulation.Account, event func(string)) (
|
accs []simulation.Account, event func(string)) (
|
||||||
action string, fOp []simulation.FutureOperation, err error) {
|
action string, fOp []simulation.FutureOperation, err error) {
|
||||||
|
|
||||||
denom := k.GetParams(ctx).BondDenom
|
|
||||||
validatorAcc := simulation.RandomAcc(r, accs)
|
|
||||||
validatorAddress := sdk.ValAddress(validatorAcc.Address)
|
|
||||||
delegatorAcc := simulation.RandomAcc(r, accs)
|
delegatorAcc := simulation.RandomAcc(r, accs)
|
||||||
delegatorAddress := delegatorAcc.Address
|
delegatorAddress := delegatorAcc.Address
|
||||||
amount := m.GetAccount(ctx, delegatorAddress).GetCoins().AmountOf(denom)
|
delegations := k.GetAllDelegatorDelegations(ctx, delegatorAddress)
|
||||||
if amount.GT(sdk.ZeroInt()) {
|
if len(delegations) == 0 {
|
||||||
amount = simulation.RandomAmount(r, amount)
|
return "no-operation", nil, nil
|
||||||
}
|
}
|
||||||
if amount.Equal(sdk.ZeroInt()) {
|
delegation := delegations[r.Intn(len(delegations))]
|
||||||
|
|
||||||
|
numShares := simulation.RandomDecAmount(r, delegation.Shares)
|
||||||
|
if numShares.Equal(sdk.ZeroDec()) {
|
||||||
return "no-operation", nil, nil
|
return "no-operation", nil, nil
|
||||||
}
|
}
|
||||||
msg := stake.MsgBeginUnbonding{
|
msg := stake.MsgBeginUnbonding{
|
||||||
DelegatorAddr: delegatorAddress,
|
DelegatorAddr: delegatorAddress,
|
||||||
ValidatorAddr: validatorAddress,
|
ValidatorAddr: delegation.ValidatorAddr,
|
||||||
SharesAmount: sdk.NewDecFromInt(amount),
|
SharesAmount: numShares,
|
||||||
}
|
}
|
||||||
if msg.ValidateBasic() != nil {
|
if msg.ValidateBasic() != nil {
|
||||||
return "", nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes())
|
return "", nil, fmt.Errorf("expected msg to pass ValidateBasic: %s, got error %v",
|
||||||
|
msg.GetSignBytes(), msg.ValidateBasic())
|
||||||
}
|
}
|
||||||
ctx, write := ctx.CacheContext()
|
ctx, write := ctx.CacheContext()
|
||||||
result := handler(ctx, msg)
|
result := handler(ctx, msg)
|
||||||
|
@ -194,10 +196,10 @@ func SimulateMsgBeginRedelegate(m auth.AccountKeeper, k stake.Keeper) simulation
|
||||||
action string, fOp []simulation.FutureOperation, err error) {
|
action string, fOp []simulation.FutureOperation, err error) {
|
||||||
|
|
||||||
denom := k.GetParams(ctx).BondDenom
|
denom := k.GetParams(ctx).BondDenom
|
||||||
sourceValidatorAcc := simulation.RandomAcc(r, accs)
|
srcVal := keeper.RandomValidator(r, k, ctx)
|
||||||
sourceValidatorAddress := sdk.ValAddress(sourceValidatorAcc.Address)
|
srcValidatorAddress := srcVal.GetOperator()
|
||||||
destValidatorAcc := simulation.RandomAcc(r, accs)
|
destVal := keeper.RandomValidator(r, k, ctx)
|
||||||
destValidatorAddress := sdk.ValAddress(destValidatorAcc.Address)
|
destValidatorAddress := destVal.GetOperator()
|
||||||
delegatorAcc := simulation.RandomAcc(r, accs)
|
delegatorAcc := simulation.RandomAcc(r, accs)
|
||||||
delegatorAddress := delegatorAcc.Address
|
delegatorAddress := delegatorAcc.Address
|
||||||
// TODO
|
// TODO
|
||||||
|
@ -210,7 +212,7 @@ func SimulateMsgBeginRedelegate(m auth.AccountKeeper, k stake.Keeper) simulation
|
||||||
}
|
}
|
||||||
msg := stake.MsgBeginRedelegate{
|
msg := stake.MsgBeginRedelegate{
|
||||||
DelegatorAddr: delegatorAddress,
|
DelegatorAddr: delegatorAddress,
|
||||||
ValidatorSrcAddr: sourceValidatorAddress,
|
ValidatorSrcAddr: srcValidatorAddress,
|
||||||
ValidatorDstAddr: destValidatorAddress,
|
ValidatorDstAddr: destValidatorAddress,
|
||||||
SharesAmount: sdk.NewDecFromInt(amount),
|
SharesAmount: sdk.NewDecFromInt(amount),
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue