Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Reduce log level when invalid messages received from peers #274

Merged
merged 9 commits into from
Nov 19, 2018
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- ECRec precompile should return empty instead of 32 zero bytes when the input is invalid (PR [#227](https://github.com/PegaSysEng/pantheon/pull/227))
- File name too long error while building from source ([#215](https://github.com/PegaSysEng/pantheon/issues/215) thanks to [@5chdn](https://github.com/5chdn) for reporting) (PR [#221](https://github.com/PegaSysEng/pantheon/pull/221))
- Loop syntax in `runPantheonPrivateNetwork.sh` (PR [#237](https://github.com/PegaSysEng/pantheon/pull/237) thanks to [@matt9ucci](https://github.com/matt9ucci))
- Fix `CompressionException: Snappy decompression failed` errors ([#251](https://github.com/PegaSysEng/pantheon/issues/251) thanks to [@5chdn](https://github.com/5chdn) for reporting) (PR [#274](https://github.com/PegaSysEng/pantheon/pull/274))

### Additions and Improvements
- Added `--ropsten` command line argument to make syncing to Ropsten easier ([#186](https://github.com/PegaSysEng/pantheon/issues/186)) (PR [#197](https://github.com/PegaSysEng/pantheon/pull/197) with thanks to [@jvirtanen](https://github.com/jvirtanen))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import tech.pegasys.pantheon.ethereum.p2p.api.PeerConnection;
import tech.pegasys.pantheon.ethereum.p2p.netty.exceptions.IncompatiblePeerException;
import tech.pegasys.pantheon.ethereum.p2p.rlpx.framing.Framer;
import tech.pegasys.pantheon.ethereum.p2p.rlpx.framing.FramingException;
import tech.pegasys.pantheon.ethereum.p2p.wire.PeerInfo;
import tech.pegasys.pantheon.ethereum.p2p.wire.SubProtocol;
import tech.pegasys.pantheon.ethereum.p2p.wire.messages.DisconnectMessage.DisconnectReason;
Expand All @@ -31,6 +32,7 @@
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.DecoderException;
import io.netty.handler.timeout.IdleStateHandler;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -117,7 +119,17 @@ protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final L
@Override
public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable throwable)
throws Exception {
if (throwable instanceof IOException) {
final Throwable cause =
throwable instanceof DecoderException && throwable.getCause() != null
? throwable.getCause()
: throwable;
if (cause instanceof FramingException) {
LOG.debug("Invalid incoming message", throwable);
if (connectFuture.isDone()) {
connectFuture.get().disconnect(DisconnectReason.BREACH_OF_PROTOCOL);
return;
}
} else if (cause instanceof IOException) {
// IO failures are routine when communicating with random peers across the network.
LOG.debug("IO error while processing incoming message", throwable);
} else {
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.bouncycastle.crypto.modes.SICBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
import org.iq80.snappy.CorruptionException;

/**
* This component is responsible for reading and composing RLPx protocol frames, conformant to the
Expand Down Expand Up @@ -68,7 +69,7 @@ public class Framer {
.extractArray();

private final HandshakeSecrets secrets;
private static final Compressor compressor = new SnappyCompressor();
private static final SnappyCompressor compressor = new SnappyCompressor();
private final StreamCipher encryptor;
private final StreamCipher decryptor;
private final BlockCipher macEncryptor;
Expand Down Expand Up @@ -254,14 +255,17 @@ private MessageData processFrame(final ByteBuf f, final int frameSize) {
// Write message data to ByteBuf, decompressing as necessary
final BytesValue data;
if (compressionEnabled) {
// Decompress data before writing to ByteBuf
final byte[] compressedMessageData = Arrays.copyOfRange(frameData, 1, frameData.length - pad);
// Check message length
Preconditions.checkState(
compressor.uncompressedLength(compressedMessageData) < LENGTH_MAX_MESSAGE_FRAME,
"Message size in excess of maximum length.");
final byte[] decompressedMessageData = compressor.decompress(compressedMessageData);
data = BytesValue.wrap(decompressedMessageData);
final int uncompressedLength = compressor.uncompressedLength(compressedMessageData);
if (uncompressedLength >= LENGTH_MAX_MESSAGE_FRAME) {
throw error("Message size %s in excess of maximum length.", uncompressedLength);
}
try {
final byte[] decompressedMessageData = compressor.decompress(compressedMessageData);
data = BytesValue.wrap(decompressedMessageData);
} catch (final CorruptionException e) {
throw new FramingException("Decompression failed", e);
rain-on marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
// Move data to a ByteBuf
final int messageLength = frameSize - LENGTH_MESSAGE_ID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,18 @@
*
* @see <a href="https://google.github.io/snappy/">Snappy algorithm</a>
*/
public class SnappyCompressor implements Compressor {
public class SnappyCompressor {

@Override
public byte[] compress(final byte[] uncompressed) {
checkNotNull(uncompressed, "input data must not be null");
return Snappy.compress(uncompressed);
}

@Override
public byte[] decompress(final byte[] compressed) {
checkNotNull(compressed, "input data must not be null");
return Snappy.uncompress(compressed, 0, compressed.length);
}

@Override
public int uncompressedLength(final byte[] compressed) {
checkNotNull(compressed, "input data must not be null");
return Snappy.getUncompressedLength(compressed, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ public void shouldThrowExceptionWhenDeframingCompressedMessageTooLong() throws I
final Framer deframer = new Framer(deframeSecrets);
deframer.enableCompression();

assertThatExceptionOfType(IllegalStateException.class)
assertThatExceptionOfType(FramingException.class)
.isThrownBy(() -> deframer.deframe(framedMessage))
.withMessageContaining("Message size in excess of maximum length.");
.withMessageContaining("Message size 16777216 in excess of maximum length.");
}

@Test
Expand Down
4 changes: 2 additions & 2 deletions pantheon/src/main/java/tech/pegasys/pantheon/Pantheon.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import tech.pegasys.pantheon.cli.PantheonCommand;
import tech.pegasys.pantheon.cli.PantheonControllerBuilder;
import tech.pegasys.pantheon.ethereum.eth.sync.SynchronizerConfiguration.Builder;
import tech.pegasys.pantheon.ethereum.eth.sync.SynchronizerConfiguration;
import tech.pegasys.pantheon.util.BlockImporter;

import picocli.CommandLine.RunLast;
Expand All @@ -32,7 +32,7 @@ public static void main(final String... args) {
new BlockImporter(),
new RunnerBuilder(),
new PantheonControllerBuilder(),
new Builder());
new SynchronizerConfiguration.Builder());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I admit this change is completely unrelated to this PR but just changes which import we use to make things more readable.


pantheonCommand.parse(
new RunLast().andExit(SUCCESS_EXIT_CODE),
Expand Down