From 568a36642c43d74132339a4e42dd41c545a764e1 Mon Sep 17 00:00:00 2001 From: valued mammal Date: Sat, 7 Jun 2025 10:43:11 -0400 Subject: [PATCH] refactor(esplora): in debug build assert that `latest_blocks` is not empty --- crates/esplora/src/async_ext.rs | 10 ++++++++-- crates/esplora/src/blocking_ext.rs | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/crates/esplora/src/async_ext.rs b/crates/esplora/src/async_ext.rs index 4ea697c9..dc82063b 100644 --- a/crates/esplora/src/async_ext.rs +++ b/crates/esplora/src/async_ext.rs @@ -205,10 +205,16 @@ async fn fetch_block( // We avoid fetching blocks higher than previously fetched `latest_blocks` as the local chain // tip is used to signal for the last-synced-up-to-height. - if let Some(tip_height) = latest_blocks.keys().last().copied() { - if height > tip_height { + match latest_blocks.keys().last().copied() { + None => { + debug_assert!(false, "`latest_blocks` should not be empty"); return Ok(None); } + Some(tip_height) => { + if height > tip_height { + return Ok(None); + } + } } Ok(Some(client.get_block_hash(height).await?)) diff --git a/crates/esplora/src/blocking_ext.rs b/crates/esplora/src/blocking_ext.rs index 6977c8f0..6c4966bf 100644 --- a/crates/esplora/src/blocking_ext.rs +++ b/crates/esplora/src/blocking_ext.rs @@ -190,10 +190,16 @@ fn fetch_block( // We avoid fetching blocks higher than previously fetched `latest_blocks` as the local chain // tip is used to signal for the last-synced-up-to-height. - if let Some(tip_height) = latest_blocks.keys().last().copied() { - if height > tip_height { + match latest_blocks.keys().last().copied() { + None => { + debug_assert!(false, "`latest_blocks` should not be empty"); return Ok(None); } + Some(tip_height) => { + if height > tip_height { + return Ok(None); + } + } } Ok(Some(client.get_block_hash(height)?)) -- 2.49.0