Skip to content

fix(wallet)!: avoid overflow panics in create_tx - #628

Open
reez wants to merge 1 commit into
bitcoindevkit:masterfrom
reez:issue-53
Open

reez wants to merge 1 commit into
bitcoindevkit:masterfrom
reez:issue-53

Conversation

@reez

@reez reez commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Description

Addresses the recipient and initial fee overflow cases from #53 "All across the crate, unchecked arithmetic is used. The unchecked arithmetic routines of rust-bitcoin's Amount type will panic if the result of an operation overflows. This is an issue for externally-provided values, such as for instance recipient amounts or fees” by using checked arithmetic in create_tx and returns CreateTxError::AmountOverflow including for targets above Amount::MAX_MONEY.

Notes to the reviewers

The Amount::MAX_MONEY cap is there because a recipient of Amount::MAX - 100 at 1 sat/vB passes the checked additions and still panics inside coin selection. Targets above MAX_MONEY that previously returned InsufficientFunds now return AmountOverflow.

Foreign UTXOs with extreme values, coin selection internals, and PsbtUtils::fee_amount (#558, #559) can still panic and are out of scope here, so #53 should stay open.

And then thinking about the direction for TxBuilder is this worth landing on master and a 3.x version?

Changelog notice

  • Add CreateTxError::AmountOverflow returned on recipient or fee overflow and for targets above Amount::MAX_MONEY which previously returned InsufficientFunds. CreateTxError is also now #[non_exhaustive], a breaking change.

Before submitting

@codecov

codecov Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.90%. Comparing base (e0c4b37) to head (9df73a1).
⚠️ Report is 9 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #628      +/-   ##
==========================================
+ Coverage   81.69%   81.90%   +0.21%     
==========================================
  Files          25       25              
  Lines        6265     6300      +35     
  Branches      299      302       +3     
==========================================
+ Hits         5118     5160      +42     
+ Misses       1041     1035       -6     
+ Partials      106      105       -1     
Flag Coverage Δ
rust 81.90% <100.00%> (+0.21%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@reez reez added this to the Wallet 4.0.0-alpha.0 milestone Nov 5, 2026
@reez
reez marked this pull request as ready for review November 5, 2026 21:26
@reez
reez requested a review from a team as a code owner November 5, 2026 21:26

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

tACK d3a600c
Thanks!

  • Should we make CreateTxError #[non_exhaustive]? e.g. #493 adds another variant
  • Fee math in coin selection can still overflow, worth a follow-up.

@noahjoeris noahjoeris moved this from Triage to Needs Review in BDK Wallet Nov 10, 2026
@reez

reez commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

tACK d3a600c Thanks!

  • Should we make CreateTxError #[non_exhaustive]? e.g. #493 adds another variant
  • Fee math in coin selection can still overflow, worth a follow-up.

Thanks for the review and tACK

Totally agree on #[non_exhaustive], added it to CreateTxError now.

And then for coin selection Q, this PR only addresses recipient amounts and initial fee calculations so I’m leaving #53 open for the remaining cases like coin selection.

@muawiya1 muawiya1 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

tACK 9df73a1

I reviewed the PR, and all the PR's tests passed locally on my macOS. To confirm the bug on master, I added 4 separate tests covering recipient and fee overflows since the PR's tests uses a new error variant (AmountOverflow) that does not exist on master, all tests failed on master and passed on the PR branch locally.

@reez

reez commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

tACK 9df73a1

I reviewed the PR, and all the PR's tests passed locally on my macOS. To confirm the bug on master, I added 4 separate tests covering recipient and fee overflows since the PR's tests uses a new error variant (AmountOverflow) that does not exist on master, all tests failed on master and passed on the PR branch locally.

THanks!

@busayo-od

busayo-od commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Fee math in coin selection can still overflow, worth a follow-up.

I reproduced cases where explicit algorithms (LargestFirst, OldestFirst, SingleRandomDraw) panic in debug builds and silently wrap in release builds when fee calculations overflow. In release, this results in an incorrect InsufficientFunds.needed value. These cases use extreme fee rates. The default algorithm also panics on UTXO values above i64::MAX.

One option is to introduce a CoinSelectionError with separate variants for InsufficientFunds and AmountOverflow, and update CoinSelectionAlgorithm to return it. This would allow us to report overflow explicitly, but it would be a breaking change for existing implementations and callers.

Another option is to keep the existing API and map arithmetic overflow to InsufficientFunds when returning from coin selection. This avoids a public API change, but the actual needed amount may not be representable, so we'd have to provide a value that doesn't reflect the actual amount. Which approach would be preferable?

There's also decide_change. Making it fallible would be another public API change. Should it be included as well, or should its current signature be kept? Returning NoChange on overflow isn't suitable because Excess::NoChange requires a change_fee that cannot be calculated.

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

tACK 9df73a1

Comment thread src/wallet/error.rs
impl core::error::Error for MiniscriptPsbtError {}

#[derive(Debug)]
#[non_exhaustive]

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think so!

I'll keep the change here focused on CreateTxError, but:

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

tACK 9df73a1

nit: Since adding #[non_exhaustive] to CreateTxError is a breaking change, I think it is reasonable to add it to the Changelog notice too.

@Arowolokehinde

Copy link
Copy Markdown
Contributor

tACK 9df73a1

I ran the tests and everything passed locally.

I also tested master and this branch in both debug and release mode. I noticed something on master that this PR fixes.

if you build a transaction with fee_rate(FeeRate::MAX) (the highest possible fee rate), a debug build panics, but a release build does not. Instead, the number silently wraps around, and the wallet builds a transaction with a 0 sat fee and returns Ok without showing any error at all.

With this PR, both debug and release builds handle it correctly and return AmountOverflow.

This branch has not been deployed

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

Labels

None yet

Projects

Status: Needs Review

Development

Successfully merging this pull request may close these issues.

7 participants