Skip to content

wallet: fix mixed-input transaction accounting in history RPCs - #34872

Open
w0xlt wants to merge 16 commits into
bitcoin:masterfrom
w0xlt:wallet-mixed-input-history-only
Open

w0xlt wants to merge 16 commits into
bitcoin:masterfrom
w0xlt:wallet-mixed-input-history-only

Conversation

@w0xlt

@w0xlt w0xlt commented Mar 19, 2026 •

Copy link
Copy Markdown
Contributor

When a wallet owns only some inputs of a transaction, such as in CoinJoins, payjoins, or other collaborative transactions, wallet history RPCs currently apply normal send accounting.

That works when all inputs are wallet-owned, but breaks for mixed-input transactions.

Example:

Inputs:

1.00 BTC wallet-owned
2.00 BTC foreign

Outputs:

0.80 BTC wallet-owned
2.19 BTC non-wallet

The wallet can safely know:

wallet_debit  = 1.00
wallet_credit = 0.80
wallet_net    = -0.20

But it cannot reliably know, from wallet history alone, the foreign input value, the total fee, the wallet's fee share, or which non-wallet output should be attributed as the user's payment. This is especially important after restore, rescan, or descriptor import, where any local transaction-intent metadata may be missing.

This PR adds a conservative fallback for that no-metadata case:

  • classify wallet transactions as having no wallet inputs, partial wallet inputs, or all wallet inputs
  • keep existing send/fee accounting when all inputs are wallet-owned
  • also keep normal per-output send/fee accounting for mixed-input transactions when every non-wallet input has a known zero value
  • otherwise report a single unattributed aggregate send entry with no address, no vout, and no fee, carrying the negative total of wallet-owned inputs spent
  • report wallet-owned outputs as receive entries, so the transaction's entries sum to the wallet's net change
  • expose involves_mixed_inputs, wallet_debit, and wallet_credit
  • document that vout and fee are optional when attribution is unavailable

This does not try to fully solve collaborative transaction attribution. A richer layer with foreign-output / fee-share / intent metadata would still be useful for wallet-created transactions. This PR only makes the fallback honest when that metadata is unavailable.

Addresses #14136

@DrahtBot

DrahtBot commented Mar 19, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
Concept NACK achow101
Concept ACK rkrux, arejula27
Stale ACK Bicaru20

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #37018 (descriptor: fix InferScript creates invalid descriptors for invalid pubkeys by Eunovo)
  • #36819 ([RFC] enable clang-tidy optin.performance.Padding by fanquake)
  • #36257 (qa: assert_equal -> assert_true/assert_false by hodlinator)
  • #35786 (wallet: drop spent parents redundant cache invalidation and notification by furszy)
  • #35302 (Silent Payments: Sending (take 2) by Eunovo)
  • #27865 (wallet: Track no-longer-spendable TXOs separately by achow101)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@luke-jr

luke-jr commented Mar 20, 2026

Copy link
Copy Markdown
Contributor

This won't show even the true send anymore... not sure that's a good idea.

See #25991 for what is needed to fix this properly.

@w0xlt

w0xlt commented Mar 22, 2026 •

Copy link
Copy Markdown
Contributor Author

The goal here, in this PR, is a smaller immediate fix: for mixed-input transactions, the history RPCs should stop reporting send entries and full fees when the wallet cannot attribute them objectively.

#25991 is an interesting direction, but as written it depends on manually providing additional wallet-local per-output metadata.
Without that metadata, we still need a safe fallback, and that is what this PR provides.

@rkrux rkrux 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.

As I read the related issue #14136, it's unfortunate that the RPC shows incorrect information in the result - glad that this PR tries to address it.

However, I'm leaning towards a NACK because this PR removes the send category details of such transactions altogether, which is not correct. The wallet does participate (though partially) in sending funds within these transactions and not showing those can be problematic as well.

From #34872 (comment)

Fixes this by introducing CachedTxIsFromMeForAccounting,

I am also not in favour of having two different views of CachedTxIsFromMe - one without accounting and one with. Having two views is also a sign that there is mismatch between what the wallet shows to the user and what the wallet does for the user.

From #34872 (comment):

Without that metadata, we still need a safe fallback, and that is what this PR provides.

