From 89f744d0640f85604f2e36c6eeee6db9cd80dbf9 Mon Sep 17 00:00:00 2001 From: Nathan Marley Date: Thu, 11 Oct 2018 07:33:48 -0700 Subject: [PATCH] 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 GetString methods on MNP class. * Revert "remove deprecated wire message types" This reverts commit 9de88a3fda999555c00cb829d60e322b2fd3800d. * Revert "remove if/else logic from Qt" This reverts commit c0f43c9b85274b13a3ac9625775817bc5b53bb9d. --- src/governance.cpp | 20 ++++++++------------ src/masternode.cpp | 28 ++++++++-------------------- src/masternodeman.h | 2 +- src/privatesend-client.cpp | 2 +- src/qt/masternodelist.cpp | 5 +---- 5 files changed, 19 insertions(+), 38 deletions(-) diff --git a/src/governance.cpp b/src/governance.cpp index 9b30e9cec..e0586362b 100644 --- a/src/governance.cpp +++ b/src/governance.cpp @@ -270,9 +270,8 @@ void CGovernanceManager::CheckOrphanVotes(CGovernanceObject& govobj, CGovernance ScopedLockBool guard(cs, fRateChecksEnabled, false); int64_t nNow = GetAdjustedTime(); - for (size_t i = 0; i < vecVotePairs.size(); ++i) { + for (auto& pairVote : vecVotePairs) { bool fRemove = false; - vote_time_pair_t& pairVote = vecVotePairs[i]; CGovernanceVote& vote = pairVote.first; CGovernanceException exception; if (pairVote.second < nNow) { @@ -368,8 +367,8 @@ void CGovernanceManager::UpdateCachesAndClean() LOCK2(cs_main, cs); - for (size_t i = 0; i < vecDirtyHashes.size(); ++i) { - object_m_it it = mapObjects.find(vecDirtyHashes[i]); + for (const uint256& nHash : vecDirtyHashes) { + object_m_it it = mapObjects.find(nHash); if (it == mapObjects.end()) { continue; } @@ -1018,8 +1017,8 @@ void CGovernanceManager::RequestGovernanceObject(CNode* pfrom, const uint256& nH filter = CBloomFilter(Params().GetConsensus().nGovernanceFilterElements, GOVERNANCE_FILTER_FP_RATE, GetRandInt(999999), BLOOM_UPDATE_ALL); std::vector vecVotes = pObj->GetVoteFile().GetVotes(); nVoteCount = vecVotes.size(); - for (size_t i = 0; i < vecVotes.size(); ++i) { - filter.insert(vecVotes[i].GetHash()); + for (const auto& vote : vecVotes) { + filter.insert(vote.GetHash()); } } } @@ -1296,8 +1295,7 @@ void CGovernanceManager::RequestOrphanObjects(CConnman& connman) std::vector vecHashes; LOCK(cs); cmmapOrphanVotes.GetKeys(vecHashes); - for (size_t i = 0; i < vecHashes.size(); ++i) { - const uint256& nHash = vecHashes[i]; + for (const uint256& nHash : vecHashes) { if (mapObjects.find(nHash) == mapObjects.end()) { vecHashesFiltered.push_back(nHash); } @@ -1305,10 +1303,8 @@ void CGovernanceManager::RequestOrphanObjects(CConnman& connman) } LogPrint("gobject", "CGovernanceObject::RequestOrphanObjects -- number objects = %d\n", vecHashesFiltered.size()); - for (size_t i = 0; i < vecHashesFiltered.size(); ++i) { - const uint256& nHash = vecHashesFiltered[i]; - for (size_t j = 0; j < vNodesCopy.size(); ++j) { - CNode* pnode = vNodesCopy[j]; + for (const uint256& nHash : vecHashesFiltered) { + for (CNode* pnode : vNodesCopy) { if (pnode->fMasternode) { continue; } diff --git a/src/masternode.cpp b/src/masternode.cpp index f8215e681..6dad0e224 100644 --- a/src/masternode.cpp +++ b/src/masternode.cpp @@ -993,20 +993,16 @@ std::string CMasternodePing::GetDaemonString() const void CMasternode::AddGovernanceVote(uint256 nGovernanceObjectHash) { - if(mapGovernanceObjectsVotedOn.count(nGovernanceObjectHash)) { - mapGovernanceObjectsVotedOn[nGovernanceObjectHash]++; - } else { - mapGovernanceObjectsVotedOn.insert(std::make_pair(nGovernanceObjectHash, 1)); - } + // Insert a zero value, or not. Then increment the value regardless. This + // ensures the value is in the map. + const auto& pair = mapGovernanceObjectsVotedOn.emplace(nGovernanceObjectHash, 0); + pair.first->second++; } void CMasternode::RemoveGovernanceObject(uint256 nGovernanceObjectHash) { - std::map::iterator it = mapGovernanceObjectsVotedOn.find(nGovernanceObjectHash); - if(it == mapGovernanceObjectsVotedOn.end()) { - return; - } - mapGovernanceObjectsVotedOn.erase(it); + // Whether or not the govobj hash exists in the map first is irrelevant. + mapGovernanceObjectsVotedOn.erase(nGovernanceObjectHash); } /** @@ -1017,15 +1013,7 @@ void CMasternode::RemoveGovernanceObject(uint256 nGovernanceObjectHash) */ void CMasternode::FlagGovernanceItemsAsDirty() { - std::vector vecDirty; - { - std::map::iterator it = mapGovernanceObjectsVotedOn.begin(); - while(it != mapGovernanceObjectsVotedOn.end()) { - vecDirty.push_back(it->first); - ++it; - } - } - for(size_t i = 0; i < vecDirty.size(); ++i) { - mnodeman.AddDirtyGovernanceObjectHash(vecDirty[i]); + for (auto& govObjHashPair : mapGovernanceObjectsVotedOn) { + mnodeman.AddDirtyGovernanceObjectHash(govObjHashPair.first); } } diff --git a/src/masternodeman.h b/src/masternodeman.h index 41f3383c0..750ba9097 100644 --- a/src/masternodeman.h +++ b/src/masternodeman.h @@ -226,7 +226,7 @@ public: LOCK(cs); std::vector vecTmp = vecDirtyGovernanceObjectHashes; vecDirtyGovernanceObjectHashes.clear(); - return vecTmp;; + return vecTmp; } bool IsSentinelPingActive(); diff --git a/src/privatesend-client.cpp b/src/privatesend-client.cpp index 48ef4dece..b3668b302 100644 --- a/src/privatesend-client.cpp +++ b/src/privatesend-client.cpp @@ -305,7 +305,7 @@ std::string CPrivateSendClientSession::GetStatus(bool fWaitForBlock) if( nStatusMessageProgress % 70 <= 30) strSuffix = "."; else if(nStatusMessageProgress % 70 <= 50) strSuffix = ".."; else if(nStatusMessageProgress % 70 <= 70) strSuffix = "..."; - return strprintf(_("Submitted to masternode, waiting in queue %s"), strSuffix);; + return strprintf(_("Submitted to masternode, waiting in queue %s"), strSuffix); case POOL_STATE_ACCEPTING_ENTRIES: if(nEntriesCount == 0) { nStatusMessageProgress = 0; diff --git a/src/qt/masternodelist.cpp b/src/qt/masternodelist.cpp index 266a14335..ce8e19784 100644 --- a/src/qt/masternodelist.cpp +++ b/src/qt/masternodelist.cpp @@ -474,10 +474,7 @@ void MasternodeList::ShowQRCode(std::string strAlias) { bool fFound = false; for (const auto& mne : masternodeConfig.getEntries()) { - if (strAlias != mne.getAlias()) { - continue; - } - else { + if (strAlias == mne.getAlias()) { strMNPrivKey = mne.getPrivKey(); strCollateral = mne.getTxHash() + "-" + mne.getOutputIndex(); strIP = mne.getIp();