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