aboutsummaryrefslogtreecommitdiff
path: root/src/rpc/util.cpp
diff options
context:
space:
mode:
authorMarcoFalke <falke.marco@gmail.com>2021-04-03 09:25:48 +0200
committerMarcoFalke <falke.marco@gmail.com>2021-04-03 09:26:31 +0200
commitad4bf8a94594e7fe424e409ba9474d91584bb78c (patch)
treea5b1d49a2522c23afdf28e17a3e78ba00eae1097 /src/rpc/util.cpp
parent9565dafed5973b35d9b242ff5352ee7abdc5799b (diff)
parentfa8192f42e1d24444f1d0433c96dbce1adf76967 (diff)
Merge #20459: rpc: Fail to return undocumented return values
fa8192f42e1d24444f1d0433c96dbce1adf76967 rpc: Fail to return undocumented return values (MarcoFalke) Pull request description: Currently a few return values are undocumented. This is causing confusion at the least. See for example #18476 Fix this by treating it as an internal bug to return undocumented return values. ACKs for top commit: ryanofsky: Code review ACK fa8192f42e1d24444f1d0433c96dbce1adf76967. Only changes: rebase, no const_cast suggestion, and tostring cleanups needed after suggestion Tree-SHA512: c006905639bafe3045de152b00c34d9864731becb3c4f468bdd61a392f10d7e7cd89a54862c8daa8c11ac4eea0eb5f13b0f647d21e21a0a797b54191cff7238c
Diffstat (limited to 'src/rpc/util.cpp')
-rw-r--r--src/rpc/util.cpp46
1 files changed, 44 insertions, 2 deletions
diff --git a/src/rpc/util.cpp b/src/rpc/util.cpp
index 44e58cb75f..52499cd1d6 100644
--- a/src/rpc/util.cpp
+++ b/src/rpc/util.cpp
@@ -442,6 +442,7 @@ std::string RPCResults::ToDescriptionString() const
{
std::string result;
for (const auto& r : m_results) {
+ if (r.m_type == RPCResult::Type::ANY) continue; // for testing only
if (r.m_cond.empty()) {
result += "\nResult:\n";
} else {
@@ -459,7 +460,7 @@ std::string RPCExamples::ToDescriptionString() const
return m_examples.empty() ? m_examples : "\nExamples:\n" + m_examples;
}
-UniValue RPCHelpMan::HandleRequest(const JSONRPCRequest& request)
+UniValue RPCHelpMan::HandleRequest(const JSONRPCRequest& request) const
{
if (request.mode == JSONRPCRequest::GET_ARGS) {
return GetArgMap();
@@ -471,7 +472,9 @@ UniValue RPCHelpMan::HandleRequest(const JSONRPCRequest& request)
if (request.mode == JSONRPCRequest::GET_HELP || !IsValidNumArgs(request.params.size())) {
throw std::runtime_error(ToString());
}
- return m_fun(*this, request);
+ const UniValue ret = m_fun(*this, request);
+ 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;
}
bool RPCHelpMan::IsValidNumArgs(size_t num_args) const
@@ -677,6 +680,9 @@ void RPCResult::ToSections(Sections& sections, const OuterType outer_type, const
sections.PushSection({indent + "..." + maybe_separator, m_description});
return;
}
+ case Type::ANY: {
+ CHECK_NONFATAL(false); // Only for testing
+ }
case Type::NONE: {
sections.PushSection({indent + "null" + maybe_separator, Description("json null")});
return;
@@ -742,6 +748,42 @@ void RPCResult::ToSections(Sections& sections, const OuterType outer_type, const
CHECK_NONFATAL(false);
}
+bool RPCResult::MatchesType(const UniValue& result) const
+{
+ switch (m_type) {
+ case Type::ELISION: {
+ return false;
+ }
+ case Type::ANY: {
+ return true;
+ }
+ case Type::NONE: {
+ return UniValue::VNULL == result.getType();
+ }
+ case Type::STR:
+ case Type::STR_HEX: {
+ return UniValue::VSTR == result.getType();
+ }
+ case Type::NUM:
+ case Type::STR_AMOUNT:
+ case Type::NUM_TIME: {
+ return UniValue::VNUM == result.getType();
+ }
+ case Type::BOOL: {
+ return UniValue::VBOOL == result.getType();
+ }
+ case Type::ARR_FIXED:
+ case Type::ARR: {
+ return UniValue::VARR == result.getType();
+ }
+ case Type::OBJ_DYN:
+ case Type::OBJ: {
+ return UniValue::VOBJ == result.getType();
+ }
+ } // no default case, so the compiler can warn about missing cases
+ CHECK_NONFATAL(false);
+}
+
std::string RPCArg::ToStringObj(const bool oneline) const
{
std::string res;