-
Notifications
You must be signed in to change notification settings - Fork 82
fix(wallet): retry coin selection on NoChange to avoid dust/zero drai… #448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alienx5499
wants to merge
4
commits into
bitcoindevkit:master
Choose a base branch
from
alienx5499:fix/376-p2a-drain-coinselection
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
55d050d
fix(wallet): retry coin selection on NoChange to avoid dust/zero drai…
alienx5499 98f32a8
refactor(wallet): split dust drain retry path and rename selection_re…
alienx5499 235d336
fix(wallet): keep last successful NoChange result in dust-drain retry…
alienx5499 11ab2d8
fix(wallet): continue dust-drain retries after failed optional UTXO p…
alienx5499 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| use bdk_wallet::test_utils::*; | ||
| use bdk_wallet::KeychainKind; | ||
| use bitcoin::{hashes::Hash, psbt, Amount, OutPoint, ScriptBuf, TxOut, Weight}; | ||
|
|
||
| // Ensures coin selection pulls a local UTXO when drain-only selection would produce dust. | ||
| #[test] | ||
| fn test_drain_to_pulls_local_utxo_when_foreign_only_dust() { | ||
| let (mut wallet, _) = get_funded_wallet_wpkh(); | ||
| let drain_spk = wallet | ||
| .next_unused_address(KeychainKind::External) | ||
| .script_pubkey(); | ||
|
|
||
| let witness_utxo = TxOut { | ||
| value: Amount::from_sat(500), | ||
| script_pubkey: ScriptBuf::new_p2a(), | ||
| }; | ||
| // Remember to include this as a "floating" txout in the wallet. | ||
| let outpoint = OutPoint::new(Hash::hash(b"foreign-p2a-prev"), 1); | ||
| wallet.insert_txout(outpoint, witness_utxo.clone()); | ||
| let satisfaction_weight = Weight::from_wu(71); | ||
| let psbt_input = psbt::Input { | ||
| witness_utxo: Some(witness_utxo), | ||
| ..Default::default() | ||
| }; | ||
|
|
||
| let mut tx_builder = wallet.build_tx(); | ||
| tx_builder | ||
| .add_foreign_utxo(outpoint, psbt_input, satisfaction_weight) | ||
| .unwrap() | ||
| .only_witness_utxo() | ||
| .fee_absolute(Amount::from_sat(400)) | ||
| .drain_to(drain_spk); | ||
|
|
||
| let psbt = tx_builder.finish().unwrap(); | ||
| let tx = psbt.unsigned_tx; | ||
| assert!(tx.input.len() >= 2); | ||
| assert!(!tx.output.is_empty()); | ||
| assert!( | ||
| tx.input.iter().any(|txin| txin.previous_output == outpoint), | ||
| "foreign_utxo should be in there" | ||
| ); | ||
| } | ||
|
|
||
| // Foreign value equals fee: no satoshis left for a drain output until a wallet UTXO is included. | ||
| #[test] | ||
| fn test_drain_to_pulls_local_utxo_when_foreign_value_equals_fee() { | ||
| let (mut wallet, _) = get_funded_wallet_wpkh(); | ||
| let drain_spk = wallet | ||
| .next_unused_address(KeychainKind::External) | ||
| .script_pubkey(); | ||
|
|
||
| let witness_utxo = TxOut { | ||
| value: Amount::from_sat(200), | ||
| script_pubkey: ScriptBuf::new_p2a(), | ||
| }; | ||
| let outpoint = OutPoint::new(Hash::hash(b"foreign-p2a-prev-200"), 1); | ||
| wallet.insert_txout(outpoint, witness_utxo.clone()); | ||
| let satisfaction_weight = Weight::from_wu(71); | ||
| let psbt_input = psbt::Input { | ||
| witness_utxo: Some(witness_utxo), | ||
| ..Default::default() | ||
| }; | ||
|
|
||
| let mut tx_builder = wallet.build_tx(); | ||
| tx_builder | ||
| .add_foreign_utxo(outpoint, psbt_input, satisfaction_weight) | ||
| .unwrap() | ||
| .only_witness_utxo() | ||
| .fee_absolute(Amount::from_sat(200)) | ||
| .drain_to(drain_spk); | ||
|
|
||
| let psbt = tx_builder.finish().unwrap(); | ||
| let tx = psbt.unsigned_tx; | ||
| assert!(tx.input.len() >= 2); | ||
| assert!(!tx.output.is_empty()); | ||
| assert!(tx.input.iter().any(|txin| txin.previous_output == outpoint)); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the retry loop, if
coin_selecterrors after promoting an optional UTXO, the code breaks out by returninglast_successful_result(which is necessarily anExcess::NoChangeresult) rather than continuing to try other optional UTXOs. This can cause false failures when the last optional popped has negative effective value (BnB explicitly filters these fromoptional_utxos), since promoting it torequiredcan triggerInsufficientFundseven though another optional UTXO could have resolved theNoChangedust case. Consider treating a failed promoted UTXO as “skipped” (remove it fromrequired_for_attemptand continue with remaining optionals), and only return an error once all candidates are exhausted.