From ad7defba9218d74f7ac6439b2930a0d080bcab31 Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Sat, 30 Mar 2019 17:54:22 +0300 Subject: [PATCH] Drop all kind of invalid votes from all types of gobjects (#2812) * Drop all kind of invalid votes from all types of gobjects Currently we only do so for proposal "funding" votes which looks incomplete. * Apply review suggestions --- src/governance-object.cpp | 10 +++------- src/governance-object.h | 8 +++++--- src/governance-votedb.cpp | 7 ++++--- src/governance-votedb.h | 2 +- src/governance.cpp | 10 ++++++---- src/governance.h | 2 +- 6 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/governance-object.cpp b/src/governance-object.cpp index 37463e5710499..81aaa94548292 100644 --- a/src/governance-object.cpp +++ b/src/governance-object.cpp @@ -229,21 +229,17 @@ void CGovernanceObject::ClearMasternodeVotes() } } -std::set CGovernanceObject::RemoveInvalidProposalVotes(const COutPoint& mnOutpoint) +std::set CGovernanceObject::RemoveInvalidVotes(const COutPoint& mnOutpoint) { LOCK(cs); - if (nObjectType != GOVERNANCE_OBJECT_PROPOSAL) { - return {}; - } - auto it = mapCurrentMNVotes.find(mnOutpoint); if (it == mapCurrentMNVotes.end()) { // don't even try as we don't have any votes from this MN return {}; } - auto removedVotes = fileVotes.RemoveInvalidProposalVotes(mnOutpoint); + auto removedVotes = fileVotes.RemoveInvalidVotes(mnOutpoint, nObjectType == GOVERNANCE_OBJECT_PROPOSAL); if (removedVotes.empty()) { return {}; } @@ -267,7 +263,7 @@ std::set CGovernanceObject::RemoveInvalidProposalVotes(const COutPoint& for (auto& h : removedVotes) { removedStr += strprintf(" %s\n", h.ToString()); } - LogPrintf("CGovernanceObject::%s -- Removed %d invalid votes for %s from MN %s:\n%s\n", __func__, removedVotes.size(), nParentHash.ToString(), mnOutpoint.ToString(), removedStr); + LogPrintf("CGovernanceObject::%s -- Removed %d invalid votes for %s from MN %s:\n%s", __func__, removedVotes.size(), nParentHash.ToString(), mnOutpoint.ToString(), removedStr); fDirtyCache = true; } diff --git a/src/governance-object.h b/src/governance-object.h index d39a83b8f1968..c4e4f87b0d239 100644 --- a/src/governance-object.h +++ b/src/governance-object.h @@ -345,9 +345,11 @@ class CGovernanceObject /// Called when MN's which have voted on this object have been removed void ClearMasternodeVotes(); - // Revalidate all votes from this MN and delete them if validation fails - // This is the case for DIP3 MNs that change voting keys. Returns deleted vote hashes - std::set RemoveInvalidProposalVotes(const COutPoint& mnOutpoint); + // Revalidate all votes from this MN and delete them if validation fails. + // This is the case for DIP3 MNs that changed voting or operator keys and + // also for MNs that were removed from the list completely. + // Returns deleted vote hashes. + std::set RemoveInvalidVotes(const COutPoint& mnOutpoint); void CheckOrphanVotes(CConnman& connman); }; diff --git a/src/governance-votedb.cpp b/src/governance-votedb.cpp index d1f528a051c62..204490b49c9a0 100644 --- a/src/governance-votedb.cpp +++ b/src/governance-votedb.cpp @@ -68,14 +68,15 @@ void CGovernanceObjectVoteFile::RemoveVotesFromMasternode(const COutPoint& outpo } } -std::set CGovernanceObjectVoteFile::RemoveInvalidProposalVotes(const COutPoint& outpointMasternode) +std::set CGovernanceObjectVoteFile::RemoveInvalidVotes(const COutPoint& outpointMasternode, bool fProposal) { std::set removedVotes; vote_l_it it = listVotes.begin(); while (it != listVotes.end()) { - if (it->GetSignal() == VOTE_SIGNAL_FUNDING && it->GetMasternodeOutpoint() == outpointMasternode) { - if (!it->IsValid(true)) { + if (it->GetMasternodeOutpoint() == outpointMasternode) { + bool useVotingKey = fProposal && (it->GetSignal() == VOTE_SIGNAL_FUNDING); + if (!it->IsValid(useVotingKey)) { removedVotes.emplace(it->GetHash()); --nMemoryVotes; mapVoteIndex.erase(it->GetHash()); diff --git a/src/governance-votedb.h b/src/governance-votedb.h index 5dda1ba4f1ec9..eb52482ddb7b4 100644 --- a/src/governance-votedb.h +++ b/src/governance-votedb.h @@ -73,7 +73,7 @@ class CGovernanceObjectVoteFile std::vector GetVotes() const; void RemoveVotesFromMasternode(const COutPoint& outpointMasternode); - std::set RemoveInvalidProposalVotes(const COutPoint& outpointMasternode); + std::set RemoveInvalidVotes(const COutPoint& outpointMasternode, bool fProposal); ADD_SERIALIZE_METHODS; diff --git a/src/governance.cpp b/src/governance.cpp index a820704826a2a..b29dcbadf4517 100644 --- a/src/governance.cpp +++ b/src/governance.cpp @@ -571,7 +571,7 @@ void CGovernanceManager::DoMaintenance(CConnman& connman) if (fLiteMode || !masternodeSync.IsSynced() || ShutdownRequested()) return; if (deterministicMNManager->IsDIP3Enforced()) { - RemoveInvalidProposalVotes(); + RemoveInvalidVotes(); } // CHECK OBJECTS WE'VE ASKED FOR, REMOVE OLD ENTRIES @@ -1286,7 +1286,7 @@ void CGovernanceManager::UpdatedBlockTip(const CBlockIndex* pindex, CConnman& co LogPrint("gobject", "CGovernanceManager::UpdatedBlockTip -- nCachedBlockHeight: %d\n", nCachedBlockHeight); if (deterministicMNManager->IsDIP3Enforced(pindex->nHeight)) { - RemoveInvalidProposalVotes(); + RemoveInvalidVotes(); } CheckPostponedObjects(connman); @@ -1341,7 +1341,7 @@ void CGovernanceManager::CleanOrphanObjects() } } -void CGovernanceManager::RemoveInvalidProposalVotes() +void CGovernanceManager::RemoveInvalidVotes() { auto curMNList = deterministicMNManager->GetListAtChainTip(); auto diff = lastMNListForVotingKeys.BuildDiff(curMNList); @@ -1353,6 +1353,8 @@ void CGovernanceManager::RemoveInvalidProposalVotes() auto oldDmn = lastMNListForVotingKeys.GetMN(p.first); if (p.second->keyIDVoting != oldDmn->pdmnState->keyIDVoting) { changedKeyMNs.emplace_back(oldDmn->collateralOutpoint); + } else if (p.second->pubKeyOperator != oldDmn->pdmnState->pubKeyOperator) { + changedKeyMNs.emplace_back(oldDmn->collateralOutpoint); } } for (const auto& proTxHash : diff.removedMns) { @@ -1362,7 +1364,7 @@ void CGovernanceManager::RemoveInvalidProposalVotes() for (const auto& outpoint : changedKeyMNs) { for (auto& p : mapObjects) { - auto removed = p.second.RemoveInvalidProposalVotes(outpoint); + auto removed = p.second.RemoveInvalidVotes(outpoint); if (removed.empty()) { continue; } diff --git a/src/governance.h b/src/governance.h index dd18c0b9c685f..bf45edc3689c3 100644 --- a/src/governance.h +++ b/src/governance.h @@ -453,7 +453,7 @@ class CGovernanceManager void CleanOrphanObjects(); - void RemoveInvalidProposalVotes(); + void RemoveInvalidVotes(); };