From 6b74652b01f0aa1e7fb65b5dd067d020fc75ccfe Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Wed, 5 Aug 2015 03:54:02 +0300 Subject: [PATCH 1/2] track hashes and update "last..." only twice per hash --- src/activemasternode.cpp | 1 + src/main.cpp | 12 +++++------ src/masternode-budget.cpp | 16 +++++++-------- src/masternode-payments.cpp | 5 +++-- src/masternode-sync.cpp | 41 ++++++++++++++++++++++++++++--------- src/masternode-sync.h | 10 ++++++--- src/masternode.cpp | 2 +- src/masternodeman.cpp | 5 +++-- 8 files changed, 60 insertions(+), 32 deletions(-) diff --git a/src/activemasternode.cpp b/src/activemasternode.cpp index 5c350a3e2..631294952 100644 --- a/src/activemasternode.cpp +++ b/src/activemasternode.cpp @@ -250,6 +250,7 @@ bool CActiveMasternode::Register(CTxIn vin, CService service, CKey keyCollateral return false; } mnodeman.mapSeenMasternodeBroadcast.insert(make_pair(mnb.GetHash(), mnb)); + masternodeSync.AddedMasternodeList(mnb.GetHash()); CMasternode* pmn = mnodeman.Find(vin); if(pmn == NULL) diff --git a/src/main.cpp b/src/main.cpp index adeabc4e4..82459d7c5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3996,37 +3996,37 @@ bool static AlreadyHave(const CInv& inv) return mapSporks.count(inv.hash); case MSG_MASTERNODE_WINNER: if(masternodePayments.mapMasternodePayeeVotes.count(inv.hash)) { - masternodeSync.AddedMasternodeWinner(); + masternodeSync.AddedMasternodeWinner(inv.hash); return true; } return false; case MSG_BUDGET_VOTE: if(budget.mapSeenMasternodeBudgetVotes.count(inv.hash)) { - masternodeSync.AddedBudgetItem(); + masternodeSync.AddedBudgetItem(inv.hash); return true; } return false; case MSG_BUDGET_PROPOSAL: if(budget.mapSeenMasternodeBudgetProposals.count(inv.hash)) { - masternodeSync.AddedBudgetItem(); + masternodeSync.AddedBudgetItem(inv.hash); return true; } return false; case MSG_BUDGET_FINALIZED_VOTE: if(budget.mapSeenFinalizedBudgetVotes.count(inv.hash)) { - masternodeSync.AddedBudgetItem(); + masternodeSync.AddedBudgetItem(inv.hash); return true; } return false; case MSG_BUDGET_FINALIZED: if(budget.mapSeenFinalizedBudgets.count(inv.hash)) { - masternodeSync.AddedBudgetItem(); + masternodeSync.AddedBudgetItem(inv.hash); return true; } return false; case MSG_MASTERNODE_ANNOUNCE: if(mnodeman.mapSeenMasternodeBroadcast.count(inv.hash)) { - masternodeSync.AddedMasternodeList(); + masternodeSync.AddedMasternodeList(inv.hash); return true; } return false; diff --git a/src/masternode-budget.cpp b/src/masternode-budget.cpp index 1b4f96ef8..a6370a364 100644 --- a/src/masternode-budget.cpp +++ b/src/masternode-budget.cpp @@ -830,7 +830,7 @@ void CBudgetManager::ProcessMessage(CNode* pfrom, std::string& strCommand, CData vRecv >> budgetProposalBroadcast; if(mapSeenMasternodeBudgetProposals.count(budgetProposalBroadcast.GetHash())){ - masternodeSync.AddedBudgetItem(); + masternodeSync.AddedBudgetItem(budgetProposalBroadcast.GetHash()); return; } @@ -849,7 +849,7 @@ void CBudgetManager::ProcessMessage(CNode* pfrom, std::string& strCommand, CData CBudgetProposal budgetProposal(budgetProposalBroadcast); if(AddProposal(budgetProposal)) {budgetProposalBroadcast.Relay();} - masternodeSync.AddedBudgetItem(); + masternodeSync.AddedBudgetItem(budgetProposalBroadcast.GetHash()); LogPrintf("mprop - new budget - %s\n", budgetProposalBroadcast.GetHash().ToString()); @@ -863,7 +863,7 @@ void CBudgetManager::ProcessMessage(CNode* pfrom, std::string& strCommand, CData vote.fValid = true; if(mapSeenMasternodeBudgetVotes.count(vote.GetHash())){ - masternodeSync.AddedBudgetItem(); + masternodeSync.AddedBudgetItem(vote.GetHash()); return; } @@ -884,7 +884,7 @@ void CBudgetManager::ProcessMessage(CNode* pfrom, std::string& strCommand, CData std::string strError = ""; if(UpdateProposal(vote, pfrom, strError)) { vote.Relay(); - masternodeSync.AddedBudgetItem(); + masternodeSync.AddedBudgetItem(vote.GetHash()); } LogPrintf("mvote - new budget vote - %s\n", vote.GetHash().ToString()); @@ -895,7 +895,7 @@ void CBudgetManager::ProcessMessage(CNode* pfrom, std::string& strCommand, CData vRecv >> finalizedBudgetBroadcast; if(mapSeenFinalizedBudgets.count(finalizedBudgetBroadcast.GetHash())){ - masternodeSync.AddedBudgetItem(); + masternodeSync.AddedBudgetItem(finalizedBudgetBroadcast.GetHash()); return; } @@ -916,7 +916,7 @@ void CBudgetManager::ProcessMessage(CNode* pfrom, std::string& strCommand, CData CFinalizedBudget finalizedBudget(finalizedBudgetBroadcast); if(AddFinalizedBudget(finalizedBudget)) {finalizedBudgetBroadcast.Relay();} - masternodeSync.AddedBudgetItem(); + masternodeSync.AddedBudgetItem(finalizedBudgetBroadcast.GetHash()); //we might have active votes for this budget that are now valid CheckOrphanVotes(); @@ -928,7 +928,7 @@ void CBudgetManager::ProcessMessage(CNode* pfrom, std::string& strCommand, CData vote.fValid = true; if(mapSeenFinalizedBudgetVotes.count(vote.GetHash())){ - masternodeSync.AddedBudgetItem(); + masternodeSync.AddedBudgetItem(vote.GetHash()); return; } @@ -948,7 +948,7 @@ void CBudgetManager::ProcessMessage(CNode* pfrom, std::string& strCommand, CData std::string strError = ""; if(UpdateFinalizedBudget(vote, pfrom, strError)) { vote.Relay(); - masternodeSync.AddedBudgetItem(); + masternodeSync.AddedBudgetItem(vote.GetHash()); } LogPrint("mnbudget", "fbs - new finalized budget vote - %s\n", vote.GetHash().ToString()); diff --git a/src/masternode-payments.cpp b/src/masternode-payments.cpp index 18a5f71f7..89c614d7d 100644 --- a/src/masternode-payments.cpp +++ b/src/masternode-payments.cpp @@ -365,7 +365,7 @@ void CMasternodePayments::ProcessMessageMasternodePayments(CNode* pfrom, std::st if(masternodePayments.mapMasternodePayeeVotes.count(winner.GetHash())){ LogPrint("mnpayments", "mnw - Already seen - %s bestHeight %d\n", winner.GetHash().ToString().c_str(), chainActive.Tip()->nHeight); - masternodeSync.AddedMasternodeWinner(); + masternodeSync.AddedMasternodeWinner(winner.GetHash()); return; } @@ -400,7 +400,7 @@ void CMasternodePayments::ProcessMessageMasternodePayments(CNode* pfrom, std::st if(masternodePayments.AddWinningMasternode(winner)){ winner.Relay(); - masternodeSync.AddedMasternodeWinner(); + masternodeSync.AddedMasternodeWinner(winner.GetHash()); } } } @@ -598,6 +598,7 @@ void CMasternodePayments::CleanPaymentList() if(chainActive.Tip()->nHeight - winner.nBlockHeight > nLimit){ LogPrint("mnpayments", "CMasternodePayments::CleanPaymentList - Removing old Masternode payment - block %d\n", winner.nBlockHeight); + masternodeSync.mapSeenSyncMNW.erase((*it).first); mapMasternodePayeeVotes.erase(it++); } else { ++it; diff --git a/src/masternode-sync.cpp b/src/masternode-sync.cpp index 5d91cb70e..83e01a157 100644 --- a/src/masternode-sync.cpp +++ b/src/masternode-sync.cpp @@ -75,19 +75,44 @@ void CMasternodeSync::Reset() RequestedMasternodeAttempt = 0; } -void CMasternodeSync::AddedMasternodeList() +void CMasternodeSync::AddedMasternodeList(uint256 hash) { - lastMasternodeList = GetTime(); + if(mnodeman.mapSeenMasternodeBroadcast.count(hash)) { + if(mapSeenSyncMNB[hash] < 2) { + lastMasternodeList = GetTime(); + mapSeenSyncMNB[hash]++; + } + } else { + lastMasternodeList = GetTime(); + mapSeenSyncMNB.insert(make_pair(hash, 1)); + } } -void CMasternodeSync::AddedMasternodeWinner() +void CMasternodeSync::AddedMasternodeWinner(uint256 hash) { - lastMasternodeWinner = GetTime(); + if(masternodePayments.mapMasternodePayeeVotes.count(hash)) { + if(mapSeenSyncMNW[hash] < 2) { + lastMasternodeWinner = GetTime(); + mapSeenSyncMNW[hash]++; + } + } else { + lastMasternodeWinner = GetTime(); + mapSeenSyncMNW.insert(make_pair(hash, 1)); + } } -void CMasternodeSync::AddedBudgetItem() +void CMasternodeSync::AddedBudgetItem(uint256 hash) { - lastBudgetItem = GetTime(); + if(budget.mapSeenMasternodeBudgetProposals.count(hash) || budget.mapSeenMasternodeBudgetVotes.count(hash) || + budget.mapSeenFinalizedBudgets.count(hash) || budget.mapSeenFinalizedBudgetVotes.count(hash)) { + if(mapSeenSyncBudget[hash] < 2) { + lastBudgetItem = GetTime(); + mapSeenSyncBudget[hash]++; + } + } else { + lastBudgetItem = GetTime(); + mapSeenSyncBudget.insert(make_pair(hash, 1)); + } } bool CMasternodeSync::IsBudgetPropEmpty() @@ -141,25 +166,21 @@ void CMasternodeSync::ProcessMessage(CNode* pfrom, std::string& strCommand, CDat { case(MASTERNODE_SYNC_LIST): if(nItemID != RequestedMasternodeAssets) return; - if(nCount == 0) lastMasternodeList = GetTime(); sumMasternodeList += nCount; countMasternodeList++; break; case(MASTERNODE_SYNC_MNW): if(nItemID != RequestedMasternodeAssets) return; - if(nCount == 0) lastMasternodeWinner = GetTime(); sumMasternodeWinner += nCount; countMasternodeWinner++; break; case(MASTERNODE_SYNC_BUDGET_PROP): if(RequestedMasternodeAssets != MASTERNODE_SYNC_BUDGET) return; - if(nCount == 0) lastBudgetItem = GetTime(); sumBudgetItemProp += nCount; countBudgetItemProp++; break; case(MASTERNODE_SYNC_BUDGET_FIN): if(RequestedMasternodeAssets != MASTERNODE_SYNC_BUDGET) return; - if(nCount == 0) lastBudgetItem = GetTime(); sumBudgetItemFin += nCount; countBudgetItemFin++; break; diff --git a/src/masternode-sync.h b/src/masternode-sync.h index f8f8205f2..b3d6486de 100644 --- a/src/masternode-sync.h +++ b/src/masternode-sync.h @@ -27,6 +27,10 @@ extern CMasternodeSync masternodeSync; class CMasternodeSync { public: + std::map mapSeenSyncMNB; + std::map mapSeenSyncMNW; + std::map mapSeenSyncBudget; + int64_t lastMasternodeList; int64_t lastMasternodeWinner; int64_t lastBudgetItem; @@ -50,9 +54,9 @@ public: CMasternodeSync(); - void AddedMasternodeList(); - void AddedMasternodeWinner(); - void AddedBudgetItem(); + void AddedMasternodeList(uint256 hash); + void AddedMasternodeWinner(uint256 hash); + void AddedBudgetItem(uint256 hash); void GetNextAsset(); void ProcessMessage(CNode* pfrom, std::string& strCommand, CDataStream& vRecv); bool IsBudgetFinEmpty(); diff --git a/src/masternode.cpp b/src/masternode.cpp index 8d6f282ec..6c581e0f7 100644 --- a/src/masternode.cpp +++ b/src/masternode.cpp @@ -390,7 +390,7 @@ bool CMasternodeBroadcast::CheckAndUpdate(int& nDos) pmn->UpdateFromNewBroadcast((*this)); pmn->Check(); if(pmn->IsEnabled()) Relay(); - masternodeSync.AddedMasternodeList(); + masternodeSync.AddedMasternodeList(GetHash()); } return true; diff --git a/src/masternodeman.cpp b/src/masternodeman.cpp index 98755cbf7..e6667dcef 100644 --- a/src/masternodeman.cpp +++ b/src/masternodeman.cpp @@ -254,6 +254,7 @@ void CMasternodeMan::CheckAndRemove(bool forceExpiredRemoval) map::iterator it3 = mapSeenMasternodeBroadcast.begin(); while(it3 != mapSeenMasternodeBroadcast.end()){ if((*it3).second.vin == (*it).vin){ + masternodeSync.mapSeenSyncMNB.erase((*it3).first); mapSeenMasternodeBroadcast.erase(it3++); } else { ++it3; @@ -644,7 +645,7 @@ void CMasternodeMan::ProcessMessage(CNode* pfrom, std::string& strCommand, CData vRecv >> mnb; if(mnodeman.mapSeenMasternodeBroadcast.count(mnb.GetHash())) { //seen - masternodeSync.AddedMasternodeList(); + masternodeSync.AddedMasternodeList(mnb.GetHash()); return; } mnodeman.mapSeenMasternodeBroadcast.insert(make_pair(mnb.GetHash(), mnb)); @@ -672,7 +673,7 @@ void CMasternodeMan::ProcessMessage(CNode* pfrom, std::string& strCommand, CData if(mnb.CheckInputsAndAdd(nDoS)) { // use this as a peer addrman.Add(CAddress(mnb.addr), pfrom->addr, 2*60*60); - masternodeSync.AddedMasternodeList(); + masternodeSync.AddedMasternodeList(mnb.GetHash()); } else { LogPrintf("mnb - Rejected Masternode entry %s\n", mnb.addr.ToString()); From 903970d0cf5a47dba9c9cef2f5a058844f3d5e7c Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Wed, 5 Aug 2015 05:10:47 +0300 Subject: [PATCH 2/2] clear mapSeenSync... on Reset --- src/masternode-sync.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/masternode-sync.cpp b/src/masternode-sync.cpp index 83e01a157..b53991bcf 100644 --- a/src/masternode-sync.cpp +++ b/src/masternode-sync.cpp @@ -61,6 +61,9 @@ void CMasternodeSync::Reset() lastMasternodeList = 0; lastMasternodeWinner = 0; lastBudgetItem = 0; + mapSeenSyncMNB.clear(); + mapSeenSyncMNW.clear(); + mapSeenSyncBudget.clear(); lastFailure = 0; nCountFailures = 0; sumMasternodeList = 0;