Explorar el Código

net: use standardized methods for net and lilith refinery's

draoi hace 2 años
padre
commit
3acf49e6a0
Se han modificado 3 ficheros con 15 adiciones y 14 borrados
  1. 2 5
      bin/lilith/src/main.rs
  2. 10 0
      src/net/hosts.rs
  3. 3 9
      src/net/session/refine_session.rs

+ 2 - 5
bin/lilith/src/main.rs

@@ -211,11 +211,8 @@ impl Lilith {
 
                     // This node is active. Update the last seen field.
                     let last_seen = UNIX_EPOCH.elapsed().unwrap().as_secs();
-                    hosts.container.update_last_seen(
-                        HostColor::White as usize,
-                        url.clone(),
-                        last_seen,
-                    );
+
+                    hosts.whitelist_host(url, last_seen)?;
                 }
                 None => {
                     debug!(target: "net::refinery::whitelist_refinery",

+ 10 - 0
src/net/hosts.rs

@@ -1226,6 +1226,16 @@ impl Hosts {
         Ok(())
     }
 
+    pub fn whitelist_host(&self, addr: &Url, last_seen: u64) -> Result<()> {
+        debug!(target: "net::hosts:whitelist_host()", "Upgrading addr={}", addr);
+        self.move_host(addr, last_seen, HostColor::White)?;
+
+        // Free up this addr for future operations.
+        self.unregister(addr);
+
+        Ok(())
+    }
+
     /// A single atomic function for moving hosts between hostlists. Called on the following occasions:
     ///
     /// * When we cannot connect to a peer: move to grey, remove from white and gold.

+ 3 - 9
src/net/session/refine_session.rs

@@ -267,6 +267,7 @@ impl GreylistRefinery {
             {
                 Some((entry, _)) => {
                     let url = &entry.0;
+                    let last_seen = &entry.1;
 
                     if let Err(e) = hosts.try_register(url.clone(), HostState::Refine) {
                         debug!(target: "net::refinery", "Unable to refine addr={}, err={}",
@@ -275,17 +276,12 @@ impl GreylistRefinery {
                     }
 
                     if !self.session().handshake_node(url.clone(), p2p.clone()).await {
-                        hosts.container.remove_if_exists(HostColor::Grey, url);
-
                         debug!(
                             target: "net::refinery",
                             "Peer {} handshake failed. Removed from greylist", url,
                         );
 
-                        // Remove this entry from HostRegistry to avoid this host getting
-                        // stuck in the Refining state. This is a safe since the hostlist
-                        // modification is now complete.
-                        hosts.unregister(url);
+                        hosts.greylist_host(url, *last_seen).unwrap();
 
                         continue
                     }
@@ -295,9 +291,7 @@ 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).unwrap();
-                    hosts.unregister(url);
+                    hosts.whitelist_host(url, last_seen).unwrap();
 
                     debug!(target: "net::refinery", "GreylistRefinery complete!");