Skip to content

Commit 89f744d

Browse files
nmarleyUdjinM6
authored andcommitted
pack of small cleanup fixes / optimizations (#2334)
* remove vector, extra loop in cleanup function This commit removes 2 loops and a vector which I don't believe are necessary in CMasternode::FlagGovernanceItemsAsDirty. I could be missing something, but can't think of any good reason why this was implemented this way. * use range operator to range over vectors * remove deprecated wire message types * mn: simplify govobj map mgmt a bit * remove extra semicolons * invert if/else condition and drop else * remove if/else logic from Qt This is the entire purpose of the Get<X>String methods on MNP class. * Revert "remove deprecated wire message types" This reverts commit 9de88a3. * Revert "remove if/else logic from Qt" This reverts commit c0f43c9.
1 parent 9603c52 commit 89f744d

File tree

5 files changed

+19
-38
lines changed

5 files changed

+19
-38
lines changed

src/governance.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,8 @@ void CGovernanceManager::CheckOrphanVotes(CGovernanceObject& govobj, CGovernance
270270
ScopedLockBool guard(cs, fRateChecksEnabled, false);
271271

272272
int64_t nNow = GetAdjustedTime();
273-
for (size_t i = 0; i < vecVotePairs.size(); ++i) {
273+
for (auto& pairVote : vecVotePairs) {
274274
bool fRemove = false;
275-
vote_time_pair_t& pairVote = vecVotePairs[i];
276275
CGovernanceVote& vote = pairVote.first;
277276
CGovernanceException exception;
278277
if (pairVote.second < nNow) {
@@ -368,8 +367,8 @@ void CGovernanceManager::UpdateCachesAndClean()
368367

369368
LOCK2(cs_main, cs);
370369

371-
for (size_t i = 0; i < vecDirtyHashes.size(); ++i) {
372-
object_m_it it = mapObjects.find(vecDirtyHashes[i]);
370+
for (const uint256& nHash : vecDirtyHashes) {
371+
object_m_it it = mapObjects.find(nHash);
373372
if (it == mapObjects.end()) {
374373
continue;
375374
}
@@ -1018,8 +1017,8 @@ void CGovernanceManager::RequestGovernanceObject(CNode* pfrom, const uint256& nH
10181017
filter = CBloomFilter(Params().GetConsensus().nGovernanceFilterElements, GOVERNANCE_FILTER_FP_RATE, GetRandInt(999999), BLOOM_UPDATE_ALL);
10191018
std::vector<CGovernanceVote> vecVotes = pObj->GetVoteFile().GetVotes();
10201019
nVoteCount = vecVotes.size();
1021-
for (size_t i = 0; i < vecVotes.size(); ++i) {
1022-
filter.insert(vecVotes[i].GetHash());
1020+
for (const auto& vote : vecVotes) {
1021+
filter.insert(vote.GetHash());
10231022
}
10241023
}
10251024
}
@@ -1296,19 +1295,16 @@ void CGovernanceManager::RequestOrphanObjects(CConnman& connman)
12961295
std::vector<uint256> vecHashes;
12971296
LOCK(cs);
12981297
cmmapOrphanVotes.GetKeys(vecHashes);
1299-
for (size_t i = 0; i < vecHashes.size(); ++i) {
1300-
const uint256& nHash = vecHashes[i];
1298+
for (const uint256& nHash : vecHashes) {
13011299
if (mapObjects.find(nHash) == mapObjects.end()) {
13021300
vecHashesFiltered.push_back(nHash);
13031301
}
13041302
}
13051303
}
13061304

13071305
LogPrint("gobject", "CGovernanceObject::RequestOrphanObjects -- number objects = %d\n", vecHashesFiltered.size());
1308-
for (size_t i = 0; i < vecHashesFiltered.size(); ++i) {
1309-
const uint256& nHash = vecHashesFiltered[i];
1310-
for (size_t j = 0; j < vNodesCopy.size(); ++j) {
1311-
CNode* pnode = vNodesCopy[j];
1306+
for (const uint256& nHash : vecHashesFiltered) {
1307+
for (CNode* pnode : vNodesCopy) {
13121308
if (pnode->fMasternode) {
13131309
continue;
13141310
}

src/masternode.cpp

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -993,20 +993,16 @@ std::string CMasternodePing::GetDaemonString() const
993993

994994
void CMasternode::AddGovernanceVote(uint256 nGovernanceObjectHash)
995995
{
996-
if(mapGovernanceObjectsVotedOn.count(nGovernanceObjectHash)) {
997-
mapGovernanceObjectsVotedOn[nGovernanceObjectHash]++;
998-
} else {
999-
mapGovernanceObjectsVotedOn.insert(std::make_pair(nGovernanceObjectHash, 1));
1000-
}
996+
// Insert a zero value, or not. Then increment the value regardless. This
997+
// ensures the value is in the map.
998+
const auto& pair = mapGovernanceObjectsVotedOn.emplace(nGovernanceObjectHash, 0);
999+
pair.first->second++;
10011000
}
10021001

10031002
void CMasternode::RemoveGovernanceObject(uint256 nGovernanceObjectHash)
10041003
{
1005-
std::map<uint256, int>::iterator it = mapGovernanceObjectsVotedOn.find(nGovernanceObjectHash);
1006-
if(it == mapGovernanceObjectsVotedOn.end()) {
1007-
return;
1008-
}
1009-
mapGovernanceObjectsVotedOn.erase(it);
1004+
// Whether or not the govobj hash exists in the map first is irrelevant.
1005+
mapGovernanceObjectsVotedOn.erase(nGovernanceObjectHash);
10101006
}
10111007

10121008
/**
@@ -1017,15 +1013,7 @@ void CMasternode::RemoveGovernanceObject(uint256 nGovernanceObjectHash)
10171013
*/
10181014
void CMasternode::FlagGovernanceItemsAsDirty()
10191015
{
1020-
std::vector<uint256> vecDirty;
1021-
{
1022-
std::map<uint256, int>::iterator it = mapGovernanceObjectsVotedOn.begin();
1023-
while(it != mapGovernanceObjectsVotedOn.end()) {
1024-
vecDirty.push_back(it->first);
1025-
++it;
1026-
}
1027-
}
1028-
for(size_t i = 0; i < vecDirty.size(); ++i) {
1029-
mnodeman.AddDirtyGovernanceObjectHash(vecDirty[i]);
1016+
for (auto& govObjHashPair : mapGovernanceObjectsVotedOn) {
1017+
mnodeman.AddDirtyGovernanceObjectHash(govObjHashPair.first);
10301018
}
10311019
}

src/masternodeman.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ class CMasternodeMan
226226
LOCK(cs);
227227
std::vector<uint256> vecTmp = vecDirtyGovernanceObjectHashes;
228228
vecDirtyGovernanceObjectHashes.clear();
229-
return vecTmp;;
229+
return vecTmp;
230230
}
231231

232232
bool IsSentinelPingActive();

src/privatesend-client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ std::string CPrivateSendClientSession::GetStatus(bool fWaitForBlock)
305305
if( nStatusMessageProgress % 70 <= 30) strSuffix = ".";
306306
else if(nStatusMessageProgress % 70 <= 50) strSuffix = "..";
307307
else if(nStatusMessageProgress % 70 <= 70) strSuffix = "...";
308-
return strprintf(_("Submitted to masternode, waiting in queue %s"), strSuffix);;
308+
return strprintf(_("Submitted to masternode, waiting in queue %s"), strSuffix);
309309
case POOL_STATE_ACCEPTING_ENTRIES:
310310
if(nEntriesCount == 0) {
311311
nStatusMessageProgress = 0;

src/qt/masternodelist.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -474,10 +474,7 @@ void MasternodeList::ShowQRCode(std::string strAlias) {
474474
bool fFound = false;
475475

476476
for (const auto& mne : masternodeConfig.getEntries()) {
477-
if (strAlias != mne.getAlias()) {
478-
continue;
479-
}
480-
else {
477+
if (strAlias == mne.getAlias()) {
481478
strMNPrivKey = mne.getPrivKey();
482479
strCollateral = mne.getTxHash() + "-" + mne.getOutputIndex();
483480
strIP = mne.getIp();

0 commit comments

Comments
 (0)