Skip to content

Commit

Permalink
Add more voting info in getgovproposal rpc (#1729)
Browse files Browse the repository at this point in the history
* Add more info in getgovproposal rpc

* Change key name

* Fix format

Co-authored-by: Shoham Chakraborty <shhmchk@gmail.com>

---------

Co-authored-by: Shoham Chakraborty <shhmchk@gmail.com>
  • Loading branch information
Mixa84 and shohamc1 authored Feb 6, 2023
1 parent 50d877a commit 1ebcda0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
44 changes: 31 additions & 13 deletions src/masternodes/rpc_proposals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -45,13 +48,19 @@ UniValue proposalToJSON(const CProposalId &propId,
auto votesPresentPct = -1;
auto votesYes = -1;
auto votesYesPct = -1;
auto votesNo = -1;
auto votesNeutral = -1;
auto votesInvalid = -1;
std::string votesPresentPctString = "-1";
std::string votesYesPctString = "-1";

auto isVotingInfoAvailable = votingInfo.has_value();
if (isVotingInfoAvailable) {
votesPresent = votingInfo->votesPresent;
votesYes = votingInfo->votesYes;
votesNo = votingInfo->votesNo;
votesNeutral = votingInfo->votesNeutral;
votesInvalid = votingInfo->votesInvalid;
votesPossible = votingInfo->votesPossible;

votesPresentPct = lround(votesPresent * 10000.f / votesPossible);
Expand Down Expand Up @@ -80,14 +89,19 @@ UniValue proposalToJSON(const CProposalId &propId,
ret.pushKV("proposalEndHeight", proposalEndHeight);
ret.pushKV("votingPeriod", votingPeriod);
ret.pushKV("quorum", quorumString);
ret.pushKV("approvalThreshold", approvalThresholdString);
if (isVotingInfoAvailable) {
ret.pushKV("votesPossible", votesPossible);
ret.pushKV("votesPresent", votesPresent);
ret.pushKV("votesPresentPct", votesPresentPctString);
ret.pushKV("votesYes", votesYes);
ret.pushKV("votesYesPct", votesYesPctString);
ret.pushKV("votesNo", votesNo);
ret.pushKV("votesNeutral", votesNeutral);
ret.pushKV("votesInvalid", votesInvalid);
ret.pushKV("feeRedistributionPerVote", ValueFromAmount(DivideAmounts(prop.fee - prop.feeBurnAmount, votesPresent * COIN)));
ret.pushKV("feeRedistributionTotal", ValueFromAmount(MultiplyAmounts(DivideAmounts(prop.fee - prop.feeBurnAmount, votesPresent * COIN), votesPresent * COIN)));
}
ret.pushKV("approvalThreshold", approvalThresholdString);
ret.pushKV("fee", feeTotalValue);
// ret.pushKV("feeBurn", feeBurnValue);
if (prop.options) {
Expand Down Expand Up @@ -768,31 +782,35 @@ UniValue getgovproposal(const JSONRPCRequest &request) {
return proposalToJSON(propId, *prop, view, std::nullopt);
}

uint32_t voteYes = 0, voters = 0;
VotingInfo info;
info.votesPossible = activeMasternodes.size();

view.ForEachProposalVote(
[&](const CProposalId &pId, uint8_t cycle, const uint256 &mnId, CProposalVoteType vote) {
if (pId != propId || cycle != prop->cycle) {
return false;
}
if (activeMasternodes.count(mnId)) {
++voters;
++info.votesPresent;
if (vote == CProposalVoteType::VoteYes) {
++voteYes;
++info.votesYes;
} else if (vote == CProposalVoteType::VoteNo) {
++info.votesNo;
} else if (vote == CProposalVoteType::VoteNeutral) {
++info.votesNeutral;
}
}
else
++info.votesInvalid;

return true;
},
CMnVotePerCycle{propId, prop->cycle});

if (!voters) {
if (!info.votesPresent) {
return proposalToJSON(propId, *prop, view, std::nullopt);
}

VotingInfo info;
info.votesPossible = activeMasternodes.size();
info.votesPresent = voters;
info.votesYes = voteYes;

return proposalToJSON(propId, *prop, view, info);
}

Expand Down
10 changes: 10 additions & 0 deletions test/functional/feature_on_chain_government.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@ def run_test(self):
assert_equal(result["votesPresentPct"], "100.00%")
assert_equal(result["votesYes"], Decimal("3"))
assert_equal(result["votesYesPct"], "75.00%")
assert_equal(result["votesNo"], Decimal("1"))
assert_equal(result["votesNeutral"], Decimal("0"))
assert_equal(result["votesInvalid"], Decimal("0"))
assert_equal(result["feeRedistributionPerVote"], Decimal("0.625"))
assert_equal(result["feeRedistributionTotal"], Decimal("2.5"))
assert_equal(result["approvalThreshold"], "66.67%")
assert_equal(result["fee"], Decimal("5"))

Expand Down Expand Up @@ -544,6 +549,11 @@ def run_test(self):
assert_equal(result["votesPresentPct"], "100.00%")
assert_equal(result["votesYes"], Decimal("2"))
assert_equal(result["votesYesPct"], "50.00%")
assert_equal(result["votesNo"], Decimal("2"))
assert_equal(result["votesNeutral"], Decimal("0"))
assert_equal(result["votesInvalid"], Decimal("0"))
assert_equal(result["feeRedistributionPerVote"], Decimal("2.5"))
assert_equal(result["feeRedistributionTotal"], Decimal("10"))
assert_equal(result["approvalThreshold"], "50.00%")
assert_equal(result["fee"], Decimal("20"))
assert_equal(result["options"], ["emergency"])
Expand Down

0 comments on commit 1ebcda0

Please sign in to comment.