mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-12 06:58:57 +01:00
Set effective_value when initializing a COutput
Previously in COutput, effective_value was initialized as the absolute value of the txout, and fee as 0. effective_value along with fee were calculated outside of the COutput constructor and set after the object had been initialized. These changes will allow either the fee or the feerate to be passed in a COutput constructor. If either are provided, fee and effective_value are calculated and set in the constructor. As a result, AvailableCoins also needs to be passed the feerate when utxos are being spent. When balance is calculated or the coins are being listed and feerate is neither available nor required, AvailableCoinsListUnspent is used instead, which runs AvailableCoins while providing the default value for feerate. Unit tests for the calculation of effective value have also been added.
This commit is contained in:
@@ -328,24 +328,18 @@ std::optional<SelectionResult> KnapsackSolver(std::vector<OutputGroup>& groups,
|
||||
******************************************************************************/
|
||||
|
||||
void OutputGroup::Insert(const COutput& output, size_t ancestors, size_t descendants, bool positive_only) {
|
||||
// Compute the effective value first
|
||||
const CAmount coin_fee = output.input_bytes < 0 ? 0 : m_effective_feerate.GetFee(output.input_bytes);
|
||||
const CAmount ev = output.txout.nValue - coin_fee;
|
||||
|
||||
// Filter for positive only here before adding the coin
|
||||
if (positive_only && ev <= 0) return;
|
||||
if (positive_only && output.GetEffectiveValue() <= 0) return;
|
||||
|
||||
m_outputs.push_back(output);
|
||||
COutput& coin = m_outputs.back();
|
||||
|
||||
coin.fee = coin_fee;
|
||||
fee += coin.fee;
|
||||
fee += coin.GetFee();
|
||||
|
||||
coin.long_term_fee = coin.input_bytes < 0 ? 0 : m_long_term_feerate.GetFee(coin.input_bytes);
|
||||
long_term_fee += coin.long_term_fee;
|
||||
|
||||
coin.effective_value = ev;
|
||||
effective_value += coin.effective_value;
|
||||
effective_value += coin.GetEffectiveValue();
|
||||
|
||||
m_from_me &= coin.from_me;
|
||||
m_value += coin.txout.nValue;
|
||||
@@ -380,8 +374,8 @@ CAmount GetSelectionWaste(const std::set<COutput>& inputs, CAmount change_cost,
|
||||
CAmount waste = 0;
|
||||
CAmount selected_effective_value = 0;
|
||||
for (const COutput& coin : inputs) {
|
||||
waste += coin.fee - coin.long_term_fee;
|
||||
selected_effective_value += use_effective_value ? coin.effective_value : coin.txout.nValue;
|
||||
waste += coin.GetFee() - coin.long_term_fee;
|
||||
selected_effective_value += use_effective_value ? coin.GetEffectiveValue() : coin.txout.nValue;
|
||||
}
|
||||
|
||||
if (change_cost) {
|
||||
|
||||
Reference in New Issue
Block a user