/
simapp.go
146 lines (126 loc) · 4.87 KB
/
simapp.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
package simulation
import (
"math/rand"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/simapp/helpers"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
"github.com/dymensionxyz/dymension/v3/simulation/types"
)
// GlobalRollappIdList is a list of created rollapps
var GlobalRollappList []types.SimRollapp = []types.SimRollapp{}
// GlobalSequencerAddressesList is a list of created sequencers
var GlobalSequencerAddressesList []types.SimSequencer = []types.SimSequencer{}
// FindAccount find a specific address from an account list
func FindAccount(accs []simtypes.Account, address string) (simtypes.Account, bool) {
creator, err := sdk.AccAddressFromBech32(address)
if err != nil {
panic(err)
}
return simtypes.FindAccount(accs, creator)
}
// RandomRollapp picks and returns a random rollapp from an array and returns its
// position in the array.
func RandomRollapp(r *rand.Rand, rollappList []types.SimRollapp) (types.SimRollapp, int) {
idx := r.Intn(len(rollappList))
return rollappList[idx], idx
}
// RandomSequencer picks and returns a random sequencer from an array and returns its
// position in the array.
func RandomSequencer(r *rand.Rand, sequencerList []types.SimSequencer) (types.SimSequencer, int) {
idx := r.Intn(len(sequencerList))
return sequencerList[idx], idx
}
// GenAndDeliverMsgWithRandFees generates a transaction with a random fee and expected Error flag (bExpectedError).
// GenAndDeliverMsgWithRandFees wraps GenAndDeliverTxWithRand Fees and checks whether or not the operation
// failed as expected by bExpectedError flag
func GenAndDeliverMsgWithRandFees(
msg sdk.Msg,
msgType string,
moduleName string,
r *rand.Rand,
app *baseapp.BaseApp,
ctx *sdk.Context,
simAccount *simtypes.Account,
bk types.BankKeeper,
ak types.AccountKeeper,
futureOperation []simtypes.FutureOperation,
bExpectedError bool,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
spendableCoins := bk.SpendableCoins(*ctx, simAccount.Address)
if spendableCoins.Empty() {
return simtypes.NoOpMsg(moduleName, msgType, "unable to grant empty coins as SpendLimit"), nil, nil
}
txCtx := simulation.OperationInput{
R: r,
App: app,
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
Cdc: nil,
Msg: msg,
MsgType: msgType,
CoinsSpentInMsg: spendableCoins,
Context: *ctx,
SimAccount: *simAccount,
AccountKeeper: ak,
Bankkeeper: bk,
ModuleName: moduleName,
}
operationMsg, additionalFutureOperation, err := GenAndDeliverTxWithRandFees(txCtx)
futureOperation = append(futureOperation, additionalFutureOperation...)
if bExpectedError {
if err == nil {
panic(err)
}
err = nil
} else {
if err != nil {
panic(err)
}
}
return operationMsg, futureOperation, err
}
// GenAndDeliverTxWithRandFees generates a transaction with a random fee and delivers it.
// Copied from github.com/cosmos/cosmos-sdk/x/simulation/util because of the need to increase the gas
// as haedcoded passed in helpers.GenTx
func GenAndDeliverTxWithRandFees(txCtx simulation.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.
// Copied from github.com/cosmos/cosmos-sdk/x/simulation/util
func GenAndDeliverTx(txCtx simulation.OperationInput, fees sdk.Coins) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
account := txCtx.AccountKeeper.GetAccount(txCtx.Context, txCtx.SimAccount.Address)
txGas := uint64(10 * helpers.DefaultGenTxGas)
tx, err := helpers.GenSignedMockTx(
txCtx.R,
txCtx.TxGen,
[]sdk.Msg{txCtx.Msg},
fees,
txGas,
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.SimDeliver(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
}