Skip to content

Commit

Permalink
Finalized ancestor check.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsson49 committed Nov 4, 2019
1 parent f090573 commit 3be8d03
Showing 1 changed file with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,24 @@ private BeaconTuple fetchRecentTuple() {
() -> new RuntimeException("Block with stored maxSlot not found, maxSlot: " + maxSlot));
}

private Hash32 getAncestor(Hash32 root, SlotNumber slot) {
Optional<BeaconBlock> beaconBlock = chainStorage.getBlockStorage().get(root);
if (!beaconBlock.isPresent()) {
throw new IllegalArgumentException("Cannot find block " + root);
}
return getAncestor(root, beaconBlock.get(), slot);
}

private Hash32 getAncestor(Hash32 root, BeaconBlock block, SlotNumber slot) {
if (block.getSlot().greater(slot)) {
return getAncestor(block.getParentRoot(), slot);
} else if (block.getSlot().equals(slot)) {
return root;
} else {
return Hash32.ZERO;
}
}

@Override
public synchronized ImportResult insert(BeaconBlock block) {
if (rejectedByTime(block)) {
Expand All @@ -96,6 +114,13 @@ public synchronized ImportResult insert(BeaconBlock block) {

long s = System.nanoTime();

Hash32 finalizedRoot = chainStorage.getFinalizedStorage().get().get().getRoot();
SlotNumber finalizedSlot = chainStorage.getBlockStorage().get(finalizedRoot).get().getSlot();
Hash32 finalizedAncestor = getAncestor(spec.signing_root(block), block, finalizedSlot);
if (!finalizedAncestor.equals(finalizedRoot)) {
return ImportResult.ExpiredBlock;
}

BeaconStateEx parentState = pullParentState(block);

BeaconStateEx preBlockState = preBlockTransition.apply(parentState, block.getSlot());
Expand Down

0 comments on commit 3be8d03

Please sign in to comment.