Skip to content

Commit

Permalink
More randomization in PrepareDenominate
Browse files Browse the repository at this point in the history
  • Loading branch information
UdjinM6 committed Sep 26, 2018
1 parent 33c12af commit ab86bd0
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/privatesend-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1268,16 +1268,15 @@ bool CPrivateSendClientSession::PrepareDenominate(int nMinRounds, int nMaxRounds
pwalletMain->LockCoin(pair.first.prevout);
}

// Try to add every needed denomination, repeat up to 5-PRIVATESEND_ENTRY_MAX_SIZE times.
// NOTE: No need to randomize order of inputs because they were
// initially shuffled in CWallet::SelectPSInOutPairsByDenominations already.
int nStepsMax = 5 + GetRandInt(PRIVATESEND_ENTRY_MAX_SIZE - 5 + 1);
int nDenomResult{0};

std::vector<CAmount> vecStandardDenoms = CPrivateSend::GetStandardDenominations();
std::vector<int> vecSteps(vecStandardDenoms.size(), 0);
vecPSInOutPairsRet.clear();

// Try to add up to PRIVATESEND_ENTRY_MAX_SIZE of every needed denomination
for (const auto& pair: vecPSInOutPairsIn) {
if (pair.second.nRounds < nMinRounds || pair.second.nRounds > nMaxRounds) {
// unlock unused coins
Expand All @@ -1286,15 +1285,20 @@ bool CPrivateSendClientSession::PrepareDenominate(int nMinRounds, int nMaxRounds
}
bool fFound = false;
for (const auto& nBit : vecBits) {
if (vecSteps[nBit] >= nStepsMax) break;
if (vecSteps[nBit] >= PRIVATESEND_ENTRY_MAX_SIZE) break;
CAmount nValueDenom = vecStandardDenoms[nBit];
if (pair.second.nValue == nValueDenom) {
CScript scriptDenom;
if (fDryRun) {
scriptDenom = CScript();
} else {
// randomly skip some inputs when we have at least one of the same denom already
if (vecSteps[nBit] > 1 && GetRandInt(2)) break;
if (vecSteps[nBit] > 1 && GetRandInt(2)) {
// still count it as a step to randomize number of inputs
// if we have more than (or exactly) PRIVATESEND_ENTRY_MAX_SIZE of them
++vecSteps[nBit];
break;
}
scriptDenom = keyHolderStorage.AddKey(pwalletMain);
}
vecPSInOutPairsRet.emplace_back(pair.first, CTxOut(nValueDenom, scriptDenom));
Expand Down

0 comments on commit ab86bd0

Please sign in to comment.