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.

rpcserver.h 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Copyright (c) 2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2014 The Bitcoin developers
  3. // Distributed under the MIT/X11 software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #ifndef _BITCOINRPC_SERVER_H_
  6. #define _BITCOINRPC_SERVER_H_ 1
  7. #include "uint256.h"
  8. #include "rpcprotocol.h"
  9. #include <list>
  10. #include <map>
  11. #include <stdint.h>
  12. #include <string>
  13. #include "json/json_spirit_reader_template.h"
  14. #include "json/json_spirit_utils.h"
  15. #include "json/json_spirit_writer_template.h"
  16. class CBlockIndex;
  17. class CNetAddr;
  18. class AcceptedConnection
  19. {
  20. public:
  21. virtual ~AcceptedConnection() {}
  22. virtual std::iostream& stream() = 0;
  23. virtual std::string peer_address_to_string() const = 0;
  24. virtual void close() = 0;
  25. };
  26. /* Start RPC threads */
  27. void StartRPCThreads();
  28. /* Alternative to StartRPCThreads for the GUI, when no server is
  29. * used. The RPC thread in this case is only used to handle timeouts.
  30. * If real RPC threads have already been started this is a no-op.
  31. */
  32. void StartDummyRPCThread();
  33. /* Stop RPC threads */
  34. void StopRPCThreads();
  35. /* Query whether RPC is running */
  36. bool IsRPCRunning();
  37. /*
  38. Type-check arguments; throws JSONRPCError if wrong type given. Does not check that
  39. the right number of arguments are passed, just that any passed are the correct type.
  40. Use like: RPCTypeCheck(params, boost::assign::list_of(str_type)(int_type)(obj_type));
  41. */
  42. void RPCTypeCheck(const json_spirit::Array& params,
  43. const std::list<json_spirit::Value_type>& typesExpected, bool fAllowNull=false);
  44. /*
  45. Check for expected keys/value types in an Object.
  46. Use like: RPCTypeCheck(object, boost::assign::map_list_of("name", str_type)("value", int_type));
  47. */
  48. void RPCTypeCheck(const json_spirit::Object& o,
  49. const std::map<std::string, json_spirit::Value_type>& typesExpected, bool fAllowNull=false);
  50. /*
  51. Run func nSeconds from now. Uses boost deadline timers.
  52. Overrides previous timer <name> (if any).
  53. */
  54. void RPCRunLater(const std::string& name, boost::function<void(void)> func, int64_t nSeconds);
  55. //! Convert boost::asio address to CNetAddr
  56. extern CNetAddr BoostAsioToCNetAddr(boost::asio::ip::address address);
  57. typedef json_spirit::Value(*rpcfn_type)(const json_spirit::Array& params, bool fHelp);
  58. class CRPCCommand
  59. {
  60. public:
  61. std::string name;
  62. rpcfn_type actor;
  63. bool okSafeMode;
  64. bool threadSafe;
  65. bool reqWallet;
  66. };
  67. /**
  68. * Bitcoin RPC command dispatcher.
  69. */
  70. class CRPCTable
  71. {
  72. private:
  73. std::map<std::string, const CRPCCommand*> mapCommands;
  74. public:
  75. CRPCTable();
  76. const CRPCCommand* operator[](std::string name) const;
  77. std::string help(std::string name) const;
  78. /**
  79. * Execute a method.
  80. * @param method Method to execute
  81. * @param params Array of arguments (JSON objects)
  82. * @returns Result of the call.
  83. * @throws an exception (json_spirit::Value) when an error happens.
  84. */
  85. json_spirit::Value execute(const std::string &method, const json_spirit::Array &params) const;
  86. };
  87. extern const CRPCTable tableRPC;
  88. //
  89. // Utilities: convert hex-encoded Values
  90. // (throws error if not hex).
  91. //
  92. extern uint256 ParseHashV(const json_spirit::Value& v, std::string strName);
  93. extern uint256 ParseHashO(const json_spirit::Object& o, std::string strKey);
  94. extern std::vector<unsigned char> ParseHexV(const json_spirit::Value& v, std::string strName);
  95. extern std::vector<unsigned char> ParseHexO(const json_spirit::Object& o, std::string strKey);
  96. extern void InitRPCMining();
  97. extern void ShutdownRPCMining();
  98. extern int64_t nWalletUnlockTime;
  99. extern int64_t AmountFromValue(const json_spirit::Value& value);
  100. extern json_spirit::Value ValueFromAmount(int64_t amount);
  101. extern double GetDifficulty(const CBlockIndex* blockindex = NULL);
  102. extern std::string HelpRequiringPassphrase();
  103. extern std::string HelpExampleCli(std::string methodname, std::string args);
  104. extern std::string HelpExampleRpc(std::string methodname, std::string args);
  105. extern void EnsureWalletIsUnlocked();
  106. extern json_spirit::Value getconnectioncount(const json_spirit::Array& params, bool fHelp); // in rpcnet.cpp
  107. extern json_spirit::Value getpeerinfo(const json_spirit::Array& params, bool fHelp);
  108. extern json_spirit::Value ping(const json_spirit::Array& params, bool fHelp);
  109. extern json_spirit::Value addnode(const json_spirit::Array& params, bool fHelp);
  110. extern json_spirit::Value getaddednodeinfo(const json_spirit::Array& params, bool fHelp);
  111. extern json_spirit::Value getnettotals(const json_spirit::Array& params, bool fHelp);
  112. extern json_spirit::Value dumpprivkey(const json_spirit::Array& params, bool fHelp); // in rpcdump.cpp
  113. extern json_spirit::Value importprivkey(const json_spirit::Array& params, bool fHelp);
  114. extern json_spirit::Value importaddress(const json_spirit::Array& params, bool fHelp);
  115. extern json_spirit::Value dumpwallet(const json_spirit::Array& params, bool fHelp);
  116. extern json_spirit::Value importwallet(const json_spirit::Array& params, bool fHelp);
  117. extern json_spirit::Value getgenerate(const json_spirit::Array& params, bool fHelp); // in rpcmining.cpp
  118. extern json_spirit::Value setgenerate(const json_spirit::Array& params, bool fHelp);
  119. extern json_spirit::Value getnetworkhashps(const json_spirit::Array& params, bool fHelp);
  120. extern json_spirit::Value gethashespersec(const json_spirit::Array& params, bool fHelp);
  121. extern json_spirit::Value getmininginfo(const json_spirit::Array& params, bool fHelp);
  122. extern json_spirit::Value prioritisetransaction(const json_spirit::Array& params, bool fHelp);
  123. extern json_spirit::Value getblocktemplate(const json_spirit::Array& params, bool fHelp);
  124. extern json_spirit::Value submitblock(const json_spirit::Array& params, bool fHelp);
  125. extern json_spirit::Value estimatefee(const json_spirit::Array& params, bool fHelp);
  126. extern json_spirit::Value estimatepriority(const json_spirit::Array& params, bool fHelp);
  127. extern json_spirit::Value getnewaddress(const json_spirit::Array& params, bool fHelp); // in rpcwallet.cpp
  128. extern json_spirit::Value getaccountaddress(const json_spirit::Array& params, bool fHelp);
  129. extern json_spirit::Value getrawchangeaddress(const json_spirit::Array& params, bool fHelp);
  130. extern json_spirit::Value setaccount(const json_spirit::Array& params, bool fHelp);
  131. extern json_spirit::Value getaccount(const json_spirit::Array& params, bool fHelp);
  132. extern json_spirit::Value getaddressesbyaccount(const json_spirit::Array& params, bool fHelp);
  133. extern json_spirit::Value sendtoaddress(const json_spirit::Array& params, bool fHelp);
  134. extern json_spirit::Value signmessage(const json_spirit::Array& params, bool fHelp);
  135. extern json_spirit::Value verifymessage(const json_spirit::Array& params, bool fHelp);
  136. extern json_spirit::Value getreceivedbyaddress(const json_spirit::Array& params, bool fHelp);
  137. extern json_spirit::Value getreceivedbyaccount(const json_spirit::Array& params, bool fHelp);
  138. extern json_spirit::Value getbalance(const json_spirit::Array& params, bool fHelp);
  139. extern json_spirit::Value getunconfirmedbalance(const json_spirit::Array& params, bool fHelp);
  140. extern json_spirit::Value movecmd(const json_spirit::Array& params, bool fHelp);
  141. extern json_spirit::Value sendfrom(const json_spirit::Array& params, bool fHelp);
  142. extern json_spirit::Value sendmany(const json_spirit::Array& params, bool fHelp);
  143. extern json_spirit::Value addmultisigaddress(const json_spirit::Array& params, bool fHelp);
  144. extern json_spirit::Value createmultisig(const json_spirit::Array& params, bool fHelp);
  145. extern json_spirit::Value listreceivedbyaddress(const json_spirit::Array& params, bool fHelp);
  146. extern json_spirit::Value listreceivedbyaccount(const json_spirit::Array& params, bool fHelp);
  147. extern json_spirit::Value listtransactions(const json_spirit::Array& params, bool fHelp);
  148. extern json_spirit::Value listaddressgroupings(const json_spirit::Array& params, bool fHelp);
  149. extern json_spirit::Value listaccounts(const json_spirit::Array& params, bool fHelp);
  150. extern json_spirit::Value listsinceblock(const json_spirit::Array& params, bool fHelp);
  151. extern json_spirit::Value gettransaction(const json_spirit::Array& params, bool fHelp);
  152. extern json_spirit::Value backupwallet(const json_spirit::Array& params, bool fHelp);
  153. extern json_spirit::Value keypoolrefill(const json_spirit::Array& params, bool fHelp);
  154. extern json_spirit::Value walletpassphrase(const json_spirit::Array& params, bool fHelp);
  155. extern json_spirit::Value walletpassphrasechange(const json_spirit::Array& params, bool fHelp);
  156. extern json_spirit::Value walletlock(const json_spirit::Array& params, bool fHelp);
  157. extern json_spirit::Value encryptwallet(const json_spirit::Array& params, bool fHelp);
  158. extern json_spirit::Value validateaddress(const json_spirit::Array& params, bool fHelp);
  159. extern json_spirit::Value getinfo(const json_spirit::Array& params, bool fHelp);
  160. extern json_spirit::Value getwalletinfo(const json_spirit::Array& params, bool fHelp);
  161. extern json_spirit::Value getblockchaininfo(const json_spirit::Array& params, bool fHelp);
  162. extern json_spirit::Value getnetworkinfo(const json_spirit::Array& params, bool fHelp);
  163. extern json_spirit::Value getrawtransaction(const json_spirit::Array& params, bool fHelp); // in rcprawtransaction.cpp
  164. extern json_spirit::Value listunspent(const json_spirit::Array& params, bool fHelp);
  165. extern json_spirit::Value lockunspent(const json_spirit::Array& params, bool fHelp);
  166. extern json_spirit::Value listlockunspent(const json_spirit::Array& params, bool fHelp);
  167. extern json_spirit::Value createrawtransaction(const json_spirit::Array& params, bool fHelp);
  168. extern json_spirit::Value decoderawtransaction(const json_spirit::Array& params, bool fHelp);
  169. extern json_spirit::Value decodescript(const json_spirit::Array& params, bool fHelp);
  170. extern json_spirit::Value signrawtransaction(const json_spirit::Array& params, bool fHelp);
  171. extern json_spirit::Value sendrawtransaction(const json_spirit::Array& params, bool fHelp);
  172. extern json_spirit::Value getblockcount(const json_spirit::Array& params, bool fHelp); // in rpcblockchain.cpp
  173. extern json_spirit::Value getbestblockhash(const json_spirit::Array& params, bool fHelp);
  174. extern json_spirit::Value getdifficulty(const json_spirit::Array& params, bool fHelp);
  175. extern json_spirit::Value settxfee(const json_spirit::Array& params, bool fHelp);
  176. extern json_spirit::Value getrawmempool(const json_spirit::Array& params, bool fHelp);
  177. extern json_spirit::Value getblockhash(const json_spirit::Array& params, bool fHelp);
  178. extern json_spirit::Value getblock(const json_spirit::Array& params, bool fHelp);
  179. extern json_spirit::Value gettxoutsetinfo(const json_spirit::Array& params, bool fHelp);
  180. extern json_spirit::Value gettxout(const json_spirit::Array& params, bool fHelp);
  181. extern json_spirit::Value verifychain(const json_spirit::Array& params, bool fHelp);
  182. #endif