Search in sources :

Example 1 with StreamClosedException

use of tech.pegasys.teku.networking.p2p.rpc.StreamClosedException in project teku by ConsenSys.

the class BeaconBlocksByRootMessageHandlerTest method onIncomingMessage_interruptedByClosedStream.

@ParameterizedTest(name = "protocol={0}")
@MethodSource("protocolIdParams")
public void onIncomingMessage_interruptedByClosedStream(final String protocolId) {
    final List<SignedBeaconBlock> blocks = buildChain(5);
    // Mock callback to appear to be closed
    doThrow(new StreamClosedException()).when(callback).respond(any());
    final BeaconBlocksByRootRequestMessage message = createRequest(blocks);
    handler.onIncomingMessage(protocolId, peer, message, callback);
    // Check that we only asked for the first block
    verify(store, times(1)).retrieveSignedBlock(any());
    verify(callback, times(1)).respond(any());
}
Also used : BeaconBlocksByRootRequestMessage(tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.BeaconBlocksByRootRequestMessage) StreamClosedException(tech.pegasys.teku.networking.p2p.rpc.StreamClosedException) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 2 with StreamClosedException

use of tech.pegasys.teku.networking.p2p.rpc.StreamClosedException 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)

Example 3 with StreamClosedException

use of tech.pegasys.teku.networking.p2p.rpc.StreamClosedException in project teku by ConsenSys.

the class LibP2PRpcStream method writeBytes.

@Override
public SafeFuture<Void> writeBytes(final Bytes bytes) throws StreamClosedException {
    if (writeStreamClosed.get()) {
        throw new StreamClosedException();
    }
    final ByteBuf reqByteBuf = ctx.alloc().buffer();
    reqByteBuf.writeBytes(bytes.toArrayUnsafe());
    return toSafeFuture(ctx.writeAndFlush(reqByteBuf));
}
Also used : StreamClosedException(tech.pegasys.teku.networking.p2p.rpc.StreamClosedException) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

StreamClosedException (tech.pegasys.teku.networking.p2p.rpc.StreamClosedException)3 ByteBuf (io.netty.buffer.ByteBuf)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1 RpcException (tech.pegasys.teku.networking.eth2.rpc.core.RpcException)1 SignedBeaconBlock (tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock)1 BeaconBlocksByRootRequestMessage (tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.BeaconBlocksByRootRequestMessage)1