Search in sources :

Example 1 with CLOSED

use of tech.pegasys.teku.networking.eth2.rpc.core.Eth2OutgoingRequestHandler.State.CLOSED in project teku by ConsenSys.

the class Eth2OutgoingRequestHandler method ensureReadCompleteArrivesInTime.

private void ensureReadCompleteArrivesInTime(final RpcStream stream) {
    final Duration timeout = RpcTimeouts.RESP_TIMEOUT;
    timeoutRunner.getDelayedFuture(timeout).thenAccept((__) -> {
        if (!(state.get() == READ_COMPLETE || state.get() == CLOSED)) {
            abortRequest(stream, new RpcTimeoutException("Timed out waiting for read channel close", timeout));
        }
    }).reportExceptions();
}
Also used : INITIAL(tech.pegasys.teku.networking.eth2.rpc.core.Eth2OutgoingRequestHandler.State.INITIAL) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SafeFuture(tech.pegasys.teku.infrastructure.async.SafeFuture) RpcRequest(tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.RpcRequest) Bytes(org.apache.tuweni.bytes.Bytes) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuf(io.netty.buffer.ByteBuf) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) SszData(tech.pegasys.teku.infrastructure.ssz.SszData) RpcStream(tech.pegasys.teku.networking.p2p.rpc.RpcStream) ABORTED(tech.pegasys.teku.networking.eth2.rpc.core.Eth2OutgoingRequestHandler.State.ABORTED) AsyncRunner(tech.pegasys.teku.infrastructure.async.AsyncRunner) Collection(java.util.Collection) DATA_COMPLETED(tech.pegasys.teku.networking.eth2.rpc.core.Eth2OutgoingRequestHandler.State.DATA_COMPLETED) RpcRequestHandler(tech.pegasys.teku.networking.p2p.rpc.RpcRequestHandler) List(java.util.List) Logger(org.apache.logging.log4j.Logger) RpcTimeoutException(tech.pegasys.teku.networking.eth2.rpc.core.RpcTimeouts.RpcTimeoutException) CLOSED(tech.pegasys.teku.networking.eth2.rpc.core.Eth2OutgoingRequestHandler.State.CLOSED) READ_COMPLETE(tech.pegasys.teku.networking.eth2.rpc.core.Eth2OutgoingRequestHandler.State.READ_COMPLETE) VisibleForTesting(com.google.common.annotations.VisibleForTesting) LogManager(org.apache.logging.log4j.LogManager) EXPECT_DATA(tech.pegasys.teku.networking.eth2.rpc.core.Eth2OutgoingRequestHandler.State.EXPECT_DATA) NodeId(tech.pegasys.teku.networking.p2p.peer.NodeId) ExtraDataAppendedException(tech.pegasys.teku.networking.eth2.rpc.core.RpcException.ExtraDataAppendedException) Duration(java.time.Duration) RpcTimeoutException(tech.pegasys.teku.networking.eth2.rpc.core.RpcTimeouts.RpcTimeoutException)

Example 2 with CLOSED

use of tech.pegasys.teku.networking.eth2.rpc.core.Eth2OutgoingRequestHandler.State.CLOSED in project teku by ConsenSys.

the class BeaconBlocksByRangeMessageHandler method onIncomingMessage.

@Override
public void onIncomingMessage(final String protocolId, final Eth2Peer peer, final BeaconBlocksByRangeRequestMessage message, final ResponseCallback<SignedBeaconBlock> callback) {
    LOG.trace("Peer {} requested {} BeaconBlocks starting at slot {} with step {}", peer.getId(), message.getCount(), message.getStartSlot(), message.getStep());
    if (message.getStep().compareTo(ONE) < 0) {
        callback.completeWithErrorResponse(new RpcException(INVALID_REQUEST_CODE, "Step must be greater than zero"));
        return;
    }
    if (message.getCount().compareTo(UInt64.valueOf(MAX_REQUEST_BLOCKS)) > 0) {
        callback.completeWithErrorResponse(new RpcException(INVALID_REQUEST_CODE, "Only a maximum of " + MAX_REQUEST_BLOCKS + " blocks can be requested per request"));
        return;
    }
    if (!peer.wantToMakeRequest() || !peer.wantToReceiveObjects(callback, maxRequestSize.min(message.getCount()).longValue())) {
        return;
    }
    sendMatchingBlocks(message, callback).finish(callback::completeSuccessfully, error -> {
        final Throwable rootCause = Throwables.getRootCause(error);
        if (rootCause instanceof RpcException) {
            // Keep full context
            LOG.trace("Rejecting beacon blocks by range request", error);
            callback.completeWithErrorResponse((RpcException) rootCause);
        } else {
            if (rootCause instanceof StreamClosedException || rootCause instanceof ClosedChannelException) {
                LOG.trace("Stream closed while sending requested blocks", error);
            } else {
                LOG.error("Failed to process blocks by range request", error);
            }
            callback.completeWithUnexpectedError(error);
        }
    });
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) RpcException(tech.pegasys.teku.networking.eth2.rpc.core.RpcException) StreamClosedException(tech.pegasys.teku.networking.p2p.rpc.StreamClosedException)

Aggregations

VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ByteBuf (io.netty.buffer.ByteBuf)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 Duration (java.time.Duration)1 Collection (java.util.Collection)1 List (java.util.List)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 Bytes (org.apache.tuweni.bytes.Bytes)1 AsyncRunner (tech.pegasys.teku.infrastructure.async.AsyncRunner)1 SafeFuture (tech.pegasys.teku.infrastructure.async.SafeFuture)1 SszData (tech.pegasys.teku.infrastructure.ssz.SszData)1 ABORTED (tech.pegasys.teku.networking.eth2.rpc.core.Eth2OutgoingRequestHandler.State.ABORTED)1 CLOSED (tech.pegasys.teku.networking.eth2.rpc.core.Eth2OutgoingRequestHandler.State.CLOSED)1 DATA_COMPLETED (tech.pegasys.teku.networking.eth2.rpc.core.Eth2OutgoingRequestHandler.State.DATA_COMPLETED)1 EXPECT_DATA (tech.pegasys.teku.networking.eth2.rpc.core.Eth2OutgoingRequestHandler.State.EXPECT_DATA)1 INITIAL (tech.pegasys.teku.networking.eth2.rpc.core.Eth2OutgoingRequestHandler.State.INITIAL)1