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"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/mock/simulation"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/stake"
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SimulateMsgCreateValidator
|
2018-10-20 23:22:48 -07:00
|
|
|
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.NewInt(10)
|
|
|
|
commission := stake.NewCommissionMsg(
|
|
|
|
sdk.NewDecWithPrec(simulation.RandomAmount(r, maxCommission).Int64(), 1),
|
|
|
|
sdk.NewDecWithPrec(simulation.RandomAmount(r, maxCommission).Int64(), 1),
|
|
|
|
sdk.NewDecWithPrec(simulation.RandomAmount(r, maxCommission).Int64(), 1),
|
|
|
|
)
|
|
|
|
|
|
|
|
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()) {
|
2018-08-27 14:27:00 -07:00
|
|
|
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 {
|
2018-09-09 08:34:09 -07:00
|
|
|
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)
|
2018-07-18 00:37:14 -07:00
|
|
|
action = fmt.Sprintf("TestMsgCreateValidator: ok %v, msg %s", result.IsOK(), msg.GetSignBytes())
|
2018-08-27 14:27:00 -07:00
|
|
|
return action, nil, nil
|
2018-07-17 15:04:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SimulateMsgEditValidator
|
2018-08-26 18:04:52 -07:00
|
|
|
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
|
|
|
|
|
|
|
maxCommission := sdk.NewInt(10)
|
|
|
|
newCommissionRate := sdk.NewDecWithPrec(simulation.RandomAmount(r, maxCommission).Int64(), 1)
|
|
|
|
|
|
|
|
acc := simulation.RandomAcc(r, accs)
|
|
|
|
address := sdk.ValAddress(acc.Address)
|
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 {
|
2018-09-09 08:34:09 -07:00
|
|
|
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()))
|
2018-07-18 00:37:14 -07:00
|
|
|
action = fmt.Sprintf("TestMsgEditValidator: ok %v, msg %s", result.IsOK(), msg.GetSignBytes())
|
2018-08-27 14:27:00 -07:00
|
|
|
return action, nil, nil
|
2018-07-17 15:04:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SimulateMsgDelegate
|
2018-10-20 23:22:48 -07:00
|
|
|
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
|
2018-09-25 14:43:26 -07:00
|
|
|
validatorAcc := simulation.RandomAcc(r, accs)
|
|
|
|
validatorAddress := sdk.ValAddress(validatorAcc.Address)
|
|
|
|
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()) {
|
2018-08-27 14:27:00 -07:00
|
|
|
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 {
|
2018-09-09 08:34:09 -07:00
|
|
|
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()))
|
2018-07-18 00:37:14 -07:00
|
|
|
action = fmt.Sprintf("TestMsgDelegate: ok %v, msg %s", result.IsOK(), msg.GetSignBytes())
|
2018-08-27 14:27:00 -07:00
|
|
|
return action, nil, nil
|
2018-07-17 15:04:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SimulateMsgBeginUnbonding
|
2018-10-20 23:22:48 -07:00
|
|
|
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-07-17 15:04:10 -07:00
|
|
|
denom := k.GetParams(ctx).BondDenom
|
2018-09-25 14:43:26 -07:00
|
|
|
validatorAcc := simulation.RandomAcc(r, accs)
|
|
|
|
validatorAddress := sdk.ValAddress(validatorAcc.Address)
|
|
|
|
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()) {
|
2018-08-27 14:27:00 -07:00
|
|
|
return "no-operation", nil, nil
|
2018-07-17 15:04:10 -07:00
|
|
|
}
|
|
|
|
msg := stake.MsgBeginUnbonding{
|
|
|
|
DelegatorAddr: delegatorAddress,
|
|
|
|
ValidatorAddr: validatorAddress,
|
2018-08-14 17:15:02 -07:00
|
|
|
SharesAmount: sdk.NewDecFromInt(amount),
|
2018-07-17 15:04:10 -07:00
|
|
|
}
|
2018-08-29 23:02:15 -07:00
|
|
|
if msg.ValidateBasic() != nil {
|
2018-09-09 08:34:09 -07:00
|
|
|
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/MsgBeginUnbonding/%v", result.IsOK()))
|
2018-07-18 00:37:14 -07:00
|
|
|
action = fmt.Sprintf("TestMsgBeginUnbonding: ok %v, msg %s", result.IsOK(), msg.GetSignBytes())
|
2018-08-27 14:27:00 -07:00
|
|
|
return action, nil, nil
|
2018-07-17 15:04:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SimulateMsgBeginRedelegate
|
2018-10-20 23:22:48 -07:00
|
|
|
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
|
2018-09-25 14:43:26 -07:00
|
|
|
sourceValidatorAcc := simulation.RandomAcc(r, accs)
|
|
|
|
sourceValidatorAddress := sdk.ValAddress(sourceValidatorAcc.Address)
|
|
|
|
destValidatorAcc := simulation.RandomAcc(r, accs)
|
|
|
|
destValidatorAddress := sdk.ValAddress(destValidatorAcc.Address)
|
|
|
|
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()) {
|
2018-08-27 14:27:00 -07:00
|
|
|
return "no-operation", nil, nil
|
2018-07-17 15:04:10 -07:00
|
|
|
}
|
|
|
|
msg := stake.MsgBeginRedelegate{
|
|
|
|
DelegatorAddr: delegatorAddress,
|
|
|
|
ValidatorSrcAddr: sourceValidatorAddress,
|
|
|
|
ValidatorDstAddr: destValidatorAddress,
|
2018-08-14 17:15:02 -07:00
|
|
|
SharesAmount: sdk.NewDecFromInt(amount),
|
2018-07-17 15:04:10 -07:00
|
|
|
}
|
2018-08-29 23:02:15 -07:00
|
|
|
if msg.ValidateBasic() != nil {
|
2018-09-09 08:34:09 -07:00
|
|
|
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())
|
2018-08-27 14:27:00 -07:00
|
|
|
return action, nil, nil
|
2018-07-17 15:04:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-17 22:50:04 -07:00
|
|
|
// Setup
|
2018-08-31 15:22:37 -07:00
|
|
|
// nolint: errcheck
|
2018-07-17 22:50:04 -07:00
|
|
|
func Setup(mapp *mock.App, k stake.Keeper) simulation.RandSetup {
|
2018-09-25 14:43:26 -07:00
|
|
|
return func(r *rand.Rand, accs []simulation.Account) {
|
2018-07-17 15:04:10 -07:00
|
|
|
ctx := mapp.NewContext(false, abci.Header{})
|
2018-08-16 08:36:15 -07:00
|
|
|
gen := stake.DefaultGenesisState()
|
|
|
|
stake.InitGenesis(ctx, k, gen)
|
2018-07-17 15:04:10 -07:00
|
|
|
params := k.GetParams(ctx)
|
|
|
|
denom := params.BondDenom
|
|
|
|
loose := sdk.ZeroInt()
|
2018-10-20 23:22:48 -07:00
|
|
|
mapp.AccountKeeper.IterateAccounts(ctx, func(acc auth.Account) bool {
|
2018-07-18 23:47:54 -07:00
|
|
|
balance := simulation.RandomAmount(r, sdk.NewInt(1000000))
|
2018-07-30 17:09:50 -07:00
|
|
|
acc.SetCoins(acc.GetCoins().Plus(sdk.Coins{sdk.NewCoin(denom, balance)}))
|
2018-10-20 23:22:48 -07:00
|
|
|
mapp.AccountKeeper.SetAccount(ctx, acc)
|
2018-07-17 15:04:10 -07:00
|
|
|
loose = loose.Add(balance)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
pool := k.GetPool(ctx)
|
2018-08-14 17:15:02 -07:00
|
|
|
pool.LooseTokens = pool.LooseTokens.Add(sdk.NewDec(loose.Int64()))
|
2018-07-17 15:04:10 -07:00
|
|
|
k.SetPool(ctx, pool)
|
|
|
|
}
|
|
|
|
}
|