Skip to content

Commit 5efa472

Browse files
committed
fix(docs): merge_chains documentation
- it's a small fix for `merge_chains` docs, reported on audit. - adds an `Errors` section to cover what scenarios it can fail.
1 parent 739b54f commit 5efa472

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

crates/chain/src/local_chain.rs

+23-4
Original file line numberDiff line numberDiff line change
@@ -545,31 +545,50 @@ impl std::error::Error for ApplyHeaderError {}
545545

546546
/// Applies `update_tip` onto `original_tip`.
547547
///
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+
/// - It found a PoA, but it was unable to invalidate previous blocks from the original chain.
555+
/// - It found a PoA, with same underlying pointer, but it's `update` chain is not a superset of `original` chain,
556+
/// and genesis block connectivity failed.
557+
/// - It didn't find a PoA, and didn't successfully invalidated the previous blocks from the original chain.
558+
/// - It didn't find a PoA, but the genesis block connectivity test failed.
559+
///
560+
/// [`CheckPoint`]: bdk_core::CheckPoint
561+
/// [`ChangeSet`]: bdk_chain::local_chain::CheckPoint
562+
/// [`CannotConnectError`]: bdk_chain::local_chain::CannotConnectError
550563
fn merge_chains(
551564
original_tip: CheckPoint,
552565
update_tip: CheckPoint,
553566
) -> Result<(CheckPoint, ChangeSet), CannotConnectError> {
554567
let mut changeset = ChangeSet::default();
568+
555569
let mut orig = original_tip.iter();
556570
let mut update = update_tip.iter();
571+
557572
let mut curr_orig = None;
558573
let mut curr_update = None;
574+
559575
let mut prev_orig: Option<CheckPoint> = None;
560576
let mut prev_update: Option<CheckPoint> = None;
577+
561578
let mut point_of_agreement_found = false;
579+
562580
let mut prev_orig_was_invalidated = false;
581+
563582
let mut potentially_invalidated_heights = vec![];
564583

565584
// If we can, we want to return the update tip as the new tip because this allows checkpoints
566585
// 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
586+
// other using this function. We can do this as long as the update contains every
568587
// block's height of the original chain.
569588
let mut is_update_height_superset_of_original = true;
570589

571590
// 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
591+
// from the tip backwards in tandem. We are always dealing with the highest one from either chain
573592
// first and move to the next highest. The crucial logic is applied when they have blocks at the
574593
// same height.
575594
loop {

0 commit comments

Comments
 (0)