-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
operations.go
167 lines (145 loc) · 5.46 KB
/
operations.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package simulation
import (
"math/rand"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
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/feegrant"
"github.com/cosmos/cosmos-sdk/x/feegrant/keeper"
"github.com/cosmos/cosmos-sdk/x/simulation"
)
// Simulation operation weights constants
//
//nolint:gosec // These aren't harcoded credentials.
const (
OpWeightMsgGrantAllowance = "op_weight_msg_grant_fee_allowance"
OpWeightMsgRevokeAllowance = "op_weight_msg_grant_revoke_allowance"
DefaultWeightGrantAllowance int = 100
DefaultWeightRevokeAllowance int = 100
)
var (
TypeMsgGrantAllowance = sdk.MsgTypeURL(&feegrant.MsgGrantAllowance{})
TypeMsgRevokeAllowance = sdk.MsgTypeURL(&feegrant.MsgRevokeAllowance{})
)
func WeightedOperations(
registry codectypes.InterfaceRegistry,
appParams simtypes.AppParams,
cdc codec.JSONCodec,
ak feegrant.AccountKeeper,
bk feegrant.BankKeeper,
k keeper.Keeper,
) simulation.WeightedOperations {
var (
weightMsgGrantAllowance int
weightMsgRevokeAllowance int
)
appParams.GetOrGenerate(cdc, OpWeightMsgGrantAllowance, &weightMsgGrantAllowance, nil,
func(_ *rand.Rand) {
weightMsgGrantAllowance = DefaultWeightGrantAllowance
},
)
appParams.GetOrGenerate(cdc, OpWeightMsgRevokeAllowance, &weightMsgRevokeAllowance, nil,
func(_ *rand.Rand) {
weightMsgRevokeAllowance = DefaultWeightRevokeAllowance
},
)
return simulation.WeightedOperations{
simulation.NewWeightedOperation(
weightMsgGrantAllowance,
SimulateMsgGrantAllowance(codec.NewProtoCodec(registry), ak, bk, k),
),
simulation.NewWeightedOperation(
weightMsgRevokeAllowance,
SimulateMsgRevokeAllowance(codec.NewProtoCodec(registry), ak, bk, k),
),
}
}
// SimulateMsgGrantAllowance generates MsgGrantAllowance with random values.
func SimulateMsgGrantAllowance(cdc *codec.ProtoCodec, ak feegrant.AccountKeeper, bk feegrant.BankKeeper, k keeper.Keeper) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
granter, _ := simtypes.RandomAcc(r, accs)
grantee, _ := simtypes.RandomAcc(r, accs)
if grantee.Address.String() == granter.Address.String() {
return simtypes.NoOpMsg(feegrant.ModuleName, TypeMsgGrantAllowance, "grantee and granter cannot be same"), nil, nil
}
if f, _ := k.GetAllowance(ctx, granter.Address, grantee.Address); f != nil {
return simtypes.NoOpMsg(feegrant.ModuleName, TypeMsgGrantAllowance, "fee allowance exists"), nil, nil
}
account := ak.GetAccount(ctx, granter.Address)
spendableCoins := bk.SpendableCoins(ctx, account.GetAddress())
if spendableCoins.Empty() {
return simtypes.NoOpMsg(feegrant.ModuleName, TypeMsgGrantAllowance, "unable to grant empty coins as SpendLimit"), nil, nil
}
oneYear := ctx.BlockTime().AddDate(1, 0, 0)
msg, err := feegrant.NewMsgGrantAllowance(&feegrant.BasicAllowance{
SpendLimit: spendableCoins,
Expiration: &oneYear,
}, granter.Address, grantee.Address)
if err != nil {
return simtypes.NoOpMsg(feegrant.ModuleName, TypeMsgGrantAllowance, err.Error()), nil, err
}
txCtx := simulation.OperationInput{
R: r,
App: app,
TxGen: tx.NewTxConfig(cdc, tx.DefaultSignModes),
Cdc: nil,
Msg: msg,
MsgType: TypeMsgGrantAllowance,
Context: ctx,
SimAccount: granter,
AccountKeeper: ak,
Bankkeeper: bk,
ModuleName: feegrant.ModuleName,
CoinsSpentInMsg: spendableCoins,
}
return simulation.GenAndDeliverTxWithRandFees(txCtx)
}
}
// SimulateMsgRevokeAllowance generates a MsgRevokeAllowance with random values.
func SimulateMsgRevokeAllowance(cdc *codec.ProtoCodec, ak feegrant.AccountKeeper, bk feegrant.BankKeeper, k keeper.Keeper) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
hasGrant := false
var granterAddr sdk.AccAddress
var granteeAddr sdk.AccAddress
k.IterateAllFeeAllowances(ctx, func(grant feegrant.Grant) bool {
granter := sdk.MustAccAddressFromBech32(grant.Granter)
grantee := sdk.MustAccAddressFromBech32(grant.Grantee)
granterAddr = granter
granteeAddr = grantee
hasGrant = true
return true
})
if !hasGrant {
return simtypes.NoOpMsg(feegrant.ModuleName, TypeMsgRevokeAllowance, "no grants"), nil, nil
}
granter, ok := simtypes.FindAccount(accs, granterAddr)
if !ok {
return simtypes.NoOpMsg(feegrant.ModuleName, TypeMsgRevokeAllowance, "Account not found"), nil, nil
}
account := ak.GetAccount(ctx, granter.Address)
spendableCoins := bk.SpendableCoins(ctx, account.GetAddress())
msg := feegrant.NewMsgRevokeAllowance(granterAddr, granteeAddr)
txCtx := simulation.OperationInput{
R: r,
App: app,
TxGen: tx.NewTxConfig(cdc, tx.DefaultSignModes),
Cdc: nil,
Msg: &msg,
MsgType: TypeMsgRevokeAllowance,
Context: ctx,
SimAccount: granter,
AccountKeeper: ak,
Bankkeeper: bk,
ModuleName: feegrant.ModuleName,
CoinsSpentInMsg: spendableCoins,
}
return simulation.GenAndDeliverTxWithRandFees(txCtx)
}
}