diff --git a/crates/chain/src/tx_graph.rs b/crates/chain/src/tx_graph.rs index 5d06bbda..e16ae88d 100644 --- a/crates/chain/src/tx_graph.rs +++ b/crates/chain/src/tx_graph.rs @@ -602,7 +602,7 @@ impl TxGraph { impl TxGraph { /// Get all heights that are relevant to the graph. - pub fn relevant_heights(&self) -> impl Iterator + '_ { + pub fn relevant_heights(&self) -> impl DoubleEndedIterator + '_ { let mut visited = HashSet::new(); self.anchors .iter() diff --git a/crates/chain/tests/test_tx_graph.rs b/crates/chain/tests/test_tx_graph.rs index 965bb259..20b3e27f 100644 --- a/crates/chain/tests/test_tx_graph.rs +++ b/crates/chain/tests/test_tx_graph.rs @@ -10,6 +10,7 @@ use bitcoin::{ hashes::Hash, BlockHash, OutPoint, PackedLockTime, Script, Transaction, TxIn, TxOut, Txid, }; use core::iter; +use std::vec; #[test] fn insert_txouts() { @@ -781,3 +782,70 @@ fn test_chain_spends() { .get_chain_position(&local_chain, tip, tx_2.txid()) .is_none()); } + +#[test] +fn test_relevant_heights() { + let mut graph = TxGraph::::default(); + + let tx1 = common::new_tx(1); + let tx2 = common::new_tx(2); + + let _ = graph.insert_tx(tx1.clone()); + assert_eq!( + graph.relevant_heights().collect::>(), + vec![], + "no anchors in graph" + ); + + let _ = graph.insert_anchor( + tx1.txid(), + BlockId { + height: 3, + hash: h!("3a"), + }, + ); + assert_eq!( + graph.relevant_heights().collect::>(), + vec![3], + "one anchor at height 3" + ); + + let _ = graph.insert_anchor( + tx1.txid(), + BlockId { + height: 3, + hash: h!("3b"), + }, + ); + assert_eq!( + graph.relevant_heights().collect::>(), + vec![3], + "introducing duplicate anchor at height 3, must not iterate over duplicate heights" + ); + + let _ = graph.insert_anchor( + tx1.txid(), + BlockId { + height: 4, + hash: h!("4a"), + }, + ); + assert_eq!( + graph.relevant_heights().collect::>(), + vec![3, 4], + "anchors in height 3 and now 4" + ); + + let _ = graph.insert_anchor( + tx2.txid(), + BlockId { + height: 5, + hash: h!("5a"), + }, + ); + assert_eq!( + graph.relevant_heights().collect::>(), + vec![3, 4, 5], + "anchor for non-existant tx is inserted at height 5, must still be in relevant heights", + ); +}