Line data Source code
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.
5 :
6 : #ifndef BITCOIN_RPCSERVER_H
7 : #define BITCOIN_RPCSERVER_H
8 :
9 : #include "amount.h"
10 : #include "rpcprotocol.h"
11 : #include "uint256.h"
12 :
13 : #include <list>
14 : #include <map>
15 : #include <stdint.h>
16 : #include <string>
17 :
18 : #include <boost/function.hpp>
19 :
20 : #include <univalue.h>
21 :
22 : class CRPCCommand;
23 :
24 : namespace RPCServer
25 : {
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);
30 : }
31 :
32 : class CBlockIndex;
33 : class CNetAddr;
34 :
35 7038 : class JSONRequest
36 : {
37 : public:
38 : UniValue id;
39 : std::string strMethod;
40 : UniValue params;
41 :
42 14076 : JSONRequest() { id = NullUniValue; }
43 : void parse(const UniValue& valRequest);
44 : };
45 :
46 : /** Query whether RPC is running */
47 : bool IsRPCRunning();
48 :
49 : /**
50 : * Set the RPC warmup status. When this is done, all RPC calls will error out
51 : * immediately with RPC_IN_WARMUP.
52 : */
53 : void SetRPCWarmupStatus(const std::string& newStatus);
54 : /* Mark warmup as done. RPC calls will be processed from now on. */
55 : void SetRPCWarmupFinished();
56 :
57 : /* returns the current warmup state. */
58 : bool RPCIsInWarmup(std::string *statusOut);
59 :
60 : /**
61 : * Type-check arguments; throws JSONRPCError if wrong type given. Does not check that
62 : * the right number of arguments are passed, just that any passed are the correct type.
63 : * Use like: RPCTypeCheck(params, boost::assign::list_of(str_type)(int_type)(obj_type));
64 : */
65 : void RPCTypeCheck(const UniValue& params,
66 : const std::list<UniValue::VType>& typesExpected, bool fAllowNull=false);
67 :
68 : /*
69 : Check for expected keys/value types in an Object.
70 : Use like: RPCTypeCheckObj(object, boost::assign::map_list_of("name", str_type)("value", int_type));
71 : */
72 : void RPCTypeCheckObj(const UniValue& o,
73 : const std::map<std::string, UniValue::VType>& typesExpected, bool fAllowNull=false);
74 :
75 : /** Opaque base class for timers returned by NewTimerFunc.
76 : * This provides no methods at the moment, but makes sure that delete
77 : * cleans up the whole state.
78 : */
79 1 : class RPCTimerBase
80 : {
81 : public:
82 1 : virtual ~RPCTimerBase() {}
83 : };
84 :
85 : /**
86 : * RPC timer "driver".
87 : */
88 94 : class RPCTimerInterface
89 : {
90 : public:
91 94 : virtual ~RPCTimerInterface() {}
92 : /** Implementation name */
93 : virtual const char *Name() = 0;
94 : /** Factory function for timers.
95 : * RPC will call the function to create a timer that will call func in *millis* milliseconds.
96 : * @note As the RPC mechanism is backend-neutral, it can use different implementations of timers.
97 : * This is needed to cope with the case in which there is no HTTP server, but
98 : * only GUI RPC console, and to break the dependency of pcserver on httprpc.
99 : */
100 : virtual RPCTimerBase* NewTimer(boost::function<void(void)>& func, int64_t millis) = 0;
101 : };
102 :
103 : /** Register factory function for timers */
104 : void RPCRegisterTimerInterface(RPCTimerInterface *iface);
105 : /** Unregister factory function for timers */
106 : void RPCUnregisterTimerInterface(RPCTimerInterface *iface);
107 :
108 : /**
109 : * Run func nSeconds from now.
110 : * Overrides previous timer <name> (if any).
111 : */
112 : void RPCRunLater(const std::string& name, boost::function<void(void)> func, int64_t nSeconds);
113 :
114 : typedef UniValue(*rpcfn_type)(const UniValue& params, bool fHelp);
115 :
116 26784 : class CRPCCommand
117 : {
118 : public:
119 : std::string category;
120 : std::string name;
121 : rpcfn_type actor;
122 : bool okSafeMode;
123 : };
124 :
125 : /**
126 : * Bitcoin RPC command dispatcher.
127 : */
128 192 : class CRPCTable
129 : {
130 : private:
131 : std::map<std::string, const CRPCCommand*> mapCommands;
132 : public:
133 : CRPCTable();
134 : const CRPCCommand* operator[](const std::string& name) const;
135 : std::string help(const std::string& name) const;
136 :
137 : /**
138 : * Execute a method.
139 : * @param method Method to execute
140 : * @param params UniValue Array of arguments (JSON objects)
141 : * @returns Result of the call.
142 : * @throws an exception (UniValue) when an error happens.
143 : */
144 : UniValue execute(const std::string &method, const UniValue ¶ms) const;
145 : };
146 :
147 : extern const CRPCTable tableRPC;
148 :
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 :
158 : extern int64_t nWalletUnlockTime;
159 : extern CAmount AmountFromValue(const UniValue& value);
160 : extern UniValue ValueFromAmount(const CAmount& amount);
161 : extern double GetDifficulty(const CBlockIndex* blockindex = NULL);
162 : extern std::string HelpRequiringPassphrase();
163 : extern std::string HelpExampleCli(const std::string& methodname, const std::string& args);
164 : extern std::string HelpExampleRpc(const std::string& methodname, const std::string& args);
165 :
166 : extern void EnsureWalletIsUnlocked();
167 :
168 : extern UniValue getconnectioncount(const UniValue& params, bool fHelp); // in rpcnet.cpp
169 : extern UniValue getpeerinfo(const UniValue& params, bool fHelp);
170 : extern UniValue ping(const UniValue& params, bool fHelp);
171 : extern UniValue addnode(const UniValue& params, bool fHelp);
172 : extern UniValue disconnectnode(const UniValue& params, bool fHelp);
173 : extern UniValue getaddednodeinfo(const UniValue& params, bool fHelp);
174 : extern UniValue getnettotals(const UniValue& params, bool fHelp);
175 : extern UniValue setban(const UniValue& params, bool fHelp);
176 : extern UniValue listbanned(const UniValue& params, bool fHelp);
177 : extern UniValue clearbanned(const UniValue& params, bool fHelp);
178 :
179 : extern UniValue dumpprivkey(const UniValue& params, bool fHelp); // in rpcdump.cpp
180 : extern UniValue importprivkey(const UniValue& params, bool fHelp);
181 : extern UniValue importaddress(const UniValue& params, bool fHelp);
182 : extern UniValue importpubkey(const UniValue& params, bool fHelp);
183 : extern UniValue dumpwallet(const UniValue& params, bool fHelp);
184 : extern UniValue importwallet(const UniValue& params, bool fHelp);
185 :
186 : extern UniValue getgenerate(const UniValue& params, bool fHelp); // in rpcmining.cpp
187 : extern UniValue setgenerate(const UniValue& params, bool fHelp);
188 : extern UniValue generate(const UniValue& params, bool fHelp);
189 : extern UniValue getnetworkhashps(const UniValue& params, bool fHelp);
190 : extern UniValue getmininginfo(const UniValue& params, bool fHelp);
191 : extern UniValue prioritisetransaction(const UniValue& params, bool fHelp);
192 : extern UniValue getblocktemplate(const UniValue& params, bool fHelp);
193 : extern UniValue submitblock(const UniValue& params, bool fHelp);
194 : extern UniValue estimatefee(const UniValue& params, bool fHelp);
195 : extern UniValue estimatepriority(const UniValue& params, bool fHelp);
196 :
197 : extern UniValue getnewaddress(const UniValue& params, bool fHelp); // in rpcwallet.cpp
198 : extern UniValue getaccountaddress(const UniValue& params, bool fHelp);
199 : extern UniValue getrawchangeaddress(const UniValue& params, bool fHelp);
200 : extern UniValue setaccount(const UniValue& params, bool fHelp);
201 : extern UniValue getaccount(const UniValue& params, bool fHelp);
202 : extern UniValue getaddressesbyaccount(const UniValue& params, bool fHelp);
203 : extern UniValue sendtoaddress(const UniValue& params, bool fHelp);
204 : extern UniValue signmessage(const UniValue& params, bool fHelp);
205 : extern UniValue verifymessage(const UniValue& params, bool fHelp);
206 : extern UniValue getreceivedbyaddress(const UniValue& params, bool fHelp);
207 : extern UniValue getreceivedbyaccount(const UniValue& params, bool fHelp);
208 : extern UniValue getbalance(const UniValue& params, bool fHelp);
209 : extern UniValue getunconfirmedbalance(const UniValue& params, bool fHelp);
210 : extern UniValue movecmd(const UniValue& params, bool fHelp);
211 : extern UniValue sendfrom(const UniValue& params, bool fHelp);
212 : extern UniValue sendmany(const UniValue& params, bool fHelp);
213 : extern UniValue addmultisigaddress(const UniValue& params, bool fHelp);
214 : extern UniValue createmultisig(const UniValue& params, bool fHelp);
215 : extern UniValue listreceivedbyaddress(const UniValue& params, bool fHelp);
216 : extern UniValue listreceivedbyaccount(const UniValue& params, bool fHelp);
217 : extern UniValue listtransactions(const UniValue& params, bool fHelp);
218 : extern UniValue listaddressgroupings(const UniValue& params, bool fHelp);
219 : extern UniValue listaccounts(const UniValue& params, bool fHelp);
220 : extern UniValue listsinceblock(const UniValue& params, bool fHelp);
221 : extern UniValue gettransaction(const UniValue& params, bool fHelp);
222 : extern UniValue backupwallet(const UniValue& params, bool fHelp);
223 : extern UniValue keypoolrefill(const UniValue& params, bool fHelp);
224 : extern UniValue walletpassphrase(const UniValue& params, bool fHelp);
225 : extern UniValue walletpassphrasechange(const UniValue& params, bool fHelp);
226 : extern UniValue walletlock(const UniValue& params, bool fHelp);
227 : extern UniValue encryptwallet(const UniValue& params, bool fHelp);
228 : extern UniValue validateaddress(const UniValue& params, bool fHelp);
229 : extern UniValue getinfo(const UniValue& params, bool fHelp);
230 : extern UniValue getwalletinfo(const UniValue& params, bool fHelp);
231 : extern UniValue getblockchaininfo(const UniValue& params, bool fHelp);
232 : extern UniValue getnetworkinfo(const UniValue& params, bool fHelp);
233 : extern UniValue setmocktime(const UniValue& params, bool fHelp);
234 : extern UniValue resendwallettransactions(const UniValue& params, bool fHelp);
235 :
236 : extern UniValue getrawtransaction(const UniValue& params, bool fHelp); // in rcprawtransaction.cpp
237 : extern UniValue listunspent(const UniValue& params, bool fHelp);
238 : extern UniValue lockunspent(const UniValue& params, bool fHelp);
239 : extern UniValue listlockunspent(const UniValue& params, bool fHelp);
240 : extern UniValue createrawtransaction(const UniValue& params, bool fHelp);
241 : extern UniValue decoderawtransaction(const UniValue& params, bool fHelp);
242 : extern UniValue decodescript(const UniValue& params, bool fHelp);
243 : extern UniValue fundrawtransaction(const UniValue& params, bool fHelp);
244 : extern UniValue signrawtransaction(const UniValue& params, bool fHelp);
245 : extern UniValue sendrawtransaction(const UniValue& params, bool fHelp);
246 : extern UniValue gettxoutproof(const UniValue& params, bool fHelp);
247 : extern UniValue verifytxoutproof(const UniValue& params, bool fHelp);
248 :
249 : extern UniValue getblockcount(const UniValue& params, bool fHelp); // in rpcblockchain.cpp
250 : extern UniValue getbestblockhash(const UniValue& params, bool fHelp);
251 : extern UniValue getdifficulty(const UniValue& params, bool fHelp);
252 : extern UniValue settxfee(const UniValue& params, bool fHelp);
253 : extern UniValue getmempoolinfo(const UniValue& params, bool fHelp);
254 : extern UniValue getrawmempool(const UniValue& params, bool fHelp);
255 : extern UniValue getblockhash(const UniValue& params, bool fHelp);
256 : extern UniValue getblockheader(const UniValue& params, bool fHelp);
257 : extern UniValue getblock(const UniValue& params, bool fHelp);
258 : extern UniValue gettxoutsetinfo(const UniValue& params, bool fHelp);
259 : extern UniValue gettxout(const UniValue& params, bool fHelp);
260 : extern UniValue verifychain(const UniValue& params, bool fHelp);
261 : extern UniValue getchaintips(const UniValue& params, bool fHelp);
262 : extern UniValue invalidateblock(const UniValue& params, bool fHelp);
263 : extern UniValue reconsiderblock(const UniValue& params, bool fHelp);
264 :
265 : bool StartRPC();
266 : void InterruptRPC();
267 : void StopRPC();
268 : std::string JSONRPCExecBatch(const UniValue& vReq);
269 :
270 : #endif // BITCOIN_RPCSERVER_H
|