Skip to content

Commit 2aaf5cc

Browse files
committed
test(tx_graph): Add test list_canonical_transactions
1 parent 6f76215 commit 2aaf5cc

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

crates/chain/tests/test_tx_graph.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,45 @@ fn update_last_seen_unconfirmed() {
10911091
);
10921092
}
10931093

1094+
#[test]
1095+
fn list_canonical_transactions() {
1096+
// Transactions inserted into a TxGraph won't be considered canonical
1097+
// until given an anchor (in best chain) or a last_seen time.
1098+
let txs = vec![new_tx(0), new_tx(1)];
1099+
let txids: Vec<Txid> = txs.iter().map(Transaction::txid).collect();
1100+
1101+
// graph
1102+
let mut graph = TxGraph::<BlockId>::new(txs);
1103+
let full_txs: Vec<_> = graph.full_txs().collect();
1104+
assert_eq!(full_txs.len(), 2);
1105+
1106+
// chain
1107+
let blocks: BTreeMap<u32, BlockHash> = [(0, h!("g")), (1, h!("A")), (2, h!("B"))]
1108+
.into_iter()
1109+
.collect();
1110+
let chain = LocalChain::from_blocks(blocks).unwrap();
1111+
let canonical_txs: Vec<_> = graph
1112+
.list_canonical_transactions(&chain, chain.tip().block_id())
1113+
.collect();
1114+
assert!(canonical_txs.is_empty());
1115+
1116+
// tx0 with seen_at should be returned by canonical txs
1117+
let _ = graph.insert_seen_at(txids[0], 2);
1118+
let mut canonical_txs = graph.list_canonical_transactions(&chain, chain.tip().block_id());
1119+
assert_eq!(
1120+
canonical_txs.next().map(|tx| tx.tx_node.txid).unwrap(),
1121+
txids[0]
1122+
);
1123+
drop(canonical_txs);
1124+
1125+
// tx1 with anchor is also canonical
1126+
let _ = graph.insert_anchor(txids[1], block_id!(2, "B"));
1127+
let mut canonical_txs = graph.list_canonical_transactions(&chain, chain.tip().block_id());
1128+
assert!(canonical_txs
1129+
.find(|tx| tx.tx_node.txid == txids[1])
1130+
.is_some());
1131+
}
1132+
10941133
#[test]
10951134
/// The `map_anchors` allow a caller to pass a function to reconstruct the [`TxGraph`] with any [`Anchor`],
10961135
/// even though the function is non-deterministic.

0 commit comments

Comments
 (0)