Repository navigation
wallet: skip APS when no partial spend exists - #34405
8144225309 wants to merge 2 commits into
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
d6edd15 to
53de94f
Compare
| @@ -119,12 +119,14 @@ def skip_test_if_missing_module(self): | |||
| self.skip_if_no_wallet() | |||
| self.skip_if_running_under_valgrind() | |||
|
|
|||
| def get_tracepoints(self, expected_types): | |||
| def get_tracepoints(self, expected_types, wallet_name=None): | |||
There was a problem hiding this comment.
The default for wallet_name is essentially self.default_wallet_name, so set it to that instead of using None.
There was a problem hiding this comment.
self isn't available at function definition time in Python. The =None with internal check matches the pattern used elsewhere in the test framework (e.g., extra_args in add_nodes()).
53de94f to
598b061
Compare
598b061 to
f65dc1b
Compare
f65dc1b to
b6319c6
Compare
|
@8144225309 this has been in need of a rebase for a while. Are you still interested in working on this change? |
|
Thank you for pinging me, resolving conflicts now. |
b6319c6 to
fb4fc43
Compare
fb4fc43 to
8a30292
Compare
|
Rebased onto master. The only conflict was CreatedTransactionResult moving from The new wallet_groups.py coverage is a separate commit; the tracepoint test changes range-diff |
polespinasa
left a comment
There was a problem hiding this comment.
concept ACK 8a30292
I am not really familiar with this part of the wallet codebase but it looks pretty good to me.
Left a few suggestions before ACKing it :)
| for (const auto& coin : available_coins.All()) { | ||
| spk_counts[coin.txout.scriptPubKey].first++; | ||
| } | ||
| // AvailableCoins skips manually selected coins, so count them here too. | ||
| for (const auto& coin : preset_inputs.All()) { | ||
| spk_counts[coin.txout.scriptPubKey].first++; | ||
| } |
There was a problem hiding this comment.
in e382330 wallet: skip APS when no partial spend exists
By calling available_coins.All() and preset_inputs.All() (both are CoinsResult::All()) you are creating two new temporary vectors of COutput.
You can just iterate the original vectors, with something like:
$ git diff
diff --git a/src/wallet/spend.cpp b/src/wallet/spend.cpp
index 3dd562ba98..e09c741538 100644
--- a/src/wallet/spend.cpp
+++ b/src/wallet/spend.cpp
@@ -1254,13 +1254,16 @@ static util::Result<CreatedTransactionResult> CreateTransactionInternal(
bool has_partial_spend{false};
if (coin_control.m_allow_other_inputs) {
std::map<CScript, std::pair<size_t, size_t>> spk_counts; // {available, selected}
- for (const auto& coin : available_coins.All()) {
- spk_counts[coin.txout.scriptPubKey].first++;
- }
+ auto count_available_outputs = [&spk_counts](const CoinsResult& coins) {
+ for (const auto& [_, outputs] : coins.coins) {
+ for (const auto& coin : outputs) {
+ spk_counts[coin.txout.scriptPubKey].first++;
+ }
+ }
+ };
+ count_available_outputs(available_coins);
// AvailableCoins skips manually selected coins, so count them here too.
- for (const auto& coin : preset_inputs.All()) {
- spk_counts[coin.txout.scriptPubKey].first++;
- }
+ count_available_outputs(preset_inputs);
for (const auto& coin : result.GetInputSet()) {
spk_counts[coin->txout.scriptPubKey].second++;
}
You can ignore the lamda, it is just a personal preference, it is fine with me to have two loops :)
There was a problem hiding this comment.
Done, thanks. Used two loops.
| for _ in range(2): | ||
| self.nodes[0].sendtoaddress(no_partial.getnewaddress(), 1.0, fee_rate=self.fee_rate) | ||
| self.generate(self.nodes[0], 1) | ||
| with self.nodes[3].assert_debug_log(expected_msgs=[], unexpected_msgs=['Fee non-grouped']): |
There was a problem hiding this comment.
in 8a30292 test: add coverage for partial spend detection
I would probably assert also here the len of vin similar to what you did in line 206
| self.nodes[0].sendtoaddress(no_partial.getnewaddress(), 1.0, fee_rate=self.fee_rate) | ||
| self.generate(self.nodes[0], 1) | ||
| with self.nodes[3].assert_debug_log(expected_msgs=[], unexpected_msgs=['Fee non-grouped']): | ||
| no_partial.sendtoaddress(self.nodes[0].getnewaddress(), 0.5, fee_rate=self.fee_rate) |
There was a problem hiding this comment.
in 8a30292 test: add coverage for partial spend detection
The test has a premise and is that the two UTXOs that the wallet has are owned by different scriptpubkey, maybe asserting it in the code would serve as documentation.
APS runs a second coin selection that spends many (possibly all) or none of the UTXOs sharing a scriptPubKey. It runs even when the first selection left no partial spend for it to avoid. Detect partial spends by comparing selected vs available UTXO counts per scriptPubKey inside CreateTransactionInternal. The result is returned in CreatedTransactionResult as has_partial_spend, and APS now runs only when it is set. Fixes bitcoin#25150
APS now runs only when the first selection leaves a scriptPubKey partially spent. Cover the two cases that gate creates in wallet_groups.py: a wallet whose scriptPubKeys hold one UTXO each never runs the second selection, and a manually preselected input counts towards its scriptPubKey, so the grouped selection still runs.
8a30292 to
559cd15
Compare
|
Took @polespinasa's suggestions: the partial spend check now iterates |

Fixes #25150
APS (Avoid Partial Spends) runs a second coin selection pass that spends many
(possibly all) or none of the UTXOs sharing a scriptPubKey. The second pass runs
even when the first selection left no partial spend for it to avoid, and
aps_create_tx_internal then reports APS as used whenever the grouped result is
taken, overreporting how often a partial spend was avoided.
Detect partial spends by comparing selected vs available UTXO counts per
scriptPubKey, reusing the coins already fetched for selection and counting
manually selected inputs towards their scriptPubKey. Skip APS if none found.
Functional tests cover APS being skipped and a manually selected input making
the spend partial.
Supersedes #34362.