Skip to content

Commit a587c2b

Browse files
MarcoFalkevijaydasmp
authored andcommitted
Merge bitcoin#21679: rpc: Keep default argument value in correct type
bee56c7 rpc: Check default value type againts argument type (João Barbosa) f81ef43 rpc: Keep default argument value in correct type (João Barbosa) Pull request description: Store default values of RPC arguments in the corresponding type instead of a string. The value is then serialized when the help output is needed. This change simplifies bitcoin#20017. The following examples illustrates how to use the new `RPCArg::Default` and `RPCArg::DefaultHint`: ```diff - {"verbose", RPCArg::Type::BOOL, /* default */ "false", "True for a json object, false for array of transaction ids"} + {"verbose", RPCArg::Type::BOOL, RPCArg::Default(false), "True for a json object, false for array of transaction ids"} ``` ```diff - {"nblocks", RPCArg::Type::NUM, /* default */ "one month", "Size of the window in number of blocks"} + {"nblocks", RPCArg::Type::NUM, RPCArg::DefaultHint("one month"), "Size of the window in number of blocks"} ``` No behavior change is expected. ACKs for top commit: LarryRuane: ACK bee56c7 MarcoFalke: ACK bee56c7 🦅 Tree-SHA512: c47d78c918e996d36631d4ad3c933b270a34c5b446b8d736be94cf4a0a7b8c0e33d954149ec786cf9550639865b79deb6a130ad044de6030f95aac33f524293a
1 parent 285e2a5 commit a587c2b

File tree

10 files changed

+232
-200
lines changed

10 files changed

+232
-200
lines changed

