lunar-mining 2 лет назад
Родитель
Сommit
ec5abf9683

+ 3 - 3
src/net/hosts/refinery.rs

@@ -30,9 +30,9 @@ use crate::{
 
 pub type GreylistRefineryPtr = Arc<GreylistRefinery>;
 
-//// Probe random peers on the greylist. If a peer is responsive, update the last_seen field and
-//// add it to the whitelist. If a node does not respond, remove it from the greylist.
-//// Called periodically.
+/// Probe random peers on the greylist. If a peer is responsive, update the last_seen field and
+/// add it to the whitelist. If a node does not respond, remove it from the greylist.
+/// Called periodically.
 pub struct GreylistRefinery {
     /// Weak pointer to parent p2p object
     pub(in crate::net) p2p: LazyWeak<P2p>,

+ 31 - 33
src/net/hosts/store.rs

@@ -268,7 +268,7 @@ impl Hosts {
                 "Found valid host '{}",
                 host
             );
-            return Some((host.clone(), last_seen.clone()))
+            return Some((host.clone(), last_seen))
         }
 
         None
@@ -286,7 +286,7 @@ impl Hosts {
             if !self.greylist_contains(&addr).await {
                 debug!(target: "store::greylist_store_or_update()", "We do not have this entry in the hostlist. Adding to store...");
 
-                self.greylist_store(addr.clone(), last_seen.clone()).await;
+                self.greylist_store(addr.clone(), last_seen).await;
             }
 
             debug!(target: "store::greylist_store_or_update()",
@@ -312,7 +312,7 @@ impl Hosts {
                 debug!(target: "store::whitelist_store_or_update()",
         "We do not have this entry in the whitelist. Adding to store...");
 
-                self.whitelist_store(addr.clone(), last_seen.clone()).await;
+                self.whitelist_store(addr.clone(), *last_seen).await;
             }
 
             debug!(target: "store::whitelist_store_or_update()",
@@ -322,7 +322,7 @@ impl Hosts {
                 .get_whitelist_index_at_addr(addr.clone())
                 .await
                 .expect("Expected whitelist entry to exist");
-            self.whitelist_update_last_seen(addr, last_seen.clone(), index).await;
+            self.whitelist_update_last_seen(addr, *last_seen, index).await;
         }
     }
 
@@ -337,7 +337,7 @@ impl Hosts {
                 debug!(target: "store::anchorlist_store_or_update()",
         "We do not have this entry in the whitelist. Adding to store...");
 
-                self.anchorlist_store(addr.clone(), last_seen.clone()).await;
+                self.anchorlist_store(addr.clone(), *last_seen).await;
             }
             debug!(target: "store::anchorlist_store_or_update()",
             "We have this entry in the anchorlist. Updating last seen...");
@@ -346,7 +346,7 @@ impl Hosts {
                 .get_anchorlist_index_at_addr(addr.clone())
                 .await
                 .expect("Expected anchorlist entry to exist");
-            self.anchorlist_update_last_seen(addr, last_seen.clone(), index).await;
+            self.anchorlist_update_last_seen(addr, *last_seen, index).await;
         }
     }
 
@@ -591,7 +591,7 @@ impl Hosts {
                 _ => continue,
             }
 
-            ret.push((addr_.clone(), last_seen.clone()));
+            ret.push((addr_.clone(), *last_seen));
         }
 
         ret
@@ -731,7 +731,7 @@ impl Hosts {
         let greylist = self.greylist.read().await;
         let position = rand::thread_rng().gen_range(0..greylist.len());
         let entry = &greylist[position];
-        (entry.clone(), position.clone())
+        (entry.clone(), position)
     }
 
     /// Get up to n random whitelisted peers that match the given transport schemes from the hosts set.
@@ -855,8 +855,6 @@ impl Hosts {
                 ret.push((addr.clone(), *last_seen));
                 limit -= 1;
                 if limit == 0 {
-                    debug!(target: "deadlock", "Found matching grey scheme, returning len: {}", ret.len());
-
                     debug!(target: "store::greylist_fetch_with_schemes", "Found matching greylist entry, returning");
                     return ret
                 }
@@ -891,18 +889,15 @@ impl Hosts {
                     ret.push((addr.clone(), *last_seen));
                     parsed_limit -= 1;
                     if parsed_limit == 0 {
-                        debug!(target: "deadlock",
-                           "Found matching white scheme, returning {:?}", ret);
                         trace!(target: "store::whitelist_fetch_with_schemes",
                            "Found matching white scheme, returning {:?}", ret);
                         return ret
                     }
-                }
-                debug!(target: "deadlock",
-                          "No matching schemes! Trying greylist...");
-                warn!(target: "store::whitelist_fetch_with_schemes",
+                } else {
+                    warn!(target: "store::whitelist_fetch_with_schemes",
                           "No matching schemes! Trying greylist...");
-                return self.greylist_fetch_with_schemes(schemes, limit).await
+                    return self.greylist_fetch_with_schemes(schemes, limit).await
+                }
             }
         }
         // Whitelist is empty!
@@ -940,21 +935,15 @@ impl Hosts {
                     ret.push((addr.clone(), *last_seen));
                     parsed_limit -= 1;
                     if parsed_limit == 0 {
-                        debug!(target: "deadlock",
-                           "Found matching anchor scheme, returning {:?}", ret);
-
                         trace!(target: "store::anchorlist_fetch_with_schemes",
                            "Found matching anchor scheme, returning {:?}", ret);
                         return ret
                     }
-                }
-
-                debug!(target: "deadlock",
-                          "No matching schemes! Trying whitelist...");
-
-                warn!(target: "store::anchorlist_fetch_with_schemes",
+                } else {
+                    warn!(target: "store::anchorlist_fetch_with_schemes",
                           "No matching schemes! Trying whitelist...");
-                return self.whitelist_fetch_with_schemes(schemes, limit).await
+                    return self.whitelist_fetch_with_schemes(schemes, limit).await
+                }
             }
         }
 
