Skip to content

fix(docs): merge_chains outdated documentation #1806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions crates/chain/src/local_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,31 +545,46 @@ impl std::error::Error for ApplyHeaderError {}

/// Applies `update_tip` onto `original_tip`.
///
/// On success, a tuple is returned `(changeset, can_replace)`. If `can_replace` is true, then the
/// `update_tip` can replace the `original_tip`.
/// On success, a tuple is returned ([`CheckPoint`], [`ChangeSet`]).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// On success, a tuple is returned ([`CheckPoint`], [`ChangeSet`]).
/// On success, a tuple is returned ([`CheckPoint`], [`ChangeSet`]). A [`CannotConnectError`] occurs when the `original_tip` and `update_tip` chains are disjoint.

I'd move the rest of the explanation for CannotConnectError to that type's doc.

But I don't want to bike-shed it too much so I'll also ACK this as is and you decide.

///
/// # Errors
///
/// [`CannotConnectError`] occurs when the `original_tip` and `update_tip` chains are disjoint:
///
/// - If no point of agreement is found between the update and original chains.
/// - A point of agreement is found but the update is ambiguous above the point of agreement (a.k.a.
/// the update and original chain both have a block above the point of agreement, but their
/// heights do not overlap).
/// - The update attempts to replace the genesis block of the original chain.
fn merge_chains(
original_tip: CheckPoint,
update_tip: CheckPoint,
) -> Result<(CheckPoint, ChangeSet), CannotConnectError> {
let mut changeset = ChangeSet::default();

let mut orig = original_tip.iter();
let mut update = update_tip.iter();

let mut curr_orig = None;
let mut curr_update = None;

let mut prev_orig: Option<CheckPoint> = None;
let mut prev_update: Option<CheckPoint> = None;

let mut point_of_agreement_found = false;

let mut prev_orig_was_invalidated = false;

let mut potentially_invalidated_heights = vec![];

// If we can, we want to return the update tip as the new tip because this allows checkpoints
// in multiple locations to keep the same `Arc` pointers when they are being updated from each
// other using this function. We can do this as long as long as the update contains every
// other using this function. We can do this as long as the update contains every
// block's height of the original chain.
let mut is_update_height_superset_of_original = true;

// To find the difference between the new chain and the original we iterate over both of them
// from the tip backwards in tandem. We always dealing with the highest one from either chain
// from the tip backwards in tandem. We are always dealing with the highest one from either chain
// first and move to the next highest. The crucial logic is applied when they have blocks at the
// same height.
loop {
Expand Down
Loading