|
|
@ -17,6 +17,10 @@ import tech.pegasys.pantheon.ethereum.p2p.api.ProtocolManager; |
|
|
|
import tech.pegasys.pantheon.ethereum.p2p.wire.Capability; |
|
|
|
import tech.pegasys.pantheon.ethereum.p2p.wire.Capability; |
|
|
|
import tech.pegasys.pantheon.ethereum.p2p.wire.SubProtocol; |
|
|
|
import tech.pegasys.pantheon.ethereum.p2p.wire.SubProtocol; |
|
|
|
import tech.pegasys.pantheon.ethereum.p2p.wire.messages.DisconnectMessage.DisconnectReason; |
|
|
|
import tech.pegasys.pantheon.ethereum.p2p.wire.messages.DisconnectMessage.DisconnectReason; |
|
|
|
|
|
|
|
import tech.pegasys.pantheon.metrics.Counter; |
|
|
|
|
|
|
|
import tech.pegasys.pantheon.metrics.LabelledMetric; |
|
|
|
|
|
|
|
import tech.pegasys.pantheon.metrics.MetricCategory; |
|
|
|
|
|
|
|
import tech.pegasys.pantheon.metrics.MetricsSystem; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Arrays; |
|
|
|
import java.util.Arrays; |
|
|
@ -50,14 +54,24 @@ public class NetworkRunner implements AutoCloseable { |
|
|
|
private final P2PNetwork network; |
|
|
|
private final P2PNetwork network; |
|
|
|
private final Map<String, SubProtocol> subProtocols; |
|
|
|
private final Map<String, SubProtocol> subProtocols; |
|
|
|
private final List<ProtocolManager> protocolManagers; |
|
|
|
private final List<ProtocolManager> protocolManagers; |
|
|
|
|
|
|
|
private final LabelledMetric<Counter> inboundMessageCounter; |
|
|
|
|
|
|
|
|
|
|
|
private NetworkRunner( |
|
|
|
private NetworkRunner( |
|
|
|
final P2PNetwork network, |
|
|
|
final P2PNetwork network, |
|
|
|
final Map<String, SubProtocol> subProtocols, |
|
|
|
final Map<String, SubProtocol> subProtocols, |
|
|
|
final List<ProtocolManager> protocolManagers) { |
|
|
|
final List<ProtocolManager> protocolManagers, |
|
|
|
|
|
|
|
final MetricsSystem metricsSystem) { |
|
|
|
this.network = network; |
|
|
|
this.network = network; |
|
|
|
this.protocolManagers = protocolManagers; |
|
|
|
this.protocolManagers = protocolManagers; |
|
|
|
this.subProtocols = subProtocols; |
|
|
|
this.subProtocols = subProtocols; |
|
|
|
|
|
|
|
inboundMessageCounter = |
|
|
|
|
|
|
|
metricsSystem.createLabelledCounter( |
|
|
|
|
|
|
|
MetricCategory.NETWORK, |
|
|
|
|
|
|
|
"p2p_messages_inbound", |
|
|
|
|
|
|
|
"Count of each P2P message received inbound.", |
|
|
|
|
|
|
|
"protocol", |
|
|
|
|
|
|
|
"name", |
|
|
|
|
|
|
|
"code"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public P2PNetwork getNetwork() { |
|
|
|
public P2PNetwork getNetwork() { |
|
|
@ -116,6 +130,7 @@ public class NetworkRunner implements AutoCloseable { |
|
|
|
message -> { |
|
|
|
message -> { |
|
|
|
final int code = message.getData().getCode(); |
|
|
|
final int code = message.getData().getCode(); |
|
|
|
if (!protocol.isValidMessageCode(cap.getVersion(), code)) { |
|
|
|
if (!protocol.isValidMessageCode(cap.getVersion(), code)) { |
|
|
|
|
|
|
|
inboundMessageCounter.labels(cap.toString(), "Invalid", "").inc(); |
|
|
|
// Handle invalid messages by disconnecting
|
|
|
|
// Handle invalid messages by disconnecting
|
|
|
|
LOG.debug( |
|
|
|
LOG.debug( |
|
|
|
"Invalid message code ({}-{}, {}) received from peer, disconnecting from:", |
|
|
|
"Invalid message code ({}-{}, {}) received from peer, disconnecting from:", |
|
|
@ -126,6 +141,12 @@ public class NetworkRunner implements AutoCloseable { |
|
|
|
message.getConnection().disconnect(DisconnectReason.BREACH_OF_PROTOCOL); |
|
|
|
message.getConnection().disconnect(DisconnectReason.BREACH_OF_PROTOCOL); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
inboundMessageCounter |
|
|
|
|
|
|
|
.labels( |
|
|
|
|
|
|
|
cap.toString(), |
|
|
|
|
|
|
|
protocol.messageName(cap.getVersion(), code), |
|
|
|
|
|
|
|
Integer.toString(code)) |
|
|
|
|
|
|
|
.inc(); |
|
|
|
protocolManager.processMessage(cap, message); |
|
|
|
protocolManager.processMessage(cap, message); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
@ -162,6 +183,7 @@ public class NetworkRunner implements AutoCloseable { |
|
|
|
private Function<List<Capability>, P2PNetwork> networkProvider; |
|
|
|
private Function<List<Capability>, P2PNetwork> networkProvider; |
|
|
|
List<ProtocolManager> protocolManagers = new ArrayList<>(); |
|
|
|
List<ProtocolManager> protocolManagers = new ArrayList<>(); |
|
|
|
List<SubProtocol> subProtocols = new ArrayList<>(); |
|
|
|
List<SubProtocol> subProtocols = new ArrayList<>(); |
|
|
|
|
|
|
|
MetricsSystem metricsSystem; |
|
|
|
|
|
|
|
|
|
|
|
public NetworkRunner build() { |
|
|
|
public NetworkRunner build() { |
|
|
|
final Map<String, SubProtocol> subProtocolMap = new HashMap<>(); |
|
|
|
final Map<String, SubProtocol> subProtocolMap = new HashMap<>(); |
|
|
@ -180,7 +202,7 @@ public class NetworkRunner implements AutoCloseable { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
final P2PNetwork network = networkProvider.apply(caps); |
|
|
|
final P2PNetwork network = networkProvider.apply(caps); |
|
|
|
return new NetworkRunner(network, subProtocolMap, protocolManagers); |
|
|
|
return new NetworkRunner(network, subProtocolMap, protocolManagers, metricsSystem); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public Builder protocolManagers(final List<ProtocolManager> protocolManagers) { |
|
|
|
public Builder protocolManagers(final List<ProtocolManager> protocolManagers) { |
|
|
@ -202,5 +224,10 @@ public class NetworkRunner implements AutoCloseable { |
|
|
|
this.subProtocols.addAll(subProtocols); |
|
|
|
this.subProtocols.addAll(subProtocols); |
|
|
|
return this; |
|
|
|
return this; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Builder metricsSystem(final MetricsSystem metricsSystem) { |
|
|
|
|
|
|
|
this.metricsSystem = metricsSystem; |
|
|
|
|
|
|
|
return this; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|