Page MenuHomePhabricator

D5809.id.diff
No OneTemporary

D5809.id.diff

diff --git a/src/rpc/abc.cpp b/src/rpc/abc.cpp
--- a/src/rpc/abc.cpp
+++ b/src/rpc/abc.cpp
@@ -14,15 +14,15 @@
static UniValue getexcessiveblock(const Config &config,
const JSONRPCRequest &request) {
if (request.fHelp || request.params.size() != 0) {
- throw std::runtime_error(
- RPCHelpMan{
- "getexcessiveblock", "\nReturn the excessive block size.", {}}
- .ToString() +
- "\nResult\n"
- " excessiveBlockSize (integer) block size in bytes\n"
- "\nExamples:\n" +
- HelpExampleCli("getexcessiveblock", "") +
- HelpExampleRpc("getexcessiveblock", ""));
+ throw std::runtime_error(RPCHelpMan{
+ "getexcessiveblock",
+ "\nReturn the excessive block size.",
+ {},
+ RPCResult{" excessiveBlockSize (integer) block size in bytes\n"},
+ RPCExamples{HelpExampleCli("getexcessiveblock", "") +
+ HelpExampleRpc("getexcessiveblock", "")},
+ }
+ .ToStringWithResultsAndExamples());
}
UniValue ret(UniValue::VOBJ);
@@ -33,24 +33,22 @@
static UniValue setexcessiveblock(Config &config,
const JSONRPCRequest &request) {
if (request.fHelp || request.params.size() != 1) {
- throw std::runtime_error(
- RPCHelpMan{
- "setexcessiveblock",
- "\nSet the excessive block size. Excessive blocks will not be "
- "used in the active chain or relayed. This discourages the "
- "propagation of blocks that you consider excessively large.",
- {
- {"blockSize", RPCArg::Type::NUM, /* opt */ false,
- /* default_value */ "",
- "Excessive block size in bytes. Must be greater than " +
- std::to_string(LEGACY_MAX_BLOCK_SIZE) + "."},
- }}
- .ToString() +
- "\nResult\n"
- " blockSize (integer) excessive block size in bytes\n"
- "\nExamples:\n" +
- HelpExampleCli("setexcessiveblock", "25000000") +
- HelpExampleRpc("setexcessiveblock", "25000000"));
+ throw std::runtime_error(RPCHelpMan{
+ "setexcessiveblock",
+ "\nSet the excessive block size. Excessive blocks will not be used "
+ "in the active chain or relayed. This discourages the propagation "
+ "of blocks that you consider excessively large.",
+ {
+ {"blockSize", RPCArg::Type::NUM, /* opt */ false,
+ /* default_value */ "",
+ "Excessive block size in bytes. Must be greater than " +
+ std::to_string(LEGACY_MAX_BLOCK_SIZE) + "."},
+ },
+ RPCResult{" blockSize (integer) excessive block size in bytes\n"},
+ RPCExamples{HelpExampleCli("setexcessiveblock", "25000000") +
+ HelpExampleRpc("setexcessiveblock", "25000000")},
+ }
+ .ToStringWithResultsAndExamples());
}
if (!request.params[0].isNum()) {
diff --git a/src/rpc/util.h b/src/rpc/util.h
--- a/src/rpc/util.h
+++ b/src/rpc/util.h
@@ -113,17 +113,65 @@
std::string ToDescriptionString(bool implicitly_required = false) const;
};
+struct RPCResult {
+ const std::string m_cond;
+ const std::string m_result;
+
+ explicit RPCResult(std::string result)
+ : m_cond{}, m_result{std::move(result)} {
+ CHECK_NONFATAL(!m_result.empty());
+ }
+
+ RPCResult(std::string cond, std::string result)
+ : m_cond{std::move(cond)}, m_result{std::move(result)} {
+ CHECK_NONFATAL(!m_cond.empty());
+ CHECK_NONFATAL(!m_result.empty());
+ }
+};
+
+struct RPCResults {
+ const std::vector<RPCResult> m_results;
+
+ RPCResults() : m_results{} {}
+
+ RPCResults(RPCResult result) : m_results{{result}} {}
+
+ RPCResults(std::initializer_list<RPCResult> results) : m_results{results} {}
+
+ /**
+ * Return the description string.
+ */
+ std::string ToDescriptionString() const;
+};
+
+struct RPCExamples {
+ const std::string m_examples;
+ RPCExamples(std::string examples) : m_examples(std::move(examples)) {}
+ RPCExamples() : m_examples(std::move("")) {}
+ std::string ToDescriptionString() const;
+};
+
class RPCHelpMan {
public:
+ // Remove once PR14987 backport is completed
RPCHelpMan(const std::string &name, const std::string &description,
const std::vector<RPCArg> &args);
+ RPCHelpMan(std::string name, std::string description,
+ std::vector<RPCArg> args, RPCResults results,
+ RPCExamples examples);
+
std::string ToString() const;
+ // Remove once PR14987 backport is completed
+ std::string ToStringWithResultsAndExamples() const;
+
private:
const std::string m_name;
const std::string m_description;
const std::vector<RPCArg> m_args;
+ const RPCResults m_results;
+ const RPCExamples m_examples;
};
#endif // BITCOIN_RPC_UTIL_H
diff --git a/src/rpc/util.cpp b/src/rpc/util.cpp
--- a/src/rpc/util.cpp
+++ b/src/rpc/util.cpp
@@ -283,9 +283,12 @@
}
};
+// Remove once backport of PR14987 is completed
RPCHelpMan::RPCHelpMan(const std::string &name, const std::string &description,
const std::vector<RPCArg> &args)
- : m_name{name}, m_description{description}, m_args{args} {
+ : m_name{std::move(name)}, m_description{std::move(description)},
+ m_args{std::move(args)}, m_results{RPCResults()}, m_examples{
+ RPCExamples()} {
std::set<std::string> named_args;
for (const auto &arg : m_args) {
// Should have unique named arguments
@@ -293,6 +296,37 @@
}
}
+RPCHelpMan::RPCHelpMan(std::string name, std::string description,
+ std::vector<RPCArg> args, RPCResults results,
+ RPCExamples examples)
+ : m_name{std::move(name)}, m_description{std::move(description)},
+ m_args{std::move(args)}, m_results{std::move(results)},
+ m_examples{std::move(examples)} {
+ std::set<std::string> named_args;
+ for (const auto &arg : m_args) {
+ // Should have unique named arguments
+ CHECK_NONFATAL(named_args.insert(arg.m_name).second);
+ }
+}
+
+std::string RPCResults::ToDescriptionString() const {
+ std::string result;
+ for (const auto &r : m_results) {
+ if (r.m_cond.empty()) {
+ result += "\nResult:\n";
+ } else {
+ result += "\nResult (" + r.m_cond + "):\n";
+ }
+ result += r.m_result;
+ }
+ return result;
+}
+
+std::string RPCExamples::ToDescriptionString() const {
+ return m_examples.empty() ? m_examples : "\nExamples:\n" + m_examples;
+}
+
+// Remove once PR14987 backport is completed
std::string RPCHelpMan::ToString() const {
std::string ret;
@@ -346,6 +380,66 @@
return ret;
}
+// Rename to ToString() once PR14987 backport is completed
+std::string RPCHelpMan::ToStringWithResultsAndExamples() const {
+ std::string ret;
+
+ // Oneline summary
+ ret += m_name;
+ bool was_optional{false};
+ for (const auto &arg : m_args) {
+ ret += " ";
+ if (arg.m_optional) {
+ if (!was_optional) {
+ ret += "( ";
+ }
+ was_optional = true;
+ } else {
+ if (was_optional) {
+ ret += ") ";
+ }
+ was_optional = false;
+ }
+ ret += arg.ToString(/* oneline */ true);
+ }
+ if (was_optional) {
+ ret += " )";
+ }
+ ret += "\n";
+
+ // Description
+ ret += m_description;
+
+ // Arguments
+ Sections sections;
+ for (size_t i{0}; i < m_args.size(); ++i) {
+ const auto &arg = m_args.at(i);
+
+ if (i == 0) {
+ ret += "\nArguments:\n";
+ }
+
+ // Push named argument name and description
+ sections.m_sections.emplace_back(std::to_string(i + 1) + ". " +
+ arg.m_name,
+ arg.ToDescriptionString());
+ sections.m_max_pad = std::max(sections.m_max_pad,
+ sections.m_sections.back().m_left.size());
+
+ // Recursively push nested args
+ sections.Push(arg);
+ }
+ ret += sections.ToString();
+
+ // Result
+ ret += m_results.ToDescriptionString();
+
+ // Examples
+ ret += m_examples.ToDescriptionString();
+
+ return ret;
+}
+
std::string RPCArg::ToDescriptionString(const bool implicitly_required) const {
std::string ret;
ret += "(";

File Metadata

Mime Type
text/plain
Expires
Mon, May 12, 01:46 (3 h, 47 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5761284
Default Alt Text
D5809.id.diff (8 KB)

Event Timeline