LCOV - code coverage report
Current view: top level - src/test - rpc_tests.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 292 292 100.0 %
Date: 2020-09-26 01:30:44 Functions: 76 76 100.0 %

          Line data    Source code
       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/ref.h>
      14             : #include <util/time.h>
      15             : 
      16             : #include <boost/algorithm/string.hpp>
      17             : #include <boost/test/unit_test.hpp>
      18             : 
      19             : #include <univalue.h>
      20             : 
      21             : #include <rpc/blockchain.h>
      22             : 
      23          20 : class RPCTestingSetup : public TestingSetup
      24             : {
      25             : public:
      26             :     UniValue CallRPC(std::string args);
      27             : };
      28             : 
      29          60 : UniValue RPCTestingSetup::CallRPC(std::string args)
      30             : {
      31          60 :     std::vector<std::string> vArgs;
      32          60 :     boost::split(vArgs, args, boost::is_any_of(" \t"));
      33          60 :     std::string strMethod = vArgs[0];
      34          60 :     vArgs.erase(vArgs.begin());
      35          60 :     util::Ref context{m_node};
      36          60 :     JSONRPCRequest request(context);
      37          60 :     request.strMethod = strMethod;
      38          60 :     request.params = RPCConvertValues(strMethod, vArgs);
      39          55 :     request.fHelp = false;
      40          55 :     if (RPCIsInWarmup(nullptr)) SetRPCWarmupFinished();
      41             :     try {
      42          55 :         UniValue result = tableRPC.execute(request);
      43             :         return result;
      44          55 :     }
      45             :     catch (const UniValue& objError) {
      46          19 :         throw std::runtime_error(find_value(objError, "message").get_str());
      47          19 :     }
      48          98 : }
      49             : 
      50             : 
      51          89 : BOOST_FIXTURE_TEST_SUITE(rpc_tests, RPCTestingSetup)
      52             : 
      53          95 : BOOST_AUTO_TEST_CASE(rpc_rawparams)
      54             : {
      55             :     // Test raw transaction API argument handling
      56           1 :     UniValue r;
      57             : 
      58           1 :     BOOST_CHECK_THROW(CallRPC("getrawtransaction"), std::runtime_error);
      59           1 :     BOOST_CHECK_THROW(CallRPC("getrawtransaction not_hex"), std::runtime_error);
      60           1 :     BOOST_CHECK_THROW(CallRPC("getrawtransaction a3b807410df0b60fcb9736768df5823938b2f838694939ba45f3c0a1bff150ed not_int"), std::runtime_error);
      61             : 
      62           1 :     BOOST_CHECK_THROW(CallRPC("createrawtransaction"), std::runtime_error);
      63           1 :     BOOST_CHECK_THROW(CallRPC("createrawtransaction null null"), std::runtime_error);
      64           1 :     BOOST_CHECK_THROW(CallRPC("createrawtransaction not_array"), std::runtime_error);
      65           1 :     BOOST_CHECK_THROW(CallRPC("createrawtransaction {} {}"), std::runtime_error);
      66           1 :     BOOST_CHECK_NO_THROW(CallRPC("createrawtransaction [] {}"));
      67           1 :     BOOST_CHECK_THROW(CallRPC("createrawtransaction [] {} extra"), std::runtime_error);
      68             : 
      69           1 :     BOOST_CHECK_THROW(CallRPC("decoderawtransaction"), std::runtime_error);
      70           1 :     BOOST_CHECK_THROW(CallRPC("decoderawtransaction null"), std::runtime_error);
      71           1 :     BOOST_CHECK_THROW(CallRPC("decoderawtransaction DEADBEEF"), std::runtime_error);
      72           1 :     std::string rawtx = "0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000";
      73           1 :     BOOST_CHECK_NO_THROW(r = CallRPC(std::string("decoderawtransaction ")+rawtx));
      74           1 :     BOOST_CHECK_EQUAL(find_value(r.get_obj(), "size").get_int(), 193);
      75           1 :     BOOST_CHECK_EQUAL(find_value(r.get_obj(), "version").get_int(), 1);
      76           1 :     BOOST_CHECK_EQUAL(find_value(r.get_obj(), "locktime").get_int(), 0);
      77           1 :     BOOST_CHECK_THROW(CallRPC(std::string("decoderawtransaction ")+rawtx+" extra"), std::runtime_error);
      78           1 :     BOOST_CHECK_NO_THROW(r = CallRPC(std::string("decoderawtransaction ")+rawtx+" false"));
      79           1 :     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           1 :     BOOST_CHECK_THROW(CallRPC("sendrawtransaction"), std::runtime_error);
      83           1 :     BOOST_CHECK_THROW(CallRPC("sendrawtransaction null"), std::runtime_error);
      84           1 :     BOOST_CHECK_THROW(CallRPC("sendrawtransaction DEADBEEF"), std::runtime_error);
      85           1 :     BOOST_CHECK_THROW(CallRPC(std::string("sendrawtransaction ")+rawtx+" extra"), std::runtime_error);
      86          18 : }
      87             : 
      88          95 : BOOST_AUTO_TEST_CASE(rpc_togglenetwork)
      89             : {
      90           1 :     UniValue r;
      91             : 
      92           1 :     r = CallRPC("getnetworkinfo");
      93           1 :     bool netState = find_value(r.get_obj(), "networkactive").get_bool();
      94           1 :     BOOST_CHECK_EQUAL(netState, true);
      95             : 
      96           1 :     BOOST_CHECK_NO_THROW(CallRPC("setnetworkactive false"));
      97           1 :     r = CallRPC("getnetworkinfo");
      98           1 :     int numConnection = find_value(r.get_obj(), "connections").get_int();
      99           1 :     BOOST_CHECK_EQUAL(numConnection, 0);
     100             : 
     101           1 :     netState = find_value(r.get_obj(), "networkactive").get_bool();
     102           1 :     BOOST_CHECK_EQUAL(netState, false);
     103             : 
     104           1 :     BOOST_CHECK_NO_THROW(CallRPC("setnetworkactive true"));
     105           1 :     r = CallRPC("getnetworkinfo");
     106           1 :     netState = find_value(r.get_obj(), "networkactive").get_bool();
     107           1 :     BOOST_CHECK_EQUAL(netState, true);
     108           1 : }
     109             : 
     110          95 : BOOST_AUTO_TEST_CASE(rpc_rawsign)
     111             : {
     112           1 :     UniValue r;
     113             :     // input is a 1-of-2 multisig (so is output):
     114           1 :     std::string prevout =
     115           1 :       "[{\"txid\":\"b4cc287e58f87cdae59417329f710f3ecd75a4ee1d2872b7248f50977c8493f3\","
     116             :       "\"vout\":1,\"scriptPubKey\":\"a914b10c9df5f7edf436c697f02f1efdba4cf399615187\","
     117             :       "\"redeemScript\":\"512103debedc17b3df2badbcdd86d5feb4562b86fe182e5998abd8bcd4f122c6155b1b21027e940bb73ab8732bfdf7f9216ecefca5b94d6df834e77e108f68e66f126044c052ae\"}]";
     118           1 :     r = CallRPC(std::string("createrawtransaction ")+prevout+" "+
     119             :       "{\"3HqAe9LtNBjnsfM4CyYaWTnvCaUYT7v4oZ\":11}");
     120           1 :     std::string notsigned = r.get_str();
     121           1 :     std::string privkey1 = "\"KzsXybp9jX64P5ekX1KUxRQ79Jht9uzW7LorgwE65i5rWACL6LQe\"";
     122           1 :     std::string privkey2 = "\"Kyhdf5LuKTRx4ge69ybABsiUAWjVRK4XGxAKk2FQLp2HjGMy87Z4\"";
     123           1 :     r = CallRPC(std::string("signrawtransactionwithkey ")+notsigned+" [] "+prevout);
     124           1 :     BOOST_CHECK(find_value(r.get_obj(), "complete").get_bool() == false);
     125           1 :     r = CallRPC(std::string("signrawtransactionwithkey ")+notsigned+" ["+privkey1+","+privkey2+"] "+prevout);
     126           1 :     BOOST_CHECK(find_value(r.get_obj(), "complete").get_bool() == true);
     127           1 : }
     128             : 
     129          95 : BOOST_AUTO_TEST_CASE(rpc_createraw_op_return)
     130             : {
     131           1 :     BOOST_CHECK_NO_THROW(CallRPC("createrawtransaction [{\"txid\":\"a3b807410df0b60fcb9736768df5823938b2f838694939ba45f3c0a1bff150ed\",\"vout\":0}] {\"data\":\"68656c6c6f776f726c64\"}"));
     132             : 
     133             :     // Key not "data" (bad address)
     134           1 :     BOOST_CHECK_THROW(CallRPC("createrawtransaction [{\"txid\":\"a3b807410df0b60fcb9736768df5823938b2f838694939ba45f3c0a1bff150ed\",\"vout\":0}] {\"somedata\":\"68656c6c6f776f726c64\"}"), std::runtime_error);
     135             : 
     136             :     // Bad hex encoding of data output
     137           1 :     BOOST_CHECK_THROW(CallRPC("createrawtransaction [{\"txid\":\"a3b807410df0b60fcb9736768df5823938b2f838694939ba45f3c0a1bff150ed\",\"vout\":0}] {\"data\":\"12345\"}"), std::runtime_error);
     138           1 :     BOOST_CHECK_THROW(CallRPC("createrawtransaction [{\"txid\":\"a3b807410df0b60fcb9736768df5823938b2f838694939ba45f3c0a1bff150ed\",\"vout\":0}] {\"data\":\"12345g\"}"), std::runtime_error);
     139             : 
     140             :     // Data 81 bytes long
     141           1 :     BOOST_CHECK_NO_THROW(CallRPC("createrawtransaction [{\"txid\":\"a3b807410df0b60fcb9736768df5823938b2f838694939ba45f3c0a1bff150ed\",\"vout\":0}] {\"data\":\"010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081\"}"));
     142           4 : }
     143             : 
     144          95 : BOOST_AUTO_TEST_CASE(rpc_format_monetary_values)
     145             : {
     146           1 :     BOOST_CHECK(ValueFromAmount(0LL).write() == "0.00000000");
     147           1 :     BOOST_CHECK(ValueFromAmount(1LL).write() == "0.00000001");
     148           1 :     BOOST_CHECK(ValueFromAmount(17622195LL).write() == "0.17622195");
     149           1 :     BOOST_CHECK(ValueFromAmount(50000000LL).write() == "0.50000000");
     150           1 :     BOOST_CHECK(ValueFromAmount(89898989LL).write() == "0.89898989");
     151           1 :     BOOST_CHECK(ValueFromAmount(100000000LL).write() == "1.00000000");
     152           1 :     BOOST_CHECK(ValueFromAmount(2099999999999990LL).write() == "20999999.99999990");
     153           1 :     BOOST_CHECK(ValueFromAmount(2099999999999999LL).write() == "20999999.99999999");
     154             : 
     155           1 :     BOOST_CHECK_EQUAL(ValueFromAmount(0).write(), "0.00000000");
     156           1 :     BOOST_CHECK_EQUAL(ValueFromAmount((COIN/10000)*123456789).write(), "12345.67890000");
     157           1 :     BOOST_CHECK_EQUAL(ValueFromAmount(-COIN).write(), "-1.00000000");
     158           1 :     BOOST_CHECK_EQUAL(ValueFromAmount(-COIN/10).write(), "-0.10000000");
     159             : 
     160           1 :     BOOST_CHECK_EQUAL(ValueFromAmount(COIN*100000000).write(), "100000000.00000000");
     161           1 :     BOOST_CHECK_EQUAL(ValueFromAmount(COIN*10000000).write(), "10000000.00000000");
     162           1 :     BOOST_CHECK_EQUAL(ValueFromAmount(COIN*1000000).write(), "1000000.00000000");
     163           1 :     BOOST_CHECK_EQUAL(ValueFromAmount(COIN*100000).write(), "100000.00000000");
     164           1 :     BOOST_CHECK_EQUAL(ValueFromAmount(COIN*10000).write(), "10000.00000000");
     165           1 :     BOOST_CHECK_EQUAL(ValueFromAmount(COIN*1000).write(), "1000.00000000");
     166           1 :     BOOST_CHECK_EQUAL(ValueFromAmount(COIN*100).write(), "100.00000000");
     167           1 :     BOOST_CHECK_EQUAL(ValueFromAmount(COIN*10).write(), "10.00000000");
     168           1 :     BOOST_CHECK_EQUAL(ValueFromAmount(COIN).write(), "1.00000000");
     169           1 :     BOOST_CHECK_EQUAL(ValueFromAmount(COIN/10).write(), "0.10000000");
     170           1 :     BOOST_CHECK_EQUAL(ValueFromAmount(COIN/100).write(), "0.01000000");
     171           1 :     BOOST_CHECK_EQUAL(ValueFromAmount(COIN/1000).write(), "0.00100000");
     172           1 :     BOOST_CHECK_EQUAL(ValueFromAmount(COIN/10000).write(), "0.00010000");
     173           1 :     BOOST_CHECK_EQUAL(ValueFromAmount(COIN/100000).write(), "0.00001000");
     174           1 :     BOOST_CHECK_EQUAL(ValueFromAmount(COIN/1000000).write(), "0.00000100");
     175           1 :     BOOST_CHECK_EQUAL(ValueFromAmount(COIN/10000000).write(), "0.00000010");
     176           1 :     BOOST_CHECK_EQUAL(ValueFromAmount(COIN/100000000).write(), "0.00000001");
     177           1 : }
     178             : 
     179          26 : static UniValue ValueFromString(const std::string &str)
     180             : {
     181          26 :     UniValue value;
     182          26 :     BOOST_CHECK(value.setNumStr(str));
     183             :     return value;
     184          26 : }
     185             : 
     186          95 : BOOST_AUTO_TEST_CASE(rpc_parse_monetary_values)
     187             : {
     188           1 :     BOOST_CHECK_THROW(AmountFromValue(ValueFromString("-0.00000001")), UniValue);
     189           1 :     BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0")), 0LL);
     190           1 :     BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.00000000")), 0LL);
     191           1 :     BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.00000001")), 1LL);
     192           1 :     BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.17622195")), 17622195LL);
     193           1 :     BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.5")), 50000000LL);
     194           1 :     BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.50000000")), 50000000LL);
     195           1 :     BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.89898989")), 89898989LL);
     196           1 :     BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("1.00000000")), 100000000LL);
     197           1 :     BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("20999999.9999999")), 2099999999999990LL);
     198           1 :     BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("20999999.99999999")), 2099999999999999LL);
     199             : 
     200           1 :     BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("1e-8")), COIN/100000000);
     201           1 :     BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.1e-7")), COIN/100000000);
     202           1 :     BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.01e-6")), COIN/100000000);
     203           1 :     BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.0000000000000000000000000000000000000000000000000000000000000000000000000001e+68")), COIN/100000000);
     204           1 :     BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("10000000000000000000000000000000000000000000000000000000000000000e-64")), COIN);
     205           1 :     BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000e64")), COIN);
     206             : 
     207           1 :     BOOST_CHECK_THROW(AmountFromValue(ValueFromString("1e-9")), UniValue); //should fail
     208           1 :     BOOST_CHECK_THROW(AmountFromValue(ValueFromString("0.000000019")), UniValue); //should fail
     209           1 :     BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.00000001000000")), 1LL); //should pass, cut trailing 0
     210           1 :     BOOST_CHECK_THROW(AmountFromValue(ValueFromString("19e-9")), UniValue); //should fail
     211           1 :     BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.19e-6")), 19); //should pass, leading 0 is present
     212             : 
     213           1 :     BOOST_CHECK_THROW(AmountFromValue(ValueFromString("92233720368.54775808")), UniValue); //overflow error
     214           1 :     BOOST_CHECK_THROW(AmountFromValue(ValueFromString("1e+11")), UniValue); //overflow error
     215           1 :     BOOST_CHECK_THROW(AmountFromValue(ValueFromString("1e11")), UniValue); //overflow error signless
     216           1 :     BOOST_CHECK_THROW(AmountFromValue(ValueFromString("93e+9")), UniValue); //overflow error
     217           9 : }
     218             : 
     219          95 : BOOST_AUTO_TEST_CASE(json_parse_errors)
     220             : {
     221             :     // Valid
     222           1 :     BOOST_CHECK_EQUAL(ParseNonRFCJSONValue("1.0").get_real(), 1.0);
     223             :     // Valid, with leading or trailing whitespace
     224           1 :     BOOST_CHECK_EQUAL(ParseNonRFCJSONValue(" 1.0").get_real(), 1.0);
     225           1 :     BOOST_CHECK_EQUAL(ParseNonRFCJSONValue("1.0 ").get_real(), 1.0);
     226             : 
     227           2 :     BOOST_CHECK_THROW(AmountFromValue(ParseNonRFCJSONValue(".19e-6")), std::runtime_error); //should fail, missing leading 0, therefore invalid JSON
     228           1 :     BOOST_CHECK_EQUAL(AmountFromValue(ParseNonRFCJSONValue("0.00000000000000000000000000000000000001e+30 ")), 1);
     229             :     // Invalid, initial garbage
     230           1 :     BOOST_CHECK_THROW(ParseNonRFCJSONValue("[1.0"), std::runtime_error);
     231           1 :     BOOST_CHECK_THROW(ParseNonRFCJSONValue("a1.0"), std::runtime_error);
     232             :     // Invalid, trailing garbage
     233           1 :     BOOST_CHECK_THROW(ParseNonRFCJSONValue("1.0sds"), std::runtime_error);
     234           1 :     BOOST_CHECK_THROW(ParseNonRFCJSONValue("1.0]"), std::runtime_error);
     235             :     // BTC addresses should fail parsing
     236           1 :     BOOST_CHECK_THROW(ParseNonRFCJSONValue("175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W"), std::runtime_error);
     237           1 :     BOOST_CHECK_THROW(ParseNonRFCJSONValue("3J98t1WpEZ73CNmQviecrnyiWrnqRhWNL"), std::runtime_error);
     238           8 : }
     239             : 
     240          95 : BOOST_AUTO_TEST_CASE(rpc_ban)
     241             : {
     242           1 :     BOOST_CHECK_NO_THROW(CallRPC(std::string("clearbanned")));
     243             : 
     244           1 :     UniValue r;
     245           1 :     BOOST_CHECK_NO_THROW(r = CallRPC(std::string("setban 127.0.0.0 add")));
     246           1 :     BOOST_CHECK_THROW(r = CallRPC(std::string("setban 127.0.0.0:8334")), std::runtime_error); //portnumber for setban not allowed
     247           1 :     BOOST_CHECK_NO_THROW(r = CallRPC(std::string("listbanned")));
     248           1 :     UniValue ar = r.get_array();
     249           1 :     UniValue o1 = ar[0].get_obj();
     250           1 :     UniValue adr = find_value(o1, "address");
     251           1 :     BOOST_CHECK_EQUAL(adr.get_str(), "127.0.0.0/32");
     252           1 :     BOOST_CHECK_NO_THROW(CallRPC(std::string("setban 127.0.0.0 remove")));
     253           1 :     BOOST_CHECK_NO_THROW(r = CallRPC(std::string("listbanned")));
     254           1 :     ar = r.get_array();
     255           1 :     BOOST_CHECK_EQUAL(ar.size(), 0U);
     256             : 
     257           1 :     BOOST_CHECK_NO_THROW(r = CallRPC(std::string("setban 127.0.0.0/24 add 9907731200 true")));
     258           1 :     BOOST_CHECK_NO_THROW(r = CallRPC(std::string("listbanned")));
     259           1 :     ar = r.get_array();
     260           1 :     o1 = ar[0].get_obj();
     261           1 :     adr = find_value(o1, "address");
     262           1 :     UniValue banned_until = find_value(o1, "banned_until");
     263           1 :     BOOST_CHECK_EQUAL(adr.get_str(), "127.0.0.0/24");
     264           1 :     BOOST_CHECK_EQUAL(banned_until.get_int64(), 9907731200); // absolute time check
     265             : 
     266           1 :     BOOST_CHECK_NO_THROW(CallRPC(std::string("clearbanned")));
     267             : 
     268           1 :     BOOST_CHECK_NO_THROW(r = CallRPC(std::string("setban 127.0.0.0/24 add 200")));
     269           1 :     BOOST_CHECK_NO_THROW(r = CallRPC(std::string("listbanned")));
     270           1 :     ar = r.get_array();
     271           1 :     o1 = ar[0].get_obj();
     272           1 :     adr = find_value(o1, "address");
     273           1 :     banned_until = find_value(o1, "banned_until");
     274           1 :     BOOST_CHECK_EQUAL(adr.get_str(), "127.0.0.0/24");
     275           1 :     int64_t now = GetTime();
     276           1 :     BOOST_CHECK(banned_until.get_int64() > now);
     277           1 :     BOOST_CHECK(banned_until.get_int64()-now <= 200);
     278             : 
     279             :     // must throw an exception because 127.0.0.1 is in already banned subnet range
     280           1 :     BOOST_CHECK_THROW(r = CallRPC(std::string("setban 127.0.0.1 add")), std::runtime_error);
     281             : 
     282           1 :     BOOST_CHECK_NO_THROW(CallRPC(std::string("setban 127.0.0.0/24 remove")));
     283           1 :     BOOST_CHECK_NO_THROW(r = CallRPC(std::string("listbanned")));
     284           1 :     ar = r.get_array();
     285           1 :     BOOST_CHECK_EQUAL(ar.size(), 0U);
     286             : 
     287           1 :     BOOST_CHECK_NO_THROW(r = CallRPC(std::string("setban 127.0.0.0/255.255.0.0 add")));
     288           1 :     BOOST_CHECK_THROW(r = CallRPC(std::string("setban 127.0.1.1 add")), std::runtime_error);
     289             : 
     290           1 :     BOOST_CHECK_NO_THROW(CallRPC(std::string("clearbanned")));
     291           1 :     BOOST_CHECK_NO_THROW(r = CallRPC(std::string("listbanned")));
     292           1 :     ar = r.get_array();
     293           1 :     BOOST_CHECK_EQUAL(ar.size(), 0U);
     294             : 
     295             : 
     296           1 :     BOOST_CHECK_THROW(r = CallRPC(std::string("setban test add")), std::runtime_error); //invalid IP
     297             : 
     298             :     //IPv6 tests
     299           1 :     BOOST_CHECK_NO_THROW(r = CallRPC(std::string("setban FE80:0000:0000:0000:0202:B3FF:FE1E:8329 add")));
     300           1 :     BOOST_CHECK_NO_THROW(r = CallRPC(std::string("listbanned")));
     301           1 :     ar = r.get_array();
     302           1 :     o1 = ar[0].get_obj();
     303           1 :     adr = find_value(o1, "address");
     304           1 :     BOOST_CHECK_EQUAL(adr.get_str(), "fe80::202:b3ff:fe1e:8329/128");
     305             : 
     306           1 :     BOOST_CHECK_NO_THROW(CallRPC(std::string("clearbanned")));
     307           1 :     BOOST_CHECK_NO_THROW(r = CallRPC(std::string("setban 2001:db8::/ffff:fffc:0:0:0:0:0:0 add")));
     308           1 :     BOOST_CHECK_NO_THROW(r = CallRPC(std::string("listbanned")));
     309           1 :     ar = r.get_array();
     310           1 :     o1 = ar[0].get_obj();
     311           1 :     adr = find_value(o1, "address");
     312           1 :     BOOST_CHECK_EQUAL(adr.get_str(), "2001:db8::/30");
     313             : 
     314           1 :     BOOST_CHECK_NO_THROW(CallRPC(std::string("clearbanned")));
     315           1 :     BOOST_CHECK_NO_THROW(r = CallRPC(std::string("setban 2001:4d48:ac57:400:cacf:e9ff:fe1d:9c63/128 add")));
     316           1 :     BOOST_CHECK_NO_THROW(r = CallRPC(std::string("listbanned")));
     317           1 :     ar = r.get_array();
     318           1 :     o1 = ar[0].get_obj();
     319           1 :     adr = find_value(o1, "address");
     320           1 :     BOOST_CHECK_EQUAL(adr.get_str(), "2001:4d48:ac57:400:cacf:e9ff:fe1d:9c63/128");
     321           5 : }
     322             : 
     323          95 : BOOST_AUTO_TEST_CASE(rpc_convert_values_generatetoaddress)
     324             : {
     325           1 :     UniValue result;
     326             : 
     327           2 :     BOOST_CHECK_NO_THROW(result = RPCConvertValues("generatetoaddress", {"101", "mkESjLZW66TmHhiFX8MCaBjrhZ543PPh9a"}));
     328           1 :     BOOST_CHECK_EQUAL(result[0].get_int(), 101);
     329           1 :     BOOST_CHECK_EQUAL(result[1].get_str(), "mkESjLZW66TmHhiFX8MCaBjrhZ543PPh9a");
     330             : 
     331           2 :     BOOST_CHECK_NO_THROW(result = RPCConvertValues("generatetoaddress", {"101", "mhMbmE2tE9xzJYCV9aNC8jKWN31vtGrguU"}));
     332           1 :     BOOST_CHECK_EQUAL(result[0].get_int(), 101);
     333           1 :     BOOST_CHECK_EQUAL(result[1].get_str(), "mhMbmE2tE9xzJYCV9aNC8jKWN31vtGrguU");
     334             : 
     335           3 :     BOOST_CHECK_NO_THROW(result = RPCConvertValues("generatetoaddress", {"1", "mkESjLZW66TmHhiFX8MCaBjrhZ543PPh9a", "9"}));
     336           1 :     BOOST_CHECK_EQUAL(result[0].get_int(), 1);
     337           1 :     BOOST_CHECK_EQUAL(result[1].get_str(), "mkESjLZW66TmHhiFX8MCaBjrhZ543PPh9a");
     338           1 :     BOOST_CHECK_EQUAL(result[2].get_int(), 9);
     339             : 
     340           3 :     BOOST_CHECK_NO_THROW(result = RPCConvertValues("generatetoaddress", {"1", "mhMbmE2tE9xzJYCV9aNC8jKWN31vtGrguU", "9"}));
     341           1 :     BOOST_CHECK_EQUAL(result[0].get_int(), 1);
     342           1 :     BOOST_CHECK_EQUAL(result[1].get_str(), "mhMbmE2tE9xzJYCV9aNC8jKWN31vtGrguU");
     343           1 :     BOOST_CHECK_EQUAL(result[2].get_int(), 9);
     344           1 : }
     345             : 
     346          95 : BOOST_AUTO_TEST_CASE(rpc_getblockstats_calculate_percentiles_by_weight)
     347             : {
     348             :     int64_t total_weight = 200;
     349           1 :     std::vector<std::pair<CAmount, int64_t>> feerates;
     350           1 :     CAmount result[NUM_GETBLOCKSTATS_PERCENTILES] = { 0 };
     351             : 
     352         101 :     for (int64_t i = 0; i < 100; i++) {
     353         100 :         feerates.emplace_back(std::make_pair(1 ,1));
     354             :     }
     355             : 
     356         101 :     for (int64_t i = 0; i < 100; i++) {
     357         100 :         feerates.emplace_back(std::make_pair(2 ,1));
     358             :     }
     359             : 
     360           1 :     CalculatePercentilesByWeight(result, feerates, total_weight);
     361           1 :     BOOST_CHECK_EQUAL(result[0], 1);
     362           1 :     BOOST_CHECK_EQUAL(result[1], 1);
     363           1 :     BOOST_CHECK_EQUAL(result[2], 1);
     364           1 :     BOOST_CHECK_EQUAL(result[3], 2);
     365           1 :     BOOST_CHECK_EQUAL(result[4], 2);
     366             : 
     367             :     // Test with more pairs, and two pairs overlapping 2 percentiles.
     368             :     total_weight = 100;
     369           1 :     CAmount result2[NUM_GETBLOCKSTATS_PERCENTILES] = { 0 };
     370           1 :     feerates.clear();
     371             : 
     372           1 :     feerates.emplace_back(std::make_pair(1, 9));
     373           1 :     feerates.emplace_back(std::make_pair(2 , 16)); //10th + 25th percentile
     374           1 :     feerates.emplace_back(std::make_pair(4 ,50)); //50th + 75th percentile
     375           1 :     feerates.emplace_back(std::make_pair(5 ,10));
     376           1 :     feerates.emplace_back(std::make_pair(9 ,15));  // 90th percentile
     377             : 
     378           1 :     CalculatePercentilesByWeight(result2, feerates, total_weight);
     379             : 
     380           1 :     BOOST_CHECK_EQUAL(result2[0], 2);
     381           1 :     BOOST_CHECK_EQUAL(result2[1], 2);
     382           1 :     BOOST_CHECK_EQUAL(result2[2], 4);
     383           1 :     BOOST_CHECK_EQUAL(result2[3], 4);
     384           1 :     BOOST_CHECK_EQUAL(result2[4], 9);
     385             : 
     386             :     // Same test as above, but one of the percentile-overlapping pairs is split in 2.
     387             :     total_weight = 100;
     388           1 :     CAmount result3[NUM_GETBLOCKSTATS_PERCENTILES] = { 0 };
     389           1 :     feerates.clear();
     390             : 
     391           1 :     feerates.emplace_back(std::make_pair(1, 9));
     392           1 :     feerates.emplace_back(std::make_pair(2 , 11)); // 10th percentile
     393           1 :     feerates.emplace_back(std::make_pair(2 , 5)); // 25th percentile
     394           1 :     feerates.emplace_back(std::make_pair(4 ,50)); //50th + 75th percentile
     395           1 :     feerates.emplace_back(std::make_pair(5 ,10));
     396           1 :     feerates.emplace_back(std::make_pair(9 ,15)); // 90th percentile
     397             : 
     398           1 :     CalculatePercentilesByWeight(result3, feerates, total_weight);
     399             : 
     400           1 :     BOOST_CHECK_EQUAL(result3[0], 2);
     401           1 :     BOOST_CHECK_EQUAL(result3[1], 2);
     402           1 :     BOOST_CHECK_EQUAL(result3[2], 4);
     403           1 :     BOOST_CHECK_EQUAL(result3[3], 4);
     404           1 :     BOOST_CHECK_EQUAL(result3[4], 9);
     405             : 
     406             :     // Test with one transaction spanning all percentiles.
     407             :     total_weight = 104;
     408           1 :     CAmount result4[NUM_GETBLOCKSTATS_PERCENTILES] = { 0 };
     409           1 :     feerates.clear();
     410             : 
     411           1 :     feerates.emplace_back(std::make_pair(1, 100));
     412           1 :     feerates.emplace_back(std::make_pair(2, 1));
     413           1 :     feerates.emplace_back(std::make_pair(3, 1));
     414           1 :     feerates.emplace_back(std::make_pair(3, 1));
     415           1 :     feerates.emplace_back(std::make_pair(999999, 1));
     416             : 
     417           1 :     CalculatePercentilesByWeight(result4, feerates, total_weight);
     418             : 
     419           6 :     for (int64_t i = 0; i < NUM_GETBLOCKSTATS_PERCENTILES; i++) {
     420           5 :         BOOST_CHECK_EQUAL(result4[i], 1);
     421             :     }
     422           1 : }
     423             : 
     424          89 : BOOST_AUTO_TEST_SUITE_END()

Generated by: LCOV version 1.15