Explorar o código

net/outbound_session: fix slot selection bug and simplify user settings

Previously you could disable logic arms in the slot selection logic by
adjusting the user settings, which could end up in slots being under
utilized. We fix this by simplifying the user settings and logic, with
the assumption that users mostly care about disabling grey connections
if needed.

In the future we might want to force some white connections. Also we
might be able to make slot selection more efficient.
darkfi hai 2 meses
pai
achega
9bdb0d6aa1
Modificáronse 2 ficheiros con 33 adicións e 38 borrados
  1. 19 16
      src/net/session/outbound_session.rs
  2. 14 22
      src/net/settings.rs

+ 19 - 16
src/net/session/outbound_session.rs

@@ -277,27 +277,30 @@ impl Slot {
         // Acquire Settings read lock
         let settings = self.p2p().settings().read_arc().await;
 
-        let white_count = (settings.white_connect_percent * settings.outbound_connections) / 100;
-        let gold_count = settings.gold_connect_count;
-
         let transports = settings.active_profiles.clone();
-        let preference_strict = settings.slot_preference_strict;
+        let outbound_connections = settings.outbound_connections;
+        let known_peer_percent = settings.known_peer_percent;
+        let disable_greys = settings.disable_greys;
 
         // Drop Settings read lock
         drop(settings);
 
-        let grey_only = hosts.container.is_empty(HostColor::White) &&
-            hosts.container.is_empty(HostColor::Gold) &&
-            !hosts.container.is_empty(HostColor::Grey);
-
-        // 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_with_schemes(HostColor::Grey, &transports, None)
-        } else if slot < gold_count {
-            container.fetch_with_schemes(HostColor::Gold, &transports, None)
-        } else if slot < white_count {
-            container.fetch_with_schemes(HostColor::White, &transports, None)
+        // Calculate the number of slots for known peers (gold or white)
+        let max_known_percent = if disable_greys { 100 } else { 80 };
+        let bounded_percent = known_peer_percent.min(max_known_percent);
+        let known_count = (bounded_percent * outbound_connections) / 100;
+
+        // For known peer slots, prefer gold then white. Otherwise use grey.
+        let addrs = if slot < known_count {
+            // Try gold first, then white for known peers.
+            // NOTE: We might want to force white connections, otherwise we may
+            // end up connecting to gold hosts only.
+            let gold = container.fetch_with_schemes(HostColor::Gold, &transports, None);
+            if gold.is_empty() {
+                container.fetch_with_schemes(HostColor::White, &transports, None)
+            } else {
+                gold
+            }
         } else {
             container.fetch_with_schemes(HostColor::Grey, &transports, None)
         };

+ 14 - 22
src/net/settings.rs

@@ -110,14 +110,12 @@ pub struct Settings {
     pub hostlist: Option<String>,
     /// Pause interval within greylist refinery process
     pub greylist_refinery_interval: u64,
-    /// Percent of connections to come from the whitelist
-    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,
+    /// Percent of connections to prefer known peers (gold or white).
+    /// Capped at 80% (better for network health).
+    pub known_peer_percent: usize,
+    /// If true, disable greylist connections entirely.
+    /// When set, known_peer_percent is effectively 100%.
+    pub disable_greys: bool,
     /// Number of seconds with no connections after which refinery
     /// process is paused.
     pub time_with_no_connections: u64,
@@ -162,9 +160,8 @@ impl Default for Settings {
             p2p_datastore: None,
             hostlist: None,
             greylist_refinery_interval: 15,
-            white_connect_percent: 70,
-            gold_connect_count: 2,
-            slot_preference_strict: false,
+            known_peer_percent: 70,
+            disable_greys: false,
             time_with_no_connections: 30,
             blacklist: vec![],
             ban_policy: BanPolicy::Strict,
@@ -333,18 +330,14 @@ pub struct SettingsOpt {
     #[structopt(skip)]
     pub greylist_refinery_interval: Option<u64>,
 
-    /// Number of whitelist connections
+    /// Percent of connections to prefer known peers (gold or white)
     #[structopt(skip)]
-    pub white_connect_percent: Option<usize>,
+    pub known_peer_percent: Option<usize>,
 
-    /// Number of goldlist connections
-    #[structopt(skip)]
-    pub gold_connect_count: Option<usize>,
-
-    /// Allow localnet hosts
+    /// If true, disable greylist connections entirely
     #[serde(default)]
     #[structopt(long)]
-    pub slot_preference_strict: bool,
+    pub disable_greys: bool,
 
     /// Number of seconds with no connections after which refinery
     /// process is paused.
@@ -437,9 +430,8 @@ impl TryFrom<(&str, &str, SettingsOpt)> for Settings {
             greylist_refinery_interval: opt
                 .greylist_refinery_interval
                 .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,
+            known_peer_percent: opt.known_peer_percent.unwrap_or(def.known_peer_percent),
+            disable_greys: opt.disable_greys,
             time_with_no_connections: opt
                 .time_with_no_connections
                 .unwrap_or(def.time_with_no_connections),