Search in sources :

Example 1 with RetryingGetHeaderFromPeerByNumberTask

use of org.hyperledger.besu.ethereum.eth.sync.tasks.RetryingGetHeaderFromPeerByNumberTask in project besu by hyperledger.

the class FastSyncTargetManager method confirmPivotBlockHeader.

private CompletableFuture<Optional<EthPeer>> confirmPivotBlockHeader(final EthPeer bestPeer) {
    final BlockHeader pivotBlockHeader = fastSyncState.getPivotBlockHeader().get();
    final RetryingGetHeaderFromPeerByNumberTask task = RetryingGetHeaderFromPeerByNumberTask.forSingleNumber(protocolSchedule, ethContext, metricsSystem, pivotBlockHeader.getNumber(), MAX_QUERY_RETRIES_PER_PEER);
    task.assignPeer(bestPeer);
    return ethContext.getScheduler().timeout(task).thenCompose(result -> {
        if (peerHasDifferentPivotBlock(result)) {
            if (!hasPivotChanged(pivotBlockHeader)) {
                // if the pivot block has not changed, then warn and disconnect this peer
                LOG.warn("Best peer has wrong pivot block (#{}) expecting {} but received {}.  Disconnect: {}", pivotBlockHeader.getNumber(), pivotBlockHeader.getHash(), result.size() == 1 ? result.get(0).getHash() : "invalid response", bestPeer);
                bestPeer.disconnect(DisconnectReason.USELESS_PEER);
                return CompletableFuture.completedFuture(Optional.<EthPeer>empty());
            }
            LOG.debug("Retrying best peer {} with new pivot block {}", bestPeer.getShortNodeId(), pivotBlockHeader.toLogString());
            return confirmPivotBlockHeader(bestPeer);
        } else {
            return CompletableFuture.completedFuture(Optional.of(bestPeer));
        }
    }).exceptionally(error -> {
        LOG.debug("Could not confirm best peer had pivot block", error);
        return Optional.empty();
    });
}
Also used : Logger(org.slf4j.Logger) EthPeer(org.hyperledger.besu.ethereum.eth.manager.EthPeer) SyncTargetManager(org.hyperledger.besu.ethereum.eth.sync.SyncTargetManager) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) LoggerFactory(org.slf4j.LoggerFactory) EthContext(org.hyperledger.besu.ethereum.eth.manager.EthContext) CompletableFuture(java.util.concurrent.CompletableFuture) SynchronizerConfiguration(org.hyperledger.besu.ethereum.eth.sync.SynchronizerConfiguration) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) List(java.util.List) RetryingGetHeaderFromPeerByNumberTask(org.hyperledger.besu.ethereum.eth.sync.tasks.RetryingGetHeaderFromPeerByNumberTask) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) Optional(java.util.Optional) WorldStateStorage(org.hyperledger.besu.ethereum.worldstate.WorldStateStorage) MetricsSystem(org.hyperledger.besu.plugin.services.MetricsSystem) MAX_QUERY_RETRIES_PER_PEER(org.hyperledger.besu.ethereum.eth.sync.fastsync.PivotBlockRetriever.MAX_QUERY_RETRIES_PER_PEER) DisconnectReason(org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason) EthPeer(org.hyperledger.besu.ethereum.eth.manager.EthPeer) RetryingGetHeaderFromPeerByNumberTask(org.hyperledger.besu.ethereum.eth.sync.tasks.RetryingGetHeaderFromPeerByNumberTask) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader)

Example 2 with RetryingGetHeaderFromPeerByNumberTask

use of org.hyperledger.besu.ethereum.eth.sync.tasks.RetryingGetHeaderFromPeerByNumberTask in project besu by hyperledger.

the class PivotBlockConfirmer method executePivotQuery.

