Repository navigation
feat(chain): order canonical txs topologically - #2464
evanlinjin wants to merge 2 commits into
Conversation
Drop this commit when rebasing onto bitcoindevkit#2139 once it lands. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Codecov Report❌ Patch coverage is 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
`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]>
dc98339 to
f8a94cc
Compare
| /// next. | ||
| pub(crate) fn topological_order<P, K: Ord>( | ||
| txs: &HashMap<Txid, CanonicalEntry<P>>, | ||
| key: impl Fn(&CanonicalEntry<P>) -> K, |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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` |
There was a problem hiding this comment.
what does @<number> mean? position? I would use something less cryptic.
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::orderis built with Kahn's algorithm, taking the smallest ready tx first:CanonicalTask::finish: by txid.CanonicalViewTask::finish: byChainPosition(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
canonicalizationbench, the worst case (many_chained_unconfirmed, 2100 chained txs) costs about 1 ms for both passes; the other benches barely move.CanonicalViewtopological ordering #2219 (comment): level order puts an unconfirmed tx before a confirmed one deeper in the graph.CanonicalViewtopological ordering #2219: the order is whattxs()returns, built once per view, not a separate method that sorts on every call.Changelog notice
bdk_chainCanonical::txs(and soCanonicalView/CanonicalTxs) yields transactions in topological order, parents before children. Otherwise aCanonicalVieworders them byChainPosition, then txid. #Checklists
All Submissions:
New Features:
🤖 Generated with Claude Code