Skip to content

Commit 7334aa5

Browse files
PastaPastaPastaUdjinM6
authored andcommitted
adjust privatesend formatting and follow some best practices (#2979)
* adjust privatesend files formatting and follow some best practices * code review Signed-off-by: Pasta <Pasta@dash.org> * use auto for iterators Signed-off-by: Pasta <Pasta@dash.org> * review pt2 Signed-off-by: Pasta <Pasta@dash.org>
1 parent f14179c commit 7334aa5

File tree

5 files changed

+71
-65
lines changed

5 files changed

+71
-65
lines changed

src/privatesend/privatesend-client.cpp

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void CPrivateSendClientManager::ProcessMessage(CNode* pfrom, const std::string&
9999
int64_t nLastDsq = mmetaman.GetMetaInfo(dmn->proTxHash)->GetLastDsq();
100100
int nThreshold = nLastDsq + mnList.GetValidMNsCount() / 5;
101101
LogPrint(BCLog::PRIVATESEND, "DSQUEUE -- nLastDsq: %d threshold: %d nDsqCount: %d\n", nLastDsq, nThreshold, mmetaman.GetDsqCount());
102-
//don't allow a few nodes to dominate the queuing process
102+
// don't allow a few nodes to dominate the queuing process
103103
if (nLastDsq != 0 && nThreshold > mmetaman.GetDsqCount()) {
104104
LogPrint(BCLog::PRIVATESEND, "DSQUEUE -- Masternode %s is sending too many dsq messages\n", dmn->proTxHash.ToString());
105105
return;
@@ -118,10 +118,9 @@ void CPrivateSendClientManager::ProcessMessage(CNode* pfrom, const std::string&
118118
dsq.Relay(connman);
119119
}
120120

121-
} else if (
122-
strCommand == NetMsgType::DSSTATUSUPDATE ||
123-
strCommand == NetMsgType::DSFINALTX ||
124-
strCommand == NetMsgType::DSCOMPLETE) {
121+
} else if (strCommand == NetMsgType::DSSTATUSUPDATE ||
122+
strCommand == NetMsgType::DSFINALTX ||
123+
strCommand == NetMsgType::DSCOMPLETE) {
125124
LOCK(cs_deqsessions);
126125
for (auto& session : deqSessions) {
127126
session.ProcessMessage(pfrom, strCommand, vRecv, connman);
@@ -201,7 +200,7 @@ void CPrivateSendClientSession::ProcessMessage(CNode* pfrom, const std::string&
201200

202201
LogPrint(BCLog::PRIVATESEND, "DSFINALTX -- txNew %s", txNew.ToString());
203202

204-
//check to see if input is spent already? (and probably not confirmed)
203+
// check to see if input is spent already? (and probably not confirmed)
205204
SignFinalTransaction(txNew, pfrom, connman);
206205

207206
} else if (strCommand == NetMsgType::DSCOMPLETE) {
@@ -294,8 +293,9 @@ std::string CPrivateSendClientSession::GetStatus(bool fWaitForBlock)
294293
nStatusMessageProgress += 10;
295294
std::string strSuffix = "";
296295

297-
if (fWaitForBlock || !masternodeSync.IsBlockchainSynced())
296+
if (fWaitForBlock || !masternodeSync.IsBlockchainSynced()) {
298297
return strAutoDenomResult;
298+
}
299299

300300
switch (nState) {
301301
case POOL_STATE_IDLE:
@@ -432,8 +432,7 @@ bool CPrivateSendClientSession::CheckTimeout()
432432
int nTimeout = (nState == POOL_STATE_SIGNING) ? PRIVATESEND_SIGNING_TIMEOUT : PRIVATESEND_QUEUE_TIMEOUT;
433433
bool fTimeout = GetTime() - nTimeLastSuccessfulStep >= nTimeout + nLagTime;
434434

435-
if (nState == POOL_STATE_IDLE || !fTimeout)
436-
return false;
435+
if (nState == POOL_STATE_IDLE || !fTimeout) return false;
437436

438437
LogPrint(BCLog::PRIVATESEND, "CPrivateSendClientSession::CheckTimeout -- %s timed out (%ds) -- resetting\n",
439438
(nState == POOL_STATE_SIGNING) ? "Signing" : "Session", nTimeout);
@@ -481,11 +480,13 @@ bool CPrivateSendClientSession::SendDenominate(const std::vector<std::pair<CTxDS
481480
}
482481

483482
// lock the funds we're going to use
484-
for (const auto& txin : txMyCollateral.vin)
483+
for (const auto& txin : txMyCollateral.vin) {
485484
vecOutPointLocked.push_back(txin.prevout);
485+
}
486486

487-
for (const auto& pair : vecPSInOutPairsIn)
487+
for (const auto& pair : vecPSInOutPairsIn) {
488488
vecOutPointLocked.push_back(pair.first.prevout);
489+
}
489490

490491
// we should already be connected to a Masternode
491492
if (!nSessionID) {
@@ -630,8 +631,9 @@ bool CPrivateSendClientSession::SignFinalTransaction(const CTransaction& finalTr
630631
}
631632
}
632633

633-
for (const auto& txout : entry.vecTxOut)
634+
for (const auto& txout : entry.vecTxOut) {
634635
nValue2 += txout.nValue;
636+
}
635637

636638
int nTargetOuputsCount = entry.vecTxOut.size();
637639
if (nFoundOutputsCount < nTargetOuputsCount || nValue1 != nValue2) {
@@ -714,11 +716,9 @@ void CPrivateSendClientManager::AddSkippedDenom(const CAmount& nDenomValue)
714716

715717
bool CPrivateSendClientManager::WaitForAnotherBlock()
716718
{
717-
if (!masternodeSync.IsBlockchainSynced())
718-
return true;
719+
if (!masternodeSync.IsBlockchainSynced()) return true;
719720

720-
if (fPrivateSendMultiSession)
721-
return false;
721+
if (fPrivateSendMultiSession) return false;
722722

723723
return nCachedBlockHeight - nCachedLastSuccessBlock < nMinBlocksToWait;
724724
}
@@ -921,8 +921,9 @@ bool CPrivateSendClientSession::DoAutomaticDenominating(CConnman& connman, bool
921921
}
922922

923923
//check if we have the collateral sized inputs
924-
if (!pwalletMain->HasCollateralInputs())
924+
if (!pwalletMain->HasCollateralInputs()) {
925925
return !pwalletMain->HasCollateralInputs(false) && MakeCollateralAmounts(connman);
926+
}
926927

927928
if (nSessionID) {
928929
strAutoDenomResult = _("Mixing in progress...");
@@ -962,14 +963,14 @@ bool CPrivateSendClientSession::DoAutomaticDenominating(CConnman& connman, bool
962963

963964
bool fUseQueue = GetRandInt(100) > 33;
964965
// don't use the queues all of the time for mixing unless we are a liquidity provider
965-
if ((privateSendClient.nLiquidityProvider || fUseQueue) && JoinExistingQueue(nBalanceNeedsAnonymized, connman))
966+
if ((privateSendClient.nLiquidityProvider || fUseQueue) && JoinExistingQueue(nBalanceNeedsAnonymized, connman)) {
966967
return true;
968+
}
967969

968970
// do not initiate queue if we are a liquidity provider to avoid useless inter-mixing
969971
if (privateSendClient.nLiquidityProvider) return false;
970972

971-
if (StartNewQueue(nBalanceNeedsAnonymized, connman))
972-
return true;
973+
if (StartNewQueue(nBalanceNeedsAnonymized, connman)) return true;
973974

974975
strAutoDenomResult = _("No compatible Masternode found.");
975976
return false;
@@ -1013,8 +1014,7 @@ bool CPrivateSendClientManager::DoAutomaticDenominating(CConnman& connman, bool
10131014
deqSessions.emplace_back();
10141015
}
10151016
for (auto& session : deqSessions) {
1016-
if (!CheckAutomaticBackup())
1017-
return false;
1017+
if (!CheckAutomaticBackup()) return false;
10181018

10191019
if (WaitForAnotherBlock()) {
10201020
LogPrintf("CPrivateSendClientManager::DoAutomaticDenominating -- Last successful PrivateSend action was too recent\n");
@@ -1456,8 +1456,9 @@ bool CPrivateSendClientSession::MakeCollateralAmounts(const CompactTallyItem& ta
14561456
LOCK2(cs_main, pwalletMain->cs_wallet);
14571457

14581458
// denominated input is always a single one, so we can check its amount directly and return early
1459-
if (!fTryDenominated && tallyItem.vecOutPoints.size() == 1 && CPrivateSend::IsDenominatedAmount(tallyItem.nAmount))
1459+
if (!fTryDenominated && tallyItem.vecOutPoints.size() == 1 && CPrivateSend::IsDenominatedAmount(tallyItem.nAmount)) {
14601460
return false;
1461+
}
14611462

14621463
CWalletTx wtx;
14631464
CAmount nFeeRet = 0;
@@ -1483,8 +1484,9 @@ bool CPrivateSendClientSession::MakeCollateralAmounts(const CompactTallyItem& ta
14831484
coinControl.fAllowWatchOnly = false;
14841485
// send change to the same address so that we were able create more denoms out of it later
14851486
coinControl.destChange = tallyItem.txdest;
1486-
for (const auto& outpoint : tallyItem.vecOutPoints)
1487+
for (const auto& outpoint : tallyItem.vecOutPoints) {
14871488
coinControl.Select(outpoint);
1489+
}
14881490

14891491
bool fSuccess = pwalletMain->CreateTransaction(vecSend, wtx, reservekeyChange,
14901492
nFeeRet, nChangePosRet, strFail, &coinControl, true, ONLY_NONDENOMINATED);
@@ -1643,8 +1645,9 @@ bool CPrivateSendClientSession::CreateDenominated(CAmount nBalanceToDenominate,
16431645
coinControl.fAllowWatchOnly = false;
16441646
// send change to the same address so that we were able create more denoms out of it later
16451647
coinControl.destChange = tallyItem.txdest;
1646-
for (const auto& outpoint : tallyItem.vecOutPoints)
1648+
for (const auto& outpoint : tallyItem.vecOutPoints) {
16471649
coinControl.Select(outpoint);
1650+
}
16481651

16491652
CWalletTx wtx;
16501653
CAmount nFeeRet = 0;
@@ -1705,8 +1708,7 @@ void CPrivateSendClientManager::DoMaintenance(CConnman& connman)
17051708
if (fLiteMode) return; // disable all Dash specific functionality
17061709
if (fMasternodeMode) return; // no client-side mixing on masternodes
17071710

1708-
if (!masternodeSync.IsBlockchainSynced() || ShutdownRequested())
1709-
return;
1711+
if (!masternodeSync.IsBlockchainSynced() || ShutdownRequested()) return;
17101712

17111713
static unsigned int nTick = 0;
17121714
static unsigned int nDoAutoNextRun = nTick + PRIVATESEND_AUTO_TIMEOUT_MIN;

src/privatesend/privatesend-server.cpp

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void CPrivateSendServer::ProcessMessage(CNode* pfrom, const std::string& strComm
5555
return;
5656
}
5757

58-
if (vecSessionCollaterals.size() == 0) {
58+
if (vecSessionCollaterals.empty()) {
5959
{
6060
TRY_LOCK(cs_vecqueue, lockRecv);
6161
if (!lockRecv) return;
@@ -185,14 +185,14 @@ void CPrivateSendServer::ProcessMessage(CNode* pfrom, const std::string& strComm
185185
return;
186186
}
187187

188-
//do we have the same denominations as the current session?
188+
// do we have the same denominations as the current session?
189189
if (!IsOutputsCompatibleWithSessionDenom(entry.vecTxOut)) {
190190
LogPrintf("DSVIN -- not compatible with existing transactions!\n");
191191
PushStatus(pfrom, STATUS_REJECTED, ERR_EXISTING_TX, connman);
192192
return;
193193
}
194194

195-
//check it like a transaction
195+
// check it like a transaction
196196
{
197197
CAmount nValueIn = 0;
198198
CAmount nValueOut = 0;
@@ -343,11 +343,12 @@ void CPrivateSendServer::CreateFinalTransaction(CConnman& connman)
343343

344344
// make our new transaction
345345
for (int i = 0; i < GetEntriesCount(); i++) {
346-
for (const auto& txout : vecEntries[i].vecTxOut)
346+
for (const auto& txout : vecEntries[i].vecTxOut) {
347347
txNew.vout.push_back(txout);
348-
349-
for (const auto& txdsin : vecEntries[i].vecTxDSIn)
348+
}
349+
for (const auto& txdsin : vecEntries[i].vecTxDSIn) {
350350
txNew.vin.push_back(txdsin);
351+
}
351352
}
352353

353354
sort(txNew.vin.begin(), txNew.vin.end(), CompareInputBIP69());
@@ -375,7 +376,7 @@ void CPrivateSendServer::CommitFinalTransaction(CConnman& connman)
375376
TRY_LOCK(cs_main, lockMain);
376377
CValidationState validationState;
377378
mempool.PrioritiseTransaction(hashTx, 0.1 * COIN);
378-
if (!lockMain || !AcceptToMemoryPool(mempool, validationState, finalTransaction, false, NULL, false, maxTxFee, true)) {
379+
if (!lockMain || !AcceptToMemoryPool(mempool, validationState, finalTransaction, false, nullptr, false, maxTxFee, true)) {
379380
LogPrintf("CPrivateSendServer::CommitFinalTransaction -- AcceptToMemoryPool() error: Transaction not valid\n");
380381
SetNull();
381382
// not much we can do in this case, just notify clients
@@ -433,9 +434,12 @@ void CPrivateSendServer::ChargeFees(CConnman& connman)
433434
if (nState == POOL_STATE_ACCEPTING_ENTRIES) {
434435
for (const auto& txCollateral : vecSessionCollaterals) {
435436
bool fFound = false;
436-
for (const auto& entry : vecEntries)
437-
if (*entry.txCollateral == *txCollateral)
437+
for (const auto& entry : vecEntries) {
438+
if (*entry.txCollateral == *txCollateral) {
438439
fFound = true;
440+
break;
441+
}
442+
}
439443

440444
// This queue entry didn't send us the promised transaction
441445
if (!fFound) {
@@ -584,9 +588,9 @@ bool CPrivateSendServer::IsInputScriptSigValid(const CTxIn& txin)
584588
CScript sigPubKey = CScript();
585589

586590
for (const auto& entry : vecEntries) {
587-
for (const auto& txout : entry.vecTxOut)
591+
for (const auto& txout : entry.vecTxOut) {
588592
txNew.vout.push_back(txout);
589-
593+
}
590594
for (const auto& txdsin : entry.vecTxDSIn) {
591595
txNew.vin.push_back(txdsin);
592596

@@ -702,9 +706,11 @@ bool CPrivateSendServer::AddScriptSig(const CTxIn& txinNew)
702706
// Check to make sure everything is signed
703707
bool CPrivateSendServer::IsSignaturesComplete()
704708
{
705-
for (const auto& entry : vecEntries)
706-
for (const auto& txdsin : entry.vecTxDSIn)
709+
for (const auto& entry : vecEntries) {
710+
for (const auto& txdsin : entry.vecTxDSIn) {
707711
if (!txdsin.fHasSig) return false;
712+
}
713+
}
708714

709715
return true;
710716
}
@@ -827,7 +833,7 @@ void CPrivateSendServer::RelayFinalTransaction(const CTransaction& txFinal, CCon
827833
__func__, nSessionID, nSessionDenom, CPrivateSend::GetDenominationsToString(nSessionDenom));
828834

829835
// final mixing tx with empty signatures should be relayed to mixing participants only
830-
for (const auto entry : vecEntries) {
836+
for (const auto& entry : vecEntries) {
831837
bool fOk = connman.ForNode(entry.addr, [&txFinal, &connman, this](CNode* pnode) {
832838
CNetMsgMaker msgMaker(pnode->GetSendVersion());
833839
connman.PushMessage(pnode, msgMaker.Make(NetMsgType::DSFINALTX, nSessionID, txFinal));
@@ -852,7 +858,7 @@ void CPrivateSendServer::RelayStatus(PoolStatusUpdate nStatusUpdate, CConnman& c
852858
{
853859
unsigned int nDisconnected{};
854860
// status updates should be relayed to mixing participants only
855-
for (const auto entry : vecEntries) {
861+
for (const auto& entry : vecEntries) {
856862
// make sure everyone is still connected
857863
bool fOk = connman.ForNode(entry.addr, [&nStatusUpdate, &nMessageID, &connman, this](CNode* pnode) {
858864
PushStatus(pnode, nStatusUpdate, nMessageID, connman);
@@ -923,8 +929,7 @@ void CPrivateSendServer::DoMaintenance(CConnman& connman)
923929
if (fLiteMode) return; // disable all Dash specific functionality
924930
if (!fMasternodeMode) return; // only run on masternodes
925931

926-
if (!masternodeSync.IsBlockchainSynced() || ShutdownRequested())
927-
return;
932+
if (!masternodeSync.IsBlockchainSynced() || ShutdownRequested()) return;
928933

929934
privateSendServer.CheckTimeout(connman);
930935
privateSendServer.CheckForCompleteQueue(connman);

src/privatesend/privatesend-util.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void CKeyHolderStorage::KeepAll()
4646
std::swap(storage, tmp);
4747
}
4848

49-
if (tmp.size() > 0) {
49+
if (!tmp.empty()) {
5050
for (auto& key : tmp) {
5151
key->KeepKey();
5252
}
@@ -63,7 +63,7 @@ void CKeyHolderStorage::ReturnAll()
6363
std::swap(storage, tmp);
6464
}
6565

66-
if (tmp.size() > 0) {
66+
if (!tmp.empty()) {
6767
for (auto& key : tmp) {
6868
key->ReturnKey();
6969
}

src/privatesend/privatesend.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ bool CPrivateSendQueue::Relay(CConnman& connman)
7474
{
7575
connman.ForEachNode([&connman, this](CNode* pnode) {
7676
CNetMsgMaker msgMaker(pnode->GetSendVersion());
77-
if (pnode->nVersion >= MIN_PRIVATESEND_PEER_PROTO_VERSION && pnode->fSendDSQueue)
77+
if (pnode->nVersion >= MIN_PRIVATESEND_PEER_PROTO_VERSION && pnode->fSendDSQueue) {
7878
connman.PushMessage(pnode, msgMaker.Make(NetMsgType::DSQUEUE, (*this)));
79+
}
7980
});
8081
return true;
8182
}
@@ -89,7 +90,6 @@ bool CPrivateSendBroadcastTx::Sign()
8990
{
9091
if (!fMasternodeMode) return false;
9192

92-
9393
uint256 hash = GetSignatureHash();
9494

9595
CBLSSignature sig = activeMasternodeInfo.blsKeyOperator->Sign(hash);
@@ -103,7 +103,6 @@ bool CPrivateSendBroadcastTx::Sign()
103103

104104
bool CPrivateSendBroadcastTx::CheckSignature(const CBLSPublicKey& blsPubKey) const
105105
{
106-
107106
uint256 hash = GetSignatureHash();
108107

109108
CBLSSignature sig;
@@ -170,13 +169,14 @@ void CPrivateSendBaseManager::CheckQueue()
170169
if (!lockDS) return; // it's ok to fail here, we run this quite frequently
171170

172171
// check mixing queue objects for timeouts
173-
std::vector<CPrivateSendQueue>::iterator it = vecPrivateSendQueue.begin();
172+
auto it = vecPrivateSendQueue.begin();
174173
while (it != vecPrivateSendQueue.end()) {
175174
if ((*it).IsExpired()) {
176175
LogPrint(BCLog::PRIVATESEND, "CPrivateSendBaseManager::%s -- Removing expired queue (%s)\n", __func__, (*it).ToString());
177176
it = vecPrivateSendQueue.erase(it);
178-
} else
177+
} else {
179178
++it;
179+
}
180180
}
181181
}
182182

@@ -350,8 +350,9 @@ int CPrivateSend::GetDenominations(const std::vector<CTxOut>& vecTxOut, bool fSi
350350
std::vector<std::pair<CAmount, int> > vecDenomUsed;
351351

352352
// make a list of denominations, with zero uses
353-
for (const auto& nDenomValue : vecStandardDenominations)
353+
for (const auto& nDenomValue : vecStandardDenominations) {
354354
vecDenomUsed.push_back(std::make_pair(nDenomValue, 0));
355+
}
355356

356357
// look for denominations and update uses to 1
357358
for (const auto& txout : vecTxOut) {
@@ -415,9 +416,9 @@ int CPrivateSend::GetDenominationsByAmounts(const std::vector<CAmount>& vecAmoun
415416

416417
bool CPrivateSend::IsDenominatedAmount(CAmount nInputAmount)
417418
{
418-
for (const auto& nDenomValue : vecStandardDenominations)
419-
if (nInputAmount == nDenomValue)
420-
return true;
419+
for (const auto& nDenomValue : vecStandardDenominations) {
420+
if (nInputAmount == nDenomValue) return true;
421+
}
421422
return false;
422423
}
423424

@@ -450,8 +451,6 @@ std::string CPrivateSend::GetMessageByID(PoolMessage nMessageID)
450451
return _("Incompatible mode.");
451452
case ERR_NON_STANDARD_PUBKEY:
452453
return _("Non-standard public key detected.");
453-
case ERR_NOT_A_MN:
454-
return _("This is not a Masternode."); // not used
455454
case ERR_QUEUE_FULL:
456455
return _("Masternode queue is full.");
457456
case ERR_RECENT:
@@ -491,7 +490,7 @@ CPrivateSendBroadcastTx CPrivateSend::GetDSTX(const uint256& hash)
491490
void CPrivateSend::CheckDSTXes(int nHeight)
492491
{
493492
LOCK(cs_mapdstx);
494-
std::map<uint256, CPrivateSendBroadcastTx>::iterator it = mapDSTX.begin();
493+
auto it = mapDSTX.begin();
495494
while (it != mapDSTX.end()) {
496495
if (it->second.IsExpired(nHeight)) {
497496
mapDSTX.erase(it++);

0 commit comments

Comments
 (0)