From 6fb48b45fae1a767596d7ce0f64eb68639f1636a Mon Sep 17 00:00:00 2001 From: Age Manning Date: Tue, 15 Jun 2021 14:40:43 +1000 Subject: [PATCH] Discovery patch (#2382) * Upgrade libp2p and unstable gossip * Network protocol upgrades * Correct dependencies, reduce incoming bucket limit * Clean up dirty DHT entries before repopulating * Update cargo lock * Update lockfile * Update ENR dep * Update deps to specific versions * Update test dependencies * Update docker rust, and remote signer tests * More remote signer test fixes * Temp commit * Update discovery * Remove cached enrs after dialing * Increase the session capacity, for improved efficiency --- beacon_node/eth2_libp2p/Cargo.toml | 2 +- beacon_node/eth2_libp2p/src/config.rs | 2 +- beacon_node/eth2_libp2p/src/discovery/mod.rs | 12 +++++++++++- beacon_node/eth2_libp2p/src/peer_manager/mod.rs | 7 +++---- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/beacon_node/eth2_libp2p/Cargo.toml b/beacon_node/eth2_libp2p/Cargo.toml index 4411517c928..bfa48d078fc 100644 --- a/beacon_node/eth2_libp2p/Cargo.toml +++ b/beacon_node/eth2_libp2p/Cargo.toml @@ -5,7 +5,7 @@ authors = ["Sigma Prime "] edition = "2018" [dependencies] -discv5 = { version = "0.1.0-beta.4", features = ["libp2p"] } +discv5 = { version = "0.1.0-beta.5", features = ["libp2p"] } unsigned-varint = { version = "0.6.0", features = ["codec"] } types = { path = "../../consensus/types" } hashset_delay = { path = "../../common/hashset_delay" } diff --git a/beacon_node/eth2_libp2p/src/config.rs b/beacon_node/eth2_libp2p/src/config.rs index c215931187d..a4c10bb650b 100644 --- a/beacon_node/eth2_libp2p/src/config.rs +++ b/beacon_node/eth2_libp2p/src/config.rs @@ -154,7 +154,7 @@ impl Default for Config { // discv5 configuration let discv5_config = Discv5ConfigBuilder::new() .enable_packet_filter() - .session_cache_capacity(1000) + .session_cache_capacity(5000) .request_timeout(Duration::from_secs(1)) .query_peer_timeout(Duration::from_secs(2)) .query_timeout(Duration::from_secs(30)) diff --git a/beacon_node/eth2_libp2p/src/discovery/mod.rs b/beacon_node/eth2_libp2p/src/discovery/mod.rs index 5f043d87776..4d1314acdab 100644 --- a/beacon_node/eth2_libp2p/src/discovery/mod.rs +++ b/beacon_node/eth2_libp2p/src/discovery/mod.rs @@ -295,6 +295,11 @@ impl Discovery { self.cached_enrs.iter() } + /// Removes a cached ENR from the list. + pub fn remove_cached_enr(&mut self, peer_id: &PeerId) -> Option { + self.cached_enrs.pop(peer_id) + } + /// This adds a new `FindPeers` query to the queue if one doesn't already exist. pub fn discover_peers(&mut self) { // If the discv5 service isn't running or we are in the process of a query, don't bother queuing a new one. @@ -502,6 +507,7 @@ impl Discovery { } } + /// Unbans the peer in discovery. pub fn unban_peer(&mut self, peer_id: &PeerId, ip_addresses: Vec) { // first try and convert the peer_id to a node_id. if let Ok(node_id) = peer_id_to_node_id(peer_id) { @@ -514,11 +520,15 @@ impl Discovery { } } - // mark node as disconnected in DHT, freeing up space for other nodes + /// Marks node as disconnected in the DHT, freeing up space for other nodes, this also removes + /// nodes from the cached ENR list. pub fn disconnect_peer(&mut self, peer_id: &PeerId) { if let Ok(node_id) = peer_id_to_node_id(peer_id) { self.discv5.disconnect_node(&node_id); } + // Remove the peer from the cached list, to prevent redialing disconnected + // peers. + self.cached_enrs.pop(peer_id); } /* Internal Functions */ diff --git a/beacon_node/eth2_libp2p/src/peer_manager/mod.rs b/beacon_node/eth2_libp2p/src/peer_manager/mod.rs index 5f87015a0ed..591e837ad51 100644 --- a/beacon_node/eth2_libp2p/src/peer_manager/mod.rs +++ b/beacon_node/eth2_libp2p/src/peer_manager/mod.rs @@ -580,10 +580,7 @@ impl PeerManager { // ENR's may have multiple Multiaddrs. The multi-addr associated with the UDP // port is removed, which is assumed to be associated with the discv5 protocol (and // therefore irrelevant for other libp2p components). - let mut out_list = enr.multiaddr(); - out_list.retain(|addr| !addr.iter().any(|v| matches!(v, MProtocol::Udp(_)))); - - out_list + enr.multiaddr_tcp() } else { // PeerId is not known Vec::new() @@ -674,6 +671,8 @@ impl PeerManager { .collect(); for peer_id in &peers_to_dial { debug!(self.log, "Dialing cached ENR peer"; "peer_id" => %peer_id); + // Remove the ENR from the cache to prevent continual re-dialing on disconnects + self.discovery.remove_cached_enr(&peer_id); self.dial_peer(peer_id); } }