Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EIP-6122 Timestamp based fork IDs #4841

Merged
merged 18 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,8 @@ public Runner build() {
.storageProvider(storageProvider)
.p2pTLSConfiguration(p2pTLSConfiguration)
.blockchain(context.getBlockchain())
.forks(besuController.getGenesisConfigOptions().getForks())
.blockNumberForks(besuController.getGenesisConfigOptions().getForkBlockNumbers())
.timestampForks(besuController.getGenesisConfigOptions().getForkBlockTimestamps())
.build();

final NetworkRunner networkRunner =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,8 @@ protected EthProtocolManager createEthProtocolManager(
mergePeerFilter,
synchronizerConfiguration,
scheduler,
genesisConfig.getForks());
genesisConfig.getForkBlockNumbers(),
genesisConfig.getForkTimestamps());
}

protected ProtocolContext createProtocolContext(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
import org.hyperledger.besu.config.GenesisConfigOptions;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.chain.GenesisState;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.forkid.ForkId;
import org.hyperledger.besu.ethereum.forkid.ForkIdManager;
import org.hyperledger.besu.ethereum.mainnet.MainnetProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.MutableProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.evm.internal.EvmConfiguration;

Expand All @@ -47,7 +47,7 @@
import org.junit.runners.Parameterized;

@RunWith(Enclosed.class)
public class ForkIdsTest {
public class ForkIdsNetworkConfigTest {

public static class NotParameterized {
@Test
Expand Down Expand Up @@ -192,19 +192,23 @@ public void testForkId() {
MainnetProtocolSchedule.fromConfig(configOptions, EvmConfiguration.DEFAULT);
final GenesisState genesisState = GenesisState.fromConfig(genesisConfigFile, schedule);
final Blockchain mockBlockchain = mock(Blockchain.class);
final BlockHeader mockBlockHeader = mock(BlockHeader.class);

when(mockBlockchain.getGenesisBlock()).thenReturn(genesisState.getBlock());

final AtomicLong blockNumber = new AtomicLong();
when(mockBlockchain.getChainHeadBlockNumber()).thenAnswer(o -> blockNumber.get());
when(mockBlockchain.getChainHeadHeader()).thenReturn(mockBlockHeader);
when(mockBlockHeader.getNumber()).thenAnswer(o -> blockNumber.get());

final ForkIdManager forkIdManager =
new ForkIdManager(mockBlockchain, genesisConfigFile.getForks(), false);
new ForkIdManager(
mockBlockchain,
genesisConfigFile.getForkBlockNumbers(),
genesisConfigFile.getForkTimestamps(),
false);

final var actualForkIds =
Streams.concat(
((MutableProtocolSchedule) schedule).streamMilestoneBlocks(),
Stream.of(Long.MAX_VALUE))
Streams.concat(schedule.streamMilestoneBlocks(), Stream.of(Long.MAX_VALUE))
.map(
block -> {
blockNumber.set(block);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ public void setup() {
when(besuController.getMiningCoordinator()).thenReturn(mock(MiningCoordinator.class));
when(besuController.getMiningCoordinator()).thenReturn(mock(MergeMiningCoordinator.class));
final GenesisConfigOptions genesisConfigOptions = mock(GenesisConfigOptions.class);
when(genesisConfigOptions.getForks()).thenReturn(Collections.emptyList());
when(genesisConfigOptions.getForkBlockNumbers()).thenReturn(Collections.emptyList());
when(genesisConfigOptions.getForkBlockTimestamps()).thenReturn(Collections.emptyList());
when(besuController.getGenesisConfigOptions()).thenReturn(genesisConfigOptions);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,11 @@ private long parseLong(final String name, final String value) {
}
}

public List<Long> getForks() {
return getConfigOptions().getForks();
public List<Long> getForkBlockNumbers() {
return getConfigOptions().getForkBlockNumbers();
}

public List<Long> getForkTimestamps() {
return getConfigOptions().getForkBlockTimestamps();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ default boolean isConsensusMigration() {

Optional<Hash> getTerminalBlockHash();

List<Long> getForks();
List<Long> getForkBlockNumbers();

List<Long> getForkBlockTimestamps();

/**
* Block number for the Dao Fork, this value is used to tell node to connect with peer that did
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ private Optional<Hash> getOptionalHash(final String key) {
}

@Override
public List<Long> getForks() {
public List<Long> getForkBlockNumbers() {
Stream<OptionalLong> forkBlockNumbers =
Stream.of(
getHomesteadBlockNumber(),
Expand Down Expand Up @@ -590,4 +590,17 @@ public List<Long> getForks() {
.sorted()
.collect(Collectors.toList());
}

@Override
public List<Long> getForkBlockTimestamps() {
Stream<OptionalLong> forkBlockTimestamps = Stream.of(getShanghaiTime(), getCancunTime());
// when adding forks add an entry to ${REPO_ROOT}/config/src/test/resources/all_forks.json
jframe marked this conversation as resolved.
Show resolved Hide resolved

return forkBlockTimestamps
.filter(OptionalLong::isPresent)
.map(OptionalLong::getAsLong)
.distinct()
.sorted()
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,12 @@ public boolean isZeroBaseFee() {
}

@Override
public List<Long> getForks() {
public List<Long> getForkBlockNumbers() {
return Collections.emptyList();
}

@Override
public List<Long> getForkBlockTimestamps() {
return Collections.emptyList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,15 @@ public void shouldFindMergeNetSplitForkAndAlias() {
GenesisConfigFile mergeNetSplitGenesis =
GenesisConfigFile.fromConfig(
"{\"config\":{\"mergeNetsplitBlock\":11},\"baseFeePerGas\":\"0xa\"}");
assertThat(mergeNetSplitGenesis.getForks()).hasSize(1);
assertThat(mergeNetSplitGenesis.getForkBlockNumbers()).hasSize(1);
assertThat(mergeNetSplitGenesis.getConfigOptions().getMergeNetSplitBlockNumber()).isPresent();
assertThat(mergeNetSplitGenesis.getConfigOptions().getMergeNetSplitBlockNumber().getAsLong())
.isEqualTo(11L);

// assert empty if not present:
GenesisConfigFile londonGenesis =
GenesisConfigFile.fromConfig("{\"config\":{\"londonBlock\":11},\"baseFeePerGas\":\"0xa\"}");
assertThat(londonGenesis.getForks()).hasSize(1);
assertThat(londonGenesis.getForkBlockNumbers()).hasSize(1);
assertThat(londonGenesis.getConfigOptions().getMergeNetSplitBlockNumber()).isEmpty();
}

Expand Down Expand Up @@ -355,7 +355,7 @@ public void testOverridePresent() {
override.put("chainId", bigBlockString);
override.put("contractSizeLimit", bigBlockString);

assertThat(config.getForks()).isNotEmpty();
assertThat(config.getForkBlockNumbers()).isNotEmpty();
assertThat(config.getConfigOptions(override).getIstanbulBlockNumber()).hasValue(bigBlock);
assertThat(config.getConfigOptions(override).getChainId())
.hasValue(BigInteger.valueOf(bigBlock));
Expand All @@ -370,7 +370,7 @@ public void testOverrideNull() {
override.put("chainId", null);
override.put("contractSizeLimit", null);

assertThat(config.getForks()).isNotEmpty();
assertThat(config.getForkBlockNumbers()).isNotEmpty();
assertThat(config.getConfigOptions(override).getIstanbulBlockNumber()).isNotPresent();
assertThat(config.getConfigOptions(override).getChainId()).isNotPresent();
assertThat(config.getConfigOptions(override).getContractSizeLimit()).isNotPresent();
Expand Down Expand Up @@ -454,7 +454,7 @@ public void shouldLoadForksInSortedOrder() throws IOException {

final GenesisConfigFile config = fromConfig(configNode);

assertThat(config.getForks()).containsExactly(1L, 2L, 3L, 1035301L, 2222222L);
assertThat(config.getForkBlockNumbers()).containsExactly(1L, 2L, 3L, 1035301L, 2222222L);
assertThat(config.getConfigOptions().getChainId()).hasValue(BigInteger.valueOf(4));
}

Expand All @@ -474,7 +474,7 @@ public void shouldLoadForksIgnoreClassicForkBlock() throws IOException {
StandardCharsets.UTF_8)));
final GenesisConfigFile config = fromConfig(configNode);

assertThat(config.getForks()).containsExactly(1L, 2L, 3L, 1035301L);
assertThat(config.getForkBlockNumbers()).containsExactly(1L, 2L, 3L, 1035301L);
assertThat(config.getConfigOptions().getChainId()).hasValue(BigInteger.valueOf(61));
}

Expand Down Expand Up @@ -522,10 +522,12 @@ public void shouldLoadForksIgnoreUnexpectedValues() throws IOException {
final GenesisConfigFile configFileMultipleUnexpectedForks =
fromConfig(configMultipleUnexpectedForks);

assertThat(configFileNoUnexpectedForks.getForks()).containsExactly(1L, 2L, 3L, 1035301L);
assertThat(configFileNoUnexpectedForks.getForks()).isEqualTo(configFileClassicFork.getForks());
assertThat(configFileNoUnexpectedForks.getForks())
.isEqualTo(configFileMultipleUnexpectedForks.getForks());
assertThat(configFileNoUnexpectedForks.getForkBlockNumbers())
.containsExactly(1L, 2L, 3L, 1035301L);
assertThat(configFileNoUnexpectedForks.getForkBlockNumbers())
.isEqualTo(configFileClassicFork.getForkBlockNumbers());
assertThat(configFileNoUnexpectedForks.getForkBlockNumbers())
.isEqualTo(configFileMultipleUnexpectedForks.getForkBlockNumbers());
assertThat(configFileNoUnexpectedForks.getConfigOptions().getChainId())
.hasValue(BigInteger.valueOf(61));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,8 @@ private P2PNetwork createP2pNetwork() {
.metricsSystem(new NoOpMetricsSystem())
.storageProvider(new InMemoryKeyValueStorageProvider())
.blockchain(blockchain)
.forks(Collections.emptyList())
.blockNumberForks(Collections.emptyList())
.timestampForks(Collections.emptyList())
.build();

p2pNetwork.start();
Expand Down
Loading