Skip to content

Commit 2d58629

Browse files
committed
wallet: fix coin selection tracing to return -1 when no change pos
1 parent 8106b26 commit 2d58629

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/wallet/spend.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1337,7 +1337,7 @@ util::Result<CreatedTransactionResult> CreateTransaction(
13371337

13381338
auto res = CreateTransactionInternal(wallet, vecSend, change_pos, coin_control, sign);
13391339
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);
1340+
res ? res->fee : 0, res && res->change_pos.has_value() ? int32_t(*res->change_pos) : -1);
13411341
if (!res) return res;
13421342
const auto& txr_ungrouped = *res;
13431343
// try with avoidpartialspends unless it's enabled already
@@ -1355,7 +1355,7 @@ util::Result<CreatedTransactionResult> CreateTransaction(
13551355
// if fee of this alternative one is within the range of the max fee, we use this one
13561356
const bool use_aps{txr_grouped.has_value() ? (txr_grouped->fee <= txr_ungrouped.fee + wallet.m_max_aps_fee) : false};
13571357
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);
1358+
txr_grouped.has_value() ? txr_grouped->fee : 0, txr_grouped.has_value() && txr_grouped->change_pos.has_value() ? int32_t(*txr_grouped->change_pos) : -1);
13591359
if (txr_grouped) {
13601360
wallet.WalletLogPrintf("Fee non-grouped = %lld, grouped = %lld, using %s\n",
13611361
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)