-
Notifications
You must be signed in to change notification settings - Fork 83
/
operation_utils.go
147 lines (131 loc) · 4.26 KB
/
operation_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package sim_helpers
import (
"fmt"
"math/rand"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/auth/tx"
"github.com/cosmos/cosmos-sdk/x/simulation"
pricestypes "github.com/dydxprotocol/v4-chain/protocol/x/prices/types"
)
// GenerateAndDeliverTx generates a random fee (or with zero fees if set), then generates a
// signed mock tx and delivers the tx to the app for simulated operations.
func GenerateAndDeliverTx(
r *rand.Rand,
app *baseapp.BaseApp,
ctx sdk.Context,
chainId string,
cdc *codec.ProtoCodec,
ak pricestypes.AccountKeeper,
bk pricestypes.BankKeeper,
account simtypes.Account,
moduleName string,
msg sdk.Msg,
msgType string,
withZeroFees bool,
) (simtypes.OperationMsg, error) {
simAccount := ak.GetAccount(ctx, account.Address)
spendable := bk.SpendableCoins(ctx, simAccount.GetAddress())
txCtx := simulation.OperationInput{
R: r,
App: app,
TxGen: tx.NewTxConfig(cdc, tx.DefaultSignModes),
Cdc: cdc,
Msg: msg,
MsgType: msgType,
Context: ctx,
SimAccount: account,
AccountKeeper: ak,
Bankkeeper: bk,
ModuleName: moduleName,
CoinsSpentInMsg: spendable,
}
var opMsg simtypes.OperationMsg
var err error
if withZeroFees {
opMsg, _, err = simulation.GenAndDeliverTx(txCtx, sdk.Coins{})
} else {
opMsg, _, err = simulation.GenAndDeliverTxWithRandFees(txCtx)
}
if err != nil || !opMsg.OK {
return opMsg, fmt.Errorf("failed to generate and deliver tx: %w", err)
}
return opMsg, nil
}
// GenerateAndCheckTx generates a random fee (or with zero fees if set), then generates a signed
// mock tx and calls `CheckTx` for simulated operations. This heavily matches the cosmos sdk
// `util.go` GenAndDeliverTx method from the simulation module.
func GenerateAndCheckTx(
r *rand.Rand,
app *baseapp.BaseApp,
ctx sdk.Context,
chainId string,
cdc *codec.ProtoCodec,
ak pricestypes.AccountKeeper,
bk pricestypes.BankKeeper,
account simtypes.Account,
moduleName string,
msg sdk.Msg,
msgType string,
withZeroFees bool,
) (simtypes.OperationMsg, error) {
// TODO(DEC-1174): Root-cause CheckTx failing on Block Height 1 and remove this workaround.
if ctx.BlockHeight() == 1 {
return simtypes.NoOpMsg(moduleName, msgType, "CheckTx skipped for block height 1"), nil
}
// Workaround: cosmos-sdk Simulation hard-codes to a deliver context. Generate and use a new
// check context (with the same headers) specifically for CheckTx.
checkTxCtx := app.NewContext(true, ctx.BlockHeader())
simAccount := ak.GetAccount(checkTxCtx, account.Address)
spendable := bk.SpendableCoins(checkTxCtx, simAccount.GetAddress())
txCtx := simulation.OperationInput{
R: r,
App: app,
TxGen: tx.NewTxConfig(cdc, tx.DefaultSignModes),
Cdc: cdc,
Msg: msg,
MsgType: msgType,
Context: checkTxCtx,
SimAccount: account,
AccountKeeper: ak,
Bankkeeper: bk,
ModuleName: moduleName,
CoinsSpentInMsg: spendable,
}
var err error
var fees sdk.Coins
if withZeroFees {
fees = sdk.Coins{}
} else {
coins, hasNeg := spendable.SafeSub(txCtx.CoinsSpentInMsg...)
if hasNeg {
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "message doesn't leave room for fees"), nil
}
fees, err = simtypes.RandomFees(txCtx.R, txCtx.Context, coins)
if err != nil {
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "unable to generate fees"), err
}
}
tx, err := simtestutil.GenSignedMockTx(
txCtx.R,
txCtx.TxGen,
[]sdk.Msg{txCtx.Msg},
fees,
simtestutil.DefaultGenTxGas,
txCtx.Context.ChainID(),
[]uint64{simAccount.GetAccountNumber()},
[]uint64{simAccount.GetSequence()},
txCtx.SimAccount.PrivKey,
)
if err != nil {
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "unable to generate mock tx"), err
}
_, _, err = txCtx.App.SimCheck(txCtx.TxGen.TxEncoder(), tx)
if err != nil {
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "unable to check tx"), err
}
return simtypes.NewOperationMsg(txCtx.Msg, true, "", txCtx.Cdc), nil
}