-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
keeper.go
330 lines (278 loc) · 9.75 KB
/
keeper.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package keeper
import (
"context"
"time"
"cosmossdk.io/collections"
"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
corecontext "cosmossdk.io/core/context"
"cosmossdk.io/core/event"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/x/feegrant"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
)
// Keeper manages state of all fee grants, as well as calculating approval.
// It must have a codec with all available allowances registered.
type Keeper struct {
appmodule.Environment
cdc codec.BinaryCodec
addrCdc address.Codec
Schema collections.Schema
// FeeAllowance key: grantee+granter | value: Grant
FeeAllowance collections.Map[collections.Pair[sdk.AccAddress, sdk.AccAddress], feegrant.Grant]
// FeeAllowanceQueue key: expiration time+grantee+granter | value: bool
FeeAllowanceQueue collections.Map[collections.Triple[time.Time, sdk.AccAddress, sdk.AccAddress], bool]
}
var _ ante.FeegrantKeeper = &Keeper{}
// NewKeeper creates a feegrant Keeper
func NewKeeper(env appmodule.Environment, cdc codec.BinaryCodec, addrCdc address.Codec) Keeper {
sb := collections.NewSchemaBuilder(env.KVStoreService)
return Keeper{
Environment: env,
cdc: cdc,
addrCdc: addrCdc,
FeeAllowance: collections.NewMap(
sb,
feegrant.FeeAllowanceKeyPrefix,
"allowances",
collections.PairKeyCodec(sdk.LengthPrefixedAddressKey(sdk.AccAddressKey), sdk.LengthPrefixedAddressKey(sdk.AccAddressKey)), //nolint: staticcheck // sdk.LengthPrefixedAddressKey is needed to retain state compatibility
codec.CollValue[feegrant.Grant](cdc),
),
FeeAllowanceQueue: collections.NewMap(
sb,
feegrant.FeeAllowanceQueueKeyPrefix,
"allowances_queue",
collections.TripleKeyCodec(sdk.TimeKey, sdk.LengthPrefixedAddressKey(sdk.AccAddressKey), sdk.LengthPrefixedAddressKey(sdk.AccAddressKey)), //nolint: staticcheck // sdk.LengthPrefixedAddressKey is needed to retain state compatibility
collections.BoolValue,
),
}
}
// GrantAllowance creates a new grant
func (k Keeper) GrantAllowance(ctx context.Context, granter, grantee sdk.AccAddress, feeAllowance feegrant.FeeAllowanceI) error {
// Checking for duplicate entry
if f, _ := k.GetAllowance(ctx, granter, grantee); f != nil {
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "fee allowance already exists")
}
exp, err := feeAllowance.ExpiresAt()
if err != nil {
return err
}
// expiration shouldn't be in the past.
now := k.HeaderService.HeaderInfo(ctx).Time
if exp != nil && exp.Before(now) {
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "expiration is before current block time")
}
// if expiry is not nil, add the new key to pruning queue.
if exp != nil {
err = k.FeeAllowanceQueue.Set(ctx, collections.Join3(*exp, grantee, granter), true)
if err != nil {
return err
}
}
granterStr, err := k.addrCdc.BytesToString(granter)
if err != nil {
return err
}
granteeStr, err := k.addrCdc.BytesToString(grantee)
if err != nil {
return err
}
// if block time is not zero, update the period reset
// if it is zero, it could be genesis initialization, so we don't need to update the period reset
if !now.IsZero() {
err = feeAllowance.UpdatePeriodReset(now)
if err != nil {
return err
}
}
grant, err := feegrant.NewGrant(granterStr, granteeStr, feeAllowance)
if err != nil {
return err
}
if err := k.FeeAllowance.Set(ctx, collections.Join(grantee, granter), grant); err != nil {
return err
}
return k.EventService.EventManager(ctx).EmitKV(
feegrant.EventTypeSetFeeGrant,
event.NewAttribute(feegrant.AttributeKeyGranter, grant.Granter),
event.NewAttribute(feegrant.AttributeKeyGrantee, grant.Grantee),
)
}
// UpdateAllowance updates the existing grant.
func (k Keeper) UpdateAllowance(ctx context.Context, granter, grantee sdk.AccAddress, feeAllowance feegrant.FeeAllowanceI) error {
_, err := k.GetAllowance(ctx, granter, grantee)
if err != nil {
return err
}
granterStr, err := k.addrCdc.BytesToString(granter)
if err != nil {
return err
}
granteeStr, err := k.addrCdc.BytesToString(grantee)
if err != nil {
return err
}
grant, err := feegrant.NewGrant(granterStr, granteeStr, feeAllowance)
if err != nil {
return err
}
if err := k.FeeAllowance.Set(ctx, collections.Join(grantee, granter), grant); err != nil {
return err
}
return k.EventService.EventManager(ctx).EmitKV(
feegrant.EventTypeUpdateFeeGrant,
event.NewAttribute(feegrant.AttributeKeyGranter, grant.Granter),
event.NewAttribute(feegrant.AttributeKeyGrantee, grant.Grantee),
)
}
// revokeAllowance removes an existing grant
func (k Keeper) revokeAllowance(ctx context.Context, granter, grantee sdk.AccAddress) error {
grant, err := k.GetAllowance(ctx, granter, grantee)
if err != nil {
return err
}
if err := k.FeeAllowance.Remove(ctx, collections.Join(grantee, granter)); err != nil {
return err
}
exp, err := grant.ExpiresAt()
if err != nil {
return err
}
if exp != nil {
if err := k.FeeAllowanceQueue.Remove(ctx, collections.Join3(*exp, grantee, granter)); err != nil {
return err
}
}
granterStr, err := k.addrCdc.BytesToString(granter)
if err != nil {
return err
}
granteeStr, err := k.addrCdc.BytesToString(grantee)
if err != nil {
return err
}
return k.EventService.EventManager(ctx).EmitKV(
feegrant.EventTypeRevokeFeeGrant,
event.NewAttribute(feegrant.AttributeKeyGranter, granterStr),
event.NewAttribute(feegrant.AttributeKeyGrantee, granteeStr),
)
}
// GetAllowance returns the allowance between the granter and grantee.
// If there is none, it returns nil, collections.ErrNotFound.
// Returns an error on parsing issues
func (k Keeper) GetAllowance(ctx context.Context, granter, grantee sdk.AccAddress) (feegrant.FeeAllowanceI, error) {
grant, err := k.FeeAllowance.Get(ctx, collections.Join(grantee, granter))
if err != nil {
return nil, err
}
return grant.GetGrant()
}
// IterateAllFeeAllowances iterates over all the grants in the store.
// Callback to get all data, returns true to stop, false to keep reading
// Calling this without pagination is very expensive and only designed for export genesis
func (k Keeper) IterateAllFeeAllowances(ctx context.Context, cb func(grant feegrant.Grant) bool) error {
return k.FeeAllowance.Walk(ctx, nil, func(key collections.Pair[sdk.AccAddress, sdk.AccAddress], grant feegrant.Grant) (stop bool, err error) {
return cb(grant), nil
})
}
// UseGrantedFees will try to pay the given fee from the granter's account as requested by the grantee
func (k Keeper) UseGrantedFees(ctx context.Context, granter, grantee sdk.AccAddress, fee sdk.Coins, msgs []sdk.Msg) error {
grant, err := k.GetAllowance(ctx, granter, grantee)
if err != nil {
return err
}
granterStr, err := k.addrCdc.BytesToString(granter)
if err != nil {
return err
}
granteeStr, err := k.addrCdc.BytesToString(grantee)
if err != nil {
return err
}
remove, err := grant.Accept(context.WithValue(ctx, corecontext.EnvironmentContextKey, k.Environment), fee, msgs)
if remove && err == nil {
// Ignoring the `revokeFeeAllowance` error, because the user has enough grants to perform this transaction.
_ = k.revokeAllowance(ctx, granter, grantee)
return k.emitUseGrantEvent(ctx, granterStr, granteeStr)
}
if err != nil {
return err
}
if err := k.emitUseGrantEvent(ctx, granterStr, granteeStr); err != nil {
return err
}
// if fee allowance is accepted, store the updated state of the allowance
return k.UpdateAllowance(ctx, granter, grantee, grant)
}
func (k *Keeper) emitUseGrantEvent(ctx context.Context, granter, grantee string) error {
return k.EventService.EventManager(ctx).EmitKV(
feegrant.EventTypeUseFeeGrant,
event.NewAttribute(feegrant.AttributeKeyGranter, granter),
event.NewAttribute(feegrant.AttributeKeyGrantee, grantee),
)
}
// InitGenesis will initialize the keeper from a *previously validated* GenesisState
func (k Keeper) InitGenesis(ctx context.Context, data *feegrant.GenesisState) error {
for _, f := range data.Allowances {
granter, err := k.addrCdc.StringToBytes(f.Granter)
if err != nil {
return err
}
grantee, err := k.addrCdc.StringToBytes(f.Grantee)
if err != nil {
return err
}
grant, err := f.GetGrant()
if err != nil {
return err
}
err = k.GrantAllowance(ctx, granter, grantee, grant)
if err != nil {
return err
}
}
return nil
}
// ExportGenesis will dump the contents of the keeper into a serializable GenesisState.
func (k Keeper) ExportGenesis(ctx context.Context) (*feegrant.GenesisState, error) {
var grants []feegrant.Grant
err := k.IterateAllFeeAllowances(ctx, func(grant feegrant.Grant) bool {
grants = append(grants, grant)
return false
})
return &feegrant.GenesisState{
Allowances: grants,
}, err
}
// RemoveExpiredAllowances iterates grantsByExpiryQueue and deletes the expired grants.
func (k Keeper) RemoveExpiredAllowances(ctx context.Context, limit int) error {
exp := k.HeaderService.HeaderInfo(ctx).Time
rng := collections.NewPrefixUntilTripleRange[time.Time, sdk.AccAddress, sdk.AccAddress](exp)
count := 0
keysToRemove := []collections.Triple[time.Time, sdk.AccAddress, sdk.AccAddress]{}
err := k.FeeAllowanceQueue.Walk(ctx, rng, func(key collections.Triple[time.Time, sdk.AccAddress, sdk.AccAddress], value bool) (stop bool, err error) {
grantee, granter := key.K2(), key.K3()
if err := k.FeeAllowance.Remove(ctx, collections.Join(grantee, granter)); err != nil {
return true, err
}
keysToRemove = append(keysToRemove, key)
// limit the amount of iterations to avoid taking too much time
count++
if count == limit {
return true, nil
}
return false, nil
})
if err != nil {
return err
}
for _, key := range keysToRemove {
if err := k.FeeAllowanceQueue.Remove(ctx, key); err != nil {
return err
}
}
return nil
}