You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

server.h 5.9KB

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