diff --git a/src/rpc/server.h b/src/rpc/server.h index 4330e74f7..3a453c30f 100644 --- a/src/rpc/server.h +++ b/src/rpc/server.h @@ -1,265 +1,265 @@ // Copyright (c) 2010 Satoshi Nakamoto // Copyright (c) 2009-2016 The Bitcoin Core developers // Copyright (c) 2017-2018 The Bitcoin developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#ifndef BITCOIN_RPCSERVER_H -#define BITCOIN_RPCSERVER_H +#ifndef BITCOIN_RPC_SERVER_H +#define BITCOIN_RPC_SERVER_H #include "amount.h" #include "rpc/jsonrpcrequest.h" #include "rpc/protocol.h" #include "uint256.h" #include #include #include #include #include #include static const unsigned int DEFAULT_RPC_SERIALIZE_VERSION = 1; class ContextFreeRPCCommand; namespace RPCServerSignals { void OnStarted(std::function slot); void OnStopped(std::function slot); void OnPreCommand(std::function slot); void OnPostCommand(std::function slot); } // namespace RPCServerSignals class CBlockIndex; class Config; class CNetAddr; /** * Wrapper for UniValue::VType, which includes typeAny: used to denote don't * care type. Only used by RPCTypeCheckObj. */ struct UniValueType { UniValueType(UniValue::VType _type) : typeAny(false), type(_type) {} UniValueType() : typeAny(true) {} bool typeAny; UniValue::VType type; }; /** * Query whether RPC is running */ bool IsRPCRunning(); /** * Set the RPC warmup status. When this is done, all RPC calls will error out * immediately with RPC_IN_WARMUP. */ void SetRPCWarmupStatus(const std::string &newStatus); /** * Mark warmup as done. RPC calls will be processed from now on. */ void SetRPCWarmupFinished(); /** * Returns the current warmup state */ bool RPCIsInWarmup(std::string *statusOut); /** * Type-check arguments; throws JSONRPCError if wrong type given. Does not check * that the right number of arguments are passed, just that any passed are the * correct type. */ void RPCTypeCheck(const UniValue ¶ms, const std::list &typesExpected, bool fAllowNull = false); /** * Type-check one argument; throws JSONRPCError if wrong type given. */ void RPCTypeCheckArgument(const UniValue &value, UniValue::VType typeExpected); /** * Check for expected keys/value types in an Object. */ void RPCTypeCheckObj(const UniValue &o, const std::map &typesExpected, bool fAllowNull = false, bool fStrict = false); /** * Opaque base class for timers returned by NewTimerFunc. * This provides no methods at the moment, but makes sure that delete cleans up * the whole state. */ class RPCTimerBase { public: virtual ~RPCTimerBase() {} }; /** * RPC timer "driver". */ class RPCTimerInterface { public: virtual ~RPCTimerInterface() {} /** * Implementation name */ virtual const char *Name() = 0; /** * Factory function for timers. * RPC will call the function to create a timer that will call func in * *millis* milliseconds. * @note As the RPC mechanism is backend-neutral, it can use different * implementations of timers. * This is needed to cope with the case in which there is no HTTP server, * but only GUI RPC console, and to break the dependency of pcserver on * httprpc. */ virtual RPCTimerBase *NewTimer(std::function &func, int64_t millis) = 0; }; /** * Set the factory function for timers */ void RPCSetTimerInterface(RPCTimerInterface *iface); /** * Set the factory function for timer, but only, if unset */ void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface); /** * Unset factory function for timers */ void RPCUnsetTimerInterface(RPCTimerInterface *iface); /** * Run func nSeconds from now. * Overrides previous timer (if any). */ void RPCRunLater(const std::string &name, std::function func, int64_t nSeconds); typedef UniValue (*rpcfn_type)(Config &config, const JSONRPCRequest &jsonRequest); typedef UniValue (*const_rpcfn_type)(const Config &config, const JSONRPCRequest &jsonRequest); class ContextFreeRPCCommand { public: std::string category; std::string name; bool okSafeMode; private: union { rpcfn_type fn; const_rpcfn_type cfn; } actor; bool useConstConfig; public: std::vector argNames; ContextFreeRPCCommand(std::string _category, std::string _name, rpcfn_type _actor, bool _okSafeMode, std::vector _argNames) : category{std::move(_category)}, name{std::move(_name)}, okSafeMode{_okSafeMode}, useConstConfig{false}, argNames{std::move( _argNames)} { actor.fn = _actor; } /** * There are 2 constructors depending Config is const or not, so we * can call the command through the proper pointer. Casting constness * on parameters of function is undefined behavior. */ ContextFreeRPCCommand(std::string _category, std::string _name, const_rpcfn_type _actor, bool _okSafeMode, std::vector _argNames) : category{std::move(_category)}, name{std::move(_name)}, okSafeMode{_okSafeMode}, useConstConfig{true}, argNames{std::move( _argNames)} { actor.cfn = _actor; } UniValue call(Config &config, const JSONRPCRequest &jsonRequest) const { return useConstConfig ? (*actor.cfn)(config, jsonRequest) : (*actor.fn)(config, jsonRequest); }; }; /** * Bitcoin RPC command dispatcher. */ class CRPCTable { private: std::map mapCommands; public: CRPCTable(); const ContextFreeRPCCommand *operator[](const std::string &name) const; std::string help(Config &config, const std::string &name, const JSONRPCRequest &helpreq) const; /** * Execute a method. * @param request The JSONRPCRequest to execute * @returns Result of the call. * @throws an exception (UniValue) when an error happens. */ UniValue execute(Config &config, const JSONRPCRequest &request) const; /** * Returns a list of registered commands * @returns List of registered commands. */ std::vector listCommands() const; /** * Appends a ContextFreeRPCCommand to the dispatch table. * Returns false if RPC server is already running (dump concurrency * protection). * Commands cannot be overwritten (returns false). */ bool appendCommand(const std::string &name, const ContextFreeRPCCommand *pcmd); }; extern CRPCTable tableRPC; /** * Utilities: convert hex-encoded values (throws error if not hex). */ extern uint256 ParseHashV(const UniValue &v, std::string strName); extern uint256 ParseHashO(const UniValue &o, std::string strKey); extern std::vector ParseHexV(const UniValue &v, std::string strName); extern std::vector ParseHexO(const UniValue &o, std::string strKey); extern Amount AmountFromValue(const UniValue &value); extern UniValue ValueFromAmount(const Amount amount); extern std::string HelpExampleCli(const std::string &methodname, const std::string &args); extern std::string HelpExampleRpc(const std::string &methodname, const std::string &args); bool StartRPC(); void InterruptRPC(); void StopRPC(); std::string JSONRPCExecBatch(Config &config, const JSONRPCRequest &req, const UniValue &vReq); void RPCNotifyBlockChange(bool ibd, const CBlockIndex *); /** * Retrieves any serialization flags requested in command line argument */ int RPCSerializationFlags(); -#endif // BITCOIN_RPCSERVER_H +#endif // BITCOIN_RPC_SERVER_H