From d1a9cbda815a66177c571a721b148c2a4457ac6c Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Fri, 29 Mar 2019 16:16:15 +0300 Subject: [PATCH 1/4] Keep the most recent gobject vote only We don't need to store full vote data for old votes - we never relay them to other nodes anyway. Still keep old hashes to avoid re-requesting data etc. --- src/governance-votedb.cpp | 19 +++++++++++++++++++ src/governance-votedb.h | 1 + 2 files changed, 20 insertions(+) diff --git a/src/governance-votedb.cpp b/src/governance-votedb.cpp index 204490b49c9a..6a3c862c526f 100644 --- a/src/governance-votedb.cpp +++ b/src/governance-votedb.cpp @@ -28,6 +28,7 @@ void CGovernanceObjectVoteFile::AddVote(const CGovernanceVote& vote) listVotes.push_front(vote); mapVoteIndex.emplace(nHash, listVotes.begin()); ++nMemoryVotes; + RemoveOldVotes(vote); } bool CGovernanceObjectVoteFile::HasVote(const uint256& nHash) const @@ -90,6 +91,24 @@ std::set CGovernanceObjectVoteFile::RemoveInvalidVotes(const COutPoint& return removedVotes; } +void CGovernanceObjectVoteFile::RemoveOldVotes(const CGovernanceVote& vote) +{ + vote_l_it it = listVotes.begin(); + while (it != listVotes.end()) { + if (it->GetMasternodeOutpoint() == vote.GetMasternodeOutpoint() + && it->GetParentHash() == vote.GetParentHash() + && it->GetTimestamp() < vote.GetTimestamp() + && it->GetHash() != vote.GetHash()) + { + --nMemoryVotes; + mapVoteIndex.erase(it->GetHash()); + listVotes.erase(it++); + } else { + ++it; + } + } +} + void CGovernanceObjectVoteFile::RebuildIndex() { mapVoteIndex.clear(); diff --git a/src/governance-votedb.h b/src/governance-votedb.h index eb52482ddb7b..3496395f1e62 100644 --- a/src/governance-votedb.h +++ b/src/governance-votedb.h @@ -88,6 +88,7 @@ class CGovernanceObjectVoteFile } private: + void RemoveOldVotes(const CGovernanceVote& vote); void RebuildIndex(); }; From a36173f4b1f3d382997c91028fd4210fbb6dd94e Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Fri, 29 Mar 2019 16:31:44 +0300 Subject: [PATCH 2/4] add comment --- src/governance-votedb.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/governance-votedb.h b/src/governance-votedb.h index 3496395f1e62..e3e61a25953c 100644 --- a/src/governance-votedb.h +++ b/src/governance-votedb.h @@ -88,7 +88,9 @@ class CGovernanceObjectVoteFile } private: + // Drop older votes for the same gobject from the same masternode void RemoveOldVotes(const CGovernanceVote& vote); + void RebuildIndex(); }; From 881d0d01aa79ea3bf24b0103d8d6b54d9d2f6bd6 Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Fri, 29 Mar 2019 22:21:49 +0300 Subject: [PATCH 3/4] Drop getvotes rpc --- src/governance.cpp | 13 ----------- src/governance.h | 1 - src/rpc/governance.cpp | 49 +----------------------------------------- 3 files changed, 1 insertion(+), 62 deletions(-) diff --git a/src/governance.cpp b/src/governance.cpp index b29dcbadf451..7017231a6f37 100644 --- a/src/governance.cpp +++ b/src/governance.cpp @@ -478,19 +478,6 @@ CGovernanceObject* CGovernanceManager::FindGovernanceObject(const uint256& nHash return nullptr; } -std::vector CGovernanceManager::GetMatchingVotes(const uint256& nParentHash) const -{ - LOCK(cs); - std::vector vecResult; - - object_m_cit it = mapObjects.find(nParentHash); - if (it == mapObjects.end()) { - return vecResult; - } - - return it->second.GetVoteFile().GetVotes(); -} - std::vector CGovernanceManager::GetCurrentVotes(const uint256& nParentHash, const COutPoint& mnCollateralOutpointFilter) const { LOCK(cs); diff --git a/src/governance.h b/src/governance.h index bf45edc3689c..466b0f971013 100644 --- a/src/governance.h +++ b/src/governance.h @@ -309,7 +309,6 @@ class CGovernanceManager CGovernanceObject* FindGovernanceObject(const uint256& nHash); // These commands are only used in RPC - std::vector GetMatchingVotes(const uint256& nParentHash) const; std::vector GetCurrentVotes(const uint256& nParentHash, const COutPoint& mnCollateralOutpointFilter) const; std::vector GetAllNewerThan(int64_t nMoreThanTime) const; diff --git a/src/rpc/governance.cpp b/src/rpc/governance.cpp index 68361b89c13a..b951737e6221 100644 --- a/src/rpc/governance.cpp +++ b/src/rpc/governance.cpp @@ -812,49 +812,6 @@ UniValue gobject_get(const JSONRPCRequest& request) return objResult; } -void gobject_getvotes_help() -{ - throw std::runtime_error( - "gobject getvotes \n" - "Get all votes for a governance object hash (including old votes)\n" - "\nArguments:\n" - "1. governance-hash (string, required) object id\n" - ); -} - -UniValue gobject_getvotes(const JSONRPCRequest& request) -{ - if (request.fHelp || request.params.size() != 2) - gobject_getvotes_help(); - - // COLLECT PARAMETERS FROM USER - - uint256 hash = ParseHashV(request.params[1], "Governance hash"); - - // FIND OBJECT USER IS LOOKING FOR - - LOCK(governance.cs); - - CGovernanceObject* pGovObj = governance.FindGovernanceObject(hash); - - if (pGovObj == nullptr) { - throw JSONRPCError(RPC_INVALID_PARAMETER, "Unknown governance-hash"); - } - - // REPORT RESULTS TO USER - - UniValue bResult(UniValue::VOBJ); - - // GET MATCHING VOTES BY HASH, THEN SHOW USERS VOTE INFORMATION - - std::vector vecVotes = governance.GetMatchingVotes(hash); - for (const auto& vote : vecVotes) { - bResult.push_back(Pair(vote.GetHash().ToString(), vote.ToString())); - } - - return bResult; -} - void gobject_getcurrentvotes_help() { throw std::runtime_error( @@ -921,7 +878,6 @@ UniValue gobject_getcurrentvotes(const JSONRPCRequest& request) " deserialize - Deserialize governance object from hex string to JSON\n" " count - Count governance objects and votes (additional param: 'json' or 'all', default: 'json')\n" " get - Get governance object by hash\n" - " getvotes - Get all votes for a governance object hash (including old votes)\n" " getcurrentvotes - Get only current (tallying) votes for a governance object hash (does not include old votes)\n" " list - List governance objects (can be filtered by signal and/or object type)\n" " diff - List differences since last diff\n" @@ -978,11 +934,8 @@ UniValue gobject(const JSONRPCRequest& request) } else if (strCommand == "get") { // GET SPECIFIC GOVERNANCE ENTRY return gobject_get(request); - } else if (strCommand == "getvotes") { - // GETVOTES FOR SPECIFIC GOVERNANCE OBJECT - return gobject_getvotes(request); } else if (strCommand == "getcurrentvotes") { - // GETVOTES FOR SPECIFIC GOVERNANCE OBJECT + // GET VOTES FOR SPECIFIC GOVERNANCE OBJECT return gobject_getcurrentvotes(request); } else { gobject_help(); From a8e0d3061340c1d7049b7fbbd8f6c626691cef3f Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Sat, 30 Mar 2019 18:08:24 +0300 Subject: [PATCH 4/4] Compare vote signals, do not compare hashes Also add comments --- src/governance-votedb.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/governance-votedb.cpp b/src/governance-votedb.cpp index 6a3c862c526f..ad08530c4b48 100644 --- a/src/governance-votedb.cpp +++ b/src/governance-votedb.cpp @@ -95,10 +95,10 @@ void CGovernanceObjectVoteFile::RemoveOldVotes(const CGovernanceVote& vote) { vote_l_it it = listVotes.begin(); while (it != listVotes.end()) { - if (it->GetMasternodeOutpoint() == vote.GetMasternodeOutpoint() - && it->GetParentHash() == vote.GetParentHash() - && it->GetTimestamp() < vote.GetTimestamp() - && it->GetHash() != vote.GetHash()) + if (it->GetMasternodeOutpoint() == vote.GetMasternodeOutpoint() // same masternode + && it->GetParentHash() == vote.GetParentHash() // same governance object (e.g. same proposal) + && it->GetSignal() == vote.GetSignal() // same signal (e.g. "funding", "delete", etc.) + && it->GetTimestamp() < vote.GetTimestamp()) // older than new vote { --nMemoryVotes; mapVoteIndex.erase(it->GetHash());