Parcourir la source

wallet: show purple status while DAG is syncing

darkfi il y a 1 an
Parent
commit
d6522d91f0

+ 31 - 26
bin/darkwallet/src/app/mod.rs

@@ -270,35 +270,40 @@ impl App {
             let net3_is_visible = PropertyBool::wrap(&net3, Role::App, "is_visible", 0).unwrap();
 
             while let Ok(data) = recvr.recv().await {
-                let peers_count: u32 = deserialize(&data).unwrap();
+                let (peers_count, is_dag_synced): (u32, bool) = deserialize(&data).unwrap();
 
                 let atom = &mut PropertyAtomicGuard::new();
-                match peers_count {
-                    0 => {
-                        net0_is_visible.set(atom, true);
-                        net1_is_visible.set(atom, false);
-                        net2_is_visible.set(atom, false);
-                        net3_is_visible.set(atom, false);
-                    }
-                    1 => {
-                        net0_is_visible.set(atom, false);
-                        net1_is_visible.set(atom, true);
-                        net2_is_visible.set(atom, false);
-                        net3_is_visible.set(atom, false);
-                    }
-                    2 => {
-                        net0_is_visible.set(atom, false);
-                        net1_is_visible.set(atom, false);
-                        net2_is_visible.set(atom, true);
-                        net3_is_visible.set(atom, false);
-                    }
-                    _ => {
-                        net0_is_visible.set(atom, false);
-                        net1_is_visible.set(atom, false);
-                        net2_is_visible.set(atom, false);
-                        net3_is_visible.set(atom, true);
-                    }
+
+                if peers_count == 0 {
+                    net0_is_visible.set(atom, true);
+                    net1_is_visible.set(atom, false);
+                    net2_is_visible.set(atom, false);
+                    net3_is_visible.set(atom, false);
+                    continue
+                }
+
+                assert!(peers_count > 0);
+                if !is_dag_synced {
+                    net0_is_visible.set(atom, false);
+                    net1_is_visible.set(atom, true);
+                    net2_is_visible.set(atom, false);
+                    net3_is_visible.set(atom, false);
+                    continue
                 }
+
+                assert!(peers_count > 0 && is_dag_synced);
+                if peers_count == 1 {
+                    net0_is_visible.set(atom, false);
+                    net1_is_visible.set(atom, false);
+                    net2_is_visible.set(atom, true);
+                    net3_is_visible.set(atom, false);
+                    continue
+                }
+
+                net0_is_visible.set(atom, false);
+                net1_is_visible.set(atom, false);
+                net2_is_visible.set(atom, false);
+                net3_is_visible.set(atom, true);
             }
         });
         self.tasks.lock().unwrap().push(listen_connect);

+ 4 - 1
bin/darkwallet/src/app/node.rs

@@ -604,7 +604,10 @@ pub fn create_darkirc(name: &str) -> SceneNode {
     node.add_signal(
         "connect",
         "Connections and disconnects",
-        vec![("peers_count", "Peers count", CallArgType::Uint32)],
+        vec![
+            ("peers_count", "Peers Count", CallArgType::Uint32),
+            ("dag_synced", "Is DAG Synced", CallArgType::Bool),
+        ],
     )
     .unwrap();
 

+ 29 - 21
bin/darkwallet/src/plugin/darkirc.rs

@@ -107,6 +107,7 @@ macro_rules! t { ($($arg:tt)*) => { trace!(target: "plugin::darkirc", $($arg)*);
 macro_rules! d { ($($arg:tt)*) => { debug!(target: "plugin::darkirc", $($arg)*); } }
 macro_rules! i { ($($arg:tt)*) => { info!(target: "plugin::darkirc", $($arg)*); } }
 macro_rules! e { ($($arg:tt)*) => { error!(target: "plugin::darkirc", $($arg)*); } }
+macro_rules! w { ($($arg:tt)*) => { warn!(target: "plugin::darkirc", $($arg)*); } }
 
 #[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
 pub struct Privmsg {
@@ -260,8 +261,12 @@ impl DarkIrc {
                 return
             }
 
+            let peers_count = self.p2p.peers_count();
+            self.notify_connect(peers_count, false).await;
+
             // Wait until we have enough connections
-            if self.p2p.hosts().peers().len() < SYNC_MIN_PEERS {
+            if peers_count < SYNC_MIN_PEERS {
+                i!("Connected to {peers_count} peers. Waiting for more connections.");
                 continue
             }
 
@@ -280,10 +285,31 @@ impl DarkIrc {
                 Err(e) => {
                     // TODO: Maybe at this point we should prune or something?
                     // TODO: Or maybe just tell the user to delete the DAG from FS.
-                    warn!("Failed DAG sync: ({e}). Waiting for more connections before retry.");
+                    w!("Failed DAG sync: ({e}). Waiting for more connections before retry.");
                 }
             }
         }
+
+        let peers_count = self.p2p.peers_count();
+        self.notify_connect(peers_count, true).await;
+
+        // Initial sync finished. Now just notify of connection changes
+        loop {
+            // Wait for a channel
+            if let Err(_) = channel_sub.receive().await {
+                e!("There was an error listening for channels. The service closed unexpectedly.");
+                // Not sure what to do here
+                return
+            }
+
+            let peers_count = self.p2p.peers_count();
+            self.notify_connect(peers_count, true).await;
+        }
+    }
+
+    async fn notify_connect(&self, peers_count: usize, is_dag_synced: bool) {
+        let node = self.node.upgrade().unwrap();
+        node.trigger("connect", serialize(&(peers_count as u32, is_dag_synced))).await.unwrap();
     }
 
     async fn relay_events(self: Arc<Self>, ev_sub: Subscription<event_graph::Event>) {
@@ -426,21 +452,6 @@ impl DarkIrc {
 
         self.p2p.broadcast(&EventPut(event)).await;
     }
-
-    async fn connect_notify(self: Arc<Self>, channel_sub: Subscription<DarkFiResult<ChannelPtr>>) {
-        loop {
-            // Wait for a channel
-            if let Err(_) = channel_sub.receive().await {
-                e!("There was an error listening for channels. The service closed unexpectedly.");
-                return
-            }
-
-            let peers_count = self.p2p.hosts().peers().len() as u32;
-
-            let node = self.node.upgrade().unwrap();
-            node.trigger("connect", serialize(&peers_count)).await.unwrap();
-        }
-    }
 }
 
 #[async_trait]
@@ -478,10 +489,7 @@ impl PluginObject for DarkIrc {
         let channel_sub = self.p2p.hosts().subscribe_channel().await;
         let dag_task = ex.spawn(self.clone().dag_sync(channel_sub));
 
-        let channel_sub = self.p2p.hosts().subscribe_channel().await;
-        let connect_notify_task = ex.spawn(self.clone().connect_notify(channel_sub));
-
-        let mut tasks = vec![send_method_task, ev_task, dag_task, connect_notify_task];
+        let mut tasks = vec![send_method_task, ev_task, dag_task];
         tasks.append(&mut on_modify.tasks);
         self.tasks.set(tasks);
     }

+ 5 - 0
src/net/p2p.rs

@@ -219,6 +219,11 @@ impl P2p {
         !self.hosts().peers().is_empty()
     }
 
+    /// The number of connected peers. This means channels which are not seed or refine.
+    pub fn peers_count(&self) -> usize {
+        self.hosts().peers().len()
+    }
+
     /// Return an atomic pointer to the set network settings
     pub fn settings(&self) -> Arc<AsyncRwLock<Settings>> {
         Arc::clone(&self.settings)