Skip to content

Commit

Permalink
Implemented isFinalizedRootOrDescendant (Consensys#7606)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucassaldanha authored Oct 19, 2023
1 parent 7fb8eb1 commit d418f8d
Showing 1 changed file with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -639,13 +639,44 @@ public boolean nodeIsViableForHead(ProtoNode node) {
.isGreaterThanOrEqualTo(currentEpoch);
}

final UInt64 finalizedSlot = spec.computeStartSlotAtEpoch(finalizedCheckpoint.getEpoch());
final boolean correctFinalized =
node.getFinalizedCheckpoint().getEpoch().equals(initialEpoch)
|| hasAncestorAtSlot(node, finalizedSlot, finalizedCheckpoint.getRoot());
|| isFinalizedRootOrDescendant(node);
return correctJustified && correctFinalized;
}

private boolean isFinalizedRootOrDescendant(final ProtoNode node) {
final UInt64 finalizedEpoch = finalizedCheckpoint.getEpoch();
final Bytes32 finalizedRoot = finalizedCheckpoint.getRoot();

final Checkpoint nodeFinalizedCheckpoint = node.getFinalizedCheckpoint();
if (nodeFinalizedCheckpoint.getEpoch().equals(finalizedEpoch)
&& nodeFinalizedCheckpoint.getRoot().equals(finalizedRoot)) {
return true;
}

final Checkpoint nodeJustifiedCheckpoint = node.getJustifiedCheckpoint();
if (nodeJustifiedCheckpoint.getEpoch().equals(finalizedEpoch)
&& nodeJustifiedCheckpoint.getRoot().equals(finalizedRoot)) {
return true;
}

final Checkpoint nodeUnrealizedFinalizedCheckpoint = node.getUnrealizedFinalizedCheckpoint();
if (nodeUnrealizedFinalizedCheckpoint.getEpoch().equals(finalizedEpoch)
&& nodeUnrealizedFinalizedCheckpoint.getRoot().equals(finalizedRoot)) {
return true;
}

final Checkpoint nodeUnrealizedJustifiedCheckpoint = node.getUnrealizedJustifiedCheckpoint();
if (nodeUnrealizedJustifiedCheckpoint.getEpoch().equals(finalizedEpoch)
&& nodeUnrealizedJustifiedCheckpoint.getRoot().equals(finalizedRoot)) {
return true;
}

final UInt64 finalizedSlot = spec.computeStartSlotAtEpoch(finalizedCheckpoint.getEpoch());
return hasAncestorAtSlot(node, finalizedSlot, finalizedRoot);
}

/**
* This is similar to the <a
* href="https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/fork-choice.md#get_ancestor">get_ancestor</a>
Expand Down

0 comments on commit d418f8d

Please sign in to comment.