From 95e3a39b4d0fe6de551fac61e9f88ce488e41477 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Mon, 19 Feb 2024 18:07:35 +0100 Subject: [PATCH] [networking-fixes] small refactoring in garage_net peering --- src/net/peering.rs | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/net/peering.rs b/src/net/peering.rs index 3f0b2279..61882a18 100644 --- a/src/net/peering.rs +++ b/src/net/peering.rs @@ -164,23 +164,22 @@ struct KnownHosts { impl KnownHosts { fn new() -> Self { let list = HashMap::new(); - let hash = Self::calculate_hash(&list); + let hash = Self::calculate_hash(vec![]); Self { list, hash } } fn update_hash(&mut self) { - self.hash = Self::calculate_hash(&self.list); + self.hash = Self::calculate_hash(self.connected_peers_vec()); } - fn map_into_vec(input: &HashMap) -> Vec<(NodeID, SocketAddr)> { - let mut list = Vec::with_capacity(input.len()); - for (id, peer) in input.iter() { - if peer.state == PeerConnState::Connected || peer.state == PeerConnState::Ourself { + fn connected_peers_vec(&self) -> Vec<(NodeID, SocketAddr)> { + let mut list = Vec::with_capacity(self.list.len()); + for (id, peer) in self.list.iter() { + if peer.state.is_up() { list.push((*id, peer.addr)); } } list } - fn calculate_hash(input: &HashMap) -> hash::Digest { - let mut list = Self::map_into_vec(input); + fn calculate_hash(mut list: Vec<(NodeID, SocketAddr)>) -> hash::Digest { list.sort(); let mut hash_state = hash::State::new(); for (id, addr) in list { @@ -231,6 +230,7 @@ impl PeeringManager { netapp.id, PeerInfoInternal::new(addr, PeerConnState::Ourself), ); + known_hosts.update_hash(); } // TODO for v0.10 / v1.0 : rename the endpoint (it will break compatibility) @@ -251,13 +251,11 @@ impl PeeringManager { let strat2 = strat.clone(); netapp.on_connected(move |id: NodeID, addr: SocketAddr, is_incoming: bool| { - let strat2 = strat2.clone(); strat2.on_connected(id, addr, is_incoming); }); let strat2 = strat.clone(); netapp.on_disconnected(move |id: NodeID, is_incoming: bool| { - let strat2 = strat2.clone(); strat2.on_disconnected(id, is_incoming); }); @@ -462,7 +460,7 @@ impl PeeringManager { } async fn exchange_peers(self: Arc, id: &NodeID) { - let peer_list = KnownHosts::map_into_vec(&self.known_hosts.read().unwrap().list); + let peer_list = self.known_hosts.read().unwrap().connected_peers_vec(); let pex_message = PeerListMessage { list: peer_list }; match self .peer_list_endpoint @@ -550,7 +548,7 @@ impl PeeringManager { } } - fn on_connected(self: Arc, id: NodeID, addr: SocketAddr, is_incoming: bool) { + fn on_connected(self: &Arc, id: NodeID, addr: SocketAddr, is_incoming: bool) { let mut known_hosts = self.known_hosts.write().unwrap(); if is_incoming { if let Some(host) = known_hosts.list.get_mut(&id) { @@ -578,7 +576,7 @@ impl PeeringManager { self.update_public_peer_list(&known_hosts); } - fn on_disconnected(self: Arc, id: NodeID, is_incoming: bool) { + fn on_disconnected(self: &Arc, id: NodeID, is_incoming: bool) { if !is_incoming { info!("Connection to {} was closed", hex::encode(&id[..8])); let mut known_hosts = self.known_hosts.write().unwrap(); @@ -620,7 +618,7 @@ impl EndpointHandler for PeeringManager { _from: NodeID, ) -> PeerListMessage { self.handle_peer_list(&peer_list.list[..]); - let peer_list = KnownHosts::map_into_vec(&self.known_hosts.read().unwrap().list); + let peer_list = self.known_hosts.read().unwrap().connected_peers_vec(); PeerListMessage { list: peer_list } } }