@@ -463,115 +463,100 @@ func hasRightElement(node node, key []byte) bool {
463
463
//
464
464
// Except returning the error to indicate the proof is valid or not, the function will
465
465
// also return a flag to indicate whether there exists more accounts/slots in the trie.
466
- func VerifyRangeProof (rootHash common.Hash , firstKey []byte , lastKey []byte , keys [][]byte , values [][]byte , proof ethdb.KeyValueReader ) (ethdb.KeyValueStore , * Trie , * KeyValueNotary , bool , error ) {
466
+ func VerifyRangeProof (rootHash common.Hash , firstKey []byte , lastKey []byte , keys [][]byte , values [][]byte , proof ethdb.KeyValueReader ) (ethdb.KeyValueStore , bool , error ) {
467
467
if len (keys ) != len (values ) {
468
- return nil , nil , nil , false , fmt .Errorf ("inconsistent proof data, keys: %d, values: %d" , len (keys ), len (values ))
468
+ return nil , false , fmt .Errorf ("inconsistent proof data, keys: %d, values: %d" , len (keys ), len (values ))
469
469
}
470
470
// Ensure the received batch is monotonic increasing.
471
471
for i := 0 ; i < len (keys )- 1 ; i ++ {
472
472
if bytes .Compare (keys [i ], keys [i + 1 ]) >= 0 {
473
- return nil , nil , nil , false , errors .New ("range is not monotonically increasing" )
473
+ return nil , false , errors .New ("range is not monotonically increasing" )
474
474
}
475
475
}
476
476
// Create a key-value notary to track which items from the given proof the
477
477
// range prover actually needed to verify the data
478
- notary := NewKeyValueNotary (proof )
478
+ notary := newKeyValueNotary (proof )
479
479
480
480
// Special case, there is no edge proof at all. The given range is expected
481
481
// to be the whole leaf-set in the trie.
482
482
if proof == nil {
483
483
var (
484
484
diskdb = memorydb .New ()
485
- triedb = NewDatabase (diskdb )
485
+ tr = NewStackTrie (diskdb )
486
486
)
487
- tr , err := New (common.Hash {}, triedb )
488
- if err != nil {
489
- return nil , nil , nil , false , err
490
- }
491
487
for index , key := range keys {
492
488
tr .TryUpdate (key , values [index ])
493
489
}
494
- if tr .Hash () != rootHash {
495
- return nil , nil , nil , false , fmt .Errorf ("invalid proof, want hash %x, got %x" , rootHash , tr .Hash ())
496
- }
497
- // Proof seems valid, serialize all the nodes into the database
498
- if _ , err := tr .Commit (nil ); err != nil {
499
- return nil , nil , nil , false , err
490
+ if have , want := tr .Hash (), rootHash ; have != want {
491
+ return nil , false , fmt .Errorf ("invalid proof, want hash %x, got %x" , want , have )
500
492
}
501
- if err := triedb .Commit (rootHash , false ); err != nil {
502
- return nil , nil , nil , false , err
493
+ // Proof seems valid, serialize remaining nodes into the database
494
+ if _ , err := tr .Commit (); err != nil {
495
+ return nil , false , err
503
496
}
504
- return diskdb , tr , notary , false , nil // No more elements
497
+ return diskdb , false , nil // No more elements
505
498
}
506
499
// Special case, there is a provided edge proof but zero key/value
507
500
// pairs, ensure there are no more accounts / slots in the trie.
508
501
if len (keys ) == 0 {
509
502
root , val , err := proofToPath (rootHash , nil , firstKey , notary , true )
510
503
if err != nil {
511
- return nil , nil , nil , false , err
504
+ return nil , false , err
512
505
}
513
506
if val != nil || hasRightElement (root , firstKey ) {
514
- return nil , nil , nil , false , errors .New ("more entries available" )
507
+ return nil , false , errors .New ("more entries available" )
515
508
}
516
509
// Since the entire proof is a single path, we can construct a trie and a
517
510
// node database directly out of the inputs, no need to generate them
518
511
diskdb := notary .Accessed ()
519
- tr := & Trie {
520
- Db : NewDatabase (diskdb ),
521
- root : root ,
522
- }
523
- return diskdb , tr , notary , hasRightElement (root , firstKey ), nil
512
+ return diskdb , hasRightElement (root , firstKey ), nil
524
513
}
525
514
// Special case, there is only one element and two edge keys are same.
526
515
// In this case, we can't construct two edge paths. So handle it here.
527
516
if len (keys ) == 1 && bytes .Equal (firstKey , lastKey ) {
528
517
root , val , err := proofToPath (rootHash , nil , firstKey , notary , false )
529
518
if err != nil {
530
- return nil , nil , nil , false , err
519
+ return nil , false , err
531
520
}
532
521
if ! bytes .Equal (firstKey , keys [0 ]) {
533
- return nil , nil , nil , false , errors .New ("correct proof but invalid key" )
522
+ return nil , false , errors .New ("correct proof but invalid key" )
534
523
}
535
524
if ! bytes .Equal (val , values [0 ]) {
536
- return nil , nil , nil , false , errors .New ("correct proof but invalid data" )
525
+ return nil , false , errors .New ("correct proof but invalid data" )
537
526
}
538
527
// Since the entire proof is a single path, we can construct a trie and a
539
528
// node database directly out of the inputs, no need to generate them
540
529
diskdb := notary .Accessed ()
541
- tr := & Trie {
542
- Db : NewDatabase (diskdb ),
543
- root : root ,
544
- }
545
- return diskdb , tr , notary , hasRightElement (root , firstKey ), nil
530
+ return diskdb , hasRightElement (root , firstKey ), nil
546
531
}
547
532
// Ok, in all other cases, we require two edge paths available.
548
533
// First check the validity of edge keys.
549
534
if bytes .Compare (firstKey , lastKey ) >= 0 {
550
- return nil , nil , nil , false , errors .New ("invalid edge keys" )
535
+ return nil , false , errors .New ("invalid edge keys" )
551
536
}
552
537
// todo(rjl493456442) different length edge keys should be supported
553
538
if len (firstKey ) != len (lastKey ) {
554
- return nil , nil , nil , false , errors .New ("inconsistent edge keys" )
539
+ return nil , false , errors .New ("inconsistent edge keys" )
555
540
}
556
541
// Convert the edge proofs to edge trie paths. Then we can
557
542
// have the same tree architecture with the original one.
558
543
// For the first edge proof, non-existent proof is allowed.
559
544
root , _ , err := proofToPath (rootHash , nil , firstKey , notary , true )
560
545
if err != nil {
561
- return nil , nil , nil , false , err
546
+ return nil , false , err
562
547
}
563
548
// Pass the root node here, the second path will be merged
564
549
// with the first one. For the last edge proof, non-existent
565
550
// proof is also allowed.
566
551
root , _ , err = proofToPath (rootHash , root , lastKey , notary , true )
567
552
if err != nil {
568
- return nil , nil , nil , false , err
553
+ return nil , false , err
569
554
}
570
555
// Remove all internal references. All the removed parts should
571
556
// be re-filled(or re-constructed) by the given leaves range.
572
557
empty , err := unsetInternal (root , firstKey , lastKey )
573
558
if err != nil {
574
- return nil , nil , nil , false , err
559
+ return nil , false , err
575
560
}
576
561
// Rebuild the trie with the leaf stream, the shape of trie
577
562
// should be same with the original one.
@@ -587,16 +572,16 @@ func VerifyRangeProof(rootHash common.Hash, firstKey []byte, lastKey []byte, key
587
572
tr .TryUpdate (key , values [index ])
588
573
}
589
574
if tr .Hash () != rootHash {
590
- return nil , nil , nil , false , fmt .Errorf ("invalid proof, want hash %x, got %x" , rootHash , tr .Hash ())
575
+ return nil , false , fmt .Errorf ("invalid proof, want hash %x, got %x" , rootHash , tr .Hash ())
591
576
}
592
577
// Proof seems valid, serialize all the nodes into the database
593
578
if _ , err := tr .Commit (nil ); err != nil {
594
- return nil , nil , nil , false , err
579
+ return nil , false , err
595
580
}
596
581
if err := triedb .Commit (rootHash , false ); err != nil {
597
- return nil , nil , nil , false , err
582
+ return nil , false , err
598
583
}
599
- return diskdb , tr , notary , hasRightElement (root , keys [len (keys )- 1 ]), nil
584
+ return diskdb , hasRightElement (root , keys [len (keys )- 1 ]), nil
600
585
}
601
586
602
587
// get returns the child of the given Node. Return nil if the
0 commit comments