Skip to content

Commit 7565b65

Browse files
committed
refactor(tx_graph)!: Rename {try_}list_chain_txs to {try_}canonical_transactions
The name `canonical_transactions` better describes the return type for this method. Docs are updated to clearly define what we mean by "canonical".
1 parent cd236f7 commit 7565b65

File tree

5 files changed

+23
-17
lines changed

5 files changed

+23
-17
lines changed

crates/bdk/src/wallet/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1103,7 +1103,7 @@ impl Wallet {
11031103
{
11041104
self.indexed_graph
11051105
.graph()
1106-
.list_chain_txs(&self.chain, self.chain.tip().block_id())
1106+
.canonical_transactions(&self.chain, self.chain.tip().block_id())
11071107
}
11081108

11091109
/// Return the balance, separated into available, trusted-pending, untrusted-pending and immature

crates/chain/src/tx_graph.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -969,18 +969,24 @@ impl<A: Anchor> TxGraph<A> {
969969

970970
/// List graph transactions that are in `chain` with `chain_tip`.
971971
///
972-
/// Each transaction is represented as a [`CanonicalTx`] that contains where the transaction is
973-
/// observed in-chain, and the [`TxNode`].
972+
/// Each transaction is represented as a [`CanonicalTx`] that contains where the transaction
973+
/// is observed in-chain and the [`TxNode`]. This means for a transaction to be considered
974+
/// canonical from the point of view of [`TxGraph`], it needs to have been observed either in
975+
/// the mempool or in a block confirmed in the active `chain`. However, it's also possible for
976+
/// [`TxGraph`] to contain non-canonical transactions if for example a tx conflicts with a
977+
/// more recent one or if it was never broadcast, in which case it can still be retrieved by
978+
/// calling [`full_txs`].
974979
///
975-
/// # Error
980+
/// # Errors
976981
///
977982
/// If the [`ChainOracle`] implementation (`chain`) fails, an error will be returned with the
978983
/// returned item.
979984
///
980-
/// If the [`ChainOracle`] is infallible, [`list_chain_txs`] can be used instead.
985+
/// If the [`ChainOracle`] is infallible, [`canonical_transactions`] can be used instead.
981986
///
982-
/// [`list_chain_txs`]: Self::list_chain_txs
983-
pub fn try_list_chain_txs<'a, C: ChainOracle + 'a>(
987+
/// [`full_txs`]: Self::full_txs
988+
/// [`canonical_transactions`]: Self::canonical_transactions
989+
pub fn try_canonical_transactions<'a, C: ChainOracle + 'a>(
984990
&'a self,
985991
chain: &'a C,
986992
chain_tip: BlockId,
@@ -1001,15 +1007,15 @@ impl<A: Anchor> TxGraph<A> {
10011007

10021008
/// List graph transactions that are in `chain` with `chain_tip`.
10031009
///
1004-
/// This is the infallible version of [`try_list_chain_txs`].
1010+
/// This is the infallible version of [`try_canonical_transactions`].
10051011
///
1006-
/// [`try_list_chain_txs`]: Self::try_list_chain_txs
1007-
pub fn list_chain_txs<'a, C: ChainOracle + 'a>(
1012+
/// [`try_canonical_transactions`]: Self::try_canonical_transactions
1013+
pub fn canonical_transactions<'a, C: ChainOracle + 'a>(
10081014
&'a self,
10091015
chain: &'a C,
10101016
chain_tip: BlockId,
10111017
) -> impl Iterator<Item = CanonicalTx<'a, Arc<Transaction>, A>> {
1012-
self.try_list_chain_txs(chain, chain_tip)
1018+
self.try_canonical_transactions(chain, chain_tip)
10131019
.map(|r| r.expect("oracle is infallible"))
10141020
}
10151021

crates/chain/tests/test_tx_graph_conflicts.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct Scenario<'a> {
1313
name: &'a str,
1414
/// Transaction templates
1515
tx_templates: &'a [TxTemplate<'a, BlockId>],
16-
/// Names of txs that must exist in the output of `list_chain_txs`
16+
/// Names of txs that must exist in the output of `canonical_transactions`
1717
exp_chain_txs: HashSet<&'a str>,
1818
/// Outpoints that must exist in the output of `filter_chain_txouts`
1919
exp_chain_txouts: HashSet<(&'a str, u32)>,
@@ -25,7 +25,7 @@ struct Scenario<'a> {
2525

2626
/// This test ensures that [`TxGraph`] will reliably filter out irrelevant transactions when
2727
/// presented with multiple conflicting transaction scenarios using the [`TxTemplate`] structure.
28-
/// This test also checks that [`TxGraph::list_chain_txs`], [`TxGraph::filter_chain_txouts`],
28+
/// This test also checks that [`TxGraph::canonical_transactions`], [`TxGraph::filter_chain_txouts`],
2929
/// [`TxGraph::filter_chain_unspents`], and [`TxGraph::balance`] return correct data.
3030
#[test]
3131
fn test_tx_conflict_handling() {
@@ -595,7 +595,7 @@ fn test_tx_conflict_handling() {
595595
let (tx_graph, spk_index, exp_tx_ids) = init_graph(scenario.tx_templates.iter());
596596

597597
let txs = tx_graph
598-
.list_chain_txs(&local_chain, chain_tip)
598+
.canonical_transactions(&local_chain, chain_tip)
599599
.map(|tx| tx.tx_node.txid)
600600
.collect::<BTreeSet<_>>();
601601
let exp_txs = scenario
@@ -605,7 +605,7 @@ fn test_tx_conflict_handling() {
605605
.collect::<BTreeSet<_>>();
606606
assert_eq!(
607607
txs, exp_txs,
608-
"\n[{}] 'list_chain_txs' failed",
608+
"\n[{}] 'canonical_transactions' failed",
609609
scenario.name
610610
);
611611

example-crates/example_electrum/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ fn main() -> anyhow::Result<()> {
264264
if unconfirmed {
265265
let unconfirmed_txids = graph
266266
.graph()
267-
.list_chain_txs(&*chain, chain_tip)
267+
.canonical_transactions(&*chain, chain_tip)
268268
.filter(|canonical_tx| !canonical_tx.chain_position.is_confirmed())
269269
.map(|canonical_tx| canonical_tx.tx_node.txid)
270270
.collect::<Vec<Txid>>();

example-crates/example_esplora/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ fn main() -> anyhow::Result<()> {
315315
// `EsploraExt::update_tx_graph_without_keychain`.
316316
let unconfirmed_txids = graph
317317
.graph()
318-
.list_chain_txs(&*chain, local_tip.block_id())
318+
.canonical_transactions(&*chain, local_tip.block_id())
319319
.filter(|canonical_tx| !canonical_tx.chain_position.is_confirmed())
320320
.map(|canonical_tx| canonical_tx.tx_node.txid)
321321
.collect::<Vec<Txid>>();

0 commit comments

Comments
 (0)