Search in sources :

Example 1 with MergeContext

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;
}
Also used : EthProtocolManager(org.hyperledger.besu.ethereum.eth.manager.EthProtocolManager) ConsensusContext(org.hyperledger.besu.ethereum.ConsensusContext) MergeContext(org.hyperledger.besu.consensus.merge.MergeContext) PostMergeContext(org.hyperledger.besu.consensus.merge.PostMergeContext)

Example 2 with MergeContext

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();
}
Also used : GenesisState(org.hyperledger.besu.ethereum.chain.GenesisState) Difficulty(org.hyperledger.besu.ethereum.core.Difficulty) Block(org.hyperledger.besu.ethereum.core.Block) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) MergeContext(org.hyperledger.besu.consensus.merge.MergeContext) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Test(org.junit.Test)

Example 3 with MergeContext

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();
}
Also used : WorldStateArchive(org.hyperledger.besu.ethereum.worldstate.WorldStateArchive) Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) InMemoryKeyValueStorageProvider.createInMemoryBlockchain(org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryBlockchain) MergeContext(org.hyperledger.besu.consensus.merge.MergeContext) Test(org.junit.Test)

Example 4 with MergeContext

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;
    }
}
Also used : PivotSelectorFromFinalizedBlock(org.hyperledger.besu.ethereum.eth.sync.fastsync.PivotSelectorFromFinalizedBlock) MergeContext(org.hyperledger.besu.consensus.merge.MergeContext) PivotSelectorFromPeers(org.hyperledger.besu.ethereum.eth.sync.fastsync.PivotSelectorFromPeers) GenesisConfigOptions(org.hyperledger.besu.config.GenesisConfigOptions) FinalizedBlockHashSupplier(org.hyperledger.besu.consensus.merge.FinalizedBlockHashSupplier) TransitionPivotSelector(org.hyperledger.besu.ethereum.eth.sync.fastsync.TransitionPivotSelector)

Example 5 with MergeContext

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;
}
Also used : Difficulty(org.hyperledger.besu.ethereum.core.Difficulty) OptionalLong(java.util.OptionalLong) MergeContext(org.hyperledger.besu.consensus.merge.MergeContext) PostMergeContext(org.hyperledger.besu.consensus.merge.PostMergeContext) Hash(org.hyperledger.besu.datatypes.Hash) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader)

Aggregations

MergeContext (org.hyperledger.besu.consensus.merge.MergeContext)8 MutableBlockchain (org.hyperledger.besu.ethereum.chain.MutableBlockchain)4 Test (org.junit.Test)4 PostMergeContext (org.hyperledger.besu.consensus.merge.PostMergeContext)3 Blockchain (org.hyperledger.besu.ethereum.chain.Blockchain)3 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)3 InMemoryKeyValueStorageProvider.createInMemoryBlockchain (org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryBlockchain)3 WorldStateArchive (org.hyperledger.besu.ethereum.worldstate.WorldStateArchive)3 ConsensusContext (org.hyperledger.besu.ethereum.ConsensusContext)2 Difficulty (org.hyperledger.besu.ethereum.core.Difficulty)2 OptionalLong (java.util.OptionalLong)1 GenesisConfigOptions (org.hyperledger.besu.config.GenesisConfigOptions)1 FinalizedBlockHashSupplier (org.hyperledger.besu.consensus.merge.FinalizedBlockHashSupplier)1 Hash (org.hyperledger.besu.datatypes.Hash)1 GenesisState (org.hyperledger.besu.ethereum.chain.GenesisState)1 Block (org.hyperledger.besu.ethereum.core.Block)1 EthProtocolManager (org.hyperledger.besu.ethereum.eth.manager.EthProtocolManager)1 DefaultSynchronizer (org.hyperledger.besu.ethereum.eth.sync.DefaultSynchronizer)1 PivotSelectorFromFinalizedBlock (org.hyperledger.besu.ethereum.eth.sync.fastsync.PivotSelectorFromFinalizedBlock)1 PivotSelectorFromPeers (org.hyperledger.besu.ethereum.eth.sync.fastsync.PivotSelectorFromPeers)1