use of org.hyperledger.besu.consensus.merge.MergeContext in project besu by hyperledger.
the class TransitionBesuControllerBuilder method createEthProtocolManager.
@Override
protected EthProtocolManager createEthProtocolManager(final ProtocolContext protocolContext, final boolean fastSyncEnabled, final TransactionPool transactionPool, final EthProtocolConfiguration ethereumWireProtocolConfiguration, final EthPeers ethPeers, final EthContext ethContext, final EthMessages ethMessages, final EthScheduler scheduler, final List<PeerValidator> peerValidators) {
EthProtocolManager ethProtocolManager = super.createEthProtocolManager(protocolContext, fastSyncEnabled, transactionPool, ethereumWireProtocolConfiguration, ethPeers, ethContext, ethMessages, scheduler, peerValidators);
ConsensusContext cc = protocolContext.getConsensusContext(ConsensusContext.class);
if (cc instanceof MergeContext) {
protocolContext.getConsensusContext(MergeContext.class).observeNewIsPostMergeState(ethProtocolManager);
protocolContext.getConsensusContext(MergeContext.class).addNewForkchoiceMessageListener(ethProtocolManager);
}
return ethProtocolManager;
}
use of org.hyperledger.besu.consensus.merge.MergeContext in project besu by hyperledger.
the class MergeBesuControllerBuilderTest method assertBuiltContextMonitorsTTD.
@Test
public void assertBuiltContextMonitorsTTD() {
final GenesisState genesisState = GenesisState.fromConfig(genesisConfigFile, this.besuControllerBuilder.createProtocolSchedule());
final MutableBlockchain blockchain = createInMemoryBlockchain(genesisState.getBlock());
final MergeContext mergeContext = spy(besuControllerBuilder.createConsensusContext(blockchain, mock(WorldStateArchive.class), this.besuControllerBuilder.createProtocolSchedule()));
assertThat(mergeContext).isNotNull();
Difficulty over = Difficulty.of(10000L);
Difficulty under = Difficulty.of(10L);
BlockHeader parent = headerGenerator.difficulty(under).parentHash(genesisState.getBlock().getHash()).number(genesisState.getBlock().getHeader().getNumber() + 1).gasLimit(genesisState.getBlock().getHeader().getGasLimit()).stateRoot(genesisState.getBlock().getHeader().getStateRoot()).buildHeader();
blockchain.appendBlock(new Block(parent, BlockBody.empty()), Collections.emptyList());
BlockHeader terminal = headerGenerator.difficulty(over).parentHash(parent.getHash()).number(parent.getNumber() + 1).gasLimit(parent.getGasLimit()).stateRoot(parent.getStateRoot()).buildHeader();
blockchain.appendBlock(new Block(terminal, BlockBody.empty()), Collections.emptyList());
assertThat(mergeContext.isPostMerge()).isTrue();
}
use of org.hyperledger.besu.consensus.merge.MergeContext in project besu by hyperledger.
the class MergeBesuControllerBuilderTest method assertConfiguredBlock.
@Test
public void assertConfiguredBlock() {
Blockchain mockChain = mock(Blockchain.class);
when(mockChain.getBlockHeader(anyLong())).thenReturn(Optional.of(mock(BlockHeader.class)));
MergeContext mergeContext = besuControllerBuilder.createConsensusContext(mockChain, mock(WorldStateArchive.class), this.besuControllerBuilder.createProtocolSchedule());
assertThat(mergeContext).isNotNull();
assertThat(mergeContext.getTerminalPoWBlock()).isPresent();
}
use of org.hyperledger.besu.consensus.merge.MergeContext in project besu by hyperledger.
the class BesuControllerBuilder method createPivotSelector.
private PivotBlockSelector createPivotSelector(final ProtocolContext protocolContext) {
final PivotSelectorFromPeers pivotSelectorFromPeers = new PivotSelectorFromPeers(syncConfig);
final GenesisConfigOptions genesisConfigOptions = configOptionsSupplier.get();
if (genesisConfigOptions.getTerminalTotalDifficulty().isPresent()) {
LOG.info("TTD difficulty is present, creating initial sync phase with transition to PoS support");
final MergeContext mergeContext = protocolContext.getConsensusContext(MergeContext.class);
final FinalizedBlockHashSupplier finalizedBlockHashSupplier = new FinalizedBlockHashSupplier();
final long subscriptionId = mergeContext.addNewForkchoiceMessageListener(finalizedBlockHashSupplier);
final Runnable unsubscribeFinalizedBlockHashListener = () -> {
mergeContext.removeNewForkchoiceMessageListener(subscriptionId);
LOG.info("Initial sync done, unsubscribe finalized block hash supplier");
};
return new TransitionPivotSelector(genesisConfigOptions, finalizedBlockHashSupplier, pivotSelectorFromPeers, new PivotSelectorFromFinalizedBlock(genesisConfigOptions, finalizedBlockHashSupplier, unsubscribeFinalizedBlockHashListener));
} else {
LOG.info("TTD difficulty is not present, creating initial sync phase for PoW");
return pivotSelectorFromPeers;
}
}
use of org.hyperledger.besu.consensus.merge.MergeContext in project besu by hyperledger.
the class MergeBesuControllerBuilder method createConsensusContext.
@Override
protected MergeContext createConsensusContext(final Blockchain blockchain, final WorldStateArchive worldStateArchive, final ProtocolSchedule protocolSchedule) {
OptionalLong terminalBlockNumber = configOptionsSupplier.get().getTerminalBlockNumber();
Optional<Hash> terminalBlockHash = configOptionsSupplier.get().getTerminalBlockHash();
final MergeContext mergeContext = PostMergeContext.get().setSyncState(syncState.get()).setTerminalTotalDifficulty(configOptionsSupplier.get().getTerminalTotalDifficulty().map(Difficulty::of).orElse(Difficulty.ZERO));
blockchain.getFinalized().flatMap(blockchain::getBlockHeader).ifPresent(mergeContext::setFinalized);
blockchain.getSafeBlock().flatMap(blockchain::getBlockHeader).ifPresent(mergeContext::setSafeBlock);
if (terminalBlockNumber.isPresent() && terminalBlockHash.isPresent()) {
Optional<BlockHeader> termBlock = blockchain.getBlockHeader(terminalBlockNumber.getAsLong());
mergeContext.setTerminalPoWBlock(termBlock);
}
blockchain.observeBlockAdded(blockAddedEvent -> blockchain.getTotalDifficultyByHash(blockAddedEvent.getBlock().getHeader().getHash()).ifPresent(mergeContext::setIsPostMerge));
return mergeContext;
}
Aggregations