-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
keeper.go
521 lines (442 loc) · 15.2 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
package keeper
import (
"bytes"
"context"
"fmt"
"time"
gogoproto "github.com/cosmos/gogoproto/proto"
gogoprotoany "github.com/cosmos/gogoproto/types/any"
"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
corecontext "cosmossdk.io/core/context"
errorsmod "cosmossdk.io/errors"
storetypes "cosmossdk.io/store/types"
"cosmossdk.io/x/authz"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// TODO: Revisit this once we have proper gas fee framework.
// Tracking issues https://github.com/cosmos/cosmos-sdk/issues/9054,
// https://github.com/cosmos/cosmos-sdk/discussions/9072
const gasCostPerIteration = uint64(20)
type Keeper struct {
appmodule.Environment
cdc codec.Codec
addrCdc address.Codec
}
// NewKeeper constructs a message authorization Keeper
func NewKeeper(env appmodule.Environment, cdc codec.Codec, addrCdc address.Codec) Keeper {
return Keeper{
Environment: env,
cdc: cdc,
addrCdc: addrCdc,
}
}
// getGrant returns grant stored at skey.
func (k Keeper) getGrant(ctx context.Context, skey []byte) (grant authz.Grant, found bool) {
store := k.KVStoreService.OpenKVStore(ctx)
bz, err := store.Get(skey)
if err != nil {
panic(err)
}
if bz == nil {
return grant, false
}
k.cdc.MustUnmarshal(bz, &grant)
return grant, true
}
func (k Keeper) updateGrant(ctx context.Context, grantee, granter sdk.AccAddress, updated authz.Authorization) error {
skey := grantStoreKey(grantee, granter, updated.MsgTypeURL())
grant, found := k.getGrant(ctx, skey)
if !found {
return authz.ErrNoAuthorizationFound
}
msg, ok := updated.(gogoproto.Message)
if !ok {
return sdkerrors.ErrPackAny.Wrapf("cannot proto marshal %T", updated)
}
any, err := codectypes.NewAnyWithValue(msg)
if err != nil {
return err
}
grant.Authorization = any
store := k.KVStoreService.OpenKVStore(ctx)
return store.Set(skey, k.cdc.MustMarshal(&grant))
}
// DispatchActions attempts to execute the provided messages via authorization
// grants from the message signer to the grantee.
func (k Keeper) DispatchActions(ctx context.Context, grantee sdk.AccAddress, msgs []sdk.Msg) ([][]byte, error) {
results := make([][]byte, len(msgs))
now := k.Environment.HeaderService.HeaderInfo(ctx).Time
for i, msg := range msgs {
signers, _, err := k.cdc.GetMsgSigners(msg)
if err != nil {
return nil, err
}
if len(signers) != 1 {
return nil, authz.ErrAuthorizationNumOfSigners
}
granter := signers[0]
// If granter != grantee then check authorization.Accept, otherwise we
// implicitly accept.
if !bytes.Equal(granter, grantee) {
skey := grantStoreKey(grantee, granter, sdk.MsgTypeURL(msg))
grant, found := k.getGrant(ctx, skey)
if !found {
return nil, errorsmod.Wrapf(authz.ErrNoAuthorizationFound,
"failed to get grant with given granter: %s, grantee: %s & msgType: %s ", sdk.AccAddress(granter), grantee, sdk.MsgTypeURL(msg))
}
if grant.Expiration != nil && grant.Expiration.Before(now) {
return nil, authz.ErrAuthorizationExpired
}
authorization, err := grant.GetAuthorization()
if err != nil {
return nil, err
}
// pass the environment in the context
// users on server/v2 are expected to unwrap the environment from the context
// users on baseapp can still unwrap the sdk context
resp, err := authorization.Accept(context.WithValue(ctx, corecontext.EnvironmentContextKey, k.Environment), msg)
if err != nil {
return nil, err
}
if resp.Delete {
err = k.DeleteGrant(ctx, grantee, granter, sdk.MsgTypeURL(msg))
} else if resp.Updated != nil {
updated, ok := resp.Updated.(authz.Authorization)
if !ok {
return nil, fmt.Errorf("expected authz.Authorization but got %T", resp.Updated)
}
err = k.updateGrant(ctx, grantee, granter, updated)
}
if err != nil {
return nil, err
}
if !resp.Accept {
return nil, sdkerrors.ErrUnauthorized
}
}
// no need to use the branch service here, as if the transaction fails, the transaction will be reverted
resp, err := k.MsgRouterService.Invoke(ctx, msg)
if err != nil {
return nil, fmt.Errorf("failed to execute message %d; message %v: %w", i, msg, err)
}
msgRespAny, err := gogoprotoany.NewAnyWithCacheWithValue(resp)
if err != nil {
return nil, fmt.Errorf("failed to create any for response %d; message %s: %w", i, gogoproto.MessageName(msg), err)
}
results[i], err = gogoproto.Marshal(msgRespAny)
if err != nil {
return nil, fmt.Errorf("failed to marshal response %d; message %s: %w", i, gogoproto.MessageName(msg), err)
}
}
return results, nil
}
// SaveGrant method grants the provided authorization to the grantee on the granter's account
// with the provided expiration time and insert authorization key into the grants queue. If there is an existing authorization grant for the
// same `sdk.Msg` type, this grant overwrites that.
func (k Keeper) SaveGrant(ctx context.Context, grantee, granter sdk.AccAddress, authorization authz.Authorization, expiration *time.Time) error {
msgType := authorization.MsgTypeURL()
store := k.KVStoreService.OpenKVStore(ctx)
skey := grantStoreKey(grantee, granter, msgType)
grant, err := authz.NewGrant(k.HeaderService.HeaderInfo(ctx).Time, authorization, expiration)
if err != nil {
return err
}
var oldExp *time.Time
if oldGrant, found := k.getGrant(ctx, skey); found {
oldExp = oldGrant.Expiration
}
if oldExp != nil && (expiration == nil || !oldExp.Equal(*expiration)) {
if err = k.removeFromGrantQueue(ctx, skey, granter, grantee, *oldExp); err != nil {
return err
}
}
// If the expiration didn't change, then we don't remove it and we should not insert again
if expiration != nil && (oldExp == nil || !oldExp.Equal(*expiration)) {
if err = k.insertIntoGrantQueue(ctx, granter, grantee, msgType, *expiration); err != nil {
return err
}
}
bz, err := k.cdc.Marshal(&grant)
if err != nil {
return err
}
err = store.Set(skey, bz)
if err != nil {
return err
}
granterAddr, err := k.addrCdc.BytesToString(granter)
if err != nil {
return err
}
granteeAddr, err := k.addrCdc.BytesToString(grantee)
if err != nil {
return err
}
return k.EventService.EventManager(ctx).Emit(&authz.EventGrant{
MsgTypeUrl: authorization.MsgTypeURL(),
Granter: granterAddr,
Grantee: granteeAddr,
})
}
// DeleteGrant revokes any authorization for the provided message type granted to the grantee
// by the granter.
func (k Keeper) DeleteGrant(ctx context.Context, grantee, granter sdk.AccAddress, msgType string) error {
store := k.KVStoreService.OpenKVStore(ctx)
skey := grantStoreKey(grantee, granter, msgType)
grant, found := k.getGrant(ctx, skey)
if !found {
granterAddr, err := k.addrCdc.BytesToString(granter)
if err != nil {
return errorsmod.Wrapf(authz.ErrNoAuthorizationFound,
"could not convert granter address to string")
}
granteeAddr, err := k.addrCdc.BytesToString(grantee)
if err != nil {
return errorsmod.Wrapf(authz.ErrNoAuthorizationFound,
"could not convert grantee address to string")
}
return errorsmod.Wrapf(authz.ErrNoAuthorizationFound,
"failed to delete grant with given granter: %s, grantee: %s & msgType: %s ", granterAddr, granteeAddr, msgType)
}
if grant.Expiration != nil {
err := k.removeFromGrantQueue(ctx, skey, granter, grantee, *grant.Expiration)
if err != nil {
return err
}
}
err := store.Delete(skey)
if err != nil {
return err
}
granterAddr, err := k.addrCdc.BytesToString(granter)
if err != nil {
return err
}
granteeAddr, err := k.addrCdc.BytesToString(grantee)
if err != nil {
return err
}
return k.EventService.EventManager(ctx).Emit(&authz.EventRevoke{
MsgTypeUrl: msgType,
Granter: granterAddr,
Grantee: granteeAddr,
})
}
// DeleteAllGrants revokes all authorizations granted to the grantee by the granter.
func (k Keeper) DeleteAllGrants(ctx context.Context, granter sdk.AccAddress) error {
var keysToDelete [][]byte
err := k.IterateGranterGrants(ctx, granter, func(grantee sdk.AccAddress, msgType string) (stop bool, err error) {
keysToDelete = append(keysToDelete, grantStoreKey(grantee, granter, msgType))
return false, nil
})
if err != nil {
return err
}
if len(keysToDelete) == 0 {
return errorsmod.Wrapf(authz.ErrNoAuthorizationFound, "no grants found for granter %s", granter)
}
for _, key := range keysToDelete {
_, granteeAddr, msgType := parseGrantStoreKey(key)
if err := k.DeleteGrant(ctx, granteeAddr, granter, msgType); err != nil {
return err
}
}
grantAddr, err := k.addrCdc.BytesToString(granter)
if err != nil {
return err
}
return k.EventService.EventManager(ctx).Emit(&authz.EventRevokeAll{
Granter: grantAddr,
})
}
// GetAuthorizations Returns list of `Authorizations` granted to the grantee by the granter.
func (k Keeper) GetAuthorizations(ctx context.Context, grantee, granter sdk.AccAddress) ([]authz.Authorization, error) {
store := runtime.KVStoreAdapter(k.KVStoreService.OpenKVStore(ctx))
key := grantStoreKey(grantee, granter, "")
iter := storetypes.KVStorePrefixIterator(store, key)
defer iter.Close()
var authorizations []authz.Authorization
for ; iter.Valid(); iter.Next() {
var authorization authz.Grant
if err := k.cdc.Unmarshal(iter.Value(), &authorization); err != nil {
return nil, err
}
a, err := authorization.GetAuthorization()
if err != nil {
return nil, err
}
authorizations = append(authorizations, a)
}
return authorizations, nil
}
// GetAuthorization returns an Authorization and it's expiration time.
// A nil Authorization is returned under the following circumstances:
// - No grant is found.
// - A grant is found, but it is expired.
// - There was an error getting the authorization from the grant.
func (k Keeper) GetAuthorization(ctx context.Context, grantee, granter sdk.AccAddress, msgType string) (authz.Authorization, *time.Time) {
grant, found := k.getGrant(ctx, grantStoreKey(grantee, granter, msgType))
if !found || (grant.Expiration != nil && grant.Expiration.Before(k.HeaderService.HeaderInfo(ctx).Time)) {
return nil, nil
}
auth, err := grant.GetAuthorization()
if err != nil {
return nil, nil
}
return auth, grant.Expiration
}
// IterateGrants iterates over all authorization grants
// This function should be used with caution because it can involve significant IO operations.
// It should not be used in query or msg services without charging additional gas.
// The iteration stops when the handler function returns true or the iterator exhaust.
func (k Keeper) IterateGrants(ctx context.Context,
handler func(granterAddr, granteeAddr sdk.AccAddress, grant authz.Grant) (bool, error),
) error {
store := runtime.KVStoreAdapter(k.KVStoreService.OpenKVStore(ctx))
iter := storetypes.KVStorePrefixIterator(store, GrantKey)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var grant authz.Grant
granterAddr, granteeAddr, _ := parseGrantStoreKey(iter.Key())
k.cdc.MustUnmarshal(iter.Value(), &grant)
ok, err := handler(granterAddr, granteeAddr, grant)
if err != nil {
return err
}
if ok {
break
}
}
return nil
}
func (k Keeper) IterateGranterGrants(ctx context.Context, granter sdk.AccAddress,
handler func(granteeAddr sdk.AccAddress, msgType string) (stop bool, err error),
) error {
// no-op if handler is nil
if handler == nil {
return nil
}
store := runtime.KVStoreAdapter(k.KVStoreService.OpenKVStore(ctx))
iter := storetypes.KVStorePrefixIterator(store, granterStoreKey(granter))
defer iter.Close()
for ; iter.Valid(); iter.Next() {
_, granteeAddr, msgType := parseGrantStoreKey(iter.Key())
ok, err := handler(granteeAddr, msgType)
if err != nil {
return err
}
if ok {
break
}
}
return nil
}
func (k Keeper) getGrantQueueItem(ctx context.Context, expiration time.Time, granter, grantee sdk.AccAddress) (*authz.GrantQueueItem, error) {
store := k.KVStoreService.OpenKVStore(ctx)
bz, err := store.Get(GrantQueueKey(expiration, granter, grantee))
if err != nil {
return nil, err
}
if bz == nil {
return &authz.GrantQueueItem{}, nil
}
var queueItems authz.GrantQueueItem
if err := k.cdc.Unmarshal(bz, &queueItems); err != nil {
return nil, err
}
return &queueItems, nil
}
func (k Keeper) setGrantQueueItem(ctx context.Context, expiration time.Time,
granter, grantee sdk.AccAddress, queueItems *authz.GrantQueueItem,
) error {
store := k.KVStoreService.OpenKVStore(ctx)
bz, err := k.cdc.Marshal(queueItems)
if err != nil {
return err
}
return store.Set(GrantQueueKey(expiration, granter, grantee), bz)
}
// insertIntoGrantQueue inserts a grant key into the grant queue
func (k Keeper) insertIntoGrantQueue(ctx context.Context, granter, grantee sdk.AccAddress, msgType string, expiration time.Time) error {
queueItems, err := k.getGrantQueueItem(ctx, expiration, granter, grantee)
if err != nil {
return err
}
queueItems.MsgTypeUrls = append(queueItems.MsgTypeUrls, msgType)
return k.setGrantQueueItem(ctx, expiration, granter, grantee, queueItems)
}
// removeFromGrantQueue removes a grant key from the grant queue
func (k Keeper) removeFromGrantQueue(ctx context.Context, grantKey []byte, granter, grantee sdk.AccAddress, expiration time.Time) error {
store := k.KVStoreService.OpenKVStore(ctx)
key := GrantQueueKey(expiration, granter, grantee)
bz, err := store.Get(key)
if err != nil {
return err
}
if bz == nil {
return errorsmod.Wrap(authz.ErrNoGrantKeyFound, "can't remove grant from the expire queue, grant key not found")
}
var queueItem authz.GrantQueueItem
if err := k.cdc.Unmarshal(bz, &queueItem); err != nil {
return err
}
_, _, msgType := parseGrantStoreKey(grantKey)
queueItems := queueItem.MsgTypeUrls
for index, typeURL := range queueItems {
if err := k.GasService.GasMeter(ctx).Consume(gasCostPerIteration, "grant queue"); err != nil {
return err
}
if typeURL == msgType {
end := len(queueItem.MsgTypeUrls) - 1
queueItems[index] = queueItems[end]
queueItems = queueItems[:end]
if err := k.setGrantQueueItem(ctx, expiration, granter, grantee, &authz.GrantQueueItem{
MsgTypeUrls: queueItems,
}); err != nil {
return err
}
break
}
}
return nil
}
// DequeueAndDeleteExpiredGrants deletes expired grants from the state and grant queue.
func (k Keeper) DequeueAndDeleteExpiredGrants(ctx context.Context, limit int) error {
store := k.KVStoreService.OpenKVStore(ctx)
iterator, err := store.Iterator(GrantQueuePrefix, storetypes.InclusiveEndBytes(GrantQueueTimePrefix(k.HeaderService.HeaderInfo(ctx).Time)))
if err != nil {
return err
}
defer iterator.Close()
count := 0
for ; iterator.Valid(); iterator.Next() {
var queueItem authz.GrantQueueItem
if err := k.cdc.Unmarshal(iterator.Value(), &queueItem); err != nil {
return err
}
_, granter, grantee, err := parseGrantQueueKey(iterator.Key())
if err != nil {
return err
}
if err = store.Delete(iterator.Key()); err != nil {
return err
}
for _, typeURL := range queueItem.MsgTypeUrls {
err = store.Delete(grantStoreKey(grantee, granter, typeURL))
if err != nil {
return err
}
}
// limit the amount of iterations to avoid taking too much time
count++
if count == limit {
return nil
}
}
return nil
}