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();
}
}
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));
}
Aggregations