Skip to content

Commit 461f082

Browse files
committed
refactor: make OutputGroup::m_outputs field a vector of shared_ptr
Initial steps towards sharing COutput instances across all possible OutputGroups (instead of copying them time after time).
1 parent d8e749b commit 461f082

File tree

7 files changed

+38
-37
lines changed

7 files changed

+38
-37
lines changed

src/bench/coin_selection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ static void add_coin(const CAmount& nValue, int nInput, std::vector<OutputGroup>
9191
tx.vout[nInput].nValue = nValue;
9292
COutput output(COutPoint(tx.GetHash(), nInput), tx.vout.at(nInput), /*depth=*/ 0, /*input_bytes=*/ -1, /*spendable=*/ true, /*solvable=*/ true, /*safe=*/ true, /*time=*/ 0, /*from_me=*/ true, /*fees=*/ 0);
9393
set.emplace_back();
94-
set.back().Insert(output, /*ancestors=*/ 0, /*descendants=*/ 0);
94+
set.back().Insert(std::make_shared<COutput>(output), /*ancestors=*/ 0, /*descendants=*/ 0);
9595
}
9696
// Copied from src/wallet/test/coinselector_tests.cpp
9797
static CAmount make_hard_case(int utxos, std::vector<OutputGroup>& utxo_pool)

