Skip to content

Commit

Permalink
make obvious when a breach of protocol is logged, add peer in some pl…
Browse files Browse the repository at this point in the history
…aces (hyperledger#4268)

Signed-off-by: Stefan <stefan.pingel@consensys.net>
Co-authored-by: Sally MacFarlane <sally.macfarlane@consensys.net>
  • Loading branch information
2 people authored and freemanzMrojo committed Aug 19, 2022
1 parent d0283f4 commit 93cc7b7
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public void processMessage(final Capability cap, final Message message) {
} else if (!ethPeer.statusHasBeenReceived()) {
// Peers are required to send status messages before any other message type
LOG.debug(
"{} requires a Status ({}) message to be sent first. Instead, received message {}. Disconnecting from {}.",
"{} requires a Status ({}) message to be sent first. Instead, received message {} (BREACH_OF_PROTOCOL). Disconnecting from {}.",
this.getClass().getSimpleName(),
EthPV62.STATUS,
code,
Expand All @@ -277,7 +277,9 @@ public void processMessage(final Capability cap, final Message message) {
final EthMessage ethMessage = new EthMessage(ethPeer, messageData);

if (!ethPeer.validateReceivedMessage(ethMessage, getSupportedProtocol())) {
LOG.debug("Unsolicited message received, disconnecting from EthPeer: {}", ethPeer);
LOG.debug(
"Unsolicited message received (BREACH_OF_PROTOCOL), disconnecting from EthPeer: {}",
ethPeer);
ethPeer.disconnect(DisconnectReason.BREACH_OF_PROTOCOL);
return;
}
Expand Down Expand Up @@ -308,7 +310,10 @@ public void processMessage(final Capability cap, final Message message) {
}
} catch (final RLPException e) {
LOG.debug(
"Received malformed message {} , disconnecting: {}", messageData.getData(), ethPeer, e);
"Received malformed message {} (BREACH_OF_PROTOCOL), disconnecting: {}",
messageData.getData(),
ethPeer,
e);

ethPeer.disconnect(DisconnectMessage.DisconnectReason.BREACH_OF_PROTOCOL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RequestManager {

private static final Logger LOG = LoggerFactory.getLogger(RequestManager.class);
private final AtomicLong requestIdCounter = new AtomicLong(0);
private final Map<BigInteger, ResponseStream> responseStreams = new ConcurrentHashMap<>();
private final EthPeer peer;
Expand Down Expand Up @@ -73,7 +78,10 @@ public void dispatchResponse(final EthMessage ethMessage) {
.ifPresentOrElse(
responseStream -> responseStream.processMessage(requestIdAndEthMessage.getValue()),
// disconnect on incorrect requestIds
() -> peer.disconnect(DisconnectMessage.DisconnectReason.BREACH_OF_PROTOCOL));
() -> {
LOG.debug("Request ID incorrect (BREACH_OF_PROTOCOL), disconnecting peer {}", peer);
peer.disconnect(DisconnectMessage.DisconnectReason.BREACH_OF_PROTOCOL);
});
} else {
// otherwise iterate through all of them
streams.forEach(stream -> stream.processMessage(ethMessage.getData()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ protected Optional<List<BlockHeader>> processResponse(
final BlockHeader child = reverse ? prevBlockHeader : header;
if (!parent.getHash().equals(child.getParentHash())) {
LOG.debug(
"Sequential headers must form a chain through hashes, disconnecting peer: {}", peer);
"Sequential headers must form a chain through hashes (BREACH_OF_PROTOCOL), disconnecting peer: {}",
peer);
peer.disconnect(DisconnectMessage.DisconnectReason.BREACH_OF_PROTOCOL);
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ private void handleNewBlockFromNetwork(final EthMessage message) {
importOrSavePendingBlock(block, message.getPeer().nodeId());
} catch (final RLPException e) {
LOG.debug(
"Malformed NEW_BLOCK message received from peer, disconnecting: {}",
"Malformed NEW_BLOCK message received from peer (BREACH_OF_PROTOCOL), disconnecting: {}",
message.getPeer(),
e);
message.getPeer().disconnect(DisconnectReason.BREACH_OF_PROTOCOL);
Expand Down Expand Up @@ -359,7 +359,7 @@ private void handleNewBlockHashesFromNetwork(final EthMessage message) {
}
} catch (final RLPException e) {
LOG.debug(
"Malformed NEW_BLOCK_HASHES message received from peer, disconnecting: {}",
"Malformed NEW_BLOCK_HASHES message received from peer (BREACH_OF_PROTOCOL), disconnecting: {}",
message.getPeer(),
e);
message.getPeer().disconnect(DisconnectReason.BREACH_OF_PROTOCOL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private CompletionStage<Void> handleFailedDownload(final Throwable error) {
pipelineErrorCounter.inc();
if (ExceptionUtils.rootCause(error) instanceof InvalidBlockException) {
LOG.warn(
"Invalid block detected. Disconnecting from sync target. {}",
"Invalid block detected (BREACH_OF_PROTOCOL). Disconnecting from sync target. {}",
ExceptionUtils.rootCause(error).getMessage());
syncState.disconnectSyncTarget(DisconnectReason.BREACH_OF_PROTOCOL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,10 @@ private CompletableFuture<List<BlockHeader>> processHeaders(
if (error == null && blockPeerTaskResult.getResult() != null) {
badBlockManager.addBadBlock(blockPeerTaskResult.getResult());
}
headersResult.getPeer().disconnect(DisconnectReason.BREACH_OF_PROTOCOL);
LOG.debug(
"Received invalid headers from peer, disconnecting from: {}",
"Received invalid headers from peer (BREACH_OF_PROTOCOL), disconnecting from: {}",
headersResult.getPeer());
headersResult.getPeer().disconnect(DisconnectReason.BREACH_OF_PROTOCOL);
future.completeExceptionally(
new InvalidBlockException(
"Header failed validation.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ private void processNewPooledTransactionHashesMessage(
} catch (final RLPException ex) {
if (peer != null) {
LOG.debug(
"Malformed pooled transaction hashes message received, disconnecting: {}", peer, ex);
"Malformed pooled transaction hashes message received (BREACH_OF_PROTOCOL), disconnecting: {}",
peer,
ex);
peer.disconnect(DisconnectReason.BREACH_OF_PROTOCOL);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ private void processTransactionsMessage(

} catch (final RLPException ex) {
if (peer != null) {
LOG.debug("Malformed transaction message received, disconnecting: {}", peer, ex);
LOG.debug(
"Malformed transaction message received (BREACH_OF_PROTOCOL), disconnecting: {}",
peer,
ex);
peer.disconnect(DisconnectReason.BREACH_OF_PROTOCOL);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final L
} else {
// Unexpected message - disconnect
LOG.debug(
"Message received before HELLO's exchanged, disconnecting. Peer: {}, Code: {}, Data: {}",
"Message received before HELLO's exchanged (BREACH_OF_PROTOCOL), disconnecting. Peer: {}, Code: {}, Data: {}",
expectedPeer.map(Peer::getEnodeURLString).orElse("unknown"),
message.getCode(),
message.getData().toString());
Expand Down Expand Up @@ -227,7 +227,7 @@ public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable thr
if (cause instanceof FramingException
|| cause instanceof RLPException
|| cause instanceof IllegalArgumentException) {
LOG.debug("Invalid incoming message", throwable);
LOG.debug("Invalid incoming message (BREACH_OF_PROTOCOL)", throwable);
if (connectFuture.isDone() && !connectFuture.isCompletedExceptionally()) {
connectFuture.get().disconnect(DisconnectMessage.DisconnectReason.BREACH_OF_PROTOCOL);
return;
Expand Down

0 comments on commit 93cc7b7

Please sign in to comment.