Skip to content

Commit a87909e

Browse files
authored
Keep the most recent gobject votes only (#2815)
* 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. * add comment * Drop getvotes rpc * Compare vote signals, do not compare hashes Also add comments
1 parent 010752d commit a87909e

File tree

5 files changed

+23
-62
lines changed

5 files changed

+23
-62
lines changed

src/governance-votedb.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ void CGovernanceObjectVoteFile::AddVote(const CGovernanceVote& vote)
2828
listVotes.push_front(vote);
2929
mapVoteIndex.emplace(nHash, listVotes.begin());
3030
++nMemoryVotes;
31+
RemoveOldVotes(vote);
3132
}
3233

3334
bool CGovernanceObjectVoteFile::HasVote(const uint256& nHash) const
@@ -90,6 +91,24 @@ std::set<uint256> CGovernanceObjectVoteFile::RemoveInvalidVotes(const COutPoint&
9091
return removedVotes;
9192
}
9293

94+
void CGovernanceObjectVoteFile::RemoveOldVotes(const CGovernanceVote& vote)
95+
{
96+
vote_l_it it = listVotes.begin();
97+
while (it != listVotes.end()) {
98+
if (it->GetMasternodeOutpoint() == vote.GetMasternodeOutpoint() // same masternode
99+
&& it->GetParentHash() == vote.GetParentHash() // same governance object (e.g. same proposal)
100+
&& it->GetSignal() == vote.GetSignal() // same signal (e.g. "funding", "delete", etc.)
101+
&& it->GetTimestamp() < vote.GetTimestamp()) // older than new vote
102+
{
103+
--nMemoryVotes;
104+
mapVoteIndex.erase(it->GetHash());
105+
listVotes.erase(it++);
106+
} else {
107+
++it;
108+
}
109+
}
110+
}
111+
93112
void CGovernanceObjectVoteFile::RebuildIndex()
94113
{
95114
mapVoteIndex.clear();

src/governance-votedb.h

+3
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ class CGovernanceObjectVoteFile
8888
}
8989

9090
private:
91+
// Drop older votes for the same gobject from the same masternode
92+
void RemoveOldVotes(const CGovernanceVote& vote);
93+
9194
void RebuildIndex();
9295
};
9396

src/governance.cpp

-13
Original file line numberDiff line numberDiff line change
@@ -478,19 +478,6 @@ CGovernanceObject* CGovernanceManager::FindGovernanceObject(const uint256& nHash
478478
return nullptr;
479479
}
480480

481-
std::vector<CGovernanceVote> CGovernanceManager::GetMatchingVotes(const uint256& nParentHash) const
482-
{
483-
LOCK(cs);
484-
std::vector<CGovernanceVote> vecResult;
485-
486-
object_m_cit it = mapObjects.find(nParentHash);
487-
if (it == mapObjects.end()) {
488-
return vecResult;
489-
}
490-
491-
return it->second.GetVoteFile().GetVotes();
492-
}
493-
494481
std::vector<CGovernanceVote> CGovernanceManager::GetCurrentVotes(const uint256& nParentHash, const COutPoint& mnCollateralOutpointFilter) const
495482
{
496483
LOCK(cs);

src/governance.h

-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@ class CGovernanceManager
309309
CGovernanceObject* FindGovernanceObject(const uint256& nHash);
310310

311311
// These commands are only used in RPC
312-
std::vector<CGovernanceVote> GetMatchingVotes(const uint256& nParentHash) const;
313312
std::vector<CGovernanceVote> GetCurrentVotes(const uint256& nParentHash, const COutPoint& mnCollateralOutpointFilter) const;
314313
std::vector<const CGovernanceObject*> GetAllNewerThan(int64_t nMoreThanTime) const;
315314

src/rpc/governance.cpp

+1-48
Original file line numberDiff line numberDiff line change
@@ -812,49 +812,6 @@ UniValue gobject_get(const JSONRPCRequest& request)
812812
return objResult;
813813
}
814814

815-
void gobject_getvotes_help()
816-
{
817-
throw std::runtime_error(
818-
"gobject getvotes <governance-hash>\n"
819-
"Get all votes for a governance object hash (including old votes)\n"
820-
"\nArguments:\n"
821-
"1. governance-hash (string, required) object id\n"
822-
);
823-
}
824-
825-
UniValue gobject_getvotes(const JSONRPCRequest& request)
826-
{
827-
if (request.fHelp || request.params.size() != 2)
828-
gobject_getvotes_help();
829-
830-
// COLLECT PARAMETERS FROM USER
831-
832-
uint256 hash = ParseHashV(request.params[1], "Governance hash");
833-
834-
// FIND OBJECT USER IS LOOKING FOR
835-
836-
LOCK(governance.cs);
837-
838-
CGovernanceObject* pGovObj = governance.FindGovernanceObject(hash);
839-
840-
if (pGovObj == nullptr) {
841-
throw JSONRPCError(RPC_INVALID_PARAMETER, "Unknown governance-hash");
842-
}
843-
844-
// REPORT RESULTS TO USER
845-
846-
UniValue bResult(UniValue::VOBJ);
847-
848-
// GET MATCHING VOTES BY HASH, THEN SHOW USERS VOTE INFORMATION
849-
850-
std::vector<CGovernanceVote> vecVotes = governance.GetMatchingVotes(hash);
851-
for (const auto& vote : vecVotes) {
852-
bResult.push_back(Pair(vote.GetHash().ToString(), vote.ToString()));
853-
}
854-
855-
return bResult;
856-
}
857-
858815
void gobject_getcurrentvotes_help()
859816
{
860817
throw std::runtime_error(
@@ -921,7 +878,6 @@ UniValue gobject_getcurrentvotes(const JSONRPCRequest& request)
921878
" deserialize - Deserialize governance object from hex string to JSON\n"
922879
" count - Count governance objects and votes (additional param: 'json' or 'all', default: 'json')\n"
923880
" get - Get governance object by hash\n"
924-
" getvotes - Get all votes for a governance object hash (including old votes)\n"
925881
" getcurrentvotes - Get only current (tallying) votes for a governance object hash (does not include old votes)\n"
926882
" list - List governance objects (can be filtered by signal and/or object type)\n"
927883
" diff - List differences since last diff\n"
@@ -978,11 +934,8 @@ UniValue gobject(const JSONRPCRequest& request)
978934
} else if (strCommand == "get") {
979935
// GET SPECIFIC GOVERNANCE ENTRY
980936
return gobject_get(request);
981-
} else if (strCommand == "getvotes") {
982-
// GETVOTES FOR SPECIFIC GOVERNANCE OBJECT
983-
return gobject_getvotes(request);
984937
} else if (strCommand == "getcurrentvotes") {
985-
// GETVOTES FOR SPECIFIC GOVERNANCE OBJECT
938+
// GET VOTES FOR SPECIFIC GOVERNANCE OBJECT
986939
return gobject_getcurrentvotes(request);
987940
} else {
988941
gobject_help();

0 commit comments

Comments
 (0)