private CompletableFuture<BlockHeader> executePivotQuery(final long blockNumber) {
    if (isCancelled.get() || result.isDone()) {
        // Stop loop if this task is done
        return CompletableFuture.failedFuture(new CancellationException());
    }
    final Optional<RetryingGetHeaderFromPeerByNumberTask> query = createPivotQuery(blockNumber);
    final CompletableFuture<BlockHeader> pivotHeaderFuture;
    if (query.isPresent()) {
        final CompletableFuture<BlockHeader> headerQuery = query.get().getHeader();
        pivotHeaderFuture = FutureUtils.exceptionallyCompose(headerQuery, (error) -> executePivotQuery(blockNumber));
    } else {
        // We were unable to find a peer to query, wait and try again
        LOG.debug("No peer currently available to query for block {}.", blockNumber);
        pivotHeaderFuture = ethContext.getScheduler().timeout(WaitForPeerTask.create(ethContext, metricsSystem), Duration.ofSeconds(5)).handle(// Ignore result
        (err, res) -> null).thenCompose(res -> executePivotQuery(blockNumber));
    }
    return pivotHeaderFuture;
}
Also used : EthTask(org.hyperledger.besu.ethereum.eth.manager.task.EthTask) Logger(org.slf4j.Logger) EthPeer(org.hyperledger.besu.ethereum.eth.manager.EthPeer) CancellationException(java.util.concurrent.CancellationException) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LoggerFactory(org.slf4j.LoggerFactory) EthContext(org.hyperledger.besu.ethereum.eth.manager.EthContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Bytes(org.apache.tuweni.bytes.Bytes) Collectors(java.util.stream.Collectors) WaitForPeerTask(org.hyperledger.besu.ethereum.eth.manager.task.WaitForPeerTask) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) FutureUtils(org.hyperledger.besu.util.FutureUtils) RetryingGetHeaderFromPeerByNumberTask(org.hyperledger.besu.ethereum.eth.sync.tasks.RetryingGetHeaderFromPeerByNumberTask) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) Optional(java.util.Optional) MetricsSystem(org.hyperledger.besu.plugin.services.MetricsSystem) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) CancellationException(java.util.concurrent.CancellationException) RetryingGetHeaderFromPeerByNumberTask(org.hyperledger.besu.ethereum.eth.sync.tasks.RetryingGetHeaderFromPeerByNumberTask) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader)

Example 3 with RetryingGetHeaderFromPeerByNumberTask

use of org.hyperledger.besu.ethereum.eth.sync.tasks.RetryingGetHeaderFromPeerByNumberTask in project besu by hyperledger.

the class PivotBlockConfirmer method createGetHeaderTask.

Optional<RetryingGetHeaderFromPeerByNumberTask> createGetHeaderTask(final EthPeer peer, final long blockNumber) {
    final RetryingGetHeaderFromPeerByNumberTask task = RetryingGetHeaderFromPeerByNumberTask.forSingleNumber(protocolSchedule, ethContext, metricsSystem, blockNumber, numberOfRetriesPerPeer);
    task.assignPeer(peer);
    // Try adding our task
    synchronized (runningQueries) {
        if (isCancelled.get()) {
            // Don't run a new query if this task is already cancelled
            return Optional.empty();
        }
        final RetryingGetHeaderFromPeerByNumberTask preexistingTask = pivotBlockQueriesByPeerId.putIfAbsent(peer.nodeId(), task);
        if (preexistingTask != null) {
            // We already have a task for this peer, try again later
            return Optional.empty();
        }
    }
    LOG.debug("Query peer {}... for block {}.", peer.nodeId().toString().substring(0, 8), blockNumber);
    return Optional.of(task);
}
Also used : RetryingGetHeaderFromPeerByNumberTask(org.hyperledger.besu.ethereum.eth.sync.tasks.RetryingGetHeaderFromPeerByNumberTask)

Aggregations

RetryingGetHeaderFromPeerByNumberTask (org.hyperledger.besu.ethereum.eth.sync.tasks.RetryingGetHeaderFromPeerByNumberTask)3 Optional (java.util.Optional)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)2 EthContext (org.hyperledger.besu.ethereum.eth.manager.EthContext)2 EthPeer (org.hyperledger.besu.ethereum.eth.manager.EthPeer)2 ProtocolSchedule (org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule)2 MetricsSystem (org.hyperledger.besu.plugin.services.MetricsSystem)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Duration (java.time.Duration)1 Collection (java.util.Collection)1 List (java.util.List)1 Map (java.util.Map)1 CancellationException (java.util.concurrent.CancellationException)1 CompletableFuture.completedFuture (java.util.concurrent.CompletableFuture.completedFuture)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1