Skip to content

Commit fa2c72d

Browse files
author
MacroFake
committed
rpc: Set RPCArg options with designated initializers
1 parent 141540a commit fa2c72d

File tree

8 files changed

+47
-49
lines changed

8 files changed

+47
-49
lines changed

src/rpc/blockchain.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ static RPCHelpMan gettxoutsetinfo()
856856
"Note this call may take some time if you are not using coinstatsindex.\n",
857857
{
858858
{"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'."},
859-
{"hash_or_height", RPCArg::Type::NUM, RPCArg::DefaultHint{"the current best block"}, "The block hash or height of the target height (only available with coinstatsindex).", "", {"", "string or numeric"}},
859+
{"hash_or_height", RPCArg::Type::NUM, RPCArg::DefaultHint{"the current best block"}, "The block hash or height of the target height (only available with coinstatsindex).", RPCArgOptions{.type_str={"", "string or numeric"}}},
860860
{"use_index", RPCArg::Type::BOOL, RPCArg::Default{true}, "Use coinstatsindex, if available."},
861861
},
862862
RPCResult{
@@ -1726,13 +1726,13 @@ static RPCHelpMan getblockstats()
17261726
"\nCompute per block statistics for a given window. All amounts are in satoshis.\n"
17271727
"It won't work for some heights with pruning.\n",
17281728
{
1729-
{"hash_or_height", RPCArg::Type::NUM, RPCArg::Optional::NO, "The block hash or height of the target block", "", {"", "string or numeric"}},
1729+
{"hash_or_height", RPCArg::Type::NUM, RPCArg::Optional::NO, "The block hash or height of the target block", RPCArgOptions{.type_str={"", "string or numeric"}}},
17301730
{"stats", RPCArg::Type::ARR, RPCArg::DefaultHint{"all values"}, "Values to plot (see result below)",
17311731
{
17321732
{"height", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "Selected statistic"},
17331733
{"time", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "Selected statistic"},
17341734
},
1735-
"stats"},
1735+
RPCArgOptions{.oneline_description="stats"}},
17361736
},
17371737
RPCResult{
17381738
RPCResult::Type::OBJ, "", "",
@@ -2052,7 +2052,7 @@ static RPCHelpMan scantxoutset()
20522052
{"range", RPCArg::Type::RANGE, RPCArg::Default{1000}, "The range of HD chain indexes to explore (either end or [begin,end])"},
20532053
}},
20542054
},
2055-
"[scanobjects,...]"},
2055+
RPCArgOptions{.oneline_description="[scanobjects,...]"}},
20562056
},
20572057
{
20582058
RPCResult{"when action=='start'; only returns after scan completes", RPCResult::Type::OBJ, "", "", {

src/rpc/mining.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ static RPCHelpMan getblocktemplate()
524524
{"str", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "other client side supported softfork deployment"},
525525
}},
526526
},
527-
"\"template_request\""},
527+
RPCArgOptions{.oneline_description="\"template_request\""}},
528528
},
529529
{
530530
RPCResult{"If the proposal was accepted with mode=='proposal'", RPCResult::Type::NONE, "", ""},

src/rpc/server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ static RPCHelpMan stop()
170170
// to the client (intended for testing)
171171
"\nRequest a graceful shutdown of " PACKAGE_NAME ".",
172172
{
173-
{"wait", RPCArg::Type::NUM, RPCArg::Optional::OMITTED_NAMED_ARG, "how long to wait in ms", "", {}, /*hidden=*/true},
173+
{"wait", RPCArg::Type::NUM, RPCArg::Optional::OMITTED_NAMED_ARG, "how long to wait in ms", RPCArgOptions{.hidden=true}},
174174
},
175175
RPCResult{RPCResult::Type::STR, "", "A string with the content '" + RESULT + "'"},
176176
RPCExamples{""},

src/rpc/util.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,8 @@ struct Sections {
418418
case RPCArg::Type::BOOL: {
419419
if (outer_type == OuterType::NONE) return; // Nothing more to do for non-recursive types on first recursion
420420
auto left = indent;
421-
if (arg.m_type_str.size() != 0 && push_name) {
422-
left += "\"" + arg.GetName() + "\": " + arg.m_type_str.at(0);
421+
if (arg.m_opts.type_str.size() != 0 && push_name) {
422+
left += "\"" + arg.GetName() + "\": " + arg.m_opts.type_str.at(0);
423423
} else {
424424
left += push_name ? arg.ToStringObj(/*oneline=*/false) : arg.ToString(/*oneline=*/false);
425425
}
@@ -618,7 +618,7 @@ std::string RPCHelpMan::ToString() const
618618
ret += m_name;
619619
bool was_optional{false};
620620
for (const auto& arg : m_args) {
621-
if (arg.m_hidden) break; // Any arg that follows is also hidden
621+
if (arg.m_opts.hidden) break; // Any arg that follows is also hidden
622622
const bool optional = arg.IsOptional();
623623
ret += " ";
624624
if (optional) {
@@ -639,7 +639,7 @@ std::string RPCHelpMan::ToString() const
639639
Sections sections;
640640
for (size_t i{0}; i < m_args.size(); ++i) {
641641
const auto& arg = m_args.at(i);
642-
if (arg.m_hidden) break; // Any arg that follows is also hidden
642+
if (arg.m_opts.hidden) break; // Any arg that follows is also hidden
643643

644644
if (i == 0) ret += "\nArguments:\n";
645645

@@ -704,8 +704,8 @@ std::string RPCArg::ToDescriptionString() const
704704
{
705705
std::string ret;
706706
ret += "(";
707-
if (m_type_str.size() != 0) {
708-
ret += m_type_str.at(1);
707+
if (m_opts.type_str.size() != 0) {
708+
ret += m_opts.type_str.at(1);
709709
} else {
710710
switch (m_type) {
711711
case Type::STR_HEX:
@@ -991,7 +991,7 @@ std::string RPCArg::ToStringObj(const bool oneline) const
991991

992992
std::string RPCArg::ToString(const bool oneline) const
993993
{
994-
if (oneline && !m_oneline_description.empty()) return m_oneline_description;
994+
if (oneline && !m_opts.oneline_description.empty()) return m_opts.oneline_description;
995995

996996
switch (m_type) {
997997
case Type::STR_HEX:

src/rpc/util.h

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ enum class OuterType {
137137
NONE, // Only set on first recursion
138138
};
139139

140+
struct RPCArgOptions {
141+
std::string oneline_description{}; //!< Should be empty unless it is supposed to override the auto-generated summary line
142+
std::vector<std::string> type_str{}; //!< Should be empty unless it is supposed to override the auto-generated type strings. Vector length is either 0 or 2, m_opts.type_str.at(0) will override the type of the value in a key-value pair, m_opts.type_str.at(1) will override the type in the argument description.
143+
bool hidden{false}; //!< For testing only
144+
};
145+
140146
struct RPCArg {
141147
enum class Type {
142148
OBJ,
@@ -169,30 +175,25 @@ struct RPCArg {
169175
using DefaultHint = std::string;
170176
using Default = UniValue;
171177
using Fallback = std::variant<Optional, /* hint for default value */ DefaultHint, /* default constant value */ Default>;
178+
172179
const std::string m_names; //!< The name of the arg (can be empty for inner args, can contain multiple aliases separated by | for named request arguments)
173180
const Type m_type;
174-
const bool m_hidden;
175181
const std::vector<RPCArg> m_inner; //!< Only used for arrays or dicts
176182
const Fallback m_fallback;
177183
const std::string m_description;
178-
const std::string m_oneline_description; //!< Should be empty unless it is supposed to override the auto-generated summary line
179-
const std::vector<std::string> m_type_str; //!< Should be empty unless it is supposed to override the auto-generated type strings. Vector length is either 0 or 2, m_type_str.at(0) will override the type of the value in a key-value pair, m_type_str.at(1) will override the type in the argument description.
184+
const RPCArgOptions m_opts;
180185

181186
RPCArg(
182187
const std::string name,
183188
const Type type,
184189
const Fallback fallback,
185190
const std::string description,
186-
const std::string oneline_description = "",
187-
const std::vector<std::string> type_str = {},
188-
const bool hidden = false)
191+
RPCArgOptions opts = {})
189192
: m_names{std::move(name)},
190193
m_type{std::move(type)},
191-
m_hidden{hidden},
192194
m_fallback{std::move(fallback)},
193195
m_description{std::move(description)},
194-
m_oneline_description{std::move(oneline_description)},
195-
m_type_str{std::move(type_str)}
196+
m_opts{std::move(opts)}
196197
{
197198
CHECK_NONFATAL(type != Type::ARR && type != Type::OBJ && type != Type::OBJ_USER_KEYS);
198199
}
@@ -203,16 +204,13 @@ struct RPCArg {
203204
const Fallback fallback,
204205
const std::string description,
205206
const std::vector<RPCArg> inner,
206-
const std::string oneline_description = "",
207-
const std::vector<std::string> type_str = {})
207+
RPCArgOptions opts = {})
208208
: m_names{std::move(name)},
209209
m_type{std::move(type)},
210-
m_hidden{false},
211210
m_inner{std::move(inner)},
212211
m_fallback{std::move(fallback)},
213212
m_description{std::move(description)},
214-
m_oneline_description{std::move(oneline_description)},
215-
m_type_str{std::move(type_str)}
213+
m_opts{std::move(opts)}
216214
{
217215
CHECK_NONFATAL(type == Type::ARR || type == Type::OBJ || type == Type::OBJ_USER_KEYS);
218216
}
@@ -227,7 +225,7 @@ struct RPCArg {
227225

228226
/**
229227
* Return the type string of the argument.
230-
* Set oneline to allow it to be overridden by a custom oneline type string (m_oneline_description).
228+
* Set oneline to allow it to be overridden by a custom oneline type string (m_opts.oneline_description).
231229
*/
232230
std::string ToString(bool oneline) const;
233231
/**

src/wallet/rpc/backup.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,15 +1266,15 @@ RPCHelpMan importmulti()
12661266
{
12671267
{"desc", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "Descriptor to import. If using descriptor, do not also provide address/scriptPubKey, scripts, or pubkeys"},
12681268
{"scriptPubKey", RPCArg::Type::STR, RPCArg::Optional::NO, "Type of scriptPubKey (string for script, json for address). Should not be provided if using a descriptor",
1269-
/*oneline_description=*/"", {"\"<script>\" | { \"address\":\"<address>\" }", "string / json"}
1269+
RPCArgOptions{.type_str={"\"<script>\" | { \"address\":\"<address>\" }", "string / json"}}
12701270
},
12711271
{"timestamp", RPCArg::Type::NUM, RPCArg::Optional::NO, "Creation time of the key expressed in " + UNIX_EPOCH_TIME + ",\n"
1272-
" or the string \"now\" to substitute the current synced blockchain time. The timestamp of the oldest\n"
1273-
" key will determine how far back blockchain rescans need to begin for missing wallet transactions.\n"
1274-
" \"now\" can be specified to bypass scanning, for keys which are known to never have been used, and\n"
1275-
" 0 can be specified to scan the entire blockchain. Blocks up to 2 hours before the earliest key\n"
1276-
" creation time of all keys being imported by the importmulti call will be scanned.",
1277-
/*oneline_description=*/"", {"timestamp | \"now\"", "integer / string"}
1272+
"or the string \"now\" to substitute the current synced blockchain time. The timestamp of the oldest\n"
1273+
"key will determine how far back blockchain rescans need to begin for missing wallet transactions.\n"
1274+
"\"now\" can be specified to bypass scanning, for keys which are known to never have been used, and\n"
1275+
"0 can be specified to scan the entire blockchain. Blocks up to 2 hours before the earliest key\n"
1276+
"creation time of all keys being imported by the importmulti call will be scanned.",
1277+
RPCArgOptions{.type_str={"timestamp | \"now\"", "integer / string"}}
12781278
},
12791279
{"redeemscript", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "Allowed only if the scriptPubKey is a P2SH or P2SH-P2WSH address/scriptPubKey"},
12801280
{"witnessscript", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "Allowed only if the scriptPubKey is a P2SH-P2WSH or P2WSH address/scriptPubKey"},
@@ -1296,12 +1296,12 @@ RPCHelpMan importmulti()
12961296
},
12971297
},
12981298
},
1299-
"\"requests\""},
1299+
RPCArgOptions{.oneline_description="\"requests\""}},
13001300
{"options", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED_NAMED_ARG, "",
13011301
{
13021302
{"rescan", RPCArg::Type::BOOL, RPCArg::Default{true}, "Scan the chain and mempool for wallet transactions after all imports."},
13031303
},
1304-
"\"options\""},
1304+
RPCArgOptions{.oneline_description="\"options\""}},
13051305
},
13061306
RPCResult{
13071307
RPCResult::Type::ARR, "", "Response is an array with the same size as the input that has the execution result",
@@ -1598,18 +1598,18 @@ RPCHelpMan importdescriptors()
15981598
{"range", RPCArg::Type::RANGE, RPCArg::Optional::OMITTED, "If a ranged descriptor is used, this specifies the end or the range (in the form [begin,end]) to import"},
15991599
{"next_index", RPCArg::Type::NUM, RPCArg::Optional::OMITTED, "If a ranged descriptor is set to active, this specifies the next index to generate addresses from"},
16001600
{"timestamp", RPCArg::Type::NUM, RPCArg::Optional::NO, "Time from which to start rescanning the blockchain for this descriptor, in " + UNIX_EPOCH_TIME + "\n"
1601-
" Use the string \"now\" to substitute the current synced blockchain time.\n"
1602-
" \"now\" can be specified to bypass scanning, for outputs which are known to never have been used, and\n"
1603-
" 0 can be specified to scan the entire blockchain. Blocks up to 2 hours before the earliest timestamp\n"
1601+
"Use the string \"now\" to substitute the current synced blockchain time.\n"
1602+
"\"now\" can be specified to bypass scanning, for outputs which are known to never have been used, and\n"
1603+
"0 can be specified to scan the entire blockchain. Blocks up to 2 hours before the earliest timestamp\n"
16041604
"of all descriptors being imported will be scanned as well as the mempool.",
1605-
/*oneline_description=*/"", {"timestamp | \"now\"", "integer / string"}
1605+
RPCArgOptions{.type_str={"timestamp | \"now\"", "integer / string"}}
16061606
},
16071607
{"internal", RPCArg::Type::BOOL, RPCArg::Default{false}, "Whether matching outputs should be treated as not incoming payments (e.g. change)"},
16081608
{"label", RPCArg::Type::STR, RPCArg::Default{""}, "Label to assign to the address, only allowed with internal=false. Disabled for ranged descriptors"},
16091609
},
16101610
},
16111611
},
1612-
"\"requests\""},
1612+
RPCArgOptions{.oneline_description="\"requests\""}},
16131613
},
16141614
RPCResult{
16151615
RPCResult::Type::ARR, "", "Response is an array with the same size as the input that has the execution result",

src/wallet/rpc/coins.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ RPCHelpMan listunspent()
520520
{"maximumCount", RPCArg::Type::NUM, RPCArg::DefaultHint{"unlimited"}, "Maximum number of UTXOs"},
521521
{"minimumSumAmount", RPCArg::Type::AMOUNT, RPCArg::DefaultHint{"unlimited"}, "Minimum sum value of all UTXOs in " + CURRENCY_UNIT + ""},
522522
},
523-
"query_options"},
523+
RPCArgOptions{.oneline_description="query_options"}},
524524
},
525525
RPCResult{
526526
RPCResult::Type::ARR, "", "",

src/wallet/rpc/spend.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ RPCHelpMan sendmany()
317317
"\nSend multiple times. Amounts are double-precision floating point numbers." +
318318
HELP_REQUIRING_PASSPHRASE,
319319
{
320-
{"dummy", RPCArg::Type::STR, RPCArg::Optional::NO, "Must be set to \"\" for backwards compatibility.", "\"\""},
320+
{"dummy", RPCArg::Type::STR, RPCArg::Optional::NO, "Must be set to \"\" for backwards compatibility.", RPCArgOptions{.oneline_description="\"\""}},
321321
{"amounts", RPCArg::Type::OBJ_USER_KEYS, RPCArg::Optional::NO, "The addresses and amounts",
322322
{
323323
{"address", RPCArg::Type::AMOUNT, RPCArg::Optional::NO, "The bitcoin address is the key, the numeric amount (can be string) in " + CURRENCY_UNIT + " is the value"},
@@ -775,7 +775,7 @@ RPCHelpMan fundrawtransaction()
775775
},
776776
},
777777
FundTxDoc()),
778-
"options"},
778+
RPCArgOptions{.oneline_description="options"}},
779779
{"iswitness", RPCArg::Type::BOOL, RPCArg::DefaultHint{"depends on heuristic tests"}, "Whether the transaction hex is a serialized witness transaction.\n"
780780
"If iswitness is not present, heuristic tests will be used in decoding.\n"
781781
"If true, only witness deserialization will be tried.\n"
@@ -969,7 +969,7 @@ static RPCHelpMan bumpfee_helper(std::string method_name)
969969
{"estimate_mode", RPCArg::Type::STR, RPCArg::Default{"unset"}, "The fee estimate mode, must be one of (case insensitive):\n"
970970
"\"" + FeeModes("\"\n\"") + "\""},
971971
},
972-
"options"},
972+
RPCArgOptions{.oneline_description="options"}},
973973
},
974974
RPCResult{
975975
RPCResult::Type::OBJ, "", "", Cat(
@@ -1174,7 +1174,7 @@ RPCHelpMan send()
11741174
},
11751175
},
11761176
FundTxDoc()),
1177-
"options"},
1177+
RPCArgOptions{.oneline_description="options"}},
11781178
},
11791179
RPCResult{
11801180
RPCResult::Type::OBJ, "", "",
@@ -1282,7 +1282,7 @@ RPCHelpMan sendall()
12821282
},
12831283
FundTxDoc()
12841284
),
1285-
"options"
1285+
RPCArgOptions{.oneline_description="options"}
12861286
},
12871287
},
12881288
RPCResult{
@@ -1603,7 +1603,7 @@ RPCHelpMan walletcreatefundedpsbt()
16031603
},
16041604
},
16051605
FundTxDoc()),
1606-
"options"},
1606+
RPCArgOptions{.oneline_description="options"}},
16071607
{"bip32derivs", RPCArg::Type::BOOL, RPCArg::Default{true}, "Include BIP 32 derivation paths for public keys if we know them"},
16081608
},
16091609
RPCResult{

0 commit comments

Comments
 (0)