Explorar o código

lilith: add missing safety check

draoi %!s(int64=2) %!d(string=hai) anos
pai
achega
8dad8dfc8f
Modificáronse 1 ficheiros con 16 adicións e 1 borrados
  1. 16 1
      bin/lilith/src/main.rs

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

@@ -39,7 +39,11 @@ use url::Url;
 
 
 use darkfi::{
 use darkfi::{
     async_daemonize, cli_desc,
     async_daemonize, cli_desc,
-    net::{self, hosts::HostColor, P2p, P2pPtr},
+    net::{
+        self,
+        hosts::{HostColor, HostState},
+        P2p, P2pPtr,
+    },
     rpc::{
     rpc::{
         jsonrpc::*,
         jsonrpc::*,
         server::{listen_and_serve, RequestHandler},
         server::{listen_and_serve, RequestHandler},
@@ -53,6 +57,7 @@ const CONFIG_FILE: &str = "lilith_config.toml";
 const CONFIG_FILE_CONTENTS: &str = include_str!("../lilith_config.toml");
 const CONFIG_FILE_CONTENTS: &str = include_str!("../lilith_config.toml");
 
 
 /// Interval after which the refinery happens (in seconds)
 /// Interval after which the refinery happens (in seconds)
+// TODO: Make this configurable
 const REFINERY_INTERVAL: u64 = 60;
 const REFINERY_INTERVAL: u64 = 60;
 
 
 #[derive(Clone, Debug, serde::Deserialize, StructOpt, StructOptToml)]
 #[derive(Clone, Debug, serde::Deserialize, StructOpt, StructOptToml)]
@@ -177,9 +182,17 @@ impl Lilith {
             }
             }
 
 
             let (entry, position) = hosts.container.fetch_last(HostColor::White).await;
             let (entry, position) = hosts.container.fetch_last(HostColor::White).await;
+
             let url = &entry.0;
             let url = &entry.0;
             let last_seen = &entry.1;
             let last_seen = &entry.1;
 
 
+            if let Err(e) = hosts.try_register(url.clone(), HostState::Refine).await {
+                debug!(target: "lilith", "Unable to refine addr={}, err={}",
+                       url.clone(), e);
+
+                continue
+            }
+
             if !p2p.session_refine().handshake_node(url.clone(), p2p.clone()).await {
             if !p2p.session_refine().handshake_node(url.clone(), p2p.clone()).await {
                 debug!(target: "lilith", "Host {} is not responsive. Downgrading from whitelist", url);
                 debug!(target: "lilith", "Host {} is not responsive. Downgrading from whitelist", url);
                 hosts.move_host(url, *last_seen, HostColor::Grey).await?;
                 hosts.move_host(url, *last_seen, HostColor::Grey).await?;
@@ -188,6 +201,8 @@ impl Lilith {
                 continue
                 continue
             }
             }
 
 
+            debug!(target: "lilith","Peer {} is responsive. Updating last_seen", url);
+
             // This node is active. Update the last seen field.
             // This node is active. Update the last seen field.
             let last_seen = UNIX_EPOCH.elapsed().unwrap().as_secs();
             let last_seen = UNIX_EPOCH.elapsed().unwrap().as_secs();
             hosts
             hosts