Skip to content

Commit

Permalink
Merge bitcoin/bitcoin#24155: doc: Fix rpc docs
Browse files Browse the repository at this point in the history
fac8caa doc: Fix rpc docs (MarcoFalke)

Pull request description:

  Broken in commit 39d9bbe.

  The fix removes the "type" `OBJ_EMPTY` added in commit 8d1a3e6, which isn't really a separate type and instead runs a check on `OBJ` whether it is empty or not.

ACKs for top commit:
  Sjors:
    tACK fac8caa

Tree-SHA512: dd978fe526a45095800249204afd26a239078e83b15124a5756ac078c473a677a3084b8f54e34d6dd5580abef7275c875a14bc9eb20d8feab066dfb0f0932967
  • Loading branch information
fanquake committed Jan 26, 2022
2 parents 2935bd9 + fac8caa commit e3699b7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -790,15 +790,15 @@ static RPCHelpMan getblockfrompeer()
{
return RPCHelpMan{
"getblockfrompeer",
"\nAttempt to fetch block from a given peer.\n"
"Attempt to fetch block from a given peer.\n"
"\nWe must have the header for this block, e.g. using submitheader.\n"
"Subsequent calls for the same block and a new peer will cause the response from the previous peer to be ignored.\n"
"\nReturns an empty JSON object if the request was successfully scheduled.",
{
{"block_hash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The block hash to try to fetch"},
{"peer_id", RPCArg::Type::NUM, RPCArg::Optional::NO, "The peer to fetch it from (see getpeerinfo for peer IDs)"},
},
RPCResult{RPCResult::Type::OBJ_EMPTY, "", /*optional=*/ false, "", {}},
RPCResult{RPCResult::Type::OBJ, "", /*optional=*/false, "", {}},
RPCExamples{
HelpExampleCli("getblockfrompeer", "\"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\" 0")
+ HelpExampleRpc("getblockfrompeer", "\"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\" 0")
Expand Down
21 changes: 15 additions & 6 deletions src/rpc/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -830,16 +830,15 @@ void RPCResult::ToSections(Sections& sections, const OuterType outer_type, const
return;
}
case Type::OBJ_DYN:
case Type::OBJ_EMPTY: {
sections.PushSection({indent + maybe_key + "{}", Description("empty JSON object")});
return;
}
case Type::OBJ: {
if (m_inner.empty()) {
sections.PushSection({indent + maybe_key + "{}", Description("empty JSON object")});
return;
}
sections.PushSection({indent + maybe_key + "{", Description("json object")});
for (const auto& i : m_inner) {
i.ToSections(sections, OuterType::OBJ, current_indent + 2);
}
CHECK_NONFATAL(!m_inner.empty());
if (m_type == Type::OBJ_DYN && m_inner.back().m_type != Type::ELISION) {
// If the dictionary keys are dynamic, use three dots for continuation
sections.PushSection({indent_next + "...", ""});
Expand Down Expand Up @@ -883,14 +882,24 @@ bool RPCResult::MatchesType(const UniValue& result) const
return UniValue::VARR == result.getType();
}
case Type::OBJ_DYN:
case Type::OBJ_EMPTY:
case Type::OBJ: {
return UniValue::VOBJ == result.getType();
}
} // no default case, so the compiler can warn about missing cases
CHECK_NONFATAL(false);
}

void RPCResult::CheckInnerDoc() const
{
if (m_type == Type::OBJ) {
// May or may not be empty
return;
}
// Everything else must either be empty or not
const bool inner_needed{m_type == Type::ARR || m_type == Type::ARR_FIXED || m_type == Type::OBJ_DYN};
CHECK_NONFATAL(inner_needed != m_inner.empty());
}

std::string RPCArg::ToStringObj(const bool oneline) const
{
std::string res;
Expand Down
10 changes: 5 additions & 5 deletions src/rpc/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ struct RPCResult {
STR_AMOUNT, //!< Special string to represent a floating point amount
STR_HEX, //!< Special string with only hex chars
OBJ_DYN, //!< Special dictionary with keys that are not literals
OBJ_EMPTY, //!< Special type to allow empty OBJ
ARR_FIXED, //!< Special array that has a fixed number of entries
NUM_TIME, //!< Special numeric to denote unix epoch time
ELISION, //!< Special type to denote elision (...)
Expand Down Expand Up @@ -268,8 +267,7 @@ struct RPCResult {
m_cond{std::move(cond)}
{
CHECK_NONFATAL(!m_cond.empty());
const bool inner_needed{type == Type::ARR || type == Type::ARR_FIXED || type == Type::OBJ || type == Type::OBJ_DYN};
CHECK_NONFATAL(inner_needed != inner.empty());
CheckInnerDoc();
}

RPCResult(
Expand All @@ -293,8 +291,7 @@ struct RPCResult {
m_description{std::move(description)},
m_cond{}
{
const bool inner_needed{type == Type::ARR || type == Type::ARR_FIXED || type == Type::OBJ || type == Type::OBJ_DYN};
CHECK_NONFATAL(inner_needed != inner.empty());
CheckInnerDoc();
}

RPCResult(
Expand All @@ -312,6 +309,9 @@ struct RPCResult {
std::string ToDescriptionString() const;
/** Check whether the result JSON type matches. */
bool MatchesType(const UniValue& result) const;

private:
void CheckInnerDoc() const;
};

struct RPCResults {
Expand Down

0 comments on commit e3699b7

Please sign in to comment.