Search in sources :

Example 1 with BlockAddedObserver

use of org.hyperledger.besu.ethereum.chain.BlockAddedObserver in project besu by hyperledger.

the class MigratingMiningCoordinator method onBlockAdded.

@Override
public void onBlockAdded(final BlockAddedEvent event) {
    final long currentBlock = event.getBlock().getHeader().getNumber();
    final MiningCoordinator nextMiningCoordinator = miningCoordinatorSchedule.getFork(currentBlock + 1).getValue();
    if (activeMiningCoordinator != nextMiningCoordinator) {
        LOG.trace("Migrating mining coordinator at block {} from {} to {}", currentBlock, activeMiningCoordinator.getClass().getSimpleName(), nextMiningCoordinator.getClass().getSimpleName());
        final Runnable stopActiveCoordinatorTask = () -> activeMiningCoordinator.stop();
        final Runnable startNextCoordinatorTask = () -> {
            activeMiningCoordinator = nextMiningCoordinator;
            startActiveMiningCoordinator();
            if (activeMiningCoordinator instanceof BlockAddedObserver) {
                ((BlockAddedObserver) activeMiningCoordinator).onBlockAdded(event);
            }
        };
        CompletableFuture.runAsync(stopActiveCoordinatorTask).thenRun(startNextCoordinatorTask);
    } else if (activeMiningCoordinator instanceof BlockAddedObserver) {
        ((BlockAddedObserver) activeMiningCoordinator).onBlockAdded(event);
    }
}
Also used : BlockAddedObserver(org.hyperledger.besu.ethereum.chain.BlockAddedObserver) MiningCoordinator(org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator)

Aggregations

MiningCoordinator (org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator)1 BlockAddedObserver (org.hyperledger.besu.ethereum.chain.BlockAddedObserver)1