Skip to content

fix(key-wallet): skip already used accounts when funding a transaction - #930

Closed
ZocoLini wants to merge 1 commit into
devfrom
fix/double-utxo-spend
Closed

fix(key-wallet): skip already used accounts when funding a transaction#930
ZocoLini wants to merge 1 commit into
devfrom
fix/double-utxo-spend

Conversation

@ZocoLini

@ZocoLini ZocoLini commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • Bug Fixes

    • Prevented duplicate account preferences from causing the same unspent funds to be selected multiple times during transaction creation.
    • Improved handling when no eligible funding accounts are available.
  • Tests

    • Added coverage confirming that naming the same account multiple times does not duplicate its funds during coin selection.

@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Funding now deduplicates repeated AccountTypePreference values with a HashSet. The transaction builder records successfully funded preferences and preserves no-funding error handling. A regression test verifies that repeated BIP44 entries do not duplicate UTXOs.

Changes

Funding preference deduplication

Layer / File(s) Summary
Track unique funding preferences
key-wallet/src/wallet/managed_wallet_info/transaction_building.rs
AccountTypePreference derives Hash. Funding skips repeated preferences and uses the set to determine whether any account was funded.
Validate duplicate preference handling
key-wallet/src/wallet/managed_wallet_info/transaction_building.rs
An asynchronous regression test verifies that listing BIP44 twice does not duplicate its 300k UTXO. A 400k transaction returns an insufficient-funds or coin-selection error.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Suggested labels: ready-for-review

Suggested reviewers: bfoss765, quantumexplorer, xdustinface

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: preventing already used accounts from being reused when funding transactions.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/double-utxo-spend

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@key-wallet/src/wallet/managed_wallet_info/transaction_building.rs`:
- Around line 915-922: Update the assertion around result to avoid consuming the
non-Copy Result before constructing its diagnostic: borrow result in matches!,
or compute the input count before the assertion and reuse it in the message.
Preserve the existing insufficient-funds/coin-selection validation and
diagnostic output.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 3e61a20e-f653-41df-9843-d0bd0a2d69d0

📥 Commits

Reviewing files that changed from the base of the PR and between 09904a6 and 2ce2e8a.

📒 Files selected for processing (1)
  • key-wallet/src/wallet/managed_wallet_info/transaction_building.rs

Comment on lines +915 to +922
assert!(
matches!(
result,
Err(BuilderError::InsufficientFunds { .. }) | Err(BuilderError::CoinSelection(_))
),
"300k must not cover a 400k target, got: {:?}",
result.map(|(tx, _)| tx.input.len())
);

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.

🎯 Functional Correctness | 🔴 Critical | ⚡ Quick win

Avoid moving result before building the diagnostic.

matches!(result, ...) consumes the non-Copy Result. The later result.map(...) use then causes a compile error. Compute the input count before the assertion, or borrow result in the match.

Proposed fix
+        let input_count = result.as_ref().map(|(tx, _)| tx.input.len());
         assert!(
             matches!(
                 result,
                 Err(BuilderError::InsufficientFunds { .. }) | Err(BuilderError::CoinSelection(_))
             ),
             "300k must not cover a 400k target, got: {:?}",
-            result.map(|(tx, _)| tx.input.len())
+            input_count
         );
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
assert!(
matches!(
result,
Err(BuilderError::InsufficientFunds { .. }) | Err(BuilderError::CoinSelection(_))
),
"300k must not cover a 400k target, got: {:?}",
result.map(|(tx, _)| tx.input.len())
);
let input_count = result.as_ref().map(|(tx, _)| tx.input.len());
assert!(
matches!(
result,
Err(BuilderError::InsufficientFunds { .. }) | Err(BuilderError::CoinSelection(_))
),
"300k must not cover a 400k target, got: {:?}",
input_count
);
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@key-wallet/src/wallet/managed_wallet_info/transaction_building.rs` around
lines 915 - 922, Update the assertion around result to avoid consuming the
non-Copy Result before constructing its diagnostic: borrow result in matches!,
or compute the input count before the assertion and reuse it in the message.
Preserve the existing insufficient-funds/coin-selection validation and
diagnostic output.

@codecov

codecov Bot commented Aug 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 75.22%. Comparing base (09904a6) to head (2ce2e8a).

Additional details and impacted files
@@            Coverage Diff             @@
##              dev     #930      +/-   ##
==========================================
- Coverage   75.23%   75.22%   -0.01%     
==========================================
  Files         328      328              
  Lines       77767    77793      +26     
==========================================
+ Hits        58507    58522      +15     
- Misses      19260    19271      +11     
Flag Coverage Δ
core 77.29% <ø> (ø)
ffi 49.85% <ø> (+<0.01%) ⬆️
rpc 20.00% <ø> (ø)
spv 91.33% <ø> (-0.07%) ⬇️
wallet 76.61% <100.00%> (+0.02%) ⬆️
Files with missing lines Coverage Δ
...wallet/managed_wallet_info/transaction_building.rs 94.07% <100.00%> (+0.26%) ⬆️

... and 6 files with indirect coverage changes

@ZocoLini

ZocoLini commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator Author

already included in #929

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant