Skip to content

Commit 416d74f

Browse files
committed
Move OutputGroup positive only filtering into Insert
1 parent d895e98 commit 416d74f

File tree

6 files changed

+27
-52
lines changed

6 files changed

+27
-52
lines changed

src/bench/coin_selection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ static void add_coin(const CAmount& nValue, int nInput, std::vector<OutputGroup>
7575
tx.vout[nInput].nValue = nValue;
7676
std::unique_ptr<CWalletTx> wtx = MakeUnique<CWalletTx>(&testWallet, MakeTransactionRef(std::move(tx)));
7777
set.emplace_back();
78-
set.back().Insert(COutput(wtx.get(), nInput, 0, true, true, true).GetInputCoin(), 0, true, 0, 0);
78+
set.back().Insert(COutput(wtx.get(), nInput, 0, true, true, true).GetInputCoin(), 0, true, 0, 0, false);
7979
wtxn.emplace_back(std::move(wtx));
8080
}
8181
// Copied from src/wallet/test/coinselector_tests.cpp

src/wallet/coinselection.cpp

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -300,16 +300,24 @@ bool KnapsackSolver(const CAmount& nTargetValue, std::vector<OutputGroup>& group
300300
301301
******************************************************************************/
302302

303-
void OutputGroup::Insert(const CInputCoin& output, int depth, bool from_me, size_t ancestors, size_t descendants) {
303+
void OutputGroup::Insert(const CInputCoin& output, int depth, bool from_me, size_t ancestors, size_t descendants, bool positive_only) {
304+
// Compute the effective value first
305+
const CAmount coin_fee = output.m_input_bytes < 0 ? 0 : m_effective_feerate.GetFee(output.m_input_bytes);
306+
const CAmount ev = output.txout.nValue - coin_fee;
307+
308+
// Filter for positive only here before adding the coin
309+
if (positive_only && ev <= 0) return;
310+
304311
m_outputs.push_back(output);
305312
CInputCoin& coin = m_outputs.back();
306-
coin.m_fee = coin.m_input_bytes < 0 ? 0 : m_effective_feerate.GetFee(coin.m_input_bytes);
313+
314+
coin.m_fee = coin_fee;
307315
fee += coin.m_fee;
308316

309317
coin.m_long_term_fee = coin.m_input_bytes < 0 ? 0 : m_long_term_feerate.GetFee(coin.m_input_bytes);
310318
long_term_fee += coin.m_long_term_fee;
311319

312-
coin.effective_value = coin.txout.nValue - coin.m_fee;
320+
coin.effective_value = ev;
313321
effective_value += coin.effective_value;
314322

315323
m_from_me &= from_me;
@@ -324,35 +332,9 @@ void OutputGroup::Insert(const CInputCoin& output, int depth, bool from_me, size
324332
m_descendants = std::max(m_descendants, descendants);
325333
}
326334

327-
std::vector<CInputCoin>::iterator OutputGroup::Discard(const CInputCoin& output) {
328-
auto it = m_outputs.begin();
329-
while (it != m_outputs.end() && it->outpoint != output.outpoint) ++it;
330-
if (it == m_outputs.end()) return it;
331-
m_value -= output.txout.nValue;
332-
effective_value -= output.effective_value;
333-
fee -= output.m_fee;
334-
long_term_fee -= output.m_long_term_fee;
335-
return m_outputs.erase(it);
336-
}
337-
338335
bool OutputGroup::EligibleForSpending(const CoinEligibilityFilter& eligibility_filter) const
339336
{
340337
return m_depth >= (m_from_me ? eligibility_filter.conf_mine : eligibility_filter.conf_theirs)
341338
&& m_ancestors <= eligibility_filter.max_ancestors
342339
&& m_descendants <= eligibility_filter.max_descendants;
343340
}
344-
345-
OutputGroup OutputGroup::GetPositiveOnlyGroup()
346-
{
347-
OutputGroup group(*this);
348-
for (auto it = group.m_outputs.begin(); it != group.m_outputs.end(); ) {
349-
const CInputCoin& coin = *it;
350-
// Only include outputs that are positive effective value (i.e. not dust)
351-
if (coin.effective_value <= 0) {
352-
it = group.Discard(coin);
353-
} else {
354-
++it;
355-
}
356-
}
357-
return group;
358-
}

src/wallet/coinselection.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,8 @@ struct OutputGroup
8787
m_long_term_feerate(long_term_feerate)
8888
{}
8989

90-
void Insert(const CInputCoin& output, int depth, bool from_me, size_t ancestors, size_t descendants);
91-
std::vector<CInputCoin>::iterator Discard(const CInputCoin& output);
90+
void Insert(const CInputCoin& output, int depth, bool from_me, size_t ancestors, size_t descendants, bool positive_only);
9291
bool EligibleForSpending(const CoinEligibilityFilter& eligibility_filter) const;
93-
94-
OutputGroup GetPositiveOnlyGroup();
9592
};
9693

9794
bool SelectCoinsBnB(std::vector<OutputGroup>& utxo_pool, const CAmount& target_value, const CAmount& cost_of_change, std::set<CInputCoin>& out_set, CAmount& value_ret, CAmount not_input_fees);

src/wallet/test/coinselector_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ inline std::vector<OutputGroup>& GroupCoins(const std::vector<CInputCoin>& coins
116116
static_groups.clear();
117117
for (auto& coin : coins) {
118118
static_groups.emplace_back();
119-
static_groups.back().Insert(coin, 0, true, 0, 0);
119+
static_groups.back().Insert(coin, 0, true, 0, 0, false);
120120
}
121121
return static_groups;
122122
}
@@ -127,7 +127,7 @@ inline std::vector<OutputGroup>& GroupCoins(const std::vector<COutput>& coins)
127127
static_groups.clear();
128128
for (auto& coin : coins) {
129129
static_groups.emplace_back();
130-
static_groups.back().Insert(coin.GetInputCoin(), coin.nDepth, coin.tx->m_amounts[CWalletTx::DEBIT].m_cached[ISMINE_SPENDABLE] && coin.tx->m_amounts[CWalletTx::DEBIT].m_value[ISMINE_SPENDABLE] == 1 /* HACK: we can't figure out the is_me flag so we use the conditions defined above; perhaps set safe to false for !fIsFromMe in add_coin() */, 0, 0);
130+
static_groups.back().Insert(coin.GetInputCoin(), coin.nDepth, coin.tx->m_amounts[CWalletTx::DEBIT].m_cached[ISMINE_SPENDABLE] && coin.tx->m_amounts[CWalletTx::DEBIT].m_value[ISMINE_SPENDABLE] == 1 /* HACK: we can't figure out the is_me flag so we use the conditions defined above; perhaps set safe to false for !fIsFromMe in add_coin() */, 0, 0, false);
131131
}
132132
return static_groups;
133133
}

src/wallet/wallet.cpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2374,7 +2374,6 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const CoinEligibil
23742374
nValueRet = 0;
23752375

23762376
if (coin_selection_params.use_bnb) {
2377-
std::vector<OutputGroup> utxo_pool;
23782377
// Get long term estimate
23792378
FeeCalculation feeCalc;
23802379
CCoinControl temp;
@@ -2388,22 +2387,17 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const CoinEligibil
23882387
effective_feerate = coin_selection_params.effective_fee;
23892388
}
23902389

2391-
std::vector<OutputGroup> groups = GroupOutputs(coins, !coin_selection_params.m_avoid_partial_spends, eligibility_filter.max_ancestors, effective_feerate, long_term_feerate, eligibility_filter);
2390+
std::vector<OutputGroup> groups = GroupOutputs(coins, !coin_selection_params.m_avoid_partial_spends, eligibility_filter.max_ancestors, effective_feerate, long_term_feerate, eligibility_filter, true /* positive_only */);
23922391

23932392
// Calculate cost of change
23942393
CAmount cost_of_change = GetDiscardRate(*this).GetFee(coin_selection_params.change_spend_size) + coin_selection_params.effective_fee.GetFee(coin_selection_params.change_output_size);
23952394

2396-
// Filter by the min conf specs and add to utxo_pool and calculate effective value
2397-
for (OutputGroup& group : groups) {
2398-
OutputGroup pos_group = group.GetPositiveOnlyGroup();
2399-
if (pos_group.effective_value > 0) utxo_pool.push_back(pos_group);
2400-
}
24012395
// Calculate the fees for things that aren't inputs
24022396
CAmount not_input_fees = coin_selection_params.effective_fee.GetFee(coin_selection_params.tx_noinputs_size);
24032397
bnb_used = true;
2404-
return SelectCoinsBnB(utxo_pool, nTargetValue, cost_of_change, setCoinsRet, nValueRet, not_input_fees);
2398+
return SelectCoinsBnB(groups, nTargetValue, cost_of_change, setCoinsRet, nValueRet, not_input_fees);
24052399
} else {
2406-
std::vector<OutputGroup> groups = GroupOutputs(coins, !coin_selection_params.m_avoid_partial_spends, eligibility_filter.max_ancestors, CFeeRate(0), CFeeRate(0), eligibility_filter);
2400+
std::vector<OutputGroup> groups = GroupOutputs(coins, !coin_selection_params.m_avoid_partial_spends, eligibility_filter.max_ancestors, CFeeRate(0), CFeeRate(0), eligibility_filter, false /* positive_only */);
24072401

24082402
bnb_used = false;
24092403
return KnapsackSolver(nTargetValue, groups, setCoinsRet, nValueRet);
@@ -4199,7 +4193,7 @@ bool CWalletTx::IsImmatureCoinBase() const
41994193
return GetBlocksToMaturity() > 0;
42004194
}
42014195

