forked from osmosis-labs/osmosis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.go
96 lines (81 loc) · 2.54 KB
/
mod.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
package simulation
import (
"math/rand"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/simapp/helpers"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
stakingTypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)
type SimulationContext struct {
R *rand.Rand
SdkCtx sdk.Context
App *baseapp.BaseApp
Accs []simtypes.Account
simAccount *simtypes.Account
}
func NewSimulationContext(r *rand.Rand, ctx sdk.Context, app *baseapp.BaseApp, accs []simtypes.Account) SimulationContext {
return SimulationContext{r, ctx, app, accs, nil}
}
func (ctx *SimulationContext) GetMsgSender() simtypes.Account {
if ctx.simAccount == nil {
sel := ctx.R.Intn(len(ctx.Accs))
ctx.simAccount = &ctx.Accs[sel]
}
return *ctx.simAccount
}
func GenAndDeliverTxWithRandFees(
r *rand.Rand,
app *baseapp.BaseApp,
txGen client.TxConfig,
msg sdk.Msg,
coinsSpentInMsg sdk.Coins,
ctx sdk.Context,
simAccount simtypes.Account,
ak stakingTypes.AccountKeeper,
bk stakingTypes.BankKeeper,
moduleName string) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
account := ak.GetAccount(ctx, simAccount.Address)
spendable := bk.SpendableCoins(ctx, account.GetAddress())
var fees sdk.Coins
var err error
coins, hasNeg := spendable.SafeSub(coinsSpentInMsg)
if hasNeg {
return simtypes.NoOpMsg(moduleName, msg.Type(), "message doesn't leave room for fees"), nil, err
}
fees, err = simtypes.RandomFees(r, ctx, coins)
if err != nil {
return simtypes.NoOpMsg(moduleName, msg.Type(), "unable to generate fees"), nil, err
}
return GenAndDeliverTx(app, txGen, msg, fees, ctx, simAccount, ak, moduleName)
}
func GenAndDeliverTx(
app *baseapp.BaseApp,
txGen client.TxConfig,
msg sdk.Msg,
fees sdk.Coins,
ctx sdk.Context,
simAccount simtypes.Account,
ak stakingTypes.AccountKeeper,
moduleName string) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
account := ak.GetAccount(ctx, simAccount.Address)
tx, err := helpers.GenTx(
txGen,
[]sdk.Msg{msg},
fees,
helpers.DefaultGenTxGas,
ctx.ChainID(),
[]uint64{account.GetAccountNumber()},
[]uint64{account.GetSequence()},
simAccount.PrivKey,
)
if err != nil {
return simtypes.NoOpMsg(moduleName, msg.Type(), "unable to generate mock tx"), nil, err
}
_, _, err = app.Deliver(txGen.TxEncoder(), tx)
if err != nil {
return simtypes.NoOpMsg(moduleName, msg.Type(), "unable to deliver tx"), nil, err
}
return simtypes.NewOperationMsg(msg, true, ""), nil, nil
}