cosmos-sdk/x/stake/simulation/msgs.go

229 lines
7.4 KiB
Go
Raw Normal View History

2018-07-17 15:04:10 -07:00
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/stake"
"github.com/cosmos/cosmos-sdk/x/stake/keeper"
2018-07-17 15:04:10 -07:00
)
// SimulateMsgCreateValidator
func SimulateMsgCreateValidator(m auth.AccountKeeper, k stake.Keeper) simulation.Operation {
2018-08-30 00:13:31 -07:00
handler := stake.NewHandler(k)
2018-10-05 17:32:06 -07:00
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
accs []simulation.Account, event func(string)) (
action string, fOp []simulation.FutureOperation, err error) {
2018-08-26 21:38:31 -07:00
2018-07-17 15:04:10 -07:00
denom := k.GetParams(ctx).BondDenom
description := stake.Description{
Moniker: simulation.RandStringOfLength(r, 10),
}
2018-09-25 14:43:26 -07:00
maxCommission := sdk.NewDecWithPrec(r.Int63n(1000), 3)
2018-09-25 14:43:26 -07:00
commission := stake.NewCommissionMsg(
simulation.RandomDecAmount(r, maxCommission),
maxCommission,
simulation.RandomDecAmount(r, maxCommission),
2018-09-25 14:43:26 -07:00
)
acc := simulation.RandomAcc(r, accs)
address := sdk.ValAddress(acc.Address)
amount := m.GetAccount(ctx, acc.Address).GetCoins().AmountOf(denom)
2018-07-17 15:04:10 -07:00
if amount.GT(sdk.ZeroInt()) {
2018-07-18 23:47:54 -07:00
amount = simulation.RandomAmount(r, amount)
2018-07-17 15:04:10 -07:00
}
2018-09-25 14:43:26 -07:00
2018-07-17 15:04:10 -07:00
if amount.Equal(sdk.ZeroInt()) {
return "no-operation", nil, nil
2018-07-17 15:04:10 -07:00
}
2018-09-25 14:43:26 -07:00
2018-07-17 15:04:10 -07:00
msg := stake.MsgCreateValidator{
Description: description,
2018-09-25 14:43:26 -07:00
Commission: commission,
2018-07-17 15:04:10 -07:00
ValidatorAddr: address,
2018-09-25 14:43:26 -07:00
DelegatorAddr: acc.Address,
PubKey: acc.PubKey,
2018-07-30 17:09:50 -07:00
Delegation: sdk.NewCoin(denom, amount),
2018-07-17 15:04:10 -07:00
}
2018-09-25 14:43:26 -07:00
2018-08-29 23:02:15 -07:00
if msg.ValidateBasic() != nil {
return "", nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes())
2018-08-29 23:02:15 -07:00
}
2018-09-25 14:43:26 -07:00
2018-07-17 15:04:10 -07:00
ctx, write := ctx.CacheContext()
2018-08-30 00:13:31 -07:00
result := handler(ctx, msg)
2018-07-17 15:04:10 -07:00
if result.IsOK() {
write()
}
2018-09-25 14:43:26 -07:00
2018-07-17 16:27:51 -07:00
event(fmt.Sprintf("stake/MsgCreateValidator/%v", result.IsOK()))
2018-09-25 14:43:26 -07:00
2018-07-17 15:04:10 -07:00
// 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
2018-07-17 15:04:10 -07:00
}
}
// SimulateMsgEditValidator
func SimulateMsgEditValidator(k stake.Keeper) simulation.Operation {
2018-08-30 00:13:31 -07:00
handler := stake.NewHandler(k)
2018-10-05 17:32:06 -07:00
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
accs []simulation.Account, event func(string)) (
action string, fOp []simulation.FutureOperation, err error) {
2018-08-26 21:38:31 -07:00
2018-07-17 15:04:10 -07:00
description := stake.Description{
Moniker: simulation.RandStringOfLength(r, 10),
Identity: simulation.RandStringOfLength(r, 10),
Website: simulation.RandStringOfLength(r, 10),
Details: simulation.RandStringOfLength(r, 10),
}
2018-09-25 14:43:26 -07:00
val := keeper.RandomValidator(r, k, ctx)
address := val.GetOperator()
newCommissionRate := simulation.RandomDecAmount(r, val.Commission.MaxRate)
2018-07-17 15:04:10 -07:00
msg := stake.MsgEditValidator{
2018-09-25 14:43:26 -07:00
Description: description,
ValidatorAddr: address,
CommissionRate: &newCommissionRate,
2018-07-17 15:04:10 -07:00
}
2018-09-25 14:43:26 -07:00
2018-08-29 23:02:15 -07:00
if msg.ValidateBasic() != nil {
return "", nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes())
2018-08-29 23:02:15 -07:00
}
2018-09-25 14:43:26 -07:00
2018-07-17 15:04:10 -07:00
ctx, write := ctx.CacheContext()
2018-08-30 00:13:31 -07:00
result := handler(ctx, msg)
2018-07-17 15:04:10 -07:00
if result.IsOK() {
write()
}
2018-07-17 16:27:51 -07:00
event(fmt.Sprintf("stake/MsgEditValidator/%v", result.IsOK()))
action = fmt.Sprintf("TestMsgEditValidator: ok %v, msg %s", result.IsOK(), msg.GetSignBytes())
return action, nil, nil
2018-07-17 15:04:10 -07:00
}
}
// SimulateMsgDelegate
func SimulateMsgDelegate(m auth.AccountKeeper, k stake.Keeper) simulation.Operation {
2018-08-30 00:13:31 -07:00
handler := stake.NewHandler(k)
2018-10-05 17:32:06 -07:00
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
accs []simulation.Account, event func(string)) (
action string, fOp []simulation.FutureOperation, err error) {
2018-08-26 21:38:31 -07:00
2018-07-17 15:04:10 -07:00
denom := k.GetParams(ctx).BondDenom
val := keeper.RandomValidator(r, k, ctx)
validatorAddress := val.GetOperator()
2018-09-25 14:43:26 -07:00
delegatorAcc := simulation.RandomAcc(r, accs)
delegatorAddress := delegatorAcc.Address
2018-07-17 15:04:10 -07:00
amount := m.GetAccount(ctx, delegatorAddress).GetCoins().AmountOf(denom)
if amount.GT(sdk.ZeroInt()) {
2018-07-18 23:47:54 -07:00
amount = simulation.RandomAmount(r, amount)
2018-07-17 15:04:10 -07:00
}
if amount.Equal(sdk.ZeroInt()) {
return "no-operation", nil, nil
2018-07-17 15:04:10 -07:00
}
msg := stake.MsgDelegate{
DelegatorAddr: delegatorAddress,
ValidatorAddr: validatorAddress,
2018-07-30 17:09:50 -07:00
Delegation: sdk.NewCoin(denom, amount),
2018-07-17 15:04:10 -07:00
}
2018-08-29 23:02:15 -07:00
if msg.ValidateBasic() != nil {
return "", nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes())
2018-08-29 23:02:15 -07:00
}
2018-07-17 15:04:10 -07:00
ctx, write := ctx.CacheContext()
2018-08-30 00:13:31 -07:00
result := handler(ctx, msg)
2018-07-17 15:04:10 -07:00
if result.IsOK() {
write()
}
2018-07-17 16:27:51 -07:00
event(fmt.Sprintf("stake/MsgDelegate/%v", result.IsOK()))
action = fmt.Sprintf("TestMsgDelegate: ok %v, msg %s", result.IsOK(), msg.GetSignBytes())
return action, nil, nil
2018-07-17 15:04:10 -07:00
}
}
// SimulateMsgBeginUnbonding
func SimulateMsgBeginUnbonding(m auth.AccountKeeper, k stake.Keeper) simulation.Operation {
2018-08-30 00:13:31 -07:00
handler := stake.NewHandler(k)
2018-10-05 17:32:06 -07:00
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
accs []simulation.Account, event func(string)) (
action string, fOp []simulation.FutureOperation, err error) {
2018-08-26 21:38:31 -07:00
2018-09-25 14:43:26 -07:00
delegatorAcc := simulation.RandomAcc(r, accs)
delegatorAddress := delegatorAcc.Address
delegations := k.GetAllDelegatorDelegations(ctx, delegatorAddress)
if len(delegations) == 0 {
return "no-operation", nil, nil
2018-07-17 15:04:10 -07:00
}
delegation := delegations[r.Intn(len(delegations))]
numShares := simulation.RandomDecAmount(r, delegation.Shares)
if numShares.Equal(sdk.ZeroDec()) {
return "no-operation", nil, nil
2018-07-17 15:04:10 -07:00
}
msg := stake.MsgBeginUnbonding{
DelegatorAddr: delegatorAddress,
ValidatorAddr: delegation.ValidatorAddr,
SharesAmount: numShares,
2018-07-17 15:04:10 -07:00
}
2018-08-29 23:02:15 -07:00
if msg.ValidateBasic() != nil {
return "", nil, fmt.Errorf("expected msg to pass ValidateBasic: %s, got error %v",
msg.GetSignBytes(), msg.ValidateBasic())
2018-08-29 23:02:15 -07:00
}
2018-07-17 15:04:10 -07:00
ctx, write := ctx.CacheContext()
2018-08-30 00:13:31 -07:00
result := handler(ctx, msg)
2018-07-17 15:04:10 -07:00
if result.IsOK() {
write()
}
2018-07-17 16:27:51 -07:00
event(fmt.Sprintf("stake/MsgBeginUnbonding/%v", result.IsOK()))
action = fmt.Sprintf("TestMsgBeginUnbonding: ok %v, msg %s", result.IsOK(), msg.GetSignBytes())
return action, nil, nil
2018-07-17 15:04:10 -07:00
}
}
// SimulateMsgBeginRedelegate
func SimulateMsgBeginRedelegate(m auth.AccountKeeper, k stake.Keeper) simulation.Operation {
2018-08-30 00:13:31 -07:00
handler := stake.NewHandler(k)
2018-10-05 17:32:06 -07:00
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
accs []simulation.Account, event func(string)) (
action string, fOp []simulation.FutureOperation, err error) {
2018-08-26 21:38:31 -07:00
2018-07-17 15:04:10 -07:00
denom := k.GetParams(ctx).BondDenom
srcVal := keeper.RandomValidator(r, k, ctx)
srcValidatorAddress := srcVal.GetOperator()
destVal := keeper.RandomValidator(r, k, ctx)
destValidatorAddress := destVal.GetOperator()
2018-09-25 14:43:26 -07:00
delegatorAcc := simulation.RandomAcc(r, accs)
delegatorAddress := delegatorAcc.Address
2018-07-17 15:04:10 -07:00
// TODO
amount := m.GetAccount(ctx, delegatorAddress).GetCoins().AmountOf(denom)
if amount.GT(sdk.ZeroInt()) {
2018-07-18 23:47:54 -07:00
amount = simulation.RandomAmount(r, amount)
2018-07-17 15:04:10 -07:00
}
if amount.Equal(sdk.ZeroInt()) {
return "no-operation", nil, nil
2018-07-17 15:04:10 -07:00
}
msg := stake.MsgBeginRedelegate{
DelegatorAddr: delegatorAddress,
ValidatorSrcAddr: srcValidatorAddress,
2018-07-17 15:04:10 -07:00
ValidatorDstAddr: destValidatorAddress,
SharesAmount: sdk.NewDecFromInt(amount),
2018-07-17 15:04:10 -07:00
}
2018-08-29 23:02:15 -07:00
if msg.ValidateBasic() != nil {
return "", nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes())
2018-08-29 23:02:15 -07:00
}
2018-07-17 15:04:10 -07:00
ctx, write := ctx.CacheContext()
2018-08-30 00:13:31 -07:00
result := handler(ctx, msg)
2018-07-17 15:04:10 -07:00
if result.IsOK() {
write()
}
2018-07-17 16:27:51 -07:00
event(fmt.Sprintf("stake/MsgBeginRedelegate/%v", result.IsOK()))
2018-07-17 15:04:10 -07:00
action = fmt.Sprintf("TestMsgBeginRedelegate: %s", msg.GetSignBytes())
return action, nil, nil
2018-07-17 15:04:10 -07:00
}
}