@@ -327,7 +327,7 @@ func (m *Manager) requestChangeProof(ctx context.Context, work *workItem) {
327
327
328
328
if work .localRootID == targetRootID {
329
329
// Start root is the same as the end root, so we're done.
330
- m .completeWorkItem (ctx , work , work .end , targetRootID , nil )
330
+ m .completeWorkItem (work , work .end , targetRootID )
331
331
m .finishWorkItem ()
332
332
return
333
333
}
@@ -342,7 +342,7 @@ func (m *Manager) requestChangeProof(ctx context.Context, work *workItem) {
342
342
return
343
343
}
344
344
work .start = maybe .Nothing [[]byte ]()
345
- m .completeWorkItem (ctx , work , maybe .Nothing [[]byte ](), targetRootID , nil )
345
+ m .completeWorkItem (work , maybe .Nothing [[]byte ](), targetRootID )
346
346
return
347
347
}
348
348
@@ -401,7 +401,7 @@ func (m *Manager) requestRangeProof(ctx context.Context, work *workItem) {
401
401
return
402
402
}
403
403
work .start = maybe .Nothing [[]byte ]()
404
- m .completeWorkItem (ctx , work , maybe .Nothing [[]byte ](), targetRootID , nil )
404
+ m .completeWorkItem (work , maybe .Nothing [[]byte ](), targetRootID )
405
405
return
406
406
}
407
407
@@ -545,7 +545,17 @@ func (m *Manager) handleRangeProofResponse(
545
545
largestHandledKey = maybe .Some (rangeProof .KeyChanges [len (rangeProof .KeyChanges )- 1 ].Key )
546
546
}
547
547
548
- m .completeWorkItem (ctx , work , largestHandledKey , targetRootID , rangeProof .EndProof )
548
+ // Find the next key to fetch.
549
+ // If this is empty, then we have no more keys to fetch.
550
+ if ! largestHandledKey .IsNothing () {
551
+ nextKey , err := m .findNextKey (ctx , largestHandledKey .Value (), work .end , rangeProof .EndProof )
552
+ if err != nil {
553
+ m .setError (err )
554
+ return nil
555
+ }
556
+ largestHandledKey = nextKey
557
+ }
558
+ m .completeWorkItem (work , largestHandledKey , targetRootID )
549
559
return nil
550
560
}
551
561
@@ -569,6 +579,10 @@ func (m *Manager) handleChangeProofResponse(
569
579
startKey := maybeBytesToMaybe (request .StartKey )
570
580
endKey := maybeBytesToMaybe (request .EndKey )
571
581
582
+ var (
583
+ largestHandledKey maybe.Maybe [[]byte ]
584
+ endProof []merkledb.ProofNode
585
+ )
572
586
switch changeProofResp := changeProofResp .Response .(type ) {
573
587
case * pb.SyncGetChangeProofResponse_ChangeProof :
574
588
// The server had enough history to send us a change proof
@@ -601,7 +615,7 @@ func (m *Manager) handleChangeProofResponse(
601
615
return fmt .Errorf ("%w due to %w" , errInvalidChangeProof , err )
602
616
}
603
617
604
- largestHandledKey : = work .end
618
+ largestHandledKey = work .end
605
619
// if the proof wasn't empty, apply changes to the sync DB
606
620
if len (changeProof .KeyChanges ) > 0 {
607
621
if err := m .config .DB .CommitChangeProof (ctx , & changeProof ); err != nil {
@@ -610,8 +624,8 @@ func (m *Manager) handleChangeProofResponse(
610
624
}
611
625
largestHandledKey = maybe .Some (changeProof .KeyChanges [len (changeProof .KeyChanges )- 1 ].Key )
612
626
}
627
+ endProof = changeProof .EndProof
613
628
614
- m .completeWorkItem (ctx , work , largestHandledKey , targetRootID , changeProof .EndProof )
615
629
case * pb.SyncGetChangeProofResponse_RangeProof :
616
630
var rangeProof merkledb.RangeProof
617
631
if err := rangeProof .UnmarshalProto (changeProofResp .RangeProof ); err != nil {
@@ -633,7 +647,7 @@ func (m *Manager) handleChangeProofResponse(
633
647
return err
634
648
}
635
649
636
- largestHandledKey : = work .end
650
+ largestHandledKey = work .end
637
651
if len (rangeProof .KeyChanges ) > 0 {
638
652
// Add all the key-value pairs we got to the database.
639
653
if err := m .config .DB .CommitRangeProof (ctx , work .start , work .end , & rangeProof ); err != nil {
@@ -642,14 +656,25 @@ func (m *Manager) handleChangeProofResponse(
642
656
}
643
657
largestHandledKey = maybe .Some (rangeProof .KeyChanges [len (rangeProof .KeyChanges )- 1 ].Key )
644
658
}
659
+ endProof = rangeProof .EndProof
645
660
646
- m .completeWorkItem (ctx , work , largestHandledKey , targetRootID , rangeProof .EndProof )
647
661
default :
648
662
return fmt .Errorf (
649
663
"%w: %T" ,
650
664
errUnexpectedChangeProofResponse , changeProofResp ,
651
665
)
652
666
}
667
+ // Find the next key to fetch.
668
+ // If this is empty, then we have no more keys to fetch.
669
+ if ! largestHandledKey .IsNothing () {
670
+ nextKey , err := m .findNextKey (ctx , largestHandledKey .Value (), work .end , endProof )
671
+ if err != nil {
672
+ m .setError (err )
673
+ return nil
674
+ }
675
+ largestHandledKey = nextKey
676
+ }
677
+ m .completeWorkItem (work , largestHandledKey , targetRootID )
653
678
654
679
return nil
655
680
}
@@ -933,28 +958,14 @@ func (m *Manager) setError(err error) {
933
958
// that gave us the range up to and including [largestHandledKey].
934
959
//
935
960
// Assumes [m.workLock] is not held.
936
- func (m * Manager ) completeWorkItem (ctx context. Context , work * workItem , largestHandledKey maybe.Maybe [[]byte ], rootID ids.ID , proofOfLargestKey []merkledb. ProofNode ) {
961
+ func (m * Manager ) completeWorkItem (work * workItem , largestHandledKey maybe.Maybe [[]byte ], rootID ids.ID ) {
937
962
if ! maybe .Equal (largestHandledKey , work .end , bytes .Equal ) {
938
- // The largest handled key isn't equal to the end of the work item.
939
- // Find the start of the next key range to fetch.
940
- // Note that [largestHandledKey] can't be Nothing.
941
- // Proof: Suppose it is. That means that we got a range/change proof that proved up to the
942
- // greatest key-value pair in the database. That means we requested a proof with no upper
943
- // bound. That is, [workItem.end] is Nothing. Since we're here, [bothNothing] is false,
944
- // which means [workItem.end] isn't Nothing. Contradiction.
945
- nextStartKey , err := m .findNextKey (ctx , largestHandledKey .Value (), work .end , proofOfLargestKey )
946
- if err != nil {
947
- m .setError (err )
948
- return
949
- }
950
-
951
- // nextStartKey being Nothing indicates that the entire range has been completed
952
- if nextStartKey .IsNothing () {
963
+ // largestHandledKey being Nothing indicates that the entire range has been completed
964
+ if largestHandledKey .IsNothing () {
953
965
largestHandledKey = work .end
954
966
} else {
955
967
// the full range wasn't completed, so enqueue a new work item for the range [nextStartKey, workItem.end]
956
- m .enqueueWork (newWorkItem (work .localRootID , nextStartKey , work .end , work .priority , time .Now ()))
957
- largestHandledKey = nextStartKey
968
+ m .enqueueWork (newWorkItem (work .localRootID , largestHandledKey , work .end , work .priority , time .Now ()))
958
969
}
959
970
}
960
971
0 commit comments