From 3fe2bb0e53b1ac720c08d30617fc69aeec8ee6a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BF=97=E5=AE=87?= Date: Tue, 17 Dec 2024 13:46:58 +1100 Subject: [PATCH] fix(wallet): `transactions` method should only return relevant txs Also update documentation. --- crates/wallet/src/wallet/mod.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/crates/wallet/src/wallet/mod.rs b/crates/wallet/src/wallet/mod.rs index 3af424d37..f9e281da1 100644 --- a/crates/wallet/src/wallet/mod.rs +++ b/crates/wallet/src/wallet/mod.rs @@ -19,6 +19,7 @@ use alloc::{ sync::Arc, vec::Vec, }; +use chain::Indexer; use core::{cmp::Ordering, fmt, mem, ops::Deref}; use bdk_chain::{ @@ -1062,14 +1063,30 @@ impl Wallet { .find(|tx| tx.tx_node.txid == txid) } - /// Iterate over the transactions in the wallet. + /// Iterate over relevant and canonical transactions in the wallet. + /// + /// A transaction is relevant when it spends from or spends to at least one tracked ouput. A + /// transactions is canonical when it is confirmed in the best chain, or does not conflict + /// with any transaction confirmed in the best chain. + /// + /// To iterate over all transactions, including those that are irrelevant and not canonical, use + /// [`TxGraph::full_txs`]. + /// + /// To iterate over all canonical transactions, including those that are irrelevant, use + /// [`TxGraph::list_canonical_txs`]. pub fn transactions(&self) -> impl Iterator + '_ { - self.indexed_graph - .graph() + let tx_graph = self.indexed_graph.graph(); + let tx_index = &self.indexed_graph.index; + tx_graph .list_canonical_txs(&self.chain, self.chain.tip().block_id()) + .filter(|c_tx| tx_index.is_tx_relevant(&c_tx.tx_node.tx)) } - /// Array of transactions in the wallet sorted with a comparator function. + /// Array of relevant and canonical transactions in the wallet sorted with a comparator + /// function. + /// + /// This is a helper method equivalent to collecting the result of [`Wallet::transactions`] + /// into a [`Vec`] and then sorting it. /// /// # Example ///