Explorar o código

net: read hostlist path from Settings. Define a default setting and allow overriding in config

lunar-mining %!s(int64=2) %!d(string=hai) anos
pai
achega
d15cc3b2bd
Modificáronse 3 ficheiros con 29 adicións e 10 borrados
  1. 16 8
      src/net/hosts/store.rs
  2. 10 0
      src/net/settings.rs
  3. 3 2
      src/net/tests.rs

+ 16 - 8
src/net/hosts/store.rs

@@ -18,7 +18,8 @@
 
 use std::{
     collections::{HashMap, HashSet},
-    path::Path,
+    fs,
+    fs::File,
     sync::Arc,
 };
 
@@ -36,7 +37,6 @@ use crate::{
     system::{Subscriber, SubscriberPtr, Subscription},
     util::{
         file::{load_file, save_file},
-        path,
         path::expand_path,
     },
     Error, Result,
@@ -1068,7 +1068,16 @@ impl Hosts {
 
     pub async fn load_hosts(&self) -> Result<()> {
         // TODO: FIXME: make this a net::Setting
-        let path = expand_path(&"~/.config/darkfi/hostlist.tsv")?;
+        //let path = expand_path(&"~/.config/darkfi/hostlist.tsv")?;
+        let path = expand_path(&self.settings.hostlist)?;
+
+        if !path.exists() {
+            if let Some(parent) = path.parent() {
+                fs::create_dir_all(parent)?;
+            }
+
+            File::create(path.clone())?;
+        }
 
         let contents = load_file(&path);
         if let Err(e) = contents {
@@ -1082,7 +1091,7 @@ impl Hosts {
             let url = match Url::parse(data[1]) {
                 Ok(u) => u,
                 Err(e) => {
-                    debug!(target: "net::hosts::store", "load_hosts(): Skipping malformed URL...");
+                    debug!(target: "net::hosts::store", "load_hosts(): Skipping malformed URL {}", e);
                     continue
                 }
             };
@@ -1090,7 +1099,7 @@ impl Hosts {
             let last_seen = match data[2].parse::<u64>() {
                 Ok(t) => t,
                 Err(e) => {
-                    debug!(target: "net::hosts::store", "load_hosts(): Skipping malformed last seen...");
+                    debug!(target: "net::hosts::store", "load_hosts(): Skipping malformed last seen {}", e);
                     continue
                 }
             };
@@ -1116,8 +1125,7 @@ impl Hosts {
 
     // Save the hostlist to a file.
     pub async fn save_hosts(&self) -> Result<()> {
-        // TODO: FIXME: make this a net::Setting
-        let path = expand_path(&"~/.config/darkfi/hostlist.tsv")?;
+        let path = expand_path(&self.settings.hostlist)?;
 
         let mut tsv = String::new();
 
@@ -1128,7 +1136,7 @@ impl Hosts {
         }
 
         if !tsv.eq("") {
-            info!(target: "net::hosts::store", "Saving current hosts of spawned networks to: {:?}",
+            info!(target: "net::hosts::store", "Saving hosts to: {:?}",
                   path);
             if let Err(e) = save_file(&path, &tsv) {
                 error!(target: "net::hosts::store", "Failed saving hosts: {}", e);

+ 10 - 0
src/net/settings.rs

@@ -70,12 +70,15 @@ pub struct Settings {
     pub outbound_peer_discovery_attempt_time: u64,
     /// Advertise our external address
     pub advertise: bool,
+    /// Hostlist storage path
+    pub hostlist: String,
 }
 
 impl Default for Settings {
     fn default() -> Self {
         let version = option_env!("CARGO_PKG_VERSION").unwrap_or("0.0.0");
         let app_version = semver::Version::parse(version).unwrap();
+        let hostlist = String::from("~/.config/darkfi/hostlist.tsv");
 
         Self {
             node_id: String::new(),
@@ -97,6 +100,7 @@ impl Default for Settings {
             outbound_peer_discovery_cooloff_time: 30,
             outbound_peer_discovery_attempt_time: 5,
             advertise: true,
+            hostlist,
         }
     }
 }
@@ -190,6 +194,11 @@ pub struct SettingsOpt {
     #[serde(default)]
     #[structopt(long)]
     pub advertise: bool,
+
+    /// Hosts .tsv file to use
+    #[serde(default)]
+    #[structopt(long)]
+    pub hostlist: String,
 }
 
 impl From<SettingsOpt> for Settings {
@@ -228,6 +237,7 @@ impl From<SettingsOpt> for Settings {
                 .outbound_peer_discovery_attempt_time
                 .unwrap_or(def.outbound_peer_discovery_attempt_time),
             advertise: opt.advertise,
+            hostlist: opt.hostlist,
         }
     }
 }

+ 3 - 2
src/net/tests.rs

@@ -31,7 +31,7 @@ use crate::{
 };
 
 // Number of nodes to spawn and number of peers each node connects to
-const N_NODES: usize = 5;
+const N_NODES: usize = 3;
 const N_CONNS: usize = 2;
 
 #[test]
@@ -88,6 +88,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(".config/darkfi/hosts.tsv"),
             peers,
             allowed_transports: vec!["tcp".to_string()],
             node_id: i.to_string(),
@@ -105,7 +106,7 @@ async fn hostlist_propagation(ex: Arc<Executor<'static>>) {
     }
 
     info!("Waiting until all peers connect");
-    sleep(60).await;
+    sleep(10).await;
 
     info!("Inspecting hostlists...");
     for p2p in p2p_instances.iter() {