Browse Source

net/hosts: looksmax move_hosts()

All code must be beautiful
darkfi 1 year ago
parent
commit
0ad773fc94
1 changed files with 46 additions and 55 deletions
  1. 46 55
      src/net/hosts.rs

+ 46 - 55
src/net/hosts.rs

@@ -1426,70 +1426,61 @@ impl Hosts {
         debug!(target: "net::hosts::move_host()", "Trying to move addr={} destination={:?}",
         debug!(target: "net::hosts::move_host()", "Trying to move addr={} destination={:?}",
                addr, destination);
                addr, destination);
 
 
-        match self.try_register(addr.clone(), HostState::Move) {
-            Ok(new_state) => {
-                debug!(target: "net::hosts::move_host()", "Moving addr={} destination={:?}, state={:?}",
-                       addr, destination, new_state);
-
-                match destination {
-                    // Downgrade to grey. Remove from white and gold.
-                    HostColor::Grey => {
-                        self.container.remove_if_exists(HostColor::Gold, addr);
-                        self.container.remove_if_exists(HostColor::White, addr);
-
-                        self.container.store_or_update(HostColor::Grey, addr.clone(), last_seen);
-                        self.container.sort_by_last_seen(HostColor::Grey as usize);
-                        self.container.resize(HostColor::Grey);
-                    }
+        // If we cannot register this address as move, this will simply return here.
+        self.try_register(addr.clone(), HostState::Move)?;
 
 
-                    // Remove from Greylist, add to Whitelist. Called by the Refinery.
-                    HostColor::White => {
-                        self.container.remove_if_exists(HostColor::Grey, addr);
+        debug!(target: "net::hosts::move_host()", "Moving addr={} destination={:?}",
+            addr.clone(), destination);
 
 
-                        self.container.store_or_update(HostColor::White, addr.clone(), last_seen);
-                        self.container.sort_by_last_seen(HostColor::White as usize);
-                        self.container.resize(HostColor::White);
-                    }
+        match destination {
+            // Downgrade to grey. Remove from white and gold.
+            HostColor::Grey => {
+                self.container.remove_if_exists(HostColor::Gold, addr);
+                self.container.remove_if_exists(HostColor::White, addr);
+
+                self.container.store_or_update(HostColor::Grey, addr.clone(), last_seen);
+                self.container.sort_by_last_seen(HostColor::Grey as usize);
+                self.container.resize(HostColor::Grey);
+            }
 
 
-                    // Upgrade to gold. Remove from white or grey.
-                    HostColor::Gold => {
-                        self.container.remove_if_exists(HostColor::Grey, addr);
-                        self.container.remove_if_exists(HostColor::White, addr);
+            // Remove from Greylist, add to Whitelist. Called by the Refinery.
+            HostColor::White => {
+                self.container.remove_if_exists(HostColor::Grey, addr);
 
 
-                        self.container.store_or_update(HostColor::Gold, addr.clone(), last_seen);
-                        self.container.sort_by_last_seen(HostColor::Gold as usize);
-                    }
+                self.container.store_or_update(HostColor::White, addr.clone(), last_seen);
+                self.container.sort_by_last_seen(HostColor::White as usize);
+                self.container.resize(HostColor::White);
+            }
 
 
-                    // Move to black. Remove from all other lists.
-                    HostColor::Black => {
-                        // We ignore UNIX sockets here so we will just work
-                        // with stuff that has host_str().
-                        if addr.host_str().is_some() {
-                            // Localhost connections should never enter the blacklist
-                            // This however allows any Tor and Nym connections.
-                            if self.is_local_host(addr) {
-                                return Ok(());
-                            }
-
-                            self.container.remove_if_exists(HostColor::Grey, addr);
-                            self.container.remove_if_exists(HostColor::White, addr);
-                            self.container.remove_if_exists(HostColor::Gold, addr);
-
-                            self.container.store_or_update(
-                                HostColor::Black,
-                                addr.clone(),
-                                last_seen,
-                            );
-                        }
+            // Upgrade to gold. Remove from white or grey.
+            HostColor::Gold => {
+                self.container.remove_if_exists(HostColor::Grey, addr);
+                self.container.remove_if_exists(HostColor::White, addr);
+
+                self.container.store_or_update(HostColor::Gold, addr.clone(), last_seen);
+                self.container.sort_by_last_seen(HostColor::Gold as usize);
+            }
+
+            // Move to black. Remove from all other lists.
+            HostColor::Black => {
+                // We ignore UNIX sockets here so we will just work
+                // with stuff that has host_str().
+                if addr.host_str().is_some() {
+                    // Localhost connections should never enter the blacklist
+                    // This however allows any Tor and Nym connections.
+                    if self.is_local_host(addr) {
+                        return Ok(());
                     }
                     }
 
 
-                    HostColor::Dark => return Err(Error::InvalidHostColor),
+                    self.container.remove_if_exists(HostColor::Grey, addr);
+                    self.container.remove_if_exists(HostColor::White, addr);
+                    self.container.remove_if_exists(HostColor::Gold, addr);
+
+                    self.container.store_or_update(HostColor::Black, addr.clone(), last_seen);
                 }
                 }
             }
             }
-            Err(e) => {
-                warn!(target: "net::hosts::move_host", "Cannot move host={:?}, err={:?}",
-                    addr.clone(), e);
-            }
+
+            HostColor::Dark => return Err(Error::InvalidHostColor),
         }
         }
 
 
         Ok(())
         Ok(())