Search in sources :

Example 1 with LengthOutOfBoundsException

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

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

the class ErrorConditionsIntegrationTest method shouldRejectInvalidRequests.

@Test
public void shouldRejectInvalidRequests() throws Exception {
    final Eth2P2PNetwork network1 = networkFactory.builder().rpcEncoding(rpcEncoding).startNetwork();
    final Eth2P2PNetwork network2 = networkFactory.builder().rpcEncoding(rpcEncoding).peer(network1).startNetwork();
    final Eth2Peer peer = network1.getPeer(network2.getNodeId()).orElseThrow();
    final Eth2RpcMethod<StatusMessage, StatusMessage> status = ((ActiveEth2P2PNetwork) network1).getBeaconChainMethods().status();
    final SafeFuture<StatusMessage> response = peer.requestSingleItem(status, new InvalidStatusMessage(spec.getGenesisSpecConfig().getGenesisForkVersion()));
    final RpcException expected = new LengthOutOfBoundsException();
    Assertions.assertThatThrownBy(() -> Waiter.waitFor(response)).isInstanceOf(ExecutionException.class).extracting(Throwable::getCause).isInstanceOf(RpcException.class).is(new Condition<>(error -> {
        final RpcException rpcException = (RpcException) error;
        return rpcException.getErrorMessageString().equals(expected.getErrorMessageString()) && rpcException.getResponseCode() == expected.getResponseCode();
    }, "Exception did not match expected exception %s", expected));
}
Also used : LengthOutOfBoundsException(tech.pegasys.teku.networking.eth2.rpc.core.RpcException.LengthOutOfBoundsException) RpcException(tech.pegasys.teku.networking.eth2.rpc.core.RpcException) StatusMessage(tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.StatusMessage) SafeFuture(tech.pegasys.teku.infrastructure.async.SafeFuture) Eth2RpcMethod(tech.pegasys.teku.networking.eth2.rpc.core.methods.Eth2RpcMethod) Bytes(org.apache.tuweni.bytes.Bytes) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) TestSpecFactory(tech.pegasys.teku.spec.TestSpecFactory) RpcEncoding(tech.pegasys.teku.networking.eth2.rpc.core.encodings.RpcEncoding) Eth2Peer(tech.pegasys.teku.networking.eth2.peers.Eth2Peer) Condition(org.assertj.core.api.Condition) Waiter(tech.pegasys.teku.infrastructure.async.Waiter) Assertions(org.assertj.core.api.Assertions) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Bytes4(tech.pegasys.teku.infrastructure.bytes.Bytes4) Spec(tech.pegasys.teku.spec.Spec) Bytes32(org.apache.tuweni.bytes.Bytes32) MAX_CHUNK_SIZE(tech.pegasys.teku.spec.config.Constants.MAX_CHUNK_SIZE) RpcException(tech.pegasys.teku.networking.eth2.rpc.core.RpcException) LengthOutOfBoundsException(tech.pegasys.teku.networking.eth2.rpc.core.RpcException.LengthOutOfBoundsException) Eth2Peer(tech.pegasys.teku.networking.eth2.peers.Eth2Peer) ExecutionException(java.util.concurrent.ExecutionException) StatusMessage(tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.StatusMessage) Test(org.junit.jupiter.api.Test)

Aggregations

Bytes (org.apache.tuweni.bytes.Bytes)2 LengthOutOfBoundsException (tech.pegasys.teku.networking.eth2.rpc.core.RpcException.LengthOutOfBoundsException)2 ByteBuf (io.netty.buffer.ByteBuf)1 ExecutionException (java.util.concurrent.ExecutionException)1 Bytes32 (org.apache.tuweni.bytes.Bytes32)1 Assertions (org.assertj.core.api.Assertions)1 Condition (org.assertj.core.api.Condition)1 AfterEach (org.junit.jupiter.api.AfterEach)1 Test (org.junit.jupiter.api.Test)1 SafeFuture (tech.pegasys.teku.infrastructure.async.SafeFuture)1 Waiter (tech.pegasys.teku.infrastructure.async.Waiter)1 Bytes4 (tech.pegasys.teku.infrastructure.bytes.Bytes4)1 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)1 Eth2Peer (tech.pegasys.teku.networking.eth2.peers.Eth2Peer)1 RpcException (tech.pegasys.teku.networking.eth2.rpc.core.RpcException)1 DecompressFailedException (tech.pegasys.teku.networking.eth2.rpc.core.RpcException.DecompressFailedException)1 ExtraDataAppendedException (tech.pegasys.teku.networking.eth2.rpc.core.RpcException.ExtraDataAppendedException)1 PayloadTruncatedException (tech.pegasys.teku.networking.eth2.rpc.core.RpcException.PayloadTruncatedException)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