Просмотр исходного кода

net: avoid adding our own address to the greylist when on localnet

also standardize the whitelist_store_or_update function call
lunar-mining 2 лет назад
Родитель
Сommit
ef3b95ffdf
2 измененных файлов с 19 добавлено и 10 удалено
  1. 1 1
      src/net/hosts/refinery.rs
  2. 18 9
      src/net/hosts/store.rs

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

@@ -84,7 +84,7 @@ impl GreylistRefinery {
                     let last_seen = UNIX_EPOCH.elapsed().unwrap().as_secs();
                     let last_seen = UNIX_EPOCH.elapsed().unwrap().as_secs();
 
 
                     // Append to the whitelist.
                     // Append to the whitelist.
-                    hosts.whitelist_store_or_update(url, last_seen).await.unwrap();
+                    hosts.whitelist_store_or_update(&[(url.clone(), last_seen)]).await.unwrap();
 
 
                     // Remove whitelisted peer from the greylist.
                     // Remove whitelisted peer from the greylist.
                     hosts.greylist_remove(url, position).await;
                     hosts.greylist_remove(url, position).await;

+ 18 - 9
src/net/hosts/store.rs

@@ -18,7 +18,7 @@
 
 
 use std::{collections::HashSet, sync::Arc};
 use std::{collections::HashSet, sync::Arc};
 
 
-use log::{debug, trace};
+use log::{debug, trace, warn};
 use rand::{
 use rand::{
     prelude::{IteratorRandom, SliceRandom},
     prelude::{IteratorRandom, SliceRandom},
     rngs::OsRng,
     rngs::OsRng,
@@ -162,20 +162,22 @@ impl Hosts {
     // Store the address in the whitelist if we don't have it.
     // Store the address in the whitelist if we don't have it.
     // Otherwise, update the last_seen field.
     // Otherwise, update the last_seen field.
     // TODO: test the performance of this method. It might be costly.
     // TODO: test the performance of this method. It might be costly.
-    pub async fn whitelist_store_or_update(&self, addr: &Url, last_seen: u64) -> Result<()> {
+    pub async fn whitelist_store_or_update(&self, addrs: &[(Url, u64)]) -> Result<()> {
         debug!(target: "net::hosts::whitelist_store_or_update()", "[START]");
         debug!(target: "net::hosts::whitelist_store_or_update()", "[START]");
 
 
-        if !self.whitelist_contains(addr).await {
-            debug!(target: "net::hosts::whitelist_store_or_update()",
+        for (addr, last_seen) in addrs {
+            if !self.whitelist_contains(addr).await {
+                debug!(target: "net::hosts::whitelist_store_or_update()",
         "We do not have this entry in the whitelist. Adding to store...");
         "We do not have this entry in the whitelist. Adding to store...");
 
 
-            self.whitelist_store(addr, last_seen).await;
-        } else {
-            debug!(target: "net::hosts::whitelist_store_or_update()",
+                self.whitelist_store(addr, last_seen).await;
+            } else {
+                debug!(target: "net::hosts::whitelist_store_or_update()",
         "We have this entry in the whitelist. Updating last seen...");
         "We have this entry in the whitelist. Updating last seen...");
 
 
-            let index = self.get_whitelist_index_at_addr(addr).await?;
-            self.whitelist_update_last_seen(addr, last_seen, index).await;
+                let index = self.get_whitelist_index_at_addr(addr).await?;
+                self.whitelist_update_last_seen(addr, last_seen.clone(), index).await;
+            }
         }
         }
         Ok(())
         Ok(())
     }
     }
@@ -381,6 +383,13 @@ impl Hosts {
                         continue 'addr_loop
                         continue 'addr_loop
                     }
                     }
                 }
                 }
+            } else {
+                // On localnet, make sure ours ports don't enter the host set.
+                for ext in &self.settings.external_addrs {
+                    if addr_.port() == ext.port() {
+                        continue 'addr_loop
+                    }
+                }
             }
             }
 
 
             // We do this hack in order to parse IPs properly.
             // We do this hack in order to parse IPs properly.