Skip to content

Commit f1289d0

Browse files
authored
refactor: add util functions to reuse in simulations (cosmos#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
1 parent f96811c commit f1289d0

File tree

7 files changed

+286
-330
lines changed

7 files changed

+286
-330
lines changed

x/distribution/simulation/operations.go

+59-97
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
"github.com/cosmos/cosmos-sdk/baseapp"
88
"github.com/cosmos/cosmos-sdk/codec"
9-
"github.com/cosmos/cosmos-sdk/simapp/helpers"
109
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
1110
sdk "github.com/cosmos/cosmos-sdk/types"
1211
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
@@ -93,34 +92,24 @@ func SimulateMsgSetWithdrawAddress(ak types.AccountKeeper, bk types.BankKeeper,
9392
account := ak.GetAccount(ctx, simAccount.Address)
9493
spendable := bk.SpendableCoins(ctx, account.GetAddress())
9594

96-
fees, err := simtypes.RandomFees(r, ctx, spendable)
97-
if err != nil {
98-
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSetWithdrawAddress, "unable to generate fees"), nil, err
99-
}
100-
10195
msg := types.NewMsgSetWithdrawAddress(simAccount.Address, simToAccount.Address)
10296

103-
txGen := simappparams.MakeTestEncodingConfig().TxConfig
104-
tx, err := helpers.GenTx(
105-
txGen,
106-
[]sdk.Msg{msg},
107-
fees,
108-
helpers.DefaultGenTxGas,
109-
chainID,
110-
[]uint64{account.GetAccountNumber()},
111-
[]uint64{account.GetSequence()},
112-
simAccount.PrivKey,
113-
)
114-
if err != nil {
115-
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
116-
}
117-
118-
_, _, err = app.Deliver(txGen.TxEncoder(), tx)
119-
if err != nil {
120-
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err
121-
}
122-
123-
return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil
97+
txCtx := simulation.OperationInput{
98+
R: r,
99+
App: app,
100+
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
101+
Cdc: nil,
102+
Msg: msg,
103+
MsgType: msg.Type(),
104+
Context: ctx,
105+
SimAccount: simAccount,
106+
AccountKeeper: ak,
107+
Bankkeeper: bk,
108+
ModuleName: types.ModuleName,
109+
CoinsSpentInMsg: spendable,
110+
}
111+
112+
return simulation.GenAndDeliverTxWithRandFees(txCtx)
124113
}
125114
}
126115

@@ -145,34 +134,24 @@ func SimulateMsgWithdrawDelegatorReward(ak types.AccountKeeper, bk types.BankKee
145134
account := ak.GetAccount(ctx, simAccount.Address)
146135
spendable := bk.SpendableCoins(ctx, account.GetAddress())
147136

148-
fees, err := simtypes.RandomFees(r, ctx, spendable)
149-
if err != nil {
150-
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgWithdrawDelegatorReward, "unable to generate fees"), nil, err
151-
}
152-
153137
msg := types.NewMsgWithdrawDelegatorReward(simAccount.Address, validator.GetOperator())
154138

155-
txGen := simappparams.MakeTestEncodingConfig().TxConfig
156-
tx, err := helpers.GenTx(
157-
txGen,
158-
[]sdk.Msg{msg},
159-
fees,
160-
helpers.DefaultGenTxGas,
161-
chainID,
162-
[]uint64{account.GetAccountNumber()},
163-
[]uint64{account.GetSequence()},
164-
simAccount.PrivKey,
165-
)
166-
if err != nil {
167-
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
168-
}
169-
170-
_, _, err = app.Deliver(txGen.TxEncoder(), tx)
171-
if err != nil {
172-
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err
173-
}
174-
175-
return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil
139+
txCtx := simulation.OperationInput{
140+
R: r,
141+
App: app,
142+
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
143+
Cdc: nil,
144+
Msg: msg,
145+
MsgType: msg.Type(),
146+
Context: ctx,
147+
SimAccount: simAccount,
148+
AccountKeeper: ak,
149+
Bankkeeper: bk,
150+
ModuleName: types.ModuleName,
151+
CoinsSpentInMsg: spendable,
152+
}
153+
154+
return simulation.GenAndDeliverTxWithRandFees(txCtx)
176155
}
177156
}
178157

