]> Untitled Git - bdk/commitdiff
Always get up to chunk_size heights to request headers for
authorLLFourn <lloyd.fourn@gmail.com>
Tue, 2 Nov 2021 05:49:19 +0000 (16:49 +1100)
committerLLFourn <lloyd.fourn@gmail.com>
Tue, 9 Nov 2021 22:07:36 +0000 (09:07 +1100)
src/blockchain/electrum.rs

index ae16d1a7781ccf34e7445a8154329250efa67f1e..3d316a75ce7ba3ac519d45d537c64a50296ea75b 100644 (file)
@@ -112,15 +112,26 @@ impl Blockchain for ElectrumBlockchain {
                 }
 
                 Request::Conftime(conftimereq) => {
-                    let needs_block_height = conftimereq
-                        .request()
-                        .filter_map(|txid| txid_to_height.get(txid).cloned())
-                        .filter(|height| block_times.get(height).is_none())
-                        .take(chunk_size)
-                        .collect::<HashSet<_>>();
-
-                    let new_block_headers =
-                        self.client.batch_block_header(needs_block_height.clone())?;
+                    // collect up to chunk_size heights to fetch from electrum
+                    let needs_block_height = {
+                        let mut needs_block_height_iter = conftimereq
+                            .request()
+                            .filter_map(|txid| txid_to_height.get(txid).cloned())
+                            .filter(|height| block_times.get(height).is_none());
+                        let mut needs_block_height = HashSet::new();
+
+                        while needs_block_height.len() < chunk_size {
+                            match needs_block_height_iter.next() {
+                                Some(height) => needs_block_height.insert(height),
+                                None => break,
+                            };
+                        }
+                        needs_block_height
+                    };
+
+                    let new_block_headers = self
+                        .client
+                        .batch_block_header(needs_block_height.iter().cloned())?;
 
                     for (height, header) in needs_block_height.into_iter().zip(new_block_headers) {
                         block_times.insert(height, header.time);