Skip to content

Commit

Permalink
test(tx_graph): Add test list_canonical_transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
ValuedMammal committed Jun 4, 2024
1 parent 6f76215 commit 2aaf5cc
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions crates/chain/tests/test_tx_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,45 @@ fn update_last_seen_unconfirmed() {
);
}

#[test]
fn list_canonical_transactions() {
// Transactions inserted into a TxGraph won't be considered canonical
// until given an anchor (in best chain) or a last_seen time.
let txs = vec![new_tx(0), new_tx(1)];
let txids: Vec<Txid> = txs.iter().map(Transaction::txid).collect();

// graph
let mut graph = TxGraph::<BlockId>::new(txs);
let full_txs: Vec<_> = graph.full_txs().collect();
assert_eq!(full_txs.len(), 2);

// chain
let blocks: BTreeMap<u32, BlockHash> = [(0, h!("g")), (1, h!("A")), (2, h!("B"))]
.into_iter()
.collect();
let chain = LocalChain::from_blocks(blocks).unwrap();
let canonical_txs: Vec<_> = graph
.list_canonical_transactions(&chain, chain.tip().block_id())
.collect();
assert!(canonical_txs.is_empty());

// tx0 with seen_at should be returned by canonical txs
let _ = graph.insert_seen_at(txids[0], 2);
let mut canonical_txs = graph.list_canonical_transactions(&chain, chain.tip().block_id());
assert_eq!(
canonical_txs.next().map(|tx| tx.tx_node.txid).unwrap(),
txids[0]
);
drop(canonical_txs);

// tx1 with anchor is also canonical
let _ = graph.insert_anchor(txids[1], block_id!(2, "B"));
let mut canonical_txs = graph.list_canonical_transactions(&chain, chain.tip().block_id());
assert!(canonical_txs
.find(|tx| tx.tx_node.txid == txids[1])
.is_some());
}

#[test]
/// The `map_anchors` allow a caller to pass a function to reconstruct the [`TxGraph`] with any [`Anchor`],
/// even though the function is non-deterministic.
Expand Down

0 comments on commit 2aaf5cc

Please sign in to comment.