Fabcoin Core  0.16.2
P2P Digital Currency
server.h
Go to the documentation of this file.
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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 FABCOIN_RPCSERVER_H
7 #define FABCOIN_RPCSERVER_H
8 
9 #include <amount.h>
10 #include <rpc/protocol.h>
11 #include <uint256.h>
12 
13 #include <list>
14 #include <map>
15 #include <stdint.h>
16 #include <string>
17 
18 #include <univalue.h>
19 #include <httpserver.h>
20 #include <mutex>
21 #include <condition_variable>
22 
23 static const unsigned int DEFAULT_RPC_SERIALIZE_VERSION = 1;
24 
26 {
28  int height;
29 };
30 static std::mutex cs_blockchange;
31 static std::condition_variable cond_blockchange;
32 static CUpdatedBlock latestblock;
33 class CRPCCommand;
34 
35 namespace RPCServer
36 {
37  void OnStarted(std::function<void ()> slot);
38  void OnStopped(std::function<void ()> slot);
39  void OnPreCommand(std::function<void (const CRPCCommand&)> slot);
40  void OnPostCommand(std::function<void (const CRPCCommand&)> slot);
41 }
42 class CBlockIndex;
43 class CNetAddr;
44 
47 struct UniValueType {
48  UniValueType(UniValue::VType _type) : typeAny(false), type(_type) {}
49  UniValueType() : typeAny(true) {}
50  bool typeAny;
52 };
53 
55 {
56 public:
58  std::string strMethod;
60  bool fHelp;
61  std::string URI;
62  std::string authUser;
63 
65 
70  id = NullUniValue;
71  params = NullUniValue;
72  fHelp = false;
73  req = NULL;
74  isLongPolling = false;
75  };
76 
78 
82  void PollStart();
83 
87  void PollPing();
88 
92  bool PollAlive();
93 
97  void PollCancel();
98 
102  void PollReply(const UniValue& result);
103 
104  void parse(const UniValue& valRequest);
105 
106  // FIXME: make this private?
108 };
109 
111 bool IsRPCRunning();
112 
117 void SetRPCWarmupStatus(const std::string& newStatus);
118 /* Mark warmup as done. RPC calls will be processed from now on. */
119 void SetRPCWarmupFinished();
120 
121 /* returns the current warmup state. */
122 bool RPCIsInWarmup(std::string *outStatus);
123 
128 void RPCTypeCheck(const UniValue& params,
129  const std::list<UniValue::VType>& typesExpected, bool fAllowNull=false);
130 
134 void RPCTypeCheckArgument(const UniValue& value, UniValue::VType typeExpected);
135 
136 /*
137  Check for expected keys/value types in an Object.
138 */
139 void RPCTypeCheckObj(const UniValue& o,
140  const std::map<std::string, UniValueType>& typesExpected,
141  bool fAllowNull = false,
142  bool fStrict = false);
143 
149 {
150 public:
151  virtual ~RPCTimerBase() {}
152 };
153 
158 {
159 public:
160  virtual ~RPCTimerInterface() {}
162  virtual const char *Name() = 0;
169  virtual RPCTimerBase* NewTimer(std::function<void(void)>& func, int64_t millis) = 0;
170 };
171 
178 
183 void RPCRunLater(const std::string& name, std::function<void(void)> func, int64_t nSeconds);
184 
185 typedef UniValue(*rpcfn_type)(const JSONRPCRequest& jsonRequest);
186 
188 {
189 public:
190  std::string category;
191  std::string name;
194  std::vector<std::string> argNames;
195 };
196 
201 {
202 private:
203  std::map<std::string, const CRPCCommand*> mapCommands;
204 public:
205  CRPCTable();
206  const CRPCCommand* operator[](const std::string& name) const;
207  std::string help(const std::string& name, const JSONRPCRequest& helpreq) const;
208 
215  UniValue execute(const JSONRPCRequest &request) const;
216 
221  std::vector<std::string> listCommands() const;
222 
223 
229  bool appendCommand(const std::string& name, const CRPCCommand* pcmd);
230 };
231 
232 extern CRPCTable tableRPC;
233 
238 extern uint256 ParseHashV(const UniValue& v, std::string strName);
239 extern uint256 ParseHashO(const UniValue& o, std::string strKey);
240 extern std::vector<unsigned char> ParseHexV(const UniValue& v, std::string strName);
241 extern std::vector<unsigned char> ParseHexO(const UniValue& o, std::string strKey);
242 
243 extern int64_t nWalletUnlockTime;
244 extern CAmount AmountFromValue(const UniValue& value);
245 extern UniValue ValueFromAmount(const CAmount& amount);
246 extern double GetDifficulty(const CBlockIndex* blockindex);
247 extern double GetPoWMHashPS();
248 
249 extern std::string HelpRequiringPassphrase();
250 extern std::string HelpExampleCli(const std::string& methodname, const std::string& args);
251 extern std::string HelpExampleRpc(const std::string& methodname, const std::string& args);
252 
253 extern void EnsureWalletIsUnlocked();
254 bool StartRPC();
255 void InterruptRPC();
256 void StopRPC();
257 std::string JSONRPCExecBatch(const UniValue& vReq);
258 void RPCNotifyBlockChange(bool ibd, const CBlockIndex *);
259 
260 // Retrieves any serialization flags requested in command line argument
262 
263 #endif // FABCOIN_RPCSERVER_H
UniValueType(UniValue::VType _type)
Definition: server.h:48
void help()
Definition: main.cpp:39
bool StartRPC()
Definition: server.cpp:312
RPC timer "driver".
Definition: server.h:157
std::string category
Definition: server.h:190
#define function(a, b, c, d, k, s)
bool okSafeMode
Definition: server.h:193
std::string JSONRPCExecBatch(const UniValue &vReq)
Definition: server.cpp:453
bool IsRPCRunning()
Query whether RPC is running.
Definition: server.cpp:335
Fabcoin RPC command dispatcher.
Definition: server.h:200
uint256 ParseHashV(const UniValue &v, std::string strName)
Utilities: convert hex-encoded Values (throws error if not hex).
Definition: server.cpp:126
rpcfn_type actor
Definition: server.h:192
std::vector< unsigned char > ParseHexO(const UniValue &o, std::string strKey)
Definition: server.cpp:152
int height
Definition: server.h:28
bool typeAny
Definition: server.h:50
void SetRPCWarmupFinished()
Definition: server.cpp:346
void OnStopped(std::function< void()> slot)
Definition: server.cpp:49
void RPCSetTimerInterface(RPCTimerInterface *iface)
Set the factory function for timers.
Definition: server.cpp:571
std::vector< std::string > argNames
Definition: server.h:194
uint256 hash
Definition: server.h:27
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
Definition: server.cpp:559
HTTPRequest * req
Definition: server.h:107
UniValue::VType type
Definition: server.h:51
int64_t CAmount
Amount in lius (Can be negative)
Definition: amount.h:15
std::string strMethod
Definition: server.h:58
CAmount AmountFromValue(const UniValue &value)
Definition: server.cpp:114
double GetPoWMHashPS()
CRPCTable tableRPC
Definition: server.cpp:599
void RPCTypeCheck(const UniValue &params, const std::list< UniValue::VType > &typesExpected, bool fAllowNull=false)
Type-check arguments; throws JSONRPCError if wrong type given.
Definition: server.cpp:59
std::string name
Definition: server.h:191
std::string HelpExampleCli(const std::string &methodname, const std::string &args)
Definition: server.cpp:554
void RPCTypeCheckArgument(const UniValue &value, UniValue::VType typeExpected)
Type-check one argument; throws JSONRPCError if wrong type given.
Definition: server.cpp:77
std::vector< unsigned char > ParseHexV(const UniValue &v, std::string strName)
Definition: server.cpp:143
bool isLongPolling
Definition: server.h:64
UniValue params
Definition: server.h:59
const char * name
Definition: rest.cpp:36
int64_t nWalletUnlockTime
UniValue id
Definition: server.h:57
int RPCSerializationFlags()
Definition: server.cpp:591
virtual ~RPCTimerInterface()
Definition: server.h:160
virtual ~RPCTimerBase()
Definition: server.h:151
std::map< std::string, const CRPCCommand * > mapCommands
Definition: server.h:203
void RPCNotifyBlockChange(bool ibd, const CBlockIndex *)
Callback for when block tip changed.
Definition: blockchain.cpp:308
std::string HelpRequiringPassphrase()
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
Definition: netaddress.h:31
bool fHelp
Definition: server.h:60
256-bit opaque blob.
Definition: uint256.h:132
void RPCTypeCheckObj(const UniValue &o, const std::map< std::string, UniValueType > &typesExpected, bool fAllowNull=false, bool fStrict=false)
Definition: server.cpp:84
void RPCUnsetTimerInterface(RPCTimerInterface *iface)
Unset factory function for timers.
Definition: server.cpp:576
UniValueType()
Definition: server.h:49
void OnPostCommand(std::function< void(const CRPCCommand &)> slot)
JSONRPCRequest()
If using batch JSON request, this object won&#39;t get the underlying HTTPRequest.
Definition: server.h:69
PlatformStyle::TableColorType type
Definition: rpcconsole.cpp:61
uint256 ParseHashO(const UniValue &o, std::string strKey)
Definition: server.cpp:139
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:177
UniValue ValueFromAmount(const CAmount &amount)
Definition: core_write.cpp:19
void StopRPC()
Definition: server.cpp:327
std::string URI
Definition: server.h:61
void InterruptRPC()
Definition: server.cpp:320
std::string authUser
Definition: server.h:62
Opaque base class for timers returned by NewTimerFunc.
Definition: server.h:148
const UniValue NullUniValue
Definition: univalue.cpp:15
void RPCRunLater(const std::string &name, std::function< void(void)> func, int64_t nSeconds)
Run func nSeconds from now.
Definition: server.cpp:582
void OnStarted(std::function< void()> slot)
Definition: server.cpp:44
In-flight HTTP request.
Definition: httpserver.h:59
void SetRPCWarmupStatus(const std::string &newStatus)
Set the RPC warmup status.
Definition: server.cpp:340
void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface)
Set the factory function for timer, but only, if unset.
Definition: server.cpp:565
UniValue(* rpcfn_type)(const JSONRPCRequest &jsonRequest)
Definition: server.h:185
double GetDifficulty(const CBlockIndex *blockindex)
Get the difficulty of the net wrt to the given block index, or the chain tip if not provided...
Definition: blockchain.cpp:105
Wrapper for UniValue::VType, which includes typeAny: Used to denote don&#39;t care type.
Definition: server.h:47
bool RPCIsInWarmup(std::string *outStatus)
Definition: server.cpp:353
void EnsureWalletIsUnlocked()
void OnPreCommand(std::function< void(const CRPCCommand &)> slot)
Definition: server.cpp:54