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.8KB

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