]> Untitled Git - bdk/commitdiff
[rpc] rescan in chunks of 10_000 blocks
authorRiccardo Casatta <riccardo@casatta.it>
Mon, 13 Sep 2021 12:52:22 +0000 (14:52 +0200)
committerRiccardo Casatta <riccardo@casatta.it>
Fri, 17 Sep 2021 13:19:52 +0000 (15:19 +0200)
src/blockchain/rpc.rs

index 050e5ca02d6311e11ec3972cc7c17a4f63468095..3ac67e4dda9372b83481a1c4ef7477bd49ef6873 100644 (file)
@@ -169,22 +169,25 @@ impl Blockchain for RpcBlockchain {
         //TODO maybe convenient using import_descriptor for compatible descriptor and import_multi as fallback
         self.client.import_multi(&requests, Some(&options))?;
 
-        let current_height = self.get_height()?;
+        loop {
+            let current_height = self.get_height()?;
 
-        // min because block invalidate may cause height to go down
-        let node_synced = self.get_node_synced_height()?.min(current_height);
+            // min because block invalidate may cause height to go down
+            let node_synced = self.get_node_synced_height()?.min(current_height);
 
-        //TODO call rescan in chunks (updating node_synced_height) so that in case of
-        // interruption work can be partially recovered
-        debug!(
-            "rescan_blockchain from:{} to:{}",
-            node_synced, current_height
-        );
-        self.client
-            .rescan_blockchain(Some(node_synced as usize), Some(current_height as usize))?;
-        progress_update.update(1.0, None)?;
+            let sync_up_to = node_synced.saturating_add(10_000).min(current_height);
+
+            debug!("rescan_blockchain from:{} to:{}", node_synced, sync_up_to);
+            self.client
+                .rescan_blockchain(Some(node_synced as usize), Some(sync_up_to as usize))?;
+            progress_update.update((sync_up_to as f32) / (current_height as f32), None)?;
 
-        self.set_node_synced_height(current_height)?;
+            self.set_node_synced_height(sync_up_to)?;
+
+            if sync_up_to == current_height {
+                break;
+            }
+        }
 
         self.sync(database, progress_update)
     }