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

Forkid timestamp #4815

Closed
wants to merge 41 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
af166e6
Composition over inheritence
gezero Nov 16, 2022
72b418f
wip
gezero Nov 17, 2022
2f8f7b6
spotless
gezero Nov 29, 2022
7cfdd52
removing unnecessary code
gezero Nov 30, 2022
3cbfc1e
fixing npe
gezero Nov 30, 2022
2687f7e
using of shanghai schedule for Reference tests
gezero Nov 30, 2022
2d5db69
Using the shanghai Timestamp
gezero Nov 30, 2022
31e9329
spotless
siladu Dec 1, 2022
71417fc
Minor renaming
siladu Dec 1, 2022
eb7dbc6
More minor renaming
siladu Dec 1, 2022
870c84c
Add unit test for TimestampScheduleBuilder
siladu Dec 1, 2022
f9303bf
validate block using correct protocol spec
jframe Dec 1, 2022
d80952b
create block using correct protocol spec
jframe Dec 1, 2022
dc15325
validate fork timestamp order and more tests for TimestampScheduleBui…
siladu Dec 5, 2022
582a8ff
Add TransitionProtocolSchedule tests and fix make unnecessary eager e…
siladu Dec 6, 2022
cfc8ecf
Add test for ProtocolSchedule.getByBlockHeader
siladu Dec 6, 2022
5cb9bc1
Added tests for TimestampSchedule and minimised visibility
siladu Dec 6, 2022
44f4631
Tentatively add TimestampProtocolSpecAdapters so it can be discussed
siladu Dec 7, 2022
7aeaecb
Add pom checksum
siladu Dec 7, 2022
5ad6634
Remove TODOs
siladu Dec 7, 2022
0f251fd
Fixed tests - only attempt to apply modifications if we have anything…
siladu Dec 8, 2022
829e83c
When block not found, throw instead of defaulting to the potentially …
siladu Dec 8, 2022
3fb6777
Add comment
siladu Dec 8, 2022
2480436
Missed some test modifications
siladu Dec 8, 2022
5954d89
Add timestamp to toLogString for more info, not expecting it will be …
siladu Dec 8, 2022
56edfa7
Revert "When block not found, throw instead of defaulting to the pote…
siladu Dec 8, 2022
727325b
private and final
siladu Dec 12, 2022
698849e
Refactor TransitionUtils.getMergeContext, pull up PostMergeContext wi…
siladu Dec 12, 2022
f4483ca
Add PrivacySupportingProtocolSchedule interface and implement in Defa…
siladu Dec 12, 2022
bf6eb04
final
siladu Dec 13, 2022
e1522b1
Remove TimestampProtocolSpecAdapters
siladu Dec 13, 2022
06813e0
Refactor TimestampScheduleBuilder and ProtocolScheduleBuilder to use …
siladu Dec 13, 2022
8b5e3ed
initial implemenation of fork id logic
gezero Dec 13, 2022
0cb8a74
fix ForkIdsTest
jframe Dec 14, 2022
f665e6f
fix unit tests
jframe Dec 14, 2022
f19abdd
Fix more units related to ForkId fixes
siladu Dec 15, 2022
285aec2
Add some withdrawals fork ID test cases
jframe Dec 15, 2022
5bf2626
Rename tests
jframe Dec 16, 2022
c5add6b
Move forkId tests to forkId package
jframe Dec 19, 2022
d86f693
comment
jframe Dec 19, 2022
4ed37bb
Refactor ForkIdManager to use a single list of forkIDs where possible
jframe Dec 20, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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().getForkTimestamps())
.build();

final NetworkRunner networkRunner =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,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 @@ -43,6 +43,7 @@
import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ScheduleBasedBlockHeaderFunctions;
import org.hyperledger.besu.ethereum.mainnet.TimestampSchedule;
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive;
import org.hyperledger.besu.plugin.services.MetricsSystem;

Expand Down Expand Up @@ -233,4 +234,9 @@ protected List<PeerValidator> createPeerValidators(final ProtocolSchedule protoc
}
return retval;
}

