mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-12 06:58:57 +01:00
Merge #19386: rpc: Assert that RPCArg names are equal to CRPCCommand ones (server)
fa7592bfa8rpc: Update server to use new RPCHelpMan (MarcoFalke)aaaaad5627rpc: Add option to hide RPCArg (MarcoFalke)fa9708f94crpc: Assert that passed arg names are equal to hardcoded ones (MarcoFalke)faaeb2b0b3rpc: Add CRPCCommand constructor which takes RPCHelpMan (MarcoFalke)fa8ec00061rpc: Check that left section is not multiline (MarcoFalke) Pull request description: This is split out from #18531 to just touch the RPC methods in server. Description from the main pr: ### Motivation RPCArg names in the rpc help are currently only used for documentation. However, in the future they could be used to teach the server the named arguments. Named arguments are currently registered by the `CRPCCommand`s and duplicate the RPCArg names from the documentation. This redundancy is fragile, and has lead to errors in the past (despite having linters to catch those kind of errors). See section "bugs found" for a list of bugs that have been found as a result of the changes here. ### Changes The changes here add an assert in the `CRPCCommand` constructor that the RPCArg names are identical to the ones in the `CRPCCommand`. ### Future work > Here or follow up, makes sense to also assert type of returned UniValue? Sure, but let's not get ahead of ourselves. I am going to submit any further works as follow-ups, including: * Removing the CRPCCommand arguments, now that they are asserted to be equal and thus redundant * Removing all python regex linters on the args, now that RPCMan can be used to generate any output, including the cli.cpp table * Auto-formatting and sanity checking the RPCExamples with RPCMan * Checking passed-in json in self-check. Removing redundant checks * Checking returned json against documentation to avoid regressions or false documentation * Compile the RPC documentation at compile-time to ensure it doesn't change at runtime and is completely static ### Bugs found * The assert identified issue #18607 * The changes itself fixed bug #19250 ACKs for top commit: laanwj: ACKfa7592bfa8ryanofsky: Code review ACKfa7592bfa8. Looks great! Just some hidden arg and Check() and comment cleanups since last review Tree-SHA512: e64b6a212f4a3aeedeee47557559bde104d5fd40cdc1746b27eb2f3d4c8885d5e6e4dd287595ea11cdbc6a939654fe103cae765fd505875444d851f0abb11308
This commit is contained in:
@@ -385,9 +385,7 @@ struct Sections {
|
||||
PushSection({indent + "]" + (outer_type != OuterType::NONE ? "," : ""), ""});
|
||||
break;
|
||||
}
|
||||
|
||||
// no default case, so the compiler can warn about missing cases
|
||||
}
|
||||
} // no default case, so the compiler can warn about missing cases
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -398,6 +396,9 @@ struct Sections {
|
||||
std::string ret;
|
||||
const size_t pad = m_max_pad + 4;
|
||||
for (const auto& s : m_sections) {
|
||||
// The left part of a section is assumed to be a single line, usually it is the name of the JSON struct or a
|
||||
// brace like {, }, [, or ]
|
||||
CHECK_NONFATAL(s.m_left.find('\n') == std::string::npos);
|
||||
if (s.m_right.empty()) {
|
||||
ret += s.m_left;
|
||||
ret += "\n";
|
||||
@@ -432,7 +433,11 @@ struct Sections {
|
||||
};
|
||||
|
||||
RPCHelpMan::RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples)
|
||||
: RPCHelpMan{std::move(name), std::move(description), std::move(args), std::move(results), std::move(examples), nullptr} {}
|
||||
|
||||
RPCHelpMan::RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples, RPCMethodImpl fun)
|
||||
: m_name{std::move(name)},
|
||||
m_fun{std::move(fun)},
|
||||
m_description{std::move(description)},
|
||||
m_args{std::move(args)},
|
||||
m_results{std::move(results)},
|
||||
@@ -481,6 +486,16 @@ bool RPCHelpMan::IsValidNumArgs(size_t num_args) const
|
||||
}
|
||||
return num_required_args <= num_args && num_args <= m_args.size();
|
||||
}
|
||||
|
||||
std::vector<std::string> RPCHelpMan::GetArgNames() const
|
||||
{
|
||||
std::vector<std::string> ret;
|
||||
for (const auto& arg : m_args) {
|
||||
ret.emplace_back(arg.m_names);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string RPCHelpMan::ToString() const
|
||||
{
|
||||
std::string ret;
|
||||
@@ -489,6 +504,7 @@ std::string RPCHelpMan::ToString() const
|
||||
ret += m_name;
|
||||
bool was_optional{false};
|
||||
for (const auto& arg : m_args) {
|
||||
if (arg.m_hidden) continue;
|
||||
const bool optional = arg.IsOptional();
|
||||
ret += " ";
|
||||
if (optional) {
|
||||
@@ -510,6 +526,7 @@ std::string RPCHelpMan::ToString() const
|
||||
Sections sections;
|
||||
for (size_t i{0}; i < m_args.size(); ++i) {
|
||||
const auto& arg = m_args.at(i);
|
||||
if (arg.m_hidden) continue;
|
||||
|
||||
if (i == 0) ret += "\nArguments:\n";
|
||||
|
||||
@@ -589,9 +606,7 @@ std::string RPCArg::ToDescriptionString() const
|
||||
ret += "json array";
|
||||
break;
|
||||
}
|
||||
|
||||
// no default case, so the compiler can warn about missing cases
|
||||
}
|
||||
} // no default case, so the compiler can warn about missing cases
|
||||
}
|
||||
if (m_fallback.which() == 1) {
|
||||
ret += ", optional, default=" + boost::get<std::string>(m_fallback);
|
||||
@@ -609,9 +624,7 @@ std::string RPCArg::ToDescriptionString() const
|
||||
ret += ", required";
|
||||
break;
|
||||
}
|
||||
|
||||
// no default case, so the compiler can warn about missing cases
|
||||
}
|
||||
} // no default case, so the compiler can warn about missing cases
|
||||
}
|
||||
ret += ")";
|
||||
ret += m_description.empty() ? "" : " " + m_description;
|
||||
@@ -706,10 +719,7 @@ void RPCResult::ToSections(Sections& sections, const OuterType outer_type, const
|
||||
sections.PushSection({indent + "}" + maybe_separator, ""});
|
||||
return;
|
||||
}
|
||||
|
||||
// no default case, so the compiler can warn about missing cases
|
||||
}
|
||||
|
||||
} // no default case, so the compiler can warn about missing cases
|
||||
CHECK_NONFATAL(false);
|
||||
}
|
||||
|
||||
@@ -746,9 +756,7 @@ std::string RPCArg::ToStringObj(const bool oneline) const
|
||||
case Type::OBJ_USER_KEYS:
|
||||
// Currently unused, so avoid writing dead code
|
||||
CHECK_NONFATAL(false);
|
||||
|
||||
// no default case, so the compiler can warn about missing cases
|
||||
}
|
||||
} // no default case, so the compiler can warn about missing cases
|
||||
CHECK_NONFATAL(false);
|
||||
}
|
||||
|
||||
@@ -783,9 +791,7 @@ std::string RPCArg::ToString(const bool oneline) const
|
||||
}
|
||||
return "[" + res + "...]";
|
||||
}
|
||||
|
||||
// no default case, so the compiler can warn about missing cases
|
||||
}
|
||||
} // no default case, so the compiler can warn about missing cases
|
||||
CHECK_NONFATAL(false);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user