Fabcoin Core  0.16.2
P2P Digital Currency
client.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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.
5 
6 #include <rpc/client.h>
7 #include <rpc/protocol.h>
8 #include <util.h>
9 
10 #include <set>
11 #include <stdint.h>
12 
13 #include <boost/algorithm/string/case_conv.hpp> // for to_lower()
14 #include <univalue.h>
15 
17 {
18 public:
19  std::string methodName;
20  int paramIdx;
21  std::string paramName;
22 };
23 
30 static const CRPCConvertParam vRPCConvertParams[] =
31 {
32  { "setmocktime", 0, "timestamp" },
33  { "generate", 0, "nblocks" },
34  { "generate", 1, "maxtries" },
35  { "generatetoaddress", 0, "nblocks" },
36  { "generatetoaddress", 2, "maxtries" },
37  { "getnetworkhashps", 0, "nblocks" },
38  { "getnetworkhashps", 1, "height" },
39  { "getblocksubsidy", 0, "height" },
40  { "sendtoaddress", 1, "amount" },
41  { "sendtoaddress", 4, "subtractfeefromamount" },
42  { "sendtoaddress", 5 , "replaceable" },
43  { "sendtoaddress", 6 , "conf_target" },
44  { "sendtoaddress", 9, "changeToSender"},
45  { "settxfee", 0, "amount" },
46  { "getsubsidy", 0, "height" },
47  { "getreceivedbyaddress", 1, "minconf" },
48  { "getreceivedbyaccount", 1, "minconf" },
49  { "listreceivedbyaddress", 0, "minconf" },
50  { "listreceivedbyaddress", 1, "include_empty" },
51  { "listreceivedbyaddress", 2, "include_watchonly" },
52  { "listreceivedbyaccount", 0, "minconf" },
53  { "listreceivedbyaccount", 1, "include_empty" },
54  { "listreceivedbyaccount", 2, "include_watchonly" },
55  { "getbalance", 1, "minconf" },
56  { "getbalance", 2, "include_watchonly" },
57  { "getblockhash", 0, "height" },
58  { "waitforblockheight", 0, "height" },
59  { "waitforblockheight", 1, "timeout" },
60  { "waitforblock", 1, "timeout" },
61  { "waitfornewblock", 0, "timeout" },
62  { "move", 2, "amount" },
63  { "move", 3, "minconf" },
64  { "sendfrom", 2, "amount" },
65  { "sendfrom", 3, "minconf" },
66  { "listtransactions", 1, "count" },
67  { "listtransactions", 2, "skip" },
68  { "listtransactions", 3, "include_watchonly" },
69  { "listaccounts", 0, "minconf" },
70  { "listaccounts", 1, "include_watchonly" },
71  { "walletpassphrase", 1, "timeout" },
72  { "walletpassphrase", 2, "stakingonly" },
73  { "getblocktemplate", 0, "template_request" },
74  { "listsinceblock", 1, "target_confirmations" },
75  { "listsinceblock", 2, "include_watchonly" },
76  { "listsinceblock", 3, "include_removed" },
77  { "sendmany", 1, "amounts" },
78  { "sendmany", 2, "minconf" },
79  { "sendmany", 4, "subtractfeefrom" },
80  { "sendmany", 5 , "replaceable" },
81  { "sendmany", 6 , "conf_target" },
82  { "sendmanywithdupes", 1, "amounts" },
83  { "sendmanywithdupes", 2, "minconf" },
84  { "sendmanywithdupes", 4, "subtractfeefrom" },
85  { "addmultisigaddress", 0, "nrequired" },
86  { "addmultisigaddress", 1, "keys" },
88  { "getaddresstxids", 0, "addresses"},
89  { "getaddressmempool", 0, "addresses"},
90  { "getaddressdeltas", 0, "addresses"},
91  { "getaddressbalance", 0, "addresses"},
92  { "getaddressutxos", 0, "addresses"},
93  { "getblockhashes", 0, "high"},
94  { "getblockhashes", 1, "low"},
95  { "getblockhashes", 2, "options"},
96  { "getspentinfo", 0, "argument"},
97  { "searchlogs", 0, "fromBlock"},
98  { "searchlogs", 1, "toBlock"},
99  { "searchlogs", 2, "address"},
100  { "searchlogs", 3, "topics"},
101  { "waitforlogs", 0, "fromBlock"},
102  { "waitforlogs", 1, "txlimit"},
103  { "waitforlogs", 2, "address"},
104  { "waitforlogs", 3, "topics"},
106  { "createmultisig", 0, "nrequired" },
107  { "createmultisig", 1, "keys" },
108  { "listunspent", 0, "minconf" },
109  { "listunspent", 1, "maxconf" },
110  { "listunspent", 2, "addresses" },
111  { "listunspent", 3, "include_unsafe" },
112  { "listunspent", 4, "query_options" },
113  { "getblock", 1, "verbosity" },
114  { "getblock", 1, "verbose" },
115  { "getblock", 2, "legacy" },
116  { "getblock", 3, "no_contract" },
117  { "getblockheader", 1, "verbose" },
118  { "getblockheader", 2, "legacy" },
119  { "getblockheader", 3, "no_contract" },
120  { "getchaintxstats", 0, "nblocks" },
121  { "gettransaction", 1, "include_watchonly" },
122  { "gettransaction", 2, "waitconf" },
123  { "getrawtransaction", 1, "verbose" },
124  { "createrawtransaction", 0, "inputs" },
125  { "createrawtransaction", 1, "outputs" },
126  { "createrawtransaction", 2, "locktime" },
127  { "createrawtransaction", 3, "replaceable" },
128  { "signrawtransaction", 1, "prevtxs" },
129  { "signrawtransaction", 2, "privkeys" },
130  { "sendrawtransaction", 1, "allowhighfees" },
131  { "combinerawtransaction", 0, "txs" },
132  { "fundrawtransaction", 1, "options" },
133  { "gettxout", 1, "n" },
134  { "gettxout", 2, "include_mempool" },
135  { "gettxoutproof", 0, "txids" },
136  { "lockunspent", 0, "unlock" },
137  { "lockunspent", 1, "transactions" },
138  { "importprivkey", 2, "rescan" },
139  { "importaddress", 2, "rescan" },
140  { "importaddress", 3, "p2sh" },
141  { "importpubkey", 2, "rescan" },
142  { "importmulti", 0, "requests" },
143  { "importmulti", 1, "options" },
144  { "verifychain", 0, "checklevel" },
145  { "verifychain", 1, "nblocks" },
146  { "pruneblockchain", 0, "height" },
147  { "keypoolrefill", 0, "newsize" },
148  { "getrawmempool", 0, "verbose" },
149  { "estimatefee", 0, "nblocks" },
150  { "estimatepriority", 0, "nblocks" },
151  { "estimatesmartpriority", 0, "nblocks" },
152  { "estimatesmartfee", 0, "conf_target" },
153  { "estimaterawfee", 0, "conf_target" },
154  { "estimaterawfee", 1, "threshold" },
155  { "prioritisetransaction", 1, "dummy" },
156  { "prioritisetransaction", 2, "fee_delta" },
157  { "setban", 2, "bantime" },
158  { "setban", 3, "absolute" },
159  { "setnetworkactive", 0, "state" },
160  { "getmempoolancestors", 1, "verbose" },
161  { "getmempooldescendants", 1, "verbose" },
162  { "bumpfee", 1, "options" },
163  { "logging", 0, "include" },
164  { "logging", 1, "exclude" },
165  { "disconnectnode", 1, "nodeid" },
166  { "callcontract", 3, "gasLimit" },
167  { "createcontract", 1, "gasLimit" },
168  { "createcontract", 2, "gasPrice" },
169  { "createcontract", 4, "broadcast" },
170  { "createcontract", 5, "changeToSender" },
171  { "sendtocontract", 2, "amount" },
172  { "sendtocontract", 3, "gasLimit" },
173  { "sendtocontract", 4, "gasPrice" },
174  { "sendtocontract", 6, "broadcast" },
175  { "sendtocontract", 7, "changeToSender" },
176  { "reservebalance", 0, "reserve"},
177  { "reservebalance", 1, "amount"},
178  { "listcontracts", 0, "start" },
179  { "listcontracts", 1, "maxDisplay" },
180  { "getstorage", 2, "index" },
181  { "getstorage", 1, "blockNum" },
182  { "logging", 0, "include" },
183  { "logging", 1, "exclude" },
184  { "disconnectnode", 1, "nodeid" },
185  // Echo with conversion (For testing only)
186  { "echojson", 0, "arg0" },
187  { "echojson", 1, "arg1" },
188  { "echojson", 2, "arg2" },
189  { "echojson", 3, "arg3" },
190  { "echojson", 4, "arg4" },
191  { "echojson", 5, "arg5" },
192  { "echojson", 6, "arg6" },
193  { "echojson", 7, "arg7" },
194  { "echojson", 8, "arg8" },
195  { "echojson", 9, "arg9" },
196  { "setgenerate", 0, "generate"},
197  { "setgenerate", 1, "genproclimit" },
198  { "getgenerate", 0, "generate"},
199 };
200 
202 {
203 private:
204  std::set<std::pair<std::string, int>> members;
205  std::set<std::pair<std::string, std::string>> membersByName;
206 
207 public:
209 
210  bool convert(const std::string& method, int idx) {
211  return (members.count(std::make_pair(method, idx)) > 0);
212  }
213  bool convert(const std::string& method, const std::string& name) {
214  return (membersByName.count(std::make_pair(method, name)) > 0);
215  }
216 };
217 
219 {
220  const unsigned int n_elem =
221  (sizeof(vRPCConvertParams) / sizeof(vRPCConvertParams[0]));
222 
223  for (unsigned int i = 0; i < n_elem; i++) {
224  members.insert(std::make_pair(vRPCConvertParams[i].methodName,
225  vRPCConvertParams[i].paramIdx));
226  membersByName.insert(std::make_pair(vRPCConvertParams[i].methodName,
227  vRPCConvertParams[i].paramName));
228  }
229 }
230 
231 static CRPCConvertTable rpcCvtTable;
232 
236 UniValue ParseNonRFCJSONValue(const std::string& strVal)
237 {
238  UniValue jVal;
239  if (!jVal.read(std::string("[")+strVal+std::string("]")) ||
240  !jVal.isArray() || jVal.size()!=1)
241  throw std::runtime_error(std::string("Error parsing JSON:")+strVal);
242  return jVal[0];
243 }
244 
245 UniValue RPCConvertValues(const std::string &strMethod, const std::vector<std::string> &strParams)
246 {
247  UniValue params(UniValue::VARR);
248 
249  for (unsigned int idx = 0; idx < strParams.size(); idx++) {
250  const std::string& strVal = strParams[idx];
251 
252  if (!rpcCvtTable.convert(strMethod, idx)) {
253  // insert string value directly
254  params.push_back(strVal);
255  } else {
256  // parse string as JSON, insert bool/number/object/etc. value
257  params.push_back(ParseNonRFCJSONValue(strVal));
258  }
259  }
260 
261  return params;
262 }
263 
264 UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector<std::string> &strParams)
265 {
266  UniValue params(UniValue::VOBJ);
267 
268  for (const std::string &s: strParams) {
269  size_t pos = s.find("=");
270  if (pos == std::string::npos) {
271  throw(std::runtime_error("No '=' in named argument '"+s+"', this needs to be present for every argument (even if it is empty)"));
272  }
273 
274  std::string name = s.substr(0, pos);
275  std::string value = s.substr(pos+1);
276 
277  if (!rpcCvtTable.convert(strMethod, name)) {
278  // insert string value directly
279  params.pushKV(name, value);
280  } else {
281  // parse string as JSON, insert bool/number/object/etc. value
282  params.pushKV(name, ParseNonRFCJSONValue(value));
283  }
284  }
285 
286  return params;
287 }
size_t size() const
Definition: univalue.h:70
bool read(const char *raw, size_t len)
bool isArray() const
Definition: univalue.h:85
std::set< std::pair< std::string, int > > members
Definition: client.cpp:204
UniValue RPCConvertValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert positional arguments to command-specific RPC representation.
Definition: client.cpp:245
bool push_back(const UniValue &val)
Definition: univalue.cpp:110
UniValue ParseNonRFCJSONValue(const std::string &strVal)
Non-RFC4627 JSON parser, accepts internal values (such as numbers, true, false, null) as well as obje...
Definition: client.cpp:236
const char * name
Definition: rest.cpp:36
bool convert(const std::string &method, const std::string &name)
Definition: client.cpp:213
std::string paramName
parameter name
Definition: client.cpp:21
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:135
bool convert(const std::string &method, int idx)
Definition: client.cpp:210
std::string methodName
method whose params want conversion
Definition: client.cpp:19
UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert named arguments to command-specific RPC representation.
Definition: client.cpp:264
std::set< std::pair< std::string, std::string > > membersByName
Definition: client.cpp:205
int paramIdx
0-based idx of param to convert
Definition: client.cpp:20