aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Chow <achow101-github@achow101.com>2021-04-23 15:14:41 -0400
committerAndrew Chow <achow101-github@achow101.com>2021-05-19 15:35:11 -0400
commit51a3ac242c92e69b59df26f8f9e287b31e5c3b0f (patch)
treebcdd8ea4f69744deab78032d16d448971b587411
parent6d6d2784759878ef0c4ac128d12aac68add1edca (diff)
downloadbitcoin-51a3ac242c92e69b59df26f8f9e287b31e5c3b0f.tar.xz
Have OutputGroup determine the value to use
Instead of hijacking the effective_feerate to use the correct value during coin selection, have OutputGroup be aware of whether we are subtracting the fee from the outputs and provide the correct value to use for selection. To do this, OutputGroup now takes CoinSelectionParams and has a new function GetSelectionAmount().
-rw-r--r--src/wallet/coinselection.cpp31
-rw-r--r--src/wallet/coinselection.h52
-rw-r--r--src/wallet/wallet.cpp27
-rw-r--r--src/wallet/wallet.h43
4 files changed, 78 insertions, 75 deletions
diff --git a/src/wallet/coinselection.cpp b/src/wallet/coinselection.cpp
index af88bf85dd..6d502e1df1 100644
--- a/src/wallet/coinselection.cpp
+++ b/src/wallet/coinselection.cpp
@@ -14,7 +14,7 @@
struct {
bool operator()(const OutputGroup& a, const OutputGroup& b) const
{
- return a.effective_value > b.effective_value;
+ return a.GetSelectionAmount() > b.GetSelectionAmount();
}
} descending;
@@ -73,8 +73,8 @@ bool SelectCoinsBnB(std::vector<OutputGroup>& utxo_pool, const CAmount& selectio
CAmount curr_available_value = 0;
for (const OutputGroup& utxo : utxo_pool) {
// Assert that this utxo is not negative. It should never be negative, effective value calculation should have removed it
- assert(utxo.effective_value > 0);
- curr_available_value += utxo.effective_value;
+ assert(utxo.GetSelectionAmount() > 0);
+ curr_available_value += utxo.GetSelectionAmount();
}
if (curr_available_value < selection_target) {
return false;
@@ -118,7 +118,7 @@ bool SelectCoinsBnB(std::vector<OutputGroup>& utxo_pool, const CAmount& selectio
// Walk backwards to find the last included UTXO that still needs to have its omission branch traversed.
while (!curr_selection.empty() && !curr_selection.back()) {
curr_selection.pop_back();
- curr_available_value += utxo_pool.at(curr_selection.size()).effective_value;
+ curr_available_value += utxo_pool.at(curr_selection.size()).GetSelectionAmount();
}
if (curr_selection.empty()) { // We have walked back to the first utxo and no branch is untraversed. All solutions searched
@@ -128,24 +128,24 @@ bool SelectCoinsBnB(std::vector<OutputGroup>& utxo_pool, const CAmount& selectio
// Output was included on previous iterations, try excluding now.
curr_selection.back() = false;
OutputGroup& utxo = utxo_pool.at(curr_selection.size() - 1);
- curr_value -= utxo.effective_value;
+ curr_value -= utxo.GetSelectionAmount();
curr_waste -= utxo.fee - utxo.long_term_fee;
} else { // Moving forwards, continuing down this branch
OutputGroup& utxo = utxo_pool.at(curr_selection.size());
// Remove this utxo from the curr_available_value utxo amount
- curr_available_value -= utxo.effective_value;
+ curr_available_value -= utxo.GetSelectionAmount();
// Avoid searching a branch if the previous UTXO has the same value and same waste and was excluded. Since the ratio of fee to
// long term fee is the same, we only need to check if one of those values match in order to know that the waste is the same.
if (!curr_selection.empty() && !curr_selection.back() &&
- utxo.effective_value == utxo_pool.at(curr_selection.size() - 1).effective_value &&
+ utxo.GetSelectionAmount() == utxo_pool.at(curr_selection.size() - 1).GetSelectionAmount() &&
utxo.fee == utxo_pool.at(curr_selection.size() - 1).fee) {
curr_selection.push_back(false);
} else {
// Inclusion branch first (Largest First Exploration)
curr_selection.push_back(true);
- curr_value += utxo.effective_value;
+ curr_value += utxo.GetSelectionAmount();
curr_waste += utxo.fee - utxo.long_term_fee;
}
}
@@ -227,14 +227,14 @@ bool KnapsackSolver(const CAmount& nTargetValue, std::vector<OutputGroup>& group
Shuffle(groups.begin(), groups.end(), FastRandomContext());
for (const OutputGroup& group : groups) {
- if (group.effective_value == nTargetValue) {
+ if (group.GetSelectionAmount() == nTargetValue) {
util::insert(setCoinsRet, group.m_outputs);
nValueRet += group.m_value;
return true;
- } else if (group.effective_value < nTargetValue + MIN_CHANGE) {
+ } else if (group.GetSelectionAmount() < nTargetValue + MIN_CHANGE) {
applicable_groups.push_back(group);
- nTotalLower += group.effective_value;
- } else if (!lowest_larger || group.effective_value < lowest_larger->effective_value) {
+ nTotalLower += group.GetSelectionAmount();
+ } else if (!lowest_larger || group.GetSelectionAmount() < lowest_larger->GetSelectionAmount()) {
lowest_larger = group;
}
}
@@ -267,7 +267,7 @@ bool KnapsackSolver(const CAmount& nTargetValue, std::vector<OutputGroup>& group
// If we have a bigger coin and (either the stochastic approximation didn't find a good solution,
// or the next bigger coin is closer), return the bigger coin
if (lowest_larger &&
- ((nBest != nTargetValue && nBest < nTargetValue + MIN_CHANGE) || lowest_larger->effective_value <= nBest)) {
+ ((nBest != nTargetValue && nBest < nTargetValue + MIN_CHANGE) || lowest_larger->GetSelectionAmount() <= nBest)) {
util::insert(setCoinsRet, lowest_larger->m_outputs);
nValueRet += lowest_larger->m_value;
} else {
@@ -336,3 +336,8 @@ bool OutputGroup::EligibleForSpending(const CoinEligibilityFilter& eligibility_f
&& m_ancestors <= eligibility_filter.max_ancestors
&& m_descendants <= eligibility_filter.max_descendants;
}
+
+CAmount OutputGroup::GetSelectionAmount() const
+{
+ return m_subtract_fee_outputs ? m_value : effective_value;
+}
diff --git a/src/wallet/coinselection.h b/src/wallet/coinselection.h
index af23178d04..7a3fb82139 100644
--- a/src/wallet/coinselection.h
+++ b/src/wallet/coinselection.h
@@ -57,6 +57,47 @@ public:
}
};
+/** Parameters for one iteration of Coin Selection. */
+struct CoinSelectionParams
+{
+ /** Size of a change output in bytes, determined by the output type. */
+ size_t change_output_size = 0;
+ /** Size of the input to spend a change output in virtual bytes. */
+ size_t change_spend_size = 0;
+ /** Cost of creating the change output. */
+ CAmount m_change_fee{0};
+ /** Cost of creating the change output + cost of spending the change output in the future. */
+ CAmount m_cost_of_change{0};
+ /** The targeted feerate of the transaction being built. */
+ CFeeRate m_effective_feerate;
+ /** The feerate estimate used to estimate an upper bound on what should be sufficient to spend
+ * the change output sometime in the future. */
+ CFeeRate m_long_term_feerate;
+ /** If the cost to spend a change output at the discard feerate exceeds its value, drop it to fees. */
+ CFeeRate m_discard_feerate;
+ /** Size of the transaction before coin selection, consisting of the header and recipient
+ * output(s), excluding the inputs and change output(s). */
+ size_t tx_noinputs_size = 0;
+ /** Indicate that we are subtracting the fee from outputs */
+ bool m_subtract_fee_outputs = false;
+ /** When true, always spend all (up to OUTPUT_GROUP_MAX_ENTRIES) or none of the outputs
+ * associated with the same address. This helps reduce privacy leaks resulting from address
+ * reuse. Dust outputs are not eligible to be added to output groups and thus not considered. */
+ bool m_avoid_partial_spends = false;
+
+ CoinSelectionParams(size_t change_output_size, size_t change_spend_size, CFeeRate effective_feerate,
+ CFeeRate long_term_feerate, CFeeRate discard_feerate, size_t tx_noinputs_size, bool avoid_partial) :
+ change_output_size(change_output_size),
+ change_spend_size(change_spend_size),
+ m_effective_feerate(effective_feerate),
+ m_long_term_feerate(long_term_feerate),
+ m_discard_feerate(discard_feerate),
+ tx_noinputs_size(tx_noinputs_size),
+ m_avoid_partial_spends(avoid_partial)
+ {}
+ CoinSelectionParams() {}
+};
+
/** Parameters for filtering which OutputGroups we may use in coin selection.
* We start by being very selective and requiring multiple confirmations and
* then get more permissive if we cannot fund the transaction. */
@@ -109,15 +150,20 @@ struct OutputGroup
* a lower feerate). Calculated using long term fee estimate. This is used to decide whether
* it could be economical to create a change output. */
CFeeRate m_long_term_feerate{0};
+ /** Indicate that we are subtracting the fee from outputs.
+ * When true, the value that is used for coin selection is the UTXO's real value rather than effective value */
+ bool m_subtract_fee_outputs{false};
OutputGroup() {}
- OutputGroup(const CFeeRate& effective_feerate, const CFeeRate& long_term_feerate) :
- m_effective_feerate(effective_feerate),
- m_long_term_feerate(long_term_feerate)
+ OutputGroup(const CoinSelectionParams& params) :
+ m_effective_feerate(params.m_effective_feerate),
+ m_long_term_feerate(params.m_long_term_feerate),
+ m_subtract_fee_outputs(params.m_subtract_fee_outputs)
{}
void Insert(const CInputCoin& output, int depth, bool from_me, size_t ancestors, size_t descendants, bool positive_only);
bool EligibleForSpending(const CoinEligibilityFilter& eligibility_filter) const;
+ CAmount GetSelectionAmount() const;
};
bool SelectCoinsBnB(std::vector<OutputGroup>& utxo_pool, const CAmount& selection_target, const CAmount& cost_of_change, std::set<CInputCoin>& out_set, CAmount& value_ret);
diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp
index 303823695e..78243a0136 100644
--- a/src/wallet/wallet.cpp
+++ b/src/wallet/wallet.cpp
@@ -2399,20 +2399,13 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const CoinEligibil
setCoinsRet.clear();
nValueRet = 0;
- // Get the feerate for effective value.
- // When subtracting the fee from the outputs, we want the effective feerate to be 0
- CFeeRate effective_feerate{0};
- if (!coin_selection_params.m_subtract_fee_outputs) {
- effective_feerate = coin_selection_params.m_effective_feerate;
- }
-
- std::vector<OutputGroup> positive_groups = GroupOutputs(coins, !coin_selection_params.m_avoid_partial_spends, effective_feerate, coin_selection_params.m_long_term_feerate, eligibility_filter, true /* positive_only */);
// Note that unlike KnapsackSolver, we do not include the fee for creating a change output as BnB will not create a change output.
+ std::vector<OutputGroup> positive_groups = GroupOutputs(coins, coin_selection_params, eligibility_filter, true /* positive_only */);
if (SelectCoinsBnB(positive_groups, nTargetValue, coin_selection_params.m_cost_of_change, setCoinsRet, nValueRet)) {
return true;
}
// The knapsack solver has some legacy behavior where it will spend dust outputs. We retain this behavior, so don't filter for positive only here.
- std::vector<OutputGroup> all_groups = GroupOutputs(coins, !coin_selection_params.m_avoid_partial_spends, effective_feerate, coin_selection_params.m_long_term_feerate, eligibility_filter, false /* positive_only */);
+ std::vector<OutputGroup> all_groups = GroupOutputs(coins, coin_selection_params, eligibility_filter, false /* positive_only */);
// While nTargetValue includes the transaction fees for non-input things, it does not include the fee for creating a change output.
// So we need to include that for KnapsackSolver as well, as we are expecting to create a change output.
return KnapsackSolver(nTargetValue + coin_selection_params.m_change_fee, all_groups, setCoinsRet, nValueRet);
@@ -4223,12 +4216,12 @@ bool CWalletTx::IsImmatureCoinBase() const
return GetBlocksToMaturity() > 0;
}
-std::vector<OutputGroup> CWallet::GroupOutputs(const std::vector<COutput>& outputs, bool separate_coins, const CFeeRate& effective_feerate, const CFeeRate& long_term_feerate, const CoinEligibilityFilter& filter, bool positive_only) const
+std::vector<OutputGroup> CWallet::GroupOutputs(const std::vector<COutput>& outputs, const CoinSelectionParams& coin_sel_params, const CoinEligibilityFilter& filter, bool positive_only) const
{
std::vector<OutputGroup> groups_out;
- if (separate_coins) {
- // Single coin means no grouping. Each COutput gets its own OutputGroup.
+ if (!coin_sel_params.m_avoid_partial_spends) {
+ // Allowing partial spends means no grouping. Each COutput gets its own OutputGroup.
for (const COutput& output : outputs) {
// Skip outputs we cannot spend
if (!output.fSpendable) continue;
@@ -4238,11 +4231,11 @@ std::vector<OutputGroup> CWallet::GroupOutputs(const std::vector<COutput>& outpu
CInputCoin input_coin = output.GetInputCoin();
// Make an OutputGroup containing just this output
- OutputGroup group{effective_feerate, long_term_feerate};
+ OutputGroup group{coin_sel_params};
group.Insert(input_coin, output.nDepth, output.tx->IsFromMe(ISMINE_ALL), ancestors, descendants, positive_only);
// Check the OutputGroup's eligibility. Only add the eligible ones.
- if (positive_only && group.effective_value <= 0) continue;
+ if (positive_only && group.GetSelectionAmount() <= 0) continue;
if (group.m_outputs.size() > 0 && group.EligibleForSpending(filter)) groups_out.push_back(group);
}
return groups_out;
@@ -4268,7 +4261,7 @@ std::vector<OutputGroup> CWallet::GroupOutputs(const std::vector<COutput>& outpu
if (groups.size() == 0) {
// No OutputGroups for this scriptPubKey yet, add one
- groups.emplace_back(effective_feerate, long_term_feerate);
+ groups.emplace_back(coin_sel_params);
}
// Get the last OutputGroup in the vector so that we can add the CInputCoin to it
@@ -4279,7 +4272,7 @@ std::vector<OutputGroup> CWallet::GroupOutputs(const std::vector<COutput>& outpu
// to avoid surprising users with very high fees.
if (group->m_outputs.size() >= OUTPUT_GROUP_MAX_ENTRIES) {
// The last output group is full, add a new group to the vector and use that group for the insertion
- groups.emplace_back(effective_feerate, long_term_feerate);
+ groups.emplace_back(coin_sel_params);
group = &groups.back();
}
@@ -4301,7 +4294,7 @@ std::vector<OutputGroup> CWallet::GroupOutputs(const std::vector<COutput>& outpu
}
// Check the OutputGroup's eligibility. Only add the eligible ones.
- if (positive_only && group.effective_value <= 0) continue;
+ if (positive_only && group.GetSelectionAmount() <= 0) continue;
if (group.m_outputs.size() > 0 && group.EligibleForSpending(filter)) groups_out.push_back(group);
}
}
diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h
index 028131174f..1f4150b702 100644
--- a/src/wallet/wallet.h
+++ b/src/wallet/wallet.h
@@ -612,47 +612,6 @@ public:
}
};
-/** Parameters for one iteration of Coin Selection. */
-struct CoinSelectionParams
-{
- /** Size of a change output in bytes, determined by the output type. */
- size_t change_output_size = 0;
- /** Size of the input to spend a change output in virtual bytes. */
- size_t change_spend_size = 0;
- /** Cost of creating the change output. */
- CAmount m_change_fee{0};
- /** Cost of creating the change output + cost of spending the change output in the future. */
- CAmount m_cost_of_change{0};
- /** The targeted feerate of the transaction being built. */
- CFeeRate m_effective_feerate;
- /** The feerate estimate used to estimate an upper bound on what should be sufficient to spend
- * the change output sometime in the future. */
- CFeeRate m_long_term_feerate;
- /** If the cost to spend a change output at the discard feerate exceeds its value, drop it to fees. */
- CFeeRate m_discard_feerate;
- /** Size of the transaction before coin selection, consisting of the header and recipient
- * output(s), excluding the inputs and change output(s). */
- size_t tx_noinputs_size = 0;
- /** Indicate that we are subtracting the fee from outputs */
- bool m_subtract_fee_outputs = false;
- /** When true, always spend all (up to OUTPUT_GROUP_MAX_ENTRIES) or none of the outputs
- * associated with the same address. This helps reduce privacy leaks resulting from address
- * reuse. Dust outputs are not eligible to be added to output groups and thus not considered. */
- bool m_avoid_partial_spends = false;
-
- CoinSelectionParams(size_t change_output_size, size_t change_spend_size, CFeeRate effective_feerate,
- CFeeRate long_term_feerate, CFeeRate discard_feerate, size_t tx_noinputs_size, bool avoid_partial) :
- change_output_size(change_output_size),
- change_spend_size(change_spend_size),
- m_effective_feerate(effective_feerate),
- m_long_term_feerate(long_term_feerate),
- m_discard_feerate(discard_feerate),
- tx_noinputs_size(tx_noinputs_size),
- m_avoid_partial_spends(avoid_partial)
- {}
- CoinSelectionParams() {}
-};
-
class WalletRescanReserver; //forward declarations for ScanForWalletTransactions/RescanFromTime
/**
* A CWallet maintains a set of transactions and balances, and provides the ability to create new transactions.
@@ -883,7 +842,7 @@ public:
bool IsSpentKey(const uint256& hash, unsigned int n) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
void SetSpentKeyState(WalletBatch& batch, const uint256& hash, unsigned int n, bool used, std::set<CTxDestination>& tx_destinations) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
- std::vector<OutputGroup> GroupOutputs(const std::vector<COutput>& outputs, bool separate_coins, const CFeeRate& effective_feerate, const CFeeRate& long_term_feerate, const CoinEligibilityFilter& filter, bool positive_only) const;
+ std::vector<OutputGroup> GroupOutputs(const std::vector<COutput>& outputs, const CoinSelectionParams& coin_sel_params, const CoinEligibilityFilter& filter, bool positive_only) const;
/** Display address on an external signer. Returns false if external signer support is not compiled */
bool DisplayAddress(const CTxDestination& dest) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);