Skip to content

Commit 041dad9

Browse files
committed
Merge #10783: [RPC] Various rpc argument fixes
4dc1915 check for null values in rpc args and handle appropriately (Gregory Sanders) 999ef20 importmulti options are optional (Gregory Sanders) a70d025 fixup some rpc param counting for rpc help (Gregory Sanders) Pull request description: Audited where named args will fail to use correct default values or may fail when additional optional arguments are added. Previously for these parameters, it was fine to omit them as positional arguments, but it would trigger UniValue runtime errors to set them to null, or to omit them while passing named parameters with greater positions (which would internally set earlier missing arguments to null). Now null values are treated the same as missing values so these errors do not occur. Included a few other small fixes while working on it. I didn't bother fixing account-based rpc calls. Tree-SHA512: 8baf781a35bd48de7878d4726850a580dab80323d3416c1c146b4fa9062f8a233c03f37e8ae3f3159e9d04a8f39c326627ca64c14e1cb7ce72538f934ab2ae1e
2 parents fd2814e + 4dc1915 commit 041dad9

File tree

6 files changed

+51
-51
lines changed

6 files changed

+51
-51
lines changed

src/rpc/blockchain.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ UniValue waitfornewblock(const JSONRPCRequest& request)
210210
+ HelpExampleRpc("waitfornewblock", "1000")
211211
);
212212
int timeout = 0;
213-
if (request.params.size() > 0)
213+
if (!request.params[0].isNull())
214214
timeout = request.params[0].get_int();
215215

216216
CUpdatedBlock block;
@@ -252,7 +252,7 @@ UniValue waitforblock(const JSONRPCRequest& request)
252252

253253
uint256 hash = uint256S(request.params[0].get_str());
254254

255-
if (request.params.size() > 1)
255+
if (!request.params[1].isNull())
256256
timeout = request.params[1].get_int();
257257

258258
CUpdatedBlock block;
@@ -295,7 +295,7 @@ UniValue waitforblockheight(const JSONRPCRequest& request)
295295

296296
int height = request.params[0].get_int();
297297

298-
if (request.params.size() > 1)
298+
if (!request.params[1].isNull())
299299
timeout = request.params[1].get_int();
300300

301301
CUpdatedBlock block;
@@ -434,7 +434,7 @@ UniValue getrawmempool(const JSONRPCRequest& request)
434434
);
435435

436436
bool fVerbose = false;
437-
if (request.params.size() > 0)
437+
if (!request.params[0].isNull())
438438
fVerbose = request.params[0].get_bool();
439439

440440
return mempoolToJSON(fVerbose);
@@ -467,7 +467,7 @@ UniValue getmempoolancestors(const JSONRPCRequest& request)
467467
}
468468

469469
bool fVerbose = false;
470-
if (request.params.size() > 1)
470+
if (!request.params[1].isNull())
471471
fVerbose = request.params[1].get_bool();
472472

473473
uint256 hash = ParseHashV(request.params[0], "parameter 1");
@@ -531,7 +531,7 @@ UniValue getmempooldescendants(const JSONRPCRequest& request)
531531
}
532532

533533
bool fVerbose = false;
534-
if (request.params.size() > 1)
534+
if (!request.params[1].isNull())
535535
fVerbose = request.params[1].get_bool();
536536

537537
uint256 hash = ParseHashV(request.params[0], "parameter 1");
@@ -666,7 +666,7 @@ UniValue getblockheader(const JSONRPCRequest& request)
666666
uint256 hash(uint256S(strHash));
667667

668668
bool fVerbose = true;
669-
if (request.params.size() > 1)
669+
if (!request.params[1].isNull())
670670
fVerbose = request.params[1].get_bool();
671671

672672
if (mapBlockIndex.count(hash) == 0)
@@ -741,7 +741,7 @@ UniValue getblock(const JSONRPCRequest& request)
741741
uint256 hash(uint256S(strHash));
742742

