@@ -2,6 +2,7 @@ package keeper
2
2
3
3
import (
4
4
"fmt"
5
+ "strings"
5
6
6
7
"github.com/tendermint/tendermint/libs/log"
7
8
49
50
}
50
51
)
51
52
53
+ // NewKeeper constructs a new CapabilityKeeper instance and initializes maps
54
+ // for capability map and scopedModules map.
52
55
func NewKeeper (cdc codec.BinaryMarshaler , storeKey , memKey sdk.StoreKey ) * Keeper {
53
56
return & Keeper {
54
57
cdc : cdc ,
@@ -67,6 +70,9 @@ func (k *Keeper) ScopeToModule(moduleName string) ScopedKeeper {
67
70
if k .sealed {
68
71
panic ("cannot scope to module via a sealed capability keeper" )
69
72
}
73
+ if strings .TrimSpace (moduleName ) == "" {
74
+ panic ("cannot scope to an empty module name" )
75
+ }
70
76
71
77
if _ , ok := k .scopedModules [moduleName ]; ok {
72
78
panic (fmt .Sprintf ("cannot create multiple scoped keepers for the same module name: %s" , moduleName ))
@@ -117,10 +123,10 @@ func (k *Keeper) InitializeAndSeal(ctx sdk.Context) {
117
123
k .sealed = true
118
124
}
119
125
120
- // SetIndex sets the index to one (or greater) in InitChain according
126
+ // InitializeIndex sets the index to one (or greater) in InitChain according
121
127
// to the GenesisState. It must only be called once.
122
128
// It will panic if the provided index is 0, or if the index is already set.
123
- func (k Keeper ) SetIndex (ctx sdk.Context , index uint64 ) error {
129
+ func (k Keeper ) InitializeIndex (ctx sdk.Context , index uint64 ) error {
124
130
if index == 0 {
125
131
panic ("SetIndex requires index > 0" )
126
132
}
@@ -200,6 +206,9 @@ func (k Keeper) InitializeCapability(ctx sdk.Context, index uint64, owners types
200
206
// Note, namespacing is completely local, which is safe since records are prefixed
201
207
// with the module name and no two ScopedKeeper can have the same module name.
202
208
func (sk ScopedKeeper ) NewCapability (ctx sdk.Context , name string ) (* types.Capability , error ) {
209
+ if strings .TrimSpace (name ) == "" {
210
+ return nil , sdkerrors .Wrap (types .ErrInvalidCapabilityName , "capability name cannot be empty" )
211
+ }
203
212
store := ctx .KVStore (sk .storeKey )
204
213
205
214
if _ , ok := sk .GetCapability (ctx , name ); ok {
@@ -247,6 +256,9 @@ func (sk ScopedKeeper) NewCapability(ctx sdk.Context, name string) (*types.Capab
247
256
// Note, the capability's forward mapping is indexed by a string which should
248
257
// contain its unique memory reference.
249
258
func (sk ScopedKeeper ) AuthenticateCapability (ctx sdk.Context , cap * types.Capability , name string ) bool {
259
+ if strings .TrimSpace (name ) == "" || cap == nil {
260
+ return false
261
+ }
250
262
return sk .GetCapabilityName (ctx , cap ) == name
251
263
}
252
264
@@ -256,6 +268,12 @@ func (sk ScopedKeeper) AuthenticateCapability(ctx sdk.Context, cap *types.Capabi
256
268
// index. If the owner already exists, it will return an error. Otherwise, it will
257
269
// also set a forward and reverse index for the capability and capability name.
258
270
func (sk ScopedKeeper ) ClaimCapability (ctx sdk.Context , cap * types.Capability , name string ) error {
271
+ if cap == nil {
272
+ return sdkerrors .Wrap (types .ErrNilCapability , "cannot claim nil capability" )
273
+ }
274
+ if strings .TrimSpace (name ) == "" {
275
+ return sdkerrors .Wrap (types .ErrInvalidCapabilityName , "capability name cannot be empty" )
276
+ }
259
277
// update capability owner set
260
278
if err := sk .addOwner (ctx , cap , name ); err != nil {
261
279
return err
@@ -282,6 +300,9 @@ func (sk ScopedKeeper) ClaimCapability(ctx sdk.Context, cap *types.Capability, n
282
300
// previously claimed or created. After releasing the capability, if no more
283
301
// owners exist, the capability will be globally removed.
284
302
func (sk ScopedKeeper ) ReleaseCapability (ctx sdk.Context , cap * types.Capability ) error {
303
+ if cap == nil {
304
+ return sdkerrors .Wrap (types .ErrNilCapability , "cannot release nil capability" )
305
+ }
285
306
name := sk .GetCapabilityName (ctx , cap )
286
307
if len (name ) == 0 {
287
308
return sdkerrors .Wrap (types .ErrCapabilityNotOwned , sk .module )
@@ -321,6 +342,9 @@ func (sk ScopedKeeper) ReleaseCapability(ctx sdk.Context, cap *types.Capability)
321
342
// by name. The module is not allowed to retrieve capabilities which it does not
322
343
// own.
323
344
func (sk ScopedKeeper ) GetCapability (ctx sdk.Context , name string ) (* types.Capability , bool ) {
345
+ if strings .TrimSpace (name ) == "" {
346
+ return nil , false
347
+ }
324
348
memStore := ctx .KVStore (sk .memKey )
325
349
326
350
key := types .RevCapabilityKey (sk .module , name )
@@ -332,15 +356,14 @@ func (sk ScopedKeeper) GetCapability(ctx sdk.Context, name string) (*types.Capab
332
356
// to still have the capability in the go map since changes to
333
357
// go map do not automatically get reverted on tx failure,
334
358
// so we delete here to remove unnecessary values in map
335
- delete (sk .capMap , index )
359
+ // TODO: Delete index correctly from capMap by storing some reverse lookup
360
+ // in-memory map. Issue: https://github.com/cosmos/cosmos-sdk/issues/7805
336
361
return nil , false
337
362
}
338
363
339
364
cap := sk .capMap [index ]
340
365
if cap == nil {
341
- // delete key from store to remove unnecessary mapping
342
- memStore .Delete (key )
343
- return nil , false
366
+ panic ("capability found in memstore is missing from map" )
344
367
}
345
368
346
369
return cap , true
@@ -349,14 +372,20 @@ func (sk ScopedKeeper) GetCapability(ctx sdk.Context, name string) (*types.Capab
349
372
// GetCapabilityName allows a module to retrieve the name under which it stored a given
350
373
// capability given the capability
351
374
func (sk ScopedKeeper ) GetCapabilityName (ctx sdk.Context , cap * types.Capability ) string {
375
+ if cap == nil {
376
+ return ""
377
+ }
352
378
memStore := ctx .KVStore (sk .memKey )
353
379
354
380
return string (memStore .Get (types .FwdCapabilityKey (sk .module , cap )))
355
381
}
356
382
357
- // Get all the Owners that own the capability associated with the name this ScopedKeeper uses
383
+ // GetOwners all the Owners that own the capability associated with the name this ScopedKeeper uses
358
384
// to refer to the capability
359
385
func (sk ScopedKeeper ) GetOwners (ctx sdk.Context , name string ) (* types.CapabilityOwners , bool ) {
386
+ if strings .TrimSpace (name ) == "" {
387
+ return nil , false
388
+ }
360
389
cap , ok := sk .GetCapability (ctx , name )
361
390
if ! ok {
362
391
return nil , false
@@ -382,6 +411,9 @@ func (sk ScopedKeeper) GetOwners(ctx sdk.Context, name string) (*types.Capabilit
382
411
// The method returns an error if either the capability or the owners cannot be
383
412
// retreived from the memstore.
384
413
func (sk ScopedKeeper ) LookupModules (ctx sdk.Context , name string ) ([]string , * types.Capability , error ) {
414
+ if strings .TrimSpace (name ) == "" {
415
+ return nil , nil , sdkerrors .Wrap (types .ErrInvalidCapabilityName , "cannot lookup modules with empty capability name" )
416
+ }
385
417
cap , ok := sk .GetCapability (ctx , name )
386
418
if ! ok {
387
419
return nil , nil , sdkerrors .Wrap (types .ErrCapabilityNotFound , name )
0 commit comments