Skip to content

Commit

Permalink
Drop all kind of invalid votes from all types of gobjects (#2812)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
UdjinM6 committed Mar 30, 2019
1 parent e75760f commit ad7defb
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 19 deletions.
10 changes: 3 additions & 7 deletions src/governance-object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,21 +229,17 @@ void CGovernanceObject::ClearMasternodeVotes()
}
}

std::set<uint256> CGovernanceObject::RemoveInvalidProposalVotes(const COutPoint& mnOutpoint)
std::set<uint256> 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 {};
}
Expand All @@ -267,7 +263,7 @@ std::set<uint256> 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;
}

Expand Down
8 changes: 5 additions & 3 deletions src/governance-object.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint256> 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<uint256> RemoveInvalidVotes(const COutPoint& mnOutpoint);

void CheckOrphanVotes(CConnman& connman);
};
Expand Down
7 changes: 4 additions & 3 deletions src/governance-votedb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,15 @@ void CGovernanceObjectVoteFile::RemoveVotesFromMasternode(const COutPoint& outpo
}
}

std::set<uint256> CGovernanceObjectVoteFile::RemoveInvalidProposalVotes(const COutPoint& outpointMasternode)
std::set<uint256> CGovernanceObjectVoteFile::RemoveInvalidVotes(const COutPoint& outpointMasternode, bool fProposal)
{
std::set<uint256> 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());
Expand Down
2 changes: 1 addition & 1 deletion src/governance-votedb.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class CGovernanceObjectVoteFile
std::vector<CGovernanceVote> GetVotes() const;

void RemoveVotesFromMasternode(const COutPoint& outpointMasternode);
std::set<uint256> RemoveInvalidProposalVotes(const COutPoint& outpointMasternode);
std::set<uint256> RemoveInvalidVotes(const COutPoint& outpointMasternode, bool fProposal);

ADD_SERIALIZE_METHODS;

Expand Down
10 changes: 6 additions & 4 deletions src/governance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1341,7 +1341,7 @@ void CGovernanceManager::CleanOrphanObjects()
}
}

void CGovernanceManager::RemoveInvalidProposalVotes()
void CGovernanceManager::RemoveInvalidVotes()
{
auto curMNList = deterministicMNManager->GetListAtChainTip();
auto diff = lastMNListForVotingKeys.BuildDiff(curMNList);
Expand All @@ -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) {
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/governance.h
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ class CGovernanceManager

void CleanOrphanObjects();

void RemoveInvalidProposalVotes();
void RemoveInvalidVotes();

};

Expand Down

0 comments on commit ad7defb

Please sign in to comment.