Skip to content

Commit

Permalink
fix: improve forkchoice getBlock hasBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Aug 23, 2023
1 parent cdfbd85 commit 3d6d19b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
31 changes: 15 additions & 16 deletions packages/fork-choice/src/forkChoice/forkChoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,12 @@ export class ForkChoice implements IForkChoice {
* Returns `true` if the block is known **and** a descendant of the finalized root.
*/
hasBlockHex(blockRoot: RootHex): boolean {
return this.protoArray.hasBlock(blockRoot) && this.isDescendantOfFinalized(blockRoot);
const node = this.protoArray.getNode(blockRoot);
if (node === undefined) {
return false;
}

return this.protoArray.isFinalizedRootOrDescendant(node);
}

/**
Expand All @@ -610,20 +615,21 @@ export class ForkChoice implements IForkChoice {
}

/**
* Returns a `ProtoBlock` if the block is known **and** a descendant of the finalized root.
* Returns a MUTABLE `ProtoBlock` if the block is known **and** a descendant of the finalized root.
*/
getBlockHex(blockRoot: RootHex): ProtoBlock | null {
const block = this.protoArray.getBlock(blockRoot);
if (!block) {
const node = this.protoArray.getNode(blockRoot);
if (!node) {
return null;
}
// If available, use the parent_root to perform the lookup since it will involve one
// less lookup. This involves making the assumption that the finalized block will
// always have `block.parent_root` of `None`.
if (!this.isDescendantOfFinalized(blockRoot)) {

if (!this.protoArray.isFinalizedRootOrDescendant(node)) {
return null;
}
return block;

return {
...node,
};
}

getJustifiedBlock(): ProtoBlock {
Expand All @@ -648,13 +654,6 @@ export class ForkChoice implements IForkChoice {
return block;
}

/**
* Return `true` if `block_root` is equal to the finalized root, or a known descendant of it.
*/
isDescendantOfFinalized(blockRoot: RootHex): boolean {
return this.protoArray.isDescendant(this.fcStore.finalizedCheckpoint.rootHex, blockRoot);
}

/**
* Returns true if the `descendantRoot` has an ancestor with `ancestorRoot`.
*
Expand Down
4 changes: 0 additions & 4 deletions packages/fork-choice/src/forkChoice/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,6 @@ export interface IForkChoice {
getBlockHex(blockRoot: RootHex): ProtoBlock | null;
getFinalizedBlock(): ProtoBlock;
getJustifiedBlock(): ProtoBlock;
/**
* Return `true` if `block_root` is equal to the finalized root, or a known descendant of it.
*/
isDescendantOfFinalized(blockRoot: RootHex): boolean;
/**
* Returns true if the `descendantRoot` has an ancestor with `ancestorRoot`.
*
Expand Down

0 comments on commit 3d6d19b

Please sign in to comment.