src/wallet/coinselection.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,9 @@ std::optional<SelectionResult> KnapsackSolver(std::vector<OutputGroup>& groups,
333333
334334
******************************************************************************/
335335

336-
void OutputGroup::Insert(const COutput& output, size_t ancestors, size_t descendants) {
336+
void OutputGroup::Insert(const std::shared_ptr<COutput>& output, size_t ancestors, size_t descendants) {
337337
m_outputs.push_back(output);
338-
COutput& coin = m_outputs.back();
338+
auto& coin = *m_outputs.back();
339339

340340
fee += coin.GetFee();
341341

@@ -355,8 +355,8 @@ void OutputGroup::Insert(const COutput& output, size_t ancestors, size_t descend
355355
// coin itself; thus, this value is counted as the max, not the sum
356356
m_descendants = std::max(m_descendants, descendants);
357357

358-
if (output.input_bytes > 0) {
359-
m_weight += output.input_bytes * WITNESS_SCALE_FACTOR;
358+
if (output->input_bytes > 0) {
359+
m_weight += output->input_bytes * WITNESS_SCALE_FACTOR;
360360
}
361361
}
362362

@@ -372,15 +372,16 @@ CAmount OutputGroup::GetSelectionAmount() const
372372
return m_subtract_fee_outputs ? m_value : effective_value;
373373
}
374374

375-
CAmount GetSelectionWaste(const std::set<COutput>& inputs, CAmount change_cost, CAmount target, bool use_effective_value)
375+
CAmount GetSelectionWaste(const std::set<std::shared_ptr<COutput>>& inputs, CAmount change_cost, CAmount target, bool use_effective_value)
376376
{
377377
// This function should not be called with empty inputs as that would mean the selection failed
378378
assert(!inputs.empty());
379379

380380
// Always consider the cost of spending an input now vs in the future.
381381
CAmount waste = 0;
382382
CAmount selected_effective_value = 0;
383-
for (const COutput& coin : inputs) {
383+
for (const auto& coin_ptr : inputs) {
384+
const COutput& coin = *coin_ptr;
384385
waste += coin.GetFee() - coin.long_term_fee;
385386
selected_effective_value += use_effective_value ? coin.GetEffectiveValue() : coin.txout.nValue;
386387
}
@@ -428,12 +429,12 @@ CAmount SelectionResult::GetWaste() const
428429

429430
CAmount SelectionResult::GetSelectedValue() const
430431
{
431-
return std::accumulate(m_selected_inputs.cbegin(), m_selected_inputs.cend(), CAmount{0}, [](CAmount sum, const auto& coin) { return sum + coin.txout.nValue; });
432+
return std::accumulate(m_selected_inputs.cbegin(), m_selected_inputs.cend(), CAmount{0}, [](CAmount sum, const auto& coin) { return sum + coin->txout.nValue; });
432433
}
433434

434435
CAmount SelectionResult::GetSelectedEffectiveValue() const
435436
{
436-
return std::accumulate(m_selected_inputs.cbegin(), m_selected_inputs.cend(), CAmount{0}, [](CAmount sum, const auto& coin) { return sum + coin.GetEffectiveValue(); });
437+
return std::accumulate(m_selected_inputs.cbegin(), m_selected_inputs.cend(), CAmount{0}, [](CAmount sum, const auto& coin) { return sum + coin->GetEffectiveValue(); });
437438
}
438439

439440
void SelectionResult::Clear()
@@ -452,14 +453,14 @@ void SelectionResult::AddInput(const OutputGroup& group)
452453
m_weight += group.m_weight;
453454
}
454455

455-
void SelectionResult::AddInputs(const std::set<COutput>& inputs, bool subtract_fee_outputs)
456+
void SelectionResult::AddInputs(const std::set<std::shared_ptr<COutput>>& inputs, bool subtract_fee_outputs)
456457
{
457458
// As it can fail, combine inputs first
458459
InsertInputs(inputs);
459460
m_use_effective = !subtract_fee_outputs;
460461

461462
m_weight += std::accumulate(inputs.cbegin(), inputs.cend(), 0, [](int sum, const auto& coin) {
462-
return sum + std::max(coin.input_bytes, 0) * WITNESS_SCALE_FACTOR;
463+
return sum + std::max(coin->input_bytes, 0) * WITNESS_SCALE_FACTOR;
463464
});
464465
}
465466

@@ -477,14 +478,14 @@ void SelectionResult::Merge(const SelectionResult& other)
477478
m_weight += other.m_weight;
478479
}
479480

480-
const std::set<COutput>& SelectionResult::GetInputSet() const
481+
const std::set<std::shared_ptr<COutput>>& SelectionResult::GetInputSet() const
481482
{
482483
return m_selected_inputs;
483484
}
484485

485-
std::vector<COutput> SelectionResult::GetShuffledInputVector() const
486+
std::vector<std::shared_ptr<COutput>> SelectionResult::GetShuffledInputVector() const
486487
{
487-
std::vector<COutput> coins(m_selected_inputs.begin(), m_selected_inputs.end());
488+
std::vector<std::shared_ptr<COutput>> coins(m_selected_inputs.begin(), m_selected_inputs.end());
488489
Shuffle(coins.begin(), coins.end(), FastRandomContext());
489490
return coins;
490491
}

src/wallet/coinselection.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ struct CoinEligibilityFilter
198198
struct OutputGroup
199199
{
200200
/** The list of UTXOs contained in this output group. */
201-
std::vector<COutput> m_outputs;
201+
std::vector<std::shared_ptr<COutput>> m_outputs;
202202
/** Whether the UTXOs were sent by the wallet to itself. This is relevant because we may want at
203203
* least a certain number of confirmations on UTXOs received from outside wallets while trusting
204204
* our own UTXOs more. */
@@ -237,7 +237,7 @@ struct OutputGroup
237237
m_subtract_fee_outputs(params.m_subtract_fee_outputs)
238238
{}
239239

240-
void Insert(const COutput& output, size_t ancestors, size_t descendants);
240+
void Insert(const std::shared_ptr<COutput>& output, size_t ancestors, size_t descendants);
241241
bool EligibleForSpending(const CoinEligibilityFilter& eligibility_filter) const;
242242
CAmount GetSelectionAmount() const;
243243
};
@@ -259,7 +259,7 @@ struct OutputGroup
259259
* @param[in] use_effective_value Whether to use the input's effective value (when true) or the real value (when false).
260260
* @return The waste
261261
*/
262-
[[nodiscard]] CAmount GetSelectionWaste(const std::set<COutput>& inputs, CAmount change_cost, CAmount target, bool use_effective_value = true);
262+
[[nodiscard]] CAmount GetSelectionWaste(const std::set<std::shared_ptr<COutput>>& inputs, CAmount change_cost, CAmount target, bool use_effective_value = true);
263263

264264

265265
/** Choose a random change target for each transaction to make it harder to fingerprint the Core
@@ -292,7 +292,7 @@ struct SelectionResult
292292
{
293293
private:
294294
/** Set of inputs selected by the algorithm to use in the transaction */
295-
std::set<COutput> m_selected_inputs;
295+
std::set<std::shared_ptr<COutput>> m_selected_inputs;
296296
/** The target the algorithm selected for. Equal to the recipient amount plus non-input fees */
297297
CAmount m_target;
298298
/** The algorithm used to produce this result */
@@ -329,7 +329,7 @@ struct SelectionResult
329329
void Clear();
330330

331331
void AddInput(const OutputGroup& group);
332-
void AddInputs(const std::set<COutput>& inputs, bool subtract_fee_outputs);
332+
void AddInputs(const std::set<std::shared_ptr<COutput>>& inputs, bool subtract_fee_outputs);
333333

334334
/** Calculates and stores the waste for this selection via GetSelectionWaste */
335335
void ComputeAndSetWaste(const CAmount min_viable_change, const CAmount change_cost, const CAmount change_fee);
@@ -344,9 +344,9 @@ struct SelectionResult
344344
void Merge(const SelectionResult& other);
345345

346346
/** Get m_selected_inputs */
347-
const std::set<COutput>& GetInputSet() const;
347+
const std::set<std::shared_ptr<COutput>>& GetInputSet() const;
348348
/** Get the vector of COutputs that will be used to fill in a CTransaction's vin */
349-
std::vector<COutput> GetShuffledInputVector() const;
349+
std::vector<std::shared_ptr<COutput>> GetShuffledInputVector() const;
350350

351351
bool operator<(SelectionResult other) const;
352352

src/wallet/spend.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ std::vector<OutputGroup> GroupOutputs(const CWallet& wallet, const std::vector<C
421421
if (!positive_only || output.GetEffectiveValue() > 0) {
422422
// Make an OutputGroup containing just this output
423423
OutputGroup group{coin_sel_params};
424-
group.Insert(output, ancestors, descendants);
424+
group.Insert(std::make_shared<COutput>(output), ancestors, descendants);
425425

426426
if (group.EligibleForSpending(filter)) groups_out.push_back(group);
427427
}
@@ -465,7 +465,7 @@ std::vector<OutputGroup> GroupOutputs(const CWallet& wallet, const std::vector<C
465465

466466
// Filter for positive only before adding the output to group
467467
if (!positive_only || output.GetEffectiveValue() > 0) {
468-
group->Insert(output, ancestors, descendants);
468+
group->Insert(std::make_shared<COutput>(output), ancestors, descendants);
469469
}
470470
}
471471

@@ -936,7 +936,7 @@ static util::Result<CreatedTransactionResult> CreateTransactionInternal(
936936
}
937937

938938
// Shuffle selected coins and fill in final vin
939-
std::vector<COutput> selected_coins = result.GetShuffledInputVector();
939+
std::vector<std::shared_ptr<COutput>> selected_coins = result.GetShuffledInputVector();
940940

941941
// The sequence number is set to non-maxint so that DiscourageFeeSniping
942942
// works.
@@ -948,7 +948,7 @@ static util::Result<CreatedTransactionResult> CreateTransactionInternal(
948948
// behavior."
949949
const uint32_t nSequence{coin_control.m_signal_bip125_rbf.value_or(wallet.m_signal_rbf) ? MAX_BIP125_RBF_SEQUENCE : CTxIn::MAX_SEQUENCE_NONFINAL};
950950
for (const auto& coin : selected_coins) {
951-
txNew.vin.push_back(CTxIn(coin.outpoint, CScript(), nSequence));
951+
txNew.vin.push_back(CTxIn(coin->outpoint, CScript(), nSequence));
952952
}
953953
DiscourageFeeSniping(txNew, rng_fast, wallet.chain(), wallet.GetLastBlockHash(), wallet.GetLastBlockHeight());
954954

src/wallet/spend.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ util::Result<SelectionResult> ChooseSelectionResult(const CWallet& wallet, const
148148
// User manually selected inputs that must be part of the transaction
149149
struct PreSelectedInputs
150150
{
151-
std::set<COutput> coins;
151+
std::set<std::shared_ptr<COutput>> coins;
152152
// If subtract fee from outputs is disabled, the 'total_amount'
153153
// will be the sum of each output effective value
154154
// instead of the sum of the outputs amount
@@ -161,7 +161,7 @@ struct PreSelectedInputs
161161
} else {
162162
total_amount += output.GetEffectiveValue();
163163
}
164-
coins.insert(output);
164+
coins.insert(std::make_shared<COutput>(output));
165165
}
166166
};
167167

src/wallet/test/coinselector_tests.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ BOOST_FIXTURE_TEST_SUITE(coinselector_tests, WalletTestingSetup)
2929
// we repeat those tests this many times and only complain if all iterations of the test fail
3030
#define RANDOM_REPEATS 5
3131

32-
typedef std::set<COutput> CoinSet;
32+
typedef std::set<std::shared_ptr<COutput>> CoinSet;
3333

3434
static const CoinEligibilityFilter filter_standard(1, 6, 0);
3535
static const CoinEligibilityFilter filter_confirmed(1, 1, 0);
@@ -53,7 +53,7 @@ static void add_coin(const CAmount& nValue, int nInput, SelectionResult& result)
5353
tx.nLockTime = nextLockTime++; // so all transactions get different hashes
5454
COutput output(COutPoint(tx.GetHash(), nInput), tx.vout.at(nInput), /*depth=*/ 1, /*input_bytes=*/ -1, /*spendable=*/ true, /*solvable=*/ true, /*safe=*/ true, /*time=*/ 0, /*from_me=*/ false, /*fees=*/ 0);
5555
OutputGroup group;
56-
group.Insert(output, /*ancestors=*/ 0, /*descendants=*/ 0);
56+
group.Insert(std::make_shared<COutput>(output), /*ancestors=*/ 0, /*descendants=*/ 0);
5757
result.AddInput(group);
5858
}
5959

@@ -65,7 +65,7 @@ static void add_coin(const CAmount& nValue, int nInput, CoinSet& set, CAmount fe
6565
tx.nLockTime = nextLockTime++; // so all transactions get different hashes
6666
COutput coin(COutPoint(tx.GetHash(), nInput), tx.vout.at(nInput), /*depth=*/ 1, /*input_bytes=*/ 148, /*spendable=*/ true, /*solvable=*/ true, /*safe=*/ true, /*time=*/ 0, /*from_me=*/ false, fee);
6767
coin.long_term_fee = long_term_fee;
68-
set.insert(coin);
68+
set.insert(std::make_shared<COutput>(coin));
6969
}
7070

7171
static void add_coin(CoinsResult& available_coins, CWallet& wallet, const CAmount& nValue, CFeeRate feerate = CFeeRate(0), int nAge = 6*24, bool fIsFromMe = false, int nInput =0, bool spendable = false)
@@ -94,10 +94,10 @@ static bool EquivalentResult(const SelectionResult& a, const SelectionResult& b)
9494
std::vector<CAmount> a_amts;
9595
std::vector<CAmount> b_amts;
9696
for (const auto& coin : a.GetInputSet()) {
97-
a_amts.push_back(coin.txout.nValue);
97+
a_amts.push_back(coin->txout.nValue);
9898
}
9999
for (const auto& coin : b.GetInputSet()) {
100-
b_amts.push_back(coin.txout.nValue);
100+
b_amts.push_back(coin->txout.nValue);
101101
}
102102
std::sort(a_amts.begin(), a_amts.end());
103103
std::sort(b_amts.begin(), b_amts.end());
@@ -110,8 +110,8 @@ static bool EquivalentResult(const SelectionResult& a, const SelectionResult& b)
110110
static bool EqualResult(const SelectionResult& a, const SelectionResult& b)
111111
{
112112
std::pair<CoinSet::iterator, CoinSet::iterator> ret = std::mismatch(a.GetInputSet().begin(), a.GetInputSet().end(), b.GetInputSet().begin(),
113-
[](const COutput& a, const COutput& b) {
114-
return a.outpoint == b.outpoint;
113+
[](const std::shared_ptr<COutput>& a, const std::shared_ptr<COutput>& b) {
114+
return a->outpoint == b->outpoint;
115115
});
116116
return ret.first == a.GetInputSet().end() && ret.second == b.GetInputSet().end();
117117
}
@@ -134,7 +134,7 @@ inline std::vector<OutputGroup>& GroupCoins(const std::vector<COutput>& availabl
134134
static_groups.clear();
135135
for (auto& coin : available_coins) {
136136
static_groups.emplace_back();
137-
static_groups.back().Insert(coin, /*ancestors=*/ 0, /*descendants=*/ 0);
137+
static_groups.back().Insert(std::make_shared<COutput>(coin), /*ancestors=*/ 0, /*descendants=*/ 0);
138138
}
139139
return static_groups;
140140
}
@@ -943,7 +943,7 @@ static util::Result<SelectionResult> select_coins(const CAmount& target, const C
943943

944944
static bool has_coin(const CoinSet& set, CAmount amount)
945945
{
946-
return std::any_of(set.begin(), set.end(), [&](const auto& coin) { return coin.GetEffectiveValue() == amount; });
946+
return std::any_of(set.begin(), set.end(), [&](const auto& coin) { return coin->GetEffectiveValue() == amount; });
947947
}
948948

949949
BOOST_AUTO_TEST_CASE(check_max_weight)

src/wallet/test/fuzz/coinselection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static void GroupCoins(FuzzedDataProvider& fuzzed_data_provider, const std::vect
3030
bool valid_outputgroup{false};
3131
for (auto& coin : coins) {
3232
if (!positive_only || (positive_only && coin.GetEffectiveValue() > 0)) {
33-
output_group.Insert(coin, /*ancestors=*/0, /*descendants=*/0);
33+
output_group.Insert(std::make_shared<COutput>(coin), /*ancestors=*/0, /*descendants=*/0);
3434
}
3535
// If positive_only was specified, nothing was inserted, leading to an empty output group
3636
// that would be invalid for the BnB algorithm

0 commit comments

Comments
 (0)