20 #include "validation.h"
29 throw std::runtime_error(
30 "getconnectioncount\n"
31 "\nReturns the number of connections to other nodes.\n"
34 "n (numeric) The connection count\n"
48 throw std::runtime_error(
50 "\nRequests that a ping be sent to all other nodes, to measure ping time.\n"
51 "Results provided in getpeerinfo, pingtime and pingwait fields are decimal seconds.\n"
52 "Ping command is handled in queue with all other commands, so it measures processing backlog, not just network ping.\n"
70 throw std::runtime_error(
72 "\nReturns data about each connected network node as a json array of objects.\n"
77 " \"id\": n, (numeric) Peer index\n"
78 " \"addr\":\"host:port\", (string) The ip address and port of the peer\n"
79 " \"addrlocal\":\"ip:port\", (string) local address\n"
80 " \"mapped_as\":\"mapped_as\", (string) The AS in the BGP route to the peer used for diversifying\n"
81 "peer selection (only available if the asmap config flag is set)\n"
82 " \"services\":\"xxxxxxxxxxxxxxxx\", (string) The services offered\n"
83 " \"lastsend\": ttt, (numeric) The time in seconds since epoch (Jan 1 1970 GMT) of the last send\n"
84 " \"lastrecv\": ttt, (numeric) The time in seconds since epoch (Jan 1 1970 GMT) of the last receive\n"
85 " \"bytessent\": n, (numeric) The total bytes sent\n"
86 " \"bytesrecv\": n, (numeric) The total bytes received\n"
87 " \"conntime\": ttt, (numeric) The connection time in seconds since epoch (Jan 1 1970 GMT)\n"
88 " \"timeoffset\": ttt, (numeric) The time offset in seconds\n"
89 " \"pingtime\": n, (numeric) ping time\n"
90 " \"pingwait\": n, (numeric) ping wait\n"
91 " \"version\": v, (numeric) The peer version, such as 7001\n"
92 " \"subver\": \"/Pivx Core:x.x.x.x/\", (string) The string version\n"
93 " \"inbound\": true|false, (boolean) Inbound (true) or Outbound (false)\n"
94 " \"addnode\": true|false, (boolean) Whether connection was due to addnode and is using an addnode slot\n"
95 " \"masternode\": true|false, (boolean) Whether the connection is only for masternode quorums related messages\n"
96 " \"startingheight\": n, (numeric) The starting height (block) of the peer\n"
97 " \"banscore\": n, (numeric) The ban score\n"
98 " \"synced_headers\": n, (numeric) The last header we have in common with this peer\n"
99 " \"synced_blocks\": n, (numeric) The last block we have in common with this peer\n"
101 " n, (numeric) The heights of blocks we're currently asking from this peer\n"
104 " \"addr_processed\": n, (numeric) The total number of addresses processed, excluding those dropped due to rate limiting\n"
105 " \"addr_rate_limited\": n, (numeric) The total number of addresses dropped due to rate limiting\n"
106 " \"bytessent_per_msg\": {\n"
107 " \"addr\": n, (numeric) The total bytes sent aggregated by message type\n"
110 " \"bytesrecv_per_msg\": {\n"
111 " \"addr\": n, (numeric) The total bytes received aggregated by message type\n"
114 " \"masternode_iqr_conn\": true|false, (boolean) Whether the connection is an intra-quorum relay connection or not\n"
115 " \"verif_mn_proreg_tx_hash\": \"hex\", (string) The MN provider register tx hash (if the connection is verified)\n"
116 " \"verif_mn_operator_pubkey_hash\": \"hex\", (string) The MN operator pubkey hash (if the connection is verified)\n"
127 std::vector<CNodeStats> vstats;
136 obj.
pushKV(
"id", stats.nodeid);
137 obj.
pushKV(
"addr", stats.addrName);
138 if (!(stats.addrLocal.empty()))
139 obj.
pushKV(
"addrlocal", stats.addrLocal);
140 if (stats.m_mapped_as != 0) {
141 obj.
pushKV(
"mapped_as", uint64_t(stats.m_mapped_as));
144 obj.
pushKV(
"lastsend", stats.nLastSend);
145 obj.
pushKV(
"lastrecv", stats.nLastRecv);
146 obj.
pushKV(
"bytessent", stats.nSendBytes);
147 obj.
pushKV(
"bytesrecv", stats.nRecvBytes);
148 obj.
pushKV(
"conntime", stats.nTimeConnected);
149 obj.
pushKV(
"timeoffset", stats.nTimeOffset);
150 obj.
pushKV(
"pingtime", stats.dPingTime);
151 if (stats.dPingWait > 0.0)
152 obj.
pushKV(
"pingwait", stats.dPingWait);
153 obj.
pushKV(
"version", stats.nVersion);
157 obj.
pushKV(
"subver", stats.cleanSubVer);
158 obj.
pushKV(
"inbound", stats.fInbound);
159 obj.
pushKV(
"addnode", stats.fAddnode);
160 obj.
pushKV(
"masternode", stats.m_masternode_connection);
161 obj.
pushKV(
"startingheight", stats.nStartingHeight);
170 obj.
pushKV(
"inflight", heights);
174 obj.
pushKV(
"whitelisted", stats.fWhitelisted);
177 for (
const mapMsgCmdSize::value_type &i : stats.mapSendBytesPerMsgCmd) {
179 sendPerMsgCmd.
pushKV(i.first, i.second);
181 obj.
pushKV(
"bytessent_per_msg", sendPerMsgCmd);
184 for (
const mapMsgCmdSize::value_type &i : stats.mapRecvBytesPerMsgCmd) {
186 recvPerMsgCmd.
pushKV(i.first, i.second);
188 obj.
pushKV(
"bytesrecv_per_msg", recvPerMsgCmd);
191 if (stats.m_masternode_connection) {
192 obj.
pushKV(
"masternode_iqr_conn", stats.m_masternode_iqr_connection);
193 obj.
pushKV(
"verif_mn_proreg_tx_hash", stats.verifiedProRegTxHash.GetHex());
194 obj.
pushKV(
"verif_mn_operator_pubkey_hash", stats.verifiedPubKeyHash.GetHex());
205 std::string strCommand;
209 (strCommand !=
"onetry" && strCommand !=
"add" && strCommand !=
"remove"))
210 throw std::runtime_error(
211 "addnode \"node\" \"add|remove|onetry\"\n"
212 "\nAttempts add or remove a node from the addnode list.\n"
213 "Or try a connection to a node once.\n"
216 "1. \"node\" (string, required) The node (see getpeerinfo for nodes)\n"
217 "2. \"command\" (string, required) 'add' to add a node to the list, 'remove' to remove a node from the list, 'onetry' to try a connection to the node once\n"
227 if (strCommand ==
"onetry") {
229 g_connman->OpenNetworkConnection(addr,
false,
nullptr, strNode.c_str());
233 if (strCommand ==
"add") {
236 }
else if (strCommand ==
"remove") {
247 throw std::runtime_error(
248 "disconnectnode \"node\" \n"
249 "\nImmediately disconnects from the specified node.\n"
252 "1. \"node\" (string, required) The node (see getpeerinfo for nodes)\n"
272 throw std::runtime_error(
273 "getaddednodeinfo dummy ( \"node\" )\n"
274 "\nReturns information about the given added node, or all added nodes\n"
275 "(note that onetry addnodes are not listed here)\n"
278 "1. dummy (boolean, required) Kept for historical purposes but ignored\n"
279 "2. \"node\" (string, optional) If provided, return information about this specific node, otherwise all nodes are returned.\n"
284 " \"addednode\" : \"192.168.0.201\", (string) The node ip address or name (as provided to addnode)\n"
285 " \"connected\" : true|false, (boolean) If connected\n"
286 " \"addresses\" : [ (list of objects) Only when connected = true\n"
288 " \"address\" : \"192.168.0.201:51472\", (string) The pivx server IP and port we're connected to\n"
289 " \"connected\" : \"outbound\" (string) connection, inbound or outbound\n"
302 std::vector<AddedNodeInfo> vInfo =
g_connman->GetAddedNodeInfo();
308 vInfo.assign(1, info);
322 obj.
pushKV(
"addednode", info.strAddedNode);
323 obj.
pushKV(
"connected", info.fConnected);
325 if (info.fConnected) {
327 address.
pushKV(
"address", info.resolvedAddress.ToString());
328 address.
pushKV(
"connected", info.fInbound ?
"inbound" :
"outbound");
331 obj.
pushKV(
"addresses", addresses);
341 throw std::runtime_error(
343 "\nReturns information about network traffic, including bytes in, bytes out,\n"
344 "and current time.\n"
348 " \"totalbytesrecv\": n, (numeric) Total bytes received\n"
349 " \"totalbytessent\": n, (numeric) Total bytes sent\n"
350 " \"timemillis\": t (numeric) Total cpu time\n"
369 for (
int n = 0; n <
NET_MAX; ++n) {
380 networks.push_back(obj);
388 throw std::runtime_error(
390 "\nReturns an object containing various state info regarding P2P networking.\n"
394 " \"version\": xxxxx, (numeric) the server version\n"
395 " \"subversion\": \"/Pivx Core:x.x.x.x/\", (string) the server subversion string\n"
396 " \"protocolversion\": xxxxx, (numeric) the protocol version\n"
397 " \"localservices\": \"xxxxxxxxxxxxxxxx\", (string) the services we offer to the network\n"
398 " \"timeoffset\": xxxxx, (numeric) the time offset\n"
399 " \"connections\": xxxxx, (numeric) the number of connections\n"
400 " \"networkactive\": true|false, (boolean) the network activity status\n"
401 " \"networks\": [ (array) information per network\n"
403 " \"name\": \"xxx\", (string) network (ipv4, ipv6 or onion)\n"
404 " \"limited\": true|false, (boolean) is the network limited using -onlynet?\n"
405 " \"reachable\": true|false, (boolean) is the network reachable?\n"
406 " \"proxy\": \"host:port\" (string) the proxy that is used for this network, or empty if none\n"
410 " \"relayfee\": x.xxxxxxxx, (numeric) minimum relay fee for transactions in " +
CURRENCY_UNIT +
"/kB\n"
411 " \"incrementalfee\": x.xxxxxxxx, (numeric) minimum fee increment for mempool limiting or BIP 125 replacement in " +
CURRENCY_UNIT +
"/kB\n"
412 " \"localaddresses\": [ (array) list of local addresses\n"
414 " \"address\": \"xxxx\", (string) network address\n"
415 " \"port\": xxx, (numeric) network port\n"
416 " \"score\": xxx (numeric) relative score\n"
420 " \"warnings\": \"...\" (string) any network and blockchain warnings\n"
428 obj.
pushKV(
"version", CLIENT_VERSION);
430 obj.
pushKV(
"protocolversion", PROTOCOL_VERSION);
438 obj.
pushKV(
"networks", GetNetworksInfo());
443 for (
const std::pair<const CNetAddr, LocalServiceInfo> &item :
mapLocalHost) {
445 rec.
pushKV(
"address", item.first.ToString());
446 rec.
pushKV(
"port", item.second.nPort);
447 rec.
pushKV(
"score", item.second.nScore);
451 obj.
pushKV(
"localaddresses", localAddresses);
458 std::string strCommand;
462 (strCommand !=
"add" && strCommand !=
"remove"))
463 throw std::runtime_error(
464 "setban \"subnet\" \"add|remove\" ( bantime absolute )\n"
465 "\nAttempts add or remove a IP/Subnet from the banned list.\n"
468 "1. \"subnet\" (string, required) The IP/Subnet (see getpeerinfo for nodes ip) with a optional netmask (default is /32 = single ip)\n"
469 "2. \"command\" (string, required) 'add' to add a IP/Subnet to the list, 'remove' to remove a IP/Subnet from the list\n"
470 "3. \"bantime\" (numeric, optional) time in seconds how long (or until when if [absolute] is set) the ip is banned (0 or empty means using the default time of 24h which can also be overwritten by the -bantime startup argument)\n"
471 "4. \"absolute\" (boolean, optional) If set, the bantime must be a absolute timestamp in seconds since epoch (Jan 1 1970 GMT)\n"
483 bool isSubnet =
false;
485 if (request.
params[0].
get_str().find(
'/') != std::string::npos)
498 if (strCommand ==
"add")
507 bool absolute =
false;
513 else if(strCommand ==
"remove")
524 throw std::runtime_error(
526 "\nList all banned IPs/Subnets.\n"
531 " \"address\": \"xxx\", (string) Network address of banned client.\n"
532 " \"banned_until\": nnn, (numeric) Timestamp when the ban is lifted.\n"
533 " \"ban_created\": nnn, (numeric) Timestamp when the ban was created.\n"
534 " \"ban_reason\": \"xxx\" (string) Reason for banning.\n"
550 for (
const auto& entry : banMap)
552 const CBanEntry& banEntry = entry.second;
554 rec.
pushKV(
"address", entry.first.ToString());
562 return bannedAddresses;
568 throw std::runtime_error(
570 "\nClear all banned IPs.\n"
587 throw std::runtime_error(
588 "getnodeaddresses ( count \"network\" )\n"
589 "\nReturn known addresses which can potentially be used to find new nodes in the network\n"
592 "1. count (numeric, optional) The maximum number of addresses to return. Specify 0 to return all known addresses.\n"
593 "2. \"network\" (string, optional) Return only addresses of the specified network. Can be one of: ipv4, ipv6, onion."
598 " \"time\": ttt, (numeric) Timestamp in seconds since epoch (Jan 1 1970 GMT) when the node was last seen\n"
599 " \"services\": n, (numeric) The services offered by the node\n"
600 " \"address\": \"host\", (string) The address of the node\n"
601 " \"port\": n, (numeric) The port number of the node\n"
602 " \"network\": \"xxxx\" (string) The network (ipv4, ipv6, onion) the node connected through\n"
627 const std::vector<CAddress> vAddr{
g_connman->GetAddresses(count, 0, network)};
630 for (
const CAddress& addr : vAddr) {
632 obj.pushKV(
"time", (
int)addr.nTime);
633 obj.pushKV(
"services", (uint64_t)addr.nServices);
634 obj.pushKV(
"address", addr.ToStringIP());
635 obj.pushKV(
"port", addr.GetPort());
645 throw std::runtime_error(
646 "addpeeraddress \"address\" port\n"
647 "\nAdd the address of a potential peer to the address manager. This RPC is for testing only.\n"
650 "1. \"address\" (string, required) The IP address of the peer\n"
651 "2. port (numeric, required) The port of the peer\n"
655 " \"success\": true|false (boolean) Whether the peer address was successfully added to the address manager\n"
672 if (!
LookupHost(addr_string, net_addr,
false)) {
673 obj.pushKV(
"success",
false);
680 if (!
g_connman->AddNewAddresses({address}, address)) {
681 obj.pushKV(
"success",
false);
685 obj.pushKV(
"success",
true);
692 throw std::runtime_error(
693 "setnetworkactive \"true|false\"\n"
694 "Disable/enable all p2p network activity.\n"
697 "status (boolean) The final network activity status\n"
712 {
"network",
"addnode", &
addnode,
true, {
"node",
"command"} },
713 {
"network",
"clearbanned", &
clearbanned,
true, {} },
715 {
"network",
"getaddednodeinfo", &
getaddednodeinfo,
true, {
"dummy",
"node"} },
719 {
"network",
"getnodeaddresses", &getnodeaddresses,
true, {
"count"} },
720 {
"network",
"getpeerinfo", &
getpeerinfo,
true, {} },
721 {
"network",
"listbanned", &
listbanned,
true, {} },
722 {
"network",
"ping", &
ping,
true, {} },
723 {
"network",
"setban", &
setban,
true, {
"subnet",
"command",
"bantime",
"absolute"} },
727 {
"hidden",
"addpeeraddress", &addpeeraddress,
true, {
"address",
"port"} },
733 for (
unsigned int vcidx = 0; vcidx <
ARRAYLEN(commands); vcidx++)
std::map< CSubNet, CBanEntry > banmap_t
A CService with information about it as peer.
uint32_t nTime
Always included in serialization, except in the network format on INIT_PROTO_VERSION.
std::string banReasonToString() const
CAmount GetFeePerK() const
Information about a peer.
std::atomic< bool > fPingQueued
PIVX RPC command dispatcher.
bool appendCommand(const std::string &name, const CRPCCommand *pcmd)
Appends a CRPCCommand to the dispatch table.
std::string ToStringIPPort() const
const std::string & get_str() const
int64_t get_int64() const
bool push_back(const UniValue &val)
bool pushKV(const std::string &key, const UniValue &val)
bool randomize_credentials
const std::string CURRENCY_UNIT
std::unique_ptr< CConnman > g_connman
std::map< CNetAddr, LocalServiceInfo > mapLocalHost
RecursiveMutex cs_mapLocalHost
std::string strSubVersion
Subversion as sent to the P2P network in version messages.
bool IsReachable(enum Network net)
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats)
Get statistics from node state.
RecursiveMutex cs_main
Global state.
@ NET_MAX
Dummy value to indicate the number of NET_* constants.
@ NET_UNROUTABLE
Addresses from these networks are not publicly routable on the global Internet.
@ NET_INTERNAL
A set of addresses that represent the hash of a string or FQDN.
enum Network ParseNetwork(std::string net)
std::string GetNetworkName(enum Network net)
bool GetProxy(enum Network net, proxyType &proxyInfoOut)
bool LookupHost(const std::string &name, std::vector< CNetAddr > &vIP, unsigned int nMaxSolutions, bool fAllowLookup)
bool LookupSubNet(const std::string &strSubnet, CSubNet &ret)
boost::optional< T > Optional
Substitute for C++17 std::optional.
ServiceFlags
nServices flags
UniValue getpeerinfo(const JSONRPCRequest &request)
UniValue setban(const JSONRPCRequest &request)
UniValue setnetworkactive(const JSONRPCRequest &request)
UniValue clearbanned(const JSONRPCRequest &request)
UniValue getnettotals(const JSONRPCRequest &request)
void RegisterNetRPCCommands(CRPCTable &tableRPC)
Register P2P networking RPC commands.
UniValue getnetworkinfo(const JSONRPCRequest &request)
UniValue ping(const JSONRPCRequest &request)
UniValue listbanned(const JSONRPCRequest &request)
UniValue addnode(const JSONRPCRequest &request)
UniValue disconnectnode(const JSONRPCRequest &request)
UniValue getconnectioncount(const JSONRPCRequest &request)
UniValue getaddednodeinfo(const JSONRPCRequest &request)
UniValue JSONRPCError(int code, const std::string &message)
@ RPC_CLIENT_NODE_NOT_CONNECTED
Node has not been added before.
@ RPC_MISC_ERROR
General application defined errors.
@ RPC_CLIENT_NODE_ALREADY_ADDED
Still downloading initial blocks.
@ RPC_INVALID_PARAMETER
Ran out of memory during operation.
@ RPC_CLIENT_NODE_NOT_ADDED
Node is already added.
@ RPC_CLIENT_P2P_DISABLED
Invalid IP/Subnet.
UniValue ValueFromAmount(const CAmount &amount)
std::string HelpExampleCli(std::string methodname, std::string args)
std::string HelpExampleRpc(std::string methodname, std::string args)
std::vector< int > vHeightInFlight
uint64_t m_addr_rate_limited
uint64_t m_addr_processed
int64_t GetAdjustedTime()
int64_t GetTimeOffset()
"Never go to sea with two chronometers; take one or three." Our three time sources are:
const UniValue NullUniValue
int64_t GetTimeMillis()
Returns the system time (not mockable)
CFeeRate minRelayTxFee
Fees smaller than this (in upiv) are considered zero fee (for relaying, mining and transaction creati...
std::string GetWarnings(const std::string &strFor)