cosmos-sdk/x/slashing/simulation/operations.go

98 lines
3.3 KiB
Go
Raw Normal View History

update simulation operations to use BaseApp (#4946) * 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 <colinaxner@berkeley.edu> * 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 * Update x/bank/simulation/operations.go Co-Authored-By: colin axner <colinaxner@berkeley.edu> * Update simapp/helpers/test_helpers.go Co-Authored-By: colin axner <colinaxner@berkeley.edu> * 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 * changelog * Update x/distribution/simulation/genesis.go Co-Authored-By: Alexander Bezobchuk <alexanderbez@users.noreply.github.com> * 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 * x/slashing/simulation/operations.go linting * x/staking/simulation/operations.go linting * update operations format * x/bank/simulation/operations.go linting * x/distribution/simulation/operations.go linting * x/staking/simulation/operations.go linting * start changes: make bank simulate send multiple coins, code cleanup * fix nondeterminism bug * fix txsiglimit err * fix multisend bug * simplify simulation, cleanup opt privkey args * make slashing test invalid unjail msgs * Update simapp/state.go * golangCI changes
2019-10-23 02:14:45 -07:00
package simulation
import (
"errors"
"math/rand"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/simapp/helpers"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/simulation"
"github.com/cosmos/cosmos-sdk/x/slashing/internal/keeper"
"github.com/cosmos/cosmos-sdk/x/slashing/internal/types"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
)
// SimulateMsgUnjail generates a MsgUnjail with random values
// nolint: funlen
func SimulateMsgUnjail(ak types.AccountKeeper, k keeper.Keeper, sk stakingkeeper.Keeper) simulation.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
accs []simulation.Account, chainID string,
) (simulation.OperationMsg, []simulation.FutureOperation, error) {
validator, ok := stakingkeeper.RandomValidator(r, sk, ctx)
if !ok {
return simulation.NoOpMsg(types.ModuleName), nil, nil // skip
}
simAccount, found := simulation.FindAccount(accs, sdk.AccAddress(validator.GetOperator()))
if !found {
return simulation.NoOpMsg(types.ModuleName), nil, nil // skip
}
if !validator.IsJailed() {
// TODO: due to this condition this message is almost, if not always, skipped !
return simulation.NoOpMsg(types.ModuleName), nil, nil
}
consAddr := sdk.ConsAddress(validator.GetConsPubKey().Address())
info, found := k.GetValidatorSigningInfo(ctx, consAddr)
if !found {
return simulation.NoOpMsg(types.ModuleName), nil, nil // skip
}
selfDel := sk.Delegation(ctx, simAccount.Address, validator.GetOperator())
if selfDel == nil {
return simulation.NoOpMsg(types.ModuleName), nil, nil // skip
}
account := ak.GetAccount(ctx, sdk.AccAddress(validator.GetOperator()))
fees, err := simulation.RandomFees(r, ctx, account.SpendableCoins(ctx.BlockTime()))
if err != nil {
return simulation.NoOpMsg(types.ModuleName), nil, err
}
msg := types.NewMsgUnjail(validator.GetOperator())
tx := helpers.GenTx(
[]sdk.Msg{msg},
fees,
chainID,
[]uint64{account.GetAccountNumber()},
[]uint64{account.GetSequence()},
simAccount.PrivKey,
)
res := app.Deliver(tx)
// result should fail if:
// - validator cannot be unjailed due to tombstone
// - validator is still in jailed period
// - self delegation too low
if info.Tombstoned ||
ctx.BlockHeader().Time.Before(info.JailedUntil) ||
validator.TokensFromShares(selfDel.GetShares()).TruncateInt().LT(validator.GetMinSelfDelegation()) {
if res.IsOK() {
if info.Tombstoned {
return simulation.NewOperationMsg(msg, true, ""), nil, errors.New("validator should not have been unjailed if validator tombstoned")
}
if ctx.BlockHeader().Time.Before(info.JailedUntil) {
return simulation.NewOperationMsg(msg, true, ""), nil, errors.New("validator unjailed while validator still in jail period")
}
if validator.TokensFromShares(selfDel.GetShares()).TruncateInt().LT(validator.GetMinSelfDelegation()) {
return simulation.NewOperationMsg(msg, true, ""), nil, errors.New("validator unjailed even though self-delegation too low")
}
}
// msg failed as expected
return simulation.NewOperationMsg(msg, false, ""), nil, nil
}
if !res.IsOK() {
return simulation.NoOpMsg(types.ModuleName), nil, errors.New(res.Log)
}
return simulation.NewOperationMsg(msg, true, ""), nil, nil
}
}