18#include <boost/test/unit_test.hpp>
62 return transformed_params;
68 std::string strMethod = vArgs[0];
69 vArgs.erase(vArgs.begin());
89 const std::vector<std::pair<std::string, bool>> arg_names{{
"arg1",
false}, {
"arg2",
false}, {
"arg3",
false}, {
"arg4",
false}, {
"arg5",
false}};
92 BOOST_CHECK_EQUAL(TransformParams(
JSON(R
"({"arg2": 2, "arg4": 4})"), arg_names).write(), "[null,2,null,4]");
95 BOOST_CHECK_EXCEPTION(TransformParams(
JSON(R
"({"arg2": 2, "arg2": 4})"), arg_names), UniValue,
96 HasJSON(R"({"code":-8,"message":"Parameter arg2 specified multiple times"})"));
99 BOOST_CHECK_EQUAL(TransformParams(
JSON(R
"({"arg5": 5, "args": [1, 2], "arg4": 4})"), arg_names).write(), "[1,2,null,4,5]");
102 BOOST_CHECK_EXCEPTION(TransformParams(
JSON(R
"({"arg2": 2, "unknown": 6})"), arg_names), UniValue,
103 HasJSON(R"({"code":-8,"message":"Unknown named parameter unknown"})"));
106 BOOST_CHECK_EXCEPTION(TransformParams(
JSON(R
"({"args": [1,2,3], "arg4": 4, "arg2": 2})"), arg_names), UniValue,
107 HasJSON(R"({"code":-8,"message":"Parameter arg2 specified twice both as positional and named argument"})"));
110 BOOST_CHECK_EQUAL(TransformParams(
JSON(R
"({"args": [1,2,3,4,5,6,7,8,9,10]})"), arg_names).write(), "[1,2,3,4,5,6,7,8,9,10]");
111 BOOST_CHECK_EQUAL(TransformParams(
JSON(R
"([1,2,3,4,5,6,7,8,9,10])"), arg_names).write(), "[1,2,3,4,5,6,7,8,9,10]");
116 const std::vector<std::pair<std::string, bool>> arg_names{{
"arg1",
false}, {
"arg2",
false}, {
"opt1",
true}, {
"opt2",
true}, {
"options",
false}};
122 BOOST_CHECK_EQUAL(TransformParams(
JSON(R
"({"arg1": 1, "arg2": 2, "opt1": 10, "opt2": 20})"), arg_names).write(), R"([1,2,{"opt1":10,"opt2":20}])");
125 BOOST_CHECK_EQUAL(TransformParams(
JSON(R
"({"arg1": 1, "arg2": 2, "options":{"opt1": 10, "opt2": 20}})"), arg_names).write(), R"([1,2,{"opt1":10,"opt2":20}])");
128 BOOST_CHECK_EXCEPTION(TransformParams(
JSON(R
"({"arg1": 1, "arg2": 2, "opt1": 10, "options":{"opt1": 10}})"), arg_names), UniValue,
129 HasJSON(R"({"code":-8,"message":"Parameter options conflicts with parameter opt1"})"));
132 BOOST_CHECK_EXCEPTION(TransformParams(
JSON(R
"({"args": [1, 2, {"opt1": 10}], "opt2": 20})"), arg_names), UniValue,
133 HasJSON(R"({"code":-8,"message":"Parameter options specified twice both as positional and named argument"})"));
143 BOOST_CHECK_THROW(
CallRPC(
"getrawtransaction a3b807410df0b60fcb9736768df5823938b2f838694939ba45f3c0a1bff150ed not_int"), std::runtime_error);
155 std::string rawtx =
"0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000";
197 std::string prevout =
198 "[{\"txid\":\"b4cc287e58f87cdae59417329f710f3ecd75a4ee1d2872b7248f50977c8493f3\","
199 "\"vout\":1,\"scriptPubKey\":\"a914b10c9df5f7edf436c697f02f1efdba4cf399615187\","
200 "\"redeemScript\":\"512103debedc17b3df2badbcdd86d5feb4562b86fe182e5998abd8bcd4f122c6155b1b21027e940bb73ab8732bfdf7f9216ecefca5b94d6df834e77e108f68e66f126044c052ae\"}]";
201 r =
CallRPC(std::string(
"createrawtransaction ")+prevout+
" "+
202 "{\"3HqAe9LtNBjnsfM4CyYaWTnvCaUYT7v4oZ\":11}");
203 std::string notsigned = r.
get_str();
204 std::string privkey1 =
"\"KzsXybp9jX64P5ekX1KUxRQ79Jht9uzW7LorgwE65i5rWACL6LQe\"";
205 std::string privkey2 =
"\"Kyhdf5LuKTRx4ge69ybABsiUAWjVRK4XGxAKk2FQLp2HjGMy87Z4\"";
206 r =
CallRPC(std::string(
"signrawtransactionwithkey ")+notsigned+
" [] "+prevout);
208 r =
CallRPC(std::string(
"signrawtransactionwithkey ")+notsigned+
" ["+privkey1+
","+privkey2+
"] "+prevout);
214 BOOST_CHECK_NO_THROW(
CallRPC(
"createrawtransaction [{\"txid\":\"a3b807410df0b60fcb9736768df5823938b2f838694939ba45f3c0a1bff150ed\",\"vout\":0}] {\"data\":\"68656c6c6f776f726c64\"}"));
217 BOOST_CHECK_THROW(
CallRPC(
"createrawtransaction [{\"txid\":\"a3b807410df0b60fcb9736768df5823938b2f838694939ba45f3c0a1bff150ed\",\"vout\":0}] {\"somedata\":\"68656c6c6f776f726c64\"}"), std::runtime_error);
220 BOOST_CHECK_THROW(
CallRPC(
"createrawtransaction [{\"txid\":\"a3b807410df0b60fcb9736768df5823938b2f838694939ba45f3c0a1bff150ed\",\"vout\":0}] {\"data\":\"12345\"}"), std::runtime_error);
221 BOOST_CHECK_THROW(
CallRPC(
"createrawtransaction [{\"txid\":\"a3b807410df0b60fcb9736768df5823938b2f838694939ba45f3c0a1bff150ed\",\"vout\":0}] {\"data\":\"12345g\"}"), std::runtime_error);
224 BOOST_CHECK_NO_THROW(
CallRPC(
"createrawtransaction [{\"txid\":\"a3b807410df0b60fcb9736768df5823938b2f838694939ba45f3c0a1bff150ed\",\"vout\":0}] {\"data\":\"010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081\"}"));
346 const int64_t time_remaining_expected{198};
353 const int64_t ban_duration{o1.
find_value(
"ban_duration").
getInt<int64_t>()};
354 const int64_t time_remaining{o1.
find_value(
"time_remaining").
getInt<int64_t>()};
429 int64_t total_weight = 200;
430 std::vector<std::pair<CAmount, int64_t>> feerates;
431 feerates.reserve(200);
434 for (int64_t i = 0; i < 100; i++) {
435 feerates.emplace_back(1 ,1);
438 for (int64_t i = 0; i < 100; i++) {
439 feerates.emplace_back(2 ,1);
454 feerates.emplace_back(1, 9);
455 feerates.emplace_back(2 , 16);
456 feerates.emplace_back(4 ,50);
457 feerates.emplace_back(5 ,10);
458 feerates.emplace_back(9 ,15);
473 feerates.emplace_back(1, 9);
474 feerates.emplace_back(2 , 11);
475 feerates.emplace_back(2 , 5);
476 feerates.emplace_back(4 ,50);
477 feerates.emplace_back(5 ,10);
478 feerates.emplace_back(9 ,15);
493 feerates.emplace_back(1, 100);
494 feerates.emplace_back(2, 1);
495 feerates.emplace_back(3, 1);
496 feerates.emplace_back(3, 1);
497 feerates.emplace_back(999999, 1);
509 enum ParamType { POSITIONAL, NAMED, NAMED_ONLY };
510 auto make_rpc = [](std::vector<std::tuple<std::string, ParamType>> param_names) {
511 std::vector<RPCArg> params;
512 std::vector<RPCArg> options;
514 for (
auto& [param_name, param_type] : param_names) {
515 if (param_type == POSITIONAL) {
527 make_rpc({{
"p1", POSITIONAL}, {
"p2", POSITIONAL}});
528 make_rpc({{
"p1", POSITIONAL}, {
"p2", NAMED}});
529 make_rpc({{
"p1", POSITIONAL}, {
"p2", NAMED_ONLY}});
530 make_rpc({{
"p1", NAMED}, {
"p2", POSITIONAL}});
531 make_rpc({{
"p1", NAMED}, {
"p2", NAMED}});
532 make_rpc({{
"p1", NAMED}, {
"p2", NAMED_ONLY}});
533 make_rpc({{
"p1", NAMED_ONLY}, {
"p2", POSITIONAL}});
534 make_rpc({{
"p1", NAMED_ONLY}, {
"p2", NAMED}});
535 make_rpc({{
"p1", NAMED_ONLY}, {
"p2", NAMED_ONLY}});
540 make_rpc({{
"p1", POSITIONAL}, {
"p1", NAMED}});
542 make_rpc({{
"p1", NAMED}, {
"p1", POSITIONAL}});
558 BOOST_CHECK_EQUAL(
HelpExampleRpcNamed(
"test",
args),
"> curl --user myusername --data-binary '{\"jsonrpc\": \"2.0\", \"id\": \"curltest\", \"method\": \"test\", \"params\": {\"foo\":\"bar\",\"b\":true,\"n\":1}}' -H 'content-type: application/json' http://127.0.0.1:8332/\n");
567 obj_value.
pushKV(
"foo",
"bar");
568 obj_value.
pushKV(
"b",
false);
571 BOOST_CHECK_EQUAL(
HelpExampleRpcNamed(
"test", {{
"name", obj_value}}),
"> curl --user myusername --data-binary '{\"jsonrpc\": \"2.0\", \"id\": \"curltest\", \"method\": \"test\", \"params\": {\"name\":{\"foo\":\"bar\",\"b\":false,\"n\":1}}}' -H 'content-type: application/json' http://127.0.0.1:8332/\n");
579 BOOST_CHECK_EQUAL(
HelpExampleRpcNamed(
"test", {{
"name", arr_value}}),
"> curl --user myusername --data-binary '{\"jsonrpc\": \"2.0\", \"id\": \"curltest\", \"method\": \"test\", \"params\": {\"name\":[\"bar\",false,1]}}' -H 'content-type: application/json' http://127.0.0.1:8332/\n");
595 rpc.HandleRequest(req);
600 constexpr bool DEFAULT_BOOL =
true;
601 constexpr auto DEFAULT_STRING =
"default";
602 constexpr uint64_t DEFAULT_UINT64_T = 3;
605 const std::vector<RPCArg> params{
622 BOOST_CHECK_EQUAL(self.
Arg<uint64_t>(
"def_uint64_t"), request.params[2].isNull() ? DEFAULT_UINT64_T : request.params[2].getInt<uint64_t>());
623 BOOST_CHECK_EQUAL(self.
Arg<std::string>(
"def_string"), request.params[3].isNull() ? DEFAULT_STRING : request.params[3].get_str());
624 BOOST_CHECK_EQUAL(self.
Arg<
bool>(
"def_bool"), request.params[4].isNull() ? DEFAULT_BOOL : request.params[4].get_bool());
625 if (!request.params[5].isNull()) {
630 if (!request.params[6].isNull()) {
638 CheckRpc(params,
UniValue{
JSON(R
"([5, "hello", null, null, null, null, null])")}, check_positional);
639 CheckRpc(params, UniValue{JSON(R"([5, "hello", 4, "test", true, 1.23, "world"])")}, check_positional);
int64_t CAmount
Amount in satoshis (Can be negative)
static constexpr CAmount COIN
The amount of satoshis in one BTC.
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)
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
UniValue execute(const JSONRPCRequest &request) const
Execute a method.
void appendCommand(const std::string &name, const CRPCCommand *pcmd)
Appends a CRPCCommand to the dispatch table.
HasJSON(std::string json)
bool operator()(const UniValue &value) const
std::function< UniValue(const RPCHelpMan &, const JSONRPCRequest &)> RPCMethodImpl
auto Arg(std::string_view key) const
Helper to get a required or default-valued request argument.
auto MaybeArg(std::string_view key) const
Helper to get an optional request argument.
UniValue TransformParams(const UniValue ¶ms, std::vector< std::pair< std::string, bool > > arg_names) const
UniValue CallRPC(std::string args)
void push_back(UniValue val)
const std::string & get_str() const
const UniValue & find_value(std::string_view key) const
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
const UniValue & get_obj() const
bool read(std::string_view raw)
const UniValue & get_array() const
void pushKV(std::string key, UniValue val)
void setNumStr(std::string str)
UniValue RPCConvertValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert positional arguments to command-specific RPC representation.
UniValue ValueFromAmount(const CAmount amount)
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
char const * json() noexcept
Template to generate JSON data.
std::vector< std::string > SplitString(std::string_view str, char sep)
#define BOOST_CHECK_THROW(stmt, excMatch)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK_NO_THROW(stmt)
#define BOOST_CHECK(expr)
std::string HelpExampleRpcNamed(const std::string &methodname, const RPCArgList &args)
std::string HelpExampleCliNamed(const std::string &methodname, const RPCArgList &args)
std::vector< std::pair< std::string, UniValue > > RPCArgList
static void CheckRpc(const std::vector< RPCArg > ¶ms, const UniValue &args, RPCHelpMan::RPCMethodImpl test_impl)
BOOST_AUTO_TEST_CASE(rpc_namedparams)
static UniValue JSON(std::string_view json)
static UniValue ValueFromString(const std::string &str) noexcept
void SetRPCWarmupFinished()
bool RPCIsInWarmup(std::string *outStatus)
@ OBJ_NAMED_PARAMS
Special type that behaves almost exactly like OBJ, defining an options object with a list of pre-defi...
@ OMITTED
Optional argument for which the default value is omitted from help text for one of two reasons:
bool also_positional
If set allows a named-parameter field in an OBJ_NAMED_PARAM options object to have the same name as a...
Testing setup that configures a complete environment.
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.