Skip to content

Commit

Permalink
feat(tx_graph): Add method txs_with_no_anchor_or_last_seen
Browse files Browse the repository at this point in the history
  • Loading branch information
ValuedMammal committed Jun 25, 2024
1 parent a3bd332 commit 92e9720
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
21 changes: 21 additions & 0 deletions crates/chain/src/tx_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,27 @@ impl<A> TxGraph<A> {
})
}

/// All full transactions filtered by a filter map `f`.
pub fn full_txs_by<'a, F, O>(&'a self, f: F) -> impl Iterator<Item = O> + 'a
where
F: FnMut(TxNode<'a, Arc<Transaction>, A>) -> Option<O> + 'a,
{
self.full_txs().filter_map(f)
}

/// 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_by(|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`].
Expand Down
3 changes: 3 additions & 0 deletions crates/chain/tests/test_tx_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,8 @@ fn list_canonical_txs() {
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"))]
Expand Down Expand Up @@ -1156,6 +1158,7 @@ fn list_canonical_txs() {
.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]
Expand Down
10 changes: 9 additions & 1 deletion crates/wallet/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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.as_ref().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
Expand Down

0 comments on commit 92e9720

Please sign in to comment.