743743
int verbosity = 1;
744-
if (request.params.size() > 1) {
744+
if (!request.params[1].isNull()) {
745745
if(request.params[1].isNum())
746746
verbosity = request.params[1].get_int();
747747
else
@@ -984,7 +984,7 @@ UniValue gettxout(const JSONRPCRequest& request)
984984
int n = request.params[1].get_int();
985985
COutPoint out(hash, n);
986986
bool fMempool = true;
987-
if (request.params.size() > 2)
987+
if (!request.params[2].isNull())
988988
fMempool = request.params[2].get_bool();
989989

990990
Coin coin;
@@ -1037,9 +1037,9 @@ UniValue verifychain(const JSONRPCRequest& request)
10371037

10381038
LOCK(cs_main);
10391039

1040-
if (request.params.size() > 0)
1040+
if (!request.params[0].isNull())
10411041
nCheckLevel = request.params[0].get_int();
1042-
if (request.params.size() > 1)
1042+
if (!request.params[1].isNull())
10431043
nCheckDepth = request.params[1].get_int();
10441044

10451045
return CVerifyDB().VerifyDB(Params(), pcoinsTip, nCheckLevel, nCheckDepth);

src/rpc/mining.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ UniValue getnetworkhashps(const JSONRPCRequest& request)
102102
);
103103

104104
LOCK(cs_main);
105-
return GetNetworkHashPS(request.params.size() > 0 ? request.params[0].get_int() : 120, request.params.size() > 1 ? request.params[1].get_int() : -1);
105+
return GetNetworkHashPS(!request.params[0].isNull() ? request.params[0].get_int() : 120, !request.params[1].isNull() ? request.params[1].get_int() : -1);
106106
}
107107

108108
UniValue generateBlocks(std::shared_ptr<CReserveScript> coinbaseScript, int nGenerate, uint64_t nMaxTries, bool keepScript)
@@ -172,7 +172,7 @@ UniValue generatetoaddress(const JSONRPCRequest& request)
172172

