Skip to content

feat(chain): add new list_ordered_canonical_txs method#2027

Merged
ValuedMammal merged 4 commits intobitcoindevkit:release/chain-0.23.xfrom
oleonardolima:test/add-canonical-iter-topological-order-tests
Nov 24, 2025
Merged

feat(chain): add new list_ordered_canonical_txs method#2027
ValuedMammal merged 4 commits intobitcoindevkit:release/chain-0.23.xfrom
oleonardolima:test/add-canonical-iter-topological-order-tests

Conversation

@oleonardolima
Copy link
Collaborator

@oleonardolima oleonardolima commented Sep 11, 2025

Description

It introduces a new method for topological ordering canonical transactions, list_ordered_canonical_txs. It now ensures the dependency-based transaction processing, guaranteeing that parent transactions always appears before their children transaction.

The existing list_canonical_txs and try_list_canonical_txs methods have been deprecated in favor of the new ordered version.

Notes to the reviewers

"[...] For those reviewing, and wondering why we don't have a fallible try version of this method, it's because we don't have a fallible ChainOracle implementation - we will get rid of ChainOracle trait soon anyway."

This PR is intended for a point release so that bdk_wallet 2.x users can get a topologically sorted list of transactions (we need a point release on bdk_wallet 2.x as well).

It might be useful to take a look at the new test scenarios that I've added, it shows some specific scenarios where the current implementation and output of canonical_txs didn't output the transactions in topological order.

Let me know if you think the TopologicalIter algorithm and/or API could be improved.

Changelog notice

#### Added
- New `list_ordered_canonical_txs` method to `TxGraph` that returns canonical transactions in topological order, ensuring parent transactions always appear before their children

#### Deprecated
- `list_canonical_txs` method - use `list_ordered_canonical_txs` instead for guaranteed topological ordering
- `try_list_canonical_txs` method - use `list_ordered_canonical_txs` instead for guaranteed topological ordering

Checklists

All Submissions:

New Features:

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

@oleonardolima oleonardolima added this to the Wallet 3.0.0 milestone Sep 11, 2025
@oleonardolima oleonardolima self-assigned this Sep 11, 2025
@oleonardolima oleonardolima force-pushed the test/add-canonical-iter-topological-order-tests branch 2 times, most recently from 6b76092 to a28cfdf Compare September 11, 2025 03:07
@evanlinjin
Copy link
Member

Nice diagrams

@oleonardolima oleonardolima force-pushed the test/add-canonical-iter-topological-order-tests branch 11 times, most recently from 08e4ce8 to 70ee41b Compare September 16, 2025 04:49
@oleonardolima

This comment was marked as outdated.

@oleonardolima oleonardolima force-pushed the test/add-canonical-iter-topological-order-tests branch 2 times, most recently from 6239c8f to 0aab769 Compare September 16, 2025 06:10
@oleonardolima oleonardolima moved this to In Progress in BDK Chain Sep 16, 2025
@oleonardolima oleonardolima force-pushed the test/add-canonical-iter-topological-order-tests branch from d57a580 to 3b79cba Compare September 19, 2025 01:52
Copy link
Member

@evanlinjin evanlinjin left a comment

Choose a reason for hiding this comment

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

The internals have completely changed. I don't think a point release would make any sense (we also have already merged other breaking changes). I recommend to just break the API.

Get rid of CanonicalIter. Just have a single function that returns impl Iterator<Item = ...>.

@oleonardolima oleonardolima force-pushed the test/add-canonical-iter-topological-order-tests branch 2 times, most recently from 0a17091 to 71c3744 Compare September 23, 2025 04:20
@ValuedMammal ValuedMammal changed the base branch from release/chain-0.23.2 to release/chain-0.23.x October 3, 2025 23:28
Copy link
Collaborator

@ValuedMammal ValuedMammal left a comment

Choose a reason for hiding this comment

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

Can we drop these commits which appear to have been included in #2061

  • 0a33219 fix(example_cli): clippy warnings
  • 377da74 fix(electrum): clippy warnings

@oleonardolima oleonardolima force-pushed the test/add-canonical-iter-topological-order-tests branch from 9c4da10 to 6fd0de9 Compare November 21, 2025 18:59
@oleonardolima oleonardolima force-pushed the test/add-canonical-iter-topological-order-tests branch 7 times, most recently from b1294f5 to 37920a2 Compare November 21, 2025 21:37
@oleonardolima
Copy link
Collaborator Author

@evanlinjin I've addressed all the comments/nits, and it's ready for a final round of review. Let me know if you see anything else.

I improved the docs, commit messages, and also squashed some of them.

--

Can we drop these commits which appear to have been included in #2061

  • 0a33219 fix(example_cli): clippy warnings
  • 377da74 fix(electrum): clippy warnings

@ValuedMammal I fixed these through the rebase, but now I'll need to drop the new one d9a7c2c, but it depends on #2075 landing in release/chain-0.23.x

Copy link
Collaborator

@ValuedMammal ValuedMammal left a comment

Choose a reason for hiding this comment

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

I'll give this another review.

oleonardolima and others added 4 commits November 22, 2025 01:39
- Add a new `test_list_ordered_canonical_txs`, to assert the topological
  spending order when yielding the list of canonical txs, it also adds a
  new helper `Scenario` structure, and `is_txs_in_topological_order`.
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>
@oleonardolima oleonardolima force-pushed the test/add-canonical-iter-topological-order-tests branch from 37920a2 to 12c1076 Compare November 22, 2025 04:40
Copy link
Collaborator

@ValuedMammal ValuedMammal left a comment

Choose a reason for hiding this comment

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

ACK 12c1076

@ValuedMammal ValuedMammal merged commit 1a77475 into bitcoindevkit:release/chain-0.23.x Nov 24, 2025
19 checks passed
@github-project-automation github-project-automation bot moved this from Needs Review to Done in BDK Chain Nov 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants