diff --git a/src/rpc/abc.cpp b/src/rpc/abc.cpp index 6bb7e9c10..dd1e9e809 100644 --- a/src/rpc/abc.cpp +++ b/src/rpc/abc.cpp @@ -1,98 +1,96 @@ // Copyright (c) 2017-2020 The Bitcoin developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include #include #include #include #include #include #include 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); ret.pushKV("excessiveBlockSize", config.GetMaxBlockSize()); return ret; } 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()) { throw JSONRPCError( RPC_INVALID_PARAMETER, std::string( "Invalid parameter, excessiveblock must be an integer")); } int64_t ebs = request.params[0].get_int64(); // Do not allow maxBlockSize to be set below historic 1MB limit if (ebs <= int64_t(LEGACY_MAX_BLOCK_SIZE)) { throw JSONRPCError( RPC_INVALID_PARAMETER, std::string( "Invalid parameter, excessiveblock must be larger than ") + std::to_string(LEGACY_MAX_BLOCK_SIZE)); } // Set the new max block size. if (!config.SetMaxBlockSize(ebs)) { throw JSONRPCError(RPC_INVALID_PARAMETER, "Unexpected error"); } // settingsToUserAgentString(); std::ostringstream ret; ret << "Excessive Block set to " << ebs << " bytes."; return UniValue(ret.str()); } // clang-format off static const ContextFreeRPCCommand commands[] = { // category name actor (function) argNames // ------------------- ------------------------ ---------------------- ---------- { "network", "getexcessiveblock", getexcessiveblock, {}}, { "network", "setexcessiveblock", setexcessiveblock, {"maxBlockSize"}}, }; // clang-format on void RegisterABCRPCCommands(CRPCTable &t) { for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++) { t.appendCommand(commands[vcidx].name, &commands[vcidx]); } } diff --git a/src/rpc/util.cpp b/src/rpc/util.cpp index 9447e9ff1..fc25ce90b 100644 --- a/src/rpc/util.cpp +++ b/src/rpc/util.cpp @@ -1,485 +1,579 @@ // Copyright (c) 2017 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include #include #include #include #include #include #include #include #include InitInterfaces *g_rpc_interfaces = nullptr; // Converts a hex string to a public key if possible CPubKey HexToPubKey(const std::string &hex_in) { if (!IsHex(hex_in)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid public key: " + hex_in); } CPubKey vchPubKey(ParseHex(hex_in)); if (!vchPubKey.IsFullyValid()) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid public key: " + hex_in); } return vchPubKey; } // Retrieves a public key for an address from the given CKeyStore CPubKey AddrToPubKey(const CChainParams &chainparams, CKeyStore *const keystore, const std::string &addr_in) { CTxDestination dest = DecodeDestination(addr_in, chainparams); if (!IsValidDestination(dest)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address: " + addr_in); } CKeyID key = GetKeyForDestination(*keystore, dest); if (key.IsNull()) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("%s does not refer to a key", addr_in)); } CPubKey vchPubKey; if (!keystore->GetPubKey(key, vchPubKey)) { throw JSONRPCError( RPC_INVALID_ADDRESS_OR_KEY, strprintf("no full public key for address %s", addr_in)); } if (!vchPubKey.IsFullyValid()) { throw JSONRPCError(RPC_INTERNAL_ERROR, "Wallet contains an invalid public key"); } return vchPubKey; } // Creates a multisig redeemscript from a given list of public keys and number // required. CScript CreateMultisigRedeemscript(const int required, const std::vector &pubkeys) { // Gather public keys if (required < 1) { throw JSONRPCError( RPC_INVALID_PARAMETER, "a multisignature address must require at least one key to redeem"); } if ((int)pubkeys.size() < required) { throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("not enough keys supplied (got %u keys, " "but need at least %d to redeem)", pubkeys.size(), required)); } if (pubkeys.size() > 16) { throw JSONRPCError(RPC_INVALID_PARAMETER, "Number of keys involved in the multisignature " "address creation > 16\nReduce the number"); } CScript result = GetScriptForMultisig(required, pubkeys); if (result.size() > MAX_SCRIPT_ELEMENT_SIZE) { throw JSONRPCError( RPC_INVALID_PARAMETER, (strprintf("redeemScript exceeds size limit: %d > %d", result.size(), MAX_SCRIPT_ELEMENT_SIZE))); } return result; } class DescribeAddressVisitor : public boost::static_visitor { public: explicit DescribeAddressVisitor() {} UniValue operator()(const CNoDestination &dest) const { return UniValue(UniValue::VOBJ); } UniValue operator()(const CKeyID &keyID) const { UniValue obj(UniValue::VOBJ); obj.pushKV("isscript", false); return obj; } UniValue operator()(const CScriptID &scriptID) const { UniValue obj(UniValue::VOBJ); obj.pushKV("isscript", true); return obj; } }; UniValue DescribeAddress(const CTxDestination &dest) { return boost::apply_visitor(DescribeAddressVisitor(), dest); } RPCErrorCode RPCErrorFromTransactionError(TransactionError terr) { switch (terr) { case TransactionError::MEMPOOL_REJECTED: return RPC_TRANSACTION_REJECTED; case TransactionError::ALREADY_IN_CHAIN: return RPC_TRANSACTION_ALREADY_IN_CHAIN; case TransactionError::P2P_DISABLED: return RPC_CLIENT_P2P_DISABLED; case TransactionError::INVALID_PSBT: case TransactionError::PSBT_MISMATCH: return RPC_INVALID_PARAMETER; case TransactionError::SIGHASH_MISMATCH: return RPC_DESERIALIZATION_ERROR; default: break; } return RPC_TRANSACTION_ERROR; } UniValue JSONRPCTransactionError(TransactionError terr, const std::string &err_string) { if (err_string.length() > 0) { return JSONRPCError(RPCErrorFromTransactionError(terr), err_string); } else { return JSONRPCError(RPCErrorFromTransactionError(terr), TransactionErrorString(terr)); } } struct Section { Section(const std::string &left, const std::string &right) : m_left{left}, m_right{right} {} const std::string m_left; const std::string m_right; }; struct Sections { std::vector
m_sections; size_t m_max_pad{0}; void PushSection(const Section &s) { m_max_pad = std::max(m_max_pad, s.m_left.size()); m_sections.push_back(s); } enum class OuterType { ARR, OBJ, // Only set on first recursion NAMED_ARG, }; void Push(const RPCArg &arg, const size_t current_indent = 5, const OuterType outer_type = OuterType::NAMED_ARG) { const auto indent = std::string(current_indent, ' '); const auto indent_next = std::string(current_indent + 2, ' '); switch (arg.m_type) { case RPCArg::Type::STR_HEX: case RPCArg::Type::STR: case RPCArg::Type::NUM: case RPCArg::Type::AMOUNT: case RPCArg::Type::BOOL: { // Nothing more to do for non-recursive types on first recursion if (outer_type == OuterType::NAMED_ARG) { return; } auto left = indent; if (arg.m_type_str.size() != 0 && outer_type == OuterType::OBJ) { left += "\"" + arg.m_name + "\": " + arg.m_type_str.at(0); } else { left += outer_type == OuterType::OBJ ? arg.ToStringObj(/* oneline */ false) : arg.ToString(/* oneline */ false); } left += ","; PushSection({left, arg.ToDescriptionString( /* implicitly_required */ outer_type == OuterType::ARR)}); break; } case RPCArg::Type::OBJ: case RPCArg::Type::OBJ_USER_KEYS: { const auto right = outer_type == OuterType::NAMED_ARG ? "" : arg.ToDescriptionString( /* implicitly_required */ outer_type == OuterType::ARR); PushSection({indent + "{", right}); for (const auto &arg_inner : arg.m_inner) { Push(arg_inner, current_indent + 2, OuterType::OBJ); } if (arg.m_type != RPCArg::Type::OBJ) { PushSection({indent_next + "...", ""}); } PushSection( {indent + "}" + (outer_type != OuterType::NAMED_ARG ? "," : ""), ""}); break; } case RPCArg::Type::ARR: { auto left = indent; left += outer_type == OuterType::OBJ ? "\"" + arg.m_name + "\": " : ""; left += "["; const auto right = outer_type == OuterType::NAMED_ARG ? "" : arg.ToDescriptionString( /* implicitly_required */ outer_type == OuterType::ARR); PushSection({left, right}); for (const auto &arg_inner : arg.m_inner) { Push(arg_inner, current_indent + 2, OuterType::ARR); } PushSection({indent_next + "...", ""}); PushSection( {indent + "]" + (outer_type != OuterType::NAMED_ARG ? "," : ""), ""}); break; } // no default case, so the compiler can warn about missing cases } } std::string ToString() const { std::string ret; const size_t pad = m_max_pad + 4; for (const auto &s : m_sections) { if (s.m_right.empty()) { ret += s.m_left; ret += "\n"; continue; } std::string left = s.m_left; left.resize(pad, ' '); ret += left; // Properly pad after newlines std::string right; size_t begin = 0; size_t new_line_pos = s.m_right.find_first_of('\n'); while (true) { right += s.m_right.substr(begin, new_line_pos - begin); if (new_line_pos == std::string::npos) { // No new line break; } right += "\n" + std::string(pad, ' '); begin = s.m_right.find_first_not_of(' ', new_line_pos + 1); if (begin == std::string::npos) { break; // Empty line } new_line_pos = s.m_right.find_first_of('\n', begin + 1); } ret += right; ret += "\n"; } return ret; } }; +// Remove once backport of PR14987 is completed RPCHelpMan::RPCHelpMan(const std::string &name, const std::string &description, const std::vector &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 named_args; for (const auto &arg : m_args) { // Should have unique named arguments CHECK_NONFATAL(named_args.insert(arg.m_name).second); } } +RPCHelpMan::RPCHelpMan(std::string name, std::string description, + std::vector 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 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; // 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(); 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 += "("; if (m_type_str.size() != 0) { ret += m_type_str.at(1); } else { switch (m_type) { case Type::STR_HEX: case Type::STR: { ret += "string"; break; } case Type::NUM: { ret += "numeric"; break; } case Type::AMOUNT: { ret += "numeric or string"; break; } case Type::BOOL: { ret += "boolean"; break; } case Type::OBJ: case Type::OBJ_USER_KEYS: { ret += "json object"; break; } case Type::ARR: { ret += "json array"; break; } // no default case, so the compiler can warn about missing cases } } if (!implicitly_required) { ret += ", "; if (m_optional) { ret += "optional"; if (!m_default_value.empty()) { ret += ", default=" + m_default_value; } else { // TODO enable this assert, when all optional parameters have // their default value documented // assert(false); } } else { ret += "required"; // Default value is ignored, and must not be present assert(m_default_value.empty()); } } ret += ")"; ret += m_description.empty() ? "" : " " + m_description; return ret; } std::string RPCArg::ToStringObj(const bool oneline) const { std::string res; res += "\""; res += m_name; if (oneline) { res += "\":"; } else { res += "\": "; } switch (m_type) { case Type::STR: return res + "\"str\""; case Type::STR_HEX: return res + "\"hex\""; case Type::NUM: return res + "n"; case Type::AMOUNT: return res + "amount"; case Type::BOOL: return res + "bool"; case Type::ARR: res += "["; for (const auto &i : m_inner) { res += i.ToString(oneline) + ","; } return res + "...]"; case Type::OBJ: case Type::OBJ_USER_KEYS: // Currently unused, so avoid writing dead code assert(false); // no default case, so the compiler can warn about missing cases } assert(false); } std::string RPCArg::ToString(const bool oneline) const { if (oneline && !m_oneline_description.empty()) { return m_oneline_description; } switch (m_type) { case Type::STR_HEX: case Type::STR: { return "\"" + m_name + "\""; } case Type::NUM: case Type::AMOUNT: case Type::BOOL: { return m_name; } case Type::OBJ: case Type::OBJ_USER_KEYS: { std::string res; for (size_t i = 0; i < m_inner.size();) { res += m_inner[i].ToStringObj(oneline); if (++i < m_inner.size()) { res += ","; } } if (m_type == Type::OBJ) { return "{" + res + "}"; } else { return "{" + res + ",...}"; } } case Type::ARR: { std::string res; for (const auto &i : m_inner) { res += i.ToString(oneline) + ","; } return "[" + res + "...]"; } // no default case, so the compiler can warn about missing cases } assert(false); } diff --git a/src/rpc/util.h b/src/rpc/util.h index 7155bd719..3de69c6dd 100644 --- a/src/rpc/util.h +++ b/src/rpc/util.h @@ -1,129 +1,177 @@ // Copyright (c) 2017 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_RPC_UTIL_H #define BITCOIN_RPC_UTIL_H #include #include