From 3be8d0387549224f31cb6393b4382683c9454818 Mon Sep 17 00:00:00 2001 From: Alex Vlasov/ericsson49 Date: Mon, 4 Nov 2019 15:38:49 +0300 Subject: [PATCH] Finalized ancestor check. https://github.com/harmony-dev/beacon-chain-java/issues/191 --- .../beacon/chain/DefaultBeaconChain.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/chain/src/main/java/org/ethereum/beacon/chain/DefaultBeaconChain.java b/chain/src/main/java/org/ethereum/beacon/chain/DefaultBeaconChain.java index cd1bdcb42..fa068a898 100644 --- a/chain/src/main/java/org/ethereum/beacon/chain/DefaultBeaconChain.java +++ b/chain/src/main/java/org/ethereum/beacon/chain/DefaultBeaconChain.java @@ -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 = 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)) { @@ -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());