[bdk_chain_redesign] Add tests for TxGraph::relevant_heights

This commit is contained in:
志宇 2023-04-19 16:14:52 +08:00
parent 8e36a2e5f6
commit 7175a82c04
No known key found for this signature in database
GPG Key ID: F6345C9837C2BDE8
2 changed files with 69 additions and 1 deletions

View File

@ -602,7 +602,7 @@ impl<A: Clone + Ord> TxGraph<A> {
impl<A: Anchor> TxGraph<A> { impl<A: Anchor> TxGraph<A> {
/// Get all heights that are relevant to the graph. /// Get all heights that are relevant to the graph.
pub fn relevant_heights(&self) -> impl Iterator<Item = u32> + '_ { pub fn relevant_heights(&self) -> impl DoubleEndedIterator<Item = u32> + '_ {
let mut visited = HashSet::new(); let mut visited = HashSet::new();
self.anchors self.anchors
.iter() .iter()

View File

@ -10,6 +10,7 @@ use bitcoin::{
hashes::Hash, BlockHash, OutPoint, PackedLockTime, Script, Transaction, TxIn, TxOut, Txid, hashes::Hash, BlockHash, OutPoint, PackedLockTime, Script, Transaction, TxIn, TxOut, Txid,
}; };
use core::iter; use core::iter;
use std::vec;
#[test] #[test]
fn insert_txouts() { fn insert_txouts() {
@ -781,3 +782,70 @@ fn test_chain_spends() {
.get_chain_position(&local_chain, tip, tx_2.txid()) .get_chain_position(&local_chain, tip, tx_2.txid())
.is_none()); .is_none());
} }
#[test]
fn test_relevant_heights() {
let mut graph = TxGraph::<BlockId>::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<_>>(),
vec![],
"no anchors in graph"
);
let _ = graph.insert_anchor(
tx1.txid(),
BlockId {
height: 3,
hash: h!("3a"),
},
);
assert_eq!(
graph.relevant_heights().collect::<Vec<_>>(),
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<_>>(),
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<_>>(),
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<_>>(),
vec![3, 4, 5],
"anchor for non-existant tx is inserted at height 5, must still be in relevant heights",
);
}