From 297ff3414909ae42d34c31f5c942fc81aa1d60f9 Mon Sep 17 00:00:00 2001 From: valued mammal Date: Sun, 24 Nov 2024 18:34:43 -0500 Subject: [PATCH] test(chain): add test `insert_anchor_without_tx` --- crates/chain/tests/test_tx_graph.rs | 31 +++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/crates/chain/tests/test_tx_graph.rs b/crates/chain/tests/test_tx_graph.rs index 08be91c7a..66fc08fcc 100644 --- a/crates/chain/tests/test_tx_graph.rs +++ b/crates/chain/tests/test_tx_graph.rs @@ -1055,6 +1055,37 @@ fn transactions_inserted_into_tx_graph_are_not_canonical_until_they_have_an_anch assert!(graph.txs_with_no_anchor_or_last_seen().next().is_none()); } +#[test] +fn insert_anchor_without_tx() { + let mut graph = TxGraph::::default(); + + let tx = new_tx(21); + let txid = tx.compute_txid(); + + let anchor = BlockId { + height: 100, + hash: hash!("A"), + }; + + // insert anchor with no corresponding tx + let mut changeset = graph.insert_anchor(txid, anchor); + assert!(changeset.anchors.contains(&(anchor, txid))); + // recover from changeset + let mut recovered = TxGraph::default(); + recovered.apply_changeset(changeset.clone()); + assert_eq!(recovered, graph); + + // now insert tx + let tx = Arc::new(tx); + let graph_changeset = graph.insert_tx(tx.clone()); + assert!(graph_changeset.txs.contains(&tx)); + changeset.merge(graph_changeset); + // recover from changeset again + let mut recovered = TxGraph::default(); + recovered.apply_changeset(changeset); + assert_eq!(recovered, graph); +} + #[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.