psbt: analyzepsbt reports an error when outputs exceed inputs #36569

Open
Ravan002 wants to merge 1 commit from analyzepsbt-outputs-exceed-inputs into master AGit
Ravan002 commented 2026-10-18 20:03:23 +00:00 (Migrated from github.com)

AnalyzePSBT computes fee = in_amt - out_amt once every input amount is known and returns it as is. A PSBT whose outputs are worth more than its inputs therefore gets a negative fee and a next role of updater, even though no wallet can ever sign it into a valid transaction. The function already returns PSBT is not valid. ... for bad input values, unspendable outputs and an out-of-range output total.

An output total above the input total is reported the same way:

before: {"fee": -1.00000000, "next": "updater", ...}
after:  {"next": "creator", "error": "PSBT is not valid. Output amount larger than input amount"}

The functional test builds a PSBT that spends a 1 BTC output into a 2 BTC output and checks the error and the creator role. Without the change the assertion on next fails. rpc_psbt.py, wallet_send.py, wallet_multisig_descriptor_psbt.py, rpc_openrpc.py and the psbt unit tests pass.

`AnalyzePSBT` computes `fee = in_amt - out_amt` once every input amount is known and returns it as is. A PSBT whose outputs are worth more than its inputs therefore gets a negative `fee` and a `next` role of updater, even though no wallet can ever sign it into a valid transaction. The function already returns `PSBT is not valid. ...` for bad input values, unspendable outputs and an out-of-range output total. An output total above the input total is reported the same way: ``` before: {"fee": -1.00000000, "next": "updater", ...} after: {"next": "creator", "error": "PSBT is not valid. Output amount larger than input amount"} ``` The functional test builds a PSBT that spends a 1 BTC output into a 2 BTC output and checks the error and the `creator` role. Without the change the assertion on `next` fails. `rpc_psbt.py`, `wallet_send.py`, `wallet_multisig_descriptor_psbt.py`, `rpc_openrpc.py` and the psbt unit tests pass.
First-time contributor

Base: 924f4c4ac78b0cd3b917c694f42c0651a6f3003a
Head: 82629330135ba7df7ef5fc061ffadca3288043b1

Findings

Major

Check fee optional before GUI dereference (src/node/psbt.cpp:123, head)

For a well-formed PSBT with all UTXOs supplied and outputs exceeding inputs, this change sets analysis.fee to nullopt instead of returning an engaged negative amount. The file and clipboard import path in WalletFrame::gotoLoadPSBT() passes such PSBTs to openWithPSBT(), which proceeds to render them even without a loaded wallet; a false return from FinalizePSBT() does not stop rendering. renderTransaction() then dereferences analysis.fee before displaying transaction status. Dereferencing the disengaged optional is undefined behavior and can abort the GUI in hardened builds. Other invalid PSBTs already expose this caller bug, but this change adds the overspending class. Preserve the validity check, and update the GUI to check optional engagement before dereferencing and add import regression coverage.

Full review report

<!-- forgejo-review-bot:bitcoin/bitcoin --> Base: `924f4c4ac78b0cd3b917c694f42c0651a6f3003a` Head: `82629330135ba7df7ef5fc061ffadca3288043b1` ##### Findings **Major** **Check fee optional before GUI dereference** (src/node/psbt.cpp:123, head) For a well-formed PSBT with all UTXOs supplied and outputs exceeding inputs, this change sets `analysis.fee` to `nullopt` instead of returning an engaged negative amount. The file and clipboard import path in `WalletFrame::gotoLoadPSBT()` passes such PSBTs to `openWithPSBT()`, which proceeds to render them even without a loaded wallet; a false return from `FinalizePSBT()` does not stop rendering. `renderTransaction()` then dereferences `analysis.fee` before displaying transaction status. Dereferencing the disengaged optional is undefined behavior and can abort the GUI in hardened builds. Other invalid PSBTs already expose this caller bug, but this change adds the overspending class. Preserve the validity check, and update the GUI to check optional engagement before dereferencing and add import regression coverage. [Full review report](<https://review.fish.foo/traces/36569-82629330135ba7df7ef5fc061ffadca3288043b1-879-1.html>)
DrahtBot commented 2026-10-19 07:16:03 +00:00 (Migrated from github.com)

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
ACK Yudis-bit, Husteem

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:

  • #36990 (test: replace hard-coded RPC error codes by l0rinc)

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.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36569 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36569.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36569#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [Yudis-bit](https://github.com/bitcoin/bitcoin/pull/36569#issuecomment-6265384243), [Husteem](https://github.com/bitcoin/bitcoin/pull/36569#issuecomment-6287850935) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#36990](https://github.com/bitcoin/bitcoin/pull/36990) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36990.svg"></sub> (test: replace hard-coded RPC error codes by l0rinc) 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.
Yudis-bit commented 2026-10-19 10:44:08 +00:00 (Migrated from github.com)

Tested ACK 8262933013 on Windows. Would it be worth also adding a unit test in psbt_tests?

Tested ACK 8262933013 on Windows. Would it be worth also adding a unit test in psbt_tests?
Husteem commented 2026-10-20 09:28:13 +00:00 (Migrated from github.com)

tACK 8262933

Verified the bug on master (where an overspending PSBT returned a negative fee, empty error, and signer role).
rpc_psbt.py passes on this branch.

The guard in AnalyzePSBT is a good fix.
Also agree with @Yudis-bit - adding a companion unit test in psbt_tests.cpp would be worthwhile for regression coverage here.

tACK 8262933 Verified the bug on `master` (where an overspending PSBT returned a negative fee, empty error, and `signer` role). `rpc_psbt.py` passes on this branch. The guard in `AnalyzePSBT` is a good fix. Also agree with @Yudis-bit - adding a companion unit test in `psbt_tests.cpp` would be worthwhile for regression coverage here.
winterrdog commented 2026-10-20 09:37:53 +00:00 (Migrated from github.com)

@Husteem @Yudis-bit

Sorry for bikeshedding. I am trying to understand what the motive for a unit test is here.. What special thing would the unit test assert that the current functional test does not 🤔 ?

@Husteem @Yudis-bit Sorry for bikeshedding. I am trying to understand what the motive for a unit test is here.. What special thing would the unit test assert that the current functional test does not 🤔 ?
Husteem commented 2026-10-20 13:09:10 +00:00 (Migrated from github.com)

Hi @winterrdog. It's totally fine to enquire!

While I'd say it's a nit, obviously, the thought was mainly direct coverage on the C++ struct (has_sane_fee = false and fee staying unset) without spinning up a test node.

That said, since rpc_psbt.py already asserts both error and next == 'creator' across the RPC boundary, you're pretty right that it covers the regression well enough.

Hi @winterrdog. It's totally fine to enquire! While I'd say it's a nit, obviously, the thought was mainly direct coverage on the C++ struct (`has_sane_fee = false` and `fee` staying unset) without spinning up a test node. That said, since rpc_psbt.py already asserts both `error` and `next == 'creator'` across the RPC boundary, you're pretty right that it covers the regression well enough.
rkrux (Migrated from github.com) requested changes 2026-12-02 13:40:05 +00:00
rkrux (Migrated from github.com) commented 2026-12-02 13:39:55 +00:00

psbt_version=0 doesn't seem required - v2 is the default.

`psbt_version=0` doesn't seem required - v2 is the default.
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36569
No description provided.