231 lines
7.5 KiB
Go
231 lines
7.5 KiB
Go
package simulation
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand"
|
|
|
|
"github.com/cosmos/cosmos-sdk/baseapp"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
|
"github.com/cosmos/cosmos-sdk/x/mock/simulation"
|
|
"github.com/cosmos/cosmos-sdk/x/staking"
|
|
"github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
|
)
|
|
|
|
const (
|
|
noOperation = "no-operation"
|
|
)
|
|
|
|
// SimulateMsgCreateValidator
|
|
func SimulateMsgCreateValidator(m auth.AccountKeeper, k staking.Keeper) simulation.Operation {
|
|
handler := staking.NewHandler(k)
|
|
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
|
|
accs []simulation.Account, event func(string)) (
|
|
action string, fOp []simulation.FutureOperation, err error) {
|
|
|
|
denom := k.GetParams(ctx).BondDenom
|
|
description := staking.Description{
|
|
Moniker: simulation.RandStringOfLength(r, 10),
|
|
}
|
|
|
|
maxCommission := sdk.NewDecWithPrec(r.Int63n(1000), 3)
|
|
commission := staking.NewCommissionMsg(
|
|
simulation.RandomDecAmount(r, maxCommission),
|
|
maxCommission,
|
|
simulation.RandomDecAmount(r, maxCommission),
|
|
)
|
|
|
|
acc := simulation.RandomAcc(r, accs)
|
|
address := sdk.ValAddress(acc.Address)
|
|
amount := m.GetAccount(ctx, acc.Address).GetCoins().AmountOf(denom)
|
|
if amount.GT(sdk.ZeroInt()) {
|
|
amount = simulation.RandomAmount(r, amount)
|
|
}
|
|
|
|
if amount.Equal(sdk.ZeroInt()) {
|
|
return noOperation, nil, nil
|
|
}
|
|
|
|
selfDelegation := sdk.NewCoin(denom, amount)
|
|
msg := staking.NewMsgCreateValidator(address, acc.PubKey,
|
|
selfDelegation, description, commission, sdk.OneInt())
|
|
|
|
if msg.ValidateBasic() != nil {
|
|
return "", nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes())
|
|
}
|
|
|
|
ctx, write := ctx.CacheContext()
|
|
result := handler(ctx, msg)
|
|
if result.IsOK() {
|
|
write()
|
|
}
|
|
|
|
event(fmt.Sprintf("staking/MsgCreateValidator/%v", result.IsOK()))
|
|
|
|
// require.True(t, result.IsOK(), "expected OK result but instead got %v", result)
|
|
action = fmt.Sprintf("TestMsgCreateValidator: ok %v, msg %s", result.IsOK(), msg.GetSignBytes())
|
|
return action, nil, nil
|
|
}
|
|
}
|
|
|
|
// SimulateMsgEditValidator
|
|
func SimulateMsgEditValidator(k staking.Keeper) simulation.Operation {
|
|
handler := staking.NewHandler(k)
|
|
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
|
|
accs []simulation.Account, event func(string)) (
|
|
action string, fOp []simulation.FutureOperation, err error) {
|
|
|
|
description := staking.Description{
|
|
Moniker: simulation.RandStringOfLength(r, 10),
|
|
Identity: simulation.RandStringOfLength(r, 10),
|
|
Website: simulation.RandStringOfLength(r, 10),
|
|
Details: simulation.RandStringOfLength(r, 10),
|
|
}
|
|
|
|
if len(k.GetAllValidators(ctx)) == 0 {
|
|
return noOperation, nil, nil
|
|
}
|
|
val := keeper.RandomValidator(r, k, ctx)
|
|
address := val.GetOperator()
|
|
newCommissionRate := simulation.RandomDecAmount(r, val.Commission.MaxRate)
|
|
|
|
msg := staking.NewMsgEditValidator(address, description, &newCommissionRate, nil)
|
|
|
|
if msg.ValidateBasic() != nil {
|
|
return "", nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes())
|
|
}
|
|
ctx, write := ctx.CacheContext()
|
|
result := handler(ctx, msg)
|
|
if result.IsOK() {
|
|
write()
|
|
}
|
|
event(fmt.Sprintf("staking/MsgEditValidator/%v", result.IsOK()))
|
|
action = fmt.Sprintf("TestMsgEditValidator: ok %v, msg %s", result.IsOK(), msg.GetSignBytes())
|
|
return action, nil, nil
|
|
}
|
|
}
|
|
|
|
// SimulateMsgDelegate
|
|
func SimulateMsgDelegate(m auth.AccountKeeper, k staking.Keeper) simulation.Operation {
|
|
handler := staking.NewHandler(k)
|
|
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
|
|
accs []simulation.Account, event func(string)) (
|
|
action string, fOp []simulation.FutureOperation, err error) {
|
|
|
|
denom := k.GetParams(ctx).BondDenom
|
|
if len(k.GetAllValidators(ctx)) == 0 {
|
|
return noOperation, nil, nil
|
|
}
|
|
val := keeper.RandomValidator(r, k, ctx)
|
|
validatorAddress := val.GetOperator()
|
|
delegatorAcc := simulation.RandomAcc(r, accs)
|
|
delegatorAddress := delegatorAcc.Address
|
|
amount := m.GetAccount(ctx, delegatorAddress).GetCoins().AmountOf(denom)
|
|
if amount.GT(sdk.ZeroInt()) {
|
|
amount = simulation.RandomAmount(r, amount)
|
|
}
|
|
if amount.Equal(sdk.ZeroInt()) {
|
|
return noOperation, nil, nil
|
|
}
|
|
|
|
msg := staking.NewMsgDelegate(
|
|
delegatorAddress, validatorAddress, sdk.NewCoin(denom, amount))
|
|
|
|
if msg.ValidateBasic() != nil {
|
|
return "", nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes())
|
|
}
|
|
ctx, write := ctx.CacheContext()
|
|
result := handler(ctx, msg)
|
|
if result.IsOK() {
|
|
write()
|
|
}
|
|
event(fmt.Sprintf("staking/MsgDelegate/%v", result.IsOK()))
|
|
action = fmt.Sprintf("TestMsgDelegate: ok %v, msg %s", result.IsOK(), msg.GetSignBytes())
|
|
return action, nil, nil
|
|
}
|
|
}
|
|
|
|
// SimulateMsgUndelegate
|
|
func SimulateMsgUndelegate(m auth.AccountKeeper, k staking.Keeper) simulation.Operation {
|
|
handler := staking.NewHandler(k)
|
|
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
|
|
accs []simulation.Account, event func(string)) (
|
|
action string, fOp []simulation.FutureOperation, err error) {
|
|
|
|
delegatorAcc := simulation.RandomAcc(r, accs)
|
|
delegatorAddress := delegatorAcc.Address
|
|
delegations := k.GetAllDelegatorDelegations(ctx, delegatorAddress)
|
|
if len(delegations) == 0 {
|
|
return noOperation, nil, nil
|
|
}
|
|
delegation := delegations[r.Intn(len(delegations))]
|
|
|
|
numShares := simulation.RandomDecAmount(r, delegation.Shares)
|
|
if numShares.Equal(sdk.ZeroDec()) {
|
|
return noOperation, nil, nil
|
|
}
|
|
msg := staking.MsgUndelegate{
|
|
DelegatorAddress: delegatorAddress,
|
|
ValidatorAddress: delegation.ValidatorAddress,
|
|
SharesAmount: numShares,
|
|
}
|
|
if msg.ValidateBasic() != nil {
|
|
return "", nil, fmt.Errorf("expected msg to pass ValidateBasic: %s, got error %v",
|
|
msg.GetSignBytes(), msg.ValidateBasic())
|
|
}
|
|
ctx, write := ctx.CacheContext()
|
|
result := handler(ctx, msg)
|
|
if result.IsOK() {
|
|
write()
|
|
}
|
|
event(fmt.Sprintf("staking/MsgUndelegate/%v", result.IsOK()))
|
|
action = fmt.Sprintf("TestMsgUndelegate: ok %v, msg %s", result.IsOK(), msg.GetSignBytes())
|
|
return action, nil, nil
|
|
}
|
|
}
|
|
|
|
// SimulateMsgBeginRedelegate
|
|
func SimulateMsgBeginRedelegate(m auth.AccountKeeper, k staking.Keeper) simulation.Operation {
|
|
handler := staking.NewHandler(k)
|
|
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
|
|
accs []simulation.Account, event func(string)) (
|
|
action string, fOp []simulation.FutureOperation, err error) {
|
|
|
|
denom := k.GetParams(ctx).BondDenom
|
|
if len(k.GetAllValidators(ctx)) == 0 {
|
|
return noOperation, nil, nil
|
|
}
|
|
srcVal := keeper.RandomValidator(r, k, ctx)
|
|
srcValidatorAddress := srcVal.GetOperator()
|
|
destVal := keeper.RandomValidator(r, k, ctx)
|
|
destValidatorAddress := destVal.GetOperator()
|
|
delegatorAcc := simulation.RandomAcc(r, accs)
|
|
delegatorAddress := delegatorAcc.Address
|
|
// TODO
|
|
amount := m.GetAccount(ctx, delegatorAddress).GetCoins().AmountOf(denom)
|
|
if amount.GT(sdk.ZeroInt()) {
|
|
amount = simulation.RandomAmount(r, amount)
|
|
}
|
|
if amount.Equal(sdk.ZeroInt()) {
|
|
return noOperation, nil, nil
|
|
}
|
|
msg := staking.MsgBeginRedelegate{
|
|
DelegatorAddress: delegatorAddress,
|
|
ValidatorSrcAddress: srcValidatorAddress,
|
|
ValidatorDstAddress: destValidatorAddress,
|
|
SharesAmount: amount.ToDec(),
|
|
}
|
|
if msg.ValidateBasic() != nil {
|
|
return "", nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes())
|
|
}
|
|
ctx, write := ctx.CacheContext()
|
|
result := handler(ctx, msg)
|
|
if result.IsOK() {
|
|
write()
|
|
}
|
|
event(fmt.Sprintf("staking/MsgBeginRedelegate/%v", result.IsOK()))
|
|
action = fmt.Sprintf("TestMsgBeginRedelegate: %s", msg.GetSignBytes())
|
|
return action, nil, nil
|
|
}
|
|
}
|