From bc96f288ed90f478beb7533a1de09694e4a91356 Mon Sep 17 00:00:00 2001 From: Tim Flynn Date: Sat, 5 Nov 2016 12:13:30 -0400 Subject: [PATCH] Governance fixes (#1131) * Added net logging messages in main.cpp * Added logging for trigger removal * Improved log message for CGovernanceManager::UpdatedBlockTip * Improved log messages in CGovernanceManager::UpdateCachesAndClean * Added more logging to CGovernanceTriggerManager * Check vote validity before pushing inventory during sync * Add triggers to map after loading governance.dat file --- src/governance-classes.cpp | 11 +++++++++++ src/governance.cpp | 32 +++++++++++++++++++++++++++----- src/governance.h | 6 ++++++ src/main.cpp | 4 ++++ 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/governance-classes.cpp b/src/governance-classes.cpp index 7c39b9fe4..f1a42a803 100644 --- a/src/governance-classes.cpp +++ b/src/governance-classes.cpp @@ -164,6 +164,7 @@ bool CGovernanceTriggerManager::AddNewTrigger(uint256 nHash) void CGovernanceTriggerManager::CleanAndRemove() { + LogPrint("gobject", "CGovernanceTriggerManager::CleanAndRemove -- Start\n"); DBG( cout << "CGovernanceTriggerManager::CleanAndRemove: Start" << endl; ); AssertLockHeld(governance.cs); @@ -186,24 +187,30 @@ void CGovernanceTriggerManager::CleanAndRemove() // Remove triggers that are invalid or already executed DBG( cout << "CGovernanceTriggerManager::CleanAndRemove: mapTrigger.size() = " << mapTrigger.size() << endl; ); + LogPrint("gobject", "CGovernanceTriggerManager::CleanAndRemove -- mapTrigger.size() = %d\n", mapTrigger.size()); trigger_m_it it = mapTrigger.begin(); while(it != mapTrigger.end()) { bool remove = false; CSuperblock_sptr& pSuperblock = it->second; if(!pSuperblock) { DBG( cout << "CGovernanceTriggerManager::CleanAndRemove: NULL superblock marked for removal " << endl; ); + LogPrint("gobject", "CGovernanceTriggerManager::CleanAndRemove -- NULL superblock marked for removal\n"); remove = true; } else { DBG( cout << "CGovernanceTriggerManager::CleanAndRemove: superblock status = " << pSuperblock->GetStatus() << endl; ); + LogPrint("gobject", "CGovernanceTriggerManager::CleanAndRemove -- superblock status = %d\n", pSuperblock->GetStatus()); switch(pSuperblock->GetStatus()) { case SEEN_OBJECT_ERROR_INVALID: case SEEN_OBJECT_UNKNOWN: + LogPrint("gobject", "CGovernanceTriggerManager::CleanAndRemove -- Unknown or invalid trigger found\n"); remove = true; break; case SEEN_OBJECT_EXECUTED: { + LogPrint("gobject", "CGovernanceTriggerManager::CleanAndRemove -- Executed trigger found\n"); CGovernanceObject* pgovobj = pSuperblock->GetGovernanceObject(); if(pgovobj) { + LogPrint("gobject", "CGovernanceTriggerManager::CleanAndRemove -- Expiring executed object: %s\n", pgovobj->GetHash().ToString()); pgovobj->fExpired = true; } } @@ -214,10 +221,13 @@ void CGovernanceTriggerManager::CleanAndRemove() int nTriggerBlock = pSuperblock->GetBlockStart(); // Rough approximation: a cycle of superblock ++ int nExpirationBlock = nTriggerBlock + Params().GetConsensus().nSuperblockCycle + GOVERNANCE_FEE_CONFIRMATIONS; + LogPrint("gobject", "CGovernanceTriggerManager::CleanAndRemove -- nTriggerBlock = %d, nExpriartionBlock = %d\n"); if(governance.GetCachedBlockHeight() > nExpirationBlock) { + LogPrint("gobject", "CGovernanceTriggerManager::CleanAndRemove -- Outdated trigger found\n"); remove = true; CGovernanceObject* pgovobj = pSuperblock->GetGovernanceObject(); if(pgovobj) { + LogPrint("gobject", "CGovernanceTriggerManager::CleanAndRemove -- Expiring outdated object: %s\n", pgovobj->GetHash().ToString()); pgovobj->fExpired = true; } } @@ -239,6 +249,7 @@ void CGovernanceTriggerManager::CleanAndRemove() << strdata << endl; ); + LogPrint("gobject", "CGovernanceTriggerManager::CleanAndRemove -- Removing trigger object\n"); mapTrigger.erase(it++); } else { diff --git a/src/governance.cpp b/src/governance.cpp index 80e18e413..f923eadbe 100644 --- a/src/governance.cpp +++ b/src/governance.cpp @@ -300,6 +300,8 @@ void CGovernanceManager::UpdateCachesAndClean() if(!pCurrentBlockIndex) return; + LogPrint("gobject", "CGovernanceManager::UpdateCachesAndClean -- After pCurrentBlockIndex (not NULL)\n"); + // UPDATE CACHE FOR EACH OBJECT THAT IS FLAGGED DIRTYCACHE=TRUE object_m_it it = mapObjects.begin(); @@ -332,7 +334,7 @@ void CGovernanceManager::UpdateCachesAndClean() // IF DELETE=TRUE, THEN CLEAN THE MESS UP! if(pObj->fCachedDelete || pObj->fExpired) { - LogPrintf("UpdateCachesAndClean -- erase obj %s\n", (*it).first.ToString()); + LogPrintf("CGovernanceManager::UpdateCachesAndClean -- erase obj %s\n", (*it).first.ToString()); mnodeman.RemoveGovernanceObject(pObj->GetHash()); mapObjects.erase(it++); } else { @@ -479,9 +481,14 @@ void CGovernanceManager::Sync(CNode* pfrom, uint256 nProp) vote_m_it it2 = mapVotesByHash.begin(); while(it2 != mapVotesByHash.end()) { - pfrom->PushInventory(CInv(MSG_GOVERNANCE_OBJECT_VOTE, (*it2).first)); - nInvCount++; - ++it2; + CGovernanceVote& vote = it2->second; + if(!vote.IsValid(true)) { + // Don't relay votes that are now invalid (ie. missing MN) to avoid being banned + continue; + } + pfrom->PushInventory(CInv(MSG_GOVERNANCE_OBJECT_VOTE, (*it2).first)); + nInvCount++; + ++it2; } } @@ -613,6 +620,21 @@ bool CGovernanceManager::MasternodeRateCheck(const CTxIn& vin, int nObjectType) return false; } +void CGovernanceManager::AddCachedTriggers() +{ + LOCK(cs); + + for(object_m_it it = mapObjects.begin(); it != mapObjects.end(); ++it) { + CGovernanceObject& govobj = it->second; + + if(govobj.nObjectType != GOVERNANCE_OBJECT_TRIGGER) { + continue; + } + + triggerman.AddNewTrigger(govobj.GetHash()); + } +} + CGovernanceObject::CGovernanceObject() : cs(), nHashParent(), @@ -1106,7 +1128,7 @@ void CGovernanceManager::UpdatedBlockTip(const CBlockIndex *pindex) LOCK(cs); pCurrentBlockIndex = pindex; nCachedBlockHeight = pCurrentBlockIndex->nHeight; - LogPrint("gobject", "pCurrentBlockIndex->nHeight: %d\n", pCurrentBlockIndex->nHeight); + LogPrint("gobject", "CGovernanceManager::UpdatedBlockTip pCurrentBlockIndex->nHeight: %d\n", pCurrentBlockIndex->nHeight); // TO REPROCESS OBJECTS WE SHOULD BE SYNCED diff --git a/src/governance.h b/src/governance.h index 7bed26bbf..7f0e085e0 100644 --- a/src/governance.h +++ b/src/governance.h @@ -185,6 +185,9 @@ public: READWRITE(mapVotesByHash); READWRITE(mapVotesByType); READWRITE(mapLastMasternodeTrigger); + if(ser_action.ForRead()) { + AddCachedTriggers(); + } } void UpdatedBlockTip(const CBlockIndex *pindex); @@ -208,6 +211,9 @@ public: bool MasternodeRateCheck(const CTxIn& vin, int nObjectType); +private: + void AddCachedTriggers(); + }; /** diff --git a/src/main.cpp b/src/main.cpp index f7b3b82fb..059dd1259 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4976,6 +4976,7 @@ void static ProcessGetData(CNode* pfrom, const Consensus::Params& consensusParam break; const CInv &inv = *it; + LogPrint("net", "ProcessGetData -- inv = %s\n", inv.ToString()); { boost::this_thread::interruption_point(); it++; @@ -5169,6 +5170,7 @@ void static ProcessGetData(CNode* pfrom, const Consensus::Params& consensusParam } if (!pushed && inv.type == MSG_GOVERNANCE_OBJECT) { + LogPrint("net", "ProcessGetData -- MSG_GOVERNANCE_OBJECT: inv = %s\n", inv.ToString()); CDataStream ss(SER_NETWORK, PROTOCOL_VERSION); bool topush = false; { @@ -5179,6 +5181,7 @@ void static ProcessGetData(CNode* pfrom, const Consensus::Params& consensusParam } } } + LogPrint("net", "ProcessGetData -- MSG_GOVERNANCE_OBJECT: topush = %d, inv = %s\n", topush, inv.ToString()); if(topush) { pfrom->PushMessage(NetMsgType::MNGOVERNANCEOBJECT, ss); pushed = true; @@ -5197,6 +5200,7 @@ void static ProcessGetData(CNode* pfrom, const Consensus::Params& consensusParam } } if(topush) { + LogPrint("net", "ProcessGetData -- pushing: inv = %s\n", inv.ToString()); pfrom->PushMessage(NetMsgType::MNGOVERNANCEOBJECTVOTE, ss); pushed = true; }