]> Untitled Git - bdk/commitdiff
test(chain): add test `insert_anchor_without_tx`
authorvalued mammal <valuedmammal@protonmail.com>
Sun, 24 Nov 2024 23:34:43 +0000 (18:34 -0500)
committer志宇 <hello@evanlinjin.me>
Mon, 25 Nov 2024 23:29:47 +0000 (10:29 +1100)
crates/chain/tests/test_tx_graph.rs

index 08be91c7ab0a9147661a107bfb1e8cd8d59170cc..66fc08fcc316959e5baf6abaa6f2d0327809b474 100644 (file)
@@ -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::<BlockId>::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.