Skip to content

Commit

Permalink
Add shanghaiTime to sepolia (hyperledger#5088)
Browse files Browse the repository at this point in the history
Update and fix forkId tests

Move timestamp forks from getForkBlockNumbers to getForkBlockTimestamps in JsonGenesisConfigOptions - this ultimately gets used to popoulate the ForkIdManager which handles lists of blocks and timestamps the same way so this hasn't changed any actual behaviour, but rather supports the test fixes.

Implement TransitionProtocolSchedule.streamMilestoneBlocks as a concatenation of blockNumbers++blockTimestamps. This may have been a latent bug since it's used to update the node record when a fork transition occurs.

Signed-off-by: Simon Dudley <[email protected]>
  • Loading branch information
siladu authored and jframe committed Feb 17, 2023
1 parent e434382 commit cb91ce1
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@
import org.hyperledger.besu.cli.config.NetworkName;
import org.hyperledger.besu.config.GenesisConfigFile;
import org.hyperledger.besu.config.GenesisConfigOptions;
import org.hyperledger.besu.consensus.merge.TransitionProtocolSchedule;
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.ProtocolSchedule;
import org.hyperledger.besu.evm.internal.EvmConfiguration;

import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -73,8 +72,9 @@ public static Collection<Object[]> parameters() {
NetworkName.SEPOLIA,
List.of(
new ForkId(Bytes.ofUnsignedInt(0xfe3366e7L), 1735371L),
new ForkId(Bytes.ofUnsignedInt(0xb96cbd13L), 0L),
new ForkId(Bytes.ofUnsignedInt(0xb96cbd13L), 0L))
new ForkId(Bytes.ofUnsignedInt(0xb96cbd13L), 1677557088L),
new ForkId(Bytes.ofUnsignedInt(0xf7f9bc08L), 0L),
new ForkId(Bytes.ofUnsignedInt(0xf7f9bc08L), 0L))
},
new Object[] {
NetworkName.RINKEBY,
Expand Down Expand Up @@ -168,8 +168,7 @@ public void testForkId() {
final GenesisConfigFile genesisConfigFile =
GenesisConfigFile.fromConfig(EthNetworkConfig.jsonConfig(chainName));
final GenesisConfigOptions configOptions = genesisConfigFile.getConfigOptions();
final ProtocolSchedule schedule =
MainnetProtocolSchedule.fromConfig(configOptions, EvmConfiguration.DEFAULT);
final ProtocolSchedule schedule = TransitionProtocolSchedule.fromConfig(configOptions);
final GenesisState genesisState = GenesisState.fromConfig(genesisConfigFile, schedule);
final Blockchain mockBlockchain = mock(Blockchain.class);
final BlockHeader mockBlockHeader = mock(BlockHeader.class);
Expand All @@ -179,6 +178,7 @@ public void testForkId() {
final AtomicLong blockNumber = new AtomicLong();
when(mockBlockchain.getChainHeadHeader()).thenReturn(mockBlockHeader);
when(mockBlockHeader.getNumber()).thenAnswer(o -> blockNumber.get());
when(mockBlockHeader.getTimestamp()).thenAnswer(o -> blockNumber.get());

final ForkIdManager forkIdManager =
new ForkIdManager(
Expand All @@ -187,7 +187,7 @@ public void testForkId() {
genesisConfigFile.getForkTimestamps(),
false);

final var actualForkIds =
final List<ForkId> actualForkIds =
Streams.concat(schedule.streamMilestoneBlocks(), Stream.of(Long.MAX_VALUE))
.map(
block -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -596,10 +596,6 @@ public List<Long> getForkBlockNumbers() {
getArrowGlacierBlockNumber(),
getGrayGlacierBlockNumber(),
getMergeNetSplitBlockNumber(),
getShanghaiTime(),
getCancunTime(),
getFutureEipsTime(),
getExperimentalEipsTime(),
getEcip1015BlockNumber(),
getDieHardBlockNumber(),
getGothamBlockNumber(),
Expand All @@ -623,7 +619,9 @@ public List<Long> getForkBlockNumbers() {

@Override
public List<Long> getForkBlockTimestamps() {
Stream<OptionalLong> forkBlockTimestamps = Stream.of(getShanghaiTime(), getCancunTime());
Stream<OptionalLong> forkBlockTimestamps =
Stream.of(
getShanghaiTime(), getCancunTime(), getFutureEipsTime(), getExperimentalEipsTime());
// when adding forks add an entry to ${REPO_ROOT}/config/src/test/resources/all_forks.json

return forkBlockTimestamps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -622,22 +622,22 @@ public StubGenesisConfigOptions cancunTime(final long timestamp) {
/**
* Future EIPs Time block.
*
* @param blockNumber the block number
* @param timestamp the block timestamp
* @return the stub genesis config options
*/
public StubGenesisConfigOptions futureEipsTime(final long blockNumber) {
futureEipsTime = OptionalLong.of(blockNumber);
public StubGenesisConfigOptions futureEipsTime(final long timestamp) {
futureEipsTime = OptionalLong.of(timestamp);
return this;
}

/**
* Experimental EIPs Time block.
*
* @param blockNumber the block number
* @param timestamp the block timestamp
* @return the stub genesis config options
*/
public StubGenesisConfigOptions experimentalEipsTime(final long blockNumber) {
experimentalEipsTime = OptionalLong.of(blockNumber);
public StubGenesisConfigOptions experimentalEipsTime(final long timestamp) {
experimentalEipsTime = OptionalLong.of(timestamp);
return this;
}

Expand Down
1 change: 1 addition & 0 deletions config/src/main/resources/sepolia.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"londonBlock":0,
"mergeNetSplitBlock": 1735371,
"terminalTotalDifficulty": 17000000000000000,
"shanghaiTime": 1677557088,
"ethash":{},
"discovery": {
"dns": "enrtree://AKA3AM6LPBYEUDMVNU3BSVQJ5AD45Y7YPOHJLEF6W26QOE4VTUDPE@all.sepolia.ethdisco.net",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@

import static org.hyperledger.besu.util.Slf4jLambdaHelper.debugLambda;

import org.hyperledger.besu.config.GenesisConfigOptions;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.core.Difficulty;
import org.hyperledger.besu.ethereum.core.PrivacyParameters;
import org.hyperledger.besu.ethereum.core.ProcessableBlockHeader;
import org.hyperledger.besu.ethereum.core.TransactionFilter;
import org.hyperledger.besu.ethereum.mainnet.HeaderBasedProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.MainnetProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSpec;
import org.hyperledger.besu.ethereum.mainnet.TimestampSchedule;
Expand Down Expand Up @@ -60,6 +63,29 @@ public TransitionProtocolSchedule(
new TransitionUtils<>(preMergeProtocolSchedule, postMergeProtocolSchedule, mergeContext);
}

/**
* Create a Proof-of-Stake protocol schedule from a config object
*
* @param genesisConfigOptions {@link GenesisConfigOptions} containing the config options for the
* milestone starting points
* @return an initialised TransitionProtocolSchedule using post-merge defaults
*/
public static TransitionProtocolSchedule fromConfig(
final GenesisConfigOptions genesisConfigOptions) {
ProtocolSchedule preMergeProtocolSchedule =
MainnetProtocolSchedule.fromConfig(genesisConfigOptions);
ProtocolSchedule postMergeProtocolSchedule =
MergeProtocolSchedule.create(genesisConfigOptions, false);
TimestampSchedule timestampSchedule =
MergeProtocolSchedule.createTimestamp(
genesisConfigOptions, PrivacyParameters.DEFAULT, false);
return new TransitionProtocolSchedule(
preMergeProtocolSchedule,
postMergeProtocolSchedule,
PostMergeContext.get(),
timestampSchedule);
}

/**
* Gets pre merge schedule.
*
Expand Down Expand Up @@ -162,8 +188,10 @@ public ProtocolSpec getByBlockNumber(final long number) {
*/
@Override
public Stream<Long> streamMilestoneBlocks() {
return transitionUtils.dispatchFunctionAccordingToMergeState(
ProtocolSchedule::streamMilestoneBlocks);
Stream<Long> milestoneBlockNumbers =
transitionUtils.dispatchFunctionAccordingToMergeState(
ProtocolSchedule::streamMilestoneBlocks);
return Stream.concat(milestoneBlockNumbers, timestampSchedule.streamMilestoneBlocks());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Optional;
import java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class DefaultTimestampSchedule implements TimestampSchedule {
private final NavigableSet<TimedProtocolSpec> protocolSpecs =
Expand All @@ -46,6 +47,11 @@ public Optional<ProtocolSpec> getByTimestamp(final long timestamp) {
return Optional.empty();
}

@Override
public Stream<Long> streamMilestoneBlocks() {
return protocolSpecs.stream().map(TimedProtocolSpec::getTimestamp).sorted();
}

@Override
public Optional<BigInteger> getChainId() {
return chainId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.math.BigInteger;
import java.util.Optional;
import java.util.stream.Stream;

public interface HeaderBasedProtocolSchedule {

Expand All @@ -31,4 +32,6 @@ public interface HeaderBasedProtocolSchedule {
void putMilestone(final long blockOrTimestamp, final ProtocolSpec protocolSpec);

String listMilestones();

Stream<Long> streamMilestoneBlocks();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@

import org.hyperledger.besu.ethereum.core.ProcessableBlockHeader;

import java.util.stream.Stream;

public interface ProtocolSchedule
extends HeaderBasedProtocolSchedule, PrivacySupportingProtocolSchedule {

ProtocolSpec getByBlockNumber(long number);

Stream<Long> streamMilestoneBlocks();

@Override
default ProtocolSpec getByBlockHeader(final ProcessableBlockHeader blockHeader) {
return getByBlockNumber(blockHeader.getNumber());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,20 @@ public static Collection<Object[]> data() {
empty()
},
{
"Sepolia // Future",
"Sepolia // Shanghai",
Network.SEPOLIA,
1735371L,
0L,
ForkIdTestUtil.wantForkId("0xb96cbd13", 0L),
ForkIdTestUtil.wantForkId("0xb96cbd13", 1677557088L),
Optional.of(ForkIds.SEPOLIA),
empty()
},
{
"Sepolia // Future",
Network.SEPOLIA,
1735372L,
1677557088L,
ForkIdTestUtil.wantForkId("0xf7f9bc08", 0L),
Optional.of(ForkIds.SEPOLIA),
empty()
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,11 @@ public static class Forks {
Arrays.asList(
1920000L, 1150000L, 2463000L, 2675000L, 2675000L, 4370000L, 7280000L, 7280000L,
9069000L, 9200000L, 12244000L, 12965000L, 13773000L, 15050000L);
public static final List<Long> SEPOLIA =
public static final List<Long> SEPOLIA_BLOCKNUMBERS =
Arrays.asList(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1735371L);

public static final List<Long> SEPOLIA_TIMESTAMPS = Arrays.asList(1677557088L);

public static final List<Long> RINKEBY =
Arrays.asList(1L, 2L, 3L, 3L, 1035301L, 3660663L, 4321234L, 5435345L);
public static final List<Long> GOERLI = Arrays.asList(0L, 0L, 0L, 0L, 0L, 0L, 0L, 1561651L);
Expand Down Expand Up @@ -106,7 +109,8 @@ public static class ForkIds {
public static final List<ForkId> SEPOLIA =
Arrays.asList(
new ForkId(Bytes.fromHexString("0xfe3366e7"), 1735371L),
new ForkId(Bytes.fromHexString("0xb96cbd13"), 0L));
new ForkId(Bytes.fromHexString("0xb96cbd13"), 1677557088L),
new ForkId(Bytes.fromHexString("0xf7f9bc08"), 0L)); // First Shanghai block (timestamp)
public static final List<ForkId> RINKEBY =
Arrays.asList(
new ForkId(Bytes.fromHexString("0x3b8e0691"), 1L),
Expand Down Expand Up @@ -145,7 +149,8 @@ public static class ForkIds {

public static class Network {
public static final Network MAINNET = network(GenesisHash.MAINNET, Forks.MAINNET, emptyList());
public static final Network SEPOLIA = network(GenesisHash.SEPOLIA, Forks.SEPOLIA, emptyList());
public static final Network SEPOLIA =
network(GenesisHash.SEPOLIA, Forks.SEPOLIA_BLOCKNUMBERS, Forks.SEPOLIA_TIMESTAMPS);
public static final Network RINKEBY = network(GenesisHash.RINKEBY, Forks.RINKEBY, emptyList());
public static final Network GOERLI = network(GenesisHash.GOERLI, Forks.GOERLI, emptyList());
public static final Network PRIVATE = network(GenesisHash.PRIVATE, Forks.PRIVATE, emptyList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,15 @@ public void getByBlockHeader_whenSpecNotFoundReturnsNull() {

assertThat(schedule.getByBlockHeader(BLOCK_HEADER)).isNull();
}

@Test
public void streamMilestoneBlocksReturnTimestampsInOrder() {
config.shanghaiTime(FIRST_TIMESTAMP_FORK);
config.cancunTime(2L);
config.experimentalEipsTime(5L);
config.futureEipsTime(3L);
final TimestampSchedule schedule = builder.createTimestampSchedule();

assertThat(schedule.streamMilestoneBlocks()).containsExactly(FIRST_TIMESTAMP_FORK, 2L, 3L, 5L);
}
}

0 comments on commit cb91ce1

Please sign in to comment.