@@ -200,34 +179,24 @@ func SimulateMsgWithdrawValidatorCommission(ak types.AccountKeeper, bk types.Ban
200179
account := ak.GetAccount(ctx, simAccount.Address)
201180
spendable := bk.SpendableCoins(ctx, account.GetAddress())
202181

203-
fees, err := simtypes.RandomFees(r, ctx, spendable)
204-
if err != nil {
205-
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgWithdrawValidatorCommission, "unable to generate fees"), nil, err
206-
}
207-
208182
msg := types.NewMsgWithdrawValidatorCommission(validator.GetOperator())
209183

210-
txGen := simappparams.MakeTestEncodingConfig().TxConfig
211-
tx, err := helpers.GenTx(
212-
txGen,
213-
[]sdk.Msg{msg},
214-
fees,
215-
helpers.DefaultGenTxGas,
216-
chainID,
217-
[]uint64{account.GetAccountNumber()},
218-
[]uint64{account.GetSequence()},
219-
simAccount.PrivKey,
220-
)
221-
if err != nil {
222-
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
223-
}
224-
225-
_, _, err = app.Deliver(txGen.TxEncoder(), tx)
226-
if err != nil {
227-
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err
228-
}
229-
230-
return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil
184+
txCtx := simulation.OperationInput{
185+
R: r,
186+
App: app,
187+
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
188+
Cdc: nil,
189+
Msg: msg,
190+
MsgType: msg.Type(),
191+
Context: ctx,
192+
SimAccount: simAccount,
193+
AccountKeeper: ak,
194+
Bankkeeper: bk,
195+
ModuleName: types.ModuleName,
196+
CoinsSpentInMsg: spendable,
197+
}
198+
199+
return simulation.GenAndDeliverTxWithRandFees(txCtx)
231200
}
232201
}
233202

@@ -262,26 +231,19 @@ func SimulateMsgFundCommunityPool(ak types.AccountKeeper, bk types.BankKeeper, k
262231
}
263232

264233
msg := types.NewMsgFundCommunityPool(fundAmount, funder.Address)
265-
txGen := simappparams.MakeTestEncodingConfig().TxConfig
266-
tx, err := helpers.GenTx(
267-
txGen,
268-
[]sdk.Msg{msg},
269-
fees,
270-
helpers.DefaultGenTxGas,
271-
chainID,
272-
[]uint64{account.GetAccountNumber()},
273-
[]uint64{account.GetSequence()},
274-
funder.PrivKey,
275-
)
276-
if err != nil {
277-
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
278-
}
279234

280-
_, _, err = app.Deliver(txGen.TxEncoder(), tx)
281-
if err != nil {
282-
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err
235+
txCtx := simulation.OperationInput{
236+
App: app,
237+
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
238+
Cdc: nil,
239+
Msg: msg,
240+
MsgType: msg.Type(),
241+
Context: ctx,
242+
SimAccount: funder,
243+
AccountKeeper: ak,
244+
ModuleName: types.ModuleName,
283245
}
284246

285-
return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil
247+
return simulation.GenAndDeliverTx(txCtx, fees)
286248
}
287249
}

x/feegrant/simulation/operations.go