4202-
std::vector<OutputGroup> CWallet::GroupOutputs(const std::vector<COutput>& outputs, bool single_coin, const size_t max_ancestors, const CFeeRate& effective_feerate, const CFeeRate& long_term_feerate, const CoinEligibilityFilter& filter) const {
4196+
std::vector<OutputGroup> CWallet::GroupOutputs(const std::vector<COutput>& outputs, bool single_coin, const size_t max_ancestors, const CFeeRate& effective_feerate, const CFeeRate& long_term_feerate, const CoinEligibilityFilter& filter, bool positive_only) const {
42034197
std::vector<OutputGroup> groups;
42044198
std::map<CTxDestination, OutputGroup> gmap;
42054199
std::set<CTxDestination> full_groups;
@@ -4224,16 +4218,17 @@ std::vector<OutputGroup> CWallet::GroupOutputs(const std::vector<COutput>& outpu
42244218
it->second = OutputGroup{effective_feerate, long_term_feerate};
42254219
full_groups.insert(dst);
42264220
}
4227-
it->second.Insert(input_coin, output.nDepth, output.tx->IsFromMe(ISMINE_ALL), ancestors, descendants);
4221+
it->second.Insert(input_coin, output.nDepth, output.tx->IsFromMe(ISMINE_ALL), ancestors, descendants, positive_only);
42284222
} else {
42294223
auto ins = gmap.emplace(dst, OutputGroup{effective_feerate, long_term_feerate});
4230-
ins.first->second.Insert(input_coin, output.nDepth, output.tx->IsFromMe(ISMINE_ALL), ancestors, descendants);
4224+
ins.first->second.Insert(input_coin, output.nDepth, output.tx->IsFromMe(ISMINE_ALL), ancestors, descendants, positive_only);
42314225
}
42324226
} else {
42334227
// This is for if each output gets it's own OutputGroup
42344228
OutputGroup coin(effective_feerate, long_term_feerate);
4235-
coin.Insert(input_coin, output.nDepth, output.tx->IsFromMe(ISMINE_ALL), ancestors, descendants);
4236-
if (coin.EligibleForSpending(filter)) groups.push_back(coin);
4229+
coin.Insert(input_coin, output.nDepth, output.tx->IsFromMe(ISMINE_ALL), ancestors, descendants, positive_only);
4230+
if (positive_only && coin.effective_value <= 0) continue;
4231+
if (coin.m_outputs.size() > 0 && coin.EligibleForSpending(filter)) groups.push_back(coin);
42374232
}
42384233
}
42394234
}
@@ -4245,7 +4240,8 @@ std::vector<OutputGroup> CWallet::GroupOutputs(const std::vector<COutput>& outpu
42454240
group.m_ancestors = max_ancestors - 1;
42464241
}
42474242
// If the OutputGroup is not eligible, don't add it
4248-
if (group.EligibleForSpending(filter)) groups.push_back(group);
4243+
if (positive_only && group.effective_value <= 0) continue;
4244+
if (group.m_outputs.size() > 0 && group.EligibleForSpending(filter)) groups.push_back(group);
42494245
}
42504246
}
42514247
return groups;

src/wallet/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
841841
bool IsSpentKey(const uint256& hash, unsigned int n) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
842842
void SetSpentKeyState(WalletBatch& batch, const uint256& hash, unsigned int n, bool used, std::set<CTxDestination>& tx_destinations) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
843843

844-
std::vector<OutputGroup> GroupOutputs(const std::vector<COutput>& outputs, bool single_coin, const size_t max_ancestors, const CFeeRate& effective_feerate, const CFeeRate& long_term_feerate, const CoinEligibilityFilter& filter) const;
844+
std::vector<OutputGroup> GroupOutputs(const std::vector<COutput>& outputs, bool single_coin, const size_t max_ancestors, const CFeeRate& effective_feerate, const CFeeRate& long_term_feerate, const CoinEligibilityFilter& filter, bool positive_only) const;
845845

846846
bool IsLockedCoin(uint256 hash, unsigned int n) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
847847
void LockCoin(const COutPoint& output) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);

0 commit comments

Comments
 (0)