From 30da3f56a7153ddd7ab8f463dc6d2d88b21bed7f Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Mon, 12 Dec 2016 05:44:46 +0400 Subject: [PATCH] gobject rpc changes: (#1201) - added description for 'deserialize' - added 'type' filter to 'list' - added 'count' command (changed CGovernanceManager::ToString to output a bit more detailed info) --- src/governance.cpp | 34 +++++++++++++++++++++++++++++----- src/rpcgovernance.cpp | 31 ++++++++++++++++++++++++------- 2 files changed, 53 insertions(+), 12 deletions(-) diff --git a/src/governance.cpp b/src/governance.cpp index 0192b5f90..6ba4684e3 100644 --- a/src/governance.cpp +++ b/src/governance.cpp @@ -989,13 +989,37 @@ void CGovernanceManager::InitOnLoad() std::string CGovernanceManager::ToString() const { - std::ostringstream info; + LOCK(cs); - info << "Governance Objects: " << (int)mapObjects.size() << - " (Seen: " << (int)mapSeenGovernanceObjects.size() << - "), Vote Count: " << (int)mapVoteToObject.GetSize(); + int nProposalCount = 0; + int nTriggerCount = 0; + int nWatchdogCount = 0; + int nOtherCount = 0; - return info.str(); + object_m_cit it = mapObjects.begin(); + + while(it != mapObjects.end()) { + switch(it->second.GetObjectType()) { + case GOVERNANCE_OBJECT_PROPOSAL: + nProposalCount++; + break; + case GOVERNANCE_OBJECT_TRIGGER: + nTriggerCount++; + break; + case GOVERNANCE_OBJECT_WATCHDOG: + nWatchdogCount++; + break; + default: + nOtherCount++; + break; + } + ++it; + } + + return strprintf("Governance Objects: %d (Proposals: %d, Triggers: %d, Watchdogs: %d, Other: %d; Seen: %d), Votes: %d", + (int)mapObjects.size(), + nProposalCount, nTriggerCount, nWatchdogCount, nOtherCount, (int)mapSeenGovernanceObjects.size(), + (int)mapVoteToObject.GetSize()); } void CGovernanceManager::UpdatedBlockTip(const CBlockIndex *pindex) diff --git a/src/rpcgovernance.cpp b/src/rpcgovernance.cpp index 90aec240c..53efcb4cf 100644 --- a/src/rpcgovernance.cpp +++ b/src/rpcgovernance.cpp @@ -35,18 +35,20 @@ UniValue gobject(const UniValue& params, bool fHelp) strCommand = params[0].get_str(); if (fHelp || - (strCommand != "vote-many" && strCommand != "vote-conf" && strCommand != "vote-alias" && strCommand != "prepare" && strCommand != "submit" && - strCommand != "vote" && strCommand != "get" && strCommand != "getvotes" && strCommand != "getcurrentvotes" && strCommand != "list" && strCommand != "diff" && strCommand != "deserialize")) + (strCommand != "vote-many" && strCommand != "vote-conf" && strCommand != "vote-alias" && strCommand != "prepare" && strCommand != "submit" && strCommand != "count" && + strCommand != "deserialize" && strCommand != "get" && strCommand != "getvotes" && strCommand != "getcurrentvotes" && strCommand != "list" && strCommand != "diff")) throw std::runtime_error( "gobject \"command\"...\n" "Manage governance objects\n" "\nAvailable commands:\n" " prepare - Prepare governance object by signing and creating tx\n" " submit - Submit governance object to network\n" + " deserialize - Deserialize governance object from hex string to JSON\n" + " count - Count governance objects and votes\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 all governance objects\n" + " list - List governance objects (can be filtered by validity and/or object type)\n" " diff - List differences since last diff\n" " vote-alias - Vote on a governance object by masternode alias (using masternode.conf setup)\n" " vote-conf - Vote on a governance object by masternode configured in dash.conf\n" @@ -54,6 +56,8 @@ UniValue gobject(const UniValue& params, bool fHelp) ); + if(strCommand == "count") + return governance.ToString(); /* ------ Example Governance Item ------ @@ -63,6 +67,10 @@ UniValue gobject(const UniValue& params, bool fHelp) // DEBUG : TEST DESERIALIZATION OF GOVERNANCE META DATA if(strCommand == "deserialize") { + if (params.size() != 2) { + throw JSONRPCError(RPC_INVALID_PARAMETER, "Correct usage is 'gobject deserialize '"); + } + std::string strHex = params[1].get_str(); std::vector v = ParseHex(strHex); @@ -539,14 +547,20 @@ UniValue gobject(const UniValue& params, bool fHelp) // USERS CAN QUERY THE SYSTEM FOR A LIST OF VARIOUS GOVERNANCE ITEMS if(strCommand == "list" || strCommand == "diff") { - if (params.size() > 2) - throw JSONRPCError(RPC_INVALID_PARAMETER, "Correct usage is 'gobject [list|diff] [valid]'"); + if (params.size() > 3) + throw JSONRPCError(RPC_INVALID_PARAMETER, "Correct usage is 'gobject [list|diff] [valid] [type]'"); // GET MAIN PARAMETER FOR THIS MODE, VALID OR ALL? std::string strShow = "valid"; if (params.size() == 2) strShow = params[1].get_str(); - if (strShow != "valid" && strShow != "all") return "Invalid mode, should be valid or all"; + if (strShow != "valid" && strShow != "all") + return "Invalid mode, should be 'valid' or 'all'"; + + std::string strType = "all"; + if (params.size() == 3) strType = params[2].get_str(); + if (strType != "proposals" && strType != "triggers" && strType != "watchdogs" && strType != "all") + return "Invalid type, should be 'proposals', 'triggers', 'watchdogs' or 'all'"; // GET STARTING TIME TO QUERY SYSTEM WITH @@ -574,9 +588,12 @@ UniValue gobject(const UniValue& params, bool fHelp) BOOST_FOREACH(CGovernanceObject* pGovObj, objs) { - // IF WE HAVE A SPECIFIC NODE REQUESTED TO VOTE, DO THAT if(strShow == "valid" && !pGovObj->IsSetCachedValid()) continue; + if(strType == "proposals" && pGovObj->GetObjectType() != GOVERNANCE_OBJECT_PROPOSAL) continue; + if(strType == "triggers" && pGovObj->GetObjectType() != GOVERNANCE_OBJECT_TRIGGER) continue; + if(strType == "watchdogs" && pGovObj->GetObjectType() != GOVERNANCE_OBJECT_WATCHDOG) continue; + UniValue bObj(UniValue::VOBJ); bObj.push_back(Pair("DataHex", pGovObj->GetDataAsHex())); bObj.push_back(Pair("DataString", pGovObj->GetDataAsString()));