]> Untitled Git - bdk/commitdiff
[bdk_chain_redesign] Rm `HashSet` from `TxGraph::relevant_heights`
author志宇 <hello@evanlinjin.me>
Thu, 20 Apr 2023 07:56:28 +0000 (15:56 +0800)
committer志宇 <hello@evanlinjin.me>
Thu, 20 Apr 2023 07:56:28 +0000 (15:56 +0800)
The `HashSet` was used for iterating without duplicate items. However,
since `anchors` is a `BTreeSet`, heights are in order. So a single
variable tracking last height will be sufficient.

crates/chain/src/tx_graph.rs

index e16ae88d60adec6afc8fe5f7dd420a939ccff737..a1ddbf875b1e6063637db532ccf2ed7aa105fda9 100644 (file)
@@ -603,11 +603,17 @@ impl<A: Clone + Ord> TxGraph<A> {
 impl<A: Anchor> TxGraph<A> {
     /// Get all heights that are relevant to the graph.
     pub fn relevant_heights(&self) -> impl DoubleEndedIterator<Item = u32> + '_ {
-        let mut visited = HashSet::new();
+        let mut last_height = Option::<u32>::None;
         self.anchors
             .iter()
             .map(|(a, _)| a.anchor_block().height)
-            .filter(move |&h| visited.insert(h))
+            .filter(move |&height| {
+                let is_unique = Some(height) != last_height;
+                if is_unique {
+                    last_height = Some(height);
+                }
+                is_unique
+            })
     }
 
     /// Get the position of the transaction in `chain` with tip `chain_tip`.