diff --git a/crates/chain/src/tx_graph.rs b/crates/chain/src/tx_graph.rs index 95797f28..73d602df 100644 --- a/crates/chain/src/tx_graph.rs +++ b/crates/chain/src/tx_graph.rs @@ -1145,7 +1145,7 @@ impl AsRef> for TxGraph { pub struct TxDescendants<'g, A, F> { graph: &'g TxGraph, visited: HashSet, - stack: Vec<(usize, Txid)>, + queue: VecDeque<(usize, Txid)>, filter_map: F, } @@ -1156,7 +1156,7 @@ impl<'g, A, F> TxDescendants<'g, A, F> { Self { graph, visited: Default::default(), - stack: [(0, txid)].into(), + queue: [(0, txid)].into(), filter_map, } } @@ -1166,10 +1166,10 @@ impl<'g, A, F> TxDescendants<'g, A, F> { let mut descendants = Self { graph, visited: Default::default(), - stack: Default::default(), + queue: Default::default(), filter_map, }; - descendants.populate_stack(1, txid); + descendants.populate_queue(1, txid); descendants } @@ -1186,7 +1186,7 @@ impl<'g, A, F> TxDescendants<'g, A, F> { Self { graph, visited: Default::default(), - stack: txids.into_iter().map(|txid| (0, txid)).collect(), + queue: txids.into_iter().map(|txid| (0, txid)).collect(), filter_map, } } @@ -1205,25 +1205,25 @@ impl<'g, A, F> TxDescendants<'g, A, F> { let mut descendants = Self { graph, visited: Default::default(), - stack: Default::default(), + queue: Default::default(), filter_map, }; for txid in txids { - descendants.populate_stack(1, txid); + descendants.populate_queue(1, txid); } descendants } } impl<'g, A, F> TxDescendants<'g, A, F> { - fn populate_stack(&mut self, depth: usize, txid: Txid) { + fn populate_queue(&mut self, depth: usize, txid: Txid) { let spend_paths = self .graph .spends .range(tx_outpoint_range(txid)) .flat_map(|(_, spends)| spends) .map(|&txid| (depth, txid)); - self.stack.extend(spend_paths); + self.queue.extend(spend_paths); } } @@ -1235,8 +1235,8 @@ where fn next(&mut self) -> Option { let (op_spends, txid, item) = loop { - // we have exhausted all paths when stack is empty - let (op_spends, txid) = self.stack.pop()?; + // we have exhausted all paths when queue is empty + let (op_spends, txid) = self.queue.pop_front()?; // we do not want to visit the same transaction twice if self.visited.insert(txid) { // ignore paths when user filters them out @@ -1246,7 +1246,7 @@ where } }; - self.populate_stack(op_spends + 1, txid); + self.populate_queue(op_spends + 1, txid); Some(item) } } diff --git a/crates/chain/tests/test_tx_graph.rs b/crates/chain/tests/test_tx_graph.rs index 4c68f510..36a27a58 100644 --- a/crates/chain/tests/test_tx_graph.rs +++ b/crates/chain/tests/test_tx_graph.rs @@ -610,7 +610,7 @@ fn test_descendants_no_repeat() { .collect::>(); let mut graph = TxGraph::<()>::default(); - let mut expected_txids = BTreeSet::new(); + let mut expected_txids = Vec::new(); // these are NOT descendants of `tx_a` for tx in txs_not_connected { @@ -625,19 +625,14 @@ fn test_descendants_no_repeat() { .chain(core::iter::once(&tx_e)) { let _ = graph.insert_tx(tx.clone()); - assert!(expected_txids.insert(tx.txid())); + expected_txids.push(tx.txid()); } let descendants = graph .walk_descendants(tx_a.txid(), |_, txid| Some(txid)) .collect::>(); - assert_eq!(descendants.len(), expected_txids.len()); - - for txid in descendants { - assert!(expected_txids.remove(&txid)); - } - assert!(expected_txids.is_empty()); + assert_eq!(descendants, expected_txids); } #[test]