From c04a0552c17e154aaca28a5c1c74f2a65a83e641 Mon Sep 17 00:00:00 2001 From: Stefan Pingel <16143240+pinges@users.noreply.github.com> Date: Tue, 29 Aug 2023 14:01:38 +1000 Subject: [PATCH] fix ForkId if there are no Forks and the starting timestamp is not 0 (#5819) Signed-off-by: Stefan --- .../besu/ethereum/forkid/ForkIdManager.java | 6 +++--- .../besu/ethereum/forkid/ForkIdTest.java | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/forkid/ForkIdManager.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/forkid/ForkIdManager.java index 61457b5bbce..bae8cfc339e 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/forkid/ForkIdManager.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/forkid/ForkIdManager.java @@ -45,7 +45,7 @@ public class ForkIdManager { private final Supplier chainHeadSupplier; private final long forkNext; - private final boolean onlyZerosForkBlocks; + private final boolean noForksAvailable; private final long highestKnownFork; private Bytes genesisHashCrc; private final boolean legacyEth64; @@ -77,11 +77,11 @@ public ForkIdManager( final List allForkNumbers = Stream.concat(blockNumberForks.stream(), timestampForks.stream()) .collect(Collectors.toList()); - this.onlyZerosForkBlocks = allForkNumbers.stream().allMatch(value -> 0L == value); this.forkNext = createForkIds(); this.allForkIds = Stream.concat(blockNumbersForkIds.stream(), timestampsForkIds.stream()) .collect(Collectors.toList()); + this.noForksAvailable = allForkIds.isEmpty(); this.highestKnownFork = !allForkNumbers.isEmpty() ? allForkNumbers.get(allForkNumbers.size() - 1) : 0L; } @@ -128,7 +128,7 @@ public static ForkId readFrom(final RLPInput in) { * @return boolean (peer valid (true) or invalid (false)) */ public boolean peerCheck(final ForkId forkId) { - if (forkId == null || onlyZerosForkBlocks) { + if (forkId == null || noForksAvailable) { return true; // Another method must be used to validate (i.e. genesis hash) } // Run the fork checksum validation rule set: diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/forkid/ForkIdTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/forkid/ForkIdTest.java index 87f210bfa00..c9c727286cd 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/forkid/ForkIdTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/forkid/ForkIdTest.java @@ -619,4 +619,18 @@ public void testGenesisTimestampEqualToShanghaiTimestamp() { assertThat(forkIdManager.getAllForkIds().get(0).getNext()).isEqualTo(2L); assertThat(forkIdManager.getAllForkIds().get(1).getNext()).isEqualTo(0L); } + + @Test + public void testNoBlockNoForksAndNoTimestampForksGreaterThanGenesisTimestamp() { + final ForkIdManager forkIdManager = + new ForkIdManager( + mockBlockchain(Hash.ZERO.toHexString(), 10L, 1L), + Collections.emptyList(), + List.of(1L), + false); + assertThat(forkIdManager.getAllForkIds().size()).isEqualTo(0); + // There is no ForkId, so peerCheck always has to return true + assertThat(forkIdManager.peerCheck(new ForkId(Bytes.fromHexString("0xdeadbeef"), 100L))) + .isEqualTo(true); + } }