Skip to content

Commit b1294f5

Browse files
oleonardolimaclaude
andcommitted
feat(chain): introduce new list_ordered_canonical_txs
Adds a new `list_ordered_canonical_txs` method which uses the new `TopologicalIterator` on top of the result of `list_canonical_txs` method, yielding the canonical txs in topological spending order. The new `list_ordered_canonical_txs` guarantees that spending transactions appears after their inputs, in topological "spending order". - Introduce the new `TopologicalIterator` for level-based topological sorting, based on Kahn's Algorithm, it uses the `ChainPosition` for sorting within the same graph level, and it takes an `Iterator<Item = CanonicalTx<'a, Arc<Transaction>, A>> of canonical txs. - Introduce the new `list_ordered_canonical_txs` method to `TxGraph`. - Update the existing tests under `test_tx_graph.rs` to verify the topological ordering correctness. - Update the existing `canonicalization` benchmark to also use the new `topological_ordered_txs` method. NOTE: - I've squashed the previous commits into a single one, as they're changing the same files and applies to the same scope. - Also, I've partially used Claude to help with the Kahn's Algorithm. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 8ddc872 commit b1294f5

File tree

8 files changed

+235
-11
lines changed

8 files changed

+235
-11
lines changed

crates/chain/benches/canonicalization.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ fn run_list_canonical_txs(tx_graph: &KeychainTxGraph, chain: &LocalChain, exp_tx
9999
assert_eq!(txs.count(), exp_txs);
100100
}
101101

102+
fn run_list_ordered_canonical_txs(tx_graph: &KeychainTxGraph, chain: &LocalChain, exp_txs: usize) {
103+
let txs = tx_graph.graph().list_ordered_canonical_txs(
104+
chain,
105+
chain.tip().block_id(),
106+
CanonicalizationParams::default(),
107+
);
108+
assert_eq!(txs.count(), exp_txs);
109+
}
110+
102111
fn run_filter_chain_txouts(tx_graph: &KeychainTxGraph, chain: &LocalChain, exp_txos: usize) {
103112
let utxos = tx_graph.graph().filter_chain_txouts(
104113
chain,
@@ -147,6 +156,13 @@ pub fn many_conflicting_unconfirmed(c: &mut Criterion) {
147156
let (tx_graph, chain) = (tx_graph.clone(), chain.clone());
148157
move |b| b.iter(|| run_list_canonical_txs(&tx_graph, &chain, 2))
149158
});
159+
c.bench_function(
160+
"many_conflicting_unconfirmed::list_ordered_canonical_txs",
161+
{
162+
let (tx_graph, chain) = (tx_graph.clone(), chain.clone());
163+
move |b| b.iter(|| run_list_ordered_canonical_txs(&tx_graph, &chain, 2))
164+
},
165+
);
150166
c.bench_function("many_conflicting_unconfirmed::filter_chain_txouts", {
151167
let (tx_graph, chain) = (tx_graph.clone(), chain.clone());
152168
move |b| b.iter(|| run_filter_chain_txouts(&tx_graph, &chain, 2))
@@ -185,6 +201,10 @@ pub fn many_chained_unconfirmed(c: &mut Criterion) {
185201
let (tx_graph, chain) = (tx_graph.clone(), chain.clone());
186202
move |b| b.iter(|| run_list_canonical_txs(&tx_graph, &chain, 2101))
187203
});
204+
c.bench_function("many_chained_unconfirmed::list_ordered_canonical_txs", {
205+
let (tx_graph, chain) = (tx_graph.clone(), chain.clone());
206+
move |b| b.iter(|| run_list_ordered_canonical_txs(&tx_graph, &chain, 2101))
207+
});
188208
c.bench_function("many_chained_unconfirmed::filter_chain_txouts", {
189209
let (tx_graph, chain) = (tx_graph.clone(), chain.clone());
190210
move |b| b.iter(|| run_filter_chain_txouts(&tx_graph, &chain, 1))
@@ -234,6 +254,13 @@ pub fn nested_conflicts(c: &mut Criterion) {
234254
let (tx_graph, chain) = (tx_graph.clone(), chain.clone());
235255
move |b| b.iter(|| run_list_canonical_txs(&tx_graph, &chain, GRAPH_DEPTH))
236256
});
257+
c.bench_function(
258+
"nested_conflicts_unconfirmed::list_ordered_canonical_txs",
259+
{
260+
let (tx_graph, chain) = (tx_graph.clone(), chain.clone());
261+
move |b| b.iter(|| run_list_ordered_canonical_txs(&tx_graph, &chain, GRAPH_DEPTH))
262+
},
263+
);
237264
c.bench_function("nested_conflicts_unconfirmed::filter_chain_txouts", {
238265
let (tx_graph, chain) = (tx_graph.clone(), chain.clone());
239266
move |b| b.iter(|| run_filter_chain_txouts(&tx_graph, &chain, GRAPH_DEPTH))

crates/chain/src/canonical_iter.rs

Lines changed: 155 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::collections::{HashMap, HashSet, VecDeque};
2-
use crate::tx_graph::{TxAncestors, TxDescendants};
2+
use crate::tx_graph::{CanonicalTx, TxAncestors, TxDescendants};
33
use crate::{Anchor, ChainOracle, TxGraph};
44
use alloc::boxed::Box;
55
use alloc::collections::BTreeSet;
@@ -342,3 +342,157 @@ impl<A: Clone> CanonicalReason<A> {
342342
}
343343
}
344344
}
345+
346+
/// Iterator based on the Kahn's Algorithm, that yields transactions in topological spending order
347+
/// in depth, and properly sorted with level.
348+
///
349+
/// NOTE: Please refer to the Kahn's Algorithm reference: https://dl.acm.org/doi/pdf/10.1145/368996.369025
350+
pub(crate) struct TopologicalIterator<'a, A> {
351+
/// Map of txid to its canonical transaction
352+
canonical_txs: HashMap<Txid, CanonicalTx<'a, Arc<Transaction>, A>>,
353+
354+
/// Current level of transactions to process
355+
current_level: Vec<Txid>,
356+
/// Next level of transactions to process
357+
next_level: Vec<Txid>,
358+
359+
/// Adjacency list: parent txid -> list of children txids
360+
children_map: HashMap<Txid, Vec<Txid>>,
361+
/// Number of unprocessed parents for each transaction
362+
parent_count: HashMap<Txid, usize>,
363+
364+
/// Current index in the current level
365+
current_index: usize,
366+
}
367+
368+
impl<'a, A: Clone + Anchor> TopologicalIterator<'a, A> {
369+
/// Constructs [`TopologicalIterator`] from a list of `canonical_txs` (e.g [`CanonicalIter`]),
370+
/// in order to handle all the graph building internally.
371+
pub(crate) fn new(
372+
canonical_txs: impl Iterator<Item = CanonicalTx<'a, Arc<Transaction>, A>>,
373+
) -> Self {
374+
// Build a map from txid to canonical tx for quick lookup
375+
let mut tx_map: HashMap<Txid, CanonicalTx<'a, Arc<Transaction>, A>> = HashMap::new();
376+
let mut canonical_set: HashSet<Txid> = HashSet::new();
377+
378+
for canonical_tx in canonical_txs {
379+
let txid = canonical_tx.tx_node.txid;
380+
canonical_set.insert(txid);
381+
tx_map.insert(txid, canonical_tx);
382+
}
383+
384+
// Build the dependency graph (txid -> parents it depends on)
385+
let mut dependencies: HashMap<Txid, Vec<Txid>> = HashMap::new();
386+
let mut has_parents: HashSet<Txid> = HashSet::new();
387+
388+
for &txid in canonical_set.iter() {
389+
let canonical_tx = tx_map.get(&txid).expect("txid must exist in map");
390+
let tx = &canonical_tx.tx_node.tx;
391+
392+
// Find all parents (transactions this one depends on)
393+
let mut parents = Vec::new();
394+
if !tx.is_coinbase() {
395+
for txin in &tx.input {
396+
let parent_txid = txin.previous_output.txid;
397+
// Only include if the parent is also canonical
398+
if canonical_set.contains(&parent_txid) {
399+
parents.push(parent_txid);
400+
has_parents.insert(txid);
401+
}
402+
}
403+
}
404+
405+
if !parents.is_empty() {
406+
dependencies.insert(txid, parents);
407+
}
408+
}
409+
410+
// Build adjacency list and parent counts for traversal
411+
let mut parent_count = HashMap::new();
412+
let mut children_map: HashMap<Txid, Vec<Txid>> = HashMap::new();
413+
414+
for (txid, parents) in &dependencies {
415+
for parent_txid in parents {
416+
children_map.entry(*parent_txid).or_default().push(*txid);
417+
*parent_count.entry(*txid).or_insert(0) += 1;
418+
}
419+
}
420+
421+
// Find root transactions (those with no parents in the canonical set)
422+
let roots: Vec<Txid> = canonical_set
423+
.iter()
424+
.filter(|&&txid| !has_parents.contains(&txid))
425+
.copied()
426+
.collect();
427+
428+
// Sort the initial level
429+
let mut current_level = roots;
430+
Self::sort_level_by_chain_position(&mut current_level, &tx_map);
431+
432+
Self {
433+
canonical_txs: tx_map,
434+
current_level,
435+
next_level: Vec::new(),
436+
children_map,
437+
parent_count,
438+
current_index: 0,
439+
}
440+
}
441+
442+
/// Sort transactions within a level by their chain position
443+
/// Confirmed transactions come first (sorted by height), then unconfirmed (sorted by last_seen)
444+
fn sort_level_by_chain_position(
445+
level: &mut [Txid],
446+
canonical_txs: &HashMap<Txid, CanonicalTx<'a, Arc<Transaction>, A>>,
447+
) {
448+
level.sort_by(|&a_txid, &b_txid| {
449+
let a_tx = canonical_txs.get(&a_txid).expect("txid must exist");
450+
let b_tx = canonical_txs.get(&b_txid).expect("txid must exist");
451+
452+
a_tx.cmp(b_tx)
453+
});
454+
}
455+
456+
fn advance_to_next_level(&mut self) {
457+
self.current_level = core::mem::take(&mut self.next_level);
458+
Self::sort_level_by_chain_position(&mut self.current_level, &self.canonical_txs);
459+
self.current_index = 0;
460+
}
461+
}
462+
463+
impl<'a, A: Clone + Anchor> Iterator for TopologicalIterator<'a, A> {
464+
type Item = CanonicalTx<'a, Arc<Transaction>, A>;
465+
466+
fn next(&mut self) -> Option<Self::Item> {
467+
// If we've exhausted the current level, move to next
468+
if self.current_index >= self.current_level.len() {
469+
if self.next_level.is_empty() {
470+
return None;
471+
}
472+
self.advance_to_next_level();
473+
}
474+
475+
let current_txid = self.current_level[self.current_index];
476+
self.current_index += 1;
477+
478+
// If this is the last item in current level, prepare dependents for next level
479+
if self.current_index == self.current_level.len() {
480+
// Process all dependents of all transactions in current level
481+
for &tx in &self.current_level {
482+
if let Some(children) = self.children_map.get(&tx) {
483+
for &child in children {
484+
if let Some(count) = self.parent_count.get_mut(&child) {
485+
*count -= 1;
486+
if *count == 0 {
487+
self.next_level.push(child);
488+
}
489+
}
490+
}
491+
}
492+
}
493+
}
494+
495+
// Return the CanonicalTx for the current txid
496+
self.canonical_txs.get(&current_txid).cloned()
497+
}
498+
}

crates/chain/src/tx_graph.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,9 @@ impl<A: Anchor> TxGraph<A> {
988988
/// Each transaction is represented as a [`CanonicalTx`] that contains where the transaction is
989989
/// observed in-chain, and the [`TxNode`].
990990
///
991+
/// NOTE: It does not guarantee the topological order of yielded transactions, the
992+
/// [`list_ordered_canonical_txs`] can be used instead.
993+
///
991994
/// # Error
992995
///
993996
/// If the [`ChainOracle`] implementation (`chain`) fails, an error will be returned with the
@@ -996,6 +999,7 @@ impl<A: Anchor> TxGraph<A> {
996999
/// If the [`ChainOracle`] is infallible, [`list_canonical_txs`] can be used instead.
9971000
///
9981001
/// [`list_canonical_txs`]: Self::list_canonical_txs
1002+
/// [`list_ordered_canonical_txs`]: Self::list_ordered_canonical_txs
9991003
pub fn try_list_canonical_txs<'a, C: ChainOracle + 'a>(
10001004
&'a self,
10011005
chain: &'a C,
@@ -1079,7 +1083,11 @@ impl<A: Anchor> TxGraph<A> {
10791083
///
10801084
/// This is the infallible version of [`try_list_canonical_txs`].
10811085
///
1086+
/// NOTE: It does not guarantee the topological order of yielded transactions, the
1087+
/// [`list_ordered_canonical_txs`] can be used instead.
1088+
///
10821089
/// [`try_list_canonical_txs`]: Self::try_list_canonical_txs
1090+
/// [`list_ordered_canonical_txs`]: Self::list_ordered_canonical_txs
10831091
pub fn list_canonical_txs<'a, C: ChainOracle<Error = Infallible> + 'a>(
10841092
&'a self,
10851093
chain: &'a C,
@@ -1090,6 +1098,28 @@ impl<A: Anchor> TxGraph<A> {
10901098
.map(|res| res.expect("infallible"))
10911099
}
10921100

1101+
/// List graph transactions that are in `chain` with `chain_tip` in topological order.
1102+
///
1103+
/// Each transaction is represented as a [`CanonicalTx`] that contains where the transaction is
1104+
/// observed in-chain, and the [`TxNode`].
1105+
///
1106+
/// Transactions are returned in topological spending order, meaning that if transaction B
1107+
/// spends from transaction A, then A will always appear before B in the resulting list.
1108+
///
1109+
/// This is the infallible version which uses [`list_canonical_txs`] internally and then
1110+
/// reorders the transactions based on their spending relationships.
1111+
///
1112+
/// [`list_canonical_txs`]: Self::list_canonical_txs
1113+
pub fn list_ordered_canonical_txs<'a, C: ChainOracle<Error = Infallible>>(
1114+
&'a self,
1115+
chain: &'a C,
1116+
chain_tip: BlockId,
1117+
params: CanonicalizationParams,
1118+
) -> impl Iterator<Item = CanonicalTx<'a, Arc<Transaction>, A>> {
1119+
use crate::canonical_iter::TopologicalIterator;
1120+
TopologicalIterator::new(self.list_canonical_txs(chain, chain_tip, params))
1121+
}
1122+
10931123
/// Get a filtered list of outputs from the given `outpoints` that are in `chain` with
10941124
/// `chain_tip`.
10951125
///
@@ -1118,6 +1148,7 @@ impl<A: Anchor> TxGraph<A> {
11181148
) -> Result<impl Iterator<Item = (OI, FullTxOut<A>)> + 'a, C::Error> {
11191149
let mut canon_txs = HashMap::<Txid, CanonicalTx<Arc<Transaction>, A>>::new();
11201150
let mut canon_spends = HashMap::<OutPoint, Txid>::new();
1151+
11211152
for r in self.try_list_canonical_txs(chain, chain_tip, params) {
11221153
let canonical_tx = r?;
11231154
let txid = canonical_tx.tx_node.txid;
@@ -1418,6 +1449,7 @@ impl<A: Anchor> TxGraph<A> {
14181449
I: fmt::Debug + Clone + Ord + 'a,
14191450
{
14201451
let indexer = indexer.as_ref();
1452+
14211453
self.try_list_canonical_txs(chain, chain_tip, CanonicalizationParams::default())
14221454
.flat_map(move |res| -> Vec<Result<(ScriptBuf, Txid), C::Error>> {
14231455
let range = &spk_index_range;

crates/chain/tests/test_indexed_tx_graph.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,7 @@ fn test_get_chain_position() {
782782
}
783783

784784
// check chain position
785+
785786
let chain_pos = graph
786787
.graph()
787788
.list_canonical_txs(

crates/chain/tests/test_tx_graph.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,7 @@ fn transactions_inserted_into_tx_graph_are_not_canonical_until_they_have_an_anch
12011201
.into_iter()
12021202
.collect();
12031203
let chain = LocalChain::from_blocks(blocks).unwrap();
1204+
12041205
let canonical_txs: Vec<_> = graph
12051206
.list_canonical_txs(
12061207
&chain,
@@ -1212,6 +1213,7 @@ fn transactions_inserted_into_tx_graph_are_not_canonical_until_they_have_an_anch
12121213

12131214
// tx0 with seen_at should be returned by canonical txs
12141215
let _ = graph.insert_seen_at(txids[0], 2);
1216+
12151217
let mut canonical_txs = graph.list_canonical_txs(
12161218
&chain,
12171219
chain.tip().block_id(),
@@ -1225,6 +1227,7 @@ fn transactions_inserted_into_tx_graph_are_not_canonical_until_they_have_an_anch
12251227

12261228
// tx1 with anchor is also canonical
12271229
let _ = graph.insert_anchor(txids[1], block_id!(2, "B"));
1230+
12281231
let canonical_txids: Vec<_> = graph
12291232
.list_canonical_txs(
12301233
&chain,
@@ -2030,31 +2033,34 @@ fn test_list_ordered_canonical_txs() {
20302033
exp_chain_txs: Vec::from(["a0", "e0", "f0", "b0", "c0", "b1", "c1", "d0"]),
20312034
}];
20322035

2033-
for (_, scenario) in scenarios.iter().enumerate() {
2036+
for scenario in scenarios {
20342037
let env = init_graph(scenario.tx_templates.iter());
20352038

2036-
let canonical_txs = env
2039+
let canonical_txids = env
20372040
.tx_graph
2038-
.list_canonical_txs(&local_chain, chain_tip, env.canonicalization_params.clone())
2041+
.list_ordered_canonical_txs(
2042+
&local_chain,
2043+
chain_tip,
2044+
env.canonicalization_params.clone(),
2045+
)
20392046
.map(|tx| tx.tx_node.txid)
2040-
.collect::<BTreeSet<_>>();
2047+
.collect::<Vec<_>>();
20412048

2042-
let exp_txs = scenario
2049+
let exp_txids = scenario
20432050
.exp_chain_txs
20442051
.iter()
20452052
.map(|txid| *env.tx_name_to_txid.get(txid).expect("txid must exist"))
2046-
.collect::<BTreeSet<_>>();
2053+
.collect::<Vec<_>>();
20472054

20482055
assert_eq!(
2049-
canonical_txs, exp_txs,
2056+
HashSet::<Txid>::from_iter(canonical_txids.clone()),
2057+
HashSet::<Txid>::from_iter(exp_txids.clone()),
20502058
"\n[{}] 'list_canonical_txs' failed",
20512059
scenario.name
20522060
);
20532061

2054-
let canonical_txs = canonical_txs.iter().map(|txid| *txid).collect::<Vec<_>>();
2055-
20562062
assert!(
2057-
is_txs_in_topological_order(canonical_txs, env.tx_graph),
2063+
is_txs_in_topological_order(canonical_txids, env.tx_graph),
20582064
"\n[{}] 'list_canonical_txs' failed to output the txs in topological order",
20592065
scenario.name
20602066
);

examples/example_bitcoind_rpc_polling/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ fn main() -> anyhow::Result<()> {
137137
} = rpc_args;
138138

139139
let rpc_client = rpc_args.new_client()?;
140+
140141
let mut emitter = {
141142
let chain = chain.lock().unwrap();
142143
let graph = graph.lock().unwrap();
@@ -237,6 +238,7 @@ fn main() -> anyhow::Result<()> {
237238
let sigterm_flag = start_ctrlc_handler();
238239

239240
let rpc_client = Arc::new(rpc_args.new_client()?);
241+
240242
let mut emitter = {
241243
let chain = chain.lock().unwrap();
242244
let graph = graph.lock().unwrap();

examples/example_electrum/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ fn main() -> anyhow::Result<()> {
238238
.map(|(_, utxo)| utxo.outpoint),
239239
);
240240
};
241+
241242
if unconfirmed {
242243
request = request.txids(
243244
graph

0 commit comments

Comments
 (0)