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 vote validity in listgovproposals and add validity filter #1740

Merged
merged 1 commit into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 49 additions & 4 deletions src/masternodes/rpc_proposals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,13 @@ UniValue proposalToJSON(const CProposalId &propId,
return ret;
}

UniValue proposalVoteToJSON(const CProposalId &propId, uint8_t cycle, const uint256 &mnId, CProposalVoteType vote) {
UniValue proposalVoteToJSON(const CProposalId &propId, uint8_t cycle, const uint256 &mnId, CProposalVoteType vote, bool valid) {
UniValue ret(UniValue::VOBJ);
ret.pushKV("proposalId", propId.GetHex());
ret.pushKV("masternodeId", mnId.GetHex());
ret.pushKV("cycle", int(cycle));
ret.pushKV("vote", CProposalVoteToString(vote));
ret.pushKV("valid", valid);
return ret;
}

Expand Down Expand Up @@ -618,6 +619,12 @@ UniValue listgovproposalvotes(const JSONRPCRequest &request) {
RPCArg::Type::BOOL,
RPCArg::Optional::OMITTED,
"0: return raw vote data, 1: return total votes by type"
},
{
"valid",
RPCArg::Type::BOOL,
RPCArg::Optional::OMITTED,
"0: show only invalid votes at current height, 1: show only valid votes at current height (default: 1)"
}
},
RPCResult{"{id:{...},...} (array) Json object with proposal vote information\n"},
Expand All @@ -628,7 +635,7 @@ UniValue listgovproposalvotes(const JSONRPCRequest &request) {
UniValue optionsObj(UniValue::VOBJ);

if (!request.params[0].isObject() && !optionsObj.read(request.params[0].getValStr()))
RPCTypeCheck(request.params, {UniValue::VSTR, UniValue::VSTR, UniValue::VNUM, UniValue::VOBJ, UniValue::VBOOL}, true);
RPCTypeCheck(request.params, {UniValue::VSTR, UniValue::VSTR, UniValue::VNUM, UniValue::VOBJ, UniValue::VBOOL, UniValue::VBOOL}, true);
else if (request.params[0].isObject())
optionsObj = request.params[0].get_obj();

Expand All @@ -641,6 +648,7 @@ UniValue listgovproposalvotes(const JSONRPCRequest &request) {
int8_t inputCycle{0};
bool aggregate = true;
bool latestOnly = true;
bool validOnly = true;

size_t limit = 100;
size_t start = 0;
Expand Down Expand Up @@ -690,6 +698,10 @@ UniValue listgovproposalvotes(const JSONRPCRequest &request) {
aggregate = optionsObj["aggregate"].getBool();
}

if (!optionsObj["valid"].isNull()) {
validOnly = optionsObj["valid"].getBool();
}

if (limit == 0) {
limit = std::numeric_limits<decltype(limit)>::max();
}
Expand Down Expand Up @@ -737,6 +749,10 @@ UniValue listgovproposalvotes(const JSONRPCRequest &request) {
aggregate = request.params[4].getBool();
}

if (request.params.size() > 5) {
validOnly = request.params[5].getBool();
}

if (limit == 0) {
limit = std::numeric_limits<decltype(limit)>::max();
}
Expand Down Expand Up @@ -777,12 +793,30 @@ UniValue listgovproposalvotes(const JSONRPCRequest &request) {
if (aggregate && latestOnly && propCycle != view.GetProposal(pId)->cycle)
return true;

int targetHeight;
auto prop = view.GetProposal(propId);
if (prop->status == CProposalStatusType::Voting) {
targetHeight = view.GetLastHeight() + 1;
} else {
targetHeight = prop->cycleEndHeight;
}

if (isMine) {
auto node = view.GetMasternode(id);
if (!node) {
return true;
}

bool valid = true;
if (!node->IsActive(targetHeight, view) || !node->mintedBlocks) {
valid = false;
}

if (validOnly && !valid)
return true;
if (!validOnly && valid)
return true;

// skip entries until we reach start index
if (!aggregate && start != 0) {
--start;
Expand All @@ -793,7 +827,7 @@ UniValue listgovproposalvotes(const JSONRPCRequest &request) {
: CTxDestination(WitnessV0KeyHash(node->ownerAuthAddress));
if (::IsMineCached(*pwallet, GetScriptForDestination(ownerDest))) {
if (!aggregate) {
ret.push_back(proposalVoteToJSON(propId, propCycle, id, vote));
ret.push_back(proposalVoteToJSON(propId, propCycle, id, vote, valid));
limit--;
} else {
proposalVoteAccounting(vote, pId, map);
Expand All @@ -806,8 +840,19 @@ UniValue listgovproposalvotes(const JSONRPCRequest &request) {
return true;
}

bool valid = true;
auto node = view.GetMasternode(id);
if (!node->IsActive(targetHeight, view) || !node->mintedBlocks) {
valid = false;
}

if (validOnly && !valid)
return true;
if (!validOnly && valid)
return true;

if (!aggregate) {
ret.push_back(proposalVoteToJSON(propId, propCycle, id, vote));
ret.push_back(proposalVoteToJSON(propId, propCycle, id, vote, valid));
limit--;
} else {
proposalVoteAccounting(vote, pId, map);
Expand Down
35 changes: 35 additions & 0 deletions test/functional/feature_on_chain_government.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,7 @@ def run_test(self):
self.test_aggregation(propId)
self.test_default_cycles_fix()
self.aggregate_all_votes()
self.test_valid_votes()

def test_aggregation(self, propId):
"""
Expand Down Expand Up @@ -778,5 +779,39 @@ def aggregate_all_votes(self):
# proposals missing from entry must have 0 votes in the latest cycle
assert_equal(len(self.nodes[0].listgovproposalvotes(miss, "all", 0)), 0)

def test_valid_votes(self):
"""
Tests valid votes filter.
"""
tx1 = self.nodes[0].creategovcfp({"title": "1111",
"context": "<Git issue url>",
"amount": 50,
"cycles": 2,
"payoutAddress": self.nodes[0].getnewaddress()})
self.nodes[0].generate(1)
self.sync_blocks()

endHeight = self.nodes[0].getgovproposal(tx1)["cycleEndHeight"]
propId = self.nodes[0].getgovproposal(tx1)["proposalId"]

for mn in range(len(self.mns)):
self.nodes[mn].votegov(propId, self.mns[mn], "yes")
self.nodes[mn].generate(1)
self.sync_blocks()

self.nodes[2].resignmasternode(self.mns[2])
self.sync_mempools()
self.nodes[0].generate(5)
self.sync_blocks()

# move to next cycle
self.nodes[0].generate(endHeight + 1 - self.nodes[0].getblockcount())

validVotes = self.nodes[0].listgovproposalvotes(propId, "all", 1, {}, False, True)
invalidVotes = self.nodes[0].listgovproposalvotes(propId, "all", 1, {}, False, False)

assert(self.mns[2] not in [x["masternodeId"] for x in validVotes])
assert_equal(self.mns[2], invalidVotes[0]["masternodeId"])

if __name__ == '__main__':
OnChainGovernanceTest().main()