src/rpc/blockchain.cpp

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ static RPCHelpMan waitfornewblock()
288288
"\nWaits for a specific new block and returns useful info about it.\n"
289289
"\nReturns the current block on timeout or exit.\n",
290290
{
291-
{"timeout", RPCArg::Type::NUM, /* default */ "0", "Time in milliseconds to wait for a response. 0 indicates no timeout."},
291+
{"timeout", RPCArg::Type::NUM, RPCArg::Default{0}, "Time in milliseconds to wait for a response. 0 indicates no timeout."},
292292
},
293293
RPCResult{
294294
RPCResult::Type::OBJ, "", "",
@@ -331,7 +331,7 @@ static RPCHelpMan waitforblock()
331331
"\nReturns the current block on timeout or exit.\n",
332332
{
333333
{"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "Block hash to wait for."},
334-
{"timeout", RPCArg::Type::NUM, /* default */ "0", "Time in milliseconds to wait for a response. 0 indicates no timeout."},
334+
{"timeout", RPCArg::Type::NUM, RPCArg::Default{0}, "Time in milliseconds to wait for a response. 0 indicates no timeout."},
335335
},
336336
RPCResult{
337337
RPCResult::Type::OBJ, "", "",
@@ -378,7 +378,7 @@ static RPCHelpMan waitforblockheight()
378378
"\nReturns the current block on timeout or exit.\n",
379379
{
380380
{"height", RPCArg::Type::NUM, RPCArg::Optional::NO, "Block height to wait for."},
381-
{"timeout", RPCArg::Type::NUM, /* default */ "0", "Time in milliseconds to wait for a response. 0 indicates no timeout."},
381+
{"timeout", RPCArg::Type::NUM, RPCArg::Default{0}, "Time in milliseconds to wait for a response. 0 indicates no timeout."},
382382
},
383383
RPCResult{
384384
RPCResult::Type::OBJ, "", "",
@@ -571,6 +571,7 @@ UniValue MempoolToJSON(const CTxMemPool& pool, llmq::CInstantSendManager* isman,
571571
return o;
572572
}
573573
}
574+
574575
}
575576

576577
static RPCHelpMan getrawmempool()
@@ -579,8 +580,8 @@ static RPCHelpMan getrawmempool()
579580
"\nReturns all transaction ids in memory pool as a json array of string transaction ids.\n"
580581
"\nHint: use getmempoolentry to fetch a specific transaction from the mempool.\n",
581582
{
582-
{"verbose", RPCArg::Type::BOOL, /* default */ "false", "True for a json object, false for array of transaction ids"},
583-
{"mempool_sequence", RPCArg::Type::BOOL, /* default */ "false", "If verbose=false, returns a json object with transaction list and mempool sequence number attached."},
583+
{"verbose", RPCArg::Type::BOOL, RPCArg::Default{false}, "True for a json object, false for array of transaction ids"},
584+
{"mempool_sequence", RPCArg::Type::BOOL, RPCArg::Default{false}, "If verbose=false, returns a json object with transaction list and mempool sequence number attached."},
584585
},
585586
{
586587
RPCResult{"for verbose = false",
@@ -632,7 +633,7 @@ static RPCHelpMan getmempoolancestors()
632633
"\nIf txid is in the mempool, returns all in-mempool ancestors.\n",
633634
{
634635
{"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id (must be in mempool)"},
635-
{"verbose", RPCArg::Type::BOOL, /* default */ "false", "True for a json object, false for array of transaction ids"},
636+
{"verbose", RPCArg::Type::BOOL, RPCArg::Default{false}, "True for a json object, false for array of transaction ids"},
636637
},
637638
{
638639
RPCResult{"for verbose = false",
@@ -699,7 +700,7 @@ static RPCHelpMan getmempooldescendants()
699700
"\nIf txid is in the mempool, returns all in-mempool descendants.\n",
700701
{
701702
{"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id (must be in mempool)"},
702-
{"verbose", RPCArg::Type::BOOL, /* default */ "false", "True for a json object, false for array of transaction ids"},
703+
{"verbose", RPCArg::Type::BOOL, RPCArg::Default{false}, "True for a json object, false for array of transaction ids"},
703704
},
704705
{
705706
RPCResult{"for verbose = false",
@@ -922,7 +923,7 @@ static RPCHelpMan getblockheader()
922923
"If verbose is true, returns an Object with information about blockheader <hash>.\n",
923924
{
924925
{"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The block hash"},
925-
{"verbose", RPCArg::Type::BOOL, /* default */ "true", "true for a json object, false for the hex-encoded data"},
926+
{"verbose", RPCArg::Type::BOOL, RPCArg::Default{true}, "true for a json object, false for the hex-encoded data"},
926927
},
927928
{
928929
RPCResult{"for verbose = true",
@@ -996,8 +997,8 @@ static RPCHelpMan getblockheaders()
996997
"If verbose is true, each item is an Object with information about a single blockheader.\n",
997998
{
998999
{"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The block hash"},
999-
{"count", RPCArg::Type::NUM, /* default */ strprintf("%s", MAX_HEADERS_RESULTS), ""},
1000-
{"verbose", RPCArg::Type::BOOL, /* default */ "true", "true for a json object, false for the hex-encoded data"},
1000+
{"count", RPCArg::Type::NUM, RPCArg::DefaultHint{strprintf("%s", MAX_HEADERS_RESULTS)}, ""},
1001+
{"verbose", RPCArg::Type::BOOL, RPCArg::Default{true}, "true for a json object, false for the hex-encoded data"},
10011002
},
10021003
{
10031004
RPCResult{"for verbose = true",
@@ -1134,7 +1135,7 @@ static RPCHelpMan getmerkleblocks()
11341135
{
11351136
{"filter", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The hex-encoded bloom filter"},
11361137
{"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The block hash"},
1137-
{"count", RPCArg::Type::NUM, /* default */ strprintf("%s", MAX_HEADERS_RESULTS), ""},
1138+
{"count", RPCArg::Type::NUM, RPCArg::DefaultHint{strprintf("%s", MAX_HEADERS_RESULTS), ""},
11381139
},
11391140
RPCResult{
11401141
RPCResult::Type::ARR, "", "",
@@ -1210,7 +1211,7 @@ static RPCHelpMan getblock()
12101211
"If verbosity is 2, returns an Object with information about block <hash> and information about each transaction. \n",
12111212
{
12121213
{"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The block hash"},
1213-
{"verbosity|verbose", RPCArg::Type::NUM, /* default */ "1", "0 for hex-encoded data, 1 for a json object, and 2 for json object with transaction data"},
1214+
{"verbosity|verbose", RPCArg::Type::NUM, RPCArg::Default{1}, "0 for hex-encoded data, 1 for a json object, and 2 for json object with transaction data"},
12141215
},
12151216
{
12161217
RPCResult{"for verbosity = 0",
@@ -1387,9 +1388,9 @@ static RPCHelpMan gettxoutsetinfo()
13871388
"\nReturns statistics about the unspent transaction output set.\n"
13881389
"Note this call may take some time if you are not using coinstatsindex.\n",
13891390
{
1390-
{"hash_type", RPCArg::Type::STR, /* default */ "hash_serialized_2", "Which UTXO set hash should be calculated. Options: 'hash_serialized_2' (the legacy algorithm), 'muhash', 'none'."},
1391+
{"hash_type", RPCArg::Type::STR, RPCArg::Default{"hash_serialized_2"}, "Which UTXO set hash should be calculated. Options: 'hash_serialized_2' (the legacy algorithm), 'muhash', 'none'."},
13911392
{"hash_or_height", RPCArg::Type::NUM, RPCArg::Optional::OMITTED, "The block hash or height of the target height (only available with coinstatsindex).", "", {"", "string or numeric"}},
1392-
{"use_index", RPCArg::Type::BOOL, /* default */ "true", "Use coinstatsindex, if available."},
1393+
{"use_index", RPCArg::Type::BOOL, RPCArg::Default{true}, "Use coinstatsindex, if available."},
13931394
},
13941395
RPCResult{
13951396
RPCResult::Type::OBJ, "", "",
@@ -1419,7 +1420,7 @@ static RPCHelpMan gettxoutsetinfo()
14191420
}}
14201421
}},
14211422
}},
1422-
RPCExamples{
1423+
RPCExamples{
14231424
HelpExampleCli("gettxoutsetinfo", "") +
14241425
HelpExampleCli("gettxoutsetinfo", R"("none")") +
14251426
HelpExampleCli("gettxoutsetinfo", R"("none" 1000)") +
@@ -1531,7 +1532,7 @@ static RPCHelpMan gettxout()
15311532
{
15321533
{"txid", RPCArg::Type::STR, RPCArg::Optional::NO, "The transaction id"},
15331534
{"n", RPCArg::Type::NUM, RPCArg::Optional::NO, "vout number"},
1534-
{"include_mempool", RPCArg::Type::BOOL, /* default */ "true", "Whether to include the mempool. Note that an unspent output that is spent in the mempool won't appear."},
1535+
{"include_mempool", RPCArg::Type::BOOL, RPCArg::Default{true}, "Whether to include the mempool. Note that an unspent output that is spent in the mempool won't appear."},
15351536
},
15361537
{
15371538
RPCResult{"If the UTXO was not found", RPCResult::Type::NONE, "", ""},
@@ -1618,9 +1619,9 @@ static RPCHelpMan verifychain()
16181619
return RPCHelpMan{"verifychain",
16191620
"\nVerifies blockchain database.\n",
16201621
{
1621-
{"checklevel", RPCArg::Type::NUM, /* default */ strprintf("%d, range=0-4", DEFAULT_CHECKLEVEL),
1622+
{"checklevel", RPCArg::Type::NUM, RPCArg::DefaultHint{strprintf("%d, range=0-4", DEFAULT_CHECKLEVEL),
16221623
strprintf("How thorough the block verification is:\n - %s", Join(CHECKLEVEL_DOC, "\n- "))},
1623-
{"nblocks", RPCArg::Type::NUM, /* default */ strprintf("%d, 0=all", DEFAULT_CHECKBLOCKS), "The number of blocks to check."},
1624+
{"nblocks", RPCArg::Type::NUM, RPCArg::DefaultHint{strprintf("%d, 0=all", DEFAULT_CHECKBLOCKS), "The number of blocks to check."},
16241625
},
16251626
RPCResult{
16261627
RPCResult::Type::BOOL, "", "Verified or not"},
@@ -2170,8 +2171,8 @@ static RPCHelpMan getchaintxstats()
21702171
return RPCHelpMan{"getchaintxstats",
21712172
"\nCompute statistics about the total number and rate of transactions in the chain.\n",
21722173
{
2173-
{"nblocks", RPCArg::Type::NUM, /* default */ "one month", "Size of the window in number of blocks"},
2174-
{"blockhash", RPCArg::Type::STR_HEX, /* default */ "chain tip", "The hash of the block that ends the window."},
2174+
{"nblocks", RPCArg::Type::NUM, RPCArg::DefaultHint{"one month"}, "Size of the window in number of blocks"},
2175+
{"blockhash", RPCArg::Type::STR_HEX, RPCArg::DefaultHint{"chain tip"}, "The hash of the block that ends the window."},
21752176
},
21762177
RPCResult{
21772178
RPCResult::Type::OBJ, "", "",
@@ -2320,7 +2321,7 @@ static RPCHelpMan getblockstats()
23202321
"It won't work for some heights with pruning.\n",
23212322
{
23222323
{"hash_or_height", RPCArg::Type::NUM, RPCArg::Optional::NO, "The block hash or height of the target block", "", {"", "string or numeric"}},
2323-
{"stats", RPCArg::Type::ARR, /* default */ "all values", "Values to plot (see result below)",
2324+
{"stats", RPCArg::Type::ARR, RPCArg::DefaultHint{"all values"}, "Values to plot (see result below)",
23242325
{
23252326
{"height", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "Selected statistic"},
23262327
{"time", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "Selected statistic"},
@@ -2540,8 +2541,8 @@ static RPCHelpMan getspecialtxes()
25402541
{"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The block hash"},
25412542
{"type", RPCArg::Type::NUM, /* default */ "-1", "Filter special txes by type, -1 means all types"},
25422543
{"count", RPCArg::Type::NUM, /* default */ "10", "The number of transactions to return"},
2543-
{"skip", RPCArg::Type::NUM, /* default */ "0", "The number of transactions to skip"},
2544-
{"verbosity", RPCArg::Type::NUM, /* default */ "0", "0 for hashes, 1 for hex-encoded data, and 2 for json object"},
2544+
{"skip", RPCArg::Type::NUM, RPCArg::Default{0}, "The number of transactions to skip"},
2545+
{"verbosity", RPCArg::Type::NUM, RPCArg::Default{0}, "0 for hashes, 1 for hex-encoded data, and 2 for json object"},
25452546
},
25462547
{
25472548
RPCResult{"for verbosity = 0",
@@ -2755,7 +2756,7 @@ static RPCHelpMan scantxoutset()
27552756
{"", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED, "An object with output descriptor and metadata",
27562757
{
27572758
{"desc", RPCArg::Type::STR, RPCArg::Optional::NO, "An output descriptor"},
2758-
{"range", RPCArg::Type::RANGE, /* default */ "1000", "The range of HD chain indexes to explore (either end or [begin,end])"},
2759+
{"range", RPCArg::Type::RANGE, RPCArg::Default{1000}, "The range of HD chain indexes to explore (either end or [begin,end])"},
27592760
},
27602761
},
27612762
},
@@ -2901,7 +2902,7 @@ static RPCHelpMan getblockfilter()
29012902
"\nRetrieve a BIP 157 content filter for a particular block.\n",
29022903
{
29032904
{"blockhash", RPCArg::Type::STR, RPCArg::Optional::NO, "The hash of the block"},
2904-
{"filtertype", RPCArg::Type::STR, /* default */ "basic", "The type name of the filter"},
2905+
{"filtertype", RPCArg::Type::STR, RPCArg::Default{"basic"}, "The type name of the filter"},
29052906
},
29062907
RPCResult{
29072908
RPCResult::Type::OBJ, "", "",

src/rpc/mining.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ static RPCHelpMan getnetworkhashps()
102102
"Pass in [blocks] to override # of blocks, -1 specifies since last difficulty change.\n"
103103
"Pass in [height] to estimate the network speed at the time when a certain block was found.\n",
104104
{
105-
{"nblocks", RPCArg::Type::NUM, /* default */ "120", "The number of blocks, or -1 for blocks since last difficulty change."},
106-
{"height", RPCArg::Type::NUM, /* default */ "-1", "To estimate at the time of the given height."},
105+
{"nblocks", RPCArg::Type::NUM, RPCArg::Default{120}, "The number of blocks, or -1 for blocks since last difficulty change."},
106+
{"height", RPCArg::Type::NUM, RPCArg::Default{-1}, "To estimate at the time of the given height."},
107107
},
108108
RPCResult{
109109
RPCResult::Type::NUM, "", "Hashes per second estimated"},
@@ -230,7 +230,7 @@ static RPCHelpMan generatetodescriptor()
230230
{
231231
{"num_blocks", RPCArg::Type::NUM, RPCArg::Optional::NO, "How many blocks are generated immediately."},
232232
{"descriptor", RPCArg::Type::STR, RPCArg::Optional::NO, "The descriptor to send the newly generated coins to."},
233-
{"maxtries", RPCArg::Type::NUM, /* default */ ToString(DEFAULT_MAX_TRIES), "How many iterations to try."},
233+
{"maxtries", RPCArg::Type::NUM, RPCArg::Default{DEFAULT_MAX_TRIES}, "How many iterations to try."},
234234
},
235235
RPCResult{
236236
RPCResult::Type::ARR, "", "",
@@ -267,7 +267,7 @@ static RPCHelpMan generatetoaddress()
267267
{
268268
{"nblocks", RPCArg::Type::NUM, RPCArg::Optional::NO, "How many blocks are generated immediately."},
269269
{"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The address to send the newly generated coins to."},
270-
{"maxtries", RPCArg::Type::NUM, /* default */ ToString(DEFAULT_MAX_TRIES), "How many iterations to try."},
270+
{"maxtries", RPCArg::Type::NUM, RPCArg::Default{DEFAULT_MAX_TRIES}, "How many iterations to try."},
271271
},
272272
RPCResult{
273273
RPCResult::Type::ARR, "", "hashes of blocks generated",
@@ -563,7 +563,7 @@ static RPCHelpMan getblocktemplate()
563563
" https://github.com/bitcoin/bips/blob/master/bip-0023.mediawiki\n"
564564
" https://github.com/bitcoin/bips/blob/master/bip-0009.mediawiki#getblocktemplate_changes\n",
565565
{
566-
{"template_request", RPCArg::Type::OBJ, /* default_val */ "", "Format of the template",
566+
{"template_request", RPCArg::Type::OBJ, RPCArg::Default{UniValue::VOBJ}, "Format of the template",
567567
{
568568
{"mode", RPCArg::Type::STR, /* treat as named arg */ RPCArg::Optional::OMITTED_NAMED_ARG, "This must be set to \"template\", \"proposal\" (see BIP 23), or omitted"},
569569
{"capabilities", RPCArg::Type::ARR, /* treat as named arg */ RPCArg::Optional::OMITTED_NAMED_ARG, "A list of strings",
@@ -1020,7 +1020,7 @@ static RPCHelpMan submitblock()
10201020
"See https://en.bitcoin.it/wiki/BIP_0022 for full specification.\n",
10211021
{
10221022
{"hexdata", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "the hex-encoded block data to submit"},
1023-
{"dummy", RPCArg::Type::STR, /* default */ "ignored", "dummy value, for compatibility with BIP22. This value is ignored."},
1023+
{"dummy", RPCArg::Type::STR, RPCArg::DefaultHint{"ignored"}, "dummy value, for compatibility with BIP22. This value is ignored."},
10241024
},
10251025
{
10261026
RPCResult{"If the block was accepted", RPCResult::Type::NONE, "", ""},
@@ -1120,7 +1120,7 @@ static RPCHelpMan estimatesmartfee()
11201120
"for which the estimate is valid.\n",
11211121
{
11221122
{"conf_target", RPCArg::Type::NUM, RPCArg::Optional::NO, "Confirmation target in blocks (1 - 1008)"},
1123-
{"estimate_mode", RPCArg::Type::STR, /* default */ "conservative", "The fee estimate mode.\n"
1123+
{"estimate_mode", RPCArg::Type::STR, RPCArg::Default{"conservative"}, "The fee estimate mode.\n"
11241124
" Whether to return a more conservative estimate which also satisfies\n"
11251125
" a longer history. A conservative estimate potentially returns a\n"
11261126
" higher feerate and is more likely to be sufficient for the desired\n"
@@ -1195,7 +1195,7 @@ static RPCHelpMan estimaterawfee()
11951195
"confirmation within conf_target blocks if possible.\n",
11961196
{
11971197
{"conf_target", RPCArg::Type::NUM, RPCArg::Optional::NO, "Confirmation target in blocks (1 - 1008)"},
1198-
{"threshold", RPCArg::Type::NUM, /* default */ "0.95", "The proportion of transactions in a given feerate range that must have been\n"
1198+
{"threshold", RPCArg::Type::NUM, RPCArg::Default{0.95}, "The proportion of transactions in a given feerate range that must have been\n"
11991199
" confirmed within conf_target in order to consider those feerates as high enough and proceed to check\n"
12001200
" lower buckets."},
12011201
},

0 commit comments

Comments
 (0)