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 6cb560192ef..61457b5bbce 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 @@ -70,7 +70,7 @@ public ForkIdManager( .collect(Collectors.toUnmodifiableList()); this.timestampForks = timestampForks.stream() - .filter(fork -> fork > 0L) + .filter(fork -> fork > blockchain.getGenesisBlock().getHeader().getTimestamp()) .distinct() .sorted() .collect(Collectors.toUnmodifiableList()); 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 5e7b28514d1..87f210bfa00 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 @@ -21,16 +21,19 @@ import static org.hyperledger.besu.ethereum.forkid.ForkIdTestUtil.mockBlockchain; import static org.hyperledger.besu.ethereum.forkid.ForkIdTestUtil.wantPeerCheck; +import org.hyperledger.besu.datatypes.Hash; import org.hyperledger.besu.ethereum.forkid.ForkIdTestUtil.ForkIds; import org.hyperledger.besu.ethereum.forkid.ForkIdTestUtil.GenesisHash; import org.hyperledger.besu.ethereum.forkid.ForkIdTestUtil.Network; import org.hyperledger.besu.ethereum.forkid.ForkIdTestUtil.PeerCheckCase; +import java.util.Collections; import java.util.List; import java.util.Optional; import java.util.stream.Stream; import org.apache.tuweni.bytes.Bytes; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -603,4 +606,17 @@ public void test( peerCheckCase.forkIdNext))) .isEqualTo(peerCheckCase.want)); } + + @Test + public void testGenesisTimestampEqualToShanghaiTimestamp() { + final ForkIdManager forkIdManager = + new ForkIdManager( + mockBlockchain(Hash.ZERO.toHexString(), 10L, 0L), + Collections.emptyList(), + List.of(1L, 2L), + false); + assertThat(forkIdManager.getAllForkIds().size()).isEqualTo(2); + assertThat(forkIdManager.getAllForkIds().get(0).getNext()).isEqualTo(2L); + assertThat(forkIdManager.getAllForkIds().get(1).getNext()).isEqualTo(0L); + } } diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/forkid/ForkIdTestUtil.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/forkid/ForkIdTestUtil.java index d88d0fcd027..7fc23c2d475 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/forkid/ForkIdTestUtil.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/forkid/ForkIdTestUtil.java @@ -16,6 +16,7 @@ import static java.util.Collections.emptyList; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; import org.hyperledger.besu.datatypes.Hash; @@ -44,7 +45,7 @@ public static Blockchain mockBlockchain( final String genesisHash, final LongSupplier chainHeightSupplier, final long timestamp) { final Blockchain mockchain = mock(Blockchain.class); final BlockHeader mockHeader = mock(BlockHeader.class); - final Block block = new Block(mockHeader, null); + final Block block = spy(new Block(mockHeader, null)); final BlockHeader mockChainHeadHeader = mock(BlockHeader.class); when(mockchain.getGenesisBlock()).thenReturn(block); when(mockchain.getChainHeadBlockNumber()).thenReturn(chainHeightSupplier.getAsLong()); @@ -52,6 +53,9 @@ public static Blockchain mockBlockchain( when(mockchain.getChainHeadHeader()).thenReturn(mockChainHeadHeader); when(mockChainHeadHeader.getNumber()).thenReturn(chainHeightSupplier.getAsLong()); when(mockChainHeadHeader.getTimestamp()).thenReturn(timestamp); + final BlockHeader mockGenesisBlockHeader = mock(BlockHeader.class); + when(block.getHeader()).thenReturn(mockGenesisBlockHeader); + when(mockGenesisBlockHeader.getTimestamp()).thenReturn(1L); return mockchain; }