Skip to content

Commit

Permalink
fix hyperledger#5744: Incorrect ForkId when first fork timestamp matc…
Browse files Browse the repository at this point in the history
…hes genesis timestamp (hyperledger#5787)

Signed-off-by: Stefan <stefan.pingel@consensys.net>
Co-authored-by: Gabriel-Trintinalia <gabriel.trintinalia@consensys.net>
  • Loading branch information
pinges and Gabriel-Trintinalia authored Aug 24, 2023
1 parent 9a0f35c commit 00d1995
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,14 +45,17 @@ 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());
when(mockHeader.getHash()).thenReturn(Hash.fromHexString(genesisHash));
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;
}

Expand Down

0 comments on commit 00d1995

Please sign in to comment.