Przeglądaj źródła

net: select from the greylist if we have no white or gold

Also add a setting to allow us to specify slot_preference_strict = true, which
would disable greylist selection if set, even if we have no white or
gold connections. (this might be useful in the case of an attack where we want to strictly avoid greylist entries).
draoi 2 lat temu
rodzic
commit
08141d9c50
3 zmienionych plików z 24 dodań i 4 usunięć
  1. 10 1
      src/net/session/outbound_session.rs
  2. 14 1
      src/net/settings.rs
  3. 0 2
      src/net/tests.rs

+ 10 - 1
src/net/session/outbound_session.rs

@@ -214,8 +214,17 @@ impl Slot {
 
         let transports = &settings.allowed_transports;
         let transport_mixing = settings.transport_mixing;
+        let preference_strict = &settings.slot_preference_strict;
 
-        let addrs = if slot < gold_count {
+        let grey_only = hosts.container.is_empty(HostColor::White).await &&
+            hosts.container.is_empty(HostColor::Gold).await &&
+            !hosts.container.is_empty(HostColor::Grey).await;
+
+        // If we only have grey entries, select from the greylist. Otherwise,
+        // use the preference defined in settings.
+        let addrs = if grey_only && !preference_strict {
+            container.fetch(HostColor::Grey, transports, transport_mixing).await
+        } else if slot < gold_count {
             container.fetch(HostColor::Gold, transports, transport_mixing).await
         } else if slot < white_count {
             container.fetch(HostColor::White, transports, transport_mixing).await

+ 14 - 1
src/net/settings.rs

@@ -72,6 +72,10 @@ pub struct Settings {
     pub white_connect_percent: usize,
     /// Number of goldlist connections
     pub gold_connect_count: usize,
+    /// If this is true, strictly follow the gold_connect_count and
+    /// white_connect_percent settings. Otherwise, connect to greylist
+    /// entries if we have no white or gold connections.
+    pub slot_preference_strict: bool,
     /// Number of seconds with no connections after which refinery
     /// process is paused.
     pub time_with_no_connections: u64,
@@ -106,6 +110,7 @@ impl Default for Settings {
             greylist_refinery_interval: 15,
             white_connect_percent: 70,
             gold_connect_count: 2,
+            slot_preference_strict: false,
             time_with_no_connections: 30,
             blacklist: vec![],
         }
@@ -176,7 +181,9 @@ pub struct SettingsOpt {
     #[structopt(long)]
     pub transport_mixing: Option<bool>,
 
-    /// Allow localnet hosts
+    /// If this is true, strictly follow the gold_connect_count and
+    /// white_connect_percent settings. Otherwise, connect to greylist
+    /// entries if we have no white or gold connections.
     #[serde(default)]
     #[structopt(long)]
     pub localnet: bool,
@@ -206,6 +213,11 @@ pub struct SettingsOpt {
     #[structopt(skip)]
     pub gold_connect_count: Option<usize>,
 
+    /// Allow localnet hosts
+    #[serde(default)]
+    #[structopt(long)]
+    pub slot_preference_strict: bool,
+
     /// Number of seconds with no connections after which refinery
     /// process is paused.
     #[structopt(skip)]
@@ -255,6 +267,7 @@ impl From<SettingsOpt> for Settings {
                 .unwrap_or(def.greylist_refinery_interval),
             white_connect_percent: opt.white_connect_percent.unwrap_or(def.white_connect_percent),
             gold_connect_count: opt.gold_connect_count.unwrap_or(def.gold_connect_count),
+            slot_preference_strict: opt.slot_preference_strict,
             time_with_no_connections: opt
                 .time_with_no_connections
                 .unwrap_or(def.time_with_no_connections),

+ 0 - 2
src/net/tests.rs

@@ -85,8 +85,6 @@ async fn spawn_node(
         inbound_addrs,
         external_addrs,
         outbound_connections: 2,
-        gold_connect_count: 0,
-        white_connect_percent: 0,
         outbound_peer_discovery_cooloff_time: 2,
         outbound_connect_timeout: 2,
         inbound_connections: usize::MAX,