Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display aggregate vote stats in listgovproposalvotes #1714

Merged
merged 15 commits into from
Feb 7, 2023
Prev Previous commit
Next Next commit
Use VotingInfo, remove unknown votes from output
shohamc1 committed Feb 3, 2023

Unverified

This user has not yet uploaded their public signing key.
commit 15fb0e5694666fbd4b8b96623fded08fd64097ee
42 changes: 17 additions & 25 deletions src/masternodes/rpc_proposals.cpp
Original file line number Diff line number Diff line change
@@ -6,9 +6,12 @@
const bool DEFAULT_RPC_GOV_NEUTRAL = false;

struct VotingInfo {
int32_t votesPossible;
int32_t votesPresent;
int32_t votesYes;
int32_t votesPossible = 0;
int32_t votesPresent = 0;
int32_t votesYes = 0;
int32_t votesNo = 0;
int32_t votesNeutral = 0;
int32_t votesInvalid = 0;
};

UniValue proposalToJSON(const CProposalId &propId,
@@ -109,32 +112,22 @@ UniValue proposalVoteToJSON(const CProposalId &propId, uint8_t cycle, const uint
return ret;
}

struct VoteAccounting {
int totalVotes;
int yesVotes;
int neutralVotes;
int noVotes;
int unknownVotes;
};

void proposalVoteAccounting(const CProposalVoteType &vote, uint256 propId, std::map<std::string, VoteAccounting> &map) {
void proposalVoteAccounting(const CProposalVoteType &vote, uint256 propId, std::map<std::string, VotingInfo> &map) {
const auto voteString = CProposalVoteToString(vote);

if (map.find(propId.GetHex()) == map.end())
map.emplace(propId.GetHex(), VoteAccounting{0, 0, 0, 0, 0});
map.emplace(propId.GetHex(), VotingInfo{0, 0, 0, 0, 0, 0});

auto entry = &map.find(propId.GetHex())->second;

if (voteString == "YES")
++entry->yesVotes;
++entry->votesYes;
else if (voteString == "NEUTRAL")
++entry->neutralVotes;
++entry->votesNeutral;
else if (voteString == "NO")
++entry->noVotes;
else
++entry->unknownVotes;
++entry->votesNo;

Jouzo marked this conversation as resolved.
Show resolved Hide resolved
++entry->totalVotes;
++entry->votesPresent;
}

/*
@@ -733,7 +726,7 @@ UniValue listgovproposalvotes(const JSONRPCRequest &request) {

UniValue ret(UniValue::VARR);

std::map<std::string, VoteAccounting> map;
std::map<std::string, VotingInfo> map;

view.ForEachProposalVote(
[&](const CProposalId &pId, uint8_t propCycle, const uint256 &id, CProposalVoteType vote) {
@@ -794,11 +787,10 @@ UniValue listgovproposalvotes(const JSONRPCRequest &request) {
UniValue stats(UniValue::VOBJ);

stats.pushKV("proposalId", entry.first);
stats.pushKV("total", entry.second.totalVotes);
stats.pushKV("yes", entry.second.yesVotes);
stats.pushKV("neutral", entry.second.neutralVotes);
stats.pushKV("no", entry.second.noVotes);
stats.pushKV("unknown", entry.second.unknownVotes);
stats.pushKV("total", entry.second.votesPresent);
stats.pushKV("yes", entry.second.votesYes);
stats.pushKV("neutral", entry.second.votesNeutral);
stats.pushKV("no", entry.second.votesNo);

ret.push_back(stats);
}