Skip to content

Commit e067673

Browse files
committed
Avoid treating null RPC arguments different from missing arguments
This changes RPC methods to treat null arguments the same as missing arguments, instead of throwing type errors. Specifically: - `getbalance` method now returns the wallet balance when the `account` param is null instead of throwing a type error (same as when parameter is missing). It is still an error to supply `minconf` or `watchonly` options when the account is null. - `addnode` and `setban` methods now return help text instead of type errors if `command` params are null (same as when params are missing). - `sendrawtransaction`, `setaccount`, `movecmd`, `sendfrom`, `addmultisigaddress`, `listaccounts`, `lockunspent` methods accept null default values where missing values were previously allowed, and treat them the same.
1 parent e666efc commit e067673

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-16
lines changed

src/rpc/net.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ UniValue getpeerinfo(const JSONRPCRequest& request)
193193
UniValue addnode(const JSONRPCRequest& request)
194194
{
195195
std::string strCommand;
196-
if (request.params.size() == 2)
196+
if (!request.params[1].isNull())
197197
strCommand = request.params[1].get_str();
198198
if (request.fHelp || request.params.size() != 2 ||
199199
(strCommand != "onetry" && strCommand != "add" && strCommand != "remove"))
@@ -490,7 +490,7 @@ UniValue getnetworkinfo(const JSONRPCRequest& request)
490490
UniValue setban(const JSONRPCRequest& request)
491491
{
492492
std::string strCommand;
493-
if (request.params.size() >= 2)
493+
if (!request.params[1].isNull())
494494
strCommand = request.params[1].get_str();
495495
if (request.fHelp || request.params.size() < 2 ||
496496
(strCommand != "add" && strCommand != "remove"))

src/rpc/rawtransaction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ UniValue sendrawtransaction(const JSONRPCRequest& request)
922922
const uint256& hashTx = tx->GetHash();
923923

924924
CAmount nMaxRawTxFee = maxTxFee;
925-
if (request.params.size() > 1 && request.params[1].get_bool())
925+
if (!request.params[1].isNull() && request.params[1].get_bool())
926926
nMaxRawTxFee = 0;
927927

928928
CCoinsViewCache &view = *pcoinsTip;

src/wallet/rpcwallet.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ UniValue setaccount(const JSONRPCRequest& request)
280280
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
281281

282282
std::string strAccount;
283-
if (request.params.size() > 1)
283+
if (!request.params[1].isNull())
284284
strAccount = AccountFromValue(request.params[1]);
285285

286286
// Only add the account if the address is yours.
@@ -768,7 +768,7 @@ UniValue getbalance(const JSONRPCRequest& request)
768768

769769
LOCK2(cs_main, pwallet->cs_wallet);
770770

771-
if (request.params.size() == 0)
771+
if (request.params[0].isNull() && request.params[1].isNull() && request.params[2].isNull())
772772
return ValueFromAmount(pwallet->GetBalance());
773773

774774
const std::string& account_param = request.params[0].get_str();
@@ -838,11 +838,11 @@ UniValue movecmd(const JSONRPCRequest& request)
838838
CAmount nAmount = AmountFromValue(request.params[2]);
839839
if (nAmount <= 0)
840840
throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount for send");
841-
if (request.params.size() > 3)
841+
if (!request.params[3].isNull())
842842
// unused parameter, used to be nMinDepth, keep type-checking it though
843843
(void)request.params[3].get_int();
844844
std::string strComment;
845-
if (request.params.size() > 4)
845+
if (!request.params[4].isNull())
846846
strComment = request.params[4].get_str();
847847

848848
if (!pwallet->AccountMove(strFrom, strTo, nAmount, strComment)) {
@@ -899,7 +899,7 @@ UniValue sendfrom(const JSONRPCRequest& request)
899899
if (nAmount <= 0)
900900
throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount for send");
901901
int nMinDepth = 1;
902-
if (request.params.size() > 3)
902+
if (!request.params[3].isNull())
903903
nMinDepth = request.params[3].get_int();
904904

905905
CWalletTx wtx;
@@ -1105,7 +1105,7 @@ UniValue addmultisigaddress(const JSONRPCRequest& request)
11051105
LOCK2(cs_main, pwallet->cs_wallet);
11061106

11071107
std::string strAccount;
1108-
if (request.params.size() > 2)
1108+
if (!request.params[2].isNull())
11091109
strAccount = AccountFromValue(request.params[2]);
11101110

11111111
// Construct using pay-to-script-hash:
@@ -1711,10 +1711,10 @@ UniValue listaccounts(const JSONRPCRequest& request)
17111711
LOCK2(cs_main, pwallet->cs_wallet);
17121712

17131713
int nMinDepth = 1;
1714-
if (request.params.size() > 0)
1714+
if (!request.params[0].isNull())
17151715
nMinDepth = request.params[0].get_int();
17161716
isminefilter includeWatchonly = ISMINE_SPENDABLE;
1717-
if(request.params.size() > 1)
1717+
if(!request.params[1].isNull())
17181718
if(request.params[1].get_bool())
17191719
includeWatchonly = includeWatchonly | ISMINE_WATCH_ONLY;
17201720

@@ -2361,19 +2361,18 @@ UniValue lockunspent(const JSONRPCRequest& request)
23612361

23622362
LOCK2(cs_main, pwallet->cs_wallet);
23632363

2364-
if (request.params.size() == 1)
2365-
RPCTypeCheck(request.params, {UniValue::VBOOL});
2366-
else
2367-
RPCTypeCheck(request.params, {UniValue::VBOOL, UniValue::VARR});
2364+
RPCTypeCheckArgument(request.params[0], UniValue::VBOOL);
23682365

23692366
bool fUnlock = request.params[0].get_bool();
23702367

2371-
if (request.params.size() == 1) {
2368+
if (request.params[1].isNull()) {
23722369
if (fUnlock)
23732370
pwallet->UnlockAllCoins();
23742371
return true;
23752372
}
23762373

2374+
RPCTypeCheckArgument(request.params[1], UniValue::VARR);
2375+
23772376
UniValue outputs = request.params[1].get_array();
23782377
for (unsigned int idx = 0; idx < outputs.size(); idx++) {
23792378
const UniValue& output = outputs[idx];

0 commit comments

Comments
 (0)