]> Untitled Git - bdk/commitdiff
fix(esplora): reuse returned height instead of zipping
author志宇 <hello@evanlinjin.me>
Mon, 29 Jan 2024 09:43:41 +0000 (18:43 +0900)
committer志宇 <hello@evanlinjin.me>
Mon, 29 Jan 2024 09:51:03 +0000 (17:51 +0800)
crates/esplora/src/async_ext.rs
crates/esplora/src/blocking_ext.rs

index ee0634360ed7cf476f5175c153b82eae7f6e0934..4a6196be7a55e45d968a80c61a9872358e1c6ad2 100644 (file)
@@ -85,20 +85,20 @@ impl EsploraAsyncExt for esplora_client::AsyncClient {
         local_tip: CheckPoint,
         request_heights: impl IntoIterator<IntoIter = impl Iterator<Item = u32> + Send> + Send,
     ) -> Result<local_chain::Update, Error> {
-        let new_tip_height = self.get_height().await?;
-
         // Atomically fetch latest blocks from Esplora. This way, we avoid creating an update with
         // an inconsistent set of blocks (assuming that a reorg depth cannot be greater than the
         // latest blocks fetched).
-        let mut fetched_blocks = {
-            let heights = (0..=new_tip_height).rev();
-            let hashes = self
-                .get_blocks(Some(new_tip_height))
-                .await?
-                .into_iter()
-                .map(|b| b.id);
-            heights.zip(hashes).collect::<BTreeMap<u32, BlockHash>>()
-        };
+        let mut fetched_blocks = self
+            .get_blocks(None)
+            .await?
+            .into_iter()
+            .map(|b| (b.time.height, b.id))
+            .collect::<BTreeMap<u32, BlockHash>>();
+        let new_tip_height = fetched_blocks
+            .keys()
+            .last()
+            .copied()
+            .expect("must have atleast one block");
 
         // fetch blocks of heights that the caller is interested in, reusing latest blocks that are
         // already fetched.
index 61660833f114dd5c9b57377138017308f06cdacc..0764be050cbfe95c5e0fdd325cafcffc29eeee50 100644 (file)
@@ -78,19 +78,19 @@ impl EsploraExt for esplora_client::BlockingClient {
         local_tip: CheckPoint,
         request_heights: impl IntoIterator<Item = u32>,
     ) -> Result<local_chain::Update, Error> {
-        let new_tip_height = self.get_height()?;
-
         // Atomically fetch latest blocks from Esplora. This way, we avoid creating an update with
         // an inconsistent set of blocks (assuming that a reorg depth cannot be greater than the
         // latest blocks fetched).
-        let mut fetched_blocks = {
-            let heights = (0..=new_tip_height).rev();
-            let hashes = self
-                .get_blocks(Some(new_tip_height))?
-                .into_iter()
-                .map(|b| b.id);
-            heights.zip(hashes).collect::<BTreeMap<u32, BlockHash>>()
-        };
+        let mut fetched_blocks = self
+            .get_blocks(None)?
+            .into_iter()
+            .map(|b| (b.time.height, b.id))
+            .collect::<BTreeMap<u32, BlockHash>>();
+        let new_tip_height = fetched_blocks
+            .keys()
+            .last()
+            .copied()
+            .expect("must atleast have one block");
 
         // fetch blocks of heights that the caller is interested in, reusing latest blocks that are
         // already fetched.