Remove unreachable or otherwise redundant code
[bitcoinplatinum.git] / src / rpc / client.cpp
blobc5585a9fbacd04ecd4d811e0251862ecde49a40f
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #include "rpc/client.h"
7 #include "rpc/protocol.h"
8 #include "util.h"
10 #include <set>
11 #include <stdint.h>
13 #include <univalue.h>
15 class CRPCConvertParam
17 public:
18 std::string methodName; //!< method whose params want conversion
19 int paramIdx; //!< 0-based idx of param to convert
20 std::string paramName; //!< parameter name
23 /**
24 * Specify a (method, idx, name) here if the argument is a non-string RPC
25 * argument and needs to be converted from JSON.
27 * @note Parameter indexes start from 0.
29 static const CRPCConvertParam vRPCConvertParams[] =
31 { "setmocktime", 0, "timestamp" },
32 { "generate", 0, "nblocks" },
33 { "generate", 1, "maxtries" },
34 { "generatetoaddress", 0, "nblocks" },
35 { "generatetoaddress", 2, "maxtries" },
36 { "getnetworkhashps", 0, "nblocks" },
37 { "getnetworkhashps", 1, "height" },
38 { "sendtoaddress", 1, "amount" },
39 { "sendtoaddress", 4, "subtractfeefromamount" },
40 { "settxfee", 0, "amount" },
41 { "getreceivedbyaddress", 1, "minconf" },
42 { "getreceivedbyaccount", 1, "minconf" },
43 { "listreceivedbyaddress", 0, "minconf" },
44 { "listreceivedbyaddress", 1, "include_empty" },
45 { "listreceivedbyaddress", 2, "include_watchonly" },
46 { "listreceivedbyaccount", 0, "minconf" },
47 { "listreceivedbyaccount", 1, "include_empty" },
48 { "listreceivedbyaccount", 2, "include_watchonly" },
49 { "getbalance", 1, "minconf" },
50 { "getbalance", 2, "include_watchonly" },
51 { "getblockhash", 0, "height" },
52 { "waitforblockheight", 0, "height" },
53 { "waitforblockheight", 1, "timeout" },
54 { "waitforblock", 1, "timeout" },
55 { "waitfornewblock", 0, "timeout" },
56 { "move", 2, "amount" },
57 { "move", 3, "minconf" },
58 { "sendfrom", 2, "amount" },
59 { "sendfrom", 3, "minconf" },
60 { "listtransactions", 1, "count" },
61 { "listtransactions", 2, "skip" },
62 { "listtransactions", 3, "include_watchonly" },
63 { "listaccounts", 0, "minconf" },
64 { "listaccounts", 1, "include_watchonly" },
65 { "walletpassphrase", 1, "timeout" },
66 { "getblocktemplate", 0, "template_request" },
67 { "listsinceblock", 1, "target_confirmations" },
68 { "listsinceblock", 2, "include_watchonly" },
69 { "sendmany", 1, "amounts" },
70 { "sendmany", 2, "minconf" },
71 { "sendmany", 4, "subtractfeefrom" },
72 { "addmultisigaddress", 0, "nrequired" },
73 { "addmultisigaddress", 1, "keys" },
74 { "createmultisig", 0, "nrequired" },
75 { "createmultisig", 1, "keys" },
76 { "listunspent", 0, "minconf" },
77 { "listunspent", 1, "maxconf" },
78 { "listunspent", 2, "addresses" },
79 { "listunspent", 4, "query_options" },
80 { "getblock", 1, "verbosity" },
81 { "getblockheader", 1, "verbose" },
82 { "getchaintxstats", 0, "nblocks" },
83 { "gettransaction", 1, "include_watchonly" },
84 { "getrawtransaction", 1, "verbose" },
85 { "createrawtransaction", 0, "inputs" },
86 { "createrawtransaction", 1, "outputs" },
87 { "createrawtransaction", 2, "locktime" },
88 { "createrawtransaction", 3, "optintorbf" },
89 { "signrawtransaction", 1, "prevtxs" },
90 { "signrawtransaction", 2, "privkeys" },
91 { "sendrawtransaction", 1, "allowhighfees" },
92 { "fundrawtransaction", 1, "options" },
93 { "gettxout", 1, "n" },
94 { "gettxout", 2, "include_mempool" },
95 { "gettxoutproof", 0, "txids" },
96 { "lockunspent", 0, "unlock" },
97 { "lockunspent", 1, "transactions" },
98 { "importprivkey", 2, "rescan" },
99 { "importaddress", 2, "rescan" },
100 { "importaddress", 3, "p2sh" },
101 { "importpubkey", 2, "rescan" },
102 { "importmulti", 0, "requests" },
103 { "importmulti", 1, "options" },
104 { "verifychain", 0, "checklevel" },
105 { "verifychain", 1, "nblocks" },
106 { "pruneblockchain", 0, "height" },
107 { "keypoolrefill", 0, "newsize" },
108 { "getrawmempool", 0, "verbose" },
109 { "estimatefee", 0, "nblocks" },
110 { "estimatesmartfee", 0, "nblocks" },
111 { "estimatesmartfee", 1, "conservative" },
112 { "estimaterawfee", 0, "nblocks" },
113 { "estimaterawfee", 1, "threshold" },
114 { "estimaterawfee", 2, "horizon" },
115 { "prioritisetransaction", 1, "dummy" },
116 { "prioritisetransaction", 2, "fee_delta" },
117 { "setban", 2, "bantime" },
118 { "setban", 3, "absolute" },
119 { "setnetworkactive", 0, "state" },
120 { "getmempoolancestors", 1, "verbose" },
121 { "getmempooldescendants", 1, "verbose" },
122 { "bumpfee", 1, "options" },
123 { "logging", 0, "include" },
124 { "logging", 1, "exclude" },
125 { "disconnectnode", 1, "nodeid" },
126 // Echo with conversion (For testing only)
127 { "echojson", 0, "arg0" },
128 { "echojson", 1, "arg1" },
129 { "echojson", 2, "arg2" },
130 { "echojson", 3, "arg3" },
131 { "echojson", 4, "arg4" },
132 { "echojson", 5, "arg5" },
133 { "echojson", 6, "arg6" },
134 { "echojson", 7, "arg7" },
135 { "echojson", 8, "arg8" },
136 { "echojson", 9, "arg9" },
139 class CRPCConvertTable
141 private:
142 std::set<std::pair<std::string, int>> members;
143 std::set<std::pair<std::string, std::string>> membersByName;
145 public:
146 CRPCConvertTable();
148 bool convert(const std::string& method, int idx) {
149 return (members.count(std::make_pair(method, idx)) > 0);
151 bool convert(const std::string& method, const std::string& name) {
152 return (membersByName.count(std::make_pair(method, name)) > 0);
156 CRPCConvertTable::CRPCConvertTable()
158 const unsigned int n_elem =
159 (sizeof(vRPCConvertParams) / sizeof(vRPCConvertParams[0]));
161 for (unsigned int i = 0; i < n_elem; i++) {
162 members.insert(std::make_pair(vRPCConvertParams[i].methodName,
163 vRPCConvertParams[i].paramIdx));
164 membersByName.insert(std::make_pair(vRPCConvertParams[i].methodName,
165 vRPCConvertParams[i].paramName));
169 static CRPCConvertTable rpcCvtTable;
171 /** Non-RFC4627 JSON parser, accepts internal values (such as numbers, true, false, null)
172 * as well as objects and arrays.
174 UniValue ParseNonRFCJSONValue(const std::string& strVal)
176 UniValue jVal;
177 if (!jVal.read(std::string("[")+strVal+std::string("]")) ||
178 !jVal.isArray() || jVal.size()!=1)
179 throw std::runtime_error(std::string("Error parsing JSON:")+strVal);
180 return jVal[0];
183 UniValue RPCConvertValues(const std::string &strMethod, const std::vector<std::string> &strParams)
185 UniValue params(UniValue::VARR);
187 for (unsigned int idx = 0; idx < strParams.size(); idx++) {
188 const std::string& strVal = strParams[idx];
190 if (!rpcCvtTable.convert(strMethod, idx)) {
191 // insert string value directly
192 params.push_back(strVal);
193 } else {
194 // parse string as JSON, insert bool/number/object/etc. value
195 params.push_back(ParseNonRFCJSONValue(strVal));
199 return params;
202 UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector<std::string> &strParams)
204 UniValue params(UniValue::VOBJ);
206 for (const std::string &s: strParams) {
207 size_t pos = s.find("=");
208 if (pos == std::string::npos) {
209 throw(std::runtime_error("No '=' in named argument '"+s+"', this needs to be present for every argument (even if it is empty)"));
212 std::string name = s.substr(0, pos);
213 std::string value = s.substr(pos+1);
215 if (!rpcCvtTable.convert(strMethod, name)) {
216 // insert string value directly
217 params.pushKV(name, value);
218 } else {
219 // parse string as JSON, insert bool/number/object/etc. value
220 params.pushKV(name, ParseNonRFCJSONValue(value));
224 return params;