Skip to content

Commit 33eef56

Browse files
author
MacroFake
committed
Merge bitcoin/bitcoin#26074: refactor: Set RPCArg options with designated initializers
fa2c72d rpc: Set RPCArg options with designated initializers (MacroFake) Pull request description: For optional constructor arguments, use a new struct. This comes with two benefits: * Earlier unused optional arguments can be omitted * Designated initializers can be used ACKs for top commit: stickies-v: re-ACK fa2c72d Tree-SHA512: 2a0619548187cc7437fee2466ac4780746490622f202659f53641be01bc2a1fea4416d1a77f3e963bf7c4cce62899b61fab0b9683440cf82f68be44f63826658
2 parents 437b608 + fa2c72d commit 33eef56

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
@@ -168,7 +168,7 @@ static RPCHelpMan stop()
168168
// to the client (intended for testing)
169169
"\nRequest a graceful shutdown of " PACKAGE_NAME ".",
170170
{
171-
{"wait", RPCArg::Type::NUM, RPCArg::Optional::OMITTED_NAMED_ARG, "how long to wait in ms", "", {}, /*hidden=*/true},
171+
{"wait", RPCArg::Type::NUM, RPCArg::Optional::OMITTED_NAMED_ARG, "how long to wait in ms", RPCArgOptions{.hidden=true}},
172172
},
173173
RPCResult{RPCResult::Type::STR, "", "A string with the content '" + RESULT + "'"},
174174
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",
@@ -1609,18 +1609,18 @@ RPCHelpMan importdescriptors()
16091609
{"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"},
16101610
{"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"},
16111611
{"timestamp", RPCArg::Type::NUM, RPCArg::Optional::NO, "Time from which to start rescanning the blockchain for this descriptor, in " + UNIX_EPOCH_TIME + "\n"
1612-
" Use the string \"now\" to substitute the current synced blockchain time.\n"
1613-
" \"now\" can be specified to bypass scanning, for outputs which are known to never have been used, and\n"
1614-
" 0 can be specified to scan the entire blockchain. Blocks up to 2 hours before the earliest timestamp\n"
1612+
"Use the string \"now\" to substitute the current synced blockchain time.\n"
1613+
"\"now\" can be specified to bypass scanning, for outputs which are known to never have been used, and\n"
1614+
"0 can be specified to scan the entire blockchain. Blocks up to 2 hours before the earliest timestamp\n"
16151615
"of all descriptors being imported will be scanned as well as the mempool.",
1616-
/*oneline_description=*/"", {"timestamp | \"now\"", "integer / string"}
1616+
RPCArgOptions{.type_str={"timestamp | \"now\"", "integer / string"}}
16171617
},
16181618
{"internal", RPCArg::Type::BOOL, RPCArg::Default{false}, "Whether matching outputs should be treated as not incoming payments (e.g. change)"},
16191619
{"label", RPCArg::Type::STR, RPCArg::Default{""}, "Label to assign to the address, only allowed with internal=false. Disabled for ranged descriptors"},
16201620
},
16211621
},
16221622
},
1623-
"\"requests\""},
1623+
RPCArgOptions{.oneline_description="\"requests\""}},
16241624
},
16251625
RPCResult{
16261626
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
@@ -516,7 +516,7 @@ RPCHelpMan listunspent()
516516
{"maximumCount", RPCArg::Type::NUM, RPCArg::DefaultHint{"unlimited"}, "Maximum number of UTXOs"},
517517
{"minimumSumAmount", RPCArg::Type::AMOUNT, RPCArg::DefaultHint{"unlimited"}, "Minimum sum value of all UTXOs in " + CURRENCY_UNIT + ""},
518518
},
519-
"query_options"},
519+
RPCArgOptions{.oneline_description="query_options"}},
520520
},
521521
RPCResult{
522522
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"},
@@ -774,7 +774,7 @@ RPCHelpMan fundrawtransaction()
774774
},
775775
},
776776
FundTxDoc()),
777-
"options"},
777+
RPCArgOptions{.oneline_description="options"}},
778778
{"iswitness", RPCArg::Type::BOOL, RPCArg::DefaultHint{"depends on heuristic tests"}, "Whether the transaction hex is a serialized witness transaction.\n"
779779
"If iswitness is not present, heuristic tests will be used in decoding.\n"
780780
"If true, only witness deserialization will be tried.\n"
@@ -968,7 +968,7 @@ static RPCHelpMan bumpfee_helper(std::string method_name)
968968
{"estimate_mode", RPCArg::Type::STR, RPCArg::Default{"unset"}, "The fee estimate mode, must be one of (case insensitive):\n"
969969
"\"" + FeeModes("\"\n\"") + "\""},
970970
},
971-
"options"},
971+
RPCArgOptions{.oneline_description="options"}},
972972
},
973973
RPCResult{
974974
RPCResult::Type::OBJ, "", "", Cat(
@@ -1173,7 +1173,7 @@ RPCHelpMan send()
11731173
},
11741174
},
11751175
FundTxDoc()),
1176-
"options"},
1176+
RPCArgOptions{.oneline_description="options"}},
11771177
},
11781178
RPCResult{
11791179
RPCResult::Type::OBJ, "", "",
@@ -1281,7 +1281,7 @@ RPCHelpMan sendall()
12811281
},
12821282
FundTxDoc()
12831283
),
1284-
"options"
1284+
RPCArgOptions{.oneline_description="options"}
12851285
},
12861286
},
12871287
RPCResult{
@@ -1611,7 +1611,7 @@ RPCHelpMan walletcreatefundedpsbt()
16111611
},
16121612
},
16131613
FundTxDoc()),
1614-
"options"},
1614+
RPCArgOptions{.oneline_description="options"}},
16151615
{"bip32derivs", RPCArg::Type::BOOL, RPCArg::Default{true}, "Include BIP 32 derivation paths for public keys if we know them"},
16161616
},
16171617
RPCResult{

0 commit comments

Comments
 (0)