Skip to content

Commit

Permalink
Support post merge forks at genesis for hive tests (hyperledger#5019)
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Frame <[email protected]>
  • Loading branch information
jframe authored and elenduuche committed Aug 16, 2023
1 parent 318387a commit add5064
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package org.hyperledger.besu.controller;

import org.hyperledger.besu.config.GenesisConfigOptions;
import org.hyperledger.besu.consensus.merge.MergeContext;
import org.hyperledger.besu.consensus.merge.MergeProtocolSchedule;
import org.hyperledger.besu.consensus.merge.PostMergeContext;
Expand Down Expand Up @@ -186,19 +187,24 @@ protected MergeContext createConsensusContext(
final WorldStateArchive worldStateArchive,
final ProtocolSchedule protocolSchedule) {

OptionalLong terminalBlockNumber = configOptionsSupplier.get().getTerminalBlockNumber();
Optional<Hash> terminalBlockHash = configOptionsSupplier.get().getTerminalBlockHash();
final GenesisConfigOptions genesisConfigOptions = configOptionsSupplier.get();
final OptionalLong terminalBlockNumber = genesisConfigOptions.getTerminalBlockNumber();
final Optional<Hash> terminalBlockHash = genesisConfigOptions.getTerminalBlockHash();
final boolean isPostMergeAtGenesis =
genesisConfigOptions.getTerminalTotalDifficulty().isPresent()
&& genesisConfigOptions.getTerminalTotalDifficulty().get().isZero()
&& blockchain.getGenesisBlockHeader().getDifficulty().isZero();

final MergeContext mergeContext =
PostMergeContext.get()
.setSyncState(syncState.get())
.setTerminalTotalDifficulty(
configOptionsSupplier
.get()
genesisConfigOptions
.getTerminalTotalDifficulty()
.map(Difficulty::of)
.orElse(Difficulty.ZERO))
.setCheckpointPostMergeSync(syncConfig.isCheckpointPostMergeEnabled());
.setCheckpointPostMergeSync(syncConfig.isCheckpointPostMergeEnabled())
.setPostMergeAtGenesis(isPostMergeAtGenesis);

blockchain
.getFinalized()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,11 @@ default boolean isChainPruningEnabled() {
* @return the boolean
*/
boolean isCheckpointPostMergeSync();

/**
* Is configured for a post-merge from genesis.
*
* @return the boolean
*/
boolean isPostMergeAtGenesis();
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public class PostMergeContext implements MergeContext {
private final AtomicReference<Optional<BlockHeader>> terminalPoWBlock =
new AtomicReference<>(Optional.empty());
private boolean isCheckpointPostMergeSync;
private boolean isPostMergeAtGenesis;

// TODO: cleanup - isChainPruningEnabled will not be required after
// https://github.com/hyperledger/besu/pull/4703 is merged.
Expand Down Expand Up @@ -329,4 +330,20 @@ public PostMergeContext setCheckpointPostMergeSync(final boolean isCheckpointPos
public boolean isCheckpointPostMergeSync() {
return this.isCheckpointPostMergeSync;
}

@Override
public boolean isPostMergeAtGenesis() {
return this.isPostMergeAtGenesis;
}

/**
* Sets whether it is post merge at genesis
*
* @param isPostMergeAtGenesis the is post merge at genesis state
* @return the post merge context
*/
public PostMergeContext setPostMergeAtGenesis(final boolean isPostMergeAtGenesis) {
this.isPostMergeAtGenesis = isPostMergeAtGenesis;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,9 @@ public Optional<BlockWithReceipts> retrieveBlockById(final PayloadIdentifier pay
public boolean isCheckpointPostMergeSync() {
return false;
}

@Override
public boolean isPostMergeAtGenesis() {
return postMergeContext.isPostMergeAtGenesis();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,21 @@ public static boolean isTerminalProofOfWorkBlock(
// if we cannot find difficulty or are merge-at-genesis
.orElse(Difficulty.ZERO);

final MergeContext consensusContext = context.getConsensusContext(MergeContext.class);

// Genesis is configured for post-merge we will never have a terminal pow block
if (consensusContext.isPostMergeAtGenesis()) {
return false;
}

if (currentChainTotalDifficulty.isZero()) {
warnLambda(
LOG,
"unable to get total difficulty for {}, parent hash {} difficulty not found",
header::toLogString,
header::getParentHash);
}
Difficulty configuredTotalTerminalDifficulty =
context.getConsensusContext(MergeContext.class).getTerminalTotalDifficulty();
Difficulty configuredTotalTerminalDifficulty = consensusContext.getTerminalTotalDifficulty();

if (currentChainTotalDifficulty
.add(headerDifficulty)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)
// TODO: post-merge cleanup, this should be unnecessary after merge
if (requireTerminalPoWBlockValidation()
&& !mergeContext.get().isCheckpointPostMergeSync()
&& !mergeContext.get().isPostMergeAtGenesis()
&& !mergeCoordinator.latestValidAncestorDescendsFromTerminal(newHead)
&& !mergeContext.get().isChainPruningEnabled()) {
logForkchoiceUpdatedCall(INVALID, forkChoice);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)
// TODO: post-merge cleanup
if (requireTerminalPoWBlockValidation()
&& !mergeContext.get().isCheckpointPostMergeSync()
&& !mergeContext.get().isPostMergeAtGenesis()
&& !mergeCoordinator.latestValidAncestorDescendsFromTerminal(newBlockHeader)
&& !mergeContext.get().isChainPruningEnabled()) {
mergeCoordinator.addBadBlock(block, Optional.empty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ default Block getGenesisBlock() {
.orElseThrow(() -> new IllegalStateException("Missing genesis block."));
}

default BlockHeader getGenesisBlockHeader() {
return getBlockHeader(BlockHeader.GENESIS_BLOCK_NUMBER)
.orElseThrow(() -> new IllegalStateException("Missing genesis block header."));
}

default Optional<Block> getBlockByHash(final Hash blockHash) {
return getBlockHeader(blockHash)
.flatMap(header -> getBlockBody(blockHash).map(body -> new Block(header, body)));
Expand Down

0 comments on commit add5064

Please sign in to comment.