Skip to content

Commit 4b6af8f

Browse files
authored
Few fixes related to SelectCoinsGroupedByAddresses (#3144)
* Fix cache usage in SelectCoinsGroupedByAddresses * Reset cache flags in SelectCoinsGroupedByAddresses when a block is (dis)connected * MakeCollateralAmounts should call SelectCoinsGroupedByAddresses with a limited number of inputs
1 parent 859d60f commit 4b6af8f

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/privatesend/privatesend-client.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1367,8 +1367,12 @@ bool CPrivateSendClientSession::MakeCollateralAmounts(CConnman& connman)
13671367
{
13681368
if (!privateSendClient.fEnablePrivateSend || !privateSendClient.fPrivateSendRunning) return false;
13691369

1370+
// NOTE: We do not allow txes larger than 100kB, so we have to limit number of inputs here.
1371+
// We still want to consume a lot of inputs to avoid creating only smaller denoms though.
1372+
// Knowing that each CTxIn is at least 148b big, 400 inputs should take 400 x ~148b = ~60kB.
1373+
// This still leaves more than enough room for another data of typical MakeCollateralAmounts tx.
13701374
std::vector<CompactTallyItem> vecTally;
1371-
if (!vpwallets[0]->SelectCoinsGroupedByAddresses(vecTally, false, false)) {
1375+
if (!vpwallets[0]->SelectCoinsGroupedByAddresses(vecTally, false, false, true, 400)) {
13721376
LogPrint(BCLog::PRIVATESEND, "CPrivateSendClientSession::MakeCollateralAmounts -- SelectCoinsGroupedByAddresses can't find any inputs!\n");
13731377
return false;
13741378
}

src/wallet/wallet.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,6 +1473,10 @@ void CWallet::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const
14731473
}
14741474

14751475
hashPrevBestCoinbase = pblock->vtx[0]->GetHash();
1476+
1477+
// reset cache to make sure no longer immature coins are included
1478+
fAnonymizableTallyCached = false;
1479+
fAnonymizableTallyCachedNonDenom = false;
14761480
}
14771481

14781482
void CWallet::BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexDisconnected) {
@@ -1482,6 +1486,10 @@ void CWallet::BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, con
14821486
// NOTE: do NOT pass pindex here
14831487
SyncTransaction(ptx);
14841488
}
1489+
1490+
// reset cache to make sure no longer mature coins are excluded
1491+
fAnonymizableTallyCached = false;
1492+
fAnonymizableTallyCachedNonDenom = false;
14851493
}
14861494

14871495

@@ -3277,8 +3285,9 @@ bool CWallet::SelectCoinsGroupedByAddresses(std::vector<CompactTallyItem>& vecTa
32773285

32783286
isminefilter filter = ISMINE_SPENDABLE;
32793287

3280-
// try to use cache for already confirmed anonymizable inputs, no cache should be used when the limit is specified
3281-
if(nMaxOupointsPerAddress != -1 && fAnonymizable && fSkipUnconfirmed) {
3288+
// Try using the cache for already confirmed anonymizable inputs.
3289+
// This should only be used if nMaxOupointsPerAddress was NOT specified.
3290+
if(nMaxOupointsPerAddress == -1 && fAnonymizable && fSkipUnconfirmed) {
32823291
if(fSkipDenominated && fAnonymizableTallyCachedNonDenom) {
32833292
vecTallyRet = vecAnonymizableTallyCachedNonDenom;
32843293
LogPrint(BCLog::SELECTCOINS, "SelectCoinsGroupedByAddresses - using cache for non-denom inputs %d\n", vecTallyRet.size());
@@ -3350,8 +3359,9 @@ bool CWallet::SelectCoinsGroupedByAddresses(std::vector<CompactTallyItem>& vecTa
33503359
vecTallyRet.push_back(item.second);
33513360
}
33523361

3353-
// cache already confirmed anonymizable entries for later use, no cache should be saved when the limit is specified
3354-
if(nMaxOupointsPerAddress != -1 && fAnonymizable && fSkipUnconfirmed) {
3362+
// Cache already confirmed anonymizable entries for later use.
3363+
// This should only be used if nMaxOupointsPerAddress was NOT specified.
3364+
if(nMaxOupointsPerAddress == -1 && fAnonymizable && fSkipUnconfirmed) {
33553365
if(fSkipDenominated) {
33563366
vecAnonymizableTallyCachedNonDenom = vecTallyRet;
33573367
fAnonymizableTallyCachedNonDenom = true;

0 commit comments

Comments
 (0)