Decode devp2p packets off the event thread (#1439)

Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
This commit is contained in:
Adrian Sutton
2019-05-14 06:17:06 +10:00
committed by GitHub
parent 16982ebad2
commit be1fa19a16

View File

@@ -26,7 +26,6 @@ import tech.pegasys.pantheon.ethereum.permissioning.node.NodePermissioningContro
import tech.pegasys.pantheon.metrics.MetricCategory;
import tech.pegasys.pantheon.metrics.MetricsSystem;
import tech.pegasys.pantheon.util.NetworkUtility;
import tech.pegasys.pantheon.util.Preconditions;
import java.io.IOException;
import java.net.BindException;
@@ -197,26 +196,34 @@ public class VertxPeerDiscoveryAgent extends PeerDiscoveryAgent {
* @param datagram the received datagram.
*/
private void handlePacket(final DatagramPacket datagram) {
try {
final int length = datagram.data().length();
Preconditions.checkGuard(
validatePacketSize(length),
PeerDiscoveryPacketDecodingException::new,
"Packet too large. Actual size (bytes): %s",
length);
// We allow exceptions to bubble up, as they'll be picked up by the exception handler.
final Packet packet = Packet.decode(datagram.data());
// Acquire the senders coordinates to build a Peer representation from them.
final String host = datagram.sender().host();
final int port = datagram.sender().port();
final Endpoint endpoint = new Endpoint(host, port, OptionalInt.empty());
handleIncomingPacket(endpoint, packet);
} catch (final PeerDiscoveryPacketDecodingException e) {
LOG.debug("Discarding invalid peer discovery packet: {}", e.getMessage());
} catch (final Throwable t) {
LOG.error("Encountered error while handling packet", t);
final int length = datagram.data().length();
if (!validatePacketSize(length)) {
LOG.debug("Discarding over-sized packet. Actual size (bytes): " + length);
return;
}
vertx.<Packet>executeBlocking(
future -> {
try {
future.complete(Packet.decode(datagram.data()));
} catch (final Throwable t) {
future.fail(t);
}
},
event -> {
if (event.succeeded()) {
// Acquire the senders coordinates to build a Peer representation from them.
final String host = datagram.sender().host();
final int port = datagram.sender().port();
final Endpoint endpoint = new Endpoint(host, port, OptionalInt.empty());
handleIncomingPacket(endpoint, event.result());
} else {
if (event.cause() instanceof PeerDiscoveryPacketDecodingException) {
LOG.debug("Discarding invalid peer discovery packet: {}", event.cause().getMessage());
} else {
LOG.error("Encountered error while handling packet", event.cause());
}
}
});
}
private class VertxAsyncExecutor implements AsyncExecutor {