173173
int nGenerate = request.params[0].get_int();
174174
uint64_t nMaxTries = 1000000;
175-
if (request.params.size() > 2) {
175+
if (!request.params[2].isNull()) {
176176
nMaxTries = request.params[2].get_int();
177177
}
178178

@@ -374,7 +374,7 @@ UniValue getblocktemplate(const JSONRPCRequest& request)
374374
UniValue lpval = NullUniValue;
375375
std::set<std::string> setClientRules;
376376
int64_t nMaxVersionPreVB = -1;
377-
if (request.params.size() > 0)
377+
if (!request.params[0].isNull())
378378
{
379379
const UniValue& oparam = request.params[0].get_obj();
380380
const UniValue& modeval = find_value(oparam, "mode");

src/rpc/net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ UniValue getaddednodeinfo(const JSONRPCRequest& request)
310310

311311
std::vector<AddedNodeInfo> vInfo = g_connman->GetAddedNodeInfo();
312312

313-
if (request.params.size() == 1) {
313+
if (request.params.size() == 1 && !request.params[0].isNull()) {
314314
bool found = false;
315315
for (const AddedNodeInfo& info : vInfo) {
316316
if (info.strAddedNode == request.params[0].get_str()) {

src/rpc/rawtransaction.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ UniValue getrawtransaction(const JSONRPCRequest& request)
137137

138138
// Accept either a bool (true) or a num (>=1) to indicate verbose output.
139139
bool fVerbose = false;
140-
if (request.params.size() > 1) {
140+
if (!request.params[1].isNull()) {
141141
if (request.params[1].isNum()) {
142142
if (request.params[1].get_int() != 0) {
143143
fVerbose = true;
@@ -211,7 +211,7 @@ UniValue gettxoutproof(const JSONRPCRequest& request)
211211
CBlockIndex* pblockindex = NULL;
212212

213213
uint256 hashBlock;
214-
if (request.params.size() > 1)
214+
if (!request.params[1].isNull())
215215
{
216216
hashBlock = uint256S(request.params[1].get_str());
217217
if (!mapBlockIndex.count(hashBlock))
@@ -412,7 +412,7 @@ UniValue createrawtransaction(const JSONRPCRequest& request)
412412
}
413413
}
414414

415-
if (request.params.size() > 3 && rbfOptIn != SignalsOptInRBF(rawTx)) {
415+
if (!request.params[3].isNull() && rbfOptIn != SignalsOptInRBF(rawTx)) {
416416
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter combination: Sequence number(s) contradict replaceable option");
417417
}
418418

src/wallet/rpcdump.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,12 @@ UniValue importprivkey(const JSONRPCRequest& request)
106106

107107
std::string strSecret = request.params[0].get_str();
108108
std::string strLabel = "";
109-
if (request.params.size() > 1)
109+
if (!request.params[1].isNull())
110110
strLabel = request.params[1].get_str();
111111

112112
// Whether to perform rescan after import
113113
bool fRescan = true;
114-
if (request.params.size() > 2)
114+
if (!request.params[2].isNull())
115115
fRescan = request.params[2].get_bool();
116116

117117
if (fRescan && fPruneMode)
@@ -245,20 +245,20 @@ UniValue importaddress(const JSONRPCRequest& request)
245245

246246

247247
std::string strLabel = "";
248-
if (request.params.size() > 1)
248+
if (!request.params[1].isNull())
249249
strLabel = request.params[1].get_str();
250250

251251
// Whether to perform rescan after import
252252
bool fRescan = true;
253-
if (request.params.size() > 2)
253+
if (!request.params[2].isNull())
254254
fRescan = request.params[2].get_bool();
255255

256256
if (fRescan && fPruneMode)
257257
throw JSONRPCError(RPC_WALLET_ERROR, "Rescan is disabled in pruned mode");
258258

259259
// Whether to import a p2sh version, too
260260
bool fP2SH = false;
261-
if (request.params.size() > 3)
261+
if (!request.params[3].isNull())
262262
fP2SH = request.params[3].get_bool();
263263

264264
LOCK2(cs_main, pwallet->cs_wallet);
@@ -410,12 +410,12 @@ UniValue importpubkey(const JSONRPCRequest& request)
410410

411411

412412
std::string strLabel = "";
413-
if (request.params.size() > 1)
413+
if (!request.params[1].isNull())
414414
strLabel = request.params[1].get_str();
415415

416416
// Whether to perform rescan after import
417417
bool fRescan = true;
418-
if (request.params.size() > 2)
418+
if (!request.params[2].isNull())
419419
fRescan = request.params[2].get_bool();
420420

421421
if (fRescan && fPruneMode)
@@ -1028,7 +1028,7 @@ UniValue importmulti(const JSONRPCRequest& mainRequest)
10281028
// clang-format off
10291029
if (mainRequest.fHelp || mainRequest.params.size() < 1 || mainRequest.params.size() > 2)
10301030
throw std::runtime_error(
1031-
"importmulti \"requests\" \"options\"\n\n"
1031+
"importmulti \"requests\" ( \"options\" )\n\n"
10321032
"Import addresses/scripts (with private or public keys, redeem script (P2SH)), rescanning all addresses in one-shot-only (rescan can be disabled via options).\n\n"
10331033
"Arguments:\n"
10341034
"1. requests (array, required) Data to be imported\n"
@@ -1071,7 +1071,7 @@ UniValue importmulti(const JSONRPCRequest& mainRequest)
10711071
//Default options
10721072
bool fRescan = true;
10731073

1074-
if (mainRequest.params.size() > 1) {
1074+
if (!mainRequest.params[1].isNull()) {
10751075
const UniValue& options = mainRequest.params[1];
10761076

10771077
if (options.exists("rescan")) {

0 commit comments

Comments
 (0)