Skip to content

Commit 15d9579

Browse files
committed
test(tx_graph): Add test list_canonical_txs
1 parent 7565b65 commit 15d9579

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

crates/chain/tests/test_tx_graph.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,52 @@ fn update_last_seen_unconfirmed() {
10981098
);
10991099
}
11001100

1101+
#[test]
1102+
fn list_canonical_txs() {
1103+
use bdk_chain::local_chain::CheckPoint;
1104+
let mut graph = TxGraph::<BlockId>::default();
1105+
1106+
// insert three new txs
1107+
// all graph transactions are present, but not canonical
1108+
let txs = vec![new_tx(0), new_tx(1), new_tx(2)];
1109+
let txids: Vec<_> = txs.iter().map(Transaction::txid).collect();
1110+
for tx in txs {
1111+
let _ = graph.insert_tx(tx);
1112+
}
1113+
let full_txs: Vec<_> = graph.full_txs().collect();
1114+
assert_eq!(full_txs.len(), 3);
1115+
1116+
let cp = CheckPoint::from_block_ids(
1117+
[(0, BlockHash::all_zeros()), (2, BlockHash::all_zeros())]
1118+
.into_iter()
1119+
.map(BlockId::from),
1120+
)
1121+
.unwrap();
1122+
let anchor: BlockId = cp.iter().next().unwrap().block_id();
1123+
let chain = LocalChain::from_tip(cp).unwrap();
1124+
1125+
let canonical_txs: Vec<_> = graph
1126+
.canonical_transactions(&chain, chain.tip().block_id())
1127+
.collect();
1128+
assert!(canonical_txs.is_empty());
1129+
1130+
// insert seen_at for tx0, it should be returned by `canonical_transactions`
1131+
let _ = graph.insert_seen_at(txids[0], 0);
1132+
let canonical_txs: Vec<_> = graph
1133+
.canonical_transactions(&chain, chain.tip().block_id())
1134+
.collect();
1135+
assert_eq!(canonical_txs.len(), 1);
1136+
assert_eq!(canonical_txs.first().unwrap().tx_node.txid, txids[0]);
1137+
1138+
// insert anchor for tx1, it is now also canonical
1139+
let _ = graph.insert_anchor(txids[1], anchor);
1140+
let canonical_txs: BTreeSet<_> = graph
1141+
.canonical_transactions(&chain, chain.tip().block_id())
1142+
.collect();
1143+
assert_eq!(canonical_txs.len(), 2);
1144+
assert_eq!(canonical_txs.first().unwrap().tx_node.txid, txids[1]);
1145+
}
1146+
11011147
#[test]
11021148
/// The `map_anchors` allow a caller to pass a function to reconstruct the [`TxGraph`] with any [`Anchor`],
11031149
/// even though the function is non-deterministic.

0 commit comments

Comments
 (0)