Skip to content

Commit 05300c1

Browse files
committed
Use SelectionResult in SelectCoins
Replace setCoinsRet and nValueRet with SelectionResult
1 parent 9d9b101 commit 05300c1

File tree

3 files changed

+37
-38
lines changed

3 files changed

+37
-38
lines changed

src/wallet/spend.cpp

+22-24
Original file line numberDiff line numberDiff line change
@@ -414,27 +414,31 @@ std::optional<SelectionResult> AttemptSelection(const CWallet& wallet, const CAm
414414
return best_result;
415415
}
416416

417-
bool SelectCoins(const CWallet& wallet, const std::vector<COutput>& vAvailableCoins, const CAmount& nTargetValue, std::set<CInputCoin>& setCoinsRet, CAmount& nValueRet, const CCoinControl& coin_control, CoinSelectionParams& coin_selection_params)
417+
std::optional<SelectionResult> SelectCoins(const CWallet& wallet, const std::vector<COutput>& vAvailableCoins, const CAmount& nTargetValue, const CCoinControl& coin_control, const CoinSelectionParams& coin_selection_params)
418418
{
419419
std::vector<COutput> vCoins(vAvailableCoins);
420420
CAmount value_to_select = nTargetValue;
421421

422+
OutputGroup preset_inputs(coin_selection_params);
423+
422424
// coin control -> return all selected outputs (we want all selected to go into the transaction for sure)
423425
if (coin_control.HasSelected() && !coin_control.fAllowOtherInputs)
424426
{
425-
for (const COutput& out : vCoins)
426-
{
427-
if (!out.fSpendable)
428-
continue;
429-
nValueRet += out.tx->tx->vout[out.i].nValue;
430-
setCoinsRet.insert(out.GetInputCoin());
427+
for (const COutput& out : vCoins) {
428+
if (!out.fSpendable) continue;
429+
/* Set depth, from_me, ancestors, and descendants to 0 or false as these don't matter for preset inputs as no actual selection is being done.
430+
* positive_only is set to false because we want to include all preset inputs, even if they are dust.
431+
*/
432+
preset_inputs.Insert(out.GetInputCoin(), 0, false, 0, 0, false);
431433
}
432-
return (nValueRet >= nTargetValue);
434+
SelectionResult result(nTargetValue);
435+
result.AddInput(preset_inputs);
436+
if (result.GetSelectedValue() < nTargetValue) return std::nullopt;
437+
return result;
433438
}
434439

435440
// calculate value from preset inputs and store them
436441
std::set<CInputCoin> setPresetCoins;
437-
OutputGroup preset_inputs(coin_selection_params);
438442

439443
std::vector<COutPoint> vPresetInputs;
440444
coin_control.ListSelected(vPresetInputs);
@@ -446,7 +450,7 @@ bool SelectCoins(const CWallet& wallet, const std::vector<COutput>& vAvailableCo
446450
const CWalletTx& wtx = it->second;
447451
// Clearly invalid input, fail
448452
if (wtx.tx->vout.size() <= outpoint.n) {
449-
return false;
453+
return std::nullopt;
450454
}
451455
input_bytes = GetTxSpendSize(wallet, wtx, outpoint.n, false);
452456
txout = wtx.tx->vout.at(outpoint.n);
@@ -455,14 +459,14 @@ bool SelectCoins(const CWallet& wallet, const std::vector<COutput>& vAvailableCo
455459
// The input is external. We either did not find the tx in mapWallet, or we did but couldn't compute the input size with wallet data
456460
if (!coin_control.GetExternalOutput(outpoint, txout)) {
457461
// Not ours, and we don't have solving data.
458-
return false;
462+
return std::nullopt;
459463
}
460464
input_bytes = CalculateMaximumSignedInputSize(txout, &coin_control.m_external_provider, /* use_max_sig */ true);
461465
}
462466

463467
CInputCoin coin(outpoint, txout, input_bytes);
464468
if (coin.m_input_bytes == -1) {
465-
return false; // Not solvable, can't estimate size for fee
469+
return std::nullopt; // Not solvable, can't estimate size for fee
466470
}
467471
coin.effective_value = coin.txout.nValue - coin_selection_params.m_effective_feerate.GetFee(coin.m_input_bytes);
468472
if (coin_selection_params.m_subtract_fee_outputs) {
@@ -557,15 +561,12 @@ bool SelectCoins(const CWallet& wallet, const std::vector<COutput>& vAvailableCo
557561
return std::optional<SelectionResult>();
558562
}();
559563

560-
if (!res) return false;
564+
if (!res) return std::nullopt;
561565

562566
// Add preset inputs to result
563567
res->AddInput(preset_inputs);
564568

565-
setCoinsRet = res->GetInputSet();
566-
nValueRet = res->GetSelectedValue();
567-
568-
return true;
569+
return res;
569570
}
570571

571572
static bool IsCurrentForAntiFeeSniping(interfaces::Chain& chain, const uint256& block_hash)
@@ -761,17 +762,15 @@ static bool CreateTransactionInternal(
761762
AvailableCoins(wallet, vAvailableCoins, &coin_control, 1, MAX_MONEY, MAX_MONEY, 0);
762763

763764
// Choose coins to use
764-
CAmount inputs_sum = 0;
765-
std::set<CInputCoin> setCoins;
766-
if (!SelectCoins(wallet, vAvailableCoins, /* nTargetValue */ selection_target, setCoins, inputs_sum, coin_control, coin_selection_params))
767-
{
765+
std::optional<SelectionResult> result = SelectCoins(wallet, vAvailableCoins, /* nTargetValue */ selection_target, coin_control, coin_selection_params);
766+
if (!result) {
768767
error = _("Insufficient funds");
769768
return false;
770769
}
771770

772771
// Always make a change output
773772
// We will reduce the fee from this change output later, and remove the output if it is too small.
774-
const CAmount change_and_fee = inputs_sum - recipients_sum;
773+
const CAmount change_and_fee = result->GetSelectedValue() - recipients_sum;
775774
assert(change_and_fee >= 0);
776775
CTxOut newTxOut(change_and_fee, scriptChange);
777776

@@ -790,8 +789,7 @@ static bool CreateTransactionInternal(
790789
auto change_position = txNew.vout.insert(txNew.vout.begin() + nChangePosInOut, newTxOut);
791790

792791
// Shuffle selected coins and fill in final vin
793-
std::vector<CInputCoin> selected_coins(setCoins.begin(), setCoins.end());
794-
Shuffle(selected_coins.begin(), selected_coins.end(), FastRandomContext());
792+
std::vector<CInputCoin> selected_coins = result->GetShuffledInputVector();
795793

796794
// Note how the sequence number is set to non-maxint so that
797795
// the nLockTime set above actually works.

src/wallet/spend.h

+10-7
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,18 @@ std::optional<SelectionResult> AttemptSelection(const CWallet& wallet, const CAm
117117
const CoinSelectionParams& coin_selection_params);
118118

119119
/**
120-
* Select a set of coins such that nValueRet >= nTargetValue and at least
120+
* Select a set of coins such that nTargetValue is met and at least
121121
* all coins from coin_control are selected; never select unconfirmed coins if they are not ours
122-
* param@[out] setCoinsRet Populated with inputs including pre-selected inputs from
123-
* coin_control and Coin Selection if successful.
124-
* param@[out] nValueRet Total value of selected coins including pre-selected ones
125-
* from coin_control and Coin Selection if successful.
122+
* param@[in] wallet The wallet which provides data necessary to spend the selected coins
123+
* param@[in] vAvailableCoins The vector of coins available to be spent
124+
* param@[in] nTargetValue The target value
125+
* param@[in] coin_selection_params Parameters for this coin selection such as feerates, whether to avoid partial spends,
126+
* and whether to subtract the fee from the outputs.
127+
* returns If successful, a SelectionResult containing the selected coins
128+
* If failed, a nullopt.
126129
*/
127-
bool SelectCoins(const CWallet& wallet, const std::vector<COutput>& vAvailableCoins, const CAmount& nTargetValue, std::set<CInputCoin>& setCoinsRet, CAmount& nValueRet,
128-
const CCoinControl& coin_control, CoinSelectionParams& coin_selection_params) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
130+
std::optional<SelectionResult> SelectCoins(const CWallet& wallet, const std::vector<COutput>& vAvailableCoins, const CAmount& nTargetValue, const CCoinControl& coin_control,
131+
const CoinSelectionParams& coin_selection_params) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
129132

130133
/**
131134
* Create a new transaction paying the recipients with a set of coins

src/wallet/test/coinselector_tests.cpp

+5-7
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,6 @@ BOOST_AUTO_TEST_CASE(bnb_search_test)
330330
wallet->SetupDescriptorScriptPubKeyMans();
331331

332332
std::vector<COutput> coins;
333-
CoinSet setCoinsRet;
334-
CAmount nValueRet;
335333

336334
add_coin(coins, *wallet, 5 * CENT, 6 * 24, false, 0, true);
337335
add_coin(coins, *wallet, 3 * CENT, 6 * 24, false, 0, true);
@@ -340,7 +338,8 @@ BOOST_AUTO_TEST_CASE(bnb_search_test)
340338
coin_control.fAllowOtherInputs = true;
341339
coin_control.Select(COutPoint(coins.at(0).tx->GetHash(), coins.at(0).i));
342340
coin_selection_params_bnb.m_effective_feerate = CFeeRate(0);
343-
BOOST_CHECK(SelectCoins(*wallet, coins, 10 * CENT, setCoinsRet, nValueRet, coin_control, coin_selection_params_bnb));
341+
const auto result10 = SelectCoins(*wallet, coins, 10 * CENT, coin_control, coin_selection_params_bnb);
342+
BOOST_CHECK(result10);
344343
}
345344
}
346345

@@ -713,11 +712,10 @@ BOOST_AUTO_TEST_CASE(SelectCoins_test)
713712
/* change_spend_size= */ 148, /* effective_feerate= */ CFeeRate(0),
714713
/* long_term_feerate= */ CFeeRate(0), /* discard_feerate= */ CFeeRate(0),
715714
/* tx_noinputs_size= */ 0, /* avoid_partial= */ false);
716-
CoinSet out_set;
717-
CAmount out_value = 0;
718715
CCoinControl cc;
719-
BOOST_CHECK(SelectCoins(*wallet, coins, target, out_set, out_value, cc, cs_params));
720-
BOOST_CHECK_GE(out_value, target);
716+
const auto result = SelectCoins(*wallet, coins, target, cc, cs_params);
717+
BOOST_CHECK(result);
718+
BOOST_CHECK_GE(result->GetSelectedValue(), target);
721719
}
722720
}
723721

0 commit comments

Comments
 (0)