5 #ifndef BITCOIN_WALLET_COINSELECTION_H
6 #define BITCOIN_WALLET_COINSELECTION_H
31 std::optional<CAmount>
fee;
240 void Insert(
const COutput& output,
size_t ancestors,
size_t descendants,
bool positive_only);
314 throw std::runtime_error(
STR_INTERNAL_BUG(
"Shared UTXOs among selection results"));
332 void AddInputs(
const std::set<COutput>& inputs,
bool subtract_fee_outputs);
379 std::optional<SelectionResult>
SelectCoinsBnB(std::vector<OutputGroup>& utxo_pool,
const CAmount& selection_target,
const CAmount& cost_of_change);
391 std::optional<SelectionResult>
KnapsackSolver(std::vector<OutputGroup>& groups,
const CAmount& nTargetValue,
int64_t CAmount
Amount in satoshis (Can be negative)
#define STR_INTERNAL_BUG(msg)
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
An outpoint - a combination of a transaction hash and an index n into its vout.
An output of a transaction.
void insert(Tdst &dst, const Tsrc &src)
Simplification of std insertion.
static constexpr CAmount CHANGE_UPPER
upper bound for randomly-chosen target change amount
CAmount GetSelectionWaste(const std::set< COutput > &inputs, CAmount change_cost, CAmount target, bool use_effective_value)
Compute the waste for this result given the cost of change and the opportunity cost of spending these...
static constexpr CAmount CHANGE_LOWER
lower bound for randomly-chosen target change amount
CAmount GenerateChangeTarget(const CAmount payment_value, const CAmount change_fee, FastRandomContext &rng)
Choose a random change target for each transaction to make it harder to fingerprint the Core wallet b...
std::optional< SelectionResult > SelectCoinsSRD(const std::vector< OutputGroup > &utxo_pool, CAmount target_value, FastRandomContext &rng)
Select coins by Single Random Draw.
std::optional< SelectionResult > KnapsackSolver(std::vector< OutputGroup > &groups, const CAmount &nTargetValue, CAmount change_target, FastRandomContext &rng)
std::string GetAlgorithmName(const SelectionAlgorithm algo)
std::optional< SelectionResult > SelectCoinsBnB(std::vector< OutputGroup > &utxo_pool, const CAmount &selection_target, const CAmount &cost_of_change)
A UTXO under consideration for use in funding a new transaction.
CAmount long_term_fee
The fee required to spend this output at the consolidation feerate.
bool from_me
Whether the transaction containing this output is sent from the owning wallet.
COutPoint outpoint
The outpoint identifying this UTXO.
std::optional< CAmount > effective_value
The output's value minus fees required to spend it.
bool solvable
Whether we know how to spend this output, ignoring the lack of keys.
int64_t time
The time of the transaction containing this output as determined by CWalletTx::nTimeSmart.
int depth
Depth in block chain.
std::string ToString() const
bool safe
Whether this output is considered safe to spend.
bool spendable
Whether we have the private keys to spend this output.
CTxOut txout
The output itself.
COutput(const COutPoint &outpoint, const CTxOut &txout, int depth, int input_bytes, bool spendable, bool solvable, bool safe, int64_t time, bool from_me, const CAmount fees)
int input_bytes
Pre-computed estimated size of this output as a fully-signed input in a transaction.
CAmount GetEffectiveValue() const
bool operator<(const COutput &rhs) const
bool HasEffectiveValue() const
std::optional< CAmount > fee
The fee required to spend this output at the transaction's target feerate.
COutput(const COutPoint &outpoint, const CTxOut &txout, int depth, int input_bytes, bool spendable, bool solvable, bool safe, int64_t time, bool from_me, const std::optional< CFeeRate > feerate=std::nullopt)
Parameters for filtering which OutputGroups we may use in coin selection.
const uint64_t max_ancestors
Maximum number of unconfirmed ancestors aggregated across all UTXOs in an OutputGroup.
CoinEligibilityFilter(int conf_mine, int conf_theirs, uint64_t max_ancestors, uint64_t max_descendants)
const bool m_include_partial_groups
When avoid_reuse=true and there are full groups (OUTPUT_GROUP_MAX_ENTRIES), whether or not to use any...
const uint64_t max_descendants
Maximum number of descendants that a single UTXO in the OutputGroup may have.
CoinEligibilityFilter()=delete
CoinEligibilityFilter(int conf_mine, int conf_theirs, uint64_t max_ancestors)
const int conf_theirs
Minimum number of confirmations for outputs received from a different wallet.
CoinEligibilityFilter(int conf_mine, int conf_theirs, uint64_t max_ancestors, uint64_t max_descendants, bool include_partial)
const int conf_mine
Minimum number of confirmations for outputs that we sent to ourselves.
Parameters for one iteration of Coin Selection.
CoinSelectionParams(FastRandomContext &rng_fast, size_t change_output_size, size_t change_spend_size, CAmount min_change_target, CFeeRate effective_feerate, CFeeRate long_term_feerate, CFeeRate discard_feerate, size_t tx_noinputs_size, bool avoid_partial)
FastRandomContext & rng_fast
Randomness to use in the context of coin selection.
CAmount m_min_change_target
Mininmum change to target in Knapsack solver: select coins to cover the payment and at least this val...
bool m_subtract_fee_outputs
Indicate that we are subtracting the fee from outputs.
CoinSelectionParams(FastRandomContext &rng_fast)
size_t change_output_size
Size of a change output in bytes, determined by the output type.
CFeeRate m_effective_feerate
The targeted feerate of the transaction being built.
CAmount min_viable_change
Minimum amount for creating a change output.
CAmount m_cost_of_change
Cost of creating the change output + cost of spending the change output in the future.
CAmount m_change_fee
Cost of creating the change output.
CFeeRate m_long_term_feerate
The feerate estimate used to estimate an upper bound on what should be sufficient to spend the change...
CFeeRate m_discard_feerate
If the cost to spend a change output at the discard feerate exceeds its value, drop it to fees.
size_t change_spend_size
Size of the input to spend a change output in virtual bytes.
size_t tx_noinputs_size
Size of the transaction before coin selection, consisting of the header and recipient output(s),...
bool m_avoid_partial_spends
When true, always spend all (up to OUTPUT_GROUP_MAX_ENTRIES) or none of the outputs associated with t...
A group of UTXOs paid to the same output script.
CFeeRate m_long_term_feerate
The feerate for spending a created change output eventually (i.e.
bool m_from_me
Whether the UTXOs were sent by the wallet to itself.
OutputGroup(const CoinSelectionParams ¶ms)
CAmount m_value
The total value of the UTXOs in sum.
bool m_subtract_fee_outputs
Indicate that we are subtracting the fee from outputs.
void Insert(const COutput &output, size_t ancestors, size_t descendants, bool positive_only)
CAmount GetSelectionAmount() const
std::vector< COutput > m_outputs
The list of UTXOs contained in this output group.
int m_depth
The minimum number of confirmations the UTXOs in the group have.
int m_weight
Total weight of the UTXOs in this group.
bool EligibleForSpending(const CoinEligibilityFilter &eligibility_filter) const
CAmount effective_value
The value of the UTXOs after deducting the cost of spending them at the effective feerate.
size_t m_ancestors
The aggregated count of unconfirmed ancestors of all UTXOs in this group.
CAmount fee
The fee to spend these UTXOs at the effective feerate.
CAmount long_term_fee
The fee to spend these UTXOs at the long term feerate.
CFeeRate m_effective_feerate
The target feerate of the transaction we're trying to build.
size_t m_descendants
The maximum count of descendants of a single UTXO in this output group.
int m_weight
Total weight of the selected inputs.
bool operator<(SelectionResult other) const
void ComputeAndSetWaste(const CAmount min_viable_change, const CAmount change_cost, const CAmount change_fee)
Calculates and stores the waste for this selection via GetSelectionWaste.
void AddInputs(const std::set< COutput > &inputs, bool subtract_fee_outputs)
std::vector< COutput > GetShuffledInputVector() const
Get the vector of COutputs that will be used to fill in a CTransaction's vin.
const std::set< COutput > & GetInputSet() const
Get m_selected_inputs.
void Merge(const SelectionResult &other)
Combines the.
SelectionAlgorithm m_algo
The algorithm used to produce this result.
void AddInput(const OutputGroup &group)
CAmount GetSelectedEffectiveValue() const
CAmount m_target
The target the algorithm selected for.
CAmount GetChange(const CAmount min_viable_change, const CAmount change_fee) const
Get the amount for the change output after paying needed fees.
void InsertInputs(const T &inputs)
SelectionResult(const CAmount target, SelectionAlgorithm algo)
std::set< COutput > m_selected_inputs
Set of inputs selected by the algorithm to use in the transaction.
CAmount GetSelectedValue() const
Get the sum of the input values.
CAmount GetTarget() const
SelectionAlgorithm GetAlgo() const
std::optional< CAmount > m_waste
The computed waste.
bool m_use_effective
Whether the input values for calculations should be the effective value (true) or normal value (false...