[tests] Add -blocknotify functional test
[bitcoinplatinum.git] / src / rpc / client.cpp
blobf54f24e2a70cad5547caa3aed681d9f9a58f46f0
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 { "sendtoaddress", 5 , "replaceable" },
41 { "sendtoaddress", 6 , "conf_target" },
42 { "settxfee", 0, "amount" },
43 { "getreceivedbyaddress", 1, "minconf" },
44 { "getreceivedbyaccount", 1, "minconf" },
45 { "listreceivedbyaddress", 0, "minconf" },
46 { "listreceivedbyaddress", 1, "include_empty" },
47 { "listreceivedbyaddress", 2, "include_watchonly" },
48 { "listreceivedbyaccount", 0, "minconf" },
49 { "listreceivedbyaccount", 1, "include_empty" },
50 { "listreceivedbyaccount", 2, "include_watchonly" },
51 { "getbalance", 1, "minconf" },
52 { "getbalance", 2, "include_watchonly" },
53 { "getblockhash", 0, "height" },
54 { "waitforblockheight", 0, "height" },
55 { "waitforblockheight", 1, "timeout" },
56 { "waitforblock", 1, "timeout" },
57 { "waitfornewblock", 0, "timeout" },
58 { "move", 2, "amount" },
59 { "move", 3, "minconf" },
60 { "sendfrom", 2, "amount" },
61 { "sendfrom", 3, "minconf" },
62 { "listtransactions", 1, "count" },
63 { "listtransactions", 2, "skip" },
64 { "listtransactions", 3, "include_watchonly" },
65 { "listaccounts", 0, "minconf" },
66 { "listaccounts", 1, "include_watchonly" },
67 { "walletpassphrase", 1, "timeout" },
68 { "getblocktemplate", 0, "template_request" },
69 { "listsinceblock", 1, "target_confirmations" },
70 { "listsinceblock", 2, "include_watchonly" },
71 { "listsinceblock", 3, "include_removed" },
72 { "sendmany", 1, "amounts" },
73 { "sendmany", 2, "minconf" },
74 { "sendmany", 4, "subtractfeefrom" },
75 { "sendmany", 5 , "replaceable" },
76 { "sendmany", 6 , "conf_target" },
77 { "addmultisigaddress", 0, "nrequired" },
78 { "addmultisigaddress", 1, "keys" },
79 { "createmultisig", 0, "nrequired" },
80 { "createmultisig", 1, "keys" },
81 { "listunspent", 0, "minconf" },
82 { "listunspent", 1, "maxconf" },
83 { "listunspent", 2, "addresses" },
84 { "listunspent", 3, "include_unsafe" },
85 { "listunspent", 4, "query_options" },
86 { "getblock", 1, "verbosity" },
87 { "getblock", 1, "verbose" },
88 { "getblockheader", 1, "verbose" },
89 { "getchaintxstats", 0, "nblocks" },
90 { "gettransaction", 1, "include_watchonly" },
91 { "getrawtransaction", 1, "verbose" },
92 { "createrawtransaction", 0, "inputs" },
93 { "createrawtransaction", 1, "outputs" },
94 { "createrawtransaction", 2, "locktime" },
95 { "createrawtransaction", 3, "replaceable" },
96 { "signrawtransaction", 1, "prevtxs" },
97 { "signrawtransaction", 2, "privkeys" },
98 { "sendrawtransaction", 1, "allowhighfees" },
99 { "combinerawtransaction", 0, "txs" },
100 { "fundrawtransaction", 1, "options" },
101 { "gettxout", 1, "n" },
102 { "gettxout", 2, "include_mempool" },
103 { "gettxoutproof", 0, "txids" },
104 { "lockunspent", 0, "unlock" },
105 { "lockunspent", 1, "transactions" },
106 { "importprivkey", 2, "rescan" },
107 { "importaddress", 2, "rescan" },
108 { "importaddress", 3, "p2sh" },
109 { "importpubkey", 2, "rescan" },
110 { "importmulti", 0, "requests" },
111 { "importmulti", 1, "options" },
112 { "verifychain", 0, "checklevel" },
113 { "verifychain", 1, "nblocks" },
114 { "pruneblockchain", 0, "height" },
115 { "keypoolrefill", 0, "newsize" },
116 { "getrawmempool", 0, "verbose" },
117 { "estimatefee", 0, "nblocks" },
118 { "estimatesmartfee", 0, "conf_target" },
119 { "estimaterawfee", 0, "conf_target" },
120 { "estimaterawfee", 1, "threshold" },
121 { "prioritisetransaction", 1, "dummy" },
122 { "prioritisetransaction", 2, "fee_delta" },
123 { "setban", 2, "bantime" },
124 { "setban", 3, "absolute" },
125 { "setnetworkactive", 0, "state" },
126 { "getmempoolancestors", 1, "verbose" },
127 { "getmempooldescendants", 1, "verbose" },
128 { "bumpfee", 1, "options" },
129 { "logging", 0, "include" },
130 { "logging", 1, "exclude" },
131 { "disconnectnode", 1, "nodeid" },
132 { "addwitnessaddress", 1, "p2sh" },
133 // Echo with conversion (For testing only)
134 { "echojson", 0, "arg0" },
135 { "echojson", 1, "arg1" },
136 { "echojson", 2, "arg2" },
137 { "echojson", 3, "arg3" },
138 { "echojson", 4, "arg4" },
139 { "echojson", 5, "arg5" },
140 { "echojson", 6, "arg6" },
141 { "echojson", 7, "arg7" },
142 { "echojson", 8, "arg8" },
143 { "echojson", 9, "arg9" },
146 class CRPCConvertTable
148 private:
149 std::set<std::pair<std::string, int>> members;
150 std::set<std::pair<std::string, std::string>> membersByName;
152 public:
153 CRPCConvertTable();
155 bool convert(const std::string& method, int idx) {
156 return (members.count(std::make_pair(method, idx)) > 0);
158 bool convert(const std::string& method, const std::string& name) {
159 return (membersByName.count(std::make_pair(method, name)) > 0);
163 CRPCConvertTable::CRPCConvertTable()
165 const unsigned int n_elem =
166 (sizeof(vRPCConvertParams) / sizeof(vRPCConvertParams[0]));
168 for (unsigned int i = 0; i < n_elem; i++) {
169 members.insert(std::make_pair(vRPCConvertParams[i].methodName,
170 vRPCConvertParams[i].paramIdx));
171 membersByName.insert(std::make_pair(vRPCConvertParams[i].methodName,
172 vRPCConvertParams[i].paramName));
176 static CRPCConvertTable rpcCvtTable;
178 /** Non-RFC4627 JSON parser, accepts internal values (such as numbers, true, false, null)
179 * as well as objects and arrays.
181 UniValue ParseNonRFCJSONValue(const std::string& strVal)
183 UniValue jVal;
184 if (!jVal.read(std::string("[")+strVal+std::string("]")) ||
185 !jVal.isArray() || jVal.size()!=1)
186 throw std::runtime_error(std::string("Error parsing JSON:")+strVal);
187 return jVal[0];
190 UniValue RPCConvertValues(const std::string &strMethod, const std::vector<std::string> &strParams)
192 UniValue params(UniValue::VARR);
194 for (unsigned int idx = 0; idx < strParams.size(); idx++) {
195 const std::string& strVal = strParams[idx];
197 if (!rpcCvtTable.convert(strMethod, idx)) {
198 // insert string value directly
199 params.push_back(strVal);
200 } else {
201 // parse string as JSON, insert bool/number/object/etc. value
202 params.push_back(ParseNonRFCJSONValue(strVal));
206 return params;
209 UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector<std::string> &strParams)
211 UniValue params(UniValue::VOBJ);
213 for (const std::string &s: strParams) {
214 size_t pos = s.find("=");
215 if (pos == std::string::npos) {
216 throw(std::runtime_error("No '=' in named argument '"+s+"', this needs to be present for every argument (even if it is empty)"));
219 std::string name = s.substr(0, pos);
220 std::string value = s.substr(pos+1);
222 if (!rpcCvtTable.convert(strMethod, name)) {
223 // insert string value directly
224 params.pushKV(name, value);
225 } else {
226 // parse string as JSON, insert bool/number/object/etc. value
227 params.pushKV(name, ParseNonRFCJSONValue(value));
231 return params;