Kaynağa Gözat

net: flatten move_hosts() so unregister call happens outside function

this leads to cleaner code since depending on the use case we still do
different things with the HostState following move_host(). However it
does mean that unregister() has to be called manually in some cases.
draoi 2 yıl önce
ebeveyn
işleme
1f1bfd3dce

+ 1 - 1
bin/lilith/src/main.rs

@@ -186,7 +186,7 @@ impl Lilith {
 
             if !ping_node(url.clone(), p2p.clone()).await {
                 debug!(target: "lilith", "Host {} is not responsive. Downgrading from whitelist", url);
-                hosts.move_host(url, *last_seen, HostColor::Grey, false, None).await?;
+                hosts.move_host(url, *last_seen, HostColor::Grey, None).await?;
                 hosts.unregister(url).await;
 
                 continue

+ 1 - 1
src/net/channel.rs

@@ -320,7 +320,7 @@ impl Channel {
     pub async fn ban(&self, peer: &Url) {
         debug!(target: "net::channel::ban()", "START {:?}", self);
         let last_seen = UNIX_EPOCH.elapsed().unwrap().as_secs();
-        self.p2p().hosts().move_host(peer, last_seen, HostColor::Black, false, None).await.unwrap();
+        self.p2p().hosts().move_host(peer, last_seen, HostColor::Black, None).await.unwrap();
 
         self.stop().await;
         debug!(target: "net::channel::ban()", "STOP {:?}", self);

+ 2 - 1
src/net/hosts/refinery.rs

@@ -161,7 +161,8 @@ impl GreylistRefinery {
                     let last_seen = UNIX_EPOCH.elapsed().unwrap().as_secs();
 
                     // Add to the whitelist and remove from the greylist.
-                    hosts.move_host(url, last_seen, HostColor::White, false, None).await.unwrap();
+                    hosts.move_host(url, last_seen, HostColor::White, None).await.unwrap();
+                    hosts.unregister(url).await;
                 }
                 None => {
                     debug!(target: "net::refinery", "No matching greylist entries found. Cannot proceed with refinery");

+ 0 - 44
src/net/hosts/store.rs

@@ -1160,7 +1160,6 @@ impl Hosts {
         addr: &Url,
         last_seen: u64,
         destination: HostColor,
-        suspend: bool,
         channel: Option<ChannelPtr>,
     ) -> Result<()> {
         debug!(target: "net::hosts::move_host()", "Trying to move addr={} node={} destination={:?}",
@@ -1172,54 +1171,22 @@ impl Hosts {
         match destination {
             // Downgrade to grey. Remove from white and gold.
             HostColor::Grey => {
-                // Remove from the gold list if it exists.
                 self.container.remove_if_exists(HostColor::Gold, addr).await;
-
-                // Remove from the white list if it exists.
                 self.container.remove_if_exists(HostColor::White, addr).await;
-
-                // If it exists on the grey list, update its last seen field.
-                // Otherwise, write to the greylist.
                 self.container.store_or_update(HostColor::Grey, addr.clone(), last_seen).await;
-
-                if suspend {
-                    // We mark this peer as Suspend which means we do not try to connect to it until it
-                    // has passed through the refinery. This should never panic.
-                    self.try_register(addr.clone(), HostState::Suspend).await.unwrap();
-                    return Ok(());
-                } else {
-                    return Ok(());
-                }
             }
 
             // Remove from Greylist, add to Whitelist. Called by the Refinery.
             HostColor::White => {
-                // Remove from the grey list if it exists.
                 self.container.remove_if_exists(HostColor::Grey, addr).await;
-
-                // If it exists on the white list, update its last seen field.
-                // Otherwise, write to the white list.
                 self.container.store_or_update(HostColor::White, addr.clone(), last_seen).await;
             }
 
             // Upgrade to gold. Remove from white or grey.
             HostColor::Gold => {
-                // Remove from the grey list if it exists.
                 self.container.remove_if_exists(HostColor::Grey, addr).await;
-
-                // Remove from the white list if it exists.
                 self.container.remove_if_exists(HostColor::White, addr).await;
-
-                // If it exists on the gold list, update its last seen field.
-                // Otherwise, write to the gold list.
                 self.container.store_or_update(HostColor::Gold, addr.clone(), last_seen).await;
-
-                // Re-register this host as Connected. We want to keep track of all connected peers
-                // in the Connected() state.
-                self.try_register(addr.clone(), HostState::Connected(channel.unwrap()))
-                    .await
-                    .unwrap();
-                return Ok(());
             }
 
             // Move to black. Remove from all other lists.
@@ -1233,16 +1200,9 @@ impl Hosts {
                         return Ok(());
                     }
 
-                    // Remove from the grey list if it exists.
                     self.container.remove_if_exists(HostColor::Grey, addr).await;
-
-                    // Remove from the white list if it exists.
                     self.container.remove_if_exists(HostColor::White, addr).await;
-
-                    // Remove from the gold list if it exists.
                     self.container.remove_if_exists(HostColor::Gold, addr).await;
-
-                    // Add to the black list.
                     self.container.store_or_update(HostColor::Gold, addr.clone(), last_seen).await;
                 }
             }
@@ -1250,10 +1210,6 @@ impl Hosts {
             HostColor::Dark => return Err(Error::InvalidHostColor),
         }
 
-        // Remove this entry from HostRegistry to avoid this host getting
-        // stuck in the Moving state.
-        self.unregister(addr).await;
-
         Ok(())
     }
 }

+ 1 - 7
src/net/session/manual_session.rs

@@ -139,13 +139,7 @@ impl ManualSession {
                             // Add this connection to the anchorlist
                             self.p2p()
                                 .hosts()
-                                .move_host(
-                                    &addr,
-                                    last_seen,
-                                    HostColor::Gold,
-                                    false,
-                                    Some(channel.clone()),
-                                )
+                                .move_host(&addr, last_seen, HostColor::Gold, Some(channel.clone()))
                                 .await?;
 
                             // Wait for channel to close

+ 1 - 1
src/net/session/mod.rs

@@ -70,7 +70,7 @@ pub async fn remove_sub_on_stop(p2p: P2pPtr, channel: ChannelPtr, type_id: Sessi
 
         if !p2p.hosts().is_connection_to_self(addr).await {
             let last_seen = p2p.hosts().fetch_last_seen(addr).await.unwrap();
-            p2p.hosts().move_host(addr, last_seen, HostColor::Grey, false, None).await.unwrap();
+            p2p.hosts().move_host(addr, last_seen, HostColor::Grey, None).await.unwrap();
         }
     }
 

+ 7 - 5
src/net/session/outbound_session.rs

@@ -44,7 +44,7 @@ use super::{
         channel::ChannelPtr,
         connector::Connector,
         dnet::{self, dnetev, DnetEvent},
-        hosts::store::HostColor,
+        hosts::store::{HostColor, HostState},
         message::GetAddrsMessage,
         p2p::{P2p, P2pPtr},
     },
@@ -401,7 +401,7 @@ impl Slot {
 
             // Add this connection to the anchorlist
             hosts
-                .move_host(&addr, last_seen, HostColor::Gold, false, Some(channel.clone()))
+                .move_host(&addr, last_seen, HostColor::Gold, Some(channel.clone()))
                 .await
                 .unwrap();
 
@@ -433,9 +433,11 @@ impl Slot {
                     self.slot, addr, e
                 );
 
-                // At this point we failed to connect. We'll downgrade this peer and
-                // mark its state as Suspend, which sends it to the Refinery for processing.
-                self.p2p().hosts().move_host(&addr, last_seen, HostColor::Grey, true, None).await?;
+                // At this point we failed to connect. We'll downgrade this peer now.
+                self.p2p().hosts().move_host(&addr, last_seen, HostColor::Grey, None).await?;
+
+                // Mark its state as Suspend, which sends it to the Refinery for processing.
+                self.p2p().hosts().try_register(addr.clone(), HostState::Suspend).await.unwrap();
 
                 // Notify that channel processing failed
                 self.p2p().hosts().channel_subscriber.notify(Err(Error::ConnectFailed)).await;