1
1
mirror of https://github.com/bitcoin/bitcoin.git synced 2024-05-17 23:56:39 +00:00

wallet: add SelectionResult::Merge

This commit is contained in:
S3RK 2022-07-06 09:18:17 +02:00
parent 06f558e4e2
commit f8e796348b
3 changed files with 22 additions and 7 deletions

View File

@ -433,6 +433,16 @@ void SelectionResult::AddInput(const OutputGroup& group)
m_use_effective = !group.m_subtract_fee_outputs; m_use_effective = !group.m_subtract_fee_outputs;
} }
void SelectionResult::Merge(const SelectionResult& other)
{
m_target += other.m_target;
m_use_effective |= other.m_use_effective;
if (m_algo == SelectionAlgorithm::MANUAL) {
m_algo = other.m_algo;
}
util::insert(m_selected_inputs, other.m_selected_inputs);
}
const std::set<COutput>& SelectionResult::GetInputSet() const const std::set<COutput>& SelectionResult::GetInputSet() const
{ {
return m_selected_inputs; return m_selected_inputs;

View File

@ -281,17 +281,16 @@ struct SelectionResult
private: private:
/** Set of inputs selected by the algorithm to use in the transaction */ /** Set of inputs selected by the algorithm to use in the transaction */
std::set<COutput> m_selected_inputs; std::set<COutput> m_selected_inputs;
/** The target the algorithm selected for. Equal to the recipient amount plus non-input fees */
CAmount m_target;
/** The algorithm used to produce this result */
SelectionAlgorithm m_algo;
/** Whether the input values for calculations should be the effective value (true) or normal value (false) */ /** Whether the input values for calculations should be the effective value (true) or normal value (false) */
bool m_use_effective{false}; bool m_use_effective{false};
/** The computed waste */ /** The computed waste */
std::optional<CAmount> m_waste; std::optional<CAmount> m_waste;
public: public:
/** The target the algorithm selected for. Note that this may not be equal to the recipient amount as it can include non-input fees */
const CAmount m_target;
/** The algorithm used to produce this result */
const SelectionAlgorithm m_algo;
explicit SelectionResult(const CAmount target, SelectionAlgorithm algo) explicit SelectionResult(const CAmount target, SelectionAlgorithm algo)
: m_target(target), m_algo(algo) {} : m_target(target), m_algo(algo) {}
@ -308,12 +307,18 @@ public:
void ComputeAndSetWaste(CAmount change_cost); void ComputeAndSetWaste(CAmount change_cost);
[[nodiscard]] CAmount GetWaste() const; [[nodiscard]] CAmount GetWaste() const;
void Merge(const SelectionResult& other);
/** Get m_selected_inputs */ /** Get m_selected_inputs */
const std::set<COutput>& GetInputSet() const; const std::set<COutput>& GetInputSet() const;
/** Get the vector of COutputs that will be used to fill in a CTransaction's vin */ /** Get the vector of COutputs that will be used to fill in a CTransaction's vin */
std::vector<COutput> GetShuffledInputVector() const; std::vector<COutput> GetShuffledInputVector() const;
bool operator<(SelectionResult other) const; bool operator<(SelectionResult other) const;
CAmount GetTarget() const { return m_target; }
SelectionAlgorithm GetAlgo() const { return m_algo; }
}; };
std::optional<SelectionResult> SelectCoinsBnB(std::vector<OutputGroup>& utxo_pool, const CAmount& selection_target, const CAmount& cost_of_change); std::optional<SelectionResult> SelectCoinsBnB(std::vector<OutputGroup>& utxo_pool, const CAmount& selection_target, const CAmount& cost_of_change);

View File

@ -667,7 +667,7 @@ std::optional<SelectionResult> SelectCoins(const CWallet& wallet, CoinsResult& a
// Add preset inputs to result // Add preset inputs to result
res->AddInput(preset_inputs); res->AddInput(preset_inputs);
if (res->m_algo == SelectionAlgorithm::MANUAL) { if (res->GetAlgo() == SelectionAlgorithm::MANUAL) {
res->ComputeAndSetWaste(coin_selection_params.m_cost_of_change); res->ComputeAndSetWaste(coin_selection_params.m_cost_of_change);
} }
@ -890,7 +890,7 @@ static util::Result<CreatedTransactionResult> CreateTransactionInternal(
if (!result) { if (!result) {
return util::Error{_("Insufficient funds")}; return util::Error{_("Insufficient funds")};
} }
TRACE5(coin_selection, selected_coins, wallet.GetName().c_str(), GetAlgorithmName(result->m_algo).c_str(), result->m_target, result->GetWaste(), result->GetSelectedValue()); TRACE5(coin_selection, selected_coins, wallet.GetName().c_str(), GetAlgorithmName(result->GetAlgo()).c_str(), result->GetTarget(), result->GetWaste(), result->GetSelectedValue());
// Always make a change output // Always make a change output
// We will reduce the fee from this change output later, and remove the output if it is too small. // We will reduce the fee from this change output later, and remove the output if it is too small.