#36284 wallet: don't double discard output groups with avoidpartialspends

full analysis

https://github.com/bitcoin/bitcoin/pull/36284 · fjahr · +29/-1 in 2 files, 1 commits · labels: Wallet

Goal

  • Prevent false insufficient funds errors during transaction creation
  • Stops double counting discarded coins when avoidpartialspends or avoid_reuse is active

Fixes a bug where output groups ineligible for coin selection are added twice to `ret_discarded_groups` when `-avoidpartialspends` or `-avoid_reuse` is active. Because `AutomaticCoinSelection` deducts discarded groups from the total balance, double counting them could trigger a false insufficient funds error even when sufficient confirmed funds exist. The fix records discards only during the mixed group pass, accompanied by a regression test.

Problem: When `-avoidpartialspends` or `-avoid_reuse` is set, `GroupOutputs` evaluates groups across both mixed and positive-only maps. Outputs that fail eligibility filters (such as hitting mempool ancestor limits) are pushed to `ret_discarded_groups` in both passes, artificially doubling the discarded coin total and potentially causing transaction creation to fail.

Category: Wallet (#30 of 84)

P3 · bug fix

  • P3 because it fixes false insufficient funds errors during coin selection
  • Affects an edge case combining specific flags with discarded unconfirmed coins

Fixes a bug where coin selection can fail with a false insufficient funds error when using avoidpartialspends. As author fjahr notes, `AutomaticCoinSelection` 'subtracts it twice and could fail with an insufficient funds error even when there would be enough confirmed coins to cover the payment'. While real, this is an edge case requiring specific wallet flags combined with discarded unconfirmed coins, making it a worthwhile fix rather than a release blocker.

Membership: Modifies coin selection output grouping in src/wallet/spend.cpp.

Factors: security/stability 0, bug 2, performance 0, user value 1, leverage 0

Reviewability: Ready

  • Ready to review
  • Small and isolated fix with a functional test, passing CI

The PR is small (+2/-1 in spend.cpp plus a functional test), cleanly isolated, and passes CI.

Author status: active

Agreement: Crickets

  • No reviews or comments yet
  • Recently opened without reviewer engagement

No reviews or comments yet

The PR was recently opened and has not received reviewer engagement yet.

Review verdicts (DrahtBot): 0

Files

27 lines under test/bench/ci.

  • test/functional/wallet_create_tx.py +27/-0
  • src/wallet/spend.cpp +2/-1

Card

Fixes a double-counting bug in output group coin selection under -avoidpartialspends and -avoid_reuse. When output groups are filtered out as ineligible, they were added twice to the discarded list, causing AutomaticCoinSelection to over-subtract and potentially fail with an erroneous insufficient funds error. The patch ensures discarded groups are only recorded once and adds a functional test reproducing the failure. The PR is newly opened and has no reviews yet.

Data

dossier JSON · extract JSON · model openrouter/google/gemini-3.8-flash, generated 2026-09-17T16:24, confidence high, input hash 0df06f17bcdb0d32