Skip to content

Commit 42fab2b

Browse files
committed
feat(tx_graph): Add method txs_with_no_anchor_or_last_seen
1 parent cc98c73 commit 42fab2b

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

crates/chain/src/tx_graph.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,27 @@ impl<A> TxGraph<A> {
258258
})
259259
}
260260

261+
/// All full transactions filtered by a filter map `f`.
262+
pub fn full_txs_by<'a, F, O>(&'a self, f: F) -> impl Iterator<Item = O> + 'a
263+
where
264+
F: FnMut(TxNode<'a, Arc<Transaction>, A>) -> Option<O> + 'a,
265+
{
266+
self.full_txs().filter_map(f)
267+
}
268+
269+
/// Iterate over graph transactions with no anchors or last-seen.
270+
pub fn txs_with_no_anchor_or_last_seen(
271+
&self,
272+
) -> impl Iterator<Item = TxNode<'_, Arc<Transaction>, A>> {
273+
self.full_txs_by(|tx| {
274+
if tx.anchors.is_empty() && tx.last_seen_unconfirmed.is_none() {
275+
Some(tx)
276+
} else {
277+
None
278+
}
279+
})
280+
}
281+
261282
/// Get a transaction by txid. This only returns `Some` for full transactions.
262283
///
263284
/// Refer to [`get_txout`] for getting a specific [`TxOut`].

crates/chain/tests/test_tx_graph.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,8 @@ fn list_canonical_txs() {
11291129
let mut graph = TxGraph::<BlockId>::new(txs);
11301130
let full_txs: Vec<_> = graph.full_txs().collect();
11311131
assert_eq!(full_txs.len(), 2);
1132+
let unseen_txs: Vec<_> = graph.txs_with_no_anchor_or_last_seen().collect();
1133+
assert_eq!(unseen_txs.len(), 2);
11321134

11331135
// chain
11341136
let blocks: BTreeMap<u32, BlockHash> = [(0, h!("g")), (1, h!("A")), (2, h!("B"))]
@@ -1156,6 +1158,7 @@ fn list_canonical_txs() {
11561158
.map(|tx| tx.tx_node.txid)
11571159
.collect();
11581160
assert!(canonical_txids.contains(&txids[1]));
1161+
assert!(graph.txs_with_no_anchor_or_last_seen().next().is_none());
11591162
}
11601163

11611164
#[test]

crates/wallet/src/wallet/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use bdk_chain::{
2727
self, ApplyHeaderError, CannotConnectError, CheckPoint, CheckPointIter, LocalChain,
2828
},
2929
spk_client::{FullScanRequest, FullScanResult, SyncRequest, SyncResult},
30-
tx_graph::{CanonicalTx, TxGraph},
30+
tx_graph::{CanonicalTx, TxGraph, TxNode},
3131
Append, BlockId, ChainPosition, ConfirmationTime, ConfirmationTimeHeightAnchor, FullTxOut,
3232
Indexed, IndexedTxGraph,
3333
};
@@ -2250,6 +2250,14 @@ impl Wallet {
22502250
self.indexed_graph.graph()
22512251
}
22522252

2253+
/// Iterate over transactions in the wallet that are unseen and unanchored likely
2254+
/// because they haven't been broadcast.
2255+
pub fn unbroadcast_transactions(
2256+
&self,
2257+
) -> impl Iterator<Item = TxNode<'_, Arc<Transaction>, ConfirmationTimeHeightAnchor>> {
2258+
self.as_ref().txs_with_no_anchor_or_last_seen()
2259+
}
2260+
22532261
/// Get a reference to the inner [`KeychainTxOutIndex`].
22542262
pub fn spk_index(&self) -> &KeychainTxOutIndex<KeychainKind> {
22552263
&self.indexed_graph.index

0 commit comments

Comments
 (0)