+28-46
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
"github.com/cosmos/cosmos-sdk/baseapp"
77
"github.com/cosmos/cosmos-sdk/codec"
8-
"github.com/cosmos/cosmos-sdk/simapp/helpers"
98
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
109
sdk "github.com/cosmos/cosmos-sdk/types"
1110
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
@@ -77,12 +76,6 @@ func SimulateMsgGrantAllowance(ak feegrant.AccountKeeper, bk feegrant.BankKeeper
7776
account := ak.GetAccount(ctx, granter.Address)
7877

7978
spendableCoins := bk.SpendableCoins(ctx, account.GetAddress())
80-
fees, err := simtypes.RandomFees(r, ctx, spendableCoins)
81-
if err != nil {
82-
return simtypes.NoOpMsg(feegrant.ModuleName, TypeMsgGrantAllowance, err.Error()), nil, err
83-
}
84-
85-
spendableCoins = spendableCoins.Sub(fees)
8679
if spendableCoins.Empty() {
8780
return simtypes.NoOpMsg(feegrant.ModuleName, TypeMsgGrantAllowance, "unable to grant empty coins as SpendLimit"), nil, nil
8881
}
@@ -96,28 +89,23 @@ func SimulateMsgGrantAllowance(ak feegrant.AccountKeeper, bk feegrant.BankKeeper
9689
if err != nil {
9790
return simtypes.NoOpMsg(feegrant.ModuleName, TypeMsgGrantAllowance, err.Error()), nil, err
9891
}
99-
txGen := simappparams.MakeTestEncodingConfig().TxConfig
100-
tx, err := helpers.GenTx(
101-
txGen,
102-
[]sdk.Msg{msg},
103-
fees,
104-
helpers.DefaultGenTxGas,
105-
chainID,
106-
[]uint64{account.GetAccountNumber()},
107-
[]uint64{account.GetSequence()},
108-
granter.PrivKey,
109-
)
11092

111-
if err != nil {
112-
return simtypes.NoOpMsg(feegrant.ModuleName, TypeMsgGrantAllowance, "unable to generate mock tx"), nil, err
93+
txCtx := simulation.OperationInput{
94+
R: r,
95+
App: app,
96+
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
97+
Cdc: nil,
98+
Msg: msg,
99+
MsgType: TypeMsgGrantAllowance,
100+
Context: ctx,
101+
SimAccount: granter,
102+
AccountKeeper: ak,
103+
Bankkeeper: bk,
104+
ModuleName: feegrant.ModuleName,
105+
CoinsSpentInMsg: spendableCoins,
113106
}
114107

115-
_, _, err = app.Deliver(txGen.TxEncoder(), tx)
116-
117-
if err != nil {
118-
return simtypes.NoOpMsg(feegrant.ModuleName, sdk.MsgTypeURL(msg), "unable to deliver tx"), nil, err
119-
}
120-
return simtypes.NewOperationMsg(msg, true, "", nil), nil, err
108+
return simulation.GenAndDeliverTxWithRandFees(txCtx)
121109
}
122110
}
123111

@@ -157,30 +145,24 @@ func SimulateMsgRevokeAllowance(ak feegrant.AccountKeeper, bk feegrant.BankKeepe
157145

158146
account := ak.GetAccount(ctx, granter.Address)
159147
spendableCoins := bk.SpendableCoins(ctx, account.GetAddress())
160-
fees, err := simtypes.RandomFees(r, ctx, spendableCoins)
161-
if err != nil {
162-
return simtypes.NoOpMsg(feegrant.ModuleName, TypeMsgRevokeAllowance, err.Error()), nil, err
163-
}
164148

165149
msg := feegrant.NewMsgRevokeAllowance(granterAddr, granteeAddr)
166150

167-
txGen := simappparams.MakeTestEncodingConfig().TxConfig
168-
tx, err := helpers.GenTx(
169-
txGen,
170-
[]sdk.Msg{&msg},
171-
fees,
172-
helpers.DefaultGenTxGas,
173-
chainID,
174-
[]uint64{account.GetAccountNumber()},
175-
[]uint64{account.GetSequence()},
176-
granter.PrivKey,
177-
)
178-
179-
if err != nil {
180-
return simtypes.NoOpMsg(feegrant.ModuleName, TypeMsgRevokeAllowance, err.Error()), nil, err
151+
txCtx := simulation.OperationInput{
152+
R: r,
153+
App: app,
154+
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
155+
Cdc: nil,
156+
Msg: &msg,
157+
MsgType: TypeMsgRevokeAllowance,
158+
Context: ctx,
159+
SimAccount: granter,
160+
AccountKeeper: ak,
161+
Bankkeeper: bk,
162+
ModuleName: feegrant.ModuleName,
163+
CoinsSpentInMsg: spendableCoins,
181164
}
182165

183-
_, _, err = app.Deliver(txGen.TxEncoder(), tx)
184-
return simtypes.NewOperationMsg(&msg, true, "", nil), nil, err
166+
return simulation.GenAndDeliverTxWithRandFees(txCtx)
185167
}
186168
}

0 commit comments

Comments
 (0)