From 57fd9e9e2a2f1c50c00147918acda03434c66933 Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Fri, 17 Feb 2017 04:48:42 +0400 Subject: [PATCH] fix watchdogs (#1346) * fix watchdogs: - do not accept if CreationTime is out of bounds (using CreationTime, not local time now) - do not sync expired - fix disk serialization * drop watchdogs early, avoid adding//removing * clean mapWatchdogObjects when object is deleted via votes --- src/governance-object.h | 2 ++ src/governance.cpp | 32 ++++++++++++++++++++++---------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/governance-object.h b/src/governance-object.h index fb6c9fa98..974c923dc 100644 --- a/src/governance-object.h +++ b/src/governance-object.h @@ -316,6 +316,8 @@ public: if(nType & SER_DISK) { // Only include these for the disk file format LogPrint("gobject", "CGovernanceObject::SerializationOp Reading/writing votes from/to disk\n"); + READWRITE(nDeletionTime); + READWRITE(fExpired); READWRITE(mapCurrentMNVotes); READWRITE(fileVotes); LogPrint("gobject", "CGovernanceObject::SerializationOp hash = %s, vote count = %d\n", GetHash().ToString(), fileVotes.GetVoteCount()); diff --git a/src/governance.cpp b/src/governance.cpp index 7dbd42c4a..0ef3fcf44 100644 --- a/src/governance.cpp +++ b/src/governance.cpp @@ -20,7 +20,7 @@ std::map mapAskedForGovernanceObject; int nSubmittedFinalBudget; -const std::string CGovernanceManager::SERIALIZATION_VERSION_STRING = "CGovernanceManager-Version-8"; +const std::string CGovernanceManager::SERIALIZATION_VERSION_STRING = "CGovernanceManager-Version-9"; CGovernanceManager::CGovernanceManager() : pCurrentBlockIndex(NULL), @@ -324,6 +324,16 @@ bool CGovernanceManager::AddGovernanceObject(CGovernanceObject& govobj) LogPrint("gobject", "CGovernanceManager::AddGovernanceObject -- Adding object: hash = %s, type = %d\n", nHash.ToString(), govobj.GetObjectType()); + // If it's a watchdog, make sure it fits required time bounds + if(govobj.nObjectType == GOVERNANCE_OBJECT_WATCHDOG && + (govobj.GetCreationTime() < GetAdjustedTime() - GOVERNANCE_WATCHDOG_EXPIRATION_TIME || + govobj.GetCreationTime() > GetAdjustedTime() + GOVERNANCE_WATCHDOG_EXPIRATION_TIME) + ) { + // drop it + LogPrint("gobject", "CGovernanceManager::AddGovernanceObject -- CreationTime is out of bounds: hash = %s\n", nHash.ToString()); + return false; + } + // INSERT INTO OUR GOVERNANCE OBJECT MEMORY mapObjects.insert(std::make_pair(nHash, govobj)); @@ -341,8 +351,8 @@ bool CGovernanceManager::AddGovernanceObject(CGovernanceObject& govobj) DBG( cout << "CGovernanceManager::AddGovernanceObject After AddNewTrigger" << endl; ); break; case GOVERNANCE_OBJECT_WATCHDOG: - mapWatchdogObjects[nHash] = GetAdjustedTime() + GOVERNANCE_WATCHDOG_EXPIRATION_TIME; - LogPrint("gobject", "CGovernanceManager::AddGovernanceObject -- Added watchdog to map: hash = %s\n", nHash.ToString()); + mapWatchdogObjects[nHash] = govobj.GetCreationTime() + GOVERNANCE_WATCHDOG_EXPIRATION_TIME; + LogPrint("gobject", "CGovernanceManager::AddGovernanceObject -- Added watchdog to map: hash = %s\n", nHash.ToString()); break; default: break; @@ -455,7 +465,9 @@ void CGovernanceManager::UpdateCachesAndClean() ++lit; } } - + if(pObj->nObjectType == GOVERNANCE_OBJECT_WATCHDOG && pObj->IsSetCachedDelete()) { + mapWatchdogObjects.erase(it->first); + } mapObjects.erase(it++); } else { ++it; @@ -686,8 +698,8 @@ void CGovernanceManager::Sync(CNode* pfrom, const uint256& nProp, const CBloomFi LogPrint("gobject", "CGovernanceManager::Sync -- attempting to sync govobj: %s, peer=%d\n", strHash, pfrom->id); - if(govobj.IsSetCachedDelete()) { - LogPrintf("CGovernanceManager::Sync -- not syncing deleted govobj: %s, peer=%d\n", + if(govobj.IsSetCachedDelete() || govobj.IsSetExpired()) { + LogPrintf("CGovernanceManager::Sync -- not syncing deleted/expired govobj: %s, peer=%d\n", strHash, pfrom->id); continue; } @@ -709,8 +721,8 @@ void CGovernanceManager::Sync(CNode* pfrom, const uint256& nProp, const CBloomFi LogPrint("gobject", "CGovernanceManager::Sync -- attempting to sync govobj: %s, peer=%d\n", strHash, pfrom->id); - if(govobj.IsSetCachedDelete()) { - LogPrintf("CGovernanceManager::Sync -- not syncing deleted govobj: %s, peer=%d\n", + if(govobj.IsSetCachedDelete() || govobj.IsSetExpired()) { + LogPrintf("CGovernanceManager::Sync -- not syncing deleted/expired govobj: %s, peer=%d\n", strHash, pfrom->id); return; } @@ -1193,9 +1205,9 @@ std::string CGovernanceManager::ToString() const ++it; } - return strprintf("Governance Objects: %d (Proposals: %d, Triggers: %d, Watchdogs: %d, Other: %d; Seen: %d), Votes: %d", + return strprintf("Governance Objects: %d (Proposals: %d, Triggers: %d, Watchdogs: %d/%d, Other: %d; Seen: %d), Votes: %d", (int)mapObjects.size(), - nProposalCount, nTriggerCount, nWatchdogCount, nOtherCount, (int)mapSeenGovernanceObjects.size(), + nProposalCount, nTriggerCount, nWatchdogCount, mapWatchdogObjects.size(), nOtherCount, (int)mapSeenGovernanceObjects.size(), (int)mapVoteToObject.GetSize()); }