Branch data Line data Source code
# 1 : : // Copyright (c) 2010 Satoshi Nakamoto
# 2 : : // Copyright (c) 2009-2021 The Bitcoin Core developers
# 3 : : // Distributed under the MIT software license, see the accompanying
# 4 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
# 5 : :
# 6 : : #ifndef BITCOIN_RPC_SERVER_H
# 7 : : #define BITCOIN_RPC_SERVER_H
# 8 : :
# 9 : : #include <rpc/request.h>
# 10 : : #include <rpc/util.h>
# 11 : :
# 12 : : #include <functional>
# 13 : : #include <map>
# 14 : : #include <stdint.h>
# 15 : : #include <string>
# 16 : :
# 17 : : #include <univalue.h>
# 18 : :
# 19 : : static const unsigned int DEFAULT_RPC_SERIALIZE_VERSION = 1;
# 20 : :
# 21 : : class CRPCCommand;
# 22 : :
# 23 : : namespace RPCServer
# 24 : : {
# 25 : : void OnStarted(std::function<void ()> slot);
# 26 : : void OnStopped(std::function<void ()> slot);
# 27 : : }
# 28 : :
# 29 : : /** Query whether RPC is running */
# 30 : : bool IsRPCRunning();
# 31 : :
# 32 : : /** Throw JSONRPCError if RPC is not running */
# 33 : : void RpcInterruptionPoint();
# 34 : :
# 35 : : /**
# 36 : : * Set the RPC warmup status. When this is done, all RPC calls will error out
# 37 : : * immediately with RPC_IN_WARMUP.
# 38 : : */
# 39 : : void SetRPCWarmupStatus(const std::string& newStatus);
# 40 : : /* Mark warmup as done. RPC calls will be processed from now on. */
# 41 : : void SetRPCWarmupFinished();
# 42 : :
# 43 : : /* returns the current warmup state. */
# 44 : : bool RPCIsInWarmup(std::string *outStatus);
# 45 : :
# 46 : : /** Opaque base class for timers returned by NewTimerFunc.
# 47 : : * This provides no methods at the moment, but makes sure that delete
# 48 : : * cleans up the whole state.
# 49 : : */
# 50 : : class RPCTimerBase
# 51 : : {
# 52 : : public:
# 53 : 62 : virtual ~RPCTimerBase() {}
# 54 : : };
# 55 : :
# 56 : : /**
# 57 : : * RPC timer "driver".
# 58 : : */
# 59 : : class RPCTimerInterface
# 60 : : {
# 61 : : public:
# 62 : 787 : virtual ~RPCTimerInterface() {}
# 63 : : /** Implementation name */
# 64 : : virtual const char *Name() = 0;
# 65 : : /** Factory function for timers.
# 66 : : * RPC will call the function to create a timer that will call func in *millis* milliseconds.
# 67 : : * @note As the RPC mechanism is backend-neutral, it can use different implementations of timers.
# 68 : : * This is needed to cope with the case in which there is no HTTP server, but
# 69 : : * only GUI RPC console, and to break the dependency of pcserver on httprpc.
# 70 : : */
# 71 : : virtual RPCTimerBase* NewTimer(std::function<void()>& func, int64_t millis) = 0;
# 72 : : };
# 73 : :
# 74 : : /** Set the factory function for timers */
# 75 : : void RPCSetTimerInterface(RPCTimerInterface *iface);
# 76 : : /** Set the factory function for timer, but only, if unset */
# 77 : : void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface);
# 78 : : /** Unset factory function for timers */
# 79 : : void RPCUnsetTimerInterface(RPCTimerInterface *iface);
# 80 : :
# 81 : : /**
# 82 : : * Run func nSeconds from now.
# 83 : : * Overrides previous timer <name> (if any).
# 84 : : */
# 85 : : void RPCRunLater(const std::string& name, std::function<void()> func, int64_t nSeconds);
# 86 : :
# 87 : : typedef RPCHelpMan (*RpcMethodFnType)();
# 88 : :
# 89 : : class CRPCCommand
# 90 : : {
# 91 : : public:
# 92 : : //! RPC method handler reading request and assigning result. Should return
# 93 : : //! true if request is fully handled, false if it should be passed on to
# 94 : : //! subsequent handlers.
# 95 : : using Actor = std::function<bool(const JSONRPCRequest& request, UniValue& result, bool last_handler)>;
# 96 : :
# 97 : : //! Constructor taking Actor callback supporting multiple handlers.
# 98 : : CRPCCommand(std::string category, std::string name, Actor actor, std::vector<std::string> args, intptr_t unique_id)
# 99 : : : category(std::move(category)), name(std::move(name)), actor(std::move(actor)), argNames(std::move(args)),
# 100 : : unique_id(unique_id)
# 101 : 179581 : {
# 102 : 179581 : }
# 103 : :
# 104 : : //! Simplified constructor taking plain RpcMethodFnType function pointer.
# 105 : : CRPCCommand(std::string category, RpcMethodFnType fn)
# 106 : : : CRPCCommand(
# 107 : : category,
# 108 : : fn().m_name,
# 109 : 138423 : [fn](const JSONRPCRequest& request, UniValue& result, bool) { result = fn().HandleRequest(request); return true; },
# 110 : : fn().GetArgNames(),
# 111 : : intptr_t(fn))
# 112 : 127646 : {
# 113 : 127646 : }
# 114 : :
# 115 : : std::string category;
# 116 : : std::string name;
# 117 : : Actor actor;
# 118 : : std::vector<std::string> argNames;
# 119 : : intptr_t unique_id;
# 120 : : };
# 121 : :
# 122 : : /**
# 123 : : * RPC command dispatcher.
# 124 : : */
# 125 : : class CRPCTable
# 126 : : {
# 127 : : private:
# 128 : : std::map<std::string, std::vector<const CRPCCommand*>> mapCommands;
# 129 : : public:
# 130 : : CRPCTable();
# 131 : : std::string help(const std::string& name, const JSONRPCRequest& helpreq) const;
# 132 : :
# 133 : : /**
# 134 : : * Execute a method.
# 135 : : * @param request The JSONRPCRequest to execute
# 136 : : * @returns Result of the call.
# 137 : : * @throws an exception (UniValue) when an error happens.
# 138 : : */
# 139 : : UniValue execute(const JSONRPCRequest &request) const;
# 140 : :
# 141 : : /**
# 142 : : * Returns a list of registered commands
# 143 : : * @returns List of registered commands.
# 144 : : */
# 145 : : std::vector<std::string> listCommands() const;
# 146 : :
# 147 : : /**
# 148 : : * Return all named arguments that need to be converted by the client from string to another JSON type
# 149 : : */
# 150 : : UniValue dumpArgMap(const JSONRPCRequest& request) const;
# 151 : :
# 152 : : /**
# 153 : : * Appends a CRPCCommand to the dispatch table.
# 154 : : *
# 155 : : * Precondition: RPC server is not running
# 156 : : *
# 157 : : * Commands with different method names but the same unique_id will
# 158 : : * be considered aliases, and only the first registered method name will
# 159 : : * show up in the help text command listing. Aliased commands do not have
# 160 : : * to have the same behavior. Server and client code can distinguish
# 161 : : * between calls based on method name, and aliased commands can also
# 162 : : * register different names, types, and numbers of parameters.
# 163 : : */
# 164 : : void appendCommand(const std::string& name, const CRPCCommand* pcmd);
# 165 : : bool removeCommand(const std::string& name, const CRPCCommand* pcmd);
# 166 : : };
# 167 : :
# 168 : : bool IsDeprecatedRPCEnabled(const std::string& method);
# 169 : :
# 170 : : extern CRPCTable tableRPC;
# 171 : :
# 172 : : void StartRPC();
# 173 : : void InterruptRPC();
# 174 : : void StopRPC();
# 175 : : std::string JSONRPCExecBatch(const JSONRPCRequest& jreq, const UniValue& vReq);
# 176 : :
# 177 : : // Retrieves any serialization flags requested in command line argument
# 178 : : int RPCSerializationFlags();
# 179 : :
# 180 : : #endif // BITCOIN_RPC_SERVER_H
|