@@ -545,31 +545,44 @@ impl std::error::Error for ApplyHeaderError {}
545
545
546
546
/// Applies `update_tip` onto `original_tip`.
547
547
///
548
- /// On success, a tuple is returned `(changeset, can_replace)`. If `can_replace` is true, then the
549
- /// `update_tip` can replace the `original_tip`.
548
+ /// On success, a tuple is returned ([`CheckPoint`], [`ChangeSet`]).
549
+ ///
550
+ /// # Errors
551
+ ///
552
+ /// [`CannotConnectError`] occurs when the `original_tip` and `update_tip` chains are disjoint:
553
+ ///
554
+ /// - If no point of agreement is found between the update and original chains.
555
+ /// - A point of agreement is found but we were unable to invalidate heights of the original chain.
556
+ /// - A point of agreement is found but we failed to apply the changeset to `CheckPoint`.
550
557
fn merge_chains (
551
558
original_tip : CheckPoint ,
552
559
update_tip : CheckPoint ,
553
560
) -> Result < ( CheckPoint , ChangeSet ) , CannotConnectError > {
554
561
let mut changeset = ChangeSet :: default ( ) ;
562
+
555
563
let mut orig = original_tip. iter ( ) ;
556
564
let mut update = update_tip. iter ( ) ;
565
+
557
566
let mut curr_orig = None ;
558
567
let mut curr_update = None ;
568
+
559
569
let mut prev_orig: Option < CheckPoint > = None ;
560
570
let mut prev_update: Option < CheckPoint > = None ;
571
+
561
572
let mut point_of_agreement_found = false ;
573
+
562
574
let mut prev_orig_was_invalidated = false ;
575
+
563
576
let mut potentially_invalidated_heights = vec ! [ ] ;
564
577
565
578
// If we can, we want to return the update tip as the new tip because this allows checkpoints
566
579
// in multiple locations to keep the same `Arc` pointers when they are being updated from each
567
- // other using this function. We can do this as long as long as the update contains every
580
+ // other using this function. We can do this as long as the update contains every
568
581
// block's height of the original chain.
569
582
let mut is_update_height_superset_of_original = true ;
570
583
571
584
// To find the difference between the new chain and the original we iterate over both of them
572
- // from the tip backwards in tandem. We always dealing with the highest one from either chain
585
+ // from the tip backwards in tandem. We are always dealing with the highest one from either chain
573
586
// first and move to the next highest. The crucial logic is applied when they have blocks at the
574
587
// same height.
575
588
loop {
0 commit comments