Răsfoiți Sursa

lilith: select last element from the whitelist, not random element

this reduces the chance of the same element being selected twice consecutively (would only happen consecutively whitelist has a length of 1)
lunar-mining 2 ani în urmă
părinte
comite
15d5f7e6d4
2 a modificat fișierele cu 40 adăugiri și 1 ștergeri
  1. 1 1
      bin/lilith/src/main.rs
  2. 39 0
      src/net/hosts/store.rs

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

@@ -152,7 +152,7 @@ impl Lilith {
                 continue
             }
 
-            let (entry, position) = hosts.whitelist_fetch_random().await;
+            let (entry, position) = hosts.whitelist_fetch_last().await;
             let url = &entry.0;
 
             if !ping_node(url, p2p.clone()).await {

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

@@ -767,6 +767,14 @@ impl Hosts {
         (entry.clone(), position)
     }
 
+    /// Get the oldest entry from the whitelist.
+    pub async fn whitelist_fetch_last(&self) -> ((Url, u64), usize) {
+        let whitelist = self.whitelist.read().await;
+        let position = whitelist.len() - 1;
+        let entry = &whitelist[position];
+        (entry.clone(), position)
+    }
+
     /// Get up to n random whitelisted peers that match the given transport schemes from the hosts set.
     pub async fn whitelist_fetch_n_random_with_schemes(
         &self,
@@ -1211,6 +1219,37 @@ mod tests {
         });
     }
 
+    #[test]
+    fn test_whitelist_get_last() {
+        smol::block_on(async {
+            let settings = Settings {
+                localnet: false,
+                external_addrs: vec![
+                    Url::parse("tcp://foo.bar:123").unwrap(),
+                    Url::parse("tcp://lol.cat:321").unwrap(),
+                ],
+                ..Default::default()
+            };
+
+            let hosts = Hosts::new(Arc::new(settings.clone()));
+
+            // Build up a hostlist
+            for i in 0..10 {
+                sleep(1).await;
+                let last_seen = UNIX_EPOCH.elapsed().unwrap().as_secs();
+                let url = Url::parse(&format!("tcp://whitelist{}:123", i)).unwrap();
+                hosts.whitelist_store(url.clone(), last_seen).await;
+            }
+
+            for (url, last_seen) in hosts.whitelist.read().await.iter() {
+                println!("{} {}", url, last_seen);
+            }
+
+            let (entry, _position) = hosts.whitelist_fetch_last().await;
+            println!("last entry: {} {}", entry.0, entry.1);
+        });
+    }
+
     #[test]
     fn test_hostlist_get_entry() {
         smol::block_on(async {