From e8febda3632be5b8a5882050f63d229ac334c27b Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Wed, 28 Feb 2018 00:01:45 +0300 Subject: [PATCH] RPC: refactor `spork` rpc --- src/rpc/client.cpp | 2 +- src/rpc/misc.cpp | 88 +++++++++++++++++++++++++++++----------------- 2 files changed, 57 insertions(+), 33 deletions(-) diff --git a/src/rpc/client.cpp b/src/rpc/client.cpp index eb5cc36506..418721ecfe 100644 --- a/src/rpc/client.cpp +++ b/src/rpc/client.cpp @@ -139,7 +139,7 @@ static const CRPCConvertParam vRPCConvertParams[] = { "setnetworkactive", 0, "state" }, { "getmempoolancestors", 1, "verbose" }, { "getmempooldescendants", 1, "verbose" }, - { "spork", 1, "datetime" }, + { "spork", 1, "value" }, { "voteraw", 1, "tx_index" }, { "voteraw", 5, "time" }, { "getblockhashes", 0, "high"}, diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index 097b3b997f..651b4d5535 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -224,27 +224,52 @@ public: */ UniValue spork(const JSONRPCRequest& request) { - if(request.params.size() == 1 && request.params[0].get_str() == "show"){ - UniValue ret(UniValue::VOBJ); - for(int nSporkID = SPORK_START; nSporkID <= SPORK_END; nSporkID++){ - if(sporkManager.GetSporkNameByID(nSporkID) != "Unknown") - ret.push_back(Pair(sporkManager.GetSporkNameByID(nSporkID), sporkManager.GetSporkValue(nSporkID))); + if (request.params.size() == 1) { + // basic mode, show info + std:: string strCommand = request.params[0].get_str(); + if (strCommand == "show") { + UniValue ret(UniValue::VOBJ); + for(int nSporkID = SPORK_START; nSporkID <= SPORK_END; nSporkID++){ + if(sporkManager.GetSporkNameByID(nSporkID) != "Unknown") + ret.push_back(Pair(sporkManager.GetSporkNameByID(nSporkID), sporkManager.GetSporkValue(nSporkID))); + } + return ret; + } else if(strCommand == "active"){ + UniValue ret(UniValue::VOBJ); + for(int nSporkID = SPORK_START; nSporkID <= SPORK_END; nSporkID++){ + if(sporkManager.GetSporkNameByID(nSporkID) != "Unknown") + ret.push_back(Pair(sporkManager.GetSporkNameByID(nSporkID), sporkManager.IsSporkActive(nSporkID))); + } + return ret; } - return ret; - } else if(request.params.size() == 1 && request.params[0].get_str() == "active"){ - UniValue ret(UniValue::VOBJ); - for(int nSporkID = SPORK_START; nSporkID <= SPORK_END; nSporkID++){ - if(sporkManager.GetSporkNameByID(nSporkID) != "Unknown") - ret.push_back(Pair(sporkManager.GetSporkNameByID(nSporkID), sporkManager.IsSporkActive(nSporkID))); - } - return ret; } -#ifdef ENABLE_WALLET - else if (request.params.size() == 2){ + + if (request.fHelp || request.params.size() != 2) { + // default help, for basic mode + throw std::runtime_error( + "spork \"command\"\n" + "\nShows information about current state of sporks\n" + "\nArguments:\n" + "1. \"command\" (string, required) 'show' to show all current spork values, 'active' to show which sporks are active\n" + "\nResult:\n" + "For 'show':\n" + "{\n" + " \"SPORK_NAME\" : spork_value, (number) The value of the specific spork with the name SPORK_NAME\n" + " ...\n" + "}\n" + "For 'active':\n" + "{\n" + " \"SPORK_NAME\" : true|false, (boolean) 'true' for time-based sporks if spork is active and 'false' otherwise\n" + " ...\n" + "}\n" + "\nExamples:\n" + + HelpExampleCli("spork", "show") + + HelpExampleRpc("spork", "\"show\"")); + } else { + // advanced mode, update spork values int nSporkID = sporkManager.GetSporkIDByName(request.params[0].get_str()); - if(nSporkID == -1){ - return "Invalid spork name"; - } + if(nSporkID == -1) + throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid spork name"); if (!g_connman) throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled"); @@ -257,22 +282,21 @@ UniValue spork(const JSONRPCRequest& request) sporkManager.ExecuteSpork(nSporkID, nValue); return "success"; } else { - return "failure"; + throw std::runtime_error( + "spork \"name\" value\n" + "\nUpdate the value of the specific spork. Requires \"-sporkkey\" to be set to sign the message.\n" + "\nArguments:\n" + "1. \"name\" (string, required) The name of the spork to update\n" + "2. value (number, required) The new desired value of the spork\n" + "\nResult:\n" + " result (string) \"success\" if spork value was updated or this help otherwise\n" + "\nExamples:\n" + + HelpExampleCli("spork", "SPORK_2_INSTANTSEND_ENABLED 4070908800") + + HelpExampleRpc("spork", "\"SPORK_2_INSTANTSEND_ENABLED\", 4070908800") + + HelpRequiringPassphrase()); } - } - throw std::runtime_error( - "spork []\n" - " is the corresponding spork name, or 'show' to show all current spork settings, 'active' to show which sporks are active\n" - " is a epoch datetime to enable or disable spork\n" - + HelpRequiringPassphrase()); -#else // ENABLE_WALLET - throw std::runtime_error( - "spork \n" - " is the corresponding spork name, or 'show' to show all current spork settings, 'active' to show which sporks are active\n"); -#endif // ENABLE_WALLET - } UniValue validateaddress(const JSONRPCRequest& request) @@ -1129,7 +1153,7 @@ static const CRPCCommand commands[] = /* Dash features */ { "dash", "mnsync", &mnsync, true, {} }, - { "dash", "spork", &spork, true, {"datetime"} }, + { "dash", "spork", &spork, true, {"value"} }, /* Not shown in help */ { "hidden", "setmocktime", &setmocktime, true, {"timestamp"}},