Forráskód Böngészése

drk/rpc: fixed genesis block not scanned on subscribe

skoupidi 1 éve
szülő
commit
7b125b38a9
1 módosított fájl, 17 hozzáadás és 2 törlés
  1. 17 2
      bin/drk/src/rpc.rs

+ 17 - 2
bin/drk/src/rpc.rs

@@ -54,10 +54,26 @@ impl Drk {
         endpoint: Url,
         ex: Arc<smol::Executor<'static>>,
     ) -> Result<()> {
+        // Grab last known block
         let rep = self
             .darkfid_daemon_request("blockchain.last_known_block", &JsonValue::Array(vec![]))
             .await?;
-        let last_known = *rep.get::<f64>().unwrap() as u32;
+        let mut last_known = *rep.get::<f64>().unwrap() as u32;
+
+        // Handle genesis(0) block
+        if last_known == 0 {
+            if let Err(e) = self.scan_blocks(true).await {
+                return Err(Error::DatabaseError(format!(
+                    "[subscribe_blocks] Scanning from genesis block failed: {e:?}"
+                )))
+            }
+        }
+
+        // Grab last known block again
+        let rep = self
+            .darkfid_daemon_request("blockchain.last_known_block", &JsonValue::Array(vec![]))
+            .await?;
+        last_known = *rep.get::<f64>().unwrap() as u32;
         let last_scanned = match self.last_scanned_block() {
             Ok(l) => l,
             Err(e) => {
@@ -67,7 +83,6 @@ impl Drk {
             }
         };
 
-        // TODO/FIXME: we can subscribe without scanning the geneseis(0) block,
         // when no other block has been created.
         if last_known != last_scanned {
             eprintln!("Warning: Last scanned block is not the last known block.");