]> Untitled Git - bdk/commitdiff
Fix the `last_derivation_index` calculation
authorAlekos Filini <alekos.filini@gmail.com>
Fri, 4 Sep 2020 19:28:35 +0000 (21:28 +0200)
committerAlekos Filini <alekos.filini@gmail.com>
Fri, 4 Sep 2020 19:53:31 +0000 (21:53 +0200)
It should be set to `0` if not transactions are found during sync.

Closes #44

src/blockchain/compact_filters/mod.rs
src/blockchain/utils.rs

index e5561ee9b37c32922dffc1abbfbe7809f104ff8c..e528db626b1493f4d4c26af50684fda7f9095a09 100644 (file)
@@ -175,8 +175,8 @@ impl CompactFilters {
         tx: &Transaction,
         height: Option<u32>,
         timestamp: u64,
-        internal_max_deriv: &mut u32,
-        external_max_deriv: &mut u32,
+        internal_max_deriv: &mut Option<u32>,
+        external_max_deriv: &mut Option<u32>,
     ) -> Result<(), Error> {
         let mut updates = database.begin_batch();
 
@@ -216,10 +216,14 @@ impl CompactFilters {
                 })?;
                 incoming += output.value;
 
-                if script_type == ScriptType::Internal && child > *internal_max_deriv {
-                    *internal_max_deriv = child;
-                } else if script_type == ScriptType::External && child > *external_max_deriv {
-                    *external_max_deriv = child;
+                if script_type == ScriptType::Internal
+                    && (internal_max_deriv.is_none() || child > internal_max_deriv.unwrap_or(0))
+                {
+                    *internal_max_deriv = Some(child);
+                } else if script_type == ScriptType::External
+                    && (external_max_deriv.is_none() || child > external_max_deriv.unwrap_or(0))
+                {
+                    *external_max_deriv = Some(child);
                 }
             }
         }
@@ -413,8 +417,8 @@ impl OnlineBlockchain for CompactFiltersBlockchain {
 
         first_peer.ask_for_mempool()?;
 
-        let mut internal_max_deriv = 0;
-        let mut external_max_deriv = 0;
+        let mut internal_max_deriv = None;
+        let mut external_max_deriv = None;
 
         for (height, block) in inner.headers.iter_full_blocks()? {
             for tx in &block.txdata {
@@ -440,14 +444,14 @@ impl OnlineBlockchain for CompactFiltersBlockchain {
         }
 
         let current_ext = database.get_last_index(ScriptType::External)?.unwrap_or(0);
-        let first_ext_new = external_max_deriv as u32 + 1;
+        let first_ext_new = external_max_deriv.map(|x| x + 1).unwrap_or(0);
         if first_ext_new > current_ext {
             info!("Setting external index to {}", first_ext_new);
             database.set_last_index(ScriptType::External, first_ext_new)?;
         }
 
         let current_int = database.get_last_index(ScriptType::Internal)?.unwrap_or(0);
-        let first_int_new = internal_max_deriv + 1;
+        let first_int_new = internal_max_deriv.map(|x| x + 1).unwrap_or(0);
         if first_int_new > current_int {
             info!("Setting internal index to {}", first_int_new);
             database.set_last_index(ScriptType::Internal, first_int_new)?;
index 63bd5a7b4948f7fddfe4b7aa45da587eba5d3ed4..0ae3d53120c798d04a9cc15acf63c4f4037b0a54 100644 (file)
@@ -88,7 +88,7 @@ pub trait ElectrumLikeSync {
         database.commit_batch(del_batch)?;
 
         // maximum derivation index for a change address that we've seen during sync
-        let mut change_max_deriv = 0;
+        let mut change_max_deriv = None;
 
         let mut already_checked: HashSet<Script> = HashSet::new();
         let mut to_check_later = VecDeque::with_capacity(batch_query_size);
@@ -104,7 +104,7 @@ pub trait ElectrumLikeSync {
 
         let mut iterating_external = true;
         let mut index = 0;
-        let mut last_found = 0;
+        let mut last_found = None;
         while !to_check_later.is_empty() {
             trace!("to_check_later size {}", to_check_later.len());
 
@@ -116,7 +116,7 @@ pub trait ElectrumLikeSync {
                 trace!("received history for {:?}, size {}", script, history.len());
 
                 if !history.is_empty() {
-                    last_found = index;
+                    last_found = Some(index);
 
                     let mut check_later_scripts = maybe_await!(self.check_history(
                         database,
@@ -134,9 +134,9 @@ pub trait ElectrumLikeSync {
             }
 
             match iterating_external {
-                true if index - last_found >= stop_gap => iterating_external = false,
+                true if index - last_found.unwrap_or(0) >= stop_gap => iterating_external = false,
                 true => {
-                    trace!("pushing one more batch from `iter_scriptpubkeys`. index = {}, last_found = {}, stop_gap = {}", index, last_found, stop_gap);
+                    trace!("pushing one more batch from `iter_scriptpubkeys`. index = {}, last_found = {:?}, stop_gap = {}", index, last_found, stop_gap);
 
                     let chunk: Vec<Script> =
                         iter_scriptpubkeys.by_ref().take(batch_query_size).collect();
@@ -178,14 +178,14 @@ pub trait ElectrumLikeSync {
         }
 
         let current_ext = database.get_last_index(ScriptType::External)?.unwrap_or(0);
-        let first_ext_new = last_found as u32 + 1;
+        let first_ext_new = last_found.map(|x| x + 1).unwrap_or(0) as u32;
         if first_ext_new > current_ext {
             info!("Setting external index to {}", first_ext_new);
             database.set_last_index(ScriptType::External, first_ext_new)?;
         }
 
         let current_int = database.get_last_index(ScriptType::Internal)?.unwrap_or(0);
-        let first_int_new = change_max_deriv + 1;
+        let first_int_new = change_max_deriv.map(|x| x + 1).unwrap_or(0);
         if first_int_new > current_int {
             info!("Setting internal index to {}", first_int_new);
             database.set_last_index(ScriptType::Internal, first_int_new)?;
@@ -202,7 +202,7 @@ pub trait ElectrumLikeSync {
         txid: &Txid,
         height: Option<u32>,
         cur_script: &Script,
-        change_max_deriv: &mut u32,
+        change_max_deriv: &mut Option<u32>,
     ) -> Result<Vec<Script>, Error> {
         debug!(
             "check_tx_and_descendant of {}, height: {:?}, script: {}",
@@ -297,8 +297,10 @@ pub trait ElectrumLikeSync {
                 }
 
                 // derive as many change addrs as external addresses that we've seen
-                if script_type == ScriptType::Internal && child > *change_max_deriv {
-                    *change_max_deriv = child;
+                if script_type == ScriptType::Internal
+                    && (change_max_deriv.is_none() || child > change_max_deriv.unwrap_or(0))
+                {
+                    *change_max_deriv = Some(child);
                 }
             }
         }
@@ -325,7 +327,7 @@ pub trait ElectrumLikeSync {
         database: &mut D,
         script_pubkey: Script,
         txs: Vec<ELSGetHistoryRes>,
-        change_max_deriv: &mut u32,
+        change_max_deriv: &mut Option<u32>,
     ) -> Result<Vec<Script>, Error> {
         let mut to_check_later = Vec::new();