1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 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.
6 #ifndef BITCOIN_RPCSERVER_H
7 #define BITCOIN_RPCSERVER_H
10 #include "rpcprotocol.h"
18 #include "json/json_spirit_reader_template.h"
19 #include "json/json_spirit_utils.h"
20 #include "json/json_spirit_writer_template.h"
26 void OnStarted(boost::function<void ()> slot);
27 void OnStopped(boost::function<void ()> slot);
28 void OnPreCommand(boost::function<void (const CRPCCommand&)> slot);
29 void OnPostCommand(boost::function<void (const CRPCCommand&)> slot);
35 class AcceptedConnection
38 virtual ~AcceptedConnection() {}
40 virtual std::iostream& stream() = 0;
41 virtual std::string peer_address_to_string() const = 0;
42 virtual void close() = 0;
45 /** Start RPC threads */
46 void StartRPCThreads();
48 * Alternative to StartRPCThreads for the GUI, when no server is
49 * used. The RPC thread in this case is only used to handle timeouts.
50 * If real RPC threads have already been started this is a no-op.
52 void StartDummyRPCThread();
53 /** Stop RPC threads */
54 void StopRPCThreads();
55 /** Query whether RPC is running */
59 * Set the RPC warmup status. When this is done, all RPC calls will error out
60 * immediately with RPC_IN_WARMUP.
62 void SetRPCWarmupStatus(const std::string& newStatus);
63 /* Mark warmup as done. RPC calls will be processed from now on. */
64 void SetRPCWarmupFinished();
66 /* returns the current warmup state. */
67 bool RPCIsInWarmup(std::string *statusOut);
70 * Type-check arguments; throws JSONRPCError if wrong type given. Does not check that
71 * the right number of arguments are passed, just that any passed are the correct type.
72 * Use like: RPCTypeCheck(params, boost::assign::list_of(str_type)(int_type)(obj_type));
74 void RPCTypeCheck(const json_spirit::Array& params,
75 const std::list<json_spirit::Value_type>& typesExpected, bool fAllowNull=false);
77 * Check for expected keys/value types in an Object.
78 * Use like: RPCTypeCheck(object, boost::assign::map_list_of("name", str_type)("value", int_type));
80 void RPCTypeCheck(const json_spirit::Object& o,
81 const std::map<std::string, json_spirit::Value_type>& typesExpected, bool fAllowNull=false);
84 * Run func nSeconds from now. Uses boost deadline timers.
85 * Overrides previous timer <name> (if any).
87 void RPCRunLater(const std::string& name, boost::function<void(void)> func, int64_t nSeconds);
89 //! Convert boost::asio address to CNetAddr
90 extern CNetAddr BoostAsioToCNetAddr(boost::asio::ip::address address);
92 typedef json_spirit::Value(*rpcfn_type)(const json_spirit::Array& params, bool fHelp);
104 * Bitcoin RPC command dispatcher.
109 std::map<std::string, const CRPCCommand*> mapCommands;
112 const CRPCCommand* operator[](std::string name) const;
113 std::string help(std::string name) const;
117 * @param method Method to execute
118 * @param params Array of arguments (JSON objects)
119 * @returns Result of the call.
120 * @throws an exception (json_spirit::Value) when an error happens.
122 json_spirit::Value execute(const std::string &method, const json_spirit::Array ¶ms) const;
125 extern const CRPCTable tableRPC;
128 * Utilities: convert hex-encoded Values
129 * (throws error if not hex).
131 extern uint256 ParseHashV(const json_spirit::Value& v, std::string strName);
132 extern uint256 ParseHashO(const json_spirit::Object& o, std::string strKey);
133 extern std::vector<unsigned char> ParseHexV(const json_spirit::Value& v, std::string strName);
134 extern std::vector<unsigned char> ParseHexO(const json_spirit::Object& o, std::string strKey);
136 extern void InitRPCMining();
137 extern void ShutdownRPCMining();
139 extern int64_t nWalletUnlockTime;
140 extern CAmount AmountFromValue(const json_spirit::Value& value);
141 extern json_spirit::Value ValueFromAmount(const CAmount& amount);
142 extern double GetDifficulty(const CBlockIndex* blockindex = NULL);
143 extern std::string HelpRequiringPassphrase();
144 extern std::string HelpExampleCli(std::string methodname, std::string args);
145 extern std::string HelpExampleRpc(std::string methodname, std::string args);
147 extern void EnsureWalletIsUnlocked();
149 extern json_spirit::Value getconnectioncount(const json_spirit::Array& params, bool fHelp); // in rpcnet.cpp
150 extern json_spirit::Value getpeerinfo(const json_spirit::Array& params, bool fHelp);
151 extern json_spirit::Value ping(const json_spirit::Array& params, bool fHelp);
152 extern json_spirit::Value addnode(const json_spirit::Array& params, bool fHelp);
153 extern json_spirit::Value getaddednodeinfo(const json_spirit::Array& params, bool fHelp);
154 extern json_spirit::Value getnettotals(const json_spirit::Array& params, bool fHelp);
156 extern json_spirit::Value dumpprivkey(const json_spirit::Array& params, bool fHelp); // in rpcdump.cpp
157 extern json_spirit::Value importprivkey(const json_spirit::Array& params, bool fHelp);
158 extern json_spirit::Value importaddress(const json_spirit::Array& params, bool fHelp);
159 extern json_spirit::Value dumpwallet(const json_spirit::Array& params, bool fHelp);
160 extern json_spirit::Value importwallet(const json_spirit::Array& params, bool fHelp);
162 extern json_spirit::Value getgenerate(const json_spirit::Array& params, bool fHelp); // in rpcmining.cpp
163 extern json_spirit::Value setgenerate(const json_spirit::Array& params, bool fHelp);
164 extern json_spirit::Value generate(const json_spirit::Array& params, bool fHelp);
165 extern json_spirit::Value getnetworkhashps(const json_spirit::Array& params, bool fHelp);
166 extern json_spirit::Value getmininginfo(const json_spirit::Array& params, bool fHelp);
167 extern json_spirit::Value prioritisetransaction(const json_spirit::Array& params, bool fHelp);
168 extern json_spirit::Value getblocktemplate(const json_spirit::Array& params, bool fHelp);
169 extern json_spirit::Value submitblock(const json_spirit::Array& params, bool fHelp);
170 extern json_spirit::Value estimatefee(const json_spirit::Array& params, bool fHelp);
171 extern json_spirit::Value estimatepriority(const json_spirit::Array& params, bool fHelp);
173 extern json_spirit::Value getnewaddress(const json_spirit::Array& params, bool fHelp); // in rpcwallet.cpp
174 extern json_spirit::Value getaccountaddress(const json_spirit::Array& params, bool fHelp);
175 extern json_spirit::Value getrawchangeaddress(const json_spirit::Array& params, bool fHelp);
176 extern json_spirit::Value setaccount(const json_spirit::Array& params, bool fHelp);
177 extern json_spirit::Value getaccount(const json_spirit::Array& params, bool fHelp);
178 extern json_spirit::Value getaddressesbyaccount(const json_spirit::Array& params, bool fHelp);
179 extern json_spirit::Value sendtoaddress(const json_spirit::Array& params, bool fHelp);
180 extern json_spirit::Value signmessage(const json_spirit::Array& params, bool fHelp);
181 extern json_spirit::Value verifymessage(const json_spirit::Array& params, bool fHelp);
182 extern json_spirit::Value getreceivedbyaddress(const json_spirit::Array& params, bool fHelp);
183 extern json_spirit::Value getreceivedbyaccount(const json_spirit::Array& params, bool fHelp);
184 extern json_spirit::Value getbalance(const json_spirit::Array& params, bool fHelp);
185 extern json_spirit::Value getunconfirmedbalance(const json_spirit::Array& params, bool fHelp);
186 extern json_spirit::Value movecmd(const json_spirit::Array& params, bool fHelp);
187 extern json_spirit::Value sendfrom(const json_spirit::Array& params, bool fHelp);
188 extern json_spirit::Value sendmany(const json_spirit::Array& params, bool fHelp);
189 extern json_spirit::Value addmultisigaddress(const json_spirit::Array& params, bool fHelp);
190 extern json_spirit::Value createmultisig(const json_spirit::Array& params, bool fHelp);
191 extern json_spirit::Value listreceivedbyaddress(const json_spirit::Array& params, bool fHelp);
192 extern json_spirit::Value listreceivedbyaccount(const json_spirit::Array& params, bool fHelp);
193 extern json_spirit::Value listtransactions(const json_spirit::Array& params, bool fHelp);
194 extern json_spirit::Value listaddressgroupings(const json_spirit::Array& params, bool fHelp);
195 extern json_spirit::Value listaccounts(const json_spirit::Array& params, bool fHelp);
196 extern json_spirit::Value listsinceblock(const json_spirit::Array& params, bool fHelp);
197 extern json_spirit::Value gettransaction(const json_spirit::Array& params, bool fHelp);
198 extern json_spirit::Value backupwallet(const json_spirit::Array& params, bool fHelp);
199 extern json_spirit::Value keypoolrefill(const json_spirit::Array& params, bool fHelp);
200 extern json_spirit::Value walletpassphrase(const json_spirit::Array& params, bool fHelp);
201 extern json_spirit::Value walletpassphrasechange(const json_spirit::Array& params, bool fHelp);
202 extern json_spirit::Value walletlock(const json_spirit::Array& params, bool fHelp);
203 extern json_spirit::Value encryptwallet(const json_spirit::Array& params, bool fHelp);
204 extern json_spirit::Value validateaddress(const json_spirit::Array& params, bool fHelp);
205 extern json_spirit::Value getinfo(const json_spirit::Array& params, bool fHelp);
206 extern json_spirit::Value getwalletinfo(const json_spirit::Array& params, bool fHelp);
207 extern json_spirit::Value getblockchaininfo(const json_spirit::Array& params, bool fHelp);
208 extern json_spirit::Value getnetworkinfo(const json_spirit::Array& params, bool fHelp);
209 extern json_spirit::Value setmocktime(const json_spirit::Array& params, bool fHelp);
210 extern json_spirit::Value resendwallettransactions(const json_spirit::Array& params, bool fHelp);
211 extern json_spirit::Value zc_benchmark(const json_spirit::Array& params, bool fHelp);
212 extern json_spirit::Value zc_raw_keygen(const json_spirit::Array& params, bool fHelp);
213 extern json_spirit::Value zc_raw_pour(const json_spirit::Array& params, bool fHelp);
214 extern json_spirit::Value zc_raw_receive(const json_spirit::Array& params, bool fHelp);
216 extern json_spirit::Value getrawtransaction(const json_spirit::Array& params, bool fHelp); // in rcprawtransaction.cpp
217 extern json_spirit::Value listunspent(const json_spirit::Array& params, bool fHelp);
218 extern json_spirit::Value lockunspent(const json_spirit::Array& params, bool fHelp);
219 extern json_spirit::Value listlockunspent(const json_spirit::Array& params, bool fHelp);
220 extern json_spirit::Value createrawtransaction(const json_spirit::Array& params, bool fHelp);
221 extern json_spirit::Value decoderawtransaction(const json_spirit::Array& params, bool fHelp);
222 extern json_spirit::Value decodescript(const json_spirit::Array& params, bool fHelp);
223 extern json_spirit::Value signrawtransaction(const json_spirit::Array& params, bool fHelp);
224 extern json_spirit::Value sendrawtransaction(const json_spirit::Array& params, bool fHelp);
225 extern json_spirit::Value gettxoutproof(const json_spirit::Array& params, bool fHelp);
226 extern json_spirit::Value verifytxoutproof(const json_spirit::Array& params, bool fHelp);
228 extern json_spirit::Value getblockcount(const json_spirit::Array& params, bool fHelp); // in rpcblockchain.cpp
229 extern json_spirit::Value getbestblockhash(const json_spirit::Array& params, bool fHelp);
230 extern json_spirit::Value getdifficulty(const json_spirit::Array& params, bool fHelp);
231 extern json_spirit::Value settxfee(const json_spirit::Array& params, bool fHelp);
232 extern json_spirit::Value getmempoolinfo(const json_spirit::Array& params, bool fHelp);
233 extern json_spirit::Value getrawmempool(const json_spirit::Array& params, bool fHelp);
234 extern json_spirit::Value getblockhash(const json_spirit::Array& params, bool fHelp);
235 extern json_spirit::Value getblock(const json_spirit::Array& params, bool fHelp);
236 extern json_spirit::Value gettxoutsetinfo(const json_spirit::Array& params, bool fHelp);
237 extern json_spirit::Value gettxout(const json_spirit::Array& params, bool fHelp);
238 extern json_spirit::Value verifychain(const json_spirit::Array& params, bool fHelp);
239 extern json_spirit::Value getchaintips(const json_spirit::Array& params, bool fHelp);
240 extern json_spirit::Value invalidateblock(const json_spirit::Array& params, bool fHelp);
241 extern json_spirit::Value reconsiderblock(const json_spirit::Array& params, bool fHelp);
244 extern bool HTTPReq_REST(AcceptedConnection *conn,
245 const std::string& strURI,
246 const std::string& strRequest,
247 const std::map<std::string, std::string>& mapHeaders,
250 #endif // BITCOIN_RPCSERVER_H