PIVX Core  5.6.99
P2P Digital Currency
server.h
Go to the documentation of this file.
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Copyright (c) 2015-2022 The PIVX Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef PIVX_RPC_SERVER_H
8 #define PIVX_RPC_SERVER_H
9 
10 #include "amount.h"
11 #include "rpc/protocol.h"
12 #include "uint256.h"
13 
14 #include <list>
15 #include <map>
16 #include <stdint.h>
17 #include <string>
18 
19 #include <univalue.h>
20 
21 class CRPCCommand;
22 
23 namespace RPCServer
24 {
25  void OnStarted(std::function<void ()> slot);
26  void OnStopped(std::function<void ()> slot);
27  void OnPreCommand(std::function<void (const CRPCCommand&)> slot);
28 }
29 
30 class CBlockIndex;
31 class CNetAddr;
32 
35 struct UniValueType {
38  bool typeAny;
40 };
41 
43 {
44 public:
46  std::string strMethod;
48  bool fHelp;
49  std::string URI;
50  std::string authUser;
51 
53  void parse(const UniValue& valRequest);
54 };
55 
57 bool IsRPCRunning();
58 
63 void SetRPCWarmupStatus(const std::string& newStatus);
64 /* Mark warmup as done. RPC calls will be processed from now on. */
66 
67 /* returns the current warmup state. */
68 bool RPCIsInWarmup(std::string* statusOut);
69 
74 void RPCTypeCheck(const UniValue& params,
75  const std::list<UniValue::VType>& typesExpected, bool fAllowNull=false);
76 
80 void RPCTypeCheckArgument(const UniValue& value, const UniValueType& typeExpected);
81 
85 void RPCTypeCheckObj(const UniValue& o,
86  const std::map<std::string, UniValueType>& typesExpected,
87  bool fAllowNull=false,
88  bool fStrict=false);
89 
95 {
96 public:
97  virtual ~RPCTimerBase() {}
98 };
99 
104 {
105 public:
106  virtual ~RPCTimerInterface() {}
108  virtual const char *Name() = 0;
115  virtual RPCTimerBase* NewTimer(std::function<void(void)>& func, int64_t millis) = 0;
116 };
117 
124 
129 void RPCRunLater(const std::string& name, std::function<void(void)> func, int64_t nSeconds);
130 
131 typedef UniValue(*rpcfn_type)(const JSONRPCRequest& jsonRequest);
132 
134 {
135 public:
136  std::string category;
137  std::string name;
140  std::vector<std::string> argNames;
141 };
142 
147 {
148 private:
149  std::map<std::string, const CRPCCommand*> mapCommands;
150 
151 public:
152  CRPCTable();
153  const CRPCCommand* operator[](const std::string& name) const;
154  std::string help(const std::string& name, const JSONRPCRequest& helpreq) const;
155 
162  UniValue execute(const JSONRPCRequest &request) const;
163 
168  std::vector<std::string> listCommands() const;
169 
175  bool appendCommand(const std::string& name, const CRPCCommand* pcmd);
176 };
177 
178 bool IsDeprecatedRPCEnabled(const std::string& method);
179 
180 extern CRPCTable tableRPC;
181 
186 extern uint256 ParseHashV(const UniValue& v, std::string strName);
187 extern uint256 ParseHashO(const UniValue& o, std::string strKey);
188 extern std::vector<unsigned char> ParseHexV(const UniValue& v, std::string strName);
189 extern std::vector<unsigned char> ParseHexO(const UniValue& o, std::string strKey);
190 extern int ParseInt(const UniValue& o, std::string strKey);
191 extern bool ParseBool(const UniValue& o, std::string strKey);
192 extern double ParseDoubleV(const UniValue& v, const std::string &strName);
193 
194 extern CAmount AmountFromValue(const UniValue& value);
195 extern UniValue ValueFromAmount(const CAmount& amount);
196 extern double GetDifficulty(const CBlockIndex* blockindex = nullptr);
197 extern std::string HelpExampleCli(std::string methodname, std::string args);
198 extern std::string HelpExampleRpc(std::string methodname, std::string args);
199 
200 bool StartRPC();
201 void InterruptRPC();
202 void StopRPC();
203 std::string JSONRPCExecBatch(const UniValue& vReq);
204 void RPCNotifyBlockChange(bool fInitialDownload, const CBlockIndex* pindex);
205 
206 #endif // PIVX_RPC_SERVER_H
int64_t CAmount
Amount in PIV (Can be negative)
Definition: amount.h:13
true
Definition: bls_dkg.cpp:153
false
Definition: bls_dkg.cpp:151
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:139
Network address.
Definition: netaddress.h:120
rpcfn_type actor
Definition: server.h:138
std::vector< std::string > argNames
Definition: server.h:140
std::string category
Definition: server.h:136
bool okSafeMode
Definition: server.h:139
std::string name
Definition: server.h:137
PIVX RPC command dispatcher.
Definition: server.h:147
CRPCTable()
Definition: server.cpp:295
std::vector< std::string > listCommands() const
Returns a list of registered commands.
Definition: server.cpp:520
const CRPCCommand * operator[](const std::string &name) const
Definition: server.cpp:306
bool appendCommand(const std::string &name, const CRPCCommand *pcmd)
Appends a CRPCCommand to the dispatch table.
Definition: server.cpp:314
UniValue execute(const JSONRPCRequest &request) const
Execute a method.
Definition: server.cpp:493
std::map< std::string, const CRPCCommand * > mapCommands
Definition: server.h:149
std::string help(const std::string &name, const JSONRPCRequest &helpreq) const
Note: This interface may still be subject to change.
Definition: server.cpp:197
UniValue params
Definition: server.h:47
std::string strMethod
Definition: server.h:46
UniValue id
Definition: server.h:45
void parse(const UniValue &valRequest)
Definition: server.cpp:377
JSONRPCRequest()
Definition: server.h:52
std::string URI
Definition: server.h:49
bool fHelp
Definition: server.h:48
std::string authUser
Definition: server.h:50
Opaque base class for timers returned by NewTimerFunc.
Definition: server.h:95
virtual ~RPCTimerBase()
Definition: server.h:97
RPC timer "driver".
Definition: server.h:104
virtual ~RPCTimerInterface()
Definition: server.h:106
virtual RPCTimerBase * NewTimer(std::function< void(void)> &func, int64_t millis)=0
Factory function for timers.
virtual const char * Name()=0
Implementation name.
@ VNULL
Definition: univalue.h:21
256-bit opaque blob.
Definition: uint256.h:138
void OnPreCommand(std::function< void(const CRPCCommand &)> slot)
Definition: server.cpp:58
void OnStarted(std::function< void()> slot)
Definition: server.cpp:48
void OnStopped(std::function< void()> slot)
Definition: server.cpp:53
const char * name
Definition: rest.cpp:37
void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface)
Set factory function for timers, but only if unset.
Definition: server.cpp:539
bool IsDeprecatedRPCEnabled(const std::string &method)
Definition: server.cpp:407
bool RPCIsInWarmup(std::string *statusOut)
Definition: server.cpp:369
void SetRPCWarmupFinished()
Definition: server.cpp:362
double GetDifficulty(const CBlockIndex *blockindex=nullptr)
Definition: blockchain.cpp:67
std::vector< unsigned char > ParseHexV(const UniValue &v, std::string strName)
Definition: server.cpp:151
void RPCNotifyBlockChange(bool fInitialDownload, const CBlockIndex *pindex)
Definition: blockchain.cpp:274
void RPCTypeCheckArgument(const UniValue &value, const UniValueType &typeExpected)
Type-check one argument; throws JSONRPCError if wrong type given.
Definition: server.cpp:82
void RPCUnsetTimerInterface(RPCTimerInterface *iface)
Unset factory function for timers.
Definition: server.cpp:550
bool ParseBool(const UniValue &o, std::string strKey)
Definition: server.cpp:183
UniValue ValueFromAmount(const CAmount &amount)
Definition: server.cpp:128
void StopRPC()
Definition: server.cpp:343
bool IsRPCRunning()
Query whether RPC is running.
Definition: server.cpp:351
void RPCRunLater(const std::string &name, std::function< void(void)> func, int64_t nSeconds)
Run func nSeconds from now.
Definition: server.cpp:556
std::string JSONRPCExecBatch(const UniValue &vReq)
Definition: server.cpp:434
int ParseInt(const UniValue &o, std::string strKey)
Definition: server.cpp:165
std::string HelpExampleCli(std::string methodname, std::string args)
Definition: server.cpp:527
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:63
void InterruptRPC()
Definition: server.cpp:336
void RPCTypeCheckObj(const UniValue &o, const std::map< std::string, UniValueType > &typesExpected, bool fAllowNull=false, bool fStrict=false)
Check for expected keys/value types in an Object.
Definition: server.cpp:89
CAmount AmountFromValue(const UniValue &value)
Definition: server.cpp:116
UniValue(* rpcfn_type)(const JSONRPCRequest &jsonRequest)
Definition: server.h:131
uint256 ParseHashO(const UniValue &o, std::string strKey)
Definition: server.cpp:147
double ParseDoubleV(const UniValue &v, const std::string &strName)
Definition: server.cpp:174
std::vector< unsigned char > ParseHexO(const UniValue &o, std::string strKey)
Definition: server.cpp:160
uint256 ParseHashV(const UniValue &v, std::string strName)
Utilities: convert hex-encoded Values (throws error if not hex).
Definition: server.cpp:138
std::string HelpExampleRpc(std::string methodname, std::string args)
Definition: server.cpp:532
bool StartRPC()
Definition: server.cpp:328
void SetRPCWarmupStatus(const std::string &newStatus)
Set the RPC warmup status.
Definition: server.cpp:356
CRPCTable tableRPC
Definition: server.cpp:565
void RPCSetTimerInterface(RPCTimerInterface *iface)
Set factory function for timers.
Definition: server.cpp:545
Wrapper for UniValue::VType, which includes typeAny: Used to denote don't care type.
Definition: server.h:35
bool typeAny
Definition: server.h:38
UniValueType(UniValue::VType _type)
Definition: server.h:36
UniValue::VType type
Definition: server.h:39
UniValueType()
Definition: server.h:37
const UniValue NullUniValue
Definition: univalue.cpp:13