Skip to content

Commit

Permalink
Fix reorg depth count (#5160)
Browse files Browse the repository at this point in the history
* Fix reorg depth count to be the diff of the max of new & old head slots and the common ancestor slot (#5136)

* doc: Updated tsdocs in comments for getCommonAncestorDepth function (#5136)

* Remove stale comments

---------

Co-authored-by: Cayman <caymannava@gmail.com>
  • Loading branch information
maschad and wemeetagain authored Feb 16, 2023
1 parent 92ab83d commit 63479b4
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/fork-choice/src/forkChoice/forkChoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ export class ForkChoice implements IForkChoice {
return blocksAtSlot;
}

/** Returns the distance of common ancestor of nodes to newNode. Returns null if newNode is descendant of prevNode */
/** Returns the distance of common ancestor of nodes to the max of the newNode and the prevNode. */
getCommonAncestorDepth(prevBlock: ProtoBlock, newBlock: ProtoBlock): AncestorResult {
const prevNode = this.protoArray.getNode(prevBlock.blockRoot);
const newNode = this.protoArray.getNode(newBlock.blockRoot);
Expand All @@ -792,7 +792,7 @@ export class ForkChoice implements IForkChoice {
return {code: AncestorStatus.Descendant};
}

return {code: AncestorStatus.CommonAncestor, depth: newNode.slot - commonAncestor.slot};
return {code: AncestorStatus.CommonAncestor, depth: Math.max(newNode.slot, prevNode.slot) - commonAncestor.slot};
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/fork-choice/src/forkChoice/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export interface IForkChoice {
forwardIterateDescendants(blockRoot: RootHex): IterableIterator<ProtoBlock>;
getBlockSummariesByParentRoot(parentRoot: RootHex): ProtoBlock[];
getBlockSummariesAtSlot(slot: Slot): ProtoBlock[];
/** Returns the distance of common ancestor of nodes to newNode. Returns null if newNode is descendant of prevNode */
/** Returns the distance of common ancestor of nodes to the max of the newNode and the prevNode. */
getCommonAncestorDepth(prevBlock: ProtoBlock, newBlock: ProtoBlock): AncestorResult;
/**
* Optimistic sync validate till validated latest hash, invalidate any decendant branch if invalidated branch decendant provided
Expand Down

0 comments on commit 63479b4

Please sign in to comment.