Skip to content

Commit

Permalink
Keep the most recent gobject votes only (#2815)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
UdjinM6 committed Apr 1, 2019
1 parent 010752d commit a87909e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 62 deletions.
19 changes: 19 additions & 0 deletions src/governance-votedb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -90,6 +91,24 @@ std::set<uint256> 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() // 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());
listVotes.erase(it++);
} else {
++it;
}
}
}

void CGovernanceObjectVoteFile::RebuildIndex()
{
mapVoteIndex.clear();
Expand Down
3 changes: 3 additions & 0 deletions src/governance-votedb.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ class CGovernanceObjectVoteFile
}

private:
// Drop older votes for the same gobject from the same masternode
void RemoveOldVotes(const CGovernanceVote& vote);

void RebuildIndex();
};

Expand Down
13 changes: 0 additions & 13 deletions src/governance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,19 +478,6 @@ CGovernanceObject* CGovernanceManager::FindGovernanceObject(const uint256& nHash
return nullptr;
}

std::vector<CGovernanceVote> CGovernanceManager::GetMatchingVotes(const uint256& nParentHash) const
{
LOCK(cs);
std::vector<CGovernanceVote> vecResult;

object_m_cit it = mapObjects.find(nParentHash);
if (it == mapObjects.end()) {
return vecResult;
}

return it->second.GetVoteFile().GetVotes();
}

std::vector<CGovernanceVote> CGovernanceManager::GetCurrentVotes(const uint256& nParentHash, const COutPoint& mnCollateralOutpointFilter) const
{
LOCK(cs);
Expand Down
1 change: 0 additions & 1 deletion src/governance.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,6 @@ class CGovernanceManager
CGovernanceObject* FindGovernanceObject(const uint256& nHash);

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

Expand Down
49 changes: 1 addition & 48 deletions src/rpc/governance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -812,49 +812,6 @@ UniValue gobject_get(const JSONRPCRequest& request)
return objResult;
}

void gobject_getvotes_help()
{
throw std::runtime_error(
"gobject getvotes <governance-hash>\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<CGovernanceVote> 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(
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit a87909e

Please sign in to comment.