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