Search in sources :

Example 1 with ExtraDataAppendedException

use of tech.pegasys.teku.networking.eth2.rpc.core.RpcException.ExtraDataAppendedException in project teku by ConsenSys.

the class Eth2OutgoingRequestHandlerTest method sendTooManyChunks.

@Test
public void sendTooManyChunks() throws Exception {
    sendInitialPayload();
    verify(rpcStream).closeWriteStream();
    for (int i = 0; i < maxChunks; i++) {
        if (i == maxChunks - 1) {
            // Send 2 chunks in the last batch of data which should only contain 1 chunk
            final Bytes lastChunk = chunks.get(i);
            final Bytes lastChunkWithExtraChunk = Bytes.concatenate(lastChunk, lastChunk);
            deliverBytes(lastChunkWithExtraChunk);
        } else {
            deliverChunk(i);
        }
    }
    complete();
    close();
    asyncRequestRunner.waitForExactly(maxChunks - 1);
    timeoutRunner.executeUntilDone();
    Waiter.waitFor(() -> assertThat(finishedProcessingFuture).isDone());
    verify(rpcStream).closeAbruptly();
    assertThat(blocks.size()).isEqualTo(2);
    assertThat(finishedProcessingFuture).isCompletedExceptionally();
    assertThatThrownBy(finishedProcessingFuture::get).hasRootCause(new ExtraDataAppendedException());
}
Also used : ExtraDataAppendedException(tech.pegasys.teku.networking.eth2.rpc.core.RpcException.ExtraDataAppendedException) Bytes(org.apache.tuweni.bytes.Bytes) Test(org.junit.jupiter.api.Test)

Example 2 with ExtraDataAppendedException

use of tech.pegasys.teku.networking.eth2.rpc.core.RpcException.ExtraDataAppendedException in project teku by ConsenSys.

the class LengthPrefixedPayloadDecoder method decodeOneMessage.

@Override
public Optional<T> decodeOneMessage(final ByteBuf in) throws RpcException {
    if (disposed) {
        throw new IllegalStateException("Trying to reuse disposed LengthPrefixedPayloadDecoder");
    }
    if (!in.isReadable()) {
        return Optional.empty();
    }
    if (decoded) {
        throw new RpcException.ExtraDataAppendedException();
    }
    if (decompressor.isEmpty()) {
        final Optional<Integer> maybeLength = readLengthPrefixHeader(in);
        if (maybeLength.isPresent()) {
            final int length = maybeLength.get();
            if (!payloadEncoder.isLengthWithinBounds(length)) {
                throw new LengthOutOfBoundsException();
            }
            decompressor = Optional.of(compressor.createDecompressor(length));
        }
    }
    if (decompressor.isPresent()) {
        final Optional<ByteBuf> ret;
        try {
            ret = decompressor.get().decodeOneMessage(in);
        } catch (PayloadSmallerThanExpectedException e) {
            throw new PayloadTruncatedException();
        } catch (PayloadLargerThanExpectedException e) {
            throw new ExtraDataAppendedException();
        } catch (CompressionException e) {
            throw new DecompressFailedException();
        }
        if (ret.isPresent()) {
            decompressor = Optional.empty();
            try {
                // making a copy here since the Bytes.wrapByteBuf(buf).slice(...)
                // would be broken after [in] buffer is released
                byte[] arr = new byte[ret.get().readableBytes()];
                ret.get().readBytes(arr);
                Bytes bytes = Bytes.wrap(arr);
                decoded = true;
                return Optional.of(payloadEncoder.decode(bytes));
            } finally {
                ret.get().release();
            }
        } else {
            return Optional.empty();
        }
    } else {
        return Optional.empty();
    }
}
Also used : PayloadLargerThanExpectedException(tech.pegasys.teku.networking.eth2.rpc.core.encodings.compression.exceptions.PayloadLargerThanExpectedException) CompressionException(tech.pegasys.teku.networking.eth2.rpc.core.encodings.compression.exceptions.CompressionException) ByteBuf(io.netty.buffer.ByteBuf) PayloadSmallerThanExpectedException(tech.pegasys.teku.networking.eth2.rpc.core.encodings.compression.exceptions.PayloadSmallerThanExpectedException) ExtraDataAppendedException(tech.pegasys.teku.networking.eth2.rpc.core.RpcException.ExtraDataAppendedException) Bytes(org.apache.tuweni.bytes.Bytes) PayloadTruncatedException(tech.pegasys.teku.networking.eth2.rpc.core.RpcException.PayloadTruncatedException) LengthOutOfBoundsException(tech.pegasys.teku.networking.eth2.rpc.core.RpcException.LengthOutOfBoundsException) DecompressFailedException(tech.pegasys.teku.networking.eth2.rpc.core.RpcException.DecompressFailedException)

Aggregations

Bytes (org.apache.tuweni.bytes.Bytes)2 ExtraDataAppendedException (tech.pegasys.teku.networking.eth2.rpc.core.RpcException.ExtraDataAppendedException)2 ByteBuf (io.netty.buffer.ByteBuf)1 Test (org.junit.jupiter.api.Test)1 DecompressFailedException (tech.pegasys.teku.networking.eth2.rpc.core.RpcException.DecompressFailedException)1 LengthOutOfBoundsException (tech.pegasys.teku.networking.eth2.rpc.core.RpcException.LengthOutOfBoundsException)1 PayloadTruncatedException (tech.pegasys.teku.networking.eth2.rpc.core.RpcException.PayloadTruncatedException)1 CompressionException (tech.pegasys.teku.networking.eth2.rpc.core.encodings.compression.exceptions.CompressionException)1 PayloadLargerThanExpectedException (tech.pegasys.teku.networking.eth2.rpc.core.encodings.compression.exceptions.PayloadLargerThanExpectedException)1 PayloadSmallerThanExpectedException (tech.pegasys.teku.networking.eth2.rpc.core.encodings.compression.exceptions.PayloadSmallerThanExpectedException)1