public TimestampSchedule createTimestampProtocolSchedule() {
return MergeProtocolSchedule.createTimestamp(
configOptionsSupplier.get(), privacyParameters, isRevertReasonEnabled);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.ConsensusContext;
import org.hyperledger.besu.ethereum.ConsensusContextFactory;
import org.hyperledger.besu.ethereum.GasLimitCalculator;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.chain.MutableBlockchain;
import org.hyperledger.besu.ethereum.core.MiningParameters;
import org.hyperledger.besu.ethereum.core.PrivacyParameters;
import org.hyperledger.besu.ethereum.core.Synchronizer;
Expand Down Expand Up @@ -75,6 +77,7 @@ public class TransitionBesuControllerBuilder extends BesuControllerBuilder {
private final MergeBesuControllerBuilder mergeBesuControllerBuilder;

private static final Logger LOG = LoggerFactory.getLogger(TransitionBesuControllerBuilder.class);
private TransitionProtocolSchedule transitionProtocolSchedule;

public TransitionBesuControllerBuilder(
final BesuControllerBuilder preMergeBesuControllerBuilder,
Expand Down Expand Up @@ -165,9 +168,26 @@ protected EthProtocolManager createEthProtocolManager(

@Override
protected ProtocolSchedule createProtocolSchedule() {
return new TransitionProtocolSchedule(
preMergeBesuControllerBuilder.createProtocolSchedule(),
mergeBesuControllerBuilder.createProtocolSchedule());
transitionProtocolSchedule =
new TransitionProtocolSchedule(
preMergeBesuControllerBuilder.createProtocolSchedule(),
mergeBesuControllerBuilder.createProtocolSchedule(),
PostMergeContext.get(),
mergeBesuControllerBuilder.createTimestampProtocolSchedule());
return transitionProtocolSchedule;
}

@Override
protected ProtocolContext createProtocolContext(
final MutableBlockchain blockchain,
final WorldStateArchive worldStateArchive,
final ProtocolSchedule protocolSchedule,
final ConsensusContextFactory consensusContextFactory) {
final ProtocolContext protocolContext =
super.createProtocolContext(
blockchain, worldStateArchive, protocolSchedule, consensusContextFactory);
transitionProtocolSchedule.setProtocolContext(protocolContext);
return protocolContext;
}

@Override
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.getForkTimestamps()).thenReturn(Collections.emptyList());
when(besuController.getGenesisConfigOptions()).thenReturn(genesisConfigOptions);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.hyperledger.besu.ethereum.mainnet.MainnetBlockHeaderValidator;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSpec;
import org.hyperledger.besu.ethereum.mainnet.TimestampSchedule;
import org.hyperledger.besu.ethereum.mainnet.feemarket.FeeMarket;
import org.hyperledger.besu.ethereum.storage.StorageProvider;
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem;
Expand All @@ -66,6 +67,7 @@ public class TransitionControllerBuilderTest {

@Mock ProtocolSchedule preMergeProtocolSchedule;
@Mock ProtocolSchedule postMergeProtocolSchedule;
@Mock TimestampSchedule timestampSchedule;
@Mock ProtocolContext protocolContext;
@Mock MutableBlockchain mockBlockchain;
@Mock TransactionPool transactionPool;
Expand All @@ -86,7 +88,11 @@ public void setup() {
transitionProtocolSchedule =
spy(
new TransitionProtocolSchedule(
preMergeProtocolSchedule, postMergeProtocolSchedule, mergeContext));
preMergeProtocolSchedule,
postMergeProtocolSchedule,
mergeContext,
timestampSchedule));
transitionProtocolSchedule.setProtocolContext(protocolContext);
cliqueBuilder.nodeKey(NodeKeyUtils.generate());
postMergeBuilder.storageProvider(storageProvider);
when(protocolContext.getBlockchain()).thenReturn(mockBlockchain);
Expand Down Expand Up @@ -126,7 +132,7 @@ public void assertPreMergeScheduleForNotPostMerge() {
when(mergeContext.isPostMerge()).thenReturn(Boolean.FALSE);
when(mergeContext.getFinalized()).thenReturn(Optional.empty());
when(preMergeProtocolSchedule.getByBlockNumber(anyLong())).thenReturn(preMergeProtocolSpec);
assertThat(transitionProtocolSchedule.getByBlockHeader(protocolContext, mockBlock))
assertThat(transitionProtocolSchedule.getByBlockHeader(mockBlock))
.isEqualTo(preMergeProtocolSpec);
}

Expand All @@ -136,7 +142,7 @@ public void assertPostMergeScheduleForAnyBlockWhenPostMergeAndFinalized() {
var postMergeProtocolSpec = mock(ProtocolSpec.class);
when(mergeContext.getFinalized()).thenReturn(Optional.of(mockBlock));
when(postMergeProtocolSchedule.getByBlockNumber(anyLong())).thenReturn(postMergeProtocolSpec);
assertThat(transitionProtocolSchedule.getByBlockHeader(protocolContext, mockBlock))
assertThat(transitionProtocolSchedule.getByBlockHeader(mockBlock))
.isEqualTo(postMergeProtocolSpec);
}

Expand All @@ -159,7 +165,7 @@ public void assertPreMergeScheduleForBelowTerminalBlockWhenPostMergeIfNotFinaliz
.thenReturn(Optional.of(Difficulty.of(1335L)));

when(preMergeProtocolSchedule.getByBlockNumber(anyLong())).thenReturn(preMergeProtocolSpec);
assertThat(transitionProtocolSchedule.getByBlockHeader(protocolContext, mockBlock))
assertThat(transitionProtocolSchedule.getByBlockHeader(mockBlock))
.isEqualTo(preMergeProtocolSpec);
}

Expand All @@ -182,7 +188,7 @@ public void assertPostMergeScheduleForPostMergeExactlyAtTerminalDifficultyIfNotF
.thenReturn(Optional.of(Difficulty.of(1337L)));

when(postMergeProtocolSchedule.getByBlockNumber(anyLong())).thenReturn(postMergeProtocolSpec);
assertThat(transitionProtocolSchedule.getByBlockHeader(protocolContext, mockBlock))
assertThat(transitionProtocolSchedule.getByBlockHeader(mockBlock))
.isEqualTo(postMergeProtocolSpec);
}

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().getForkTimestamps();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ default boolean isConsensusMigration() {

OptionalLong getMergeNetSplitBlockNumber();

OptionalLong getShanghaiTimestamp();

OptionalLong getCancunTimestamp();

OptionalLong getShandongBlockNumber();

Optional<Wei> getBaseFeePerGas();
Expand All @@ -100,7 +104,9 @@ default boolean isConsensusMigration() {

Optional<Hash> getTerminalBlockHash();

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

List<Long> getForkTimestamps();

/**
* 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 @@ -278,6 +278,16 @@ public OptionalLong getMergeNetSplitBlockNumber() {
return getOptionalLong("mergenetsplitblock");
}

@Override
public OptionalLong getShanghaiTimestamp() {
return getOptionalLong("shanghaitimestamp");
}

@Override
public OptionalLong getCancunTimestamp() {
return getOptionalLong("cancuntimestamp");
}

@Override
public OptionalLong getShandongBlockNumber() {
return getOptionalLong("shandongblock");
Expand Down Expand Up @@ -433,6 +443,8 @@ public Map<String, Object> asMap() {
getArrowGlacierBlockNumber().ifPresent(l -> builder.put("arrowGlacierBlock", l));
getGrayGlacierBlockNumber().ifPresent(l -> builder.put("grayGlacierBlock", l));
getMergeNetSplitBlockNumber().ifPresent(l -> builder.put("mergeNetSplitBlock", l));
getShanghaiTimestamp().ifPresent(l -> builder.put("shanghaiTimestamp", l));
getCancunTimestamp().ifPresent(l -> builder.put("cancunTimestamp", l));
getShandongBlockNumber().ifPresent(l -> builder.put("shandongBlock", l));
getTerminalBlockNumber().ifPresent(l -> builder.put("terminalBlockNumber", l));
getTerminalBlockHash().ifPresent(h -> builder.put("terminalBlockHash", h.toHexString()));
Expand Down Expand Up @@ -540,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 @@ -578,4 +590,18 @@ public List<Long> getForks() {
.sorted()
.collect(Collectors.toList());
}

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

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 @@ -44,6 +44,8 @@ public class StubGenesisConfigOptions implements GenesisConfigOptions {
private OptionalLong arrowGlacierBlockNumber = OptionalLong.empty();
private OptionalLong grayGlacierBlockNumber = OptionalLong.empty();
private OptionalLong mergeNetSplitBlockNumber = OptionalLong.empty();
private OptionalLong shanghaiTimestamp = OptionalLong.empty();
private OptionalLong cancunTimestamp = OptionalLong.empty();
private OptionalLong shandongBlockNumber = OptionalLong.empty();
private OptionalLong terminalBlockNumber = OptionalLong.empty();
private Optional<Hash> terminalBlockHash = Optional.empty();
Expand Down Expand Up @@ -217,6 +219,16 @@ public OptionalLong getMergeNetSplitBlockNumber() {
return mergeNetSplitBlockNumber;
}

@Override
public OptionalLong getShanghaiTimestamp() {
return shanghaiTimestamp;
}

@Override
public OptionalLong getCancunTimestamp() {
return cancunTimestamp;
}

@Override
public OptionalLong getShandongBlockNumber() {
return shandongBlockNumber;
Expand Down Expand Up @@ -342,6 +354,8 @@ public Map<String, Object> asMap() {
getArrowGlacierBlockNumber().ifPresent(l -> builder.put("arrowGlacierBlock", l));
getGrayGlacierBlockNumber().ifPresent(l -> builder.put("grayGlacierBlock", l));
getMergeNetSplitBlockNumber().ifPresent(l -> builder.put("mergeNetSplitBlock", l));
getShanghaiTimestamp().ifPresent(l -> builder.put("shanghaiTimestamp", l));
getCancunTimestamp().ifPresent(l -> builder.put("cancunTimestamp", l));
getShandongBlockNumber().ifPresent(l -> builder.put("shandongBlock", l));
getTerminalBlockNumber().ifPresent(l -> builder.put("terminalBlockNumber", l));
getTerminalBlockHash().ifPresent(h -> builder.put("terminalBlockHash", h));
Expand Down Expand Up @@ -412,7 +426,12 @@ public boolean isZeroBaseFee() {
}

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

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

Expand Down Expand Up @@ -486,6 +505,16 @@ public StubGenesisConfigOptions mergeNetSplitBlock(final long blockNumber) {
return this;
}

public StubGenesisConfigOptions shanghaiTimestamp(final long timestamp) {
shanghaiTimestamp = OptionalLong.of(timestamp);
return this;
}

public StubGenesisConfigOptions cancunTimestamp(final long timestamp) {
cancunTimestamp = OptionalLong.of(timestamp);
return this;
}

public StubGenesisConfigOptions shandongBlock(final long blockNumber) {
shandongBlockNumber = OptionalLong.of(blockNumber);
return this;
Expand Down