Bitcoin ABC 0.34.2
P2P Digital Currency
client.cpp
Go to the documentation of this file.
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.
5
6#include <rpc/client.h>
7
8#include <common/args.h>
9#include <tinyformat.h>
10
11#include <cstdint>
12#include <set>
13#include <string>
14#include <string_view>
15
17public:
18 std::string methodName;
20 std::string paramName;
21};
22
30 {"setmocktime", 0, "timestamp"},
31 {"mockscheduler", 0, "delta_time"},
32 {"utxoupdatepsbt", 1, "descriptors"},
33 {"generatetoaddress", 0, "nblocks"},
34 {"generatetoaddress", 2, "maxtries"},
35 {"generatetodescriptor", 0, "num_blocks"},
36 {"generatetodescriptor", 2, "maxtries"},
37 {"generateblock", 1, "transactions"},
38 {"getnetworkhashps", 0, "nblocks"},
39 {"getnetworkhashps", 1, "height"},
40 {"sendtoaddress", 1, "amount"},
41 {"sendtoaddress", 4, "subtractfeefromamount"},
42 {"sendtoaddress", 5, "avoid_reuse"},
43 {"settxfee", 0, "amount"},
44 {"sethdseed", 0, "newkeypool"},
45 {"getreceivedbyaddress", 1, "minconf"},
46 {"getreceivedbylabel", 1, "minconf"},
47 {"listreceivedbyaddress", 0, "minconf"},
48 {"listreceivedbyaddress", 1, "include_empty"},
49 {"listreceivedbyaddress", 2, "include_watchonly"},
50 {"listreceivedbylabel", 0, "minconf"},
51 {"listreceivedbylabel", 1, "include_empty"},
52 {"listreceivedbylabel", 2, "include_watchonly"},
53 {"getbalance", 1, "minconf"},
54 {"getbalance", 2, "include_watchonly"},
55 {"getbalance", 3, "avoid_reuse"},
56 {"getblockfrompeer", 1, "peer_id"},
57 {"getblockhash", 0, "height"},
58 {"waitforblockheight", 0, "height"},
59 {"waitforblockheight", 1, "timeout"},
60 {"waitforblock", 1, "timeout"},
61 {"waitfornewblock", 0, "timeout"},
62 {"listtransactions", 1, "count"},
63 {"listtransactions", 2, "skip"},
64 {"listtransactions", 3, "include_watchonly"},
65 {"walletpassphrase", 1, "timeout"},
66 {"getblocktemplate", 0, "template_request"},
67 {"listsinceblock", 1, "target_confirmations"},
68 {"listsinceblock", 2, "include_watchonly"},
69 {"listsinceblock", 3, "include_removed"},
70 {"sendmany", 1, "amounts"},
71 {"sendmany", 2, "minconf"},
72 {"sendmany", 4, "subtractfeefrom"},
73 {"deriveaddresses", 1, "range"},
74 {"scantxoutset", 1, "scanobjects"},
75 {"addmultisigaddress", 0, "nrequired"},
76 {"addmultisigaddress", 1, "keys"},
77 {"createmultisig", 0, "nrequired"},
78 {"createmultisig", 1, "keys"},
79 {"listunspent", 0, "minconf"},
80 {"listunspent", 1, "maxconf"},
81 {"listunspent", 2, "addresses"},
82 {"listunspent", 3, "include_unsafe"},
83 {"listunspent", 4, "query_options"},
84 {"listunspent", 4, "minimumAmount"},
85 {"listunspent", 4, "maximumAmount"},
86 {"listunspent", 4, "maximumCount"},
87 {"listunspent", 4, "minimumSumAmount"},
88 {"getblock", 1, "verbosity"},
89 {"getblock", 1, "verbose"},
90 {"getblockheader", 1, "verbose"},
91 {"getchaintxstats", 0, "nblocks"},
92 {"gettransaction", 1, "include_watchonly"},
93 {"gettransaction", 2, "verbose"},
94 {"getrawtransaction", 1, "verbose"},
95 {"createrawtransaction", 0, "inputs"},
96 {"createrawtransaction", 1, "outputs"},
97 {"createrawtransaction", 2, "locktime"},
98 {"signrawtransactionwithkey", 1, "privkeys"},
99 {"signrawtransactionwithkey", 2, "prevtxs"},
100 {"signrawtransactionwithwallet", 1, "prevtxs"},
101 {"sendrawtransaction", 1, "maxfeerate"},
102 {"submitpackage", 0, "package"},
103 {"testmempoolaccept", 0, "rawtxs"},
104 {"testmempoolaccept", 1, "maxfeerate"},
105 {"combinerawtransaction", 0, "txs"},
106 {"fundrawtransaction", 1, "options"},
107 {"fundrawtransaction", 1, "add_inputs"},
108 {"fundrawtransaction", 1, "include_unsafe"},
109 {"fundrawtransaction", 1, "changePosition"},
110 {"fundrawtransaction", 1, "includeWatching"},
111 {"fundrawtransaction", 1, "lockUnspents"},
112 {"fundrawtransaction", 1, "feeRate"},
113 {"fundrawtransaction", 1, "subtractFeeFromOutputs"},
114 {"walletcreatefundedpsbt", 0, "inputs"},
115 {"walletcreatefundedpsbt", 1, "outputs"},
116 {"walletcreatefundedpsbt", 2, "locktime"},
117 {"walletcreatefundedpsbt", 3, "options"},
118 {"walletcreatefundedpsbt", 3, "add_inputs"},
119 {"walletcreatefundedpsbt", 3, "include_unsafe"},
120 {"walletcreatefundedpsbt", 3, "changePosition"},
121 {"walletcreatefundedpsbt", 3, "includeWatching"},
122 {"walletcreatefundedpsbt", 3, "lockUnspents"},
123 {"walletcreatefundedpsbt", 3, "feeRate"},
124 {"walletcreatefundedpsbt", 3, "subtractFeeFromOutputs"},
125 {"walletcreatefundedpsbt", 4, "bip32derivs"},
126 {"walletprocesspsbt", 1, "sign"},
127 {"walletprocesspsbt", 3, "bip32derivs"},
128 {"walletprocesspsbt", 4, "finalize"},
129 {"createpsbt", 0, "inputs"},
130 {"createpsbt", 1, "outputs"},
131 {"createpsbt", 2, "locktime"},
132 {"combinepsbt", 0, "txs"},
133 {"joinpsbts", 0, "txs"},
134 {"finalizepsbt", 1, "extract"},
135 {"converttopsbt", 1, "permitsigdata"},
136 {"gettxout", 1, "n"},
137 {"gettxout", 2, "include_mempool"},
138 {"gettxoutproof", 0, "txids"},
139 {"gettxoutsetinfo", 1, "hash_or_height"},
140 {"gettxoutsetinfo", 2, "use_index"},
141 {"dumptxoutset", 2, "options"},
142 {"dumptxoutset", 2, "rollback"},
143 {"lockunspent", 0, "unlock"},
144 {"lockunspent", 1, "transactions"},
145 {"send", 0, "outputs"},
146 {"send", 1, "options"},
147 {"send", 1, "add_inputs"},
148 {"send", 1, "include_unsafe"},
149 {"send", 1, "add_to_wallet"},
150 {"send", 1, "change_position"},
151 {"send", 1, "fee_rate"},
152 {"send", 1, "include_watching"},
153 {"send", 1, "inputs"},
154 {"send", 1, "locktime"},
155 {"send", 1, "lock_unspents"},
156 {"send", 1, "psbt"},
157 {"send", 1, "subtract_fee_from_outputs"},
158 {"importprivkey", 2, "rescan"},
159 {"importaddress", 2, "rescan"},
160 {"importaddress", 3, "p2sh"},
161 {"importpubkey", 2, "rescan"},
162 {"importmempool", 1, "options"},
163 {"importmempool", 1, "apply_fee_delta_priority"},
164 {"importmempool", 1, "use_current_time"},
165 {"importmempool", 1, "apply_unbroadcast_set"},
166 {"importmulti", 0, "requests"},
167 {"importmulti", 1, "options"},
168 {"importmulti", 1, "rescan"},
169 {"importdescriptors", 0, "requests"},
170 {"verifychain", 0, "checklevel"},
171 {"verifychain", 1, "nblocks"},
172 {"getblockstats", 0, "hash_or_height"},
173 {"getblockstats", 1, "stats"},
174 {"pruneblockchain", 0, "height"},
175 {"keypoolrefill", 0, "newsize"},
176 {"getrawmempool", 0, "verbose"},
177 {"getrawmempool", 1, "mempool_sequence"},
178 {"prioritisetransaction", 1, "dummy"},
179 {"prioritisetransaction", 2, "fee_delta"},
180 {"setban", 2, "bantime"},
181 {"setban", 3, "absolute"},
182 {"setnetworkactive", 0, "state"},
183 {"setwalletflag", 1, "value"},
184 {"getmempoolancestors", 1, "verbose"},
185 {"getmempooldescendants", 1, "verbose"},
186 {"disconnectnode", 1, "nodeid"},
187 {"logging", 0, "include"},
188 {"logging", 1, "exclude"},
189 {"upgradewallet", 0, "version"},
190 // Echo with conversion (For testing only)
191 {"echojson", 0, "arg0"},
192 {"echojson", 1, "arg1"},
193 {"echojson", 2, "arg2"},
194 {"echojson", 3, "arg3"},
195 {"echojson", 4, "arg4"},
196 {"echojson", 5, "arg5"},
197 {"echojson", 6, "arg6"},
198 {"echojson", 7, "arg7"},
199 {"echojson", 8, "arg8"},
200 {"echojson", 9, "arg9"},
201 {"rescanblockchain", 0, "start_height"},
202 {"rescanblockchain", 1, "stop_height"},
203 {"createwallet", 1, "disable_private_keys"},
204 {"createwallet", 2, "blank"},
205 {"createwallet", 4, "avoid_reuse"},
206 {"createwallet", 5, "descriptors"},
207 {"createwallet", 6, "load_on_startup"},
208 {"restorewallet", 2, "load_on_startup"},
209 {"loadwallet", 1, "load_on_startup"},
210 {"unloadwallet", 1, "load_on_startup"},
211 {"getnodeaddresses", 0, "count"},
212 {"addpeeraddress", 1, "port"},
213 {"addpeeraddress", 2, "tried"},
214 {"sendmsgtopeer", 0, "peer_id"},
215 {"stop", 0, "wait"},
216 {"createwallettransaction", 1, "amount"},
217 // Avalanche
218 {"addavalanchenode", 0, "nodeid"},
219 {"buildavalancheproof", 0, "sequence"},
220 {"buildavalancheproof", 1, "expiration"},
221 {"buildavalancheproof", 3, "stakes"},
222 {"getavailabilityscore", 0, "nodeid"},
223 {"getremoteproofs", 0, "nodeid"},
224 {"getstakingreward", 1, "recompute"},
225 {"setstakingreward", 2, "append"},
226 {"setflakyproof", 1, "flaky"},
227 {"getfinaltransactions", 0, "verbose"},
228};
229
234static UniValue Parse(std::string_view raw) {
235 UniValue parsed;
236 if (!parsed.read(raw)) {
237 throw std::runtime_error(tfm::format("Error parsing JSON: %s", raw));
238 }
239
240 return parsed;
241}
242
244private:
245 std::set<std::pair<std::string, int>> members;
246 std::set<std::pair<std::string, std::string>> membersByName;
247
248public:
250
255 UniValue ArgToUniValue(std::string_view arg_value,
256 const std::string &method, int param_idx) {
257 return members.count({method, param_idx}) > 0 ? Parse(arg_value)
258 : arg_value;
259 }
260
265 UniValue ArgToUniValue(std::string_view arg_value,
266 const std::string &method,
267 const std::string &param_name) {
268 return membersByName.count({method, param_name}) > 0 ? Parse(arg_value)
269 : arg_value;
270 }
271};
272
274 for (const auto &cp : vRPCConvertParams) {
275 members.emplace(cp.methodName, cp.paramIdx);
276 membersByName.emplace(cp.methodName, cp.paramName);
277 }
278}
279
281
282UniValue RPCConvertValues(const std::string &strMethod,
283 const std::vector<std::string> &strParams) {
284 UniValue params(UniValue::VARR);
285
286 for (unsigned int idx = 0; idx < strParams.size(); idx++) {
287 std::string_view value{strParams[idx]};
288 params.push_back(rpcCvtTable.ArgToUniValue(value, strMethod, idx));
289 }
290
291 return params;
292}
293
294UniValue RPCConvertNamedValues(const std::string &strMethod,
295 const std::vector<std::string> &strParams) {
296 UniValue params(UniValue::VOBJ);
297 UniValue positional_args{UniValue::VARR};
298
299 for (std::string_view s : strParams) {
300 size_t pos = s.find('=');
301 if (pos == std::string::npos) {
302 positional_args.push_back(rpcCvtTable.ArgToUniValue(
303 s, strMethod, positional_args.size()));
304 continue;
305 }
306
307 std::string name{s.substr(0, pos)};
308 std::string_view value{s.substr(pos + 1)};
309
310 // Intentionally overwrite earlier named values with later ones as a
311 // convenience for scripts and command line users that want to merge
312 // options.
313 params.pushKV(name, rpcCvtTable.ArgToUniValue(value, strMethod, name));
314 }
315
316 if (!positional_args.empty()) {
317 // Use pushKVEnd instead of pushKV to avoid overwriting an explicit
318 // "args" value with an implicit one. Let the RPC server handle the
319 // request as given.
320 params.pushKVEnd("args", std::move(positional_args));
321 }
322
323 return params;
324}
std::string methodName
method whose params want conversion
Definition: client.cpp:18
int paramIdx
0-based idx of param to convert
Definition: client.cpp:19
std::string paramName
parameter name
Definition: client.cpp:20
std::set< std::pair< std::string, std::string > > membersByName
Definition: client.cpp:246
UniValue ArgToUniValue(std::string_view arg_value, const std::string &method, int param_idx)
Return arg_value as UniValue, and first parse it if it is a non-string parameter.
Definition: client.cpp:255
std::set< std::pair< std::string, int > > members
Definition: client.cpp:245
UniValue ArgToUniValue(std::string_view arg_value, const std::string &method, const std::string &param_name)
Return arg_value as UniValue, and first parse it if it is a non-string parameter.
Definition: client.cpp:265
void push_back(UniValue val)
Definition: univalue.cpp:96
@ VOBJ
Definition: univalue.h:31
@ VARR
Definition: univalue.h:32
void pushKVEnd(std::string key, UniValue val)
Definition: univalue.cpp:108
bool read(std::string_view raw)
void pushKV(std::string key, UniValue val)
Definition: univalue.cpp:115
UniValue RPCConvertValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert positional arguments to command-specific RPC representation.
Definition: client.cpp:282
static UniValue Parse(std::string_view raw)
Parse string to UniValue or throw runtime_error if string contains invalid JSON.
Definition: client.cpp:234
UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert named arguments to command-specific RPC representation.
Definition: client.cpp:294
static CRPCConvertTable rpcCvtTable
Definition: client.cpp:280
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:29
void format(std::ostream &out, const char *fmt, const Args &...args)
Format list of arguments to the stream according to given format string.
Definition: tinyformat.h:1113
const char * name
Definition: rest.cpp:46