Print an error if we discover our own network identity (#6047)

* Add an error if we discover our own network identity

* Fix tests
This commit is contained in:
Pierre Krieger
2020-05-18 18:42:44 +02:00
committed by GitHub
parent d1bf24caef
commit 7536de97b1
4 changed files with 28 additions and 4 deletions
@@ -109,6 +109,9 @@ use wasm_timer::Instant;
/// tries to connect, the connection is accepted. A ban only delays dialing attempts.
///
pub struct GenericProto {
/// `PeerId` of the local node.
local_peer_id: PeerId,
/// Legacy protocol to open with peers. Never modified.
legacy_protocol: RegisteredProtocol,
@@ -321,6 +324,7 @@ impl GenericProto {
/// The `queue_size_report` is an optional Prometheus metric that can report the size of the
/// messages queue. If passed, it must have one label for the protocol name.
pub fn new(
local_peer_id: PeerId,
protocol: impl Into<ProtocolId>,
versions: &[u8],
peerset: sc_peerset::Peerset,
@@ -329,6 +333,7 @@ impl GenericProto {
let legacy_protocol = RegisteredProtocol::new(protocol, versions);
GenericProto {
local_peer_id,
legacy_protocol,
notif_protocols: Vec::new(),
peerset,
@@ -507,9 +512,18 @@ impl GenericProto {
///
/// Can be called multiple times with the same `PeerId`s.
pub fn add_discovered_nodes(&mut self, peer_ids: impl Iterator<Item = PeerId>) {
self.peerset.discovered(peer_ids.map(|peer_id| {
let local_peer_id = &self.local_peer_id;
self.peerset.discovered(peer_ids.filter_map(|peer_id| {
if peer_id == *local_peer_id {
error!(
target: "sub-libp2p",
"Discovered our own identity. This is a minor inconsequential bug."
);
return None;
}
debug!(target: "sub-libp2p", "PSM <= Discovered({:?})", peer_id);
peer_id
Some(peer_id)
}));
}
@@ -42,6 +42,7 @@ fn build_nodes() -> (Swarm<CustomProtoWithAddr>, Swarm<CustomProtoWithAddr>) {
for index in 0 .. 2 {
let keypair = keypairs[index].clone();
let local_peer_id = keypair.public().into_peer_id();
let transport = libp2p::core::transport::MemoryTransport
.and_then(move |out, endpoint| {
let secio = libp2p::secio::SecioConfig::new(keypair);
@@ -82,7 +83,7 @@ fn build_nodes() -> (Swarm<CustomProtoWithAddr>, Swarm<CustomProtoWithAddr>) {
});
let behaviour = CustomProtoWithAddr {
inner: GenericProto::new(&b"test"[..], &[1], peerset, None),
inner: GenericProto::new(local_peer_id, &b"test"[..], &[1], peerset, None),
addrs: addrs
.iter()
.enumerate()