Skip to content

Commit d4300a1

Browse files
author
MarcoFalke
committed
Merge #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 #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
2 parents 17b51cd + bee56c7 commit d4300a1

File tree

10 files changed

+214
-183
lines changed

10 files changed

+214
-183
lines changed

src/rpc/blockchain.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ static RPCHelpMan waitfornewblock()
254254
"\nWaits for a specific new block and returns useful info about it.\n"
255255
"\nReturns the current block on timeout or exit.\n",
256256
{
257-
{"timeout", RPCArg::Type::NUM, /* default */ "0", "Time in milliseconds to wait for a response. 0 indicates no timeout."},
257+
{"timeout", RPCArg::Type::NUM, RPCArg::Default{0}, "Time in milliseconds to wait for a response. 0 indicates no timeout."},
258258
},
259259
RPCResult{
260260
RPCResult::Type::OBJ, "", "",
@@ -297,7 +297,7 @@ static RPCHelpMan waitforblock()
297297
"\nReturns the current block on timeout or exit.\n",
298298
{
299299
{"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "Block hash to wait for."},
300-
{"timeout", RPCArg::Type::NUM, /* default */ "0", "Time in milliseconds to wait for a response. 0 indicates no timeout."},
300+
{"timeout", RPCArg::Type::NUM, RPCArg::Default{0}, "Time in milliseconds to wait for a response. 0 indicates no timeout."},
301301
},
302302
RPCResult{
303303
RPCResult::Type::OBJ, "", "",
@@ -344,7 +344,7 @@ static RPCHelpMan waitforblockheight()
344344
"\nReturns the current block on timeout or exit.\n",
345345
{
346346
{"height", RPCArg::Type::NUM, RPCArg::Optional::NO, "Block height to wait for."},
347-
{"timeout", RPCArg::Type::NUM, /* default */ "0", "Time in milliseconds to wait for a response. 0 indicates no timeout."},
347+
{"timeout", RPCArg::Type::NUM, RPCArg::Default{0}, "Time in milliseconds to wait for a response. 0 indicates no timeout."},
348348
},
349349
RPCResult{
350350
RPCResult::Type::OBJ, "", "",
@@ -558,8 +558,8 @@ static RPCHelpMan getrawmempool()
558558
"\nReturns all transaction ids in memory pool as a json array of string transaction ids.\n"
559559
"\nHint: use getmempoolentry to fetch a specific transaction from the mempool.\n",
560560
{
561-
{"verbose", RPCArg::Type::BOOL, /* default */ "false", "True for a json object, false for array of transaction ids"},
562-
{"mempool_sequence", RPCArg::Type::BOOL, /* default */ "false", "If verbose=false, returns a json object with transaction list and mempool sequence number attached."},
561+
{"verbose", RPCArg::Type::BOOL, RPCArg::Default{false}, "True for a json object, false for array of transaction ids"},
562+
{"mempool_sequence", RPCArg::Type::BOOL, RPCArg::Default{false}, "If verbose=false, returns a json object with transaction list and mempool sequence number attached."},
563563
},
564564
{
565565
RPCResult{"for verbose = false",
@@ -608,7 +608,7 @@ static RPCHelpMan getmempoolancestors()
608608
"\nIf txid is in the mempool, returns all in-mempool ancestors.\n",
609609
{
610610
{"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id (must be in mempool)"},
611-
{"verbose", RPCArg::Type::BOOL, /* default */ "false", "True for a json object, false for array of transaction ids"},
611+
{"verbose", RPCArg::Type::BOOL, RPCArg::Default{false}, "True for a json object, false for array of transaction ids"},
612612
},
613613
{
614614
RPCResult{"for verbose = false",
@@ -672,7 +672,7 @@ static RPCHelpMan getmempooldescendants()
672672
"\nIf txid is in the mempool, returns all in-mempool descendants.\n",
673673
{
674674
{"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id (must be in mempool)"},
675-
{"verbose", RPCArg::Type::BOOL, /* default */ "false", "True for a json object, false for array of transaction ids"},
675+
{"verbose", RPCArg::Type::BOOL, RPCArg::Default{false}, "True for a json object, false for array of transaction ids"},
676676
},
677677
{
678678
RPCResult{"for verbose = false",
@@ -800,7 +800,7 @@ static RPCHelpMan getblockheader()
800800
"If verbose is true, returns an Object with information about blockheader <hash>.\n",
801801
{
802802
{"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The block hash"},
803-
{"verbose", RPCArg::Type::BOOL, /* default */ "true", "true for a json object, false for the hex-encoded data"},
803+
{"verbose", RPCArg::Type::BOOL, RPCArg::Default{true}, "true for a json object, false for the hex-encoded data"},
804804
},
805805
{
806806
RPCResult{"for verbose = true",
@@ -902,7 +902,7 @@ static RPCHelpMan getblock()
902902
"If verbosity is 2, returns an Object with information about block <hash> and information about each transaction. \n",
903903
{
904904
{"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The block hash"},
905-
{"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"},
905+
{"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"},
906906
},
907907
{
908908
RPCResult{"for verbosity = 0",
@@ -1070,7 +1070,7 @@ static RPCHelpMan gettxoutsetinfo()
10701070
"\nReturns statistics about the unspent transaction output set.\n"
10711071
"Note this call may take some time.\n",
10721072
{
1073-
{"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'."},
1073+
{"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'."},
10741074
},
10751075
RPCResult{
10761076
RPCResult::Type::OBJ, "", "",
@@ -1137,7 +1137,7 @@ static RPCHelpMan gettxout()
11371137
{
11381138
{"txid", RPCArg::Type::STR, RPCArg::Optional::NO, "The transaction id"},
11391139
{"n", RPCArg::Type::NUM, RPCArg::Optional::NO, "vout number"},
1140-
{"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."},
1140+
{"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."},
11411141
},
11421142
{
11431143
RPCResult{"If the UTXO was not found", RPCResult::Type::NONE, "", ""},
@@ -1220,9 +1220,9 @@ static RPCHelpMan verifychain()
12201220
return RPCHelpMan{"verifychain",
12211221
"\nVerifies blockchain database.\n",
12221222
{
1223-
{"checklevel", RPCArg::Type::NUM, /* default */ strprintf("%d, range=0-4", DEFAULT_CHECKLEVEL),
1223+
{"checklevel", RPCArg::Type::NUM, RPCArg::DefaultHint{strprintf("%d, range=0-4", DEFAULT_CHECKLEVEL)},
12241224
strprintf("How thorough the block verification is:\n - %s", Join(CHECKLEVEL_DOC, "\n- "))},
1225-
{"nblocks", RPCArg::Type::NUM, /* default */ strprintf("%d, 0=all", DEFAULT_CHECKBLOCKS), "The number of blocks to check."},
1225+
{"nblocks", RPCArg::Type::NUM, RPCArg::DefaultHint{strprintf("%d, 0=all", DEFAULT_CHECKBLOCKS)}, "The number of blocks to check."},
12261226
},
12271227
RPCResult{
12281228
RPCResult::Type::BOOL, "", "Verified or not"},
@@ -1704,8 +1704,8 @@ static RPCHelpMan getchaintxstats()
17041704
return RPCHelpMan{"getchaintxstats",
17051705
"\nCompute statistics about the total number and rate of transactions in the chain.\n",
17061706
{
1707-
{"nblocks", RPCArg::Type::NUM, /* default */ "one month", "Size of the window in number of blocks"},
1708-
{"blockhash", RPCArg::Type::STR_HEX, /* default */ "chain tip", "The hash of the block that ends the window."},
1707+
{"nblocks", RPCArg::Type::NUM, RPCArg::DefaultHint{"one month"}, "Size of the window in number of blocks"},
1708+
{"blockhash", RPCArg::Type::STR_HEX, RPCArg::DefaultHint{"chain tip"}, "The hash of the block that ends the window."},
17091709
},
17101710
RPCResult{
17111711
RPCResult::Type::OBJ, "", "",
@@ -1852,7 +1852,7 @@ static RPCHelpMan getblockstats()
18521852
"It won't work for some heights with pruning.\n",
18531853
{
18541854
{"hash_or_height", RPCArg::Type::NUM, RPCArg::Optional::NO, "The block hash or height of the target block", "", {"", "string or numeric"}},
1855-
{"stats", RPCArg::Type::ARR, /* default */ "all values", "Values to plot (see result below)",
1855+
{"stats", RPCArg::Type::ARR, RPCArg::DefaultHint{"all values"}, "Values to plot (see result below)",
18561856
{
18571857
{"height", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "Selected statistic"},
18581858
{"time", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "Selected statistic"},
@@ -2221,7 +2221,7 @@ static RPCHelpMan scantxoutset()
22212221
{"", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED, "An object with output descriptor and metadata",
22222222
{
22232223
{"desc", RPCArg::Type::STR, RPCArg::Optional::NO, "An output descriptor"},
2224-
{"range", RPCArg::Type::RANGE, /* default */ "1000", "The range of HD chain indexes to explore (either end or [begin,end])"},
2224+
{"range", RPCArg::Type::RANGE, RPCArg::Default{1000}, "The range of HD chain indexes to explore (either end or [begin,end])"},
22252225
}},
22262226
},
22272227
"[scanobjects,...]"},
@@ -2360,7 +2360,7 @@ static RPCHelpMan getblockfilter()
23602360
"\nRetrieve a BIP 157 content filter for a particular block.\n",
23612361
{
23622362
{"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The hash of the block"},
2363-
{"filtertype", RPCArg::Type::STR, /*default*/ "basic", "The type name of the filter"},
2363+
{"filtertype", RPCArg::Type::STR, RPCArg::Default{"basic"}, "The type name of the filter"},
23642364
},
23652365
RPCResult{
23662366
RPCResult::Type::OBJ, "", "",

src/rpc/mining.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ static RPCHelpMan getnetworkhashps()
8989
"Pass in [blocks] to override # of blocks, -1 specifies since last difficulty change.\n"
9090
"Pass in [height] to estimate the network speed at the time when a certain block was found.\n",
9191
{
92-
{"nblocks", RPCArg::Type::NUM, /* default */ "120", "The number of blocks, or -1 for blocks since last difficulty change."},
93-
{"height", RPCArg::Type::NUM, /* default */ "-1", "To estimate at the time of the given height."},
92+
{"nblocks", RPCArg::Type::NUM, RPCArg::Default{120}, "The number of blocks, or -1 for blocks since last difficulty change."},
93+
{"height", RPCArg::Type::NUM, RPCArg::Default{-1}, "To estimate at the time of the given height."},
9494
},
9595
RPCResult{
9696
RPCResult::Type::NUM, "", "Hashes per second estimated"},
@@ -214,7 +214,7 @@ static RPCHelpMan generatetodescriptor()
214214
{
215215
{"num_blocks", RPCArg::Type::NUM, RPCArg::Optional::NO, "How many blocks are generated immediately."},
216216
{"descriptor", RPCArg::Type::STR, RPCArg::Optional::NO, "The descriptor to send the newly generated bitcoin to."},
217-
{"maxtries", RPCArg::Type::NUM, /* default */ ToString(DEFAULT_MAX_TRIES), "How many iterations to try."},
217+
{"maxtries", RPCArg::Type::NUM, RPCArg::Default{DEFAULT_MAX_TRIES}, "How many iterations to try."},
218218
},
219219
RPCResult{
220220
RPCResult::Type::ARR, "", "hashes of blocks generated",
@@ -258,7 +258,7 @@ static RPCHelpMan generatetoaddress()
258258
{
259259
{"nblocks", RPCArg::Type::NUM, RPCArg::Optional::NO, "How many blocks are generated immediately."},
260260
{"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The address to send the newly generated bitcoin to."},
261-
{"maxtries", RPCArg::Type::NUM, /* default */ ToString(DEFAULT_MAX_TRIES), "How many iterations to try."},
261+
{"maxtries", RPCArg::Type::NUM, RPCArg::Default{DEFAULT_MAX_TRIES}, "How many iterations to try."},
262262
},
263263
RPCResult{
264264
RPCResult::Type::ARR, "", "hashes of blocks generated",
@@ -525,7 +525,7 @@ static RPCHelpMan getblocktemplate()
525525
" https://github.com/bitcoin/bips/blob/master/bip-0009.mediawiki#getblocktemplate_changes\n"
526526
" https://github.com/bitcoin/bips/blob/master/bip-0145.mediawiki\n",
527527
{
528-
{"template_request", RPCArg::Type::OBJ, "{}", "Format of the template",
528+
{"template_request", RPCArg::Type::OBJ, RPCArg::Default{UniValue::VOBJ}, "Format of the template",
529529
{
530530
{"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"},
531531
{"capabilities", RPCArg::Type::ARR, /* treat as named arg */ RPCArg::Optional::OMITTED_NAMED_ARG, "A list of strings",
@@ -960,7 +960,7 @@ static RPCHelpMan submitblock()
960960
"See https://en.bitcoin.it/wiki/BIP_0022 for full specification.\n",
961961
{
962962
{"hexdata", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "the hex-encoded block data to submit"},
963-
{"dummy", RPCArg::Type::STR, /* default */ "ignored", "dummy value, for compatibility with BIP22. This value is ignored."},
963+
{"dummy", RPCArg::Type::STR, RPCArg::DefaultHint{"ignored"}, "dummy value, for compatibility with BIP22. This value is ignored."},
964964
},
965965
{
966966
RPCResult{"If the block was accepted", RPCResult::Type::NONE, "", ""},
@@ -1069,7 +1069,7 @@ static RPCHelpMan estimatesmartfee()
10691069
"in BIP 141 (witness data is discounted).\n",
10701070
{
10711071
{"conf_target", RPCArg::Type::NUM, RPCArg::Optional::NO, "Confirmation target in blocks (1 - 1008)"},
1072-
{"estimate_mode", RPCArg::Type::STR, /* default */ "conservative", "The fee estimate mode.\n"
1072+
{"estimate_mode", RPCArg::Type::STR, RPCArg::Default{"conservative"}, "The fee estimate mode.\n"
10731073
" Whether to return a more conservative estimate which also satisfies\n"
10741074
" a longer history. A conservative estimate potentially returns a\n"
10751075
" higher feerate and is more likely to be sufficient for the desired\n"
@@ -1140,7 +1140,7 @@ static RPCHelpMan estimaterawfee()
11401140
"defined in BIP 141 (witness data is discounted).\n",
11411141
{
11421142
{"conf_target", RPCArg::Type::NUM, RPCArg::Optional::NO, "Confirmation target in blocks (1 - 1008)"},
1143-
{"threshold", RPCArg::Type::NUM, /* default */ "0.95", "The proportion of transactions in a given feerate range that must have been\n"
1143+
{"threshold", RPCArg::Type::NUM, RPCArg::Default{0.95}, "The proportion of transactions in a given feerate range that must have been\n"
11441144
" confirmed within conf_target in order to consider those feerates as high enough and proceed to check\n"
11451145
" lower buckets."},
11461146
},

src/rpc/misc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ static RPCHelpMan createmultisig()
9090
{
9191
{"key", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, "The hex-encoded public key"},
9292
}},
93-
{"address_type", RPCArg::Type::STR, /* default */ "legacy", "The address type to use. Options are \"legacy\", \"p2sh-segwit\", and \"bech32\"."},
93+
{"address_type", RPCArg::Type::STR, RPCArg::Default{"legacy"}, "The address type to use. Options are \"legacy\", \"p2sh-segwit\", and \"bech32\"."},
9494
},
9595
RPCResult{
9696
RPCResult::Type::OBJ, "", "",
@@ -475,7 +475,7 @@ static RPCHelpMan getmemoryinfo()
475475
return RPCHelpMan{"getmemoryinfo",
476476
"Returns an object containing information about memory usage.\n",
477477
{
478-
{"mode", RPCArg::Type::STR, /* default */ "\"stats\"", "determines what kind of information is returned.\n"
478+
{"mode", RPCArg::Type::STR, RPCArg::Default{"stats"}, "determines what kind of information is returned.\n"
479479
" - \"stats\" returns general statistics about memory usage in the daemon.\n"
480480
" - \"mallocinfo\" returns an XML string describing low-level heap state (only available if compiled with glibc 2.10+)."},
481481
},

0 commit comments

Comments
 (0)