Skip to content

Commit 7cb7759

Browse files
committed
Merge bitcoin#29272: wallet: fix coin selection tracing to return -1 when no change pos
d55fdb1 Move TRACEx parameters to seperate lines (Richard Myers) 2d58629 wallet: fix coin selection tracing to return -1 when no change pos (Richard Myers) Pull request description: This is a bugfix for from when [optional was introduced](bitcoin@758501b) for `change_pos` in the wallet. When optional `change_pos` is unset, we should return -1 and not 0. I added two new checks to the `test/functional/interface_usdt_coinselection.py` which adds coverage for the situations when `normal_create_tx_internal` and `aps_create_tx_internal` events occur with no change. You can reproduce this bug using the coin-selection-simulation scripts as described in [issue #16](achow101/coin-selection-simulation#16). You can also run the `interface_usdt_coinselection.py` test without the changes to `wallet/spend.cpp`. ACKs for top commit: 0xB10C: ACK d55fdb1 achow101: ACK d55fdb1 murchandamus: ACK d55fdb1 Tree-SHA512: 6efac3b756bdf51debbcb759dc3c4b7a4304626bc047b70025cec02f3a04937ace7712e9558ac71e560fd136005a98c518ac5bb4b90c3282d776beccd0de9749
2 parents f1ab078 + d55fdb1 commit 7cb7759

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

src/wallet/spend.cpp

+17-5
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,12 @@ static util::Result<CreatedTransactionResult> CreateTransactionInternal(
11271127
return util::Error{err.empty() ?_("Insufficient funds") : err};
11281128
}
11291129
const SelectionResult& result = *select_coins_res;
1130-
TRACE5(coin_selection, selected_coins, wallet.GetName().c_str(), GetAlgorithmName(result.GetAlgo()).c_str(), result.GetTarget(), result.GetWaste(), result.GetSelectedValue());
1130+
TRACE5(coin_selection, selected_coins,
1131+
wallet.GetName().c_str(),
1132+
GetAlgorithmName(result.GetAlgo()).c_str(),
1133+
result.GetTarget(),
1134+
result.GetWaste(),
1135+
result.GetSelectedValue());
11311136

11321137
const CAmount change_amount = result.GetChange(coin_selection_params.min_viable_change, coin_selection_params.m_change_fee);
11331138
if (change_amount > 0) {
@@ -1336,8 +1341,11 @@ util::Result<CreatedTransactionResult> CreateTransaction(
13361341
LOCK(wallet.cs_wallet);
13371342

13381343
auto res = CreateTransactionInternal(wallet, vecSend, change_pos, coin_control, sign);
1339-
TRACE4(coin_selection, normal_create_tx_internal, wallet.GetName().c_str(), bool(res),
1340-
res ? res->fee : 0, res && res->change_pos.has_value() ? *res->change_pos : 0);
1344+
TRACE4(coin_selection, normal_create_tx_internal,
1345+
wallet.GetName().c_str(),
1346+
bool(res),
1347+
res ? res->fee : 0,
1348+
res && res->change_pos.has_value() ? int32_t(*res->change_pos) : -1);
13411349
if (!res) return res;
13421350
const auto& txr_ungrouped = *res;
13431351
// try with avoidpartialspends unless it's enabled already
@@ -1354,8 +1362,12 @@ util::Result<CreatedTransactionResult> CreateTransaction(
13541362
auto txr_grouped = CreateTransactionInternal(wallet, vecSend, change_pos, tmp_cc, sign);
13551363
// if fee of this alternative one is within the range of the max fee, we use this one
13561364
const bool use_aps{txr_grouped.has_value() ? (txr_grouped->fee <= txr_ungrouped.fee + wallet.m_max_aps_fee) : false};
1357-
TRACE5(coin_selection, aps_create_tx_internal, wallet.GetName().c_str(), use_aps, txr_grouped.has_value(),
1358-
txr_grouped.has_value() ? txr_grouped->fee : 0, txr_grouped.has_value() && txr_grouped->change_pos.has_value() ? *txr_grouped->change_pos : 0);
1365+
TRACE5(coin_selection, aps_create_tx_internal,
1366+
wallet.GetName().c_str(),
1367+
use_aps,
1368+
txr_grouped.has_value(),
1369+
txr_grouped.has_value() ? txr_grouped->fee : 0,
1370+
txr_grouped.has_value() && txr_grouped->change_pos.has_value() ? int32_t(*txr_grouped->change_pos) : -1);
13591371
if (txr_grouped) {
13601372
wallet.WalletLogPrintf("Fee non-grouped = %lld, grouped = %lld, using %s\n",
13611373
txr_ungrouped.fee, txr_grouped->fee, use_aps ? "grouped" : "non-grouped");

test/functional/interface_usdt_coinselection.py

+23
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,29 @@ def run_test(self):
204204
assert_equal(success, True)
205205
assert_equal(use_aps, None)
206206

207+
self.log.info("Change position is -1 if no change is created with APS when APS was initially not used")
208+
# We should have 2 tracepoints in the order:
209+
# 1. selected_coins (type 1)
210+
# 2. normal_create_tx_internal (type 2)
211+
# 3. attempting_aps_create_tx (type 3)
212+
# 4. selected_coins (type 1)
213+
# 5. aps_create_tx_internal (type 4)
214+
wallet.sendtoaddress(address=wallet.getnewaddress(), amount=wallet.getbalance(), subtractfeefromamount=True, avoid_reuse=False)
215+
events = self.get_tracepoints([1, 2, 3, 1, 4])
216+
success, use_aps, algo, waste, change_pos = self.determine_selection_from_usdt(events)
217+
assert_equal(success, True)
218+
assert_equal(change_pos, -1)
219+
220+
self.log.info("Change position is -1 if no change is created normally and APS is not used")
221+
# We should have 2 tracepoints in the order:
222+
# 1. selected_coins (type 1)
223+
# 2. normal_create_tx_internal (type 2)
224+
wallet.sendtoaddress(address=wallet.getnewaddress(), amount=wallet.getbalance(), subtractfeefromamount=True)
225+
events = self.get_tracepoints([1, 2])
226+
success, use_aps, algo, waste, change_pos = self.determine_selection_from_usdt(events)
227+
assert_equal(success, True)
228+
assert_equal(change_pos, -1)
229+
207230
self.bpf.cleanup()
208231

209232

0 commit comments

Comments
 (0)