]> Untitled Git - bdk/commitdiff
feat(tx_graph): Add method `txs_with_no_anchor_or_last_seen`
authorvalued mammal <valuedmammal@protonmail.com>
Tue, 25 Jun 2024 16:46:53 +0000 (12:46 -0400)
committervalued mammal <valuedmammal@protonmail.com>
Sun, 30 Jun 2024 14:08:54 +0000 (10:08 -0400)
crates/chain/src/tx_graph.rs
crates/chain/tests/test_tx_graph.rs
crates/wallet/src/wallet/mod.rs

index 965174fe5fc1db8bb11a59395dacf72457a85e47..199c85ea91428bfac8bb4987a2c85ca60c4298ca 100644 (file)
@@ -258,6 +258,19 @@ impl<A> TxGraph<A> {
             })
     }
 
+    /// Iterate over graph transactions with no anchors or last-seen.
+    pub fn txs_with_no_anchor_or_last_seen(
+        &self,
+    ) -> impl Iterator<Item = TxNode<'_, Arc<Transaction>, A>> {
+        self.full_txs().filter_map(|tx| {
+            if tx.anchors.is_empty() && tx.last_seen_unconfirmed.is_none() {
+                Some(tx)
+            } else {
+                None
+            }
+        })
+    }
+
     /// Get a transaction by txid. This only returns `Some` for full transactions.
     ///
     /// Refer to [`get_txout`] for getting a specific [`TxOut`].
index 907fec27e8fa1787be4939891e00972ebf0ef19a..c46ab169aa2fc5de23b179107b40d4690832951c 100644 (file)
@@ -1127,6 +1127,8 @@ fn transactions_inserted_into_tx_graph_are_not_canonical_until_they_have_an_anch
     let mut graph = TxGraph::<BlockId>::new(txs);
     let full_txs: Vec<_> = graph.full_txs().collect();
     assert_eq!(full_txs.len(), 2);
+    let unseen_txs: Vec<_> = graph.txs_with_no_anchor_or_last_seen().collect();
+    assert_eq!(unseen_txs.len(), 2);
 
     // chain
     let blocks: BTreeMap<u32, BlockHash> = [(0, h!("g")), (1, h!("A")), (2, h!("B"))]
@@ -1154,6 +1156,7 @@ fn transactions_inserted_into_tx_graph_are_not_canonical_until_they_have_an_anch
         .map(|tx| tx.tx_node.txid)
         .collect();
     assert!(canonical_txids.contains(&txids[1]));
+    assert!(graph.txs_with_no_anchor_or_last_seen().next().is_none());
 }
 
 #[test]
index 235f600b62835d72b02c11a91c894993006b9d16..105d07f83818b1d0961589a2f314db684525e54f 100644 (file)
@@ -27,7 +27,7 @@ use bdk_chain::{
         self, ApplyHeaderError, CannotConnectError, CheckPoint, CheckPointIter, LocalChain,
     },
     spk_client::{FullScanRequest, FullScanResult, SyncRequest, SyncResult},
-    tx_graph::{CanonicalTx, TxGraph},
+    tx_graph::{CanonicalTx, TxGraph, TxNode},
     Append, BlockId, ChainPosition, ConfirmationTime, ConfirmationTimeHeightAnchor, FullTxOut,
     Indexed, IndexedTxGraph,
 };
@@ -2250,6 +2250,14 @@ impl Wallet {
         self.indexed_graph.graph()
     }
 
+    /// Iterate over transactions in the wallet that are unseen and unanchored likely
+    /// because they haven't been broadcast.
+    pub fn unbroadcast_transactions(
+        &self,
+    ) -> impl Iterator<Item = TxNode<'_, Arc<Transaction>, ConfirmationTimeHeightAnchor>> {
+        self.tx_graph().txs_with_no_anchor_or_last_seen()
+    }
+
     /// Get a reference to the inner [`KeychainTxOutIndex`].
     pub fn spk_index(&self) -> &KeychainTxOutIndex<KeychainKind> {
         &self.indexed_graph.index