@@ -1244,10 +1233,17 @@ mod tests {
                 hostlist.push(addrs);
             }
 
-            // Check we're returning the correct addresses.
-            assert!(anchor_urls.sort() == hostlist[0].sort());
-            assert!(white_urls.sort() == hostlist[4].sort());
-            assert!(grey_urls.sort() == hostlist[7].sort());
+            //// Check we're returning the correct addresses.
+            anchor_urls.sort();
+            white_urls.sort();
+            grey_urls.sort();
+            hostlist[0].sort();
+            hostlist[4].sort();
+            hostlist[7].sort();
+
+            assert!(anchor_urls == hostlist[0]);
+            assert!(white_urls == hostlist[4]);
+            assert!(grey_urls == hostlist[7]);
 
             // Now clear the anchorlist.
             // anchorlist_fetch_address should return whitelist entries if
@@ -1257,7 +1253,8 @@ mod tests {
             drop(anchorlist);
 
             let mut addrs = hosts.anchorlist_fetch_address(transports).await;
-            assert!(white_urls.sort() == addrs.sort());
+            addrs.sort();
+            assert!(white_urls == addrs);
 
             // Now clear the whitelist.
             // anchorlist_fetch_address should return greylist entries if
@@ -1267,7 +1264,8 @@ mod tests {
             drop(whitelist);
 
             let mut addrs = hosts.anchorlist_fetch_address(transports).await;
-            assert!(grey_urls.sort() == addrs.sort());
+            addrs.sort();
+            assert!(grey_urls == addrs);
         })
     }
 }

+ 1 - 1
src/net/protocol/protocol_address.rs

@@ -180,7 +180,7 @@ impl ProtocolAddress {
         }
 
         // Do nothing if advertise is set to false
-        if self.settings.advertise == false {
+        if !self.settings.advertise {
             debug!(target: "net::protocol_address::send_my_addrs()", "Advertise is false. Stopping");
             return Ok(())
         }

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

@@ -163,14 +163,14 @@ pub trait Session: Sync {
         let last_seen = UNIX_EPOCH.elapsed().unwrap().as_secs();
         hosts.anchorlist_store_or_update(&[(addr.clone(), last_seen)]).await;
 
-        if hosts.whitelist_contains(&addr).await {
+        if hosts.whitelist_contains(addr).await {
             let index = hosts.get_whitelist_index_at_addr(addr.clone()).await.unwrap();
-            hosts.whitelist_remove(&addr, index).await;
+            hosts.whitelist_remove(addr, index).await;
         }
 
-        if hosts.greylist_contains(&addr).await {
+        if hosts.greylist_contains(addr).await {
             let index = hosts.get_greylist_index_at_addr(addr.clone()).await.unwrap();
-            hosts.greylist_remove(&addr, index).await;
+            hosts.greylist_remove(addr, index).await;
         }
     }
 

+ 2 - 3
src/net/session/outbound_session.rs

@@ -227,8 +227,7 @@ impl Slot {
         // * we already have this connection established
         // * we already have this configured as a manual peer
         // * address is already pending a connection
-        let addr = hosts.check_address_with_lock(self.p2p(), addrs).await;
-        return addr
+        hosts.check_address_with_lock(self.p2p(), addrs).await
     }
 
     // We first try to make connections to the addresses on our anchor list. We then find some
@@ -294,7 +293,7 @@ impl Slot {
             );
 
             dnetev!(self, OutboundSlotConnecting, {
-                slot: slot,
+                slot,
                 addr: host.clone(),
             });
 

+ 10 - 5
src/net/tests.rs

@@ -20,7 +20,7 @@
 
 use std::sync::Arc;
 
-use log::{debug, info};
+use log::{debug, info, warn};
 use rand::Rng;
 use smol::{channel, future, Executor};
 use url::Url;
@@ -63,7 +63,9 @@ fn p2p_test() {
     cfg.add_filter_ignore("net::channel::main_receive_loop()".to_string());
     cfg.add_filter_ignore("net::tcp".to_string());
 
-    simplelog::TermLogger::init(
+    // We check this error so we can execute same file tests in parallel,
+    // otherwise second one fails to init logger here.
+    if simplelog::TermLogger::init(
         simplelog::LevelFilter::Info,
         //simplelog::LevelFilter::Debug,
         //simplelog::LevelFilter::Trace,
@@ -71,7 +73,10 @@ fn p2p_test() {
         simplelog::TerminalMode::Mixed,
         simplelog::ColorChoice::Auto,
     )
-    .unwrap();
+    .is_err()
+    {
+        warn!(target: "net::test", "Logger already initialized");
+    }
 
     let ex = Arc::new(Executor::new());
     let ex_ = ex.clone();
@@ -131,7 +136,7 @@ async fn hostlist_propagation(ex: Arc<Executor<'static>>) {
             //outbound_connect_timeout: 10,
             inbound_connections: usize::MAX,
             seeds: vec![seed_addr.clone()],
-            hostlist: String::from(format!("~/.config/darkfi/hosts{}.tsv", i)),
+            hostlist: format!("~/.config/darkfi/hosts{}.tsv", i),
             peers,
             allowed_transports: vec!["tcp".to_string()],
             node_id: i.to_string(),
@@ -148,7 +153,7 @@ async fn hostlist_propagation(ex: Arc<Executor<'static>>) {
     }
 
     info!("Waiting until all peers connect");
-    sleep(5).await;
+    sleep(10).await;
 
     info!("Inspecting hostlists...");
     for p2p in p2p_instances.iter() {