refactor(chain)!: remove min_confirmations parameter from CanonicalView::balance#2221
refactor(chain)!: remove min_confirmations parameter from CanonicalView::balance#2221Dmenec wants to merge 1 commit into
min_confirmations parameter from CanonicalView::balance#2221Conversation
|
cc: @evanlinjin , what do you think? Just last commit: dec3133 |
|
@nymius I'll have a look! Give me a bit of time, I just got informed for the incomplete Balance problem. |
fa6d466 to
dec3133
Compare
Co-authored-by: Dmenec <dmenec@proton.me>
dec3133 to
5a7c95c
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2221 +/- ##
==========================================
- Coverage 78.65% 78.60% -0.06%
==========================================
Files 30 30
Lines 5909 5894 -15
Branches 279 277 -2
==========================================
- Hits 4648 4633 -15
Misses 1185 1185
Partials 76 76
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:
|
|
@Dmenec thank you for this PR. Unfortunately, I don't think I agree that I've made a prototype of the direction I think we should take in #2235. Let me know what you think. |
Description
This builds on top of #2038.
Removes the
min_confirmationsparameter fromCanonicalView::balance.This is part of the redesign of the wallet
balanceAPI (see bitcoindevkit/bdk_wallet#451).Context
Trust Categorization
The goal is to fix how
balancedecides if a pending output is trusted or not. Right now the wallet decides this from the keychain (internal = trusted), which has some issues (bitcoindevkit/bdk_wallet#16, bitcoindevkit/bdk_wallet#273).The approach discussed in bitcoindevkit/bdk_wallet#431 describes creating the trust predicate by walking the tx ancestors. An unconfirmed output is trusted only if every unconfirmed ancestor spends inputs that are ours.
To achieve that, we need a few things together in chain:
CanonicalIterwith sans-IOCanonicalTask+ChainQuerytrait #2038 — the new CanonicalView, which lets us look up any tx by txid and get its canonical position.There is a small prototype test of this trust logic here: b0acd3b#diff-d3f18e9a77fd241fa351a0cbcd75e993d90819834ccde09b7f4247c153ff26eeR586-R607. It is just how the idea may work in wallet, not the final API.
Removing
min_confirmationsThe ancestor walk decides trust by checking each ancestor's canonical position. A
Confirmedancestor stops the walk, anUnconfirmedone is only trusted if all of its inputs are also ours. For this to be coherent,Confirmedhas to mean the same thing inbalanceas it does in the walk.On the previous approach the parameter
min_confirmationsbroke that. AConfirmedoutput below themin_confirmationsthreshold was reported aspending, even though the walk still saw it asConfirmed.Removing it,
balanceclassifies purely from the canonical position, andConfirmedmeans the same thing everywhere. This also alignsbalancewith the usage of every other method ofCanonicalViewwhich already reads positions relative to the view's tip and leaves interpretation to the caller.With the parameter gone, the way to ask for a confirmation threshold is though the tip the
CanonicalViewis build with. A view is always a snapshot of the chain seen from a given tip, so "how many confirmations does this output have" is really the same question as "which tip am I looking from". To require N confirmations, the view is built attip - N +1:Any tx whose block ends up above that tip is no longer in the chain from a view's perspective, so it falls back to
Unconfirmedand is classified by the trust predicate.The downside is that this moves some work to the caller. The tip used to build the view must be a block that the
LocalChainalready has, sincecanonical_viewchecks it against the chain's checkpoints. The chain is sparse, it does not keep every height, so a checkpoint that is not there can't be used. Before, the threshold was just a number checked against the current tip and nothing else was needed. Now, if the caller wants to measure from a height the chain never kept, it has to insert or fetch that block first.Notes to the reviewers
In the
Confirmedbranch of balance theif confirmations < min_confirmationspath is dropped, so a confirmed output can now only go toconfirmedorimmature, never to*_pending. Thependingcategories are now reached only from theUnconfirmedbranch.Changelog notice
min_confirmationsparameter fromCanonicalView::balance.Checklists
All Submissions: