Branch data Line data Source code
1 : : // Copyright (c) 2009-present The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <rpc/register.h> // IWYU pragma: associated
6 : : #include <rpc/server.h>
7 : :
8 : : #include <addrman.h>
9 : : #include <addrman_impl.h>
10 : : #include <banman.h>
11 : : #include <chainparams.h>
12 : : #include <clientversion.h>
13 : : #include <core_io.h>
14 : : #include <crypto/hex_base.h>
15 : : #include <net.h>
16 : : #include <net_permissions.h>
17 : : #include <net_processing.h>
18 : : #include <net_types.h>
19 : : #include <netaddress.h>
20 : : #include <netbase.h>
21 : : #include <netgroup.h>
22 : : #include <node/connection_types.h>
23 : : #include <node/context.h>
24 : : #include <node/protocol_version.h>
25 : : #include <node/warnings.h>
26 : : #include <policy/feerate.h>
27 : : #include <protocol.h>
28 : : #include <rpc/protocol.h>
29 : : #include <rpc/request.h>
30 : : #include <rpc/server_util.h>
31 : : #include <rpc/util.h>
32 : : #include <semaphore_grant.h>
33 : : #include <sync.h>
34 : : #include <tinyformat.h>
35 : : #include <txmempool.h>
36 : : #include <uint256.h>
37 : : #include <univalue.h>
38 : : #include <util/chaintype.h>
39 : : #include <util/check.h>
40 : : #include <util/strencodings.h>
41 : : #include <util/string.h>
42 : : #include <util/time.h>
43 : : #include <validation.h>
44 : : #ifdef ENABLE_EMBEDDED_ASMAP
45 : : #include <common/args.h>
46 : : #include <node/data/ip_asn.dat.h>
47 : : #include <streams.h>
48 : : #include <util/asmap.h>
49 : : #include <util/fs.h>
50 : : #endif
51 : :
52 : : #include <atomic>
53 : : #include <compare>
54 : : #include <cstdint>
55 : : #include <map>
56 : : #include <memory>
57 : : #include <optional>
58 : : #include <span>
59 : : #include <sstream>
60 : : #include <stdexcept>
61 : : #include <string>
62 : : #include <string_view>
63 : : #include <type_traits>
64 : : #include <utility>
65 : : #include <vector>
66 : :
67 : : using node::NodeContext;
68 : : using util::Join;
69 : : using util::TrimStringView;
70 : :
71 : : const std::vector<std::string> CONNECTION_TYPE_DOC{
72 : : "outbound-full-relay (default automatic connections)",
73 : : "block-relay-only (does not relay transactions or addresses)",
74 : : "inbound (initiated by the peer)",
75 : : "manual (added via addnode RPC or -addnode/-connect configuration options)",
76 : : "addr-fetch (short-lived automatic connection for soliciting addresses)",
77 : : "feeler (short-lived automatic connection for testing addresses)",
78 : : "private-broadcast (short-lived automatic connection for broadcasting privacy-sensitive transactions)"
79 : : };
80 : :
81 : : const std::vector<std::string> TRANSPORT_TYPE_DOC{
82 : : "detecting (peer could be v1 or v2)",
83 : : "v1 (plaintext transport protocol)",
84 : : "v2 (BIP324 encrypted transport protocol)"
85 : : };
86 : :
87 : 2543 : static RPCMethod getconnectioncount()
88 : : {
89 : 2543 : return RPCMethod{
90 : 2543 : "getconnectioncount",
91 [ + - ]: 5086 : "Returns the number of connections to other nodes.\n",
92 : : {},
93 [ + - ]: 5086 : RPCResult{
94 [ + - ]: 5086 : RPCResult::Type::NUM, "", "The connection count"
95 [ + - ]: 5086 : },
96 : 2543 : RPCExamples{
97 [ + - + - : 5086 : HelpExampleCli("getconnectioncount", "")
+ - ]
98 [ + - + - : 10172 : + HelpExampleRpc("getconnectioncount", "")
+ - + - ]
99 [ + - ]: 2543 : },
100 : 2543 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
101 : : {
102 : 7 : NodeContext& node = EnsureAnyNodeContext(request.context);
103 : 7 : const CConnman& connman = EnsureConnman(node);
104 : :
105 : 7 : return connman.GetNodeCount(ConnectionDirection::Both);
106 : : },
107 [ + - + - ]: 10172 : };
108 : : }
109 : :
110 : 2541 : static RPCMethod ping()
111 : : {
112 : 2541 : return RPCMethod{
113 : 2541 : "ping",
114 [ + - ]: 5082 : "Requests that a ping be sent to all other nodes, to measure ping time.\n"
115 : : "Results are provided in getpeerinfo.\n"
116 : : "Ping command is handled in queue with all other commands, so it measures processing backlog, not just network ping.\n",
117 : : {},
118 [ + - + - : 5082 : RPCResult{RPCResult::Type::NONE, "", ""},
+ - ]
119 : 2541 : RPCExamples{
120 [ + - + - : 5082 : HelpExampleCli("ping", "")
+ - ]
121 [ + - + - : 10164 : + HelpExampleRpc("ping", "")
+ - + - ]
122 [ + - ]: 2541 : },
123 : 2541 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
124 : : {
125 : 5 : NodeContext& node = EnsureAnyNodeContext(request.context);
126 : 5 : PeerManager& peerman = EnsurePeerman(node);
127 : :
128 : : // Request that each node send a ping during next message processing pass
129 : 5 : peerman.SendPings();
130 : 5 : return UniValue::VNULL;
131 : : },
132 [ + - + - ]: 10164 : };
133 : : }
134 : :
135 : : /** Returns, given services flags, a list of humanly readable (known) network services */
136 : 14673 : static UniValue GetServicesNames(ServiceFlags services)
137 : : {
138 : 14673 : UniValue servicesNames(UniValue::VARR);
139 : :
140 [ + - + + ]: 52272 : for (const auto& flag : serviceFlagsToStr(services)) {
141 [ + - + - ]: 37599 : servicesNames.push_back(flag);
142 : 14673 : }
143 : :
144 : 14673 : return servicesNames;
145 : 0 : }
146 : :
147 : 9609 : static RPCMethod getpeerinfo()
148 : : {
149 : 9609 : return RPCMethod{
150 : 9609 : "getpeerinfo",
151 [ + - ]: 19218 : "Returns data about each connected network peer as a json array of objects.",
152 : : {},
153 [ + - ]: 19218 : RPCResult{
154 [ + - ]: 19218 : RPCResult::Type::ARR, "", "",
155 : : {
156 [ + - + - ]: 19218 : {RPCResult::Type::OBJ, "", "",
157 : : {
158 : : {
159 [ + - + - ]: 19218 : {RPCResult::Type::NUM, "id", "Peer index"},
160 [ + - + - ]: 19218 : {RPCResult::Type::STR, "addr", "(host:port) The IP address/hostname optionally followed by :port of the peer"},
161 [ + - + - ]: 19218 : {RPCResult::Type::STR, "addrbind", /*optional=*/true, "(ip:port) Bind address of the connection to the peer"},
162 [ + - + - ]: 19218 : {RPCResult::Type::STR, "addrlocal", /*optional=*/true, "(ip:port) Local address as reported by the peer"},
163 [ + - + - : 19218 : {RPCResult::Type::STR, "network", "Network (" + Join(GetNetworkNames(/*append_unroutable=*/true), ", ") + ")"},
+ - + - ]
164 [ + - + - ]: 19218 : {RPCResult::Type::NUM, "mapped_as", /*optional=*/true, "Mapped AS (Autonomous System) number at the end of the BGP route to the peer, used for diversifying\n"
165 : : "peer selection (only displayed if the -asmap config option is set)"},
166 [ + - + - ]: 19218 : {RPCResult::Type::STR_HEX, "services", "The services offered"},
167 [ + - + - ]: 19218 : {RPCResult::Type::ARR, "servicesnames", "the services offered, in human-readable form",
168 : : {
169 [ + - + - ]: 19218 : {RPCResult::Type::STR, "SERVICE_NAME", "the service name if it is recognised"}
170 : : }},
171 [ + - + - ]: 19218 : {RPCResult::Type::BOOL, "relaytxes", "Whether we relay transactions to this peer"},
172 [ + - + - ]: 19218 : {RPCResult::Type::NUM, "last_inv_sequence", "Mempool sequence number of this peer's last INV"},
173 [ + - + - ]: 19218 : {RPCResult::Type::NUM, "inv_to_send", "How many txs we have queued to announce to this peer"},
174 [ + - + - ]: 19218 : {RPCResult::Type::NUM_TIME, "lastsend", "The " + UNIX_EPOCH_TIME + " of the last send"},
175 [ + - + - ]: 19218 : {RPCResult::Type::NUM_TIME, "lastrecv", "The " + UNIX_EPOCH_TIME + " of the last receive"},
176 [ + - + - ]: 19218 : {RPCResult::Type::NUM_TIME, "last_transaction", "The " + UNIX_EPOCH_TIME + " of the last valid transaction received from this peer"},
177 [ + - + - ]: 19218 : {RPCResult::Type::NUM_TIME, "last_block", "The " + UNIX_EPOCH_TIME + " of the last block received from this peer"},
178 [ + - + - ]: 19218 : {RPCResult::Type::NUM_TIME, "last_block_announcement", "The " + UNIX_EPOCH_TIME + " this peer was first to announce a block"},
179 [ + - + - ]: 19218 : {RPCResult::Type::NUM, "bytessent", "The total bytes sent"},
180 [ + - + - ]: 19218 : {RPCResult::Type::NUM, "bytesrecv", "The total bytes received"},
181 [ + - + - ]: 19218 : {RPCResult::Type::NUM_TIME, "conntime", "The " + UNIX_EPOCH_TIME + " of the connection"},
182 [ + - + - ]: 19218 : {RPCResult::Type::NUM, "timeoffset", "The time offset in seconds"},
183 [ + - + - ]: 19218 : {RPCResult::Type::NUM, "pingtime", /*optional=*/true, "The last ping time in seconds, if any"},
184 [ + - + - ]: 19218 : {RPCResult::Type::NUM, "minping", /*optional=*/true, "The minimum observed ping time in seconds, if any"},
185 [ + - + - ]: 19218 : {RPCResult::Type::NUM, "pingwait", /*optional=*/true, "The duration in seconds of an outstanding ping (if non-zero)"},
186 [ + - + - ]: 19218 : {RPCResult::Type::NUM, "version", "The peer version, such as 70001"},
187 [ + - + - ]: 19218 : {RPCResult::Type::STR, "subver", "The string version"},
188 [ + - + - ]: 19218 : {RPCResult::Type::BOOL, "inbound", "Inbound (true) or Outbound (false)"},
189 [ + - + - ]: 9609 : {RPCResult::Type::BOOL, "bip152_hb_to", "Whether we selected peer as (compact blocks) high-bandwidth peer"},
190 [ + - + - ]: 19218 : {RPCResult::Type::BOOL, "bip152_hb_from", "Whether peer selected us as (compact blocks) high-bandwidth peer"},
191 [ + - + - ]: 19218 : {RPCResult::Type::NUM, "presynced_headers", "The current height of header pre-synchronization with this peer, or -1 if no low-work sync is in progress"},
192 [ + - + - ]: 19218 : {RPCResult::Type::NUM, "synced_headers", "The last header we have in common with this peer"},
193 [ + - + - ]: 19218 : {RPCResult::Type::NUM, "synced_blocks", "The last block we have in common with this peer"},
194 [ + - + - ]: 19218 : {RPCResult::Type::ARR, "inflight", "",
195 : : {
196 [ + - + - ]: 19218 : {RPCResult::Type::NUM, "n", "The heights of blocks we're currently asking from this peer"},
197 : : }},
198 [ + - + - ]: 19218 : {RPCResult::Type::BOOL, "addr_relay_enabled", "Whether we participate in address relay with this peer"},
199 [ + - + - ]: 19218 : {RPCResult::Type::NUM, "addr_processed", "The total number of addresses processed, excluding those dropped due to rate limiting"},
200 [ + - + - ]: 19218 : {RPCResult::Type::NUM, "addr_rate_limited", "The total number of addresses dropped due to rate limiting"},
201 [ + - + - ]: 19218 : {RPCResult::Type::ARR, "permissions", "Any special permissions that have been granted to this peer",
202 : : {
203 [ + - + - ]: 19218 : {RPCResult::Type::STR, "permission_type", Join(NET_PERMISSIONS_DOC, ",\n") + ".\n"},
204 : : }},
205 [ + - + - ]: 19218 : {RPCResult::Type::STR_AMOUNT, "minfeefilter", "The minimum fee rate for transactions this peer accepts"},
206 [ + - + - ]: 19218 : {RPCResult::Type::OBJ_DYN, "bytessent_per_msg", "",
207 : : {
208 [ + - + - ]: 19218 : {RPCResult::Type::NUM, "msg", "The total bytes sent aggregated by message type\n"
209 : : "When a message type is not listed in this json object, the bytes sent are 0.\n"
210 : : "Only known message types can appear as keys in the object."}
211 : : }},
212 [ + - + - ]: 19218 : {RPCResult::Type::OBJ_DYN, "bytesrecv_per_msg", "",
213 : : {
214 [ + - ]: 19218 : {RPCResult::Type::NUM, "msg", "The total bytes received aggregated by message type\n"
215 : : "When a message type is not listed in this json object, the bytes received are 0.\n"
216 : : "Only known message types can appear as keys in the object and all bytes received\n"
217 [ + - ]: 19218 : "of unknown message types are listed under '"+NET_MESSAGE_TYPE_OTHER+"'."}
218 : : }},
219 [ + - + - : 19218 : {RPCResult::Type::STR, "connection_type", "Type of connection: \n" + Join(CONNECTION_TYPE_DOC, ",\n") + ".\n"
+ - ]
220 : : "Please note this output is unlikely to be stable in upcoming releases as we iterate to\n"
221 : 9609 : "best capture connection behaviors."},
222 [ + - + - : 19218 : {RPCResult::Type::STR, "transport_protocol_type", "Type of transport protocol: \n" + Join(TRANSPORT_TYPE_DOC, ",\n") + ".\n"},
+ - ]
223 [ + - + - ]: 19218 : {RPCResult::Type::STR, "session_id", "The session ID for this connection, or \"\" if there is none (\"v2\" transport protocol only).\n"},
224 : : }},
225 : : }},
226 [ + - + - : 999336 : },
+ - + - +
- + - + -
+ - + + +
+ + + + +
+ + + + +
+ - - - -
- - - - -
- - - -
- ]
227 : 9609 : RPCExamples{
228 [ + - + - : 19218 : HelpExampleCli("getpeerinfo", "")
+ - ]
229 [ + - + - : 38436 : + HelpExampleRpc("getpeerinfo", "")
+ - + - ]
230 [ + - ]: 9609 : },
231 : 9609 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
232 : : {
233 : 7071 : NodeContext& node = EnsureAnyNodeContext(request.context);
234 : 7071 : const CConnman& connman = EnsureConnman(node);
235 : 7071 : const PeerManager& peerman = EnsurePeerman(node);
236 : :
237 : 7071 : std::vector<CNodeStats> vstats;
238 [ + - ]: 7071 : connman.GetNodeStats(vstats);
239 : :
240 : 7071 : UniValue ret(UniValue::VARR);
241 : :
242 [ + + ]: 20729 : for (const CNodeStats& stats : vstats) {
243 : 13658 : UniValue obj(UniValue::VOBJ);
244 [ + - ]: 13658 : CNodeStateStats statestats;
245 [ + - ]: 13658 : bool fStateStats = peerman.GetNodeStateStats(stats.nodeid, statestats);
246 : : // GetNodeStateStats() requires the existence of a CNodeState and a Peer object
247 : : // to succeed for this peer. These are created at connection initialisation and
248 : : // exist for the duration of the connection - except if there is a race where the
249 : : // peer got disconnected in between the GetNodeStats() and the GetNodeStateStats()
250 : : // calls. In this case, the peer doesn't need to be reported here.
251 [ + + ]: 13658 : if (!fStateStats) {
252 : 1 : continue;
253 : : }
254 [ + - + - : 27314 : obj.pushKV("id", stats.nodeid);
+ - ]
255 [ + - + - : 27314 : obj.pushKV("addr", stats.m_addr_name);
+ - ]
256 [ + - + + ]: 13657 : if (stats.addrBind.IsValid()) {
257 [ + - + - : 27300 : obj.pushKV("addrbind", stats.addrBind.ToStringAddrPort());
+ - + - ]
258 : : }
259 [ + + ]: 13657 : if (!(stats.addrLocal.empty())) {
260 [ + - + - : 9322 : obj.pushKV("addrlocal", stats.addrLocal);
+ - ]
261 : : }
262 [ + - + - : 27314 : obj.pushKV("network", GetNetworkName(stats.m_network));
+ - + - ]
263 [ - + ]: 13657 : if (stats.m_mapped_as != 0) {
264 [ # # # # : 0 : obj.pushKV("mapped_as", stats.m_mapped_as);
# # ]
265 : : }
266 : 13657 : ServiceFlags services{statestats.their_services};
267 [ + - + - : 27314 : obj.pushKV("services", strprintf("%016x", services));
+ - + - ]
268 [ + - + - : 27314 : obj.pushKV("servicesnames", GetServicesNames(services));
+ - ]
269 [ + - + - : 27314 : obj.pushKV("relaytxes", statestats.m_relay_txs);
+ - ]
270 [ + - + - : 27314 : obj.pushKV("last_inv_sequence", statestats.m_last_inv_seq);
+ - ]
271 [ + - + - : 27314 : obj.pushKV("inv_to_send", statestats.m_inv_to_send);
+ - ]
272 [ + - + - : 27314 : obj.pushKV("lastsend", TicksSinceEpoch<std::chrono::seconds>(stats.m_last_send));
+ - ]
273 [ + - + - : 27314 : obj.pushKV("lastrecv", TicksSinceEpoch<std::chrono::seconds>(stats.m_last_recv));
+ - ]
274 [ + - + - : 27314 : obj.pushKV("last_transaction", count_seconds(stats.m_last_tx_time));
+ - ]
275 [ + - + - : 27314 : obj.pushKV("last_block", count_seconds(stats.m_last_block_time));
+ - ]
276 [ + - + - : 27314 : obj.pushKV("last_block_announcement", TicksSinceEpoch<std::chrono::seconds>(statestats.m_last_block_announcement));
+ - ]
277 [ + - + - : 27314 : obj.pushKV("bytessent", stats.nSendBytes);
+ - ]
278 [ + - + - : 27314 : obj.pushKV("bytesrecv", stats.nRecvBytes);
+ - ]
279 [ + - + - : 27314 : obj.pushKV("conntime", TicksSinceEpoch<std::chrono::seconds>(stats.m_connected));
+ - ]
280 [ + - + - : 27314 : obj.pushKV("timeoffset", Ticks<std::chrono::seconds>(statestats.time_offset));
+ - ]
281 [ + + ]: 13657 : if (stats.m_last_ping_time > 0us) {
282 [ + - + - : 19326 : obj.pushKV("pingtime", Ticks<SecondsDouble>(stats.m_last_ping_time));
+ - ]
283 : : }
284 [ + + ]: 13657 : if (stats.m_min_ping_time < decltype(CNode::m_min_ping_time.load())::max()) {
285 [ + - + - : 25598 : obj.pushKV("minping", Ticks<SecondsDouble>(stats.m_min_ping_time));
+ - ]
286 : : }
287 [ + + ]: 13657 : if (statestats.m_ping_wait > 0s) {
288 [ + - + - : 106 : obj.pushKV("pingwait", Ticks<SecondsDouble>(statestats.m_ping_wait));
+ - ]
289 : : }
290 [ + - + - : 27314 : obj.pushKV("version", stats.nVersion);
+ - ]
291 : : // Use the sanitized form of subver here, to avoid tricksy remote peers from
292 : : // corrupting or modifying the JSON output by putting special characters in
293 : : // their ver message.
294 [ + - + - : 27314 : obj.pushKV("subver", stats.cleanSubVer);
+ - ]
295 [ + - + - : 27314 : obj.pushKV("inbound", stats.fInbound);
+ - ]
296 [ + - + - : 27314 : obj.pushKV("bip152_hb_to", stats.m_bip152_highbandwidth_to);
+ - ]
297 [ + - + - : 27314 : obj.pushKV("bip152_hb_from", stats.m_bip152_highbandwidth_from);
+ - ]
298 [ + - + - : 27314 : obj.pushKV("presynced_headers", statestats.presync_height);
+ - ]
299 [ + - + - : 27314 : obj.pushKV("synced_headers", statestats.nSyncHeight);
+ - ]
300 [ + - + - : 27314 : obj.pushKV("synced_blocks", statestats.nCommonHeight);
+ - ]
301 : 13657 : UniValue heights(UniValue::VARR);
302 [ + + ]: 18982 : for (const int height : statestats.vHeightInFlight) {
303 [ + - + - ]: 5325 : heights.push_back(height);
304 : : }
305 [ + - + - ]: 27314 : obj.pushKV("inflight", std::move(heights));
306 [ + - + - : 27314 : obj.pushKV("addr_relay_enabled", statestats.m_addr_relay_enabled);
+ - ]
307 [ + - + - : 27314 : obj.pushKV("addr_processed", statestats.m_addr_processed);
+ - ]
308 [ + - + - : 27314 : obj.pushKV("addr_rate_limited", statestats.m_addr_rate_limited);
+ - ]
309 : 13657 : UniValue permissions(UniValue::VARR);
310 [ + - + + ]: 18481 : for (const auto& permission : NetPermissions::ToStrings(stats.m_permission_flags)) {
311 [ + - + - ]: 4824 : permissions.push_back(permission);
312 : 13657 : }
313 [ + - + - ]: 27314 : obj.pushKV("permissions", std::move(permissions));
314 [ + - + - : 27314 : obj.pushKV("minfeefilter", ValueFromAmount(statestats.m_fee_filter_received));
+ - ]
315 : :
316 : 13657 : UniValue sendPerMsgType(UniValue::VOBJ);
317 [ + - + + ]: 160393 : for (const auto& [message_type, total_bytes] : stats.mapSendBytesPerMsgType) {
318 [ + - ]: 146736 : if (total_bytes > 0) {
319 [ + - + - ]: 440208 : sendPerMsgType.pushKVEnd(message_type, total_bytes);
320 : : }
321 : : }
322 [ + - + - ]: 27314 : obj.pushKV("bytessent_per_msg", std::move(sendPerMsgType));
323 : :
324 : 13657 : UniValue recvPerMsgType(UniValue::VOBJ);
325 [ + + + + ]: 518966 : for (const auto& [message_type, total_bytes] : stats.mapRecvBytesPerMsgType) {
326 [ + + ]: 505309 : if (total_bytes > 0) {
327 [ + - + - ]: 385911 : recvPerMsgType.pushKVEnd(message_type, total_bytes);
328 : : }
329 : : }
330 [ + - + - ]: 27314 : obj.pushKV("bytesrecv_per_msg", std::move(recvPerMsgType));
331 [ + - + - : 27314 : obj.pushKV("connection_type", ConnectionTypeAsString(stats.m_conn_type));
+ - + - ]
332 [ + - + - : 27314 : obj.pushKV("transport_protocol_type", TransportTypeAsString(stats.m_transport_type));
+ - + - ]
333 [ + - + - : 27314 : obj.pushKV("session_id", stats.m_session_id);
+ - ]
334 : :
335 [ + - ]: 13657 : ret.push_back(std::move(obj));
336 : 13658 : }
337 : :
338 : 7071 : return ret;
339 : 7071 : },
340 [ + - + - ]: 38436 : };
341 [ + - + - : 922464 : }
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - - - ]
342 : :
343 : 3054 : static RPCMethod addnode()
344 : : {
345 : 3054 : return RPCMethod{
346 : 3054 : "addnode",
347 : : "Attempts to add or remove a node from the addnode list.\n"
348 : : "Or try a connection to a node once.\n"
349 : : "Nodes added using addnode (or -connect) are protected from DoS disconnection and IBD block stalling\n"
350 : : "disconnection, and are not required to be full nodes or support SegWit as other outbound peers are (though\n"
351 : 3054 : "such peers will not be synced from).\n" +
352 [ + - + - ]: 9162 : strprintf("Addnode connections are limited to %u at a time", MAX_ADDNODE_CONNECTIONS) +
353 : 3054 : " and are counted separately from the -maxconnections limit.\n",
354 : : {
355 [ + - + - ]: 6108 : {"node", RPCArg::Type::STR, RPCArg::Optional::NO, "The IP address/hostname optionally followed by :port of the peer to connect to"},
356 [ + - + - ]: 6108 : {"command", RPCArg::Type::STR, RPCArg::Optional::NO, "'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"},
357 [ + - + - : 9162 : {"v2transport", RPCArg::Type::BOOL, RPCArg::DefaultHint{"set by -v2transport"}, "Attempt to connect using BIP324 v2 transport protocol (ignored for 'remove' command)"},
+ - ]
358 : : },
359 [ + - + - : 6108 : RPCResult{RPCResult::Type::NONE, "", ""},
+ - + - ]
360 : 3054 : RPCExamples{
361 [ + - + - : 6108 : HelpExampleCli("addnode", "\"192.168.0.6:8333\" \"onetry\" true")
+ - ]
362 [ + - + - : 12216 : + HelpExampleRpc("addnode", R"("192.168.0.6:8333", "onetry", true)")
+ - + - ]
363 [ + - ]: 3054 : },
364 : 3054 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
365 : : {
366 : 518 : const auto command{self.Arg<std::string_view>("command")};
367 [ + + + + : 518 : if (command != "onetry" && command != "add" && command != "remove") {
+ + ]
368 : 2 : throw std::runtime_error(
369 [ + - + - ]: 4 : self.ToString());
370 : : }
371 : :
372 : 516 : NodeContext& node = EnsureAnyNodeContext(request.context);
373 : 516 : CConnman& connman = EnsureConnman(node);
374 : :
375 : 516 : const auto node_arg{self.Arg<std::string_view>("node")};
376 [ + + ]: 516 : if (TrimStringView(node_arg).empty()) {
377 : : // Such a node would never resolve, but would be retried indefinitely.
378 [ + - + - ]: 24 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Error: Node address cannot be empty");
379 : : }
380 : :
381 : 504 : bool node_v2transport = connman.GetLocalServices() & NODE_P2P_V2;
382 [ + + ]: 504 : bool use_v2transport = self.MaybeArg<bool>("v2transport").value_or(node_v2transport);
383 : :
384 [ + + + + ]: 504 : if (use_v2transport && !node_v2transport) {
385 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Error: v2transport requested but not enabled (see -v2transport)");
386 : : }
387 : :
388 [ + + ]: 503 : if (command == "onetry")
389 : : {
390 : 491 : CAddress addr;
391 [ + - ]: 982 : connman.OpenNetworkConnection(/*addrConnect=*/addr,
392 : : /*fCountFailure=*/false,
393 : : /*grant_outbound=*/{},
394 [ - + ]: 491 : /*pszDest=*/std::string{node_arg}.c_str(),
395 : : /*conn_type=*/ConnectionType::MANUAL,
396 : : /*use_v2transport=*/use_v2transport,
397 [ + - ]: 491 : /*proxy_override=*/std::nullopt);
398 : 491 : return UniValue::VNULL;
399 : 491 : }
400 : :
401 [ + + ]: 12 : if (command == "add")
402 : : {
403 [ + + ]: 8 : if (!connman.AddNode({std::string{node_arg}, use_v2transport})) {
404 [ + - + - ]: 8 : throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: Node already added");
405 : : }
406 : : }
407 [ + - ]: 4 : else if (command == "remove")
408 : : {
409 [ + + ]: 4 : if (!connman.RemoveAddedNode(node_arg)) {
410 [ + - + - ]: 4 : throw JSONRPCError(RPC_CLIENT_NODE_NOT_ADDED, "Error: Node could not be removed. It has not been added previously.");
411 : : }
412 : : }
413 : :
414 : 6 : return UniValue::VNULL;
415 [ + - ]: 8 : },
416 [ + - + - : 21378 : };
+ + - - ]
417 [ + - + - : 18324 : }
+ - - - ]
418 : :
419 : 2690 : static RPCMethod addconnection()
420 : : {
421 : 2690 : return RPCMethod{
422 : 2690 : "addconnection",
423 [ + - ]: 5380 : "Open an outbound connection to a specified node. This RPC is for testing only.\n",
424 : : {
425 [ + - + - ]: 5380 : {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The IP address and port to attempt connecting to."},
426 [ + - + - ]: 5380 : {"connection_type", RPCArg::Type::STR, RPCArg::Optional::NO, "Type of connection to open (\"outbound-full-relay\", \"block-relay-only\", \"addr-fetch\", \"feeler\" or \"manual\")."},
427 [ + - + - ]: 5380 : {"v2transport", RPCArg::Type::BOOL, RPCArg::Optional::NO, "Attempt to connect using BIP324 v2 transport protocol"},
428 : : },
429 [ + - ]: 5380 : RPCResult{
430 [ + - + - ]: 5380 : RPCResult::Type::OBJ, "", "",
431 : : {
432 [ + - + - ]: 5380 : { RPCResult::Type::STR, "address", "Address of newly added connection." },
433 [ + - + - ]: 5380 : { RPCResult::Type::STR, "connection_type", "Type of connection opened." },
434 [ + - + - : 16140 : }},
+ + - - ]
435 : 2690 : RPCExamples{
436 [ + - + - : 5380 : HelpExampleCli("addconnection", "\"192.168.0.6:8333\" \"outbound-full-relay\" true")
+ - ]
437 [ + - + - : 10760 : + HelpExampleRpc("addconnection", R"("192.168.0.6:8333", "outbound-full-relay", true)")
+ - + - ]
438 [ + - ]: 2690 : },
439 : 2690 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
440 : : {
441 [ - + ]: 165 : if (Params().GetChainType() != ChainType::REGTEST) {
442 [ # # ]: 0 : throw std::runtime_error("addconnection is for regression testing (-regtest mode) only.");
443 : : }
444 : :
445 [ - + ]: 165 : const std::string address = request.params[0].get_str();
446 [ + - + - ]: 165 : auto conn_type_in{util::TrimStringView(self.Arg<std::string_view>("connection_type"))};
447 : 165 : ConnectionType conn_type{};
448 [ + + ]: 165 : if (conn_type_in == "outbound-full-relay") {
449 : : conn_type = ConnectionType::OUTBOUND_FULL_RELAY;
450 [ + + ]: 59 : } else if (conn_type_in == "block-relay-only") {
451 : : conn_type = ConnectionType::BLOCK_RELAY;
452 [ + + ]: 23 : } else if (conn_type_in == "addr-fetch") {
453 : : conn_type = ConnectionType::ADDR_FETCH;
454 [ + + ]: 8 : } else if (conn_type_in == "feeler") {
455 : : conn_type = ConnectionType::FEELER;
456 [ - + ]: 3 : } else if (conn_type_in == "manual") {
457 : : conn_type = ConnectionType::MANUAL;
458 : : } else {
459 [ # # # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, self.ToString());
460 : : }
461 [ + - ]: 165 : bool use_v2transport{self.Arg<bool>("v2transport")};
462 : :
463 [ + - ]: 165 : NodeContext& node = EnsureAnyNodeContext(request.context);
464 [ + - ]: 165 : CConnman& connman = EnsureConnman(node);
465 : :
466 [ + + + - : 165 : if (use_v2transport && !(connman.GetLocalServices() & NODE_P2P_V2)) {
- + ]
467 [ # # # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Error: Adding v2transport connections requires -v2transport init flag to be set.");
468 : : }
469 : :
470 [ + - ]: 165 : const bool success = connman.AddConnection(address, conn_type, use_v2transport);
471 [ - + ]: 165 : if (!success) {
472 [ # # # # ]: 0 : throw JSONRPCError(RPC_CLIENT_NODE_CAPACITY_REACHED, "Error: Already at capacity for specified connection type.");
473 : : }
474 : :
475 : 165 : UniValue info(UniValue::VOBJ);
476 [ + - + - : 330 : info.pushKV("address", address);
+ - ]
477 [ + - + - : 330 : info.pushKV("connection_type", conn_type_in);
+ - ]
478 : :
479 : 165 : return info;
480 : 165 : },
481 [ + - + - : 18830 : };
+ + - - ]
482 [ + - + - : 26900 : }
+ - + - +
- - - -
- ]
483 : :
484 : 2669 : static RPCMethod disconnectnode()
485 : : {
486 : 2669 : return RPCMethod{
487 : 2669 : "disconnectnode",
488 [ + - ]: 5338 : "Immediately disconnects from the specified peer node.\n"
489 : : "\nStrictly one out of 'address' and 'nodeid' can be provided to identify the node.\n"
490 : : "\nTo disconnect by nodeid, either set 'address' to the empty string, or call using the named 'nodeid' argument only.\n",
491 : : {
492 [ + - + - : 8007 : {"address", RPCArg::Type::STR, RPCArg::DefaultHint{"fallback to nodeid"}, "The IP address/port of the node"},
+ - ]
493 [ + - + - : 8007 : {"nodeid", RPCArg::Type::NUM, RPCArg::DefaultHint{"fallback to address"}, "The node ID (see getpeerinfo for node IDs)"},
+ - ]
494 : : },
495 [ + - + - : 5338 : RPCResult{RPCResult::Type::NONE, "", ""},
+ - + - ]
496 : 2669 : RPCExamples{
497 [ + - + - : 5338 : HelpExampleCli("disconnectnode", "\"192.168.0.6:8333\"")
+ - ]
498 [ + - + - : 10676 : + HelpExampleCli("disconnectnode", "\"\" 1")
+ - + - ]
499 [ + - + - : 10676 : + HelpExampleRpc("disconnectnode", "\"192.168.0.6:8333\"")
+ - + - ]
500 [ + - + - : 10676 : + HelpExampleRpc("disconnectnode", "\"\", 1")
+ - + - ]
501 [ + - ]: 2669 : },
502 : 2669 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
503 : : {
504 : 133 : NodeContext& node = EnsureAnyNodeContext(request.context);
505 : 133 : CConnman& connman = EnsureConnman(node);
506 : :
507 : 133 : bool success;
508 : 133 : auto address{self.MaybeArg<std::string_view>("address")};
509 : 133 : auto node_id{self.MaybeArg<int64_t>("nodeid")};
510 : :
511 [ + + + + ]: 133 : if (address && !node_id) {
512 : : /* handle disconnect-by-address */
513 : 4 : success = connman.DisconnectNode(*address);
514 [ + - + + : 129 : } else if (node_id && (!address || address->empty())) {
- + ]
515 : : /* handle disconnect-by-id */
516 : 127 : success = connman.DisconnectNode(*node_id);
517 : : } else {
518 [ + - + - ]: 4 : throw JSONRPCError(RPC_INVALID_PARAMS, "Only one of address and nodeid should be provided.");
519 : : }
520 : :
521 [ + + ]: 131 : if (!success) {
522 [ + - + - ]: 4 : throw JSONRPCError(RPC_CLIENT_NODE_NOT_CONNECTED, "Node not found in connected nodes");
523 : : }
524 : :
525 : 129 : return UniValue::VNULL;
526 : : },
527 [ + - + - : 16014 : };
+ + - - ]
528 [ + - + - : 10676 : }
- - ]
529 : :
530 : 2551 : static RPCMethod getaddednodeinfo()
531 : : {
532 : 2551 : return RPCMethod{
533 : 2551 : "getaddednodeinfo",
534 [ + - ]: 5102 : "Returns information about the given added node, or all added nodes\n"
535 : : "(note that onetry addnodes are not listed here)\n",
536 : : {
537 [ + - + - : 7653 : {"node", RPCArg::Type::STR, RPCArg::DefaultHint{"all nodes"}, "If provided, return information about this specific node, otherwise all nodes are returned."},
+ - ]
538 : : },
539 [ + - ]: 5102 : RPCResult{
540 [ + - + - ]: 5102 : RPCResult::Type::ARR, "", "",
541 : : {
542 [ + - + - ]: 5102 : {RPCResult::Type::OBJ, "", "",
543 : : {
544 [ + - + - ]: 5102 : {RPCResult::Type::STR, "addednode", "The node IP address or name (as provided to addnode)"},
545 [ + - + - ]: 5102 : {RPCResult::Type::BOOL, "connected", "If connected"},
546 [ + - + - ]: 5102 : {RPCResult::Type::ARR, "addresses", "Only when connected = true",
547 : : {
548 [ + - + - ]: 5102 : {RPCResult::Type::OBJ, "", "",
549 : : {
550 [ + - + - ]: 5102 : {RPCResult::Type::STR, "address", "The bitcoin server IP and port we're connected to"},
551 [ + - + - ]: 5102 : {RPCResult::Type::STR, "connected", "connection, inbound or outbound"},
552 : : }},
553 : : }},
554 : : }},
555 : : }
556 [ + - + - : 48469 : },
+ - + - +
- + + + +
+ + + + -
- - - - -
- - ]
557 : 2551 : RPCExamples{
558 [ + - + - : 5102 : HelpExampleCli("getaddednodeinfo", "\"192.168.0.201\"")
+ - ]
559 [ + - + - : 10204 : + HelpExampleRpc("getaddednodeinfo", "\"192.168.0.201\"")
+ - + - ]
560 [ + - ]: 2551 : },
561 : 2551 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
562 : : {
563 : 15 : NodeContext& node = EnsureAnyNodeContext(request.context);
564 : 15 : const CConnman& connman = EnsureConnman(node);
565 : :
566 : 15 : std::vector<AddedNodeInfo> vInfo = connman.GetAddedNodeInfo(/*include_connected=*/true);
567 : :
568 [ + - + + ]: 15 : if (auto node{self.MaybeArg<std::string_view>("node")}) {
569 : 4 : bool found = false;
570 [ + + ]: 6 : for (const AddedNodeInfo& info : vInfo) {
571 [ - + + + ]: 4 : if (info.m_params.m_added_node == *node) {
572 [ + - ]: 2 : vInfo.assign(1, info);
573 : : found = true;
574 : : break;
575 : : }
576 : : }
577 : 2 : if (!found) {
578 [ + - + - ]: 4 : throw JSONRPCError(RPC_CLIENT_NODE_NOT_ADDED, "Error: Node has not been added.");
579 : : }
580 : : }
581 : :
582 : 13 : UniValue ret(UniValue::VARR);
583 : :
584 [ + + ]: 26 : for (const AddedNodeInfo& info : vInfo) {
585 : 13 : UniValue obj(UniValue::VOBJ);
586 [ + - + - : 26 : obj.pushKV("addednode", info.m_params.m_added_node);
+ - ]
587 [ + - + - : 26 : obj.pushKV("connected", info.fConnected);
+ - ]
588 : 13 : UniValue addresses(UniValue::VARR);
589 [ - + ]: 13 : if (info.fConnected) {
590 : 0 : UniValue address(UniValue::VOBJ);
591 [ # # # # : 0 : address.pushKV("address", info.resolvedAddress.ToStringAddrPort());
# # # # ]
592 [ # # # # : 0 : address.pushKV("connected", info.fInbound ? "inbound" : "outbound");
# # # # ]
593 [ # # ]: 0 : addresses.push_back(std::move(address));
594 : 0 : }
595 [ + - + - ]: 26 : obj.pushKV("addresses", std::move(addresses));
596 [ + - ]: 13 : ret.push_back(std::move(obj));
597 : 13 : }
598 : :
599 : 13 : return ret;
600 : 15 : },
601 [ + - + - : 12755 : };
+ + - - ]
602 [ + - + - : 40816 : }
+ - + - +
- + - + -
+ - - - -
- ]
603 : :
604 : 2554 : static RPCMethod getnettotals()
605 : : {
606 : 2554 : return RPCMethod{"getnettotals",
607 [ + - ]: 5108 : "Returns information about network traffic, including bytes in, bytes out,\n"
608 : : "and current system time.",
609 : : {},
610 [ + - ]: 5108 : RPCResult{
611 [ + - ]: 5108 : RPCResult::Type::OBJ, "", "",
612 : : {
613 [ + - + - ]: 5108 : {RPCResult::Type::NUM, "totalbytesrecv", "Total bytes received"},
614 [ + - + - ]: 5108 : {RPCResult::Type::NUM, "totalbytessent", "Total bytes sent"},
615 [ + - + - ]: 5108 : {RPCResult::Type::NUM_TIME, "timemillis", "Current system " + UNIX_EPOCH_TIME + " in milliseconds"},
616 [ + - + - ]: 5108 : {RPCResult::Type::OBJ, "uploadtarget", "",
617 : : {
618 [ + - + - ]: 5108 : {RPCResult::Type::NUM, "timeframe", "Length of the measuring timeframe in seconds"},
619 [ + - + - ]: 5108 : {RPCResult::Type::NUM, "target", "Target in bytes"},
620 [ + - + - ]: 5108 : {RPCResult::Type::BOOL, "target_reached", "True if target is reached"},
621 [ + - + - ]: 5108 : {RPCResult::Type::BOOL, "serve_historical_blocks", "True if serving historical blocks"},
622 [ + - + - ]: 5108 : {RPCResult::Type::NUM, "bytes_left_in_cycle", "Bytes left in current time cycle"},
623 [ + - + - ]: 5108 : {RPCResult::Type::NUM, "time_left_in_cycle", "Seconds left in current time cycle"},
624 : : }},
625 : : }
626 [ + - + - : 58742 : },
+ - + + +
+ - - -
- ]
627 : 2554 : RPCExamples{
628 [ + - + - : 5108 : HelpExampleCli("getnettotals", "")
+ - ]
629 [ + - + - : 10216 : + HelpExampleRpc("getnettotals", "")
+ - + - ]
630 [ + - ]: 2554 : },
631 : 2554 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
632 : : {
633 : 18 : NodeContext& node = EnsureAnyNodeContext(request.context);
634 : 18 : const CConnman& connman = EnsureConnman(node);
635 : :
636 : 18 : UniValue obj(UniValue::VOBJ);
637 [ + - + - : 36 : obj.pushKV("totalbytesrecv", connman.GetTotalBytesRecv());
+ - + - ]
638 [ + - + - : 36 : obj.pushKV("totalbytessent", connman.GetTotalBytesSent());
+ - + - ]
639 [ + - + - : 36 : obj.pushKV("timemillis", TicksSinceEpoch<std::chrono::milliseconds>(SystemClock::now()));
+ - ]
640 : :
641 : 18 : UniValue outboundLimit(UniValue::VOBJ);
642 [ + - + - : 36 : outboundLimit.pushKV("timeframe", count_seconds(connman.GetMaxOutboundTimeframe()));
+ - + - ]
643 [ + - + - : 36 : outboundLimit.pushKV("target", connman.GetMaxOutboundTarget());
+ - + - ]
644 [ + - + - : 36 : outboundLimit.pushKV("target_reached", connman.OutboundTargetReached(false));
+ - + - ]
645 [ + - + - : 36 : outboundLimit.pushKV("serve_historical_blocks", !connman.OutboundTargetReached(true));
+ - + - ]
646 [ + - + - : 36 : outboundLimit.pushKV("bytes_left_in_cycle", connman.GetOutboundTargetBytesLeft());
+ - + - ]
647 [ + - + - : 36 : outboundLimit.pushKV("time_left_in_cycle", count_seconds(connman.GetMaxOutboundTimeLeftInCycle()));
+ - + - ]
648 [ + - + - ]: 36 : obj.pushKV("uploadtarget", std::move(outboundLimit));
649 : 18 : return obj;
650 : 18 : },
651 [ + - + - ]: 10216 : };
652 [ + - + - : 51080 : }
+ - + - +
- + - + -
+ - + - +
- - - -
- ]
653 : :
654 : 1016 : static UniValue GetNetworksInfo()
655 : : {
656 : 1016 : UniValue networks(UniValue::VARR);
657 [ + + ]: 8128 : for (int n = 0; n < NET_MAX; ++n) {
658 : 7112 : enum Network network = static_cast<enum Network>(n);
659 [ + + ]: 7112 : if (network == NET_UNROUTABLE || network == NET_INTERNAL) continue;
660 : 5080 : UniValue obj(UniValue::VOBJ);
661 [ + - + - : 10160 : obj.pushKV("name", GetNetworkName(network));
+ - + - ]
662 [ + - + - : 10160 : obj.pushKV("limited", !g_reachable_nets.Contains(network));
+ - + - ]
663 [ + - + - : 10160 : obj.pushKV("reachable", g_reachable_nets.Contains(network));
+ - + - ]
664 [ + - + + ]: 5080 : if (const auto proxy = GetProxy(network)) {
665 [ + - + - : 436 : obj.pushKV("proxy", proxy->ToString());
+ - + - ]
666 [ + - + - : 436 : obj.pushKV("proxy_randomize_credentials", proxy->m_tor_stream_isolation);
+ - ]
667 : : } else {
668 [ + - + - : 9724 : obj.pushKV("proxy", std::string());
+ - ]
669 [ + - + - : 9724 : obj.pushKV("proxy_randomize_credentials", false);
+ - ]
670 : 0 : }
671 [ + - ]: 5080 : networks.push_back(std::move(obj));
672 : 5080 : }
673 : 1016 : return networks;
674 : 0 : }
675 : :
676 : 3554 : static RPCMethod getnetworkinfo()
677 : : {
678 : 3554 : return RPCMethod{"getnetworkinfo",
679 [ + - ]: 7108 : "Returns an object containing various state info regarding P2P networking.\n",
680 : : {},
681 [ + - ]: 7108 : RPCResult{
682 [ + - ]: 7108 : RPCResult::Type::OBJ, "", "",
683 : : {
684 [ + - + - ]: 7108 : {RPCResult::Type::NUM, "version", "the server version"},
685 [ + - + - ]: 7108 : {RPCResult::Type::STR, "subversion", "the server subversion string"},
686 [ + - + - ]: 7108 : {RPCResult::Type::NUM, "protocolversion", "the protocol version"},
687 [ + - + - ]: 7108 : {RPCResult::Type::STR_HEX, "localservices", "the services we offer to the network"},
688 [ + - + - ]: 7108 : {RPCResult::Type::ARR, "localservicesnames", "the services we offer to the network, in human-readable form",
689 : : {
690 [ + - + - ]: 7108 : {RPCResult::Type::STR, "SERVICE_NAME", "the service name"},
691 : : }},
692 [ + - + - ]: 7108 : {RPCResult::Type::BOOL, "localrelay", "true if transaction relay is requested from peers"},
693 [ + - + - ]: 7108 : {RPCResult::Type::NUM, "timeoffset", "the time offset"},
694 [ + - + - ]: 7108 : {RPCResult::Type::NUM, "tx_send_rate", "configured target for maximum number of transactions per second to send to inbound peers"},
695 [ + - + - ]: 7108 : {RPCResult::Type::OBJ_DYN, "inv_buckets", "", {
696 [ + - + - ]: 7108 : {RPCResult::Type::OBJ, "inbound/outbound", "connection direction",
697 : : {
698 [ + - + - ]: 7108 : {RPCResult::Type::NUM, "backlog", "number of queued txs to announce"},
699 [ + - + - ]: 7108 : {RPCResult::Type::NUM, "count_tok", "tokens available to be consumed per-transaction"},
700 [ + - + - ]: 7108 : {RPCResult::Type::NUM, "size_tok", "tokens available to be consumed per-byte"},
701 : : }
702 : : }
703 : : }},
704 [ + - + - ]: 7108 : {RPCResult::Type::NUM, "connections", "the total number of connections"},
705 [ + - + - ]: 7108 : {RPCResult::Type::NUM, "connections_in", "the number of inbound connections"},
706 [ + - + - ]: 7108 : {RPCResult::Type::NUM, "connections_out", "the number of outbound connections"},
707 [ + - + - ]: 7108 : {RPCResult::Type::BOOL, "networkactive", "whether p2p networking is enabled"},
708 [ + - + - ]: 7108 : {RPCResult::Type::ARR, "networks", "information per network",
709 : : {
710 [ + - + - ]: 7108 : {RPCResult::Type::OBJ, "", "",
711 : : {
712 [ + - + - : 7108 : {RPCResult::Type::STR, "name", "network (" + Join(GetNetworkNames(), ", ") + ")"},
+ - + - ]
713 [ + - + - ]: 7108 : {RPCResult::Type::BOOL, "limited", "is the network limited using -onlynet?"},
714 [ + - + - ]: 7108 : {RPCResult::Type::BOOL, "reachable", "is the network reachable?"},
715 [ + - + - ]: 7108 : {RPCResult::Type::STR, "proxy", "(\"host:port\") the proxy that is used for this network, or empty if none"},
716 [ + - + - ]: 7108 : {RPCResult::Type::BOOL, "proxy_randomize_credentials", "Whether randomized credentials are used"},
717 : : }},
718 : : }},
719 [ + - + - ]: 7108 : {RPCResult::Type::STR_HEX, "asmap_version", /*optional=*/true, "the SHA256 hash of the asmap data used for IP bucketing (only displayed if the -asmap config option is set)"},
720 [ + - + - ]: 7108 : {RPCResult::Type::STR_AMOUNT, "relayfee", "minimum relay fee rate for transactions in " + CURRENCY_UNIT + "/kvB"},
721 [ + - + - ]: 7108 : {RPCResult::Type::STR_AMOUNT, "incrementalfee", "minimum fee rate increment for mempool limiting or replacement in " + CURRENCY_UNIT + "/kvB"},
722 [ + - + - ]: 7108 : {RPCResult::Type::ARR, "localaddresses", "list of local addresses",
723 : : {
724 [ + - + - ]: 7108 : {RPCResult::Type::OBJ, "", "",
725 : : {
726 [ + - + - ]: 7108 : {RPCResult::Type::STR, "address", "network address"},
727 [ + - + - ]: 7108 : {RPCResult::Type::NUM, "port", "network port"},
728 [ + - + - ]: 7108 : {RPCResult::Type::NUM, "score", "relative score"},
729 : : }},
730 : : }},
731 [ + - + - : 7108 : (IsDeprecatedRPCEnabled("warnings") ?
- + ]
732 [ - - - - : 3554 : RPCResult{RPCResult::Type::STR, "warnings", "any network and blockchain warnings (DEPRECATED)"} :
- + - + -
+ - - - -
- - ]
733 [ + - + - : 14216 : RPCResult{RPCResult::Type::ARR, "warnings", "any network and blockchain warnings (run with `-deprecatedrpc=warnings` to return the latest warning as a single string)",
+ - + - -
- - - ]
734 : : {
735 [ + - + - : 14216 : {RPCResult::Type::STR, "", "warning"},
+ - + - -
- - - ]
736 : : }
737 [ + - + - : 17770 : }
+ - + + +
- - - - -
- - - - ]
738 : : ),
739 : : }
740 [ + - + - : 270104 : },
+ - + - +
- + - + -
+ - + - +
+ + + + +
+ + + + +
+ + + + +
- - - - -
- - - - -
- - - - -
- ]
741 : 3554 : RPCExamples{
742 [ + - + - : 7108 : HelpExampleCli("getnetworkinfo", "")
+ - ]
743 [ + - + - : 14216 : + HelpExampleRpc("getnetworkinfo", "")
+ - + - ]
744 [ + - ]: 3554 : },
745 : 3554 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
746 : : {
747 : 1016 : LOCK(cs_main);
748 : 1016 : UniValue obj(UniValue::VOBJ);
749 [ + - + - : 2032 : obj.pushKV("version", CLIENT_VERSION);
+ - ]
750 [ + - + - : 2032 : obj.pushKV("subversion", strSubVersion);
+ - ]
751 [ + - + - : 2032 : obj.pushKV("protocolversion",PROTOCOL_VERSION);
+ - ]
752 [ + - ]: 1016 : NodeContext& node = EnsureAnyNodeContext(request.context);
753 [ + - ]: 1016 : CConnman& connman = EnsureConnman(node);
754 [ + - ]: 1016 : ServiceFlags services = connman.GetLocalServices();
755 [ + - + - : 2032 : obj.pushKV("localservices", strprintf("%016x", services));
+ - + - ]
756 [ + - + - : 2032 : obj.pushKV("localservicesnames", GetServicesNames(services));
+ - ]
757 [ + - + - ]: 1016 : auto peerman_info{EnsurePeerman(node).GetInfo()};
758 [ + - + - : 2032 : obj.pushKV("localrelay", !peerman_info.ignores_incoming_txs);
+ - ]
759 [ + - + - : 2032 : obj.pushKV("timeoffset", Ticks<std::chrono::seconds>(peerman_info.median_outbound_time_offset));
+ - ]
760 [ + - + - : 2032 : obj.pushKV("tx_send_rate", peerman_info.tx_send_rate);
+ - ]
761 : 2032 : auto buckjson = [&](const auto& buckinfo) {
762 : 2032 : UniValue b{UniValue::VOBJ};
763 [ + - + - : 4064 : b.pushKV("backlog", buckinfo.backlog_count);
+ - ]
764 [ + - + - : 4064 : b.pushKV("count_tok", buckinfo.count_bucket);
+ - ]
765 [ + - + - : 4064 : b.pushKV("size_tok", buckinfo.size_bucket);
+ - ]
766 : 2032 : return b;
767 : 0 : };
768 : 1016 : UniValue invbuckets{UniValue::VOBJ};
769 [ + - + - : 2032 : invbuckets.pushKV("inbound", buckjson(peerman_info.inbound_bucket));
+ - ]
770 [ + - + - : 2032 : invbuckets.pushKV("outbound", buckjson(peerman_info.outbound_bucket));
+ - ]
771 [ + - + - : 2032 : obj.pushKV("inv_buckets", invbuckets);
+ - ]
772 [ + - + - : 2032 : obj.pushKV("networkactive", connman.GetNetworkActive());
+ - ]
773 [ + - + - : 2032 : obj.pushKV("connections", connman.GetNodeCount(ConnectionDirection::Both));
+ - + - ]
774 [ + - + - : 2032 : obj.pushKV("connections_in", connman.GetNodeCount(ConnectionDirection::In));
+ - + - ]
775 [ + - + - : 2032 : obj.pushKV("connections_out", connman.GetNodeCount(ConnectionDirection::Out));
+ - + - ]
776 [ + - + - : 2032 : obj.pushKV("networks", GetNetworksInfo());
+ - ]
777 [ + - + - ]: 1016 : const NetGroupManager& netgroupman{*CHECK_NONFATAL(node.netgroupman)};
778 [ + - + + ]: 1016 : if (netgroupman.UsingASMap()) {
779 [ + - + - : 6 : obj.pushKV("asmap_version", HexStr(netgroupman.GetAsmapVersion()));
+ - + - +
- ]
780 : : }
781 [ + - ]: 1016 : const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
782 : : // Those fields can be deprecated, to be replaced by the getmempoolinfo fields
783 [ + - + - : 2032 : obj.pushKV("relayfee", ValueFromAmount(mempool.m_opts.min_relay_feerate.GetFeePerK()));
+ - ]
784 [ + - + - : 2032 : obj.pushKV("incrementalfee", ValueFromAmount(mempool.m_opts.incremental_relay_feerate.GetFeePerK()));
+ - ]
785 : 1016 : UniValue localAddresses(UniValue::VARR);
786 : 1016 : {
787 [ + - ]: 1016 : LOCK(g_maplocalhost_mutex);
788 [ + + ]: 1021 : for (const std::pair<const CNetAddr, LocalServiceInfo> &item : mapLocalHost)
789 : : {
790 : 5 : UniValue rec(UniValue::VOBJ);
791 [ + - + - : 10 : rec.pushKV("address", item.first.ToStringAddr());
+ - + - ]
792 [ + - + - : 10 : rec.pushKV("port", item.second.nPort);
+ - ]
793 [ + - + - : 10 : rec.pushKV("score", item.second.nScore);
+ - ]
794 [ + - ]: 5 : localAddresses.push_back(std::move(rec));
795 : 5 : }
796 : 0 : }
797 [ + - + - ]: 2032 : obj.pushKV("localaddresses", std::move(localAddresses));
798 [ + - + - : 2032 : obj.pushKV("warnings", node::GetWarningsForRpc(*CHECK_NONFATAL(node.warnings), IsDeprecatedRPCEnabled("warnings")));
+ - + - +
- + - ]
799 : 2032 : return obj;
800 [ + - ]: 2032 : },
801 [ + - + - ]: 14216 : };
802 [ + - + - : 259442 : }
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - - - +
- + - + -
+ - - + -
- - - - -
- - - - -
- - - ]
803 : :
804 : 2585 : static RPCMethod setban()
805 : : {
806 : 2585 : return RPCMethod{
807 : 2585 : "setban",
808 [ + - ]: 5170 : "Attempts to add or remove an IP/Subnet from the banned list.\n",
809 : : {
810 [ + - + - ]: 5170 : {"subnet", RPCArg::Type::STR, RPCArg::Optional::NO, "The IP/Subnet (see getpeerinfo for nodes IP) with an optional netmask (default is /32 = single IP)"},
811 [ + - + - ]: 5170 : {"command", RPCArg::Type::STR, RPCArg::Optional::NO, "'add' to add an IP/Subnet to the list, 'remove' to remove an IP/Subnet from the list"},
812 [ + - + - : 7755 : {"bantime", RPCArg::Type::NUM, RPCArg::Default{0}, "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)"},
+ - ]
813 [ + - + - : 7755 : {"absolute", RPCArg::Type::BOOL, RPCArg::Default{false}, "If set, the bantime must be an absolute timestamp expressed in " + UNIX_EPOCH_TIME},
+ - ]
814 : : },
815 [ + - + - : 5170 : RPCResult{RPCResult::Type::NONE, "", ""},
+ - + - ]
816 : 2585 : RPCExamples{
817 [ + - + - : 5170 : HelpExampleCli("setban", "\"192.168.0.6\" \"add\" 86400")
+ - ]
818 [ + - + - : 10340 : + HelpExampleCli("setban", "\"192.168.0.0/24\" \"add\"")
+ - + - ]
819 [ + - + - : 10340 : + HelpExampleRpc("setban", "\"192.168.0.6\", \"add\", 86400")
+ - + - ]
820 [ + - ]: 2585 : },
821 : 2585 : [](const RPCMethod& help, const JSONRPCRequest& request) -> UniValue
822 : : {
823 : 48 : auto command{help.Arg<std::string_view>("command")};
824 [ + + - + ]: 48 : if (command != "add" && command != "remove") {
825 [ # # # # ]: 0 : throw std::runtime_error(help.ToString());
826 : : }
827 : 48 : NodeContext& node = EnsureAnyNodeContext(request.context);
828 : 48 : BanMan& banman = EnsureBanman(node);
829 : :
830 : 48 : CSubNet subNet;
831 [ + - ]: 48 : CNetAddr netAddr;
832 [ + - + - ]: 48 : std::string subnet_arg{help.Arg<std::string_view>("subnet")};
833 : 48 : const bool isSubnet{subnet_arg.find('/') != subnet_arg.npos};
834 : :
835 [ + + ]: 48 : if (!isSubnet) {
836 [ + - + - ]: 30 : const std::optional<CNetAddr> addr{LookupHost(subnet_arg, false)};
837 [ + + ]: 30 : if (addr.has_value()) {
838 [ + - + - ]: 29 : netAddr = static_cast<CNetAddr>(MaybeFlipIPv6toCJDNS(CService{addr.value(), /*port=*/0}));
839 : : }
840 : 30 : } else {
841 [ + - ]: 18 : subNet = LookupSubNet(subnet_arg);
842 : : }
843 : :
844 [ + + + - : 48 : if (! (isSubnet ? subNet.IsValid() : netAddr.IsValid()) ) {
+ - + + ]
845 [ + - + - ]: 6 : throw JSONRPCError(RPC_CLIENT_INVALID_IP_OR_SUBNET, "Error: Invalid IP/Subnet");
846 : : }
847 : :
848 [ + + ]: 45 : if (command == "add") {
849 [ + + + - : 35 : if (isSubnet ? banman.IsBanned(subNet) : banman.IsBanned(netAddr)) {
+ - + + ]
850 [ + - + - ]: 8 : throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: IP/Subnet already banned");
851 : : }
852 : :
853 : 31 : int64_t banTime = 0; //use standard bantime if not specified
854 [ + - + + ]: 31 : if (!request.params[2].isNull())
855 [ + - + - ]: 10 : banTime = request.params[2].getInt<int64_t>();
856 : :
857 [ + - + + : 31 : const bool absolute{request.params[3].isNull() ? false : request.params[3].get_bool()};
+ - + - ]
858 : :
859 [ + - + - : 5 : if (absolute && banTime < GetTime()) {
+ + ]
860 [ + - + - ]: 4 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Error: Absolute timestamp is in the past");
861 : : }
862 : :
863 [ + + ]: 29 : if (isSubnet) {
864 [ + - ]: 13 : banman.Ban(subNet, banTime, absolute);
865 [ + - ]: 13 : if (node.connman) {
866 [ + - ]: 13 : node.connman->DisconnectNode(subNet);
867 : : }
868 : : } else {
869 [ + - ]: 16 : banman.Ban(netAddr, banTime, absolute);
870 [ + - ]: 16 : if (node.connman) {
871 [ + - ]: 16 : node.connman->DisconnectNode(netAddr);
872 : : }
873 : : }
874 [ + - ]: 10 : } else if(command == "remove") {
875 [ + + + - : 10 : if (!( isSubnet ? banman.Unban(subNet) : banman.Unban(netAddr) )) {
+ - + + ]
876 [ + - + - ]: 4 : throw JSONRPCError(RPC_CLIENT_INVALID_IP_OR_SUBNET, "Error: Unban failed. Requested address/subnet was not previously manually banned.");
877 : : }
878 : : }
879 : 37 : return UniValue::VNULL;
880 : 59 : },
881 [ + - + - : 20680 : };
+ + - - ]
882 [ + - + - : 20680 : }
+ - + - -
- ]
883 : :
884 : 2583 : static RPCMethod listbanned()
885 : : {
886 : 2583 : return RPCMethod{
887 : 2583 : "listbanned",
888 [ + - ]: 5166 : "List all manually banned IPs/Subnets.\n",
889 : : {},
890 [ + - + - ]: 10332 : RPCResult{RPCResult::Type::ARR, "", "",
891 : : {
892 [ + - + - ]: 5166 : {RPCResult::Type::OBJ, "", "",
893 : : {
894 [ + - + - ]: 5166 : {RPCResult::Type::STR, "address", "The IP/Subnet of the banned node"},
895 [ + - + - ]: 7749 : {RPCResult::Type::NUM_TIME, "ban_created", "The " + UNIX_EPOCH_TIME + " the ban was created"},
896 [ + - + - ]: 5166 : {RPCResult::Type::NUM_TIME, "banned_until", "The " + UNIX_EPOCH_TIME + " the ban expires"},
897 [ + - + - ]: 5166 : {RPCResult::Type::NUM_TIME, "ban_duration", "The ban duration, in seconds"},
898 [ + - + - ]: 5166 : {RPCResult::Type::NUM_TIME, "time_remaining", "The time remaining until the ban expires, in seconds"},
899 : : }},
900 [ + - + - : 38745 : }},
+ - + + +
+ - - -
- ]
901 : 2583 : RPCExamples{
902 [ + - + - : 5166 : HelpExampleCli("listbanned", "")
+ - ]
903 [ + - + - : 10332 : + HelpExampleRpc("listbanned", "")
+ - + - ]
904 [ + - ]: 2583 : },
905 : 2583 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
906 : : {
907 : 47 : BanMan& banman = EnsureAnyBanman(request.context);
908 : :
909 [ + - ]: 47 : banmap_t banMap;
910 [ + - ]: 47 : banman.GetBanned(banMap);
911 [ + - ]: 47 : const int64_t current_time{GetTime()};
912 : :
913 : 47 : UniValue bannedAddresses(UniValue::VARR);
914 [ + + ]: 107 : for (const auto& entry : banMap)
915 : : {
916 : 60 : const CBanEntry& banEntry = entry.second;
917 : 60 : UniValue rec(UniValue::VOBJ);
918 [ + - + - : 120 : rec.pushKV("address", entry.first.ToString());
+ - + - ]
919 [ + - + - : 120 : rec.pushKV("ban_created", banEntry.nCreateTime);
+ - ]
920 [ + - + - : 120 : rec.pushKV("banned_until", banEntry.nBanUntil);
+ - ]
921 [ + - + - : 120 : rec.pushKV("ban_duration", (banEntry.nBanUntil - banEntry.nCreateTime));
+ - ]
922 [ + - + - : 120 : rec.pushKV("time_remaining", (banEntry.nBanUntil - current_time));
+ - ]
923 : :
924 [ + - ]: 60 : bannedAddresses.push_back(std::move(rec));
925 : 60 : }
926 : :
927 : 47 : return bannedAddresses;
928 : 47 : },
929 [ + - + - ]: 10332 : };
930 [ + - + - : 30996 : }
+ - + - +
- + - -
- ]
931 : :
932 : 2547 : static RPCMethod clearbanned()
933 : : {
934 : 2547 : return RPCMethod{
935 : 2547 : "clearbanned",
936 [ + - ]: 5094 : "Clear all banned IPs.\n",
937 : : {},
938 [ + - + - : 5094 : RPCResult{RPCResult::Type::NONE, "", ""},
+ - ]
939 : 2547 : RPCExamples{
940 [ + - + - : 5094 : HelpExampleCli("clearbanned", "")
+ - ]
941 [ + - + - : 10188 : + HelpExampleRpc("clearbanned", "")
+ - + - ]
942 [ + - ]: 2547 : },
943 : 2547 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
944 : : {
945 : 11 : BanMan& banman = EnsureAnyBanman(request.context);
946 : :
947 : 11 : banman.ClearBanned();
948 : :
949 : 11 : return UniValue::VNULL;
950 : : },
951 [ + - + - ]: 10188 : };
952 : : }
953 : :
954 : 2547 : static RPCMethod setnetworkactive()
955 : : {
956 : 2547 : return RPCMethod{
957 : 2547 : "setnetworkactive",
958 [ + - ]: 5094 : "Disable/enable all p2p network activity.\n",
959 : : {
960 [ + - + - ]: 5094 : {"state", RPCArg::Type::BOOL, RPCArg::Optional::NO, "true to enable networking, false to disable"},
961 : : },
962 [ + - + - : 5094 : RPCResult{RPCResult::Type::BOOL, "", "The value that was passed in"},
+ - + - ]
963 [ + - + - ]: 7641 : RPCExamples{""},
964 : 2547 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
965 : : {
966 : 11 : NodeContext& node = EnsureAnyNodeContext(request.context);
967 : 11 : CConnman& connman = EnsureConnman(node);
968 : :
969 : 11 : connman.SetNetworkActive(request.params[0].get_bool());
970 : :
971 : 11 : return connman.GetNetworkActive();
972 : : },
973 [ + - + - : 12735 : };
+ + - - ]
974 [ + - ]: 5094 : }
975 : :
976 : 2577 : static RPCMethod getnodeaddresses()
977 : : {
978 : 2577 : return RPCMethod{"getnodeaddresses",
979 [ + - ]: 5154 : "Return known addresses, after filtering for quality and recency.\n"
980 : : "These can potentially be used to find new peers in the network.\n"
981 : : "The total number of addresses known to the node may be higher.",
982 : : {
983 [ + - + - : 7731 : {"count", RPCArg::Type::NUM, RPCArg::Default{1}, "The maximum number of addresses to return. Specify 0 to return all known addresses."},
+ - ]
984 [ + - + - : 7731 : {"network", RPCArg::Type::STR, RPCArg::DefaultHint{"all networks"}, "Return only addresses of the specified network. Can be one of: " + Join(GetNetworkNames(), ", ") + "."},
+ - + - +
- ]
985 : : },
986 [ + - ]: 5154 : RPCResult{
987 [ + - + - ]: 5154 : RPCResult::Type::ARR, "", "",
988 : : {
989 [ + - + - ]: 5154 : {RPCResult::Type::OBJ, "", "",
990 : : {
991 [ + - + - ]: 5154 : {RPCResult::Type::NUM_TIME, "time", "The " + UNIX_EPOCH_TIME + " when the node was last seen"},
992 [ + - + - ]: 5154 : {RPCResult::Type::NUM, "services", "The services offered by the node"},
993 [ + - + - ]: 5154 : {RPCResult::Type::STR, "address", "The address of the node"},
994 [ + - + - ]: 5154 : {RPCResult::Type::NUM, "port", "The port number of the node"},
995 [ + - + - : 5154 : {RPCResult::Type::STR, "network", "The network (" + Join(GetNetworkNames(), ", ") + ") the node connected through"},
+ - + - ]
996 : : }},
997 : : }
998 [ + - + - : 38655 : },
+ - + + +
+ - - -
- ]
999 : 2577 : RPCExamples{
1000 [ + - + - : 5154 : HelpExampleCli("getnodeaddresses", "8")
+ - ]
1001 [ + - + - : 10308 : + HelpExampleCli("getnodeaddresses", "4 \"i2p\"")
+ - + - ]
1002 [ + - + - : 10308 : + HelpExampleCli("-named getnodeaddresses", "network=onion count=12")
+ - + - ]
1003 [ + - + - : 10308 : + HelpExampleRpc("getnodeaddresses", "8")
+ - + - ]
1004 [ + - + - : 10308 : + HelpExampleRpc("getnodeaddresses", "4, \"i2p\"")
+ - + - ]
1005 [ + - ]: 2577 : },
1006 : 2577 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
1007 : : {
1008 : 41 : NodeContext& node = EnsureAnyNodeContext(request.context);
1009 : 41 : const CConnman& connman = EnsureConnman(node);
1010 : :
1011 [ + + ]: 41 : const int count{request.params[0].isNull() ? 1 : request.params[0].getInt<int>()};
1012 [ + + + - : 37 : if (count < 0) throw JSONRPCError(RPC_INVALID_PARAMETER, "Address count out of range");
+ - ]
1013 : :
1014 [ + + ]: 39 : const std::optional<Network> network{request.params[1].isNull() ? std::nullopt : std::optional<Network>{ParseNetwork(request.params[1].get_str())}};
1015 [ + + ]: 39 : if (network == NET_UNROUTABLE) {
1016 [ + - + - : 4 : throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Network not recognized: %s", request.params[1].get_str()));
+ - + - ]
1017 : : }
1018 : :
1019 : : // returns a shuffled list of CAddress
1020 : 37 : const std::vector<CAddress> vAddr{connman.GetAddressesUnsafe(count, /*max_pct=*/0, network)};
1021 : 37 : UniValue ret(UniValue::VARR);
1022 : :
1023 [ + + ]: 27785 : for (const CAddress& addr : vAddr) {
1024 : 27748 : UniValue obj(UniValue::VOBJ);
1025 [ + - + - : 55496 : obj.pushKV("time", TicksSinceEpoch<std::chrono::seconds>(addr.nTime));
+ - ]
1026 [ + - + - : 55496 : obj.pushKV("services", static_cast<std::underlying_type_t<decltype(addr.nServices)>>(addr.nServices));
+ - ]
1027 [ + - + - : 55496 : obj.pushKV("address", addr.ToStringAddr());
+ - + - ]
1028 [ + - + - : 55496 : obj.pushKV("port", addr.GetPort());
+ - + - ]
1029 [ + - + - : 55496 : obj.pushKV("network", GetNetworkName(addr.GetNetClass()));
+ - + - +
- ]
1030 [ + - ]: 27748 : ret.push_back(std::move(obj));
1031 : 27748 : }
1032 : 37 : return ret;
1033 : 37 : },
1034 [ + - + - : 15462 : };
+ + - - ]
1035 [ + - + - : 41232 : }
+ - + - +
- + - + -
+ - - - -
- ]
1036 : :
1037 : 34879 : static RPCMethod addpeeraddress()
1038 : : {
1039 : 34879 : return RPCMethod{"addpeeraddress",
1040 [ + - ]: 69758 : "Add the address of a potential peer to an address manager table. This RPC is for testing only.",
1041 : : {
1042 [ + - + - ]: 69758 : {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The IP address of the peer"},
1043 [ + - + - ]: 69758 : {"port", RPCArg::Type::NUM, RPCArg::Optional::NO, "The port of the peer"},
1044 [ + - + - : 104637 : {"tried", RPCArg::Type::BOOL, RPCArg::Default{false}, "If true, attempt to add the peer to the tried addresses table"},
+ - ]
1045 : : },
1046 [ + - ]: 69758 : RPCResult{
1047 [ + - + - ]: 69758 : RPCResult::Type::OBJ, "", "",
1048 : : {
1049 [ + - + - ]: 69758 : {RPCResult::Type::BOOL, "success", "whether the peer address was successfully added to the address manager table"},
1050 [ + - + - ]: 69758 : {RPCResult::Type::STR, "error", /*optional=*/true, "error description, if the address could not be added"},
1051 : : },
1052 [ + - + - : 209274 : },
+ + - - ]
1053 : 34879 : RPCExamples{
1054 [ + - + - : 69758 : HelpExampleCli("addpeeraddress", "\"1.2.3.4\" 8333 true")
+ - ]
1055 [ + - + - : 139516 : + HelpExampleRpc("addpeeraddress", "\"1.2.3.4\", 8333, true")
+ - + - ]
1056 [ + - ]: 34879 : },
1057 : 34879 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
1058 : : {
1059 : 32350 : AddrMan& addrman = EnsureAnyAddrman(request.context);
1060 : :
1061 : 32350 : const std::string& addr_string{request.params[0].get_str()};
1062 : 32350 : const auto port{request.params[1].getInt<uint16_t>()};
1063 [ + + ]: 32346 : const bool tried{request.params[2].isNull() ? false : request.params[2].get_bool()};
1064 : :
1065 : 32346 : UniValue obj(UniValue::VOBJ);
1066 [ + - + - ]: 32346 : std::optional<CNetAddr> net_addr{LookupHost(addr_string, false)};
1067 [ + + ]: 32346 : if (!net_addr.has_value()) {
1068 [ + - + - ]: 8 : throw JSONRPCError(RPC_CLIENT_INVALID_IP_OR_SUBNET, "Invalid IP address");
1069 : : }
1070 : :
1071 : 32342 : bool success{false};
1072 : :
1073 [ + - ]: 32342 : CService service{net_addr.value(), port};
1074 [ + - ]: 64684 : CAddress address{MaybeFlipIPv6toCJDNS(service), ServiceFlags{NODE_NETWORK | NODE_WITNESS}};
1075 : 32342 : address.nTime = Now<NodeSeconds>();
1076 : : // The source address is set equal to the address. This is equivalent to the peer
1077 : : // announcing itself.
1078 [ + - + - : 64684 : if (addrman.Add({address}, address)) {
+ + + + -
- ]
1079 : 28588 : success = true;
1080 [ + + ]: 28588 : if (tried) {
1081 : : // Attempt to move the address to the tried addresses table.
1082 [ + - + + ]: 22 : if (!addrman.Good(address)) {
1083 : 2 : success = false;
1084 [ + - + - : 4 : obj.pushKV("error", "failed-adding-to-tried");
+ - ]
1085 : : }
1086 : : }
1087 : : } else {
1088 [ + - + - : 7508 : obj.pushKV("error", "failed-adding-to-new");
+ - ]
1089 : : }
1090 : :
1091 [ + - + - : 64684 : obj.pushKV("success", success);
+ - ]
1092 : 32342 : return obj;
1093 : 64688 : },
1094 [ + - + - : 244153 : };
+ + - - ]
1095 [ + - + - : 348790 : }
+ - + - +
- - - -
- ]
1096 : :
1097 : 2543 : static RPCMethod sendmsgtopeer()
1098 : : {
1099 : 2543 : return RPCMethod{
1100 : 2543 : "sendmsgtopeer",
1101 [ + - ]: 5086 : "Send a p2p message to a peer specified by id.\n"
1102 : : "The message type and body must be provided, the message header will be generated.\n"
1103 : : "This RPC is for testing only.",
1104 : : {
1105 [ + - + - ]: 5086 : {"peer_id", RPCArg::Type::NUM, RPCArg::Optional::NO, "The peer to send the message to."},
1106 [ + - + - ]: 5086 : {"msg_type", RPCArg::Type::STR, RPCArg::Optional::NO, strprintf("The message type (maximum length %i)", CMessageHeader::MESSAGE_TYPE_SIZE)},
1107 [ + - + - ]: 5086 : {"msg", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The serialized message body to send, in hex, without a message header"},
1108 : : },
1109 [ + - + - : 5086 : RPCResult{RPCResult::Type::OBJ, "", "", std::vector<RPCResult>{}},
+ - + - ]
1110 : 2543 : RPCExamples{
1111 [ + - + - : 7629 : HelpExampleCli("sendmsgtopeer", "0 \"addr\" \"ffffff\"") + HelpExampleRpc("sendmsgtopeer", R"(0, "addr", "ffffff")")},
+ - + - +
- + - + -
+ - ]
1112 : 2543 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue {
1113 : 18 : const NodeId peer_id{request.params[0].getInt<int64_t>()};
1114 : 18 : const auto msg_type{self.Arg<std::string_view>("msg_type")};
1115 [ + + ]: 18 : if (msg_type.size() > CMessageHeader::MESSAGE_TYPE_SIZE) {
1116 [ + - + - ]: 4 : throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Error: msg_type too long, max length is %i", CMessageHeader::MESSAGE_TYPE_SIZE));
1117 : : }
1118 : 16 : auto msg{TryParseHex<unsigned char>(self.Arg<std::string_view>("msg"))};
1119 [ - + ]: 16 : if (!msg.has_value()) {
1120 [ # # # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Error parsing input for msg");
1121 : : }
1122 : :
1123 [ + - ]: 16 : NodeContext& node = EnsureAnyNodeContext(request.context);
1124 [ + - ]: 16 : CConnman& connman = EnsureConnman(node);
1125 : :
1126 [ + - ]: 16 : CSerializedNetMsg msg_ser;
1127 [ + - + - ]: 16 : msg_ser.data = msg.value();
1128 [ + - ]: 16 : msg_ser.m_type = msg_type;
1129 : :
1130 [ + - ]: 16 : bool success = connman.ForNode(peer_id, [&](CNode* node) {
1131 : 14 : connman.PushMessage(node, std::move(msg_ser));
1132 : 14 : return true;
1133 : : });
1134 : :
1135 [ + + ]: 16 : if (!success) {
1136 [ + - + - ]: 4 : throw JSONRPCError(RPC_MISC_ERROR, "Error: Could not send message to peer");
1137 : : }
1138 : :
1139 : 14 : UniValue ret{UniValue::VOBJ};
1140 : 14 : return ret;
1141 : 16 : },
1142 [ + - + - : 17801 : };
+ + - - ]
1143 [ + - + - : 15258 : }
+ - - - ]
1144 : :
1145 : 2544 : static RPCMethod getaddrmaninfo()
1146 : : {
1147 : 2544 : return RPCMethod{
1148 : 2544 : "getaddrmaninfo",
1149 [ + - ]: 5088 : "Provides information about the node's address manager by returning the number of "
1150 : : "unique addresses in the `new` and `tried` tables and their sum for all networks.\n",
1151 : : {},
1152 [ + - ]: 5088 : RPCResult{
1153 [ + - ]: 5088 : RPCResult::Type::OBJ_DYN, "", "json object with network type as keys", {
1154 [ + - + - : 5088 : {RPCResult::Type::OBJ, "network", "the network (" + Join(GetNetworkNames(), ", ") + ", all_networks)", {
+ - + - ]
1155 [ + - + - ]: 5088 : {RPCResult::Type::NUM, "new", "number of unique addresses in the new table, which represent potential peers the node has discovered but hasn't yet successfully connected to. "
1156 : : "An address can be stored in multiple new table buckets but is counted only once."},
1157 [ + - + - ]: 5088 : {RPCResult::Type::NUM, "tried", "number of addresses in the tried table, which represent peers the node has successfully connected to in the past."},
1158 [ + - + - ]: 5088 : {RPCResult::Type::NUM, "total", "total number of unique addresses in both new/tried tables"},
1159 : : }},
1160 [ + - + - : 27984 : }},
+ - + + +
+ - - -
- ]
1161 [ + - + - : 7632 : RPCExamples{HelpExampleCli("getaddrmaninfo", "") + HelpExampleRpc("getaddrmaninfo", "")},
+ - + - +
- + - + -
+ - ]
1162 : 2544 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue {
1163 : 8 : AddrMan& addrman = EnsureAnyAddrman(request.context);
1164 : :
1165 : 8 : UniValue ret(UniValue::VOBJ);
1166 [ + + ]: 64 : for (int n = 0; n < NET_MAX; ++n) {
1167 : 56 : enum Network network = static_cast<enum Network>(n);
1168 [ + + ]: 56 : if (network == NET_UNROUTABLE || network == NET_INTERNAL) continue;
1169 : 40 : UniValue obj(UniValue::VOBJ);
1170 [ + - + - : 80 : obj.pushKV("new", addrman.Size(network, true));
+ - + - ]
1171 [ + - + - : 80 : obj.pushKV("tried", addrman.Size(network, false));
+ - + - ]
1172 [ + - + - : 80 : obj.pushKV("total", addrman.Size(network));
+ - + - ]
1173 [ + - + - ]: 80 : ret.pushKV(GetNetworkName(network), std::move(obj));
1174 : 40 : }
1175 : 8 : UniValue obj(UniValue::VOBJ);
1176 [ + - + - : 16 : obj.pushKV("new", addrman.Size(std::nullopt, true));
+ - + - ]
1177 [ + - + - : 16 : obj.pushKV("tried", addrman.Size(std::nullopt, false));
+ - + - ]
1178 [ + - + - : 16 : obj.pushKV("total", addrman.Size());
+ - + - ]
1179 [ + - + - ]: 16 : ret.pushKV("all_networks", std::move(obj));
1180 : 8 : return ret;
1181 : 8 : },
1182 [ + - + - ]: 10176 : };
1183 [ + - + - : 20352 : }
+ - + - -
- ]
1184 : :
1185 : 2537 : static RPCMethod exportasmap()
1186 : : {
1187 : 2537 : return RPCMethod{
1188 : 2537 : "exportasmap",
1189 [ + - ]: 5074 : "Export the embedded ASMap data to a file. Any existing file at the path will be overwritten.\n",
1190 : : {
1191 [ + - + - ]: 5074 : {"path", RPCArg::Type::STR, RPCArg::Optional::NO, "Path to the output file. If relative, will be prefixed by datadir."},
1192 : : },
1193 [ + - ]: 5074 : RPCResult{
1194 [ + - + - ]: 5074 : RPCResult::Type::OBJ, "", "",
1195 : : {
1196 [ + - + - ]: 5074 : {RPCResult::Type::STR, "path", "the absolute path that the ASMap data was written to"},
1197 [ + - + - ]: 5074 : {RPCResult::Type::NUM, "bytes_written", "the number of bytes written to the file"},
1198 [ + - + - ]: 5074 : {RPCResult::Type::STR_HEX, "file_hash", "the SHA256 hash of the exported ASMap data"},
1199 : : }
1200 [ + - + - : 20296 : },
+ + - - ]
1201 : 2537 : RPCExamples{
1202 [ + - + - : 7611 : HelpExampleCli("exportasmap", "\"asmap.dat\"") + HelpExampleRpc("exportasmap", "\"asmap.dat\"")},
+ - + - +
- + - + -
+ - ]
1203 : 2537 : [&](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue {
1204 : : #ifndef ENABLE_EMBEDDED_ASMAP
1205 : : throw JSONRPCError(RPC_MISC_ERROR, "No embedded ASMap data available");
1206 : : #else
1207 [ - + ]: 1 : if (node::data::ip_asn.empty() || !CheckStandardAsmap(node::data::ip_asn)) {
1208 [ # # # # ]: 0 : throw JSONRPCError(RPC_MISC_ERROR, "Embedded ASMap data appears to be corrupted");
1209 : : }
1210 : :
1211 : 1 : const ArgsManager& args{EnsureAnyArgsman(request.context)};
1212 [ + - + - ]: 2 : const fs::path export_path{fsbridge::AbsPathJoin(args.GetDataDirNet(), fs::u8path(self.Arg<std::string_view>("path")))};
1213 : :
1214 [ + - + - ]: 2 : AutoFile file{fsbridge::fopen(export_path, "wb")};
1215 [ - + ]: 1 : if (file.IsNull()) {
1216 [ # # # # : 0 : throw JSONRPCError(RPC_MISC_ERROR, strprintf("Failed to open asmap file: %s", fs::PathToString(export_path)));
# # ]
1217 : : }
1218 : :
1219 [ + - ]: 1 : file << node::data::ip_asn;
1220 : :
1221 [ + - - + ]: 2 : if (file.fclose() != 0) {
1222 [ # # # # : 0 : throw JSONRPCError(RPC_MISC_ERROR, strprintf("Failed to close asmap file: %s", fs::PathToString(export_path)));
# # ]
1223 : : }
1224 : :
1225 : 1 : UniValue result(UniValue::VOBJ);
1226 [ + - + - : 2 : result.pushKV("path", export_path.utf8string());
+ - + - ]
1227 [ + - + - : 2 : result.pushKV("bytes_written", node::data::ip_asn.size());
+ - ]
1228 [ + - + - : 2 : result.pushKV("file_hash", HexStr(AsmapVersion(node::data::ip_asn)));
+ - + - +
- ]
1229 : 2 : return result;
1230 : : #endif
1231 : 2 : },
1232 [ + - + - : 12685 : };
+ + - - ]
1233 [ + - + - : 20296 : }
+ - + - -
- ]
1234 : :
1235 : 26 : UniValue AddrmanEntryToJSON(const AddrInfo& info, const CConnman& connman)
1236 : : {
1237 : 26 : UniValue ret(UniValue::VOBJ);
1238 [ + - + - : 52 : ret.pushKV("address", info.ToStringAddr());
+ - + - ]
1239 [ + - ]: 26 : const uint32_t mapped_as{connman.GetMappedAS(info)};
1240 [ + + ]: 26 : if (mapped_as) {
1241 [ + - + - : 8 : ret.pushKV("mapped_as", mapped_as);
+ - ]
1242 : : }
1243 [ + - + - : 52 : ret.pushKV("port", info.GetPort());
+ - + - ]
1244 [ + - + - : 52 : ret.pushKV("services", static_cast<std::underlying_type_t<decltype(info.nServices)>>(info.nServices));
+ - ]
1245 [ + - + - : 52 : ret.pushKV("time", TicksSinceEpoch<std::chrono::seconds>(info.nTime));
+ - ]
1246 [ + - + - : 52 : ret.pushKV("network", GetNetworkName(info.GetNetClass()));
+ - + - +
- ]
1247 [ + - + - : 52 : ret.pushKV("source", info.source.ToStringAddr());
+ - + - ]
1248 [ + - + - : 52 : ret.pushKV("source_network", GetNetworkName(info.source.GetNetClass()));
+ - + - +
- ]
1249 [ + - ]: 26 : const uint32_t source_mapped_as{connman.GetMappedAS(info.source)};
1250 [ + + ]: 26 : if (source_mapped_as) {
1251 [ + - + - : 8 : ret.pushKV("source_mapped_as", source_mapped_as);
+ - ]
1252 : : }
1253 : 26 : return ret;
1254 : 0 : }
1255 : :
1256 : 14 : UniValue AddrmanTableToJSON(const std::vector<std::pair<AddrInfo, AddressPosition>>& tableInfos, const CConnman& connman)
1257 : : {
1258 : 14 : UniValue table(UniValue::VOBJ);
1259 [ + + ]: 40 : for (const auto& e : tableInfos) {
1260 : 26 : AddrInfo info = e.first;
1261 : 26 : AddressPosition location = e.second;
1262 [ + - ]: 26 : std::ostringstream key;
1263 [ + - + - : 26 : key << location.bucket << "/" << location.position;
+ - ]
1264 : : // Address manager tables have unique entries so there is no advantage
1265 : : // in using UniValue::pushKV, which checks if the key already exists
1266 : : // in O(N). UniValue::pushKVEnd is used instead which currently is O(1).
1267 [ + - + - ]: 78 : table.pushKVEnd(key.str(), AddrmanEntryToJSON(info, connman));
1268 : 52 : }
1269 : 14 : return table;
1270 : 0 : }
1271 : :
1272 : 2534 : static RPCMethod getrawaddrman()
1273 : : {
1274 : 2534 : return RPCMethod{"getrawaddrman",
1275 [ + - ]: 5068 : "EXPERIMENTAL warning: this call may be changed in future releases.\n"
1276 : : "\nReturns information on all address manager entries for the new and tried tables.\n",
1277 : : {},
1278 [ + - ]: 5068 : RPCResult{
1279 [ + - ]: 5068 : RPCResult::Type::OBJ_DYN, "", "", {
1280 [ + - + - ]: 5068 : {RPCResult::Type::OBJ_DYN, "table", "buckets with addresses in the address manager table ( new, tried )", {
1281 [ + - + - ]: 5068 : {RPCResult::Type::OBJ, "bucket/position", "the location in the address manager table (<bucket>/<position>)", {
1282 [ + - + - ]: 5068 : {RPCResult::Type::STR, "address", "The address of the node"},
1283 [ + - + - ]: 5068 : {RPCResult::Type::NUM, "mapped_as", /*optional=*/true, "Mapped AS (Autonomous System) number at the end of the BGP route to the peer, used for diversifying peer selection (only displayed if the -asmap config option is set)"},
1284 [ + - + - ]: 5068 : {RPCResult::Type::NUM, "port", "The port number of the node"},
1285 [ + - + - : 5068 : {RPCResult::Type::STR, "network", "The network (" + Join(GetNetworkNames(), ", ") + ") of the address"},
+ - + - ]
1286 [ + - + - ]: 5068 : {RPCResult::Type::NUM, "services", "The services offered by the node"},
1287 [ + - + - ]: 5068 : {RPCResult::Type::NUM_TIME, "time", "The " + UNIX_EPOCH_TIME + " when the node was last seen"},
1288 [ + - + - ]: 5068 : {RPCResult::Type::STR, "source", "The address that relayed the address to us"},
1289 [ + - + - : 5068 : {RPCResult::Type::STR, "source_network", "The network (" + Join(GetNetworkNames(), ", ") + ") of the source address"},
+ - + - ]
1290 [ + - + - ]: 5068 : {RPCResult::Type::NUM, "source_mapped_as", /*optional=*/true, "Mapped AS (Autonomous System) number at the end of the BGP route to the source, used for diversifying peer selection (only displayed if the -asmap config option is set)"}
1291 : : }}
1292 : : }}
1293 : : }
1294 [ + - + - : 65884 : },
+ - + - +
+ + + + +
- - - - -
- ]
1295 : 2534 : RPCExamples{
1296 [ + - + - : 5068 : HelpExampleCli("getrawaddrman", "")
+ - ]
1297 [ + - + - : 10136 : + HelpExampleRpc("getrawaddrman", "")
+ - + - ]
1298 [ + - ]: 2534 : },
1299 : 2534 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue {
1300 : 7 : AddrMan& addrman = EnsureAnyAddrman(request.context);
1301 : 7 : NodeContext& node_context = EnsureAnyNodeContext(request.context);
1302 : 7 : CConnman& connman = EnsureConnman(node_context);
1303 : :
1304 : 7 : UniValue ret(UniValue::VOBJ);
1305 [ + - + - : 14 : ret.pushKV("new", AddrmanTableToJSON(addrman.GetEntries(false), connman));
+ - + - ]
1306 [ + - + - : 14 : ret.pushKV("tried", AddrmanTableToJSON(addrman.GetEntries(true), connman));
+ - + - ]
1307 : 7 : return ret;
1308 : 0 : },
1309 [ + - + - ]: 10136 : };
1310 [ + - + - : 55748 : }
+ - + - +
- + - + -
+ - + - +
- + - -
- ]
1311 : :
1312 : 1404 : void RegisterNetRPCCommands(CRPCTable& t)
1313 : : {
1314 : 1404 : static const CRPCCommand commands[]{
1315 [ + - ]: 2522 : {"network", &getconnectioncount},
1316 [ + - ]: 2522 : {"network", &ping},
1317 [ + - ]: 2522 : {"network", &getpeerinfo},
1318 [ + - ]: 2522 : {"network", &addnode},
1319 [ + - ]: 2522 : {"network", &disconnectnode},
1320 [ + - ]: 2522 : {"network", &getaddednodeinfo},
1321 [ + - ]: 2522 : {"network", &getnettotals},
1322 [ + - ]: 2522 : {"network", &getnetworkinfo},
1323 [ + - ]: 2522 : {"network", &setban},
1324 [ + - ]: 2522 : {"network", &listbanned},
1325 [ + - ]: 2522 : {"network", &clearbanned},
1326 [ + - ]: 2522 : {"network", &setnetworkactive},
1327 [ + - ]: 2522 : {"network", &getnodeaddresses},
1328 [ + - ]: 2522 : {"network", &getaddrmaninfo},
1329 [ + - ]: 2522 : {"network", &exportasmap},
1330 [ + - ]: 2522 : {"hidden", &addconnection},
1331 [ + - ]: 2522 : {"hidden", &addpeeraddress},
1332 [ + - ]: 2522 : {"hidden", &sendmsgtopeer},
1333 [ + - ]: 2522 : {"hidden", &getrawaddrman},
1334 [ + + + - : 25363 : };
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - -
- ]
1335 [ + + ]: 28080 : for (const auto& c : commands) {
1336 : 26676 : t.appendCommand(c.name, &c);
1337 : : }
1338 : 1404 : }
|