]> Untitled Git - bdk/commitdiff
move scan in setup
authorRiccardo Casatta <riccardo@casatta.it>
Thu, 3 Jun 2021 13:10:31 +0000 (15:10 +0200)
committerRiccardo Casatta <riccardo@casatta.it>
Thu, 3 Jun 2021 13:26:47 +0000 (15:26 +0200)
src/blockchain/rpc.rs

index 32c2d82e74a5afc372f423afe950de153ced390f..1c4c6cf6cb623dc718406a0d5a58668a54af1949 100644 (file)
@@ -130,25 +130,12 @@ impl Blockchain for RpcBlockchain {
         // https://bitcoindevkit.org/descriptors/#compatibility-matrix
         //TODO maybe convenient using import_descriptor for compatible descriptor and import_multi as fallback
         self.client.import_multi(&requests, Some(&options))?;
-        self.sync(stop_gap, database, progress_update)
-    }
 
-    fn sync<D: BatchDatabase, P: 'static + Progress>(
-        &self,
-        _stop_gap: Option<usize>,
-        db: &mut D,
-        progress_update: P,
-    ) -> Result<(), Error> {
         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);
 
-        let mut indexes = HashMap::new();
-        for keykind in &[KeychainKind::External, KeychainKind::Internal] {
-            indexes.insert(*keykind, db.get_last_index(*keykind)?.unwrap_or(0));
-        }
-
         //TODO call rescan in chunks (updating node_synced_height) so that in case of
         // interruption work can be partially recovered
         debug!(
@@ -159,6 +146,22 @@ impl Blockchain for RpcBlockchain {
             .rescan_blockchain(Some(node_synced as usize), Some(current_height as usize))?;
         progress_update.update(1.0, None)?;
 
+        self.set_node_synced_height(current_height)?;
+
+        self.sync(stop_gap, database, progress_update)
+    }
+
+    fn sync<D: BatchDatabase, P: 'static + Progress>(
+        &self,
+        _stop_gap: Option<usize>,
+        db: &mut D,
+        _progress_update: P,
+    ) -> Result<(), Error> {
+        let mut indexes = HashMap::new();
+        for keykind in &[KeychainKind::External, KeychainKind::Internal] {
+            indexes.insert(*keykind, db.get_last_index(*keykind)?.unwrap_or(0));
+        }
+
         let mut known_txs: HashMap<_, _> = db
             .iter_txs(true)?
             .into_iter()
@@ -275,7 +278,6 @@ impl Blockchain for RpcBlockchain {
             db.set_last_index(keykind, index)?;
         }
 
-        self.set_node_synced_height(current_height)?;
         Ok(())
     }