Search in sources :

Example 1 with PayloadTruncatedException

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

the class RpcRequestDecoder method complete.

public Optional<T> complete() throws RpcException {
    Optional<T> maybeRequest = Optional.empty();
    if (!complete) {
        // complete() might be the only event on empty request
        // so we might need to produce a message with decodeRequest(EMPTY_BUFFER)
        maybeRequest = decodeRequest(Unpooled.EMPTY_BUFFER);
    }
    decoder.complete();
    if (!complete)
        throw new PayloadTruncatedException();
    return maybeRequest;
}
Also used : PayloadTruncatedException(tech.pegasys.teku.networking.eth2.rpc.core.RpcException.PayloadTruncatedException)

Example 2 with PayloadTruncatedException

use of tech.pegasys.teku.networking.eth2.rpc.core.RpcException.PayloadTruncatedException 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)

Example 3 with PayloadTruncatedException

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

the class RpcResponseDecoder method complete.

public void complete() throws RpcException {
    final List<RpcException> exceptions = new ArrayList<>();
    if (respCodeMaybe.isPresent() && payloadDecoder.isEmpty() && errorDecoder.isEmpty()) {
        exceptions.add(new MessageTruncatedException());
    } else if (respCodeMaybe.isPresent()) {
        exceptions.add(new PayloadTruncatedException());
    }
    completeDecoder(payloadDecoder).ifPresent(exceptions::add);
    payloadDecoder = Optional.empty();
    completeDecoder(contextDecoder).ifPresent(exceptions::add);
    contextDecoder = Optional.empty();
    completeDecoder(errorDecoder).ifPresent(exceptions::add);
    errorDecoder = Optional.empty();
    if (exceptions.size() > 0) {
        throw exceptions.stream().reduce((a, b) -> {
            a.addSuppressed(b);
            return a;
        }).get();
    }
}
Also used : SUCCESS_RESPONSE_CODE(tech.pegasys.teku.networking.eth2.rpc.core.RpcResponseStatus.SUCCESS_RESPONSE_CODE) RpcErrorMessage(tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.RpcErrorMessage) RpcContextCodec(tech.pegasys.teku.networking.eth2.rpc.core.encodings.context.RpcContextCodec) ByteUtil.toUnsignedInt(tech.pegasys.teku.infrastructure.unsigned.ByteUtil.toUnsignedInt) Bytes(org.apache.tuweni.bytes.Bytes) ByteUtil.toByteExactUnsigned(tech.pegasys.teku.infrastructure.unsigned.ByteUtil.toByteExactUnsigned) RpcByteBufDecoder(tech.pegasys.teku.networking.eth2.rpc.core.encodings.RpcByteBufDecoder) ArrayList(java.util.ArrayList) ByteBufDecoder(tech.pegasys.teku.networking.eth2.rpc.core.encodings.ByteBufDecoder) List(java.util.List) ByteBuf(io.netty.buffer.ByteBuf) RpcEncoding(tech.pegasys.teku.networking.eth2.rpc.core.encodings.RpcEncoding) MessageTruncatedException(tech.pegasys.teku.networking.eth2.rpc.core.RpcException.MessageTruncatedException) Optional(java.util.Optional) SszData(tech.pegasys.teku.infrastructure.ssz.SszData) UnrecognizedContextBytesException(tech.pegasys.teku.networking.eth2.rpc.core.RpcException.UnrecognizedContextBytesException) PayloadTruncatedException(tech.pegasys.teku.networking.eth2.rpc.core.RpcException.PayloadTruncatedException) SszSchema(tech.pegasys.teku.infrastructure.ssz.schema.SszSchema) PayloadTruncatedException(tech.pegasys.teku.networking.eth2.rpc.core.RpcException.PayloadTruncatedException) ArrayList(java.util.ArrayList) MessageTruncatedException(tech.pegasys.teku.networking.eth2.rpc.core.RpcException.MessageTruncatedException)

Aggregations

PayloadTruncatedException (tech.pegasys.teku.networking.eth2.rpc.core.RpcException.PayloadTruncatedException)3 ByteBuf (io.netty.buffer.ByteBuf)2 Bytes (org.apache.tuweni.bytes.Bytes)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Optional (java.util.Optional)1 SszData (tech.pegasys.teku.infrastructure.ssz.SszData)1 SszSchema (tech.pegasys.teku.infrastructure.ssz.schema.SszSchema)1 ByteUtil.toByteExactUnsigned (tech.pegasys.teku.infrastructure.unsigned.ByteUtil.toByteExactUnsigned)1 ByteUtil.toUnsignedInt (tech.pegasys.teku.infrastructure.unsigned.ByteUtil.toUnsignedInt)1 DecompressFailedException (tech.pegasys.teku.networking.eth2.rpc.core.RpcException.DecompressFailedException)1 ExtraDataAppendedException (tech.pegasys.teku.networking.eth2.rpc.core.RpcException.ExtraDataAppendedException)1 LengthOutOfBoundsException (tech.pegasys.teku.networking.eth2.rpc.core.RpcException.LengthOutOfBoundsException)1 MessageTruncatedException (tech.pegasys.teku.networking.eth2.rpc.core.RpcException.MessageTruncatedException)1 UnrecognizedContextBytesException (tech.pegasys.teku.networking.eth2.rpc.core.RpcException.UnrecognizedContextBytesException)1 SUCCESS_RESPONSE_CODE (tech.pegasys.teku.networking.eth2.rpc.core.RpcResponseStatus.SUCCESS_RESPONSE_CODE)1 ByteBufDecoder (tech.pegasys.teku.networking.eth2.rpc.core.encodings.ByteBufDecoder)1 RpcByteBufDecoder (tech.pegasys.teku.networking.eth2.rpc.core.encodings.RpcByteBufDecoder)1 RpcEncoding (tech.pegasys.teku.networking.eth2.rpc.core.encodings.RpcEncoding)1 CompressionException (tech.pegasys.teku.networking.eth2.rpc.core.encodings.compression.exceptions.CompressionException)1