Bitcoin Core  22.99.0
P2P Digital Currency
rpc_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2020 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <rpc/client.h>
6 #include <rpc/server.h>
7 #include <rpc/util.h>
8 
9 #include <core_io.h>
10 #include <interfaces/chain.h>
11 #include <node/context.h>
12 #include <test/util/setup_common.h>
13 #include <util/time.h>
14 
15 #include <any>
16 
17 #include <boost/algorithm/string.hpp>
18 #include <boost/test/unit_test.hpp>
19 
20 #include <univalue.h>
21 
22 #include <rpc/blockchain.h>
23 
25 {
26 public:
27  UniValue CallRPC(std::string args);
28 };
29 
31 {
32  std::vector<std::string> vArgs;
33  boost::split(vArgs, args, boost::is_any_of(" \t"));
34  std::string strMethod = vArgs[0];
35  vArgs.erase(vArgs.begin());
36  JSONRPCRequest request;
37  request.context = &m_node;
38  request.strMethod = strMethod;
39  request.params = RPCConvertValues(strMethod, vArgs);
40  if (RPCIsInWarmup(nullptr)) SetRPCWarmupFinished();
41  try {
42  UniValue result = tableRPC.execute(request);
43  return result;
44  }
45  catch (const UniValue& objError) {
46  throw std::runtime_error(find_value(objError, "message").get_str());
47  }
48 }
49 
50 
52 
53 BOOST_AUTO_TEST_CASE(rpc_rawparams)
54 {
55  // Test raw transaction API argument handling
56  UniValue r;
57 
58  BOOST_CHECK_THROW(CallRPC("getrawtransaction"), std::runtime_error);
59  BOOST_CHECK_THROW(CallRPC("getrawtransaction not_hex"), std::runtime_error);
60  BOOST_CHECK_THROW(CallRPC("getrawtransaction a3b807410df0b60fcb9736768df5823938b2f838694939ba45f3c0a1bff150ed not_int"), std::runtime_error);
61 
62  BOOST_CHECK_THROW(CallRPC("createrawtransaction"), std::runtime_error);
63  BOOST_CHECK_THROW(CallRPC("createrawtransaction null null"), std::runtime_error);
64  BOOST_CHECK_THROW(CallRPC("createrawtransaction not_array"), std::runtime_error);
65  BOOST_CHECK_THROW(CallRPC("createrawtransaction {} {}"), std::runtime_error);
66  BOOST_CHECK_NO_THROW(CallRPC("createrawtransaction [] {}"));
67  BOOST_CHECK_THROW(CallRPC("createrawtransaction [] {} extra"), std::runtime_error);
68 
69  BOOST_CHECK_THROW(CallRPC("decoderawtransaction"), std::runtime_error);
70  BOOST_CHECK_THROW(CallRPC("decoderawtransaction null"), std::runtime_error);
71  BOOST_CHECK_THROW(CallRPC("decoderawtransaction DEADBEEF"), std::runtime_error);
72  std::string rawtx = "0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000";
73  BOOST_CHECK_NO_THROW(r = CallRPC(std::string("decoderawtransaction ")+rawtx));
74  BOOST_CHECK_EQUAL(find_value(r.get_obj(), "size").get_int(), 193);
75  BOOST_CHECK_EQUAL(find_value(r.get_obj(), "version").get_int(), 1);
76  BOOST_CHECK_EQUAL(find_value(r.get_obj(), "locktime").get_int(), 0);
77  BOOST_CHECK_THROW(CallRPC(std::string("decoderawtransaction ")+rawtx+" extra"), std::runtime_error);
78  BOOST_CHECK_NO_THROW(r = CallRPC(std::string("decoderawtransaction ")+rawtx+" false"));
79  BOOST_CHECK_THROW(r = CallRPC(std::string("decoderawtransaction ")+rawtx+" false extra"), std::runtime_error);
80 
81  // Only check failure cases for sendrawtransaction, there's no network to send to...
82  BOOST_CHECK_THROW(CallRPC("sendrawtransaction"), std::runtime_error);
83  BOOST_CHECK_THROW(CallRPC("sendrawtransaction null"), std::runtime_error);
84  BOOST_CHECK_THROW(CallRPC("sendrawtransaction DEADBEEF"), std::runtime_error);
85  BOOST_CHECK_THROW(CallRPC(std::string("sendrawtransaction ")+rawtx+" extra"), std::runtime_error);
86 }
87 
88 BOOST_AUTO_TEST_CASE(rpc_togglenetwork)
89 {
90  UniValue r;
91 
92  r = CallRPC("getnetworkinfo");
93  bool netState = find_value(r.get_obj(), "networkactive").get_bool();
94  BOOST_CHECK_EQUAL(netState, true);
95 
96  BOOST_CHECK_NO_THROW(CallRPC("setnetworkactive false"));
97  r = CallRPC("getnetworkinfo");
98  int numConnection = find_value(r.get_obj(), "connections").get_int();
99  BOOST_CHECK_EQUAL(numConnection, 0);
100 
101  netState = find_value(r.get_obj(), "networkactive").get_bool();
102  BOOST_CHECK_EQUAL(netState, false);
103 
104  BOOST_CHECK_NO_THROW(CallRPC("setnetworkactive true"));
105  r = CallRPC("getnetworkinfo");
106  netState = find_value(r.get_obj(), "networkactive").get_bool();
107  BOOST_CHECK_EQUAL(netState, true);
108 }
109 
111 {
112  UniValue r;
113  // input is a 1-of-2 multisig (so is output):
114  std::string prevout =
115  "[{\"txid\":\"b4cc287e58f87cdae59417329f710f3ecd75a4ee1d2872b7248f50977c8493f3\","
116  "\"vout\":1,\"scriptPubKey\":\"a914b10c9df5f7edf436c697f02f1efdba4cf399615187\","
117  "\"redeemScript\":\"512103debedc17b3df2badbcdd86d5feb4562b86fe182e5998abd8bcd4f122c6155b1b21027e940bb73ab8732bfdf7f9216ecefca5b94d6df834e77e108f68e66f126044c052ae\"}]";
118  r = CallRPC(std::string("createrawtransaction ")+prevout+" "+
119  "{\"3HqAe9LtNBjnsfM4CyYaWTnvCaUYT7v4oZ\":11}");
120  std::string notsigned = r.get_str();
121  std::string privkey1 = "\"KzsXybp9jX64P5ekX1KUxRQ79Jht9uzW7LorgwE65i5rWACL6LQe\"";
122  std::string privkey2 = "\"Kyhdf5LuKTRx4ge69ybABsiUAWjVRK4XGxAKk2FQLp2HjGMy87Z4\"";
123  r = CallRPC(std::string("signrawtransactionwithkey ")+notsigned+" [] "+prevout);
124  BOOST_CHECK(find_value(r.get_obj(), "complete").get_bool() == false);
125  r = CallRPC(std::string("signrawtransactionwithkey ")+notsigned+" ["+privkey1+","+privkey2+"] "+prevout);
126  BOOST_CHECK(find_value(r.get_obj(), "complete").get_bool() == true);
127 }
128 
129 BOOST_AUTO_TEST_CASE(rpc_createraw_op_return)
130 {
131  BOOST_CHECK_NO_THROW(CallRPC("createrawtransaction [{\"txid\":\"a3b807410df0b60fcb9736768df5823938b2f838694939ba45f3c0a1bff150ed\",\"vout\":0}] {\"data\":\"68656c6c6f776f726c64\"}"));
132 
133  // Key not "data" (bad address)
134  BOOST_CHECK_THROW(CallRPC("createrawtransaction [{\"txid\":\"a3b807410df0b60fcb9736768df5823938b2f838694939ba45f3c0a1bff150ed\",\"vout\":0}] {\"somedata\":\"68656c6c6f776f726c64\"}"), std::runtime_error);
135 
136  // Bad hex encoding of data output
137  BOOST_CHECK_THROW(CallRPC("createrawtransaction [{\"txid\":\"a3b807410df0b60fcb9736768df5823938b2f838694939ba45f3c0a1bff150ed\",\"vout\":0}] {\"data\":\"12345\"}"), std::runtime_error);
138  BOOST_CHECK_THROW(CallRPC("createrawtransaction [{\"txid\":\"a3b807410df0b60fcb9736768df5823938b2f838694939ba45f3c0a1bff150ed\",\"vout\":0}] {\"data\":\"12345g\"}"), std::runtime_error);
139 
140  // Data 81 bytes long
141  BOOST_CHECK_NO_THROW(CallRPC("createrawtransaction [{\"txid\":\"a3b807410df0b60fcb9736768df5823938b2f838694939ba45f3c0a1bff150ed\",\"vout\":0}] {\"data\":\"010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081\"}"));
142 }
143 
144 BOOST_AUTO_TEST_CASE(rpc_format_monetary_values)
145 {
146  BOOST_CHECK(ValueFromAmount(0LL).write() == "0.00000000");
147  BOOST_CHECK(ValueFromAmount(1LL).write() == "0.00000001");
148  BOOST_CHECK(ValueFromAmount(17622195LL).write() == "0.17622195");
149  BOOST_CHECK(ValueFromAmount(50000000LL).write() == "0.50000000");
150  BOOST_CHECK(ValueFromAmount(89898989LL).write() == "0.89898989");
151  BOOST_CHECK(ValueFromAmount(100000000LL).write() == "1.00000000");
152  BOOST_CHECK(ValueFromAmount(2099999999999990LL).write() == "20999999.99999990");
153  BOOST_CHECK(ValueFromAmount(2099999999999999LL).write() == "20999999.99999999");
154 
155  BOOST_CHECK_EQUAL(ValueFromAmount(0).write(), "0.00000000");
156  BOOST_CHECK_EQUAL(ValueFromAmount((COIN/10000)*123456789).write(), "12345.67890000");
157  BOOST_CHECK_EQUAL(ValueFromAmount(-COIN).write(), "-1.00000000");
158  BOOST_CHECK_EQUAL(ValueFromAmount(-COIN/10).write(), "-0.10000000");
159 
160  BOOST_CHECK_EQUAL(ValueFromAmount(COIN*100000000).write(), "100000000.00000000");
161  BOOST_CHECK_EQUAL(ValueFromAmount(COIN*10000000).write(), "10000000.00000000");
162  BOOST_CHECK_EQUAL(ValueFromAmount(COIN*1000000).write(), "1000000.00000000");
163  BOOST_CHECK_EQUAL(ValueFromAmount(COIN*100000).write(), "100000.00000000");
164  BOOST_CHECK_EQUAL(ValueFromAmount(COIN*10000).write(), "10000.00000000");
165  BOOST_CHECK_EQUAL(ValueFromAmount(COIN*1000).write(), "1000.00000000");
166  BOOST_CHECK_EQUAL(ValueFromAmount(COIN*100).write(), "100.00000000");
167  BOOST_CHECK_EQUAL(ValueFromAmount(COIN*10).write(), "10.00000000");
168  BOOST_CHECK_EQUAL(ValueFromAmount(COIN).write(), "1.00000000");
169  BOOST_CHECK_EQUAL(ValueFromAmount(COIN/10).write(), "0.10000000");
170  BOOST_CHECK_EQUAL(ValueFromAmount(COIN/100).write(), "0.01000000");
171  BOOST_CHECK_EQUAL(ValueFromAmount(COIN/1000).write(), "0.00100000");
172  BOOST_CHECK_EQUAL(ValueFromAmount(COIN/10000).write(), "0.00010000");
173  BOOST_CHECK_EQUAL(ValueFromAmount(COIN/100000).write(), "0.00001000");
174  BOOST_CHECK_EQUAL(ValueFromAmount(COIN/1000000).write(), "0.00000100");
175  BOOST_CHECK_EQUAL(ValueFromAmount(COIN/10000000).write(), "0.00000010");
176  BOOST_CHECK_EQUAL(ValueFromAmount(COIN/100000000).write(), "0.00000001");
177 
178  BOOST_CHECK_EQUAL(ValueFromAmount(std::numeric_limits<CAmount>::max()).write(), "92233720368.54775807");
179  BOOST_CHECK_EQUAL(ValueFromAmount(std::numeric_limits<CAmount>::max() - 1).write(), "92233720368.54775806");
180  BOOST_CHECK_EQUAL(ValueFromAmount(std::numeric_limits<CAmount>::max() - 2).write(), "92233720368.54775805");
181  BOOST_CHECK_EQUAL(ValueFromAmount(std::numeric_limits<CAmount>::max() - 3).write(), "92233720368.54775804");
182  // ...
183  BOOST_CHECK_EQUAL(ValueFromAmount(std::numeric_limits<CAmount>::min() + 3).write(), "-92233720368.54775805");
184  BOOST_CHECK_EQUAL(ValueFromAmount(std::numeric_limits<CAmount>::min() + 2).write(), "-92233720368.54775806");
185  BOOST_CHECK_EQUAL(ValueFromAmount(std::numeric_limits<CAmount>::min() + 1).write(), "-92233720368.54775807");
186  BOOST_CHECK_EQUAL(ValueFromAmount(std::numeric_limits<CAmount>::min()).write(), "-92233720368.54775808");
187 }
188 
189 static UniValue ValueFromString(const std::string &str)
190 {
191  UniValue value;
192  BOOST_CHECK(value.setNumStr(str));
193  return value;
194 }
195 
196 BOOST_AUTO_TEST_CASE(rpc_parse_monetary_values)
197 {
202  BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.17622195")), 17622195LL);
204  BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.50000000")), 50000000LL);
205  BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.89898989")), 89898989LL);
206  BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("1.00000000")), 100000000LL);
207  BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("20999999.9999999")), 2099999999999990LL);
208  BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("20999999.99999999")), 2099999999999999LL);
209 
211  BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.1e-7")), COIN/100000000);
212  BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.01e-6")), COIN/100000000);
213  BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.0000000000000000000000000000000000000000000000000000000000000000000000000001e+68")), COIN/100000000);
214  BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("10000000000000000000000000000000000000000000000000000000000000000e-64")), COIN);
215  BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000e64")), COIN);
216 
218  BOOST_CHECK_THROW(AmountFromValue(ValueFromString("0.000000019")), UniValue); //should fail
219  BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.00000001000000")), 1LL); //should pass, cut trailing 0
220  BOOST_CHECK_THROW(AmountFromValue(ValueFromString("19e-9")), UniValue); //should fail
221  BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.19e-6")), 19); //should pass, leading 0 is present
222 
223  BOOST_CHECK_THROW(AmountFromValue(ValueFromString("92233720368.54775808")), UniValue); //overflow error
224  BOOST_CHECK_THROW(AmountFromValue(ValueFromString("1e+11")), UniValue); //overflow error
225  BOOST_CHECK_THROW(AmountFromValue(ValueFromString("1e11")), UniValue); //overflow error signless
226  BOOST_CHECK_THROW(AmountFromValue(ValueFromString("93e+9")), UniValue); //overflow error
227 }
228 
229 BOOST_AUTO_TEST_CASE(json_parse_errors)
230 {
231  // Valid
232  BOOST_CHECK_EQUAL(ParseNonRFCJSONValue("1.0").get_real(), 1.0);
233  // Valid, with leading or trailing whitespace
234  BOOST_CHECK_EQUAL(ParseNonRFCJSONValue(" 1.0").get_real(), 1.0);
235  BOOST_CHECK_EQUAL(ParseNonRFCJSONValue("1.0 ").get_real(), 1.0);
236 
237  BOOST_CHECK_THROW(AmountFromValue(ParseNonRFCJSONValue(".19e-6")), std::runtime_error); //should fail, missing leading 0, therefore invalid JSON
238  BOOST_CHECK_EQUAL(AmountFromValue(ParseNonRFCJSONValue("0.00000000000000000000000000000000000001e+30 ")), 1);
239  // Invalid, initial garbage
240  BOOST_CHECK_THROW(ParseNonRFCJSONValue("[1.0"), std::runtime_error);
241  BOOST_CHECK_THROW(ParseNonRFCJSONValue("a1.0"), std::runtime_error);
242  // Invalid, trailing garbage
243  BOOST_CHECK_THROW(ParseNonRFCJSONValue("1.0sds"), std::runtime_error);
244  BOOST_CHECK_THROW(ParseNonRFCJSONValue("1.0]"), std::runtime_error);
245  // BTC addresses should fail parsing
246  BOOST_CHECK_THROW(ParseNonRFCJSONValue("175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W"), std::runtime_error);
247  BOOST_CHECK_THROW(ParseNonRFCJSONValue("3J98t1WpEZ73CNmQviecrnyiWrnqRhWNL"), std::runtime_error);
248 }
249 
251 {
252  BOOST_CHECK_NO_THROW(CallRPC(std::string("clearbanned")));
253 
254  UniValue r;
255  BOOST_CHECK_NO_THROW(r = CallRPC(std::string("setban 127.0.0.0 add")));
256  BOOST_CHECK_THROW(r = CallRPC(std::string("setban 127.0.0.0:8334")), std::runtime_error); //portnumber for setban not allowed
257  BOOST_CHECK_NO_THROW(r = CallRPC(std::string("listbanned")));
258  UniValue ar = r.get_array();
259  UniValue o1 = ar[0].get_obj();
260  UniValue adr = find_value(o1, "address");
261  BOOST_CHECK_EQUAL(adr.get_str(), "127.0.0.0/32");
262  BOOST_CHECK_NO_THROW(CallRPC(std::string("setban 127.0.0.0 remove")));
263  BOOST_CHECK_NO_THROW(r = CallRPC(std::string("listbanned")));
264  ar = r.get_array();
265  BOOST_CHECK_EQUAL(ar.size(), 0U);
266 
267  BOOST_CHECK_NO_THROW(r = CallRPC(std::string("setban 127.0.0.0/24 add 9907731200 true")));
268  BOOST_CHECK_NO_THROW(r = CallRPC(std::string("listbanned")));
269  ar = r.get_array();
270  o1 = ar[0].get_obj();
271  adr = find_value(o1, "address");
272  int64_t banned_until{find_value(o1, "banned_until").get_int64()};
273  BOOST_CHECK_EQUAL(adr.get_str(), "127.0.0.0/24");
274  BOOST_CHECK_EQUAL(banned_until, 9907731200); // absolute time check
275 
276  BOOST_CHECK_NO_THROW(CallRPC(std::string("clearbanned")));
277 
278  auto now = 10'000s;
279  SetMockTime(now);
280  BOOST_CHECK_NO_THROW(r = CallRPC(std::string("setban 127.0.0.0/24 add 200")));
281  SetMockTime(now += 2s);
282  const int64_t time_remaining_expected{198};
283  BOOST_CHECK_NO_THROW(r = CallRPC(std::string("listbanned")));
284  ar = r.get_array();
285  o1 = ar[0].get_obj();
286  adr = find_value(o1, "address");
287  banned_until = find_value(o1, "banned_until").get_int64();
288  const int64_t ban_created{find_value(o1, "ban_created").get_int64()};
289  const int64_t ban_duration{find_value(o1, "ban_duration").get_int64()};
290  const int64_t time_remaining{find_value(o1, "time_remaining").get_int64()};
291  BOOST_CHECK_EQUAL(adr.get_str(), "127.0.0.0/24");
292  BOOST_CHECK_EQUAL(banned_until, time_remaining_expected + now.count());
293  BOOST_CHECK_EQUAL(ban_duration, banned_until - ban_created);
294  BOOST_CHECK_EQUAL(time_remaining, time_remaining_expected);
295 
296  // must throw an exception because 127.0.0.1 is in already banned subnet range
297  BOOST_CHECK_THROW(r = CallRPC(std::string("setban 127.0.0.1 add")), std::runtime_error);
298 
299  BOOST_CHECK_NO_THROW(CallRPC(std::string("setban 127.0.0.0/24 remove")));
300  BOOST_CHECK_NO_THROW(r = CallRPC(std::string("listbanned")));
301  ar = r.get_array();
302  BOOST_CHECK_EQUAL(ar.size(), 0U);
303 
304  BOOST_CHECK_NO_THROW(r = CallRPC(std::string("setban 127.0.0.0/255.255.0.0 add")));
305  BOOST_CHECK_THROW(r = CallRPC(std::string("setban 127.0.1.1 add")), std::runtime_error);
306 
307  BOOST_CHECK_NO_THROW(CallRPC(std::string("clearbanned")));
308  BOOST_CHECK_NO_THROW(r = CallRPC(std::string("listbanned")));
309  ar = r.get_array();
310  BOOST_CHECK_EQUAL(ar.size(), 0U);
311 
312 
313  BOOST_CHECK_THROW(r = CallRPC(std::string("setban test add")), std::runtime_error); //invalid IP
314 
315  //IPv6 tests
316  BOOST_CHECK_NO_THROW(r = CallRPC(std::string("setban FE80:0000:0000:0000:0202:B3FF:FE1E:8329 add")));
317  BOOST_CHECK_NO_THROW(r = CallRPC(std::string("listbanned")));
318  ar = r.get_array();
319  o1 = ar[0].get_obj();
320  adr = find_value(o1, "address");
321  BOOST_CHECK_EQUAL(adr.get_str(), "fe80::202:b3ff:fe1e:8329/128");
322 
323  BOOST_CHECK_NO_THROW(CallRPC(std::string("clearbanned")));
324  BOOST_CHECK_NO_THROW(r = CallRPC(std::string("setban 2001:db8::/ffff:fffc:0:0:0:0:0:0 add")));
325  BOOST_CHECK_NO_THROW(r = CallRPC(std::string("listbanned")));
326  ar = r.get_array();
327  o1 = ar[0].get_obj();
328  adr = find_value(o1, "address");
329  BOOST_CHECK_EQUAL(adr.get_str(), "2001:db8::/30");
330 
331  BOOST_CHECK_NO_THROW(CallRPC(std::string("clearbanned")));
332  BOOST_CHECK_NO_THROW(r = CallRPC(std::string("setban 2001:4d48:ac57:400:cacf:e9ff:fe1d:9c63/128 add")));
333  BOOST_CHECK_NO_THROW(r = CallRPC(std::string("listbanned")));
334  ar = r.get_array();
335  o1 = ar[0].get_obj();
336  adr = find_value(o1, "address");
337  BOOST_CHECK_EQUAL(adr.get_str(), "2001:4d48:ac57:400:cacf:e9ff:fe1d:9c63/128");
338 }
339 
340 BOOST_AUTO_TEST_CASE(rpc_convert_values_generatetoaddress)
341 {
342  UniValue result;
343 
344  BOOST_CHECK_NO_THROW(result = RPCConvertValues("generatetoaddress", {"101", "mkESjLZW66TmHhiFX8MCaBjrhZ543PPh9a"}));
345  BOOST_CHECK_EQUAL(result[0].get_int(), 101);
346  BOOST_CHECK_EQUAL(result[1].get_str(), "mkESjLZW66TmHhiFX8MCaBjrhZ543PPh9a");
347 
348  BOOST_CHECK_NO_THROW(result = RPCConvertValues("generatetoaddress", {"101", "mhMbmE2tE9xzJYCV9aNC8jKWN31vtGrguU"}));
349  BOOST_CHECK_EQUAL(result[0].get_int(), 101);
350  BOOST_CHECK_EQUAL(result[1].get_str(), "mhMbmE2tE9xzJYCV9aNC8jKWN31vtGrguU");
351 
352  BOOST_CHECK_NO_THROW(result = RPCConvertValues("generatetoaddress", {"1", "mkESjLZW66TmHhiFX8MCaBjrhZ543PPh9a", "9"}));
353  BOOST_CHECK_EQUAL(result[0].get_int(), 1);
354  BOOST_CHECK_EQUAL(result[1].get_str(), "mkESjLZW66TmHhiFX8MCaBjrhZ543PPh9a");
355  BOOST_CHECK_EQUAL(result[2].get_int(), 9);
356 
357  BOOST_CHECK_NO_THROW(result = RPCConvertValues("generatetoaddress", {"1", "mhMbmE2tE9xzJYCV9aNC8jKWN31vtGrguU", "9"}));
358  BOOST_CHECK_EQUAL(result[0].get_int(), 1);
359  BOOST_CHECK_EQUAL(result[1].get_str(), "mhMbmE2tE9xzJYCV9aNC8jKWN31vtGrguU");
360  BOOST_CHECK_EQUAL(result[2].get_int(), 9);
361 }
362 
363 BOOST_AUTO_TEST_CASE(rpc_getblockstats_calculate_percentiles_by_weight)
364 {
365  int64_t total_weight = 200;
366  std::vector<std::pair<CAmount, int64_t>> feerates;
367  CAmount result[NUM_GETBLOCKSTATS_PERCENTILES] = { 0 };
368 
369  for (int64_t i = 0; i < 100; i++) {
370  feerates.emplace_back(std::make_pair(1 ,1));
371  }
372 
373  for (int64_t i = 0; i < 100; i++) {
374  feerates.emplace_back(std::make_pair(2 ,1));
375  }
376 
377  CalculatePercentilesByWeight(result, feerates, total_weight);
378  BOOST_CHECK_EQUAL(result[0], 1);
379  BOOST_CHECK_EQUAL(result[1], 1);
380  BOOST_CHECK_EQUAL(result[2], 1);
381  BOOST_CHECK_EQUAL(result[3], 2);
382  BOOST_CHECK_EQUAL(result[4], 2);
383 
384  // Test with more pairs, and two pairs overlapping 2 percentiles.
385  total_weight = 100;
386  CAmount result2[NUM_GETBLOCKSTATS_PERCENTILES] = { 0 };
387  feerates.clear();
388 
389  feerates.emplace_back(std::make_pair(1, 9));
390  feerates.emplace_back(std::make_pair(2 , 16)); //10th + 25th percentile
391  feerates.emplace_back(std::make_pair(4 ,50)); //50th + 75th percentile
392  feerates.emplace_back(std::make_pair(5 ,10));
393  feerates.emplace_back(std::make_pair(9 ,15)); // 90th percentile
394 
395  CalculatePercentilesByWeight(result2, feerates, total_weight);
396 
397  BOOST_CHECK_EQUAL(result2[0], 2);
398  BOOST_CHECK_EQUAL(result2[1], 2);
399  BOOST_CHECK_EQUAL(result2[2], 4);
400  BOOST_CHECK_EQUAL(result2[3], 4);
401  BOOST_CHECK_EQUAL(result2[4], 9);
402 
403  // Same test as above, but one of the percentile-overlapping pairs is split in 2.
404  total_weight = 100;
405  CAmount result3[NUM_GETBLOCKSTATS_PERCENTILES] = { 0 };
406  feerates.clear();
407 
408  feerates.emplace_back(std::make_pair(1, 9));
409  feerates.emplace_back(std::make_pair(2 , 11)); // 10th percentile
410  feerates.emplace_back(std::make_pair(2 , 5)); // 25th percentile
411  feerates.emplace_back(std::make_pair(4 ,50)); //50th + 75th percentile
412  feerates.emplace_back(std::make_pair(5 ,10));
413  feerates.emplace_back(std::make_pair(9 ,15)); // 90th percentile
414 
415  CalculatePercentilesByWeight(result3, feerates, total_weight);
416 
417  BOOST_CHECK_EQUAL(result3[0], 2);
418  BOOST_CHECK_EQUAL(result3[1], 2);
419  BOOST_CHECK_EQUAL(result3[2], 4);
420  BOOST_CHECK_EQUAL(result3[3], 4);
421  BOOST_CHECK_EQUAL(result3[4], 9);
422 
423  // Test with one transaction spanning all percentiles.
424  total_weight = 104;
425  CAmount result4[NUM_GETBLOCKSTATS_PERCENTILES] = { 0 };
426  feerates.clear();
427 
428  feerates.emplace_back(std::make_pair(1, 100));
429  feerates.emplace_back(std::make_pair(2, 1));
430  feerates.emplace_back(std::make_pair(3, 1));
431  feerates.emplace_back(std::make_pair(3, 1));
432  feerates.emplace_back(std::make_pair(999999, 1));
433 
434  CalculatePercentilesByWeight(result4, feerates, total_weight);
435 
436  for (int64_t i = 0; i < NUM_GETBLOCKSTATS_PERCENTILES; i++) {
437  BOOST_CHECK_EQUAL(result4[i], 1);
438  }
439 }
440 
441 BOOST_AUTO_TEST_CASE(help_example)
442 {
443  // test different argument types
444  const RPCArgList& args = {{"foo", "bar"}, {"b", true}, {"n", 1}};
445  BOOST_CHECK_EQUAL(HelpExampleCliNamed("test", args), "> bitcoin-cli -named test foo=bar b=true n=1\n");
446  BOOST_CHECK_EQUAL(HelpExampleRpcNamed("test", args), "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", \"id\": \"curltest\", \"method\": \"test\", \"params\": {\"foo\":\"bar\",\"b\":true,\"n\":1}}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n");
447 
448  // test shell escape
449  BOOST_CHECK_EQUAL(HelpExampleCliNamed("test", {{"foo", "b'ar"}}), "> bitcoin-cli -named test foo='b'''ar'\n");
450  BOOST_CHECK_EQUAL(HelpExampleCliNamed("test", {{"foo", "b\"ar"}}), "> bitcoin-cli -named test foo='b\"ar'\n");
451  BOOST_CHECK_EQUAL(HelpExampleCliNamed("test", {{"foo", "b ar"}}), "> bitcoin-cli -named test foo='b ar'\n");
452 
453  // test object params
454  UniValue obj_value(UniValue::VOBJ);
455  obj_value.pushKV("foo", "bar");
456  obj_value.pushKV("b", false);
457  obj_value.pushKV("n", 1);
458  BOOST_CHECK_EQUAL(HelpExampleCliNamed("test", {{"name", obj_value}}), "> bitcoin-cli -named test name='{\"foo\":\"bar\",\"b\":false,\"n\":1}'\n");
459  BOOST_CHECK_EQUAL(HelpExampleRpcNamed("test", {{"name", obj_value}}), "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", \"id\": \"curltest\", \"method\": \"test\", \"params\": {\"name\":{\"foo\":\"bar\",\"b\":false,\"n\":1}}}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n");
460 
461  // test array params
462  UniValue arr_value(UniValue::VARR);
463  arr_value.push_back("bar");
464  arr_value.push_back(false);
465  arr_value.push_back(1);
466  BOOST_CHECK_EQUAL(HelpExampleCliNamed("test", {{"name", arr_value}}), "> bitcoin-cli -named test name='[\"bar\",false,1]'\n");
467  BOOST_CHECK_EQUAL(HelpExampleRpcNamed("test", {{"name", arr_value}}), "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", \"id\": \"curltest\", \"method\": \"test\", \"params\": {\"name\":[\"bar\",false,1]}}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n");
468 
469  // test types don't matter for shell
470  BOOST_CHECK_EQUAL(HelpExampleCliNamed("foo", {{"arg", true}}), HelpExampleCliNamed("foo", {{"arg", "true"}}));
471 
472  // test types matter for Rpc
473  BOOST_CHECK_NE(HelpExampleRpcNamed("foo", {{"arg", true}}), HelpExampleRpcNamed("foo", {{"arg", "true"}}));
474 }
475 
RPCTestingSetup::CallRPC
UniValue CallRPC(std::string args)
Definition: rpc_tests.cpp:30
UniValue::VOBJ
@ VOBJ
Definition: univalue.h:21
UniValue::get_bool
bool get_bool() const
Definition: univalue_get.cpp:90
JSONRPCRequest::context
std::any context
Definition: request.h:38
setup_common.h
RPCConvertValues
UniValue RPCConvertValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert positional arguments to command-specific RPC representation.
Definition: client.cpp:240
chain.h
BOOST_FIXTURE_TEST_SUITE
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
context.h
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
core_io.h
UniValue::pushKV
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:133
ValueFromAmount
UniValue ValueFromAmount(const CAmount amount)
Definition: core_write.cpp:20
UniValue
Definition: univalue.h:19
UniValue::get_str
const std::string & get_str() const
Definition: univalue_get.cpp:97
SetRPCWarmupFinished
void SetRPCWarmupFinished()
Definition: server.cpp:337
RPCTestingSetup
Definition: rpc_tests.cpp:24
UniValue::get_int64
int64_t get_int64() const
Definition: univalue_get.cpp:114
UniValue::get_obj
const UniValue & get_obj() const
Definition: univalue_get.cpp:134
CallRPC
static UniValue CallRPC(BaseRequestHandler *rh, const std::string &strMethod, const std::vector< std::string > &args, const std::optional< std::string > &rpcwallet={})
Definition: bitcoin-cli.cpp:696
CRPCTable::execute
UniValue execute(const JSONRPCRequest &request) const
Execute a method.
Definition: server.cpp:451
JSONRPCRequest::strMethod
std::string strMethod
Definition: request.h:32
univalue.h
tableRPC
CRPCTable tableRPC
Definition: server.cpp:548
time.h
BOOST_CHECK_NO_THROW
#define BOOST_CHECK_NO_THROW(stmt)
Definition: object.cpp:28
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(rpc_rawparams)
Definition: rpc_tests.cpp:53
HelpExampleRpcNamed
std::string HelpExampleRpcNamed(const std::string &methodname, const RPCArgList &args)
Definition: util.cpp:179
JSONRPCRequest::params
UniValue params
Definition: request.h:33
ValueFromString
static UniValue ValueFromString(const std::string &str)
Definition: rpc_tests.cpp:189
BasicTestingSetup::m_node
NodeContext m_node
Definition: setup_common.h:78
UniValue::setNumStr
bool setNumStr(const std::string &val)
Definition: univalue.cpp:46
UniValue::get_int
int get_int() const
Definition: univalue_get.cpp:104
UniValue::push_back
bool push_back(const UniValue &val)
Definition: univalue.cpp:108
TestingSetup
Testing setup that configures a complete environment.
Definition: setup_common.h:99
COIN
static const CAmount COIN
Definition: amount.h:14
BOOST_CHECK_THROW
#define BOOST_CHECK_THROW(stmt, excMatch)
Definition: object.cpp:19
UniValue::size
size_t size() const
Definition: univalue.h:68
JSONRPCRequest
Definition: request.h:28
util.h
blockchain.h
HelpExampleCliNamed
std::string HelpExampleCliNamed(const std::string &methodname, const RPCArgList &args)
Definition: util.cpp:160
find_value
const UniValue & find_value(const UniValue &obj, const std::string &name)
Definition: univalue.cpp:234
RPCIsInWarmup
bool RPCIsInWarmup(std::string *outStatus)
Definition: server.cpp:344
UniValue::get_array
const UniValue & get_array() const
Definition: univalue_get.cpp:141
UniValue::VARR
@ VARR
Definition: univalue.h:21
AmountFromValue
static CAmount AmountFromValue(const UniValue &value)
Definition: bitcoin-tx.cpp:538
server.h
ParseNonRFCJSONValue
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:231
client.h
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18