diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -378,9 +378,8 @@ HelpExampleRpc("getmemoryinfo", "")); } - std::string mode = (request.params.size() < 1 || request.params[0].isNull()) - ? "stats" - : request.params[0].get_str(); + std::string mode = + request.params[0].isNull() ? "stats" : request.params[0].get_str(); if (mode == "stats") { UniValue obj(UniValue::VOBJ); obj.pushKV("locked", RPCLockedMemoryInfo()); @@ -463,11 +462,11 @@ } uint32_t original_log_categories = GetLogger().GetCategoryMask(); - if (request.params.size() > 0 && request.params[0].isArray()) { + if (request.params[0].isArray()) { EnableOrDisableLogCategories(request.params[0], true); } - if (request.params.size() > 1 && request.params[1].isArray()) { + if (request.params[1].isArray()) { EnableOrDisableLogCategories(request.params[1], false); } diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -239,7 +239,7 @@ static UniValue addnode(const Config &config, const JSONRPCRequest &request) { std::string strCommand; - if (request.params.size() == 2) { + if (!request.params[1].isNull()) { strCommand = request.params[1].get_str(); } @@ -323,8 +323,7 @@ bool success; const UniValue &address_arg = request.params[0]; - const UniValue &id_arg = - request.params.size() < 2 ? NullUniValue : request.params[1]; + const UniValue &id_arg = request.params[1]; if (!address_arg.isNull() && id_arg.isNull()) { /* handle disconnect-by-address */ @@ -391,7 +390,7 @@ std::vector vInfo = g_connman->GetAddedNodeInfo(); - if (request.params.size() == 1 && !request.params[0].isNull()) { + if (!request.params[0].isNull()) { bool found = false; for (const AddedNodeInfo &info : vInfo) { if (info.strAddedNode == request.params[0].get_str()) { @@ -616,7 +615,7 @@ static UniValue setban(const Config &config, const JSONRPCRequest &request) { std::string strCommand; - if (request.params.size() >= 2) { + if (!request.params[1].isNull()) { strCommand = request.params[1].get_str(); } @@ -679,12 +678,12 @@ // Use standard bantime if not specified. int64_t banTime = 0; - if (request.params.size() >= 3 && !request.params[2].isNull()) { + if (!request.params[2].isNull()) { banTime = request.params[2].get_int64(); } bool absolute = false; - if (request.params.size() == 4 && request.params[3].isTrue()) { + if (request.params[3].isTrue()) { absolute = true; } diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp --- a/src/rpc/rawtransaction.cpp +++ b/src/rpc/rawtransaction.cpp @@ -475,7 +475,7 @@ CMutableTransaction rawTx; - if (request.params.size() > 2 && !request.params[2].isNull()) { + if (!request.params[2].isNull()) { int64_t nLockTime = request.params[2].get_int64(); if (nLockTime < 0 || nLockTime > std::numeric_limits::max()) { throw JSONRPCError(RPC_INVALID_PARAMETER, @@ -1176,7 +1176,7 @@ const TxId &txid = tx->GetId(); Amount nMaxRawTxFee = maxTxFee; - if (request.params.size() > 1 && request.params[1].get_bool()) { + if (!request.params[1].isNull() && request.params[1].get_bool()) { nMaxRawTxFee = Amount::zero(); } diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -599,17 +599,15 @@ // Wallet comments mapValue_t mapValue; - if (request.params.size() > 2 && !request.params[2].isNull() && - !request.params[2].get_str().empty()) { + if (!request.params[2].isNull() && !request.params[2].get_str().empty()) { mapValue["comment"] = request.params[2].get_str(); } - if (request.params.size() > 3 && !request.params[3].isNull() && - !request.params[3].get_str().empty()) { + if (!request.params[3].isNull() && !request.params[3].get_str().empty()) { mapValue["to"] = request.params[3].get_str(); } bool fSubtractFeeFromAmount = false; - if (request.params.size() > 4) { + if (!request.params[4].isNull()) { fSubtractFeeFromAmount = request.params[4].get_bool(); } @@ -995,21 +993,36 @@ LOCK2(cs_main, pwallet->cs_wallet); - if (request.params.size() == 0) { + const UniValue &account_value = request.params[0]; + const UniValue &minconf = request.params[1]; + const UniValue &include_watchonly = request.params[2]; + + if (account_value.isNull()) { + if (!minconf.isNull()) { + throw JSONRPCError(RPC_INVALID_PARAMETER, + "getbalance minconf option is only currently " + "supported if an account is specified"); + } + if (!include_watchonly.isNull()) { + throw JSONRPCError( + RPC_INVALID_PARAMETER, + "getbalance include_watchonly option is only currently " + "supported if an account is specified"); + } return ValueFromAmount(pwallet->GetBalance()); } - const std::string &account_param = request.params[0].get_str(); + const std::string &account_param = account_value.get_str(); const std::string *account = account_param != "*" ? &account_param : nullptr; int nMinDepth = 1; - if (!request.params[1].isNull()) { - nMinDepth = request.params[1].get_int(); + if (!minconf.isNull()) { + nMinDepth = minconf.get_int(); } isminefilter filter = ISMINE_SPENDABLE; - if (!request.params[2].isNull() && request.params[2].get_bool()) { + if (!include_watchonly.isNull() && include_watchonly.get_bool()) { filter = filter | ISMINE_WATCH_ONLY; } @@ -1094,13 +1107,13 @@ if (nAmount <= Amount::zero()) { throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount for send"); } - if (request.params.size() > 3) { + if (!request.params[3].isNull()) { // Unused parameter, used to be nMinDepth, keep type-checking it though. (void)request.params[3].get_int(); } std::string strComment; - if (request.params.size() > 4) { + if (!request.params[4].isNull()) { strComment = request.params[4].get_str(); } @@ -1195,18 +1208,16 @@ } int nMinDepth = 1; - if (request.params.size() > 3) { + if (!request.params[3].isNull()) { nMinDepth = request.params[3].get_int(); } mapValue_t mapValue; - if (request.params.size() > 4 && !request.params[4].isNull() && - !request.params[4].get_str().empty()) { + if (!request.params[4].isNull() && !request.params[4].get_str().empty()) { mapValue["comment"] = request.params[4].get_str(); } - if (request.params.size() > 5 && !request.params[5].isNull() && - !request.params[5].get_str().empty()) { + if (!request.params[5].isNull() && !request.params[5].get_str().empty()) { mapValue["to"] = request.params[5].get_str(); } @@ -1326,13 +1337,12 @@ } mapValue_t mapValue; - if (request.params.size() > 3 && !request.params[3].isNull() && - !request.params[3].get_str().empty()) { + if (!request.params[3].isNull() && !request.params[3].get_str().empty()) { mapValue["comment"] = request.params[3].get_str(); } UniValue subtractFeeFromAmount(UniValue::VARR); - if (request.params.size() > 4) { + if (!request.params[4].isNull()) { subtractFeeFromAmount = request.params[4].get_array(); } @@ -2143,12 +2153,12 @@ LOCK2(cs_main, pwallet->cs_wallet); int nMinDepth = 1; - if (request.params.size() > 0) { + if (!request.params[0].isNull()) { nMinDepth = request.params[0].get_int(); } isminefilter includeWatchonly = ISMINE_SPENDABLE; - if (request.params.size() > 1 && request.params[1].get_bool()) { + if (!request.params[1].isNull() && request.params[1].get_bool()) { includeWatchonly = includeWatchonly | ISMINE_WATCH_ONLY; } @@ -3046,21 +3056,19 @@ LOCK2(cs_main, pwallet->cs_wallet); - if (request.params.size() == 1) { - RPCTypeCheck(request.params, {UniValue::VBOOL}); - } else { - RPCTypeCheck(request.params, {UniValue::VBOOL, UniValue::VARR}); - } + RPCTypeCheckArgument(request.params[0], UniValue::VBOOL); bool fUnlock = request.params[0].get_bool(); - if (request.params.size() == 1) { + if (request.params[1].isNull()) { if (fUnlock) { pwallet->UnlockAllCoins(); } return true; } + RPCTypeCheckArgument(request.params[1], UniValue::VARR); + const UniValue &output_params = request.params[1]; // Create and validate the COutPoints first. @@ -3620,19 +3628,19 @@ } int nMinDepth = 1; - if (request.params.size() > 0 && !request.params[0].isNull()) { + if (!request.params[0].isNull()) { RPCTypeCheckArgument(request.params[0], UniValue::VNUM); nMinDepth = request.params[0].get_int(); } int nMaxDepth = 9999999; - if (request.params.size() > 1 && !request.params[1].isNull()) { + if (!request.params[1].isNull()) { RPCTypeCheckArgument(request.params[1], UniValue::VNUM); nMaxDepth = request.params[1].get_int(); } std::set destinations; - if (request.params.size() > 2 && !request.params[2].isNull()) { + if (!request.params[2].isNull()) { RPCTypeCheckArgument(request.params[2], UniValue::VARR); UniValue inputs = request.params[2].get_array(); for (size_t idx = 0; idx < inputs.size(); idx++) { @@ -3654,7 +3662,7 @@ } bool include_unsafe = true; - if (request.params.size() > 3 && !request.params[3].isNull()) { + if (!request.params[3].isNull()) { RPCTypeCheckArgument(request.params[3], UniValue::VBOOL); include_unsafe = request.params[3].get_bool(); } @@ -4081,7 +4089,7 @@ int num_generate = request.params[0].get_int(); uint64_t max_tries = 1000000; - if (request.params.size() > 1 && !request.params[1].isNull()) { + if (!request.params[1].isNull()) { max_tries = request.params[1].get_int(); }