refactor: add util functions to reuse in simulations (#9456)
* add util functions to reuse in simulations * keeper interface cleanup, gov migrate * fix module name * fix sims * fix staking tests * introduce a txContext * fix feegrant * take in consideration * fix names * fix tests * add bank keeper where needed * add bank keeper where needed
This commit is contained in:
parent
f96811c0dd
commit
f1289d0808
|
@ -6,7 +6,6 @@ import (
|
||||||
|
|
||||||
"github.com/cosmos/cosmos-sdk/baseapp"
|
"github.com/cosmos/cosmos-sdk/baseapp"
|
||||||
"github.com/cosmos/cosmos-sdk/codec"
|
"github.com/cosmos/cosmos-sdk/codec"
|
||||||
"github.com/cosmos/cosmos-sdk/simapp/helpers"
|
|
||||||
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
|
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
|
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
|
||||||
|
@ -93,34 +92,24 @@ func SimulateMsgSetWithdrawAddress(ak types.AccountKeeper, bk types.BankKeeper,
|
||||||
account := ak.GetAccount(ctx, simAccount.Address)
|
account := ak.GetAccount(ctx, simAccount.Address)
|
||||||
spendable := bk.SpendableCoins(ctx, account.GetAddress())
|
spendable := bk.SpendableCoins(ctx, account.GetAddress())
|
||||||
|
|
||||||
fees, err := simtypes.RandomFees(r, ctx, spendable)
|
|
||||||
if err != nil {
|
|
||||||
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSetWithdrawAddress, "unable to generate fees"), nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
msg := types.NewMsgSetWithdrawAddress(simAccount.Address, simToAccount.Address)
|
msg := types.NewMsgSetWithdrawAddress(simAccount.Address, simToAccount.Address)
|
||||||
|
|
||||||
txGen := simappparams.MakeTestEncodingConfig().TxConfig
|
txCtx := simulation.OperationInput{
|
||||||
tx, err := helpers.GenTx(
|
R: r,
|
||||||
txGen,
|
App: app,
|
||||||
[]sdk.Msg{msg},
|
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
|
||||||
fees,
|
Cdc: nil,
|
||||||
helpers.DefaultGenTxGas,
|
Msg: msg,
|
||||||
chainID,
|
MsgType: msg.Type(),
|
||||||
[]uint64{account.GetAccountNumber()},
|
Context: ctx,
|
||||||
[]uint64{account.GetSequence()},
|
SimAccount: simAccount,
|
||||||
simAccount.PrivKey,
|
AccountKeeper: ak,
|
||||||
)
|
Bankkeeper: bk,
|
||||||
if err != nil {
|
ModuleName: types.ModuleName,
|
||||||
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
|
CoinsSpentInMsg: spendable,
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _, err = app.Deliver(txGen.TxEncoder(), tx)
|
return simulation.GenAndDeliverTxWithRandFees(txCtx)
|
||||||
if err != nil {
|
|
||||||
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -145,34 +134,24 @@ func SimulateMsgWithdrawDelegatorReward(ak types.AccountKeeper, bk types.BankKee
|
||||||
account := ak.GetAccount(ctx, simAccount.Address)
|
account := ak.GetAccount(ctx, simAccount.Address)
|
||||||
spendable := bk.SpendableCoins(ctx, account.GetAddress())
|
spendable := bk.SpendableCoins(ctx, account.GetAddress())
|
||||||
|
|
||||||
fees, err := simtypes.RandomFees(r, ctx, spendable)
|
|
||||||
if err != nil {
|
|
||||||
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgWithdrawDelegatorReward, "unable to generate fees"), nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
msg := types.NewMsgWithdrawDelegatorReward(simAccount.Address, validator.GetOperator())
|
msg := types.NewMsgWithdrawDelegatorReward(simAccount.Address, validator.GetOperator())
|
||||||
|
|
||||||
txGen := simappparams.MakeTestEncodingConfig().TxConfig
|
txCtx := simulation.OperationInput{
|
||||||
tx, err := helpers.GenTx(
|
R: r,
|
||||||
txGen,
|
App: app,
|
||||||
[]sdk.Msg{msg},
|
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
|
||||||
fees,
|
Cdc: nil,
|
||||||
helpers.DefaultGenTxGas,
|
Msg: msg,
|
||||||
chainID,
|
MsgType: msg.Type(),
|
||||||
[]uint64{account.GetAccountNumber()},
|
Context: ctx,
|
||||||
[]uint64{account.GetSequence()},
|
SimAccount: simAccount,
|
||||||
simAccount.PrivKey,
|
AccountKeeper: ak,
|
||||||
)
|
Bankkeeper: bk,
|
||||||
if err != nil {
|
ModuleName: types.ModuleName,
|
||||||
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
|
CoinsSpentInMsg: spendable,
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _, err = app.Deliver(txGen.TxEncoder(), tx)
|
return simulation.GenAndDeliverTxWithRandFees(txCtx)
|
||||||
if err != nil {
|
|
||||||
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -200,34 +179,24 @@ func SimulateMsgWithdrawValidatorCommission(ak types.AccountKeeper, bk types.Ban
|
||||||
account := ak.GetAccount(ctx, simAccount.Address)
|
account := ak.GetAccount(ctx, simAccount.Address)
|
||||||
spendable := bk.SpendableCoins(ctx, account.GetAddress())
|
spendable := bk.SpendableCoins(ctx, account.GetAddress())
|
||||||
|
|
||||||
fees, err := simtypes.RandomFees(r, ctx, spendable)
|
|
||||||
if err != nil {
|
|
||||||
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgWithdrawValidatorCommission, "unable to generate fees"), nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
msg := types.NewMsgWithdrawValidatorCommission(validator.GetOperator())
|
msg := types.NewMsgWithdrawValidatorCommission(validator.GetOperator())
|
||||||
|
|
||||||
txGen := simappparams.MakeTestEncodingConfig().TxConfig
|
txCtx := simulation.OperationInput{
|
||||||
tx, err := helpers.GenTx(
|
R: r,
|
||||||
txGen,
|
App: app,
|
||||||
[]sdk.Msg{msg},
|
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
|
||||||
fees,
|
Cdc: nil,
|
||||||
helpers.DefaultGenTxGas,
|
Msg: msg,
|
||||||
chainID,
|
MsgType: msg.Type(),
|
||||||
[]uint64{account.GetAccountNumber()},
|
Context: ctx,
|
||||||
[]uint64{account.GetSequence()},
|
SimAccount: simAccount,
|
||||||
simAccount.PrivKey,
|
AccountKeeper: ak,
|
||||||
)
|
Bankkeeper: bk,
|
||||||
if err != nil {
|
ModuleName: types.ModuleName,
|
||||||
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
|
CoinsSpentInMsg: spendable,
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _, err = app.Deliver(txGen.TxEncoder(), tx)
|
return simulation.GenAndDeliverTxWithRandFees(txCtx)
|
||||||
if err != nil {
|
|
||||||
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -262,26 +231,19 @@ func SimulateMsgFundCommunityPool(ak types.AccountKeeper, bk types.BankKeeper, k
|
||||||
}
|
}
|
||||||
|
|
||||||
msg := types.NewMsgFundCommunityPool(fundAmount, funder.Address)
|
msg := types.NewMsgFundCommunityPool(fundAmount, funder.Address)
|
||||||
txGen := simappparams.MakeTestEncodingConfig().TxConfig
|
|
||||||
tx, err := helpers.GenTx(
|
txCtx := simulation.OperationInput{
|
||||||
txGen,
|
App: app,
|
||||||
[]sdk.Msg{msg},
|
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
|
||||||
fees,
|
Cdc: nil,
|
||||||
helpers.DefaultGenTxGas,
|
Msg: msg,
|
||||||
chainID,
|
MsgType: msg.Type(),
|
||||||
[]uint64{account.GetAccountNumber()},
|
Context: ctx,
|
||||||
[]uint64{account.GetSequence()},
|
SimAccount: funder,
|
||||||
funder.PrivKey,
|
AccountKeeper: ak,
|
||||||
)
|
ModuleName: types.ModuleName,
|
||||||
if err != nil {
|
|
||||||
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _, err = app.Deliver(txGen.TxEncoder(), tx)
|
return simulation.GenAndDeliverTx(txCtx, fees)
|
||||||
if err != nil {
|
|
||||||
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@ import (
|
||||||
|
|
||||||
"github.com/cosmos/cosmos-sdk/baseapp"
|
"github.com/cosmos/cosmos-sdk/baseapp"
|
||||||
"github.com/cosmos/cosmos-sdk/codec"
|
"github.com/cosmos/cosmos-sdk/codec"
|
||||||
"github.com/cosmos/cosmos-sdk/simapp/helpers"
|
|
||||||
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
|
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
|
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
|
||||||
|
@ -77,12 +76,6 @@ func SimulateMsgGrantAllowance(ak feegrant.AccountKeeper, bk feegrant.BankKeeper
|
||||||
account := ak.GetAccount(ctx, granter.Address)
|
account := ak.GetAccount(ctx, granter.Address)
|
||||||
|
|
||||||
spendableCoins := bk.SpendableCoins(ctx, account.GetAddress())
|
spendableCoins := bk.SpendableCoins(ctx, account.GetAddress())
|
||||||
fees, err := simtypes.RandomFees(r, ctx, spendableCoins)
|
|
||||||
if err != nil {
|
|
||||||
return simtypes.NoOpMsg(feegrant.ModuleName, TypeMsgGrantAllowance, err.Error()), nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
spendableCoins = spendableCoins.Sub(fees)
|
|
||||||
if spendableCoins.Empty() {
|
if spendableCoins.Empty() {
|
||||||
return simtypes.NoOpMsg(feegrant.ModuleName, TypeMsgGrantAllowance, "unable to grant empty coins as SpendLimit"), nil, nil
|
return simtypes.NoOpMsg(feegrant.ModuleName, TypeMsgGrantAllowance, "unable to grant empty coins as SpendLimit"), nil, nil
|
||||||
}
|
}
|
||||||
|
@ -96,28 +89,23 @@ func SimulateMsgGrantAllowance(ak feegrant.AccountKeeper, bk feegrant.BankKeeper
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return simtypes.NoOpMsg(feegrant.ModuleName, TypeMsgGrantAllowance, err.Error()), nil, err
|
return simtypes.NoOpMsg(feegrant.ModuleName, TypeMsgGrantAllowance, err.Error()), nil, err
|
||||||
}
|
}
|
||||||
txGen := simappparams.MakeTestEncodingConfig().TxConfig
|
|
||||||
tx, err := helpers.GenTx(
|
|
||||||
txGen,
|
|
||||||
[]sdk.Msg{msg},
|
|
||||||
fees,
|
|
||||||
helpers.DefaultGenTxGas,
|
|
||||||
chainID,
|
|
||||||
[]uint64{account.GetAccountNumber()},
|
|
||||||
[]uint64{account.GetSequence()},
|
|
||||||
granter.PrivKey,
|
|
||||||
)
|
|
||||||
|
|
||||||
if err != nil {
|
txCtx := simulation.OperationInput{
|
||||||
return simtypes.NoOpMsg(feegrant.ModuleName, TypeMsgGrantAllowance, "unable to generate mock tx"), nil, err
|
R: r,
|
||||||
|
App: app,
|
||||||
|
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
|
||||||
|
Cdc: nil,
|
||||||
|
Msg: msg,
|
||||||
|
MsgType: TypeMsgGrantAllowance,
|
||||||
|
Context: ctx,
|
||||||
|
SimAccount: granter,
|
||||||
|
AccountKeeper: ak,
|
||||||
|
Bankkeeper: bk,
|
||||||
|
ModuleName: feegrant.ModuleName,
|
||||||
|
CoinsSpentInMsg: spendableCoins,
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _, err = app.Deliver(txGen.TxEncoder(), tx)
|
return simulation.GenAndDeliverTxWithRandFees(txCtx)
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return simtypes.NoOpMsg(feegrant.ModuleName, sdk.MsgTypeURL(msg), "unable to deliver tx"), nil, err
|
|
||||||
}
|
|
||||||
return simtypes.NewOperationMsg(msg, true, "", nil), nil, err
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -157,30 +145,24 @@ func SimulateMsgRevokeAllowance(ak feegrant.AccountKeeper, bk feegrant.BankKeepe
|
||||||
|
|
||||||
account := ak.GetAccount(ctx, granter.Address)
|
account := ak.GetAccount(ctx, granter.Address)
|
||||||
spendableCoins := bk.SpendableCoins(ctx, account.GetAddress())
|
spendableCoins := bk.SpendableCoins(ctx, account.GetAddress())
|
||||||
fees, err := simtypes.RandomFees(r, ctx, spendableCoins)
|
|
||||||
if err != nil {
|
|
||||||
return simtypes.NoOpMsg(feegrant.ModuleName, TypeMsgRevokeAllowance, err.Error()), nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
msg := feegrant.NewMsgRevokeAllowance(granterAddr, granteeAddr)
|
msg := feegrant.NewMsgRevokeAllowance(granterAddr, granteeAddr)
|
||||||
|
|
||||||
txGen := simappparams.MakeTestEncodingConfig().TxConfig
|
txCtx := simulation.OperationInput{
|
||||||
tx, err := helpers.GenTx(
|
R: r,
|
||||||
txGen,
|
App: app,
|
||||||
[]sdk.Msg{&msg},
|
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
|
||||||
fees,
|
Cdc: nil,
|
||||||
helpers.DefaultGenTxGas,
|
Msg: &msg,
|
||||||
chainID,
|
MsgType: TypeMsgRevokeAllowance,
|
||||||
[]uint64{account.GetAccountNumber()},
|
Context: ctx,
|
||||||
[]uint64{account.GetSequence()},
|
SimAccount: granter,
|
||||||
granter.PrivKey,
|
AccountKeeper: ak,
|
||||||
)
|
Bankkeeper: bk,
|
||||||
|
ModuleName: feegrant.ModuleName,
|
||||||
if err != nil {
|
CoinsSpentInMsg: spendableCoins,
|
||||||
return simtypes.NoOpMsg(feegrant.ModuleName, TypeMsgRevokeAllowance, err.Error()), nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _, err = app.Deliver(txGen.TxEncoder(), tx)
|
return simulation.GenAndDeliverTxWithRandFees(txCtx)
|
||||||
return simtypes.NewOperationMsg(&msg, true, "", nil), nil, err
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -241,26 +241,19 @@ func SimulateMsgDeposit(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Ke
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
txGen := simappparams.MakeTestEncodingConfig().TxConfig
|
txCtx := simulation.OperationInput{
|
||||||
tx, err := helpers.GenTx(
|
App: app,
|
||||||
txGen,
|
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
|
||||||
[]sdk.Msg{msg},
|
Cdc: nil,
|
||||||
fees,
|
Msg: msg,
|
||||||
helpers.DefaultGenTxGas,
|
MsgType: msg.Type(),
|
||||||
chainID,
|
Context: ctx,
|
||||||
[]uint64{account.GetAccountNumber()},
|
SimAccount: simAccount,
|
||||||
[]uint64{account.GetSequence()},
|
AccountKeeper: ak,
|
||||||
simAccount.PrivKey,
|
ModuleName: types.ModuleName,
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
|
|
||||||
}
|
|
||||||
_, _, err = app.Deliver(txGen.TxEncoder(), tx)
|
|
||||||
if err != nil {
|
|
||||||
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil
|
return simulation.GenAndDeliverTx(txCtx, fees)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -298,32 +291,22 @@ func operationSimulateMsgVote(ak types.AccountKeeper, bk types.BankKeeper, k kee
|
||||||
account := ak.GetAccount(ctx, simAccount.Address)
|
account := ak.GetAccount(ctx, simAccount.Address)
|
||||||
spendable := bk.SpendableCoins(ctx, account.GetAddress())
|
spendable := bk.SpendableCoins(ctx, account.GetAddress())
|
||||||
|
|
||||||
fees, err := simtypes.RandomFees(r, ctx, spendable)
|
txCtx := simulation.OperationInput{
|
||||||
if err != nil {
|
R: r,
|
||||||
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate fees"), nil, err
|
App: app,
|
||||||
|
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
|
||||||
|
Cdc: nil,
|
||||||
|
Msg: msg,
|
||||||
|
MsgType: msg.Type(),
|
||||||
|
Context: ctx,
|
||||||
|
SimAccount: simAccount,
|
||||||
|
AccountKeeper: ak,
|
||||||
|
Bankkeeper: bk,
|
||||||
|
ModuleName: types.ModuleName,
|
||||||
|
CoinsSpentInMsg: spendable,
|
||||||
}
|
}
|
||||||
|
|
||||||
txGen := simappparams.MakeTestEncodingConfig().TxConfig
|
return simulation.GenAndDeliverTxWithRandFees(txCtx)
|
||||||
tx, err := helpers.GenTx(
|
|
||||||
txGen,
|
|
||||||
[]sdk.Msg{msg},
|
|
||||||
fees,
|
|
||||||
helpers.DefaultGenTxGas,
|
|
||||||
chainID,
|
|
||||||
[]uint64{account.GetAccountNumber()},
|
|
||||||
[]uint64{account.GetSequence()},
|
|
||||||
simAccount.PrivKey,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, _, err = app.Deliver(txGen.TxEncoder(), tx)
|
|
||||||
if err != nil {
|
|
||||||
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -361,32 +344,22 @@ func operationSimulateMsgVoteWeighted(ak types.AccountKeeper, bk types.BankKeepe
|
||||||
account := ak.GetAccount(ctx, simAccount.Address)
|
account := ak.GetAccount(ctx, simAccount.Address)
|
||||||
spendable := bk.SpendableCoins(ctx, account.GetAddress())
|
spendable := bk.SpendableCoins(ctx, account.GetAddress())
|
||||||
|
|
||||||
fees, err := simtypes.RandomFees(r, ctx, spendable)
|
txCtx := simulation.OperationInput{
|
||||||
if err != nil {
|
R: r,
|
||||||
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate fees"), nil, err
|
App: app,
|
||||||
|
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
|
||||||
|
Cdc: nil,
|
||||||
|
Msg: msg,
|
||||||
|
MsgType: msg.Type(),
|
||||||
|
Context: ctx,
|
||||||
|
SimAccount: simAccount,
|
||||||
|
AccountKeeper: ak,
|
||||||
|
Bankkeeper: bk,
|
||||||
|
ModuleName: types.ModuleName,
|
||||||
|
CoinsSpentInMsg: spendable,
|
||||||
}
|
}
|
||||||
|
|
||||||
txGen := simappparams.MakeTestEncodingConfig().TxConfig
|
return simulation.GenAndDeliverTxWithRandFees(txCtx)
|
||||||
tx, err := helpers.GenTx(
|
|
||||||
txGen,
|
|
||||||
[]sdk.Msg{msg},
|
|
||||||
fees,
|
|
||||||
helpers.DefaultGenTxGas,
|
|
||||||
chainID,
|
|
||||||
[]uint64{account.GetAccountNumber()},
|
|
||||||
[]uint64{account.GetSequence()},
|
|
||||||
simAccount.PrivKey,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, _, err = app.Deliver(txGen.TxEncoder(), tx)
|
|
||||||
if err != nil {
|
|
||||||
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
package simulation
|
||||||
|
|
||||||
|
import (
|
||||||
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
"github.com/cosmos/cosmos-sdk/x/auth/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
// AccountKeeper defines the expected account keeper used for simulations (noalias)
|
||||||
|
type AccountKeeper interface {
|
||||||
|
GetAccount(ctx sdk.Context, addr sdk.AccAddress) types.AccountI
|
||||||
|
}
|
||||||
|
|
||||||
|
// BankKeeper defines the expected interface needed to retrieve account balances.
|
||||||
|
type BankKeeper interface {
|
||||||
|
SpendableCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins
|
||||||
|
}
|
|
@ -5,6 +5,13 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/cosmos/cosmos-sdk/baseapp"
|
||||||
|
"github.com/cosmos/cosmos-sdk/client"
|
||||||
|
"github.com/cosmos/cosmos-sdk/codec"
|
||||||
|
"github.com/cosmos/cosmos-sdk/simapp/helpers"
|
||||||
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getTestingMode(tb testing.TB) (testingMode bool, t *testing.T, b *testing.B) {
|
func getTestingMode(tb testing.TB) (testingMode bool, t *testing.T, b *testing.B) {
|
||||||
|
@ -53,3 +60,66 @@ func mustMarshalJSONIndent(o interface{}) []byte {
|
||||||
|
|
||||||
return bz
|
return bz
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OperationInput is a struct that holds all the needed values to generate a tx and deliver it
|
||||||
|
type OperationInput struct {
|
||||||
|
R *rand.Rand
|
||||||
|
App *baseapp.BaseApp
|
||||||
|
TxGen client.TxConfig
|
||||||
|
Cdc *codec.ProtoCodec
|
||||||
|
Msg sdk.Msg
|
||||||
|
MsgType string
|
||||||
|
CoinsSpentInMsg sdk.Coins
|
||||||
|
Context sdk.Context
|
||||||
|
SimAccount simtypes.Account
|
||||||
|
AccountKeeper AccountKeeper
|
||||||
|
Bankkeeper BankKeeper
|
||||||
|
ModuleName string
|
||||||
|
}
|
||||||
|
|
||||||
|
// GenAndDeliverTxWithRandFees generates a transaction with a random fee and delivers it.
|
||||||
|
func GenAndDeliverTxWithRandFees(txCtx OperationInput) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
|
||||||
|
account := txCtx.AccountKeeper.GetAccount(txCtx.Context, txCtx.SimAccount.Address)
|
||||||
|
spendable := txCtx.Bankkeeper.SpendableCoins(txCtx.Context, account.GetAddress())
|
||||||
|
|
||||||
|
var fees sdk.Coins
|
||||||
|
var err error
|
||||||
|
|
||||||
|
coins, hasNeg := spendable.SafeSub(txCtx.CoinsSpentInMsg)
|
||||||
|
if hasNeg {
|
||||||
|
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "message doesn't leave room for fees"), nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
fees, err = simtypes.RandomFees(txCtx.R, txCtx.Context, coins)
|
||||||
|
if err != nil {
|
||||||
|
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "unable to generate fees"), nil, err
|
||||||
|
}
|
||||||
|
return GenAndDeliverTx(txCtx, fees)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GenAndDeliverTx generates a transactions and delivers it.
|
||||||
|
func GenAndDeliverTx(txCtx OperationInput, fees sdk.Coins) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
|
||||||
|
account := txCtx.AccountKeeper.GetAccount(txCtx.Context, txCtx.SimAccount.Address)
|
||||||
|
tx, err := helpers.GenTx(
|
||||||
|
txCtx.TxGen,
|
||||||
|
[]sdk.Msg{txCtx.Msg},
|
||||||
|
fees,
|
||||||
|
helpers.DefaultGenTxGas,
|
||||||
|
txCtx.Context.ChainID(),
|
||||||
|
[]uint64{account.GetAccountNumber()},
|
||||||
|
[]uint64{account.GetSequence()},
|
||||||
|
txCtx.SimAccount.PrivKey,
|
||||||
|
)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "unable to generate mock tx"), nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, _, err = txCtx.App.Deliver(txCtx.TxGen.TxEncoder(), tx)
|
||||||
|
if err != nil {
|
||||||
|
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "unable to deliver tx"), nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return simtypes.NewOperationMsg(txCtx.Msg, true, "", txCtx.Cdc), nil, nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -6,7 +6,6 @@ import (
|
||||||
|
|
||||||
"github.com/cosmos/cosmos-sdk/baseapp"
|
"github.com/cosmos/cosmos-sdk/baseapp"
|
||||||
"github.com/cosmos/cosmos-sdk/codec"
|
"github.com/cosmos/cosmos-sdk/codec"
|
||||||
"github.com/cosmos/cosmos-sdk/simapp/helpers"
|
|
||||||
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
|
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
|
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
|
||||||
|
@ -152,27 +151,19 @@ func SimulateMsgCreateValidator(ak types.AccountKeeper, bk types.BankKeeper, k k
|
||||||
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to create CreateValidator message"), nil, err
|
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to create CreateValidator message"), nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
txGen := simappparams.MakeTestEncodingConfig().TxConfig
|
txCtx := simulation.OperationInput{
|
||||||
tx, err := helpers.GenTx(
|
App: app,
|
||||||
txGen,
|
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
|
||||||
[]sdk.Msg{msg},
|
Cdc: nil,
|
||||||
fees,
|
Msg: msg,
|
||||||
helpers.DefaultGenTxGas,
|
MsgType: msg.Type(),
|
||||||
chainID,
|
Context: ctx,
|
||||||
[]uint64{account.GetAccountNumber()},
|
SimAccount: simAccount,
|
||||||
[]uint64{account.GetSequence()},
|
AccountKeeper: ak,
|
||||||
simAccount.PrivKey,
|
ModuleName: types.ModuleName,
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _, err = app.Deliver(txGen.TxEncoder(), tx)
|
return simulation.GenAndDeliverTx(txCtx, fees)
|
||||||
if err != nil {
|
|
||||||
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -207,11 +198,6 @@ func SimulateMsgEditValidator(ak types.AccountKeeper, bk types.BankKeeper, k kee
|
||||||
account := ak.GetAccount(ctx, simAccount.Address)
|
account := ak.GetAccount(ctx, simAccount.Address)
|
||||||
spendable := bk.SpendableCoins(ctx, account.GetAddress())
|
spendable := bk.SpendableCoins(ctx, account.GetAddress())
|
||||||
|
|
||||||
fees, err := simtypes.RandomFees(r, ctx, spendable)
|
|
||||||
if err != nil {
|
|
||||||
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgEditValidator, "unable to generate fees"), nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
description := types.NewDescription(
|
description := types.NewDescription(
|
||||||
simtypes.RandStringOfLength(r, 10),
|
simtypes.RandStringOfLength(r, 10),
|
||||||
simtypes.RandStringOfLength(r, 10),
|
simtypes.RandStringOfLength(r, 10),
|
||||||
|
@ -222,27 +208,22 @@ func SimulateMsgEditValidator(ak types.AccountKeeper, bk types.BankKeeper, k kee
|
||||||
|
|
||||||
msg := types.NewMsgEditValidator(address, description, &newCommissionRate, nil)
|
msg := types.NewMsgEditValidator(address, description, &newCommissionRate, nil)
|
||||||
|
|
||||||
txGen := simappparams.MakeTestEncodingConfig().TxConfig
|
txCtx := simulation.OperationInput{
|
||||||
tx, err := helpers.GenTx(
|
R: r,
|
||||||
txGen,
|
App: app,
|
||||||
[]sdk.Msg{msg},
|
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
|
||||||
fees,
|
Cdc: nil,
|
||||||
helpers.DefaultGenTxGas,
|
Msg: msg,
|
||||||
chainID,
|
MsgType: msg.Type(),
|
||||||
[]uint64{account.GetAccountNumber()},
|
Context: ctx,
|
||||||
[]uint64{account.GetSequence()},
|
SimAccount: simAccount,
|
||||||
simAccount.PrivKey,
|
AccountKeeper: ak,
|
||||||
)
|
Bankkeeper: bk,
|
||||||
if err != nil {
|
ModuleName: types.ModuleName,
|
||||||
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
|
CoinsSpentInMsg: spendable,
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _, err = app.Deliver(txGen.TxEncoder(), tx)
|
return simulation.GenAndDeliverTxWithRandFees(txCtx)
|
||||||
if err != nil {
|
|
||||||
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -294,27 +275,19 @@ func SimulateMsgDelegate(ak types.AccountKeeper, bk types.BankKeeper, k keeper.K
|
||||||
|
|
||||||
msg := types.NewMsgDelegate(simAccount.Address, val.GetOperator(), bondAmt)
|
msg := types.NewMsgDelegate(simAccount.Address, val.GetOperator(), bondAmt)
|
||||||
|
|
||||||
txGen := simappparams.MakeTestEncodingConfig().TxConfig
|
txCtx := simulation.OperationInput{
|
||||||
tx, err := helpers.GenTx(
|
App: app,
|
||||||
txGen,
|
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
|
||||||
[]sdk.Msg{msg},
|
Cdc: nil,
|
||||||
fees,
|
Msg: msg,
|
||||||
helpers.DefaultGenTxGas,
|
MsgType: msg.Type(),
|
||||||
chainID,
|
Context: ctx,
|
||||||
[]uint64{account.GetAccountNumber()},
|
SimAccount: simAccount,
|
||||||
[]uint64{account.GetSequence()},
|
AccountKeeper: ak,
|
||||||
simAccount.PrivKey,
|
ModuleName: types.ModuleName,
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _, err = app.Deliver(txGen.TxEncoder(), tx)
|
return simulation.GenAndDeliverTx(txCtx, fees)
|
||||||
if err != nil {
|
|
||||||
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -378,32 +351,22 @@ func SimulateMsgUndelegate(ak types.AccountKeeper, bk types.BankKeeper, k keeper
|
||||||
account := ak.GetAccount(ctx, delAddr)
|
account := ak.GetAccount(ctx, delAddr)
|
||||||
spendable := bk.SpendableCoins(ctx, account.GetAddress())
|
spendable := bk.SpendableCoins(ctx, account.GetAddress())
|
||||||
|
|
||||||
fees, err := simtypes.RandomFees(r, ctx, spendable)
|
txCtx := simulation.OperationInput{
|
||||||
if err != nil {
|
R: r,
|
||||||
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate fees"), nil, err
|
App: app,
|
||||||
|
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
|
||||||
|
Cdc: nil,
|
||||||
|
Msg: msg,
|
||||||
|
MsgType: msg.Type(),
|
||||||
|
Context: ctx,
|
||||||
|
SimAccount: simAccount,
|
||||||
|
AccountKeeper: ak,
|
||||||
|
Bankkeeper: bk,
|
||||||
|
ModuleName: types.ModuleName,
|
||||||
|
CoinsSpentInMsg: spendable,
|
||||||
}
|
}
|
||||||
|
|
||||||
txGen := simappparams.MakeTestEncodingConfig().TxConfig
|
return simulation.GenAndDeliverTxWithRandFees(txCtx)
|
||||||
tx, err := helpers.GenTx(
|
|
||||||
txGen,
|
|
||||||
[]sdk.Msg{msg},
|
|
||||||
fees,
|
|
||||||
helpers.DefaultGenTxGas,
|
|
||||||
chainID,
|
|
||||||
[]uint64{account.GetAccountNumber()},
|
|
||||||
[]uint64{account.GetSequence()},
|
|
||||||
simAccount.PrivKey,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, _, err = app.Deliver(txGen.TxEncoder(), tx)
|
|
||||||
if err != nil {
|
|
||||||
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -485,36 +448,26 @@ func SimulateMsgBeginRedelegate(ak types.AccountKeeper, bk types.BankKeeper, k k
|
||||||
account := ak.GetAccount(ctx, delAddr)
|
account := ak.GetAccount(ctx, delAddr)
|
||||||
spendable := bk.SpendableCoins(ctx, account.GetAddress())
|
spendable := bk.SpendableCoins(ctx, account.GetAddress())
|
||||||
|
|
||||||
fees, err := simtypes.RandomFees(r, ctx, spendable)
|
|
||||||
if err != nil {
|
|
||||||
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgBeginRedelegate, "unable to generate fees"), nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
msg := types.NewMsgBeginRedelegate(
|
msg := types.NewMsgBeginRedelegate(
|
||||||
delAddr, srcAddr, destAddr,
|
delAddr, srcAddr, destAddr,
|
||||||
sdk.NewCoin(k.BondDenom(ctx), redAmt),
|
sdk.NewCoin(k.BondDenom(ctx), redAmt),
|
||||||
)
|
)
|
||||||
|
|
||||||
txGen := simappparams.MakeTestEncodingConfig().TxConfig
|
txCtx := simulation.OperationInput{
|
||||||
tx, err := helpers.GenTx(
|
R: r,
|
||||||
txGen,
|
App: app,
|
||||||
[]sdk.Msg{msg},
|
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
|
||||||
fees,
|
Cdc: nil,
|
||||||
helpers.DefaultGenTxGas,
|
Msg: msg,
|
||||||
chainID,
|
MsgType: msg.Type(),
|
||||||
[]uint64{account.GetAccountNumber()},
|
Context: ctx,
|
||||||
[]uint64{account.GetSequence()},
|
SimAccount: simAccount,
|
||||||
simAccount.PrivKey,
|
AccountKeeper: ak,
|
||||||
)
|
Bankkeeper: bk,
|
||||||
if err != nil {
|
ModuleName: types.ModuleName,
|
||||||
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
|
CoinsSpentInMsg: spendable,
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _, err = app.Deliver(txGen.TxEncoder(), tx)
|
return simulation.GenAndDeliverTxWithRandFees(txCtx)
|
||||||
if err != nil {
|
|
||||||
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -120,10 +120,10 @@ func TestSimulateMsgEditValidator(t *testing.T) {
|
||||||
|
|
||||||
require.True(t, operationMsg.OK)
|
require.True(t, operationMsg.OK)
|
||||||
require.Equal(t, "0.280623462081924936", msg.CommissionRate.String())
|
require.Equal(t, "0.280623462081924936", msg.CommissionRate.String())
|
||||||
require.Equal(t, "rBqDOTtGTO", msg.Description.Moniker)
|
require.Equal(t, "xKGLwQvuyN", msg.Description.Moniker)
|
||||||
require.Equal(t, "BSpYuLyYgg", msg.Description.Identity)
|
require.Equal(t, "SlcxgdXhhu", msg.Description.Identity)
|
||||||
require.Equal(t, "wNbeHVIkPZ", msg.Description.Website)
|
require.Equal(t, "WeLrQKjLxz", msg.Description.Website)
|
||||||
require.Equal(t, "MOXcnQfyze", msg.Description.SecurityContact)
|
require.Equal(t, "rBqDOTtGTO", msg.Description.SecurityContact)
|
||||||
require.Equal(t, types.TypeMsgEditValidator, msg.Type())
|
require.Equal(t, types.TypeMsgEditValidator, msg.Type())
|
||||||
require.Equal(t, "cosmosvaloper1tnh2q55v8wyygtt9srz5safamzdengsn9dsd7z", msg.ValidatorAddress)
|
require.Equal(t, "cosmosvaloper1tnh2q55v8wyygtt9srz5safamzdengsn9dsd7z", msg.ValidatorAddress)
|
||||||
require.Len(t, futureOperations, 0)
|
require.Len(t, futureOperations, 0)
|
||||||
|
|
Loading…
Reference in New Issue