Kaynağa Gözat

darkfid: don't require minerd to be running to start

skoupidi 1 yıl önce
ebeveyn
işleme
40ca0d4b92

+ 3 - 8
bin/darkfid/src/lib.rs

@@ -21,7 +21,7 @@ use std::{
     sync::Arc,
     sync::Arc,
 };
 };
 
 
-use log::{debug, error, info};
+use log::{debug, error, info, warn};
 use smol::lock::Mutex;
 use smol::lock::Mutex;
 use url::Url;
 use url::Url;
 
 
@@ -158,11 +158,7 @@ impl Darkfid {
         // Initialize JSON-RPC client to perform requests to minerd
         // Initialize JSON-RPC client to perform requests to minerd
         let rpc_client = match minerd_endpoint {
         let rpc_client = match minerd_endpoint {
             Some(endpoint) => {
             Some(endpoint) => {
-                let Ok(rpc_client) = MinerRpcClient::new(endpoint.clone(), ex.clone()).await else {
-                    error!(target: "darkfid::Darkfid::init", "Failed to initialize miner daemon rpc client, check if minerd is running");
-                    return Err(Error::RpcClientStopped)
-                };
-                Some(Mutex::new(rpc_client))
+                Some(Mutex::new(MinerRpcClient::new(endpoint.clone(), ex.clone()).await))
             }
             }
             None => None,
             None => None,
         };
         };
@@ -196,8 +192,7 @@ impl Darkfid {
         // Pinging minerd daemon to verify it listens
         // Pinging minerd daemon to verify it listens
         if self.node.rpc_client.is_some() {
         if self.node.rpc_client.is_some() {
             if let Err(e) = self.node.ping_miner_daemon().await {
             if let Err(e) = self.node.ping_miner_daemon().await {
-                error!(target: "darkfid::Darkfid::start", "Failed to ping miner daemon: {}", e);
-                return Err(Error::RpcClientStopped)
+                warn!(target: "darkfid::Darkfid::start", "Failed to ping miner daemon: {}", e);
             }
             }
         }
         }
 
 

+ 17 - 8
bin/darkfid/src/rpc.rs

@@ -19,7 +19,7 @@
 use std::{collections::HashSet, time::Instant};
 use std::{collections::HashSet, time::Instant};
 
 
 use async_trait::async_trait;
 use async_trait::async_trait;
-use log::{debug, error, info};
+use log::{debug, error, info, warn};
 use smol::lock::MutexGuard;
 use smol::lock::MutexGuard;
 use tinyjson::JsonValue;
 use tinyjson::JsonValue;
 use url::Url;
 use url::Url;
@@ -52,18 +52,26 @@ pub struct MmRpcHandler;
 pub struct MinerRpcClient {
 pub struct MinerRpcClient {
     endpoint: Url,
     endpoint: Url,
     ex: ExecutorPtr,
     ex: ExecutorPtr,
-    client: RpcChadClient,
+    client: Option<RpcChadClient>,
 }
 }
 
 
 impl MinerRpcClient {
 impl MinerRpcClient {
-    pub async fn new(endpoint: Url, ex: ExecutorPtr) -> Result<Self> {
-        let client = RpcChadClient::new(endpoint.clone(), ex.clone()).await?;
-        Ok(Self { endpoint, ex, client })
+    pub async fn new(endpoint: Url, ex: ExecutorPtr) -> Self {
+        let client = match RpcChadClient::new(endpoint.clone(), ex.clone()).await {
+            Ok(c) => Some(c),
+            Err(_) => {
+                warn!(target: "darkfid::Darkfid::init", "Failed to initialize miner daemon rpc client, will try later");
+                None
+            }
+        };
+        Self { endpoint, ex, client }
     }
     }
 
 
     /// Stop the client.
     /// Stop the client.
     pub async fn stop(&self) {
     pub async fn stop(&self) {
-        self.client.stop().await
+        if let Some(ref client) = self.client {
+            client.stop().await
+        }
     }
     }
 }
 }
 
 
@@ -227,7 +235,8 @@ impl DarkfiNode {
         let latency = Instant::now();
         let latency = Instant::now();
         let req = JsonRequest::new(method, params.clone());
         let req = JsonRequest::new(method, params.clone());
         let lock = rpc_client.lock().await;
         let lock = rpc_client.lock().await;
-        let rep = lock.client.request(req).await?;
+        let Some(ref client) = lock.client else { return Err(Error::RpcClientStopped) };
+        let rep = client.request(req).await?;
         drop(lock);
         drop(lock);
         let latency = latency.elapsed();
         let latency = latency.elapsed();
         debug!(target: "darkfid::rpc::miner_daemon_request", "Got reply: {:?}", rep);
         debug!(target: "darkfid::rpc::miner_daemon_request", "Got reply: {:?}", rep);
@@ -265,7 +274,7 @@ impl DarkfiNode {
                 };
                 };
                 info!(target: "darkfid::rpc::miner_daemon_request_with_retry", "Connection re-established!");
                 info!(target: "darkfid::rpc::miner_daemon_request_with_retry", "Connection re-established!");
                 // Set the new client as the daemon one
                 // Set the new client as the daemon one
-                rpc_client.client = client;
+                rpc_client.client = Some(client);
                 break;
                 break;
             }
             }
         }
         }

+ 1 - 1
bin/drk/src/main.rs

@@ -23,6 +23,7 @@ use std::{
     sync::Arc,
     sync::Arc,
 };
 };
 
 
+use log::info;
 use prettytable::{format, row, Table};
 use prettytable::{format, row, Table};
 use rand::rngs::OsRng;
 use rand::rngs::OsRng;
 use smol::{fs::read_to_string, stream::StreamExt};
 use smol::{fs::read_to_string, stream::StreamExt};
@@ -50,7 +51,6 @@ use darkfi_sdk::{
     tx::TransactionHash,
     tx::TransactionHash,
 };
 };
 use darkfi_serial::{deserialize_async, serialize_async};
 use darkfi_serial::{deserialize_async, serialize_async};
-use log::info;
 
 
 use drk::{
 use drk::{
     cli_util::{
     cli_util::{

+ 1 - 1
bin/minerd/src/main.rs

@@ -71,6 +71,6 @@ async fn realmain(args: Args, ex: Arc<Executor<'static>>) -> Result<()> {
 
 
     daemon.stop().await?;
     daemon.stop().await?;
 
 
-    info!(target: "minerd", "Shut down successfully");
+    info!(target: "minerd", "Shut down successfully, waitting background threads to stop...");
     Ok(())
     Ok(())
 }
 }