From 91d7d81f33fd4fb4567a1438ef13ee71400a059e Mon Sep 17 00:00:00 2001 From: MSNTCS Date: Fri, 4 Sep 2020 18:53:20 +0900 Subject: [PATCH 1/2] Fix duplicate connection bugs --- network/src/p2p/handler.rs | 32 ++++++++++++++++++-------------- sync/src/block/extension.rs | 23 ++++++++++++++--------- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/network/src/p2p/handler.rs b/network/src/p2p/handler.rs index 5ac800c6b1..2168c010d4 100644 --- a/network/src/p2p/handler.rs +++ b/network/src/p2p/handler.rs @@ -516,13 +516,15 @@ impl IoHandler for Handler { remote_node_id, token ); - assert_eq!( - None, - self.remote_node_ids_reverse.write().insert(remote_node_id, token), - "{}:{} is already registered", - remote_node_id, - token - ); + if !self.remote_node_ids_reverse.write().contains_key(&remote_node_id) { + assert_eq!( + None, + self.remote_node_ids_reverse.write().insert(remote_node_id, token), + "{}:{} is already registered", + remote_node_id, + token + ); + } let t = inbound_connections.insert(token, connection); assert!(t.is_none()); @@ -546,13 +548,15 @@ impl IoHandler for Handler { remote_node_id, token ); - assert_eq!( - None, - self.remote_node_ids_reverse.write().insert(remote_node_id, token), - "{}:{} is already registered", - remote_node_id, - token - ); + if !self.remote_node_ids_reverse.write().contains_key(&remote_node_id) { + assert_eq!( + None, + self.remote_node_ids_reverse.write().insert(remote_node_id, token), + "{}:{} is already registered", + remote_node_id, + token + ); + } let mut network_message_size = 0; for (name, versions) in self.client.extension_versions() { diff --git a/sync/src/block/extension.rs b/sync/src/block/extension.rs index 3829ccb9c1..5ae82e2ac7 100644 --- a/sync/src/block/extension.rs +++ b/sync/src/block/extension.rs @@ -477,8 +477,10 @@ impl NetworkExtension for Extension { cinfo!(SYNC, "New peer detected #{}", id); self.send_status(id); - let t = self.connected_nodes.insert(*id); - debug_assert!(t, "{} is already added to peer list", id); + if !self.connected_nodes.contains(id) { + let t = self.connected_nodes.insert(*id); + debug_assert!(t, "{} is already added to peer list", id); + } let token = self.token_generator.gen().expect("Token generator is full"); let token_info = TokenInfo { @@ -486,13 +488,16 @@ impl NetworkExtension for Extension { request_id: None, }; - let t = self.requests.insert(*id, Vec::new()); - debug_assert_eq!(None, t); - let t = self.tokens_info.insert(token, token_info); - debug_assert_eq!(None, t); - let t = self.tokens.insert(*id, token); - debug_assert_eq!(None, t); - debug_assert!(t.is_none()); + if !self.requests.contains_key(id) { + let t = self.requests.insert(*id, Vec::new()); + debug_assert_eq!(None, t); + } + self.tokens_info.entry(token).or_insert(token_info); + if !self.tokens.contains_key(id) { + let t = self.tokens.insert(*id, token); + debug_assert_eq!(None, t); + debug_assert!(t.is_none()); + } } fn on_node_removed(&mut self, id: &NodeId) { From 5c9a46b2a798f016c3e2554e0493aaef0b8ce2be Mon Sep 17 00:00:00 2001 From: Park Juhyung Date: Mon, 21 Dec 2020 13:34:37 +0900 Subject: [PATCH 2/2] Add hotfix comments --- network/src/p2p/handler.rs | 4 ++++ sync/src/block/extension.rs | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/network/src/p2p/handler.rs b/network/src/p2p/handler.rs index 2168c010d4..a25e8a0d09 100644 --- a/network/src/p2p/handler.rs +++ b/network/src/p2p/handler.rs @@ -516,6 +516,8 @@ impl IoHandler for Handler { remote_node_id, token ); + // FIXME the if condition below is a hotfix. + // `self.remote_node_ids_reverse.write().contains_key(&remote_node_id)` always should be false if !self.remote_node_ids_reverse.write().contains_key(&remote_node_id) { assert_eq!( None, @@ -548,6 +550,8 @@ impl IoHandler for Handler { remote_node_id, token ); + // FIXME the if condition below is a hotfix. + // `self.remote_node_ids_reverse.write().contains_key(&remote_node_id)` always should be false if !self.remote_node_ids_reverse.write().contains_key(&remote_node_id) { assert_eq!( None, diff --git a/sync/src/block/extension.rs b/sync/src/block/extension.rs index 5ae82e2ac7..5090ac4218 100644 --- a/sync/src/block/extension.rs +++ b/sync/src/block/extension.rs @@ -477,6 +477,8 @@ impl NetworkExtension for Extension { cinfo!(SYNC, "New peer detected #{}", id); self.send_status(id); + // FIXME the if condition below is a hotfix. + // `self.connected_nodes.contains(id)` always should be false if !self.connected_nodes.contains(id) { let t = self.connected_nodes.insert(*id); debug_assert!(t, "{} is already added to peer list", id); @@ -488,6 +490,8 @@ impl NetworkExtension for Extension { request_id: None, }; + // FIXME the if condition below is a hotfix. + // `self.requests.contains(id)` always should be false if !self.requests.contains_key(id) { let t = self.requests.insert(*id, Vec::new()); debug_assert_eq!(None, t);