4
4
package api
5
5
6
6
import (
7
+ "iter"
7
8
"sync"
8
9
9
10
"github.com/ChainSafe/gossamer/internal/primitives/blockchain"
@@ -352,6 +353,181 @@ type Backend[
352
353
// UsageInfo() *UsageInfo
353
354
}
354
355
356
+ // KeysIter is an iterator over storage keys.
357
+ type KeysIter [H runtime.Hash , Hasher runtime.Hasher [H ]] struct {
358
+ backend * statemachine.TrieBackend [H , Hasher ]
359
+ rawIter statemachine.StorageIterator [H , Hasher ]
360
+ }
361
+
362
+ func NewKeysIter [H runtime.Hash , Hasher runtime.Hasher [H ]](
363
+ backend * statemachine.TrieBackend [H , Hasher ],
364
+ prefix * storage.StorageKey ,
365
+ startAt * storage.StorageKey ,
366
+ ) (* KeysIter [H , Hasher ], error ) {
367
+ args := statemachine.IterArgs {
368
+ StartAtExclusive : true ,
369
+ }
370
+
371
+ if prefix != nil {
372
+ args .Prefix = * prefix
373
+ }
374
+
375
+ if startAt != nil {
376
+ args .StartAt = * startAt
377
+ }
378
+
379
+ rawIter , err := backend .RawIter (args )
380
+ if err != nil {
381
+ return nil , err
382
+ }
383
+
384
+ return & KeysIter [H , Hasher ]{backend , rawIter }, nil
385
+ }
386
+
387
+ func NewChildKeysIter [H runtime.Hash , Hasher runtime.Hasher [H ]](
388
+ backend * statemachine.TrieBackend [H , Hasher ],
389
+ childInfo storage.ChildInfo ,
390
+ prefix * storage.StorageKey ,
391
+ startAt * storage.StorageKey ,
392
+ ) (* KeysIter [H , Hasher ], error ) {
393
+ args := statemachine.IterArgs {
394
+ ChildInfo : childInfo ,
395
+ StartAtExclusive : true ,
396
+ }
397
+
398
+ if prefix != nil {
399
+ args .Prefix = * prefix
400
+ }
401
+
402
+ if startAt != nil {
403
+ args .StartAt = * startAt
404
+ }
405
+
406
+ rawIter , err := backend .RawIter (args )
407
+ if err != nil {
408
+ return nil , err
409
+ }
410
+
411
+ return & KeysIter [H , Hasher ]{backend , rawIter }, nil
412
+ }
413
+
414
+ func (ki * KeysIter [H , Hasher ]) Next () (storage.StorageKey , error ) {
415
+ key , err := ki .rawIter .NextKey (ki .backend )
416
+ return storage .StorageKey (key ), err
417
+ }
418
+
419
+ func (ki * KeysIter [H , Hasher ]) All () iter.Seq2 [storage.StorageKey , error ] {
420
+ return func (yield func (storage.StorageKey , error ) bool ) {
421
+ for {
422
+ item , err := ki .Next ()
423
+ if err != nil {
424
+ return
425
+ }
426
+ if item == nil {
427
+ return
428
+ }
429
+ if ! yield (item , err ) {
430
+ return
431
+ }
432
+ }
433
+ }
434
+ }
435
+
436
+ // StorageKeyData is a storage key/value pair.
437
+ type StorageKeyData struct {
438
+ storage.StorageKey
439
+ storage.StorageData
440
+ }
441
+
442
+ // PairsIter is an iterator over storage key/value pairs.
443
+ type PairsIter [H runtime.Hash , Hasher runtime.Hasher [H ]] struct {
444
+ backend * statemachine.TrieBackend [H , Hasher ]
445
+ rawIter statemachine.StorageIterator [H , Hasher ]
446
+ }
447
+
448
+ func NewPairsIter [H runtime.Hash , Hasher runtime.Hasher [H ]](
449
+ backend * statemachine.TrieBackend [H , Hasher ],
450
+ prefix * storage.StorageKey ,
451
+ startAt * storage.StorageKey ,
452
+ ) (* PairsIter [H , Hasher ], error ) {
453
+ args := statemachine.IterArgs {
454
+ StartAtExclusive : true ,
455
+ }
456
+
457
+ if prefix != nil {
458
+ args .Prefix = * prefix
459
+ }
460
+
461
+ if startAt != nil {
462
+ args .StartAt = * startAt
463
+ }
464
+
465
+ rawIter , err := backend .RawIter (args )
466
+ if err != nil {
467
+ return nil , err
468
+ }
469
+
470
+ return & PairsIter [H , Hasher ]{backend , rawIter }, nil
471
+ }
472
+
473
+ func NewChildPairsIter [H runtime.Hash , Hasher runtime.Hasher [H ]](
474
+ backend * statemachine.TrieBackend [H , Hasher ],
475
+ childInfo storage.ChildInfo ,
476
+ prefix * storage.StorageKey ,
477
+ startAt * storage.StorageKey ,
478
+ ) (* PairsIter [H , Hasher ], error ) {
479
+ args := statemachine.IterArgs {
480
+ ChildInfo : childInfo ,
481
+ StartAtExclusive : true ,
482
+ }
483
+
484
+ if prefix != nil {
485
+ args .Prefix = * prefix
486
+ }
487
+
488
+ if startAt != nil {
489
+ args .StartAt = * startAt
490
+ }
491
+
492
+ rawIter , err := backend .RawIter (args )
493
+ if err != nil {
494
+ return nil , err
495
+ }
496
+
497
+ return & PairsIter [H , Hasher ]{backend , rawIter }, nil
498
+ }
499
+
500
+ func (ki * PairsIter [H , Hasher ]) Next () (* StorageKeyData , error ) {
501
+ keyValue , err := ki .rawIter .NextKeyValue (ki .backend )
502
+ if err != nil {
503
+ return nil , err
504
+ }
505
+
506
+ data := StorageKeyData {
507
+ StorageKey : storage .StorageKey (keyValue .StorageKey ),
508
+ StorageData : storage .StorageData (keyValue .StorageValue ),
509
+ }
510
+
511
+ return & data , nil
512
+ }
513
+
514
+ func (ki * PairsIter [H , Hasher ]) All () iter.Seq2 [StorageKeyData , error ] {
515
+ return func (yield func (StorageKeyData , error ) bool ) {
516
+ for {
517
+ item , err := ki .Next ()
518
+ if err != nil {
519
+ return
520
+ }
521
+ if item == nil {
522
+ return
523
+ }
524
+ if ! yield (* item , err ) {
525
+ return
526
+ }
527
+ }
528
+ }
529
+ }
530
+
355
531
// StorageProvider provides access to storage primitives
356
532
type StorageProvider [H runtime.Hash , Hasher runtime.Hasher [H ]] interface {
357
533
// Storage returns the value under the key in that block, given a blocks hash and a key.
@@ -360,13 +536,13 @@ type StorageProvider[H runtime.Hash, Hasher runtime.Hasher[H]] interface {
360
536
// StorageHash returns the value under the hash in that block, given a blocks hash and a key.
361
537
StorageHash (hash H , key storage.StorageKey ) (* H , error )
362
538
363
- // StorageKeys returns a [statemachine. KeysIter] that iterates over matching storage keys in that block
539
+ // StorageKeys returns a [KeysIter] that iterates over matching storage keys in that block
364
540
// given a blocks hash and a key prefix.
365
- StorageKeys (hash H , prefix , startKey storage.StorageKey ) (statemachine. KeysIter [H , Hasher ], error )
541
+ StorageKeys (hash H , prefix , startKey storage.StorageKey ) (KeysIter [H , Hasher ], error )
366
542
367
543
// StoragePairs returns an iterator over the storage keys and values in that block,
368
544
// given the blocks hash and a key prefix.
369
- StoragePairs (hash H , prefix , startKey storage.StorageKey ) (statemachine. PairsIter [H , Hasher ], error )
545
+ StoragePairs (hash H , prefix , startKey storage.StorageKey ) (PairsIter [H , Hasher ], error )
370
546
371
547
// ChildStorage returns the value under the key in that block, given a blocks hash,
372
548
// a key and a child storage key.
@@ -376,14 +552,14 @@ type StorageProvider[H runtime.Hash, Hasher runtime.Hasher[H]] interface {
376
552
key storage.StorageKey ,
377
553
) (storage.StorageData , error )
378
554
379
- // ChildStorageKeys returns a [statemachine. KeysIter] that iterates matching storage keys in that block,
555
+ // ChildStorageKeys returns a [KeysIter] that iterates matching storage keys in that block,
380
556
// given a blocks hash, an optional key prefix and an optional child storage key.
381
557
ChildStorageKeys (
382
558
hash H ,
383
559
childInfo storage.ChildInfo ,
384
560
prefix storage.StorageKey ,
385
561
startKey storage.StorageKey ,
386
- ) (statemachine. KeysIter [H , Hasher ], error )
562
+ ) (KeysIter [H , Hasher ], error )
387
563
388
564
// ChildStorageHash returns the hash under the key in a block, given its hash,
389
565
// a key and a child storage key.
0 commit comments