Line data Source code
1 : // Copyright (c) 2010 Satoshi Nakamoto 2 : // Copyright (c) 2009-2014 The Bitcoin developers 3 : // Copyright (c) 2015-2022 The PIVX Core developers 4 : // Distributed under the MIT software license, see the accompanying 5 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 6 : 7 : #ifndef PIVX_RPC_SERVER_H 8 : #define PIVX_RPC_SERVER_H 9 : 10 : #include "amount.h" 11 : #include "rpc/protocol.h" 12 : #include "uint256.h" 13 : 14 : #include <list> 15 : #include <map> 16 : #include <stdint.h> 17 : #include <string> 18 : 19 : #include <univalue.h> 20 : 21 : class CRPCCommand; 22 : 23 : namespace RPCServer 24 : { 25 : void OnStarted(std::function<void ()> slot); 26 : void OnStopped(std::function<void ()> slot); 27 : void OnPreCommand(std::function<void (const CRPCCommand&)> slot); 28 : } 29 : 30 : class CBlockIndex; 31 : class CNetAddr; 32 : 33 : /** Wrapper for UniValue::VType, which includes typeAny: 34 : * Used to denote don't care type. Only used by RPCTypeCheckObj */ 35 : struct UniValueType { 36 254 : UniValueType(UniValue::VType _type) : typeAny(false), type(_type) {} 37 44 : UniValueType() : typeAny(true) {} 38 : bool typeAny; 39 : UniValue::VType type{UniValue::VNULL}; 40 : }; 41 : 42 : class JSONRPCRequest 43 : { 44 : public: 45 : UniValue id; 46 : std::string strMethod; 47 : UniValue params; 48 : bool fHelp; 49 : std::string URI; 50 : std::string authUser; 51 : 52 300307 : JSONRPCRequest() { id = NullUniValue; params = NullUniValue; fHelp = false; } 53 : void parse(const UniValue& valRequest); 54 : }; 55 : 56 : /** Query whether RPC is running */ 57 : bool IsRPCRunning(); 58 : 59 : /** 60 : * Set the RPC warmup status. When this is done, all RPC calls will error out 61 : * immediately with RPC_IN_WARMUP. 62 : */ 63 : void SetRPCWarmupStatus(const std::string& newStatus); 64 : /* Mark warmup as done. RPC calls will be processed from now on. */ 65 : void SetRPCWarmupFinished(); 66 : 67 : /* returns the current warmup state. */ 68 : bool RPCIsInWarmup(std::string* statusOut); 69 : 70 : /** 71 : * Type-check arguments; throws JSONRPCError if wrong type given. Does not check that 72 : * the right number of arguments are passed, just that any passed are the correct type. 73 : */ 74 : void RPCTypeCheck(const UniValue& params, 75 : const std::list<UniValue::VType>& typesExpected, bool fAllowNull=false); 76 : 77 : /** 78 : * Type-check one argument; throws JSONRPCError if wrong type given. 79 : */ 80 : void RPCTypeCheckArgument(const UniValue& value, const UniValueType& typeExpected); 81 : 82 : /** 83 : * Check for expected keys/value types in an Object. 84 : */ 85 : void RPCTypeCheckObj(const UniValue& o, 86 : const std::map<std::string, UniValueType>& typesExpected, 87 : bool fAllowNull=false, 88 : bool fStrict=false); 89 : 90 : /** Opaque base class for timers returned by NewTimerFunc. 91 : * This provides no methods at the moment, but makes sure that delete 92 : * cleans up the whole state. 93 : */ 94 14 : class RPCTimerBase 95 : { 96 : public: 97 : virtual ~RPCTimerBase() {} 98 : }; 99 : 100 : /** 101 : * RPC timer "driver". 102 : */ 103 375 : class RPCTimerInterface 104 : { 105 : public: 106 : virtual ~RPCTimerInterface() {} 107 : /** Implementation name */ 108 : virtual const char *Name() = 0; 109 : /** Factory function for timers. 110 : * RPC will call the function to create a timer that will call func in *millis* milliseconds. 111 : * @note As the RPC mechanism is backend-neutral, it can use different implementations of timers. 112 : * This is needed to cope with the case in which there is no HTTP server, but 113 : * only GUI RPC console, and to break the dependency of pcserver on httprpc. 114 : */ 115 : virtual RPCTimerBase* NewTimer(std::function<void(void)>& func, int64_t millis) = 0; 116 : }; 117 : 118 : /** Set factory function for timers */ 119 : void RPCSetTimerInterface(RPCTimerInterface *iface); 120 : /** Set factory function for timers, but only if unset */ 121 : void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface); 122 : /** Unset factory function for timers */ 123 : void RPCUnsetTimerInterface(RPCTimerInterface *iface); 124 : 125 : /** 126 : * Run func nSeconds from now. 127 : * Overrides previous timer <name> (if any). 128 : */ 129 : void RPCRunLater(const std::string& name, std::function<void(void)> func, int64_t nSeconds); 130 : 131 : typedef UniValue(*rpcfn_type)(const JSONRPCRequest& jsonRequest); 132 : 133 : class CRPCCommand 134 : { 135 : public: 136 : std::string category; 137 : std::string name; 138 : rpcfn_type actor; 139 : bool okSafeMode; 140 : std::vector<std::string> argNames; 141 : }; 142 : 143 : /** 144 : * PIVX RPC command dispatcher. 145 : */ 146 : class CRPCTable 147 : { 148 : private: 149 : std::map<std::string, const CRPCCommand*> mapCommands; 150 : 151 : public: 152 : CRPCTable(); 153 : const CRPCCommand* operator[](const std::string& name) const; 154 : std::string help(const std::string& name, const JSONRPCRequest& helpreq) const; 155 : 156 : /** 157 : * Execute a method. 158 : * @param request The JSONRPCRequest to execute 159 : * @returns Result of the call. 160 : * @throws an exception (UniValue) when an error happens. 161 : */ 162 : UniValue execute(const JSONRPCRequest &request) const; 163 : 164 : /** 165 : * Returns a list of registered commands 166 : * @returns List of registered commands. 167 : */ 168 : std::vector<std::string> listCommands() const; 169 : 170 : /** 171 : * Appends a CRPCCommand to the dispatch table. 172 : * Returns false if RPC server is already running (dump concurrency protection). 173 : * Commands cannot be overwritten (returns false). 174 : */ 175 : bool appendCommand(const std::string& name, const CRPCCommand* pcmd); 176 : }; 177 : 178 : bool IsDeprecatedRPCEnabled(const std::string& method); 179 : 180 : extern CRPCTable tableRPC; 181 : 182 : /** 183 : * Utilities: convert hex-encoded Values 184 : * (throws error if not hex). 185 : */ 186 : extern uint256 ParseHashV(const UniValue& v, std::string strName); 187 : extern uint256 ParseHashO(const UniValue& o, std::string strKey); 188 : extern std::vector<unsigned char> ParseHexV(const UniValue& v, std::string strName); 189 : extern std::vector<unsigned char> ParseHexO(const UniValue& o, std::string strKey); 190 : extern int ParseInt(const UniValue& o, std::string strKey); 191 : extern bool ParseBool(const UniValue& o, std::string strKey); 192 : extern double ParseDoubleV(const UniValue& v, const std::string &strName); 193 : 194 : extern CAmount AmountFromValue(const UniValue& value); 195 : extern UniValue ValueFromAmount(const CAmount& amount); 196 : extern double GetDifficulty(const CBlockIndex* blockindex = nullptr); 197 : extern std::string HelpExampleCli(std::string methodname, std::string args); 198 : extern std::string HelpExampleRpc(std::string methodname, std::string args); 199 : 200 : bool StartRPC(); 201 : void InterruptRPC(); 202 : void StopRPC(); 203 : std::string JSONRPCExecBatch(const UniValue& vReq); 204 : void RPCNotifyBlockChange(bool fInitialDownload, const CBlockIndex* pindex); 205 : 206 : #endif // PIVX_RPC_SERVER_H