1
// Copyright (c) 2010 Satoshi Nakamoto
2
// Copyright (c) 2009-2015 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 "rpc/protocol.h"
18
#include <boost/function.hpp>
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
/** Wrapper for UniValue::VType, which includes typeAny:
36
* Used to denote don't care type. Only used by RPCTypeCheckObj */
38
UniValueType(UniValue::VType _type) : typeAny(false), type(_type) {}
39
UniValueType() : typeAny(true) {}
48
std::string strMethod;
51
JSONRequest() { id = NullUniValue; }
52
void parse(const UniValue& valRequest);
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.
73
void RPCTypeCheck(const UniValue& params,
74
const std::list<UniValue::VType>& typesExpected, bool fAllowNull=false);
77
Check for expected keys/value types in an Object.
79
void RPCTypeCheckObj(const UniValue& o,
80
const std::map<std::string, UniValueType>& typesExpected,
81
bool fAllowNull = false,
82
bool fStrict = false);
84
/** Opaque base class for timers returned by NewTimerFunc.
85
* This provides no methods at the moment, but makes sure that delete
86
* cleans up the whole state.
91
virtual ~RPCTimerBase() {}
97
class RPCTimerInterface
100
virtual ~RPCTimerInterface() {}
101
/** Implementation name */
102
virtual const char *Name() = 0;
103
/** Factory function for timers.
104
* RPC will call the function to create a timer that will call func in *millis* milliseconds.
105
* @note As the RPC mechanism is backend-neutral, it can use different implementations of timers.
106
* This is needed to cope with the case in which there is no HTTP server, but
107
* only GUI RPC console, and to break the dependency of pcserver on httprpc.
109
virtual RPCTimerBase* NewTimer(boost::function<void(void)>& func, int64_t millis) = 0;
112
/** Set the factory function for timers */
113
void RPCSetTimerInterface(RPCTimerInterface *iface);
114
/** Set the factory function for timer, but only, if unset */
115
void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface);
116
/** Unset factory function for timers */
117
void RPCUnsetTimerInterface(RPCTimerInterface *iface);
120
* Run func nSeconds from now.
121
* Overrides previous timer <name> (if any).
123
void RPCRunLater(const std::string& name, boost::function<void(void)> func, int64_t nSeconds);
125
typedef UniValue(*rpcfn_type)(const UniValue& params, bool fHelp);
130
std::string category;
137
* Bitcoin RPC command dispatcher.
142
std::map<std::string, const CRPCCommand*> mapCommands;
145
const CRPCCommand* operator[](const std::string& name) const;
146
std::string help(const std::string& name) const;
150
* @param method Method to execute
151
* @param params UniValue Array of arguments (JSON objects)
152
* @returns Result of the call.
153
* @throws an exception (UniValue) when an error happens.
155
UniValue execute(const std::string &method, const UniValue ¶ms) const;
158
* Returns a list of registered commands
159
* @returns List of registered commands.
161
std::vector<std::string> listCommands() const;
165
* Appends a CRPCCommand to the dispatch table.
166
* Returns false if RPC server is already running (dump concurrency protection).
167
* Commands cannot be overwritten (returns false).
169
bool appendCommand(const std::string& name, const CRPCCommand* pcmd);
172
extern CRPCTable tableRPC;
175
* Utilities: convert hex-encoded Values
176
* (throws error if not hex).
178
extern uint256 ParseHashV(const UniValue& v, std::string strName);
179
extern uint256 ParseHashO(const UniValue& o, std::string strKey);
180
extern std::vector<unsigned char> ParseHexV(const UniValue& v, std::string strName);
181
extern std::vector<unsigned char> ParseHexO(const UniValue& o, std::string strKey);
183
extern int64_t nWalletUnlockTime;
184
extern CAmount AmountFromValue(const UniValue& value);
185
extern UniValue ValueFromAmount(const CAmount& amount);
186
extern double GetDifficulty(const CBlockIndex* blockindex = NULL);
187
extern std::string HelpRequiringPassphrase();
188
extern std::string HelpExampleCli(const std::string& methodname, const std::string& args);
189
extern std::string HelpExampleRpc(const std::string& methodname, const std::string& args);
191
extern void EnsureWalletIsUnlocked();
196
std::string JSONRPCExecBatch(const UniValue& vReq);
198
#endif // BITCOIN_RPCSERVER_H