aboutsummaryrefslogtreecommitdiff
path: root/src/wallet/coinselection.cpp
diff options
context:
space:
mode:
authorfurszy <matiasfurszyfer@protonmail.com>2022-08-01 16:15:36 -0300
committerfurszy <matiasfurszyfer@protonmail.com>2023-03-06 09:45:40 -0300
commit461f0821a2dff0a27f755464b0eb92314fba1acd (patch)
treed04797fd9b8cd29d0a0690af982d7f29547e0326 /src/wallet/coinselection.cpp
parentd8e749bb840cf65065ed00561998255156126278 (diff)
downloadbitcoin-461f0821a2dff0a27f755464b0eb92314fba1acd.tar.xz
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).
Diffstat (limited to 'src/wallet/coinselection.cpp')
-rw-r--r--src/wallet/coinselection.cpp27
1 files changed, 14 insertions, 13 deletions
diff --git a/src/wallet/coinselection.cpp b/src/wallet/coinselection.cpp
index cebe2cd7ed..f99beabc70 100644
--- a/src/wallet/coinselection.cpp
+++ b/src/wallet/coinselection.cpp
@@ -333,9 +333,9 @@ std::optional<SelectionResult> KnapsackSolver(std::vector<OutputGroup>& groups,
******************************************************************************/
-void OutputGroup::Insert(const COutput& output, size_t ancestors, size_t descendants) {
+void OutputGroup::Insert(const std::shared_ptr<COutput>& output, size_t ancestors, size_t descendants) {
m_outputs.push_back(output);
- COutput& coin = m_outputs.back();
+ auto& coin = *m_outputs.back();
fee += coin.GetFee();
@@ -355,8 +355,8 @@ void OutputGroup::Insert(const COutput& output, size_t ancestors, size_t descend
// coin itself; thus, this value is counted as the max, not the sum
m_descendants = std::max(m_descendants, descendants);
- if (output.input_bytes > 0) {
- m_weight += output.input_bytes * WITNESS_SCALE_FACTOR;
+ if (output->input_bytes > 0) {
+ m_weight += output->input_bytes * WITNESS_SCALE_FACTOR;
}
}
@@ -372,7 +372,7 @@ CAmount OutputGroup::GetSelectionAmount() const
return m_subtract_fee_outputs ? m_value : effective_value;
}
-CAmount GetSelectionWaste(const std::set<COutput>& inputs, CAmount change_cost, CAmount target, bool use_effective_value)
+CAmount GetSelectionWaste(const std::set<std::shared_ptr<COutput>>& inputs, CAmount change_cost, CAmount target, bool use_effective_value)
{
// This function should not be called with empty inputs as that would mean the selection failed
assert(!inputs.empty());
@@ -380,7 +380,8 @@ CAmount GetSelectionWaste(const std::set<COutput>& inputs, CAmount change_cost,
// Always consider the cost of spending an input now vs in the future.
CAmount waste = 0;
CAmount selected_effective_value = 0;
- for (const COutput& coin : inputs) {
+ for (const auto& coin_ptr : inputs) {
+ const COutput& coin = *coin_ptr;
waste += coin.GetFee() - coin.long_term_fee;
selected_effective_value += use_effective_value ? coin.GetEffectiveValue() : coin.txout.nValue;
}
@@ -428,12 +429,12 @@ CAmount SelectionResult::GetWaste() const
CAmount SelectionResult::GetSelectedValue() const
{
- return std::accumulate(m_selected_inputs.cbegin(), m_selected_inputs.cend(), CAmount{0}, [](CAmount sum, const auto& coin) { return sum + coin.txout.nValue; });
+ return std::accumulate(m_selected_inputs.cbegin(), m_selected_inputs.cend(), CAmount{0}, [](CAmount sum, const auto& coin) { return sum + coin->txout.nValue; });
}
CAmount SelectionResult::GetSelectedEffectiveValue() const
{
- return std::accumulate(m_selected_inputs.cbegin(), m_selected_inputs.cend(), CAmount{0}, [](CAmount sum, const auto& coin) { return sum + coin.GetEffectiveValue(); });
+ return std::accumulate(m_selected_inputs.cbegin(), m_selected_inputs.cend(), CAmount{0}, [](CAmount sum, const auto& coin) { return sum + coin->GetEffectiveValue(); });
}
void SelectionResult::Clear()
@@ -452,14 +453,14 @@ void SelectionResult::AddInput(const OutputGroup& group)
m_weight += group.m_weight;
}
-void SelectionResult::AddInputs(const std::set<COutput>& inputs, bool subtract_fee_outputs)
+void SelectionResult::AddInputs(const std::set<std::shared_ptr<COutput>>& inputs, bool subtract_fee_outputs)
{
// As it can fail, combine inputs first
InsertInputs(inputs);
m_use_effective = !subtract_fee_outputs;
m_weight += std::accumulate(inputs.cbegin(), inputs.cend(), 0, [](int sum, const auto& coin) {
- return sum + std::max(coin.input_bytes, 0) * WITNESS_SCALE_FACTOR;
+ return sum + std::max(coin->input_bytes, 0) * WITNESS_SCALE_FACTOR;
});
}
@@ -477,14 +478,14 @@ void SelectionResult::Merge(const SelectionResult& other)
m_weight += other.m_weight;
}
-const std::set<COutput>& SelectionResult::GetInputSet() const
+const std::set<std::shared_ptr<COutput>>& SelectionResult::GetInputSet() const
{
return m_selected_inputs;
}
-std::vector<COutput> SelectionResult::GetShuffledInputVector() const
+std::vector<std::shared_ptr<COutput>> SelectionResult::GetShuffledInputVector() const
{
- std::vector<COutput> coins(m_selected_inputs.begin(), m_selected_inputs.end());
+ std::vector<std::shared_ptr<COutput>> coins(m_selected_inputs.begin(), m_selected_inputs.end());
Shuffle(coins.begin(), coins.end(), FastRandomContext());
return coins;
}