Changeset View
Changeset View
Standalone View
Standalone View
src/rpc/util.h
// Copyright (c) 2017 The Bitcoin Core developers | // Copyright (c) 2017 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#ifndef BITCOIN_RPC_UTIL_H | #ifndef BITCOIN_RPC_UTIL_H | ||||
#define BITCOIN_RPC_UTIL_H | #define BITCOIN_RPC_UTIL_H | ||||
#include <node/transaction.h> | #include <node/transaction.h> | ||||
#include <rpc/protocol.h> | #include <rpc/protocol.h> | ||||
#include <script/standard.h> // For CTxDestination | #include <script/standard.h> // For CTxDestination | ||||
#include <univalue.h> | #include <univalue.h> | ||||
#include <util/check.h> | #include <util/check.h> | ||||
#include <boost/variant.hpp> | |||||
#include <string> | #include <string> | ||||
#include <vector> | #include <vector> | ||||
class CChainParams; | class CChainParams; | ||||
class CKeyStore; | class CKeyStore; | ||||
class CPubKey; | class CPubKey; | ||||
class CScript; | class CScript; | ||||
struct NodeContext; | struct NodeContext; | ||||
▲ Show 20 Lines • Show All 75 Lines • ▼ Show 20 Lines | enum class Type { | ||||
//! keys are predefined | //! keys are predefined | ||||
OBJ_USER_KEYS, | OBJ_USER_KEYS, | ||||
//! Special type representing a floating point amount (can be either NUM | //! Special type representing a floating point amount (can be either NUM | ||||
//! or STR) | //! or STR) | ||||
AMOUNT, | AMOUNT, | ||||
//! Special type that is a STR with only hex chars | //! Special type that is a STR with only hex chars | ||||
STR_HEX, | STR_HEX, | ||||
}; | }; | ||||
enum class Optional { | |||||
/** Required arg */ | |||||
NO, | |||||
/** | |||||
* Optinal arg that is a named argument and has a default value of | |||||
* `null`. When possible, the default value should be specified. | |||||
*/ | |||||
OMITTED_NAMED_ARG, | |||||
/** | |||||
* Optional argument with default value omitted because they are | |||||
* implicitly clear. That is, elements in an array or object may not | |||||
* exist by default. | |||||
* When possible, the default value should be specified. | |||||
*/ | |||||
OMITTED, | |||||
}; | |||||
using Fallback = | |||||
boost::variant<Optional, | |||||
/* default value for optional args */ std::string>; | |||||
//! The name of the arg (can be empty for inner args) | //! The name of the arg (can be empty for inner args) | ||||
const std::string m_name; | const std::string m_name; | ||||
const Type m_type; | const Type m_type; | ||||
//! Only used for arrays or dicts | //! Only used for arrays or dicts | ||||
const std::vector<RPCArg> m_inner; | const std::vector<RPCArg> m_inner; | ||||
const bool m_optional; | const Fallback m_fallback; | ||||
const std::string m_default_value; //!< Only used for optional args | |||||
const std::string m_description; | const std::string m_description; | ||||
//! Should be empty unless it is supposed to override the auto-generated | //! Should be empty unless it is supposed to override the auto-generated | ||||
//! summary line | //! summary line | ||||
const std::string m_oneline_description; | const std::string m_oneline_description; | ||||
//! Should be empty unless it is supposed to override the | //! Should be empty unless it is supposed to override the | ||||
//! auto-generated type strings. Vector length is either 0 | //! auto-generated type strings. Vector length is either 0 | ||||
//! or 2, m_type_str.at(0) will override the type of the | //! or 2, m_type_str.at(0) will override the type of the | ||||
//! value in a key-value pair, m_type_str.at(1) will | //! value in a key-value pair, m_type_str.at(1) will | ||||
//! override the type in the argument description. | //! override the type in the argument description. | ||||
const std::vector<std::string> m_type_str; | const std::vector<std::string> m_type_str; | ||||
RPCArg(const std::string &name, const Type &type, const bool opt, | RPCArg(const std::string &name, const Type &type, const Fallback &fallback, | ||||
const std::string &default_val, const std::string &description, | const std::string &description, | ||||
const std::string &oneline_description = "", | const std::string &oneline_description = "", | ||||
const std::vector<std::string> &type_str = {}) | const std::vector<std::string> &type_str = {}) | ||||
: m_name{name}, m_type{type}, m_optional{opt}, | : m_name{name}, m_type{type}, m_fallback{fallback}, | ||||
m_default_value{default_val}, m_description{description}, | m_description{description}, | ||||
m_oneline_description{oneline_description}, m_type_str{type_str} { | m_oneline_description{oneline_description}, m_type_str{type_str} { | ||||
CHECK_NONFATAL(type != Type::ARR && type != Type::OBJ); | CHECK_NONFATAL(type != Type::ARR && type != Type::OBJ); | ||||
} | } | ||||
RPCArg(const std::string &name, const Type &type, const bool opt, | RPCArg(const std::string &name, const Type &type, const Fallback &fallback, | ||||
const std::string &default_val, const std::string &description, | const std::string &description, const std::vector<RPCArg> &inner, | ||||
const std::vector<RPCArg> &inner, | |||||
const std::string &oneline_description = "", | const std::string &oneline_description = "", | ||||
const std::vector<std::string> &type_str = {}) | const std::vector<std::string> &type_str = {}) | ||||
: m_name{name}, m_type{type}, m_inner{inner}, m_optional{opt}, | : m_name{name}, m_type{type}, m_inner{inner}, m_fallback{fallback}, | ||||
m_default_value{default_val}, m_description{description}, | m_description{description}, | ||||
m_oneline_description{oneline_description}, m_type_str{type_str} { | m_oneline_description{oneline_description}, m_type_str{type_str} { | ||||
CHECK_NONFATAL(type == Type::ARR || type == Type::OBJ); | CHECK_NONFATAL(type == Type::ARR || type == Type::OBJ); | ||||
} | } | ||||
/** | /** | ||||
* Return the type string of the argument. | * Return the type string of the argument. | ||||
* Set oneline to allow it to be overridden by a custom oneline type string | * Set oneline to allow it to be overridden by a custom oneline type string | ||||
* (m_oneline_description). | * (m_oneline_description). | ||||
*/ | */ | ||||
std::string ToString(bool oneline) const; | std::string ToString(bool oneline) const; | ||||
/** | /** | ||||
* Return the type string of the argument when it is in an object (dict). | * Return the type string of the argument when it is in an object (dict). | ||||
* Set oneline to get the oneline representation (less whitespace) | * Set oneline to get the oneline representation (less whitespace) | ||||
*/ | */ | ||||
std::string ToStringObj(bool oneline) const; | std::string ToStringObj(bool oneline) const; | ||||
/** | /** | ||||
* Return the description string, including the argument type and whether | * Return the description string, including the argument type and whether | ||||
* the argument is required. | * the argument is required. | ||||
* implicitly_required is set for arguments in an array, which are neither | |||||
* optional nor required. | |||||
*/ | */ | ||||
std::string ToDescriptionString(bool implicitly_required = false) const; | std::string ToDescriptionString() const; | ||||
}; | }; | ||||
struct RPCResult { | struct RPCResult { | ||||
const std::string m_cond; | const std::string m_cond; | ||||
const std::string m_result; | const std::string m_result; | ||||
explicit RPCResult(std::string result) | explicit RPCResult(std::string result) | ||||
: m_cond{}, m_result{std::move(result)} { | : m_cond{}, m_result{std::move(result)} { | ||||
▲ Show 20 Lines • Show All 49 Lines • Show Last 20 Lines |