From 63fa710319f231f7f28d92cb49b0399f3250c9da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BF=97=E5=AE=87?= Date: Mon, 29 Jan 2024 18:43:41 +0900 Subject: [PATCH] fix(esplora): reuse returned height instead of zipping --- crates/esplora/src/async_ext.rs | 22 +++++++++++----------- crates/esplora/src/blocking_ext.rs | 20 ++++++++++---------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/crates/esplora/src/async_ext.rs b/crates/esplora/src/async_ext.rs index ee063436..4a6196be 100644 --- a/crates/esplora/src/async_ext.rs +++ b/crates/esplora/src/async_ext.rs @@ -85,20 +85,20 @@ impl EsploraAsyncExt for esplora_client::AsyncClient { local_tip: CheckPoint, request_heights: impl IntoIterator + Send> + Send, ) -> Result { - 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::>() - }; + let mut fetched_blocks = self + .get_blocks(None) + .await? + .into_iter() + .map(|b| (b.time.height, b.id)) + .collect::>(); + 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. diff --git a/crates/esplora/src/blocking_ext.rs b/crates/esplora/src/blocking_ext.rs index 61660833..0764be05 100644 --- a/crates/esplora/src/blocking_ext.rs +++ b/crates/esplora/src/blocking_ext.rs @@ -78,19 +78,19 @@ impl EsploraExt for esplora_client::BlockingClient { local_tip: CheckPoint, request_heights: impl IntoIterator, ) -> Result { - 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::>() - }; + let mut fetched_blocks = self + .get_blocks(None)? + .into_iter() + .map(|b| (b.time.height, b.id)) + .collect::>(); + 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.