-
Notifications
You must be signed in to change notification settings - Fork 1.2k
backport: Merge bitcoin#26053, 28230, 28500 #7256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
bcf55ba
3fe49c9
e6df404
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -525,13 +525,59 @@ UniValue RPCHelpMan::HandleRequest(const JSONRPCRequest& request) const | |
| if (request.mode == JSONRPCRequest::GET_HELP || !IsValidNumArgs(request.params.size())) { | ||
| throw std::runtime_error(ToString()); | ||
| } | ||
| CHECK_NONFATAL(m_req == nullptr); | ||
| m_req = &request; | ||
| UniValue ret = m_fun(*this, request); | ||
| m_req = nullptr; | ||
| if (gArgs.GetBoolArg("-rpcdoccheck", DEFAULT_RPC_DOC_CHECK)) { | ||
| CHECK_NONFATAL(std::any_of(m_results.m_results.begin(), m_results.m_results.end(), [&ret](const RPCResult& res) { return res.MatchesType(ret); })); | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| using CheckFn = void(const RPCArg&); | ||
| static const UniValue* DetailMaybeArg(CheckFn* check, const std::vector<RPCArg>& params, const JSONRPCRequest* req, size_t i) | ||
| { | ||
| CHECK_NONFATAL(i < params.size()); | ||
| const UniValue& arg{CHECK_NONFATAL(req)->params[i]}; | ||
| const RPCArg& param{params.at(i)}; | ||
| if (check) check(param); | ||
|
|
||
| if (!arg.isNull()) return &arg; | ||
| if (!std::holds_alternative<RPCArg::Default>(param.m_fallback)) return nullptr; | ||
| return &std::get<RPCArg::Default>(param.m_fallback); | ||
| } | ||
|
|
||
| static void CheckRequiredOrDefault(const RPCArg& param) | ||
| { | ||
| // Must use `Arg<Type>(i)` to get the argument or its default value. | ||
| const bool required{ | ||
| std::holds_alternative<RPCArg::Optional>(param.m_fallback) && RPCArg::Optional::NO == std::get<RPCArg::Optional>(param.m_fallback), | ||
| }; | ||
| CHECK_NONFATAL(required || std::holds_alternative<RPCArg::Default>(param.m_fallback)); | ||
| } | ||
|
|
||
| #define TMPL_INST(check_param, ret_type, return_code) \ | ||
| template <> \ | ||
| ret_type RPCHelpMan::ArgValue<ret_type>(size_t i) const \ | ||
| { \ | ||
| const UniValue* maybe_arg{ \ | ||
| DetailMaybeArg(check_param, m_args, m_req, i), \ | ||
| }; \ | ||
| return return_code \ | ||
| } \ | ||
| void force_semicolon(ret_type) | ||
|
|
||
| // Optional arg (without default). Can also be called on required args, if needed. | ||
| TMPL_INST(nullptr, std::optional<double>, maybe_arg ? std::optional{maybe_arg->get_real()} : std::nullopt;); | ||
| TMPL_INST(nullptr, std::optional<bool>, maybe_arg ? std::optional{maybe_arg->get_bool()} : std::nullopt;); | ||
| TMPL_INST(nullptr, const std::string*, maybe_arg ? &maybe_arg->get_str() : nullptr;); | ||
|
|
||
| // Required arg or optional arg with default value. | ||
| TMPL_INST(CheckRequiredOrDefault, int, CHECK_NONFATAL(maybe_arg)->getInt<int>();); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Useful? React with 👍 / 👎. |
||
| TMPL_INST(CheckRequiredOrDefault, uint64_t, CHECK_NONFATAL(maybe_arg)->getInt<uint64_t>();); | ||
| TMPL_INST(CheckRequiredOrDefault, const std::string&, CHECK_NONFATAL(maybe_arg)->get_str();); | ||
|
|
||
| bool RPCHelpMan::IsValidNumArgs(size_t num_args) const | ||
| { | ||
| size_t num_required_args = 0; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep
MaybeArg()from materializing documented defaults.DetailMaybeArg()feedsRPCArg::Defaultback to bothArg()andMaybeArg(). ForMaybeArg<T>(), that makes an omitted parameter with a documented default indistinguishable from an explicitly provided value, which defeats callers that need to detect omission separately.🛠️ One way to preserve omission for
MaybeArg()Also applies to: 560-579
🤖 Prompt for AI Agents