Due to the above reasons, I don't believe this is a safe fallback, and it only replaces one issue by another.

From #34872 (comment)

This is because the accounting code treats any nDebit > 0 (i.e. any wallet-owned input) as a full wallet spend.

It appears that this assumption made by the wallet doesn't hold true anymore and maybe it needs to be reworked for this issue to be completely fixed.

@w0xlt

w0xlt commented Mar 23, 2026 •

Copy link
Copy Markdown
Contributor Author

@rkrux Thanks for the review. I agree this isn’t a complete solution for mixed-input accounting.

The goal here is narrower: to prevent history RPCs from reporting send entries and full fees that the wallet cannot objectively attribute from the transaction alone.

So I see this as an improvement over the current behavior.

A complete solution likely requires additional attribution metadata. However, that’s tricky - even if we add something like #25991, or a more automated way to set it, the metadata would not survive an importdescriptors restore, since there’s currently no way to import it.

In those cases, we would still need a conservative fallback for history RPCs when attribution is unavailable.

Do you see another way to address this without relying on additional metadata?

@achow101

Copy link
Copy Markdown
Member

NACK

I think it's even more misleading to not show any send entries at all for transactions where we participate in the inputs.

@murchandamus

Copy link
Copy Markdown
Member

At first glance, I would expect our wallet to be able to recognize which inputs are ours, and I’m astonished that it would just assume that all of the inputs must be ours when it recognizes one. Would it perhaps be possible to recognize such transactions as “multi user transactions” and only apply the inputs that came from our wallet as debits? If our wallet’s balance is decreased by the transaction, we might recognize any outputs from the transaction to our wallet as change output, whereas if the wallet’s balance is increased by the transaction (e.g., a Payjoin that paid us), we would treat the output as an incoming payment. The transaction should then be labeled as having us paid the amount that our wallet balance increased.

@achow101

achow101 commented May 19, 2026 •

Copy link
Copy Markdown
Member

it would just assume that all of the inputs must be ours when it recognizes one

That's not really what it is doing. It is correctly treating transactions that have any inputs belonging to the wallet as that transaction belonging to the wallet. The "assumption" comes from CachedTxGetDebit only retrieving the total amount of our inputs, which only works when all of the inputs of the transaction belong to the wallet.

But without storing more information, there is nothing that we can do to fix that. It shouldn't be astonishing that we are unable to calculate fees when the wallet does not know how much value the inputs that it doesn't know about are providing to the transaction.

The transaction should then be labeled as having us paid the amount that our wallet balance increased.

This isn't about annotating Bitcoin transactions, it's about annotating logical financial transactions and trying to determine what they are from a single Bitcoin transaction. Because a Bitcoin transaction can have multiple outputs, it can have multiple logical transactions, and the wallet tries to display these as such. For example, a multi party transaction could conceivably contain an output created by someone else in that transaction which pays us, while the same transaction simultaneously has an output that we create paying someone else. These should show as two entries in listtransactions - one showing a receive and one showing the send. The issue is with annotating the rest of the outputs (we don't store the logical transactions, so how do we know that the remaining outputs are not sends initiated by the user), and showing the correct amount for the fee (we don't know about the inputs that aren't ours, thus we cannot accurately calculate the fees).

The solution presented in this PR is to omit all of that logical transaction information entirely for multi party transactions, which I disagree with.

@w0xlt
w0xlt force-pushed the wallet-mixed-input-history-only branch from 5c5176e to a302846 Compare May 20, 2026 13:35
@w0xlt

w0xlt commented May 20, 2026 •

Copy link
Copy Markdown
Contributor Author

@achow101 @murchandamus Thanks for the feedback.

I pushed a new proposal that addresses it.

As I understand it, a complete solution has two layers:

  1. A conservative fallback for mixed-input transactions when the wallet has no extra attribution metadata.


2. A richer metadata layer, e.g. foreign-output / ownership / fee-share marks, for wallet-created collaborative transactions.

The idea here to solve the layer 1 only.
Layer 2 is still useful, but it cannot replace the fallback. If a wallet is restored from descriptors, rescanned, or imported without the original local metadata, the wallet still needs a reliable way to report history without inventing send outputs or fees.

@w0xlt

w0xlt commented May 20, 2026 •

Copy link
Copy Markdown
Contributor Author

The new proposal:



In master, wallet history accounting effectively does this when any input is ours:

wallet_debit = sum(wallet-owned inputs)
fee = wallet_debit - total_outputs
send entries = all non-change outputs

That works only when all inputs are wallet-owned.

Example:

Inputs:

    1.00 BTC mine
    2.00 BTC foreign

Outputs:

    0.80 BTC mine
    2.19 BTC foreign

Master can calculate wallet_debit = 1.00, but it does not know/store the foreign input value reliably in wallet history. So subtracting all outputs from only wallet-owned inputs gives wrong accounting:

fee = 1.00 - 2.99 = -1.99

It can also report foreign outputs as wallet send entries, even though the wallet cannot know from the transaction alone whether those outputs are wallet's payment, another participant’s change, or something else.

This new push changes the previous fallback model to:

wallet_debit  = sum(wallet-owned inputs)
wallet_credit = sum(wallet-owned outputs)
amount        = wallet_credit - wallet_debit

For the same example:

wallet_debit  = 1.00
wallet_credit = 0.80
amount        = -0.20
fee_known     = false
category      = mixed

So the wallet reports the part it can prove: “the wallet balance went down by 0.20 BTC.” It does not guess the fee share or recipient attribution.


@w0xlt

w0xlt commented May 20, 2026

Copy link
Copy Markdown
Contributor Author

Moving this to draft for further discussion.

@w0xlt
w0xlt marked this pull request as draft May 20, 2026 13:49
@w0xlt

w0xlt commented May 20, 2026

Copy link
Copy Markdown
Contributor Author

PR description updated.

@rkrux

rkrux commented May 20, 2026

Copy link
Copy Markdown
Contributor

Do you see another way to address this without relying on additional metadata?

Sorry, I had forgotten about this PR. I would prefer a basic conservative solution first without relying on additional metadata by letting the wallet return correct information (as much as it can) while not showing incorrect responses based on any missing transaction data.

Based on a cursory glance, the new proposal seems to be in this^ direction, will review.

@w0xlt

w0xlt commented May 20, 2026

Copy link
Copy Markdown
Contributor Author

CI error unrelated

@rkrux

rkrux commented May 21, 2026

Copy link
Copy Markdown
Contributor

Concept ACK 29fcb54

The new approach lgtm, will review when taken out of draft.

@w0xlt
w0xlt marked this pull request as ready for review May 21, 2026 17:05
@w0xlt

w0xlt commented May 21, 2026

Copy link
Copy Markdown
Contributor Author

Done.

@DrahtBot
DrahtBot requested review from arejula27 and rkrux September 29, 2026 14:35
@w0xlt

w0xlt commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

@arejula27 Thanks for the review and the suggestion! I like the idea of showing the net change alongside the wallet’s spent and created outputs.

This PR already has quite a bit of complexity, so I’d prefer to explore that format in a follow-up. That would give the interface and compatibility questions their own discussion while keeping this PR focused on fixing the incorrect accounting.

Would you be interested in opening a draft PR on top of this one? I’d concept ACK that direction and be happy to help review it.

@w0xlt
w0xlt force-pushed the wallet-mixed-input-history-only branch from 66763e7 to 9d82e07 Compare October 6, 2026 21:44
@arejula27

Copy link
Copy Markdown
Contributor

Thanks! Yes, I'd be happy to open a draft PR on top of this one, i will try to do it this week :D

For another follow-up (or in that one but i think it fit betters on its own) I'd also like to propose moving the classification logic into the wallet so the GUI can share it. Today TransactionRecord::decomposeTransaction redoes the same split, so the two implementations can diverge. Exposing the result through interfaces::WalletTx would let the RPCs and the GUI use the same logic. What do you think?

@w0xlt

w0xlt commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks! Looking forward to the draft.

I agree that sharing the classification with the GUI would be useful, and I’d support keeping that as its own follow-up.

There’s already a starting point in this PR: CachedTxGetHistoryAccounting computes input ownership, debit, credit, and an optional attributable fee in the wallet. Exposing those facts through interfaces::WalletTx could let the GUI use the same accounting rules as the RPCs, while leaving presentation decisions with each caller.

One useful test case would be a transaction where we spend 1 BTC and the only foreign input is known to be worth 0 BTC. That foreign input adds no money, so all the funds still come from our wallet. This PR recognizes that and can report the outputs and fee normally.

The GUI on master currently puts this transaction into its generic “Other” handling because one input isn’t ours. Sharing the wallet’s accounting result would let it recognize that all the funds still came from our wallet.

I’d concept ACK that direction too, and would be happy to help review both drafts.

w0xlt and others added 16 commits November 11, 2026 13:56
Add a cached helper that classifies wallet transaction inputs as all,
partial, or none owned by the wallet. This lets history code distinguish
mixed-input transactions without repeating input ownership scans.
Add a cached history accounting helper that returns input ownership,
wallet debit, wallet credit, and fee information in one place before
the RPC code consumes it.
Invalidate cached input ownership for wallet transactions that spend
outputs from a newly inserted parent, so later imports can refresh child
accounting.
Rename ListTransactions to AppendWalletTxEntries to clarify that the
helper appends entries for a single wallet transaction to the
caller-provided result container (ret).

The helper is shared by listtransactions, listsinceblock, and
gettransaction. The public RPC names and behavior are unchanged.
Make the existing caller-held wallet lock requirement explicit and remove
the redundant internal lock. AppendWalletTxEntries is the only caller and
already requires cs_wallet. Leave accounting behavior unchanged.
Use aggregate wallet debit for mixed-input transaction history when
outputs and fees cannot be attributed to specific inputs. Expose wallet
debit and credit fields so callers can identify the conservative
accounting.

Keep one receive entry for each wallet-owned output, showing its amount,
address, label, and vout. Keep the send entry to record the wallet inputs
used in the transaction. Use this format even when every output belongs
to the wallet.

Return the accounting from CachedTxGetAmounts to the RPC helper so it
can reuse the result when appending history entries.

Co-authored-by: Bicaru20 <[email protected]>
Add the functional test fixture and shared assertions for conservative
mixed-input history in gettransaction, listtransactions, and listsinceblock.
Check both participants, label filtering, confirmation, and reorg removal
and reinclusion.
Check that a wallet-owned change output remains a receive entry beside the
aggregate send. Their amounts must sum to the wallet's net change, both
before and after confirmation.
Check positive, zero, and negative net changes when every output belongs
to the wallet. Preserve the aggregate send and each output's receive
metadata, including label filtering, before and after confirmation.
Track whether every non-wallet input of a mixed-input wallet
transaction is known to spend a zero-value output.

This is not used for attribution yet. It lets later history accounting
differentiate unknown or nonzero foreign contributions from foreign
inputs that provably cannot fund outputs or fees.
Use normal per-output send and fee accounting for mixed-input
transactions when every non-wallet input is known to have zero value.

Unknown or positive-value foreign inputs remain conservative because
the wallet cannot attribute recipient outputs or its fee share from the
transaction alone.
Add functional coverage for a mixed-input transaction that spends a
wallet-owned input together with a zero-value pay-to-anchor input.

The test asserts that the wallet keeps normal send and fee entries,
while still marking the transaction as involving mixed inputs.
Exercise the case where a wallet first sees a mixed-input child
transaction before seeing the parent that creates a zero-value foreign
input.

After the parent is imported, the child history accounting must be
recomputed and switch from conservative aggregate accounting to normal
send and fee attribution.
Add coverage showing that a mixed-input transaction remains
conservatively reported when the wallet knows a foreign input has
nonzero value but still cannot attribute the fee share.
Add coverage for mixed-input transactions where the wallet-owned input
has zero value, ensuring history still reports the wallet credit with a
zero aggregate debit.
Document how wallet history RPCs report transactions that spend both
wallet-owned and non-wallet inputs.

Mention the normal attribution allowed for known zero-value foreign
inputs, the conservative aggregate fallback, and the new mixed-input
metadata fields.
@w0xlt
w0xlt force-pushed the wallet-mixed-input-history-only branch from 9d82e07 to e9b990c Compare November 11, 2026 22:18
@w0xlt

w0xlt commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

Rebased

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants