Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

client.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 The Bitcoin Core developers
3 // Copyright (c) 2014-2020 The Dash Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #include <rpc/client.h>
8 #include <rpc/protocol.h>
9 #include <util.h>
10 
11 #include <set>
12 #include <stdint.h>
13 
15 {
16 public:
17  std::string methodName;
18  int paramIdx;
19  std::string paramName;
20 };
21 
29 {
30  { "setmocktime", 0, "timestamp" },
31 #if ENABLE_MINER
32  { "generate", 0, "nblocks" },
33  { "generate", 1, "maxtries" },
34  { "generatetoaddress", 0, "nblocks" },
35  { "generatetoaddress", 2, "maxtries" },
36 #endif // ENABLE_MINER
37  { "getnetworkhashps", 0, "nblocks" },
38  { "getnetworkhashps", 1, "height" },
39  { "sendtoaddress", 1, "amount" },
40  { "sendtoaddress", 4, "subtractfeefromamount" },
41  { "sendtoaddress", 5, "use_is" },
42  { "sendtoaddress", 6, "use_ps" },
43  { "sendtoaddress", 7, "conf_target" },
44  { "settxfee", 0, "amount" },
45  { "getreceivedbyaddress", 1, "minconf" },
46  { "getreceivedbyaddress", 2, "addlocked" },
47  { "getreceivedbyaccount", 1, "minconf" },
48  { "getreceivedbyaccount", 2, "addlocked" },
49  { "listaddressbalances", 0, "minamount" },
50  { "listreceivedbyaddress", 0, "minconf" },
51  { "listreceivedbyaddress", 1, "addlocked" },
52  { "listreceivedbyaddress", 2, "include_empty" },
53  { "listreceivedbyaddress", 3, "include_watchonly" },
54  { "listreceivedbyaccount", 0, "minconf" },
55  { "listreceivedbyaccount", 1, "addlocked" },
56  { "listreceivedbyaccount", 2, "include_empty" },
57  { "listreceivedbyaccount", 3, "include_watchonly" },
58  { "getbalance", 1, "minconf" },
59  { "getbalance", 2, "addlocked" },
60  { "getbalance", 3, "include_watchonly" },
61  { "getchaintips", 0, "count" },
62  { "getchaintips", 1, "branchlen" },
63  { "getblockhash", 0, "height" },
64  { "getsuperblockbudget", 0, "index" },
65  { "waitforblockheight", 0, "height" },
66  { "waitforblockheight", 1, "timeout" },
67  { "waitforblock", 1, "timeout" },
68  { "waitfornewblock", 0, "timeout" },
69  { "move", 2, "amount" },
70  { "move", 3, "minconf" },
71  { "sendfrom", 2, "amount" },
72  { "sendfrom", 3, "minconf" },
73  { "sendfrom", 4, "addlocked" },
74  { "listtransactions", 1, "count" },
75  { "listtransactions", 2, "skip" },
76  { "listtransactions", 3, "include_watchonly" },
77  { "listaccounts", 0, "minconf" },
78  { "listaccounts", 1, "addlocked" },
79  { "listaccounts", 2, "include_watchonly" },
80  { "walletpassphrase", 1, "timeout" },
81  { "walletpassphrase", 2, "mixingonly" },
82  { "getblocktemplate", 0, "template_request" },
83  { "listsinceblock", 1, "target_confirmations" },
84  { "listsinceblock", 2, "include_watchonly" },
85  { "listsinceblock", 3, "include_removed" },
86  { "sendmany", 1, "amounts" },
87  { "sendmany", 2, "minconf" },
88  { "sendmany", 3, "addlocked" },
89  { "sendmany", 5, "subtractfeefrom" },
90  { "sendmany", 6, "use_is" },
91  { "sendmany", 7, "use_ps" },
92  { "sendmany", 8, "conf_target" },
93  { "addmultisigaddress", 0, "nrequired" },
94  { "addmultisigaddress", 1, "keys" },
95  { "createmultisig", 0, "nrequired" },
96  { "createmultisig", 1, "keys" },
97  { "listunspent", 0, "minconf" },
98  { "listunspent", 1, "maxconf" },
99  { "listunspent", 2, "addresses" },
100  { "listunspent", 3, "include_unsafe" },
101  { "listunspent", 4, "query_options" },
102  { "getblock", 1, "verbosity" },
103  { "getblock", 1, "verbose" },
104  { "getblockheader", 1, "verbose" },
105  { "getblockheaders", 1, "count" },
106  { "getblockheaders", 2, "verbose" },
107  { "getchaintxstats", 0, "nblocks" },
108  { "getmerkleblocks", 2, "count" },
109  { "gettransaction", 1, "include_watchonly" },
110  { "getrawtransaction", 1, "verbose" },
111  { "createrawtransaction", 0, "inputs" },
112  { "createrawtransaction", 1, "outputs" },
113  { "createrawtransaction", 2, "locktime" },
114  { "signrawtransaction", 1, "prevtxs" },
115  { "signrawtransaction", 2, "privkeys" },
116  { "sendrawtransaction", 1, "allowhighfees" },
117  { "sendrawtransaction", 2, "instantsend" },
118  { "sendrawtransaction", 3, "bypasslimits" },
119  { "combinerawtransaction", 0, "txs" },
120  { "fundrawtransaction", 1, "options" },
121  { "gettxout", 1, "n" },
122  { "gettxout", 2, "include_mempool" },
123  { "gettxoutproof", 0, "txids" },
124  { "lockunspent", 0, "unlock" },
125  { "lockunspent", 1, "transactions" },
126  { "importprivkey", 2, "rescan" },
127  { "importelectrumwallet", 1, "index" },
128  { "importaddress", 2, "rescan" },
129  { "importaddress", 3, "p2sh" },
130  { "importpubkey", 2, "rescan" },
131  { "importmulti", 0, "requests" },
132  { "importmulti", 1, "options" },
133  { "verifychain", 0, "checklevel" },
134  { "verifychain", 1, "nblocks" },
135  { "getblockstats", 0, "hash_or_height" },
136  { "getblockstats", 1, "stats" },
137  { "pruneblockchain", 0, "height" },
138  { "keypoolrefill", 0, "newsize" },
139  { "getrawmempool", 0, "verbose" },
140  { "estimatefee", 0, "nblocks" },
141  { "estimatesmartfee", 0, "conf_target" },
142  { "estimaterawfee", 0, "conf_target" },
143  { "estimaterawfee", 1, "threshold" },
144  { "prioritisetransaction", 1, "fee_delta" },
145  { "setban", 2, "bantime" },
146  { "setban", 3, "absolute" },
147  { "setnetworkactive", 0, "state" },
148  { "setprivatesendrounds", 0, "rounds" },
149  { "setprivatesendamount", 0, "amount" },
150  { "getmempoolancestors", 1, "verbose" },
151  { "getmempooldescendants", 1, "verbose" },
152  { "logging", 0, "include" },
153  { "logging", 1, "exclude" },
154  { "spork", 1, "value" },
155  { "voteraw", 1, "tx_index" },
156  { "voteraw", 5, "time" },
157  { "getblockhashes", 0, "high"},
158  { "getblockhashes", 1, "low" },
159  { "getspentinfo", 0, "json" },
160  { "getaddresstxids", 0, "addresses" },
161  { "getaddressbalance", 0, "addresses" },
162  { "getaddressdeltas", 0, "addresses" },
163  { "getaddressutxos", 0, "addresses" },
164  { "getaddressmempool", 0, "addresses" },
165  { "getspecialtxes", 1, "type" },
166  { "getspecialtxes", 2, "count" },
167  { "getspecialtxes", 3, "skip" },
168  { "getspecialtxes", 4, "verbosity" },
169  { "disconnectnode", 1, "nodeid" },
170  // Echo with conversion (For testing only)
171  { "echojson", 0, "arg0" },
172  { "echojson", 1, "arg1" },
173  { "echojson", 2, "arg2" },
174  { "echojson", 3, "arg3" },
175  { "echojson", 4, "arg4" },
176  { "echojson", 5, "arg5" },
177  { "echojson", 6, "arg6" },
178  { "echojson", 7, "arg7" },
179  { "echojson", 8, "arg8" },
180  { "echojson", 9, "arg9" },
181  { "rescanblockchain", 0, "start_height"},
182  { "rescanblockchain", 1, "stop_height"},
183  { "stop", 0, "wait" },
184 };
185 
187 {
188 private:
189  std::set<std::pair<std::string, int>> members;
190  std::set<std::pair<std::string, std::string>> membersByName;
191 
192 public:
194 
195  bool convert(const std::string& method, int idx) {
196  return (members.count(std::make_pair(method, idx)) > 0);
197  }
198  bool convert(const std::string& method, const std::string& name) {
199  return (membersByName.count(std::make_pair(method, name)) > 0);
200  }
201 };
202 
204 {
205  const unsigned int n_elem =
206  (sizeof(vRPCConvertParams) / sizeof(vRPCConvertParams[0]));
207 
208  for (unsigned int i = 0; i < n_elem; i++) {
209  members.insert(std::make_pair(vRPCConvertParams[i].methodName,
210  vRPCConvertParams[i].paramIdx));
211  membersByName.insert(std::make_pair(vRPCConvertParams[i].methodName,
212  vRPCConvertParams[i].paramName));
213  }
214 }
215 
217 
221 UniValue ParseNonRFCJSONValue(const std::string& strVal)
222 {
223  UniValue jVal;
224  if (!jVal.read(std::string("[")+strVal+std::string("]")) ||
225  !jVal.isArray() || jVal.size()!=1)
226  throw std::runtime_error(std::string("Error parsing JSON:")+strVal);
227  return jVal[0];
228 }
229 
230 UniValue RPCConvertValues(const std::string &strMethod, const std::vector<std::string> &strParams)
231 {
232  UniValue params(UniValue::VARR);
233 
234  for (unsigned int idx = 0; idx < strParams.size(); idx++) {
235  const std::string& strVal = strParams[idx];
236 
237  if (!rpcCvtTable.convert(strMethod, idx)) {
238  // insert string value directly
239  params.push_back(strVal);
240  } else {
241  // parse string as JSON, insert bool/number/object/etc. value
242  params.push_back(ParseNonRFCJSONValue(strVal));
243  }
244  }
245 
246  return params;
247 }
248 
249 UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector<std::string> &strParams)
250 {
251  UniValue params(UniValue::VOBJ);
252 
253  for (const std::string &s: strParams) {
254  size_t pos = s.find('=');
255  if (pos == std::string::npos) {
256  throw(std::runtime_error("No '=' in named argument '"+s+"', this needs to be present for every argument (even if it is empty)"));
257  }
258 
259  std::string name = s.substr(0, pos);
260  std::string value = s.substr(pos+1);
261 
262  if (!rpcCvtTable.convert(strMethod, name)) {
263  // insert string value directly
264  params.pushKV(name, value);
265  } else {
266  // parse string as JSON, insert bool/number/object/etc. value
267  params.pushKV(name, ParseNonRFCJSONValue(value));
268  }
269  }
270 
271  return params;
272 }
static CRPCConvertTable rpcCvtTable
Definition: client.cpp:216
bool read(const char *raw, size_t len)
std::set< std::pair< std::string, int > > members
Definition: client.cpp:189
UniValue RPCConvertValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert positional arguments to command-specific RPC representation.
Definition: client.cpp:230
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:221
const char * name
Definition: rest.cpp:36
bool convert(const std::string &method, const std::string &name)
Definition: client.cpp:198
std::string paramName
parameter name
Definition: client.cpp:19
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:135
bool convert(const std::string &method, int idx)
Definition: client.cpp:195
static const CRPCConvertParam vRPCConvertParams[]
Specify a (method, idx, name) here if the argument is a non-string RPC argument and needs to be conve...
Definition: client.cpp:28
std::string methodName
method whose params want conversion
Definition: client.cpp:17
UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert named arguments to command-specific RPC representation.
Definition: client.cpp:249
size_t size() const
Definition: univalue.h:69
std::set< std::pair< std::string, std::string > > membersByName
Definition: client.cpp:190
bool isArray() const
Definition: univalue.h:84
int paramIdx
0-based idx of param to convert
Definition: client.cpp:18
Released under the MIT license