Skip to content

fix(wallet): exclude unconfirmed v3 outputs from non-v3 coin selection#442

Open
EliteCoder18 wants to merge 2 commits intobitcoindevkit:masterfrom
EliteCoder18:fix/truc-utxo-filter
Open

fix(wallet): exclude unconfirmed v3 outputs from non-v3 coin selection#442
EliteCoder18 wants to merge 2 commits intobitcoindevkit:masterfrom
EliteCoder18:fix/truc-utxo-filter

Conversation

@EliteCoder18
Copy link
Copy Markdown

Description

BIP-431 (TRUC) requires that any transaction spending an unconfirmed v3 output must itself be v3. BDK's coin selection had no awareness of this and would freely include unconfirmed v3 parent outputs when building standard non-v3 transactions, causing bitcoind to reject the broadcast.

This PR threads tx_version into filter_utxos to exclude such UTXOs when the target transaction is not v3. Confirmed outputs are unaffected.

Fixes #419.

Notes to the reviewers

The fix is intentionally minimal only the cross-version spending restriction from BIP-431 is addressed here. The broader TRUC topological constraints (cluster size, child vbyte limit) are out of scope for this PR and can be tracked separately.

Manually selected UTXOs via TxBuilder::add_utxo bypass filter_utxos entirely and are not covered by this fix. Callers are responsible for not adding unconfirmed v3 outputs to non-v3 transactions manually.

Changelog notice

  • Fixed: filter_utxos now excludes unconfirmed UTXOs from v3 parent transactions when constructing non-v3 transactions, preventing TRUC policy violations at broadcast.

Checklists

All Submissions:

  • I've signed all my commits
  • I followed the contribution guidelines
  • I ran just p before pushing

Bugfixes:

  • This pull request breaks the existing API
  • I've added tests to reproduce the issue which are now passing
  • I'm linking the issue being fixed by this PR

When a v3 (TRUC) transaction produced change back into the wallet, that
unconfirmed output entered coin selection like any other UTXO. Building
a subsequent non-v3 transaction that spent it caused bitcoind to reject
the broadcast with "TRUC-violation, non-version=3 tx cannot spend from
version=3 tx" (BIP-431).

Thread `tx_version` into `filter_utxos` so that unconfirmed UTXOs whose
parent is version 3 are excluded when building a non-v3 transaction.
Confirmed outputs are unaffected since TRUC rules only apply while the
parent remains unconfirmed.

Fixes bitcoindevkit#419.
Add two tests covering BIP-431 (TRUC) coin selection behavior.

`test_create_tx_non_v3_excludes_unconfirmed_v3_utxos` verifies that a
non-v3 transaction cannot coin-select an unconfirmed v3 parent output,
while a v3 transaction targeting the same output succeeds.

`test_create_tx_non_v3_allows_unconfirmed_non_v3_utxos` verifies that
unconfirmed outputs from non-v3 parents remain eligible for standard
transactions, preserving existing behavior.
@ValuedMammal ValuedMammal added the bug Something isn't working label Apr 14, 2026
@ValuedMammal ValuedMammal added this to the Wallet 3.1.0 milestone Apr 14, 2026
@ValuedMammal ValuedMammal moved this to In Progress in BDK Wallet Apr 14, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 14, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.08%. Comparing base (5df32ca) to head (07d6718).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #442      +/-   ##
==========================================
+ Coverage   80.04%   80.08%   +0.04%     
==========================================
  Files          24       24              
  Lines        5336     5349      +13     
  Branches      242      244       +2     
==========================================
+ Hits         4271     4284      +13     
  Misses        987      987              
  Partials       78       78              
Flag Coverage Δ
rust 80.08% <100.00%> (+0.04%) ⬆️

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

☔ View full report in Codecov by Sentry.
📢 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.

@ValuedMammal ValuedMammal moved this from In Progress to Needs Review in BDK Wallet Apr 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

Status: Needs Review

Development

Successfully merging this pull request may close these issues.

Coin selection includes unconfirmed v3/TRUC outputs in v2 transactions, causing mempool rejection

2 participants