Periodicially request orphan objects (#1383)

* Implement RequestOrphanObjects

* Ensure governance objects are only requested from peers once per call

* Add gobject log messages

* Implemented CleanOrphanObjects

* Move governance maintenance functions from NewBlock to a time-based function

* Remove unused delcaration for mapAskedForGovernanceObject
This commit is contained in:
Tim Flynn 2017-03-06 02:46:59 -05:00 committed by Holger Schinzel
parent 85d152bac0
commit c8b6199979
4 changed files with 80 additions and 24 deletions

View File

@ -146,6 +146,13 @@ public:
return true;
}
void GetKeys(std::vector<K>& vecKeys)
{
for(map_cit it = mapIndex.begin(); it != mapIndex.end(); ++it) {
vecKeys.push_back(it->first);
}
}
void Erase(const K& key)
{
map_it mit = mapIndex.find(key);

View File

@ -6,6 +6,7 @@
#include "coincontrol.h"
#include "consensus/validation.h"
#include "darksend.h"
#include "governance.h"
#include "init.h"
#include "instantx.h"
#include "masternode-payments.h"
@ -2521,6 +2522,10 @@ void ThreadCheckDarkSendPool()
mnodeman.DoFullVerificationStep();
}
if(nTick % (60 * 5) == 0) {
governance.DoMaintenance();
}
darkSendPool.CheckTimeout();
darkSendPool.CheckForCompleteQueue();

View File

@ -16,8 +16,6 @@
CGovernanceManager governance;
std::map<uint256, int64_t> mapAskedForGovernanceObject;
int nSubmittedFinalBudget;
const std::string CGovernanceManager::SERIALIZATION_VERSION_STRING = "CGovernanceManager-Version-11";
@ -647,25 +645,23 @@ struct sortProposalsByVotes {
}
};
void CGovernanceManager::NewBlock()
void CGovernanceManager::DoMaintenance()
{
// NOTHING TO DO IN LITEMODE
if(fLiteMode) {
return;
}
// IF WE'RE NOT SYNCED, EXIT
if(!masternodeSync.IsSynced()) return;
if(!pCurrentBlockIndex) return;
LOCK(cs);
// CHECK OBJECTS WE'VE ASKED FOR, REMOVE OLD ENTRIES
std::map<uint256, int64_t>::iterator it = mapAskedForGovernanceObject.begin();
while(it != mapAskedForGovernanceObject.end()) {
if((*it).second > GetTime() - (60*60*24)) {
++it;
} else {
mapAskedForGovernanceObject.erase(it++);
}
}
CleanOrphanObjects();
RequestOrphanObjects();
// CHECK AND REMOVE - REPROCESS GOVERNANCE OBJECTS
@ -1029,6 +1025,8 @@ void CGovernanceManager::RequestGovernanceObject(CNode* pfrom, const uint256& nH
return;
}
LogPrint("gobject", "CGovernanceObject::RequestGovernanceObject -- hash = %s (peer=%d)\n", nHash.ToString(), pfrom->GetId());
if(pfrom->nVersion < GOVERNANCE_FILTER_PROTO_VERSION) {
pfrom->PushMessage(NetMsgType::MNGOVERNANCESYNC, nHash);
return;
@ -1291,13 +1289,56 @@ void CGovernanceManager::UpdatedBlockTip(const CBlockIndex *pindex)
return;
}
LOCK(cs);
pCurrentBlockIndex = pindex;
nCachedBlockHeight = pCurrentBlockIndex->nHeight;
LogPrint("gobject", "CGovernanceManager::UpdatedBlockTip pCurrentBlockIndex->nHeight: %d\n", pCurrentBlockIndex->nHeight);
// TO REPROCESS OBJECTS WE SHOULD BE SYNCED
if(!fLiteMode && masternodeSync.IsSynced())
NewBlock();
{
LOCK(cs);
pCurrentBlockIndex = pindex;
nCachedBlockHeight = pCurrentBlockIndex->nHeight;
LogPrint("gobject", "CGovernanceManager::UpdatedBlockTip pCurrentBlockIndex->nHeight: %d\n", pCurrentBlockIndex->nHeight);
}
}
void CGovernanceManager::RequestOrphanObjects()
{
std::vector<CNode*> vNodesCopy = CopyNodeVector();
{
LOCK(cs);
std::vector<uint256> vecHashes;
mapOrphanVotes.GetKeys(vecHashes);
LogPrint("gobject", "CGovernanceObject::RequestOrphanObjects -- number objects = %d\n", vecHashes.size());
for(size_t i = 0; i < vecHashes.size(); ++i) {
const uint256& nHash = vecHashes[i];
if(mapObjects.find(nHash) != mapObjects.end()) {
continue;
}
for(size_t j = 0; j < vNodesCopy.size(); ++j) {
CNode* pnode = vNodesCopy[j];
if(pnode->fMasternode) {
continue;
}
RequestGovernanceObject(pnode, nHash);
}
}
}
ReleaseNodeVector(vNodesCopy);
}
void CGovernanceManager::CleanOrphanObjects()
{
LOCK(cs);
const vote_mcache_t::list_t& items = mapOrphanVotes.GetItemList();
int64_t nNow = GetAdjustedTime();
vote_mcache_t::list_cit it = items.begin();
while(it != items.end()) {
vote_mcache_t::list_cit prevIt = it;
++it;
const vote_time_pair_t& pairVote = prevIt->value;
if(pairVote.second < nNow) {
mapOrphanVotes.Erase(prevIt->key, prevIt->value);
}
}
}

View File

@ -24,7 +24,6 @@ class CGovernanceTriggerManager;
class CGovernanceObject;
class CGovernanceVote;
extern std::map<uint256, int64_t> mapAskedForGovernanceObject;
extern CGovernanceManager governance;
typedef std::pair<CGovernanceObject, int64_t> object_time_pair_t;
@ -280,7 +279,7 @@ public:
void ProcessMessage(CNode* pfrom, std::string& strCommand, CDataStream& vRecv);
void NewBlock();
void DoMaintenance();
CGovernanceObject *FindGovernanceObject(const uint256& nHash);
@ -425,6 +424,10 @@ private:
bool UpdateCurrentWatchdog(CGovernanceObject& watchdogNew);
void RequestOrphanObjects();
void CleanOrphanObjects();
};
#endif