Skip to content

feat(chain): order canonical txs topologically - #2464

Open
evanlinjin wants to merge 2 commits into
bitcoindevkit:masterfrom
evanlinjin:dv/replace-2219
Open

evanlinjin wants to merge 2 commits into
bitcoindevkit:masterfrom
evanlinjin:dv/replace-2219

Conversation

@evanlinjin

Copy link
Copy Markdown
Contributor

Builds on #2139: review only the last commit. The first commit is #2139 squashed onto master, to be dropped when rebasing onto #2139 once it lands.

Closes #2201
Replaces #2219

Description

Canonical::txs() now yields parents before children, deterministically. Before, it followed the order txs were canonicalized in.

Canonical::order is built with Kahn's algorithm, taking the smallest ready tx first:

  • CanonicalTask::finish: by txid.
  • CanonicalViewTask::finish: by ChainPosition (confirmed by anchor, then unconfirmed by first seen), then txid. So a wallet's tx list shows confirmed txs before unconfirmed ones wherever the spends allow.

Notes to the reviewers

Changelog notice

bdk_chain

  • Changed: Canonical::txs (and so CanonicalView/CanonicalTxs) yields transactions in topological order, parents before children. Otherwise a CanonicalView orders them by ChainPosition, then txid. #

Checklists

All Submissions:

New Features:

  • I've added tests for the new feature
  • I've added docs for the new feature

🤖 Generated with Claude Code

Drop this commit when rebasing onto bitcoindevkit#2139 once it lands.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@codecov

codecov Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.76108% with 22 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.47%. Comparing base (02b7b12) to head (f8a94cc).
⚠️ Report is 4 commits behind head on master.

Files with missing lines Patch % Lines
crates/chain/src/canonical_view_task.rs 91.42% 11 Missing and 1 partial ⚠️
crates/chain/src/canonical_task.rs 94.56% 5 Missing ⚠️
crates/chain/src/indexed_tx_graph.rs 0.00% 2 Missing ⚠️
crates/chain/src/local_chain.rs 95.83% 1 Missing and 1 partial ⚠️
crates/chain/src/canonical.rs 99.08% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2464      +/-   ##
==========================================
+ Coverage   83.25%   83.47%   +0.21%     
==========================================
  Files          31       32       +1     
  Lines        6236     6494     +258     
  Branches      293      298       +5     
==========================================
+ Hits         5192     5421     +229     
- Misses        976     1010      +34     
+ Partials       68       63       -5     
Flag Coverage Δ
rust 83.47% <95.76%> (+0.21%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

`Canonical::order` held the order txs were canonicalized in, which is
neither topological nor by position. Both tasks now build it with
Kahn's algorithm, taking the smallest ready tx first: by txid in
`CanonicalTask::finish`, and by `ChainPosition` (confirmed by anchor,
then unconfirmed by first seen), then txid, in
`CanonicalViewTask::finish`.

So `txs()` yields parents before children, deterministically, and a
view lists confirmed txs before unconfirmed ones where spends allow.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@evanlinjin evanlinjin moved this from Triage to In Progress in BDK Chain Nov 10, 2026
@evanlinjin
evanlinjin marked this pull request as ready for review November 11, 2026 01:16
@evanlinjin
evanlinjin requested a review from a team as a code owner November 11, 2026 01:16

@nymius nymius left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK f8a94cc

/// next.
pub(crate) fn topological_order<P, K: Ord>(
txs: &HashMap<Txid, CanonicalEntry<P>>,
key: impl Fn(&CanonicalEntry<P>) -> K,

@nymius nymius Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some docs about the purpose of key would be helpful here. Nothing fancy, just what's the purpose and an example of the shape of K.

// `tx` double spends itself.
let mut detected_self_double_spend = false;
let mut undo_not_canonical = Vec::<Txid>::new();
let mut staged_canonical = Vec::<(Txid, Arc<Transaction>, CanonicalReason<A>)>::new();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the reason for this type shape? It doesn't seem order related

}
}

/// `txs()` is in topological order, otherwise by chain position: `b` @3 (spends `a` twice) and `c`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does @<number> mean? position? I would use something less cryptic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

Add optional topological ordering stage CanonicalViewTask

2 participants