Search in sources :

Example 1 with INVALID_REQUEST_CODE

use of tech.pegasys.teku.networking.eth2.rpc.core.RpcResponseStatus.INVALID_REQUEST_CODE 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 2 with INVALID_REQUEST_CODE

use of tech.pegasys.teku.networking.eth2.rpc.core.RpcResponseStatus.INVALID_REQUEST_CODE in project teku by ConsenSys.

the class BeaconBlocksByRangeMessageHandlerTest method shouldRejectRequestWhenStepIsZero.

@Test
public void shouldRejectRequestWhenStepIsZero() {
    final int startBlock = 15;
    final UInt64 count = UInt64.valueOf(MAX_REQUEST_BLOCKS);
    final int skip = 0;
    withCanonicalHeadBlock(blocksWStates.get(5));
    handler.onIncomingMessage(protocolId, peer, new BeaconBlocksByRangeRequestMessage(UInt64.valueOf(startBlock), count, UInt64.valueOf(skip)), listener);
    verify(listener).completeWithErrorResponse(new RpcException(INVALID_REQUEST_CODE, "Step must be greater than zero"));
    verifyNoMoreInteractions(listener);
    verifyNoMoreInteractions(combinedChainDataClient);
}
Also used : RpcException(tech.pegasys.teku.networking.eth2.rpc.core.RpcException) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) BeaconBlocksByRangeRequestMessage(tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.BeaconBlocksByRangeRequestMessage) Test(org.junit.jupiter.api.Test)

Example 3 with INVALID_REQUEST_CODE

use of tech.pegasys.teku.networking.eth2.rpc.core.RpcResponseStatus.INVALID_REQUEST_CODE in project teku by ConsenSys.

the class DefaultEth2Peer method wantToReceiveObjects.

@Override
public boolean wantToReceiveObjects(final ResponseCallback<SignedBeaconBlock> callback, final long objectCount) {
    if (blockRequestTracker.wantToRequestObjects(objectCount) == 0L) {
        LOG.debug("Peer {} disconnected due to block rate limits", getId());
        callback.completeWithErrorResponse(new RpcException(INVALID_REQUEST_CODE, "Peer has been rate limited"));
        disconnectCleanly(DisconnectReason.RATE_LIMITING).reportExceptions();
        return false;
    }
    return true;
}
Also used : RpcException(tech.pegasys.teku.networking.eth2.rpc.core.RpcException)

Aggregations

RpcException (tech.pegasys.teku.networking.eth2.rpc.core.RpcException)3 ClosedChannelException (java.nio.channels.ClosedChannelException)1 Test (org.junit.jupiter.api.Test)1 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)1 StreamClosedException (tech.pegasys.teku.networking.p2p.rpc.StreamClosedException)1 BeaconBlocksByRangeRequestMessage (tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.BeaconBlocksByRangeRequestMessage)1