Branch data Line data Source code
1 : : // Copyright (c) 2009-2022 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/server.h>
6 : :
7 : : #include <addrman.h>
8 : : #include <addrman_impl.h>
9 : : #include <banman.h>
10 : : #include <chainparams.h>
11 : : #include <clientversion.h>
12 : : #include <core_io.h>
13 : : #include <net_permissions.h>
14 : : #include <net_processing.h>
15 : : #include <net_types.h> // For banmap_t
16 : : #include <netbase.h>
17 : : #include <node/context.h>
18 : : #include <node/protocol_version.h>
19 : : #include <policy/settings.h>
20 : : #include <protocol.h>
21 : : #include <rpc/blockchain.h>
22 : : #include <rpc/protocol.h>
23 : : #include <rpc/server_util.h>
24 : : #include <rpc/util.h>
25 : : #include <sync.h>
26 : : #include <timedata.h>
27 : 2 : #include <util/chaintype.h>
28 : : #include <util/strencodings.h>
29 : : #include <util/string.h>
30 : : #include <util/time.h>
31 : : #include <util/translation.h>
32 : : #include <validation.h>
33 : : #include <warnings.h>
34 : :
35 : : #include <optional>
36 : :
37 : : #include <univalue.h>
38 : :
39 : : using node::NodeContext;
40 : :
41 [ + - ][ # # ]: 2 : const std::vector<std::string> CONNECTION_TYPE_DOC{
42 [ + - ]: 2 : "outbound-full-relay (default automatic connections)",
43 [ + - ]: 2 : "block-relay-only (does not relay transactions or addresses)",
44 [ + - ]: 2 : "inbound (initiated by the peer)",
45 [ + - ]: 2 : "manual (added via addnode RPC or -addnode/-connect configuration options)",
46 [ + - ]: 2 : "addr-fetch (short-lived automatic connection for soliciting addresses)",
47 [ + - ]: 2 : "feeler (short-lived automatic connection for testing addresses)"
48 : : };
49 : :
50 [ + - ][ # # ]: 2 : const std::vector<std::string> TRANSPORT_TYPE_DOC{
51 [ + - ]: 2 : "detecting (peer could be v1 or v2)",
52 [ + - ]: 2 : "v1 (plaintext transport protocol)",
53 [ + - ]: 2 : "v2 (BIP324 encrypted transport protocol)"
54 : : };
55 : :
56 : 0 : static RPCHelpMan getconnectioncount()
57 : : {
58 [ # # ][ # # ]: 0 : return RPCHelpMan{"getconnectioncount",
59 [ # # ]: 0 : "\nReturns the number of connections to other nodes.\n",
60 : 0 : {},
61 [ # # ][ # # ]: 0 : RPCResult{
62 [ # # ][ # # ]: 0 : RPCResult::Type::NUM, "", "The connection count"
63 : : },
64 [ # # ]: 0 : RPCExamples{
65 [ # # ][ # # ]: 0 : HelpExampleCli("getconnectioncount", "")
[ # # ]
66 [ # # ][ # # ]: 0 : + HelpExampleRpc("getconnectioncount", "")
[ # # ][ # # ]
67 : : },
68 : 0 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
69 : : {
70 : 0 : NodeContext& node = EnsureAnyNodeContext(request.context);
71 : 0 : const CConnman& connman = EnsureConnman(node);
72 : :
73 : 0 : return connman.GetNodeCount(ConnectionDirection::Both);
74 : : },
75 : : };
76 : 0 : }
77 : :
78 : 0 : static RPCHelpMan ping()
79 : : {
80 [ # # ][ # # ]: 0 : return RPCHelpMan{"ping",
81 [ # # ]: 0 : "\nRequests that a ping be sent to all other nodes, to measure ping time.\n"
82 : : "Results provided in getpeerinfo, pingtime and pingwait fields are decimal seconds.\n"
83 : : "Ping command is handled in queue with all other commands, so it measures processing backlog, not just network ping.\n",
84 : 0 : {},
85 [ # # ][ # # ]: 0 : RPCResult{RPCResult::Type::NONE, "", ""},
[ # # ][ # # ]
86 [ # # ]: 0 : RPCExamples{
87 [ # # ][ # # ]: 0 : HelpExampleCli("ping", "")
[ # # ]
88 [ # # ][ # # ]: 0 : + HelpExampleRpc("ping", "")
[ # # ][ # # ]
89 : : },
90 : 0 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
91 : : {
92 : 0 : NodeContext& node = EnsureAnyNodeContext(request.context);
93 : 0 : PeerManager& peerman = EnsurePeerman(node);
94 : :
95 : : // Request that each node send a ping during next message processing pass
96 : 0 : peerman.SendPings();
97 [ # # ]: 0 : return UniValue::VNULL;
98 : 0 : },
99 : : };
100 : 0 : }
101 : :
102 : : /** Returns, given services flags, a list of humanly readable (known) network services */
103 : 0 : static UniValue GetServicesNames(ServiceFlags services)
104 : : {
105 [ # # ]: 0 : UniValue servicesNames(UniValue::VARR);
106 : :
107 [ # # ][ # # ]: 0 : for (const auto& flag : serviceFlagsToStr(services)) {
108 [ # # ][ # # ]: 0 : servicesNames.push_back(flag);
109 : : }
110 : :
111 : 0 : return servicesNames;
112 [ # # ]: 0 : }
113 : :
114 : 0 : static RPCHelpMan getpeerinfo()
115 : : {
116 [ # # ][ # # ]: 0 : return RPCHelpMan{
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
117 [ # # ]: 0 : "getpeerinfo",
118 [ # # ]: 0 : "Returns data about each connected network peer as a json array of objects.",
119 : 0 : {},
120 [ # # ][ # # ]: 0 : RPCResult{
121 [ # # ][ # # ]: 0 : RPCResult::Type::ARR, "", "",
122 [ # # ]: 0 : {
123 [ # # ][ # # ]: 0 : {RPCResult::Type::OBJ, "", "",
[ # # ]
124 [ # # ]: 0 : {
125 : 0 : {
126 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "id", "Peer index"},
[ # # ]
127 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "addr", "(host:port) The IP address and port of the peer"},
[ # # ]
128 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "addrbind", /*optional=*/true, "(ip:port) Bind address of the connection to the peer"},
[ # # ]
129 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "addrlocal", /*optional=*/true, "(ip:port) Local address as reported by the peer"},
[ # # ]
130 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "network", "Network (" + Join(GetNetworkNames(/*append_unroutable=*/true), ", ") + ")"},
[ # # ][ # # ]
[ # # ][ # # ]
131 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "mapped_as", /*optional=*/true, "The AS in the BGP route to the peer used for diversifying\n"
[ # # ]
132 : : "peer selection (only available if the asmap config flag is set)"},
133 [ # # ][ # # ]: 0 : {RPCResult::Type::STR_HEX, "services", "The services offered"},
[ # # ]
134 [ # # ][ # # ]: 0 : {RPCResult::Type::ARR, "servicesnames", "the services offered, in human-readable form",
[ # # ]
135 [ # # ]: 0 : {
136 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "SERVICE_NAME", "the service name if it is recognised"}
[ # # ]
137 : : }},
138 [ # # ][ # # ]: 0 : {RPCResult::Type::BOOL, "relaytxes", "Whether we relay transactions to this peer"},
[ # # ]
139 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM_TIME, "lastsend", "The " + UNIX_EPOCH_TIME + " of the last send"},
[ # # ][ # # ]
140 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM_TIME, "lastrecv", "The " + UNIX_EPOCH_TIME + " of the last receive"},
[ # # ][ # # ]
141 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM_TIME, "last_transaction", "The " + UNIX_EPOCH_TIME + " of the last valid transaction received from this peer"},
[ # # ][ # # ]
142 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM_TIME, "last_block", "The " + UNIX_EPOCH_TIME + " of the last block received from this peer"},
[ # # ][ # # ]
143 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "bytessent", "The total bytes sent"},
[ # # ]
144 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "bytesrecv", "The total bytes received"},
[ # # ]
145 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM_TIME, "conntime", "The " + UNIX_EPOCH_TIME + " of the connection"},
[ # # ][ # # ]
146 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "timeoffset", "The time offset in seconds"},
[ # # ]
147 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "pingtime", /*optional=*/true, "The last ping time in milliseconds (ms), if any"},
[ # # ]
148 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "minping", /*optional=*/true, "The minimum observed ping time in milliseconds (ms), if any"},
[ # # ]
149 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "pingwait", /*optional=*/true, "The duration in milliseconds (ms) of an outstanding ping (if non-zero)"},
[ # # ]
150 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "version", "The peer version, such as 70001"},
[ # # ]
151 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "subver", "The string version"},
[ # # ]
152 [ # # ][ # # ]: 0 : {RPCResult::Type::BOOL, "inbound", "Inbound (true) or Outbound (false)"},
[ # # ]
153 [ # # ][ # # ]: 0 : {RPCResult::Type::BOOL, "bip152_hb_to", "Whether we selected peer as (compact blocks) high-bandwidth peer"},
[ # # ]
154 [ # # ][ # # ]: 0 : {RPCResult::Type::BOOL, "bip152_hb_from", "Whether peer selected us as (compact blocks) high-bandwidth peer"},
[ # # ]
155 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "startingheight", "The starting height (block) of the peer"},
[ # # ]
156 [ # # ][ # # ]: 0 : {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"},
[ # # ]
157 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "synced_headers", "The last header we have in common with this peer"},
[ # # ]
158 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "synced_blocks", "The last block we have in common with this peer"},
[ # # ]
159 [ # # ][ # # ]: 0 : {RPCResult::Type::ARR, "inflight", "",
[ # # ]
160 [ # # ]: 0 : {
161 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "n", "The heights of blocks we're currently asking from this peer"},
[ # # ]
162 : : }},
163 [ # # ][ # # ]: 0 : {RPCResult::Type::BOOL, "addr_relay_enabled", "Whether we participate in address relay with this peer"},
[ # # ]
164 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "addr_processed", "The total number of addresses processed, excluding those dropped due to rate limiting"},
[ # # ]
165 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "addr_rate_limited", "The total number of addresses dropped due to rate limiting"},
[ # # ]
166 [ # # ][ # # ]: 0 : {RPCResult::Type::ARR, "permissions", "Any special permissions that have been granted to this peer",
[ # # ]
167 [ # # ]: 0 : {
168 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "permission_type", Join(NET_PERMISSIONS_DOC, ",\n") + ".\n"},
[ # # ][ # # ]
169 : : }},
170 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "minfeefilter", "The minimum fee rate for transactions this peer accepts"},
[ # # ]
171 [ # # ][ # # ]: 0 : {RPCResult::Type::OBJ_DYN, "bytessent_per_msg", "",
[ # # ]
172 [ # # ]: 0 : {
173 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "msg", "The total bytes sent aggregated by message type\n"
[ # # ]
174 : : "When a message type is not listed in this json object, the bytes sent are 0.\n"
175 : : "Only known message types can appear as keys in the object."}
176 : : }},
177 [ # # ][ # # ]: 0 : {RPCResult::Type::OBJ_DYN, "bytesrecv_per_msg", "",
[ # # ]
178 [ # # ]: 0 : {
179 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "msg", "The total bytes received aggregated by message type\n"
180 : : "When a message type is not listed in this json object, the bytes received are 0.\n"
181 : : "Only known message types can appear as keys in the object and all bytes received\n"
182 [ # # ][ # # ]: 0 : "of unknown message types are listed under '"+NET_MESSAGE_TYPE_OTHER+"'."}
183 : : }},
184 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "connection_type", "Type of connection: \n" + Join(CONNECTION_TYPE_DOC, ",\n") + ".\n"
[ # # ][ # # ]
[ # # ]
185 : : "Please note this output is unlikely to be stable in upcoming releases as we iterate to\n"
186 : : "best capture connection behaviors."},
187 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "transport_protocol_type", "Type of transport protocol: \n" + Join(TRANSPORT_TYPE_DOC, ",\n") + ".\n"},
[ # # ][ # # ]
[ # # ]
188 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "session_id", "The session ID for this connection, or \"\" if there is none (\"v2\" transport protocol only).\n"},
[ # # ]
189 : : }},
190 : : }},
191 : : },
192 [ # # ]: 0 : RPCExamples{
193 [ # # ][ # # ]: 0 : HelpExampleCli("getpeerinfo", "")
[ # # ]
194 [ # # ][ # # ]: 0 : + HelpExampleRpc("getpeerinfo", "")
[ # # ][ # # ]
195 : : },
196 : 0 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
197 : : {
198 : 0 : NodeContext& node = EnsureAnyNodeContext(request.context);
199 : 0 : const CConnman& connman = EnsureConnman(node);
200 : 0 : const PeerManager& peerman = EnsurePeerman(node);
201 : :
202 : 0 : std::vector<CNodeStats> vstats;
203 [ # # ]: 0 : connman.GetNodeStats(vstats);
204 : :
205 [ # # ]: 0 : UniValue ret(UniValue::VARR);
206 : :
207 [ # # ]: 0 : for (const CNodeStats& stats : vstats) {
208 [ # # ]: 0 : UniValue obj(UniValue::VOBJ);
209 : 0 : CNodeStateStats statestats;
210 [ # # ]: 0 : bool fStateStats = peerman.GetNodeStateStats(stats.nodeid, statestats);
211 : : // GetNodeStateStats() requires the existence of a CNodeState and a Peer object
212 : : // to succeed for this peer. These are created at connection initialisation and
213 : : // exist for the duration of the connection - except if there is a race where the
214 : : // peer got disconnected in between the GetNodeStats() and the GetNodeStateStats()
215 : : // calls. In this case, the peer doesn't need to be reported here.
216 [ # # ]: 0 : if (!fStateStats) {
217 : 0 : continue;
218 : : }
219 [ # # ][ # # ]: 0 : obj.pushKV("id", stats.nodeid);
[ # # ]
220 [ # # ][ # # ]: 0 : obj.pushKV("addr", stats.m_addr_name);
[ # # ]
221 [ # # ][ # # ]: 0 : if (stats.addrBind.IsValid()) {
222 [ # # ][ # # ]: 0 : obj.pushKV("addrbind", stats.addrBind.ToStringAddrPort());
[ # # ][ # # ]
223 : 0 : }
224 [ # # ]: 0 : if (!(stats.addrLocal.empty())) {
225 [ # # ][ # # ]: 0 : obj.pushKV("addrlocal", stats.addrLocal);
[ # # ]
226 : 0 : }
227 [ # # ][ # # ]: 0 : obj.pushKV("network", GetNetworkName(stats.m_network));
[ # # ][ # # ]
228 [ # # ]: 0 : if (stats.m_mapped_as != 0) {
229 [ # # ][ # # ]: 0 : obj.pushKV("mapped_as", uint64_t(stats.m_mapped_as));
[ # # ]
230 : 0 : }
231 : 0 : ServiceFlags services{statestats.their_services};
232 [ # # ][ # # ]: 0 : obj.pushKV("services", strprintf("%016x", services));
[ # # ][ # # ]
233 [ # # ][ # # ]: 0 : obj.pushKV("servicesnames", GetServicesNames(services));
[ # # ]
234 [ # # ][ # # ]: 0 : obj.pushKV("relaytxes", statestats.m_relay_txs);
[ # # ]
235 [ # # ][ # # ]: 0 : obj.pushKV("lastsend", count_seconds(stats.m_last_send));
[ # # ][ # # ]
236 [ # # ][ # # ]: 0 : obj.pushKV("lastrecv", count_seconds(stats.m_last_recv));
[ # # ][ # # ]
237 [ # # ][ # # ]: 0 : obj.pushKV("last_transaction", count_seconds(stats.m_last_tx_time));
[ # # ][ # # ]
238 [ # # ][ # # ]: 0 : obj.pushKV("last_block", count_seconds(stats.m_last_block_time));
[ # # ][ # # ]
239 [ # # ][ # # ]: 0 : obj.pushKV("bytessent", stats.nSendBytes);
[ # # ]
240 [ # # ][ # # ]: 0 : obj.pushKV("bytesrecv", stats.nRecvBytes);
[ # # ]
241 [ # # ][ # # ]: 0 : obj.pushKV("conntime", count_seconds(stats.m_connected));
[ # # ][ # # ]
242 [ # # ][ # # ]: 0 : obj.pushKV("timeoffset", stats.nTimeOffset);
[ # # ]
243 [ # # ][ # # ]: 0 : if (stats.m_last_ping_time > 0us) {
[ # # ]
244 [ # # ][ # # ]: 0 : obj.pushKV("pingtime", Ticks<SecondsDouble>(stats.m_last_ping_time));
[ # # ][ # # ]
245 : 0 : }
246 [ # # ][ # # ]: 0 : if (stats.m_min_ping_time < std::chrono::microseconds::max()) {
247 [ # # ][ # # ]: 0 : obj.pushKV("minping", Ticks<SecondsDouble>(stats.m_min_ping_time));
[ # # ][ # # ]
248 : 0 : }
249 [ # # ][ # # ]: 0 : if (statestats.m_ping_wait > 0s) {
[ # # ]
250 [ # # ][ # # ]: 0 : obj.pushKV("pingwait", Ticks<SecondsDouble>(statestats.m_ping_wait));
[ # # ][ # # ]
251 : 0 : }
252 [ # # ][ # # ]: 0 : obj.pushKV("version", stats.nVersion);
[ # # ]
253 : : // Use the sanitized form of subver here, to avoid tricksy remote peers from
254 : : // corrupting or modifying the JSON output by putting special characters in
255 : : // their ver message.
256 [ # # ][ # # ]: 0 : obj.pushKV("subver", stats.cleanSubVer);
[ # # ]
257 [ # # ][ # # ]: 0 : obj.pushKV("inbound", stats.fInbound);
[ # # ]
258 [ # # ][ # # ]: 0 : obj.pushKV("bip152_hb_to", stats.m_bip152_highbandwidth_to);
[ # # ]
259 [ # # ][ # # ]: 0 : obj.pushKV("bip152_hb_from", stats.m_bip152_highbandwidth_from);
[ # # ]
260 [ # # ][ # # ]: 0 : obj.pushKV("startingheight", statestats.m_starting_height);
[ # # ]
261 [ # # ][ # # ]: 0 : obj.pushKV("presynced_headers", statestats.presync_height);
[ # # ]
262 [ # # ][ # # ]: 0 : obj.pushKV("synced_headers", statestats.nSyncHeight);
[ # # ]
263 [ # # ][ # # ]: 0 : obj.pushKV("synced_blocks", statestats.nCommonHeight);
[ # # ]
264 [ # # ]: 0 : UniValue heights(UniValue::VARR);
265 [ # # ]: 0 : for (const int height : statestats.vHeightInFlight) {
266 [ # # ][ # # ]: 0 : heights.push_back(height);
267 : : }
268 [ # # ][ # # ]: 0 : obj.pushKV("inflight", heights);
[ # # ]
269 [ # # ][ # # ]: 0 : obj.pushKV("addr_relay_enabled", statestats.m_addr_relay_enabled);
[ # # ]
270 [ # # ][ # # ]: 0 : obj.pushKV("addr_processed", statestats.m_addr_processed);
[ # # ]
271 [ # # ][ # # ]: 0 : obj.pushKV("addr_rate_limited", statestats.m_addr_rate_limited);
[ # # ]
272 [ # # ]: 0 : UniValue permissions(UniValue::VARR);
273 [ # # ][ # # ]: 0 : for (const auto& permission : NetPermissions::ToStrings(stats.m_permission_flags)) {
274 [ # # ][ # # ]: 0 : permissions.push_back(permission);
275 : : }
276 [ # # ][ # # ]: 0 : obj.pushKV("permissions", permissions);
[ # # ]
277 [ # # ][ # # ]: 0 : obj.pushKV("minfeefilter", ValueFromAmount(statestats.m_fee_filter_received));
[ # # ]
278 : :
279 [ # # ]: 0 : UniValue sendPerMsgType(UniValue::VOBJ);
280 [ # # ]: 0 : for (const auto& i : stats.mapSendBytesPerMsgType) {
281 [ # # ]: 0 : if (i.second > 0)
282 [ # # ][ # # ]: 0 : sendPerMsgType.pushKV(i.first, i.second);
[ # # ]
283 : : }
284 [ # # ][ # # ]: 0 : obj.pushKV("bytessent_per_msg", sendPerMsgType);
[ # # ]
285 : :
286 [ # # ]: 0 : UniValue recvPerMsgType(UniValue::VOBJ);
287 [ # # ]: 0 : for (const auto& i : stats.mapRecvBytesPerMsgType) {
288 [ # # ]: 0 : if (i.second > 0)
289 [ # # ][ # # ]: 0 : recvPerMsgType.pushKV(i.first, i.second);
[ # # ]
290 : : }
291 [ # # ][ # # ]: 0 : obj.pushKV("bytesrecv_per_msg", recvPerMsgType);
[ # # ]
292 [ # # ][ # # ]: 0 : obj.pushKV("connection_type", ConnectionTypeAsString(stats.m_conn_type));
[ # # ][ # # ]
293 [ # # ][ # # ]: 0 : obj.pushKV("transport_protocol_type", TransportTypeAsString(stats.m_transport_type));
[ # # ][ # # ]
294 [ # # ][ # # ]: 0 : obj.pushKV("session_id", stats.m_session_id);
[ # # ]
295 : :
296 [ # # ][ # # ]: 0 : ret.push_back(obj);
297 [ # # # ]: 0 : }
298 : :
299 : 0 : return ret;
300 [ # # ]: 0 : },
301 : : };
302 : 0 : }
303 : :
304 : 0 : static RPCHelpMan addnode()
305 : : {
306 [ # # ][ # # ]: 0 : return RPCHelpMan{"addnode",
[ # # ]
307 : : "\nAttempts to add or remove a node from the addnode list.\n"
308 : : "Or try a connection to a node once.\n"
309 : : "Nodes added using addnode (or -connect) are protected from DoS disconnection and are not required to be\n"
310 [ # # ]: 0 : "full nodes/support SegWit as other outbound peers are (though such peers will not be synced from).\n" +
311 [ # # ][ # # ]: 0 : strprintf("Addnode connections are limited to %u at a time", MAX_ADDNODE_CONNECTIONS) +
312 : : " and are counted separately from the -maxconnections limit.\n",
313 [ # # ]: 0 : {
314 [ # # ][ # # ]: 0 : {"node", RPCArg::Type::STR, RPCArg::Optional::NO, "The address of the peer to connect to"},
[ # # ]
315 [ # # ][ # # ]: 0 : {"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"},
[ # # ]
316 [ # # ][ # # ]: 0 : {"v2transport", RPCArg::Type::BOOL, RPCArg::Default{false}, "Attempt to connect using BIP324 v2 transport protocol (ignored for 'remove' command)"},
[ # # ][ # # ]
317 : : },
318 [ # # ][ # # ]: 0 : RPCResult{RPCResult::Type::NONE, "", ""},
[ # # ][ # # ]
319 [ # # ]: 0 : RPCExamples{
320 [ # # ][ # # ]: 0 : HelpExampleCli("addnode", "\"192.168.0.6:8333\" \"onetry\" true")
[ # # ]
321 [ # # ][ # # ]: 0 : + HelpExampleRpc("addnode", "\"192.168.0.6:8333\", \"onetry\" true")
[ # # ][ # # ]
322 : : },
323 : 0 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
324 : : {
325 : 0 : const std::string command{request.params[1].get_str()};
326 [ # # ][ # # ]: 0 : if (command != "onetry" && command != "add" && command != "remove") {
[ # # ][ # # ]
[ # # ][ # # ]
327 [ # # ][ # # ]: 0 : throw std::runtime_error(
328 [ # # ]: 0 : self.ToString());
329 : : }
330 : :
331 [ # # ]: 0 : NodeContext& node = EnsureAnyNodeContext(request.context);
332 [ # # ]: 0 : CConnman& connman = EnsureConnman(node);
333 : :
334 [ # # ][ # # ]: 0 : const std::string node_arg{request.params[0].get_str()};
[ # # ]
335 [ # # ]: 0 : bool use_v2transport = self.Arg<bool>(2);
336 : :
337 [ # # ][ # # ]: 0 : if (use_v2transport && !(node.connman->GetLocalServices() & NODE_P2P_V2)) {
[ # # ]
338 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Error: v2transport requested but not enabled (see -v2transport)");
[ # # ]
339 : : }
340 : :
341 [ # # ][ # # ]: 0 : if (command == "onetry")
342 : : {
343 [ # # ]: 0 : CAddress addr;
344 [ # # ]: 0 : connman.OpenNetworkConnection(addr, /*fCountFailure=*/false, /*grant_outbound=*/{}, node_arg.c_str(), ConnectionType::MANUAL, use_v2transport);
345 [ # # ]: 0 : return UniValue::VNULL;
346 : 0 : }
347 : :
348 [ # # ][ # # ]: 0 : if (command == "add")
349 : : {
350 [ # # ][ # # ]: 0 : if (!connman.AddNode({node_arg, use_v2transport})) {
[ # # ]
351 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: Node already added");
[ # # ]
352 : : }
353 : 0 : }
354 [ # # ][ # # ]: 0 : else if (command == "remove")
355 : : {
356 [ # # ][ # # ]: 0 : if (!connman.RemoveAddedNode(node_arg)) {
357 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_CLIENT_NODE_NOT_ADDED, "Error: Node could not be removed. It has not been added previously.");
[ # # ]
358 : : }
359 : 0 : }
360 : :
361 [ # # ]: 0 : return UniValue::VNULL;
362 : 0 : },
363 : : };
364 : 0 : }
365 : :
366 : 0 : static RPCHelpMan addconnection()
367 : : {
368 [ # # ][ # # ]: 0 : return RPCHelpMan{"addconnection",
[ # # ][ # # ]
369 [ # # ]: 0 : "\nOpen an outbound connection to a specified node. This RPC is for testing only.\n",
370 [ # # ]: 0 : {
371 [ # # ][ # # ]: 0 : {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The IP address and port to attempt connecting to."},
[ # # ]
372 [ # # ][ # # ]: 0 : {"connection_type", RPCArg::Type::STR, RPCArg::Optional::NO, "Type of connection to open (\"outbound-full-relay\", \"block-relay-only\", \"addr-fetch\" or \"feeler\")."},
[ # # ]
373 : : },
374 [ # # ][ # # ]: 0 : RPCResult{
375 [ # # ][ # # ]: 0 : RPCResult::Type::OBJ, "", "",
376 [ # # ]: 0 : {
377 [ # # ][ # # ]: 0 : { RPCResult::Type::STR, "address", "Address of newly added connection." },
[ # # ]
378 [ # # ][ # # ]: 0 : { RPCResult::Type::STR, "connection_type", "Type of connection opened." },
[ # # ]
379 : : }},
380 [ # # ]: 0 : RPCExamples{
381 [ # # ][ # # ]: 0 : HelpExampleCli("addconnection", "\"192.168.0.6:8333\" \"outbound-full-relay\"")
[ # # ]
382 [ # # ][ # # ]: 0 : + HelpExampleRpc("addconnection", "\"192.168.0.6:8333\" \"outbound-full-relay\"")
[ # # ][ # # ]
383 : : },
384 : 0 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
385 : : {
386 [ # # ]: 0 : if (Params().GetChainType() != ChainType::REGTEST) {
387 [ # # ]: 0 : throw std::runtime_error("addconnection is for regression testing (-regtest mode) only.");
388 : : }
389 : :
390 : 0 : const std::string address = request.params[0].get_str();
391 [ # # ][ # # ]: 0 : const std::string conn_type_in{TrimString(request.params[1].get_str())};
[ # # ]
392 : 0 : ConnectionType conn_type{};
393 [ # # ][ # # ]: 0 : if (conn_type_in == "outbound-full-relay") {
394 : 0 : conn_type = ConnectionType::OUTBOUND_FULL_RELAY;
395 [ # # ][ # # ]: 0 : } else if (conn_type_in == "block-relay-only") {
396 : 0 : conn_type = ConnectionType::BLOCK_RELAY;
397 [ # # ][ # # ]: 0 : } else if (conn_type_in == "addr-fetch") {
398 : 0 : conn_type = ConnectionType::ADDR_FETCH;
399 [ # # ][ # # ]: 0 : } else if (conn_type_in == "feeler") {
400 : 0 : conn_type = ConnectionType::FEELER;
401 : 0 : } else {
402 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, self.ToString());
[ # # ]
403 : : }
404 : :
405 [ # # ]: 0 : NodeContext& node = EnsureAnyNodeContext(request.context);
406 [ # # ]: 0 : CConnman& connman = EnsureConnman(node);
407 : :
408 [ # # ]: 0 : const bool success = connman.AddConnection(address, conn_type);
409 [ # # ]: 0 : if (!success) {
410 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_CLIENT_NODE_CAPACITY_REACHED, "Error: Already at capacity for specified connection type.");
[ # # ]
411 : : }
412 : :
413 [ # # ]: 0 : UniValue info(UniValue::VOBJ);
414 [ # # ][ # # ]: 0 : info.pushKV("address", address);
[ # # ]
415 [ # # ][ # # ]: 0 : info.pushKV("connection_type", conn_type_in);
[ # # ]
416 : :
417 : 0 : return info;
418 [ # # ]: 0 : },
419 : : };
420 : 0 : }
421 : :
422 : 0 : static RPCHelpMan disconnectnode()
423 : : {
424 [ # # ][ # # ]: 0 : return RPCHelpMan{"disconnectnode",
[ # # ]
425 [ # # ]: 0 : "\nImmediately disconnects from the specified peer node.\n"
426 : : "\nStrictly one out of 'address' and 'nodeid' can be provided to identify the node.\n"
427 : : "\nTo disconnect by nodeid, either set 'address' to the empty string, or call using the named 'nodeid' argument only.\n",
428 [ # # ]: 0 : {
429 [ # # ][ # # ]: 0 : {"address", RPCArg::Type::STR, RPCArg::DefaultHint{"fallback to nodeid"}, "The IP address/port of the node"},
[ # # ][ # # ]
430 [ # # ][ # # ]: 0 : {"nodeid", RPCArg::Type::NUM, RPCArg::DefaultHint{"fallback to address"}, "The node ID (see getpeerinfo for node IDs)"},
[ # # ][ # # ]
431 : : },
432 [ # # ][ # # ]: 0 : RPCResult{RPCResult::Type::NONE, "", ""},
[ # # ][ # # ]
433 [ # # ]: 0 : RPCExamples{
434 [ # # ][ # # ]: 0 : HelpExampleCli("disconnectnode", "\"192.168.0.6:8333\"")
[ # # ]
435 [ # # ][ # # ]: 0 : + HelpExampleCli("disconnectnode", "\"\" 1")
[ # # ][ # # ]
436 [ # # ][ # # ]: 0 : + HelpExampleRpc("disconnectnode", "\"192.168.0.6:8333\"")
[ # # ][ # # ]
437 [ # # ][ # # ]: 0 : + HelpExampleRpc("disconnectnode", "\"\", 1")
[ # # ][ # # ]
438 : : },
439 : 0 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
440 : : {
441 : 0 : NodeContext& node = EnsureAnyNodeContext(request.context);
442 : 0 : CConnman& connman = EnsureConnman(node);
443 : :
444 : : bool success;
445 : 0 : const UniValue &address_arg = request.params[0];
446 : 0 : const UniValue &id_arg = request.params[1];
447 : :
448 [ # # ][ # # ]: 0 : if (!address_arg.isNull() && id_arg.isNull()) {
449 : : /* handle disconnect-by-address */
450 : 0 : success = connman.DisconnectNode(address_arg.get_str());
451 [ # # ][ # # ]: 0 : } else if (!id_arg.isNull() && (address_arg.isNull() || (address_arg.isStr() && address_arg.get_str().empty()))) {
452 : : /* handle disconnect-by-id */
453 : 0 : NodeId nodeid = (NodeId) id_arg.getInt<int64_t>();
454 : 0 : success = connman.DisconnectNode(nodeid);
455 : 0 : } else {
456 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMS, "Only one of address and nodeid should be provided.");
[ # # ]
457 : : }
458 : :
459 [ # # ]: 0 : if (!success) {
460 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_CLIENT_NODE_NOT_CONNECTED, "Node not found in connected nodes");
[ # # ]
461 : : }
462 : :
463 [ # # ]: 0 : return UniValue::VNULL;
464 : 0 : },
465 : : };
466 : 0 : }
467 : :
468 : 0 : static RPCHelpMan getaddednodeinfo()
469 : : {
470 [ # # ][ # # ]: 0 : return RPCHelpMan{"getaddednodeinfo",
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
471 [ # # ]: 0 : "\nReturns information about the given added node, or all added nodes\n"
472 : : "(note that onetry addnodes are not listed here)\n",
473 [ # # ]: 0 : {
474 [ # # ][ # # ]: 0 : {"node", RPCArg::Type::STR, RPCArg::DefaultHint{"all nodes"}, "If provided, return information about this specific node, otherwise all nodes are returned."},
[ # # ][ # # ]
475 : : },
476 [ # # ][ # # ]: 0 : RPCResult{
477 [ # # ][ # # ]: 0 : RPCResult::Type::ARR, "", "",
478 [ # # ]: 0 : {
479 [ # # ][ # # ]: 0 : {RPCResult::Type::OBJ, "", "",
[ # # ]
480 [ # # ]: 0 : {
481 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "addednode", "The node IP address or name (as provided to addnode)"},
[ # # ]
482 [ # # ][ # # ]: 0 : {RPCResult::Type::BOOL, "connected", "If connected"},
[ # # ]
483 [ # # ][ # # ]: 0 : {RPCResult::Type::ARR, "addresses", "Only when connected = true",
[ # # ]
484 [ # # ]: 0 : {
485 [ # # ][ # # ]: 0 : {RPCResult::Type::OBJ, "", "",
[ # # ]
486 [ # # ]: 0 : {
487 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "address", "The bitcoin server IP and port we're connected to"},
[ # # ]
488 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "connected", "connection, inbound or outbound"},
[ # # ]
489 : : }},
490 : : }},
491 : : }},
492 : : }
493 : : },
494 [ # # ]: 0 : RPCExamples{
495 [ # # ][ # # ]: 0 : HelpExampleCli("getaddednodeinfo", "\"192.168.0.201\"")
[ # # ]
496 [ # # ][ # # ]: 0 : + HelpExampleRpc("getaddednodeinfo", "\"192.168.0.201\"")
[ # # ][ # # ]
497 : : },
498 : 0 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
499 : : {
500 : 0 : NodeContext& node = EnsureAnyNodeContext(request.context);
501 : 0 : const CConnman& connman = EnsureConnman(node);
502 : :
503 : 0 : std::vector<AddedNodeInfo> vInfo = connman.GetAddedNodeInfo(/*include_connected=*/true);
504 : :
505 [ # # ][ # # ]: 0 : if (!request.params[0].isNull()) {
506 : 0 : bool found = false;
507 [ # # ]: 0 : for (const AddedNodeInfo& info : vInfo) {
508 [ # # ][ # # ]: 0 : if (info.m_params.m_added_node == request.params[0].get_str()) {
[ # # ]
509 [ # # ]: 0 : vInfo.assign(1, info);
510 : 0 : found = true;
511 : 0 : break;
512 : : }
513 : : }
514 [ # # ]: 0 : if (!found) {
515 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_CLIENT_NODE_NOT_ADDED, "Error: Node has not been added.");
[ # # ][ # # ]
516 : : }
517 : 0 : }
518 : :
519 [ # # ]: 0 : UniValue ret(UniValue::VARR);
520 : :
521 [ # # ]: 0 : for (const AddedNodeInfo& info : vInfo) {
522 [ # # ]: 0 : UniValue obj(UniValue::VOBJ);
523 [ # # ][ # # ]: 0 : obj.pushKV("addednode", info.m_params.m_added_node);
[ # # ]
524 [ # # ][ # # ]: 0 : obj.pushKV("connected", info.fConnected);
[ # # ]
525 [ # # ]: 0 : UniValue addresses(UniValue::VARR);
526 [ # # ]: 0 : if (info.fConnected) {
527 [ # # ]: 0 : UniValue address(UniValue::VOBJ);
528 [ # # ][ # # ]: 0 : address.pushKV("address", info.resolvedAddress.ToStringAddrPort());
[ # # ][ # # ]
529 [ # # ][ # # ]: 0 : address.pushKV("connected", info.fInbound ? "inbound" : "outbound");
[ # # ]
530 [ # # ][ # # ]: 0 : addresses.push_back(address);
531 : 0 : }
532 [ # # ][ # # ]: 0 : obj.pushKV("addresses", addresses);
[ # # ]
533 [ # # ][ # # ]: 0 : ret.push_back(obj);
534 : 0 : }
535 : :
536 : 0 : return ret;
537 [ # # ]: 0 : },
538 : : };
539 : 0 : }
540 : :
541 : 0 : static RPCHelpMan getnettotals()
542 : : {
543 [ # # ][ # # ]: 0 : return RPCHelpMan{"getnettotals",
[ # # ][ # # ]
544 [ # # ]: 0 : "Returns information about network traffic, including bytes in, bytes out,\n"
545 : : "and current system time.",
546 : 0 : {},
547 [ # # ][ # # ]: 0 : RPCResult{
548 [ # # ][ # # ]: 0 : RPCResult::Type::OBJ, "", "",
549 [ # # ]: 0 : {
550 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "totalbytesrecv", "Total bytes received"},
[ # # ]
551 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "totalbytessent", "Total bytes sent"},
[ # # ]
552 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM_TIME, "timemillis", "Current system " + UNIX_EPOCH_TIME + " in milliseconds"},
[ # # ][ # # ]
553 [ # # ][ # # ]: 0 : {RPCResult::Type::OBJ, "uploadtarget", "",
[ # # ]
554 [ # # ]: 0 : {
555 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "timeframe", "Length of the measuring timeframe in seconds"},
[ # # ]
556 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "target", "Target in bytes"},
[ # # ]
557 [ # # ][ # # ]: 0 : {RPCResult::Type::BOOL, "target_reached", "True if target is reached"},
[ # # ]
558 [ # # ][ # # ]: 0 : {RPCResult::Type::BOOL, "serve_historical_blocks", "True if serving historical blocks"},
[ # # ]
559 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "bytes_left_in_cycle", "Bytes left in current time cycle"},
[ # # ]
560 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "time_left_in_cycle", "Seconds left in current time cycle"},
[ # # ]
561 : : }},
562 : : }
563 : : },
564 [ # # ]: 0 : RPCExamples{
565 [ # # ][ # # ]: 0 : HelpExampleCli("getnettotals", "")
[ # # ]
566 [ # # ][ # # ]: 0 : + HelpExampleRpc("getnettotals", "")
[ # # ][ # # ]
567 : : },
568 : 0 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
569 : : {
570 : 0 : NodeContext& node = EnsureAnyNodeContext(request.context);
571 : 0 : const CConnman& connman = EnsureConnman(node);
572 : :
573 [ # # ]: 0 : UniValue obj(UniValue::VOBJ);
574 [ # # ][ # # ]: 0 : obj.pushKV("totalbytesrecv", connman.GetTotalBytesRecv());
[ # # ][ # # ]
575 [ # # ][ # # ]: 0 : obj.pushKV("totalbytessent", connman.GetTotalBytesSent());
[ # # ][ # # ]
576 [ # # ][ # # ]: 0 : obj.pushKV("timemillis", TicksSinceEpoch<std::chrono::milliseconds>(SystemClock::now()));
[ # # ][ # # ]
577 : :
578 [ # # ]: 0 : UniValue outboundLimit(UniValue::VOBJ);
579 [ # # ][ # # ]: 0 : outboundLimit.pushKV("timeframe", count_seconds(connman.GetMaxOutboundTimeframe()));
[ # # ][ # # ]
580 [ # # ][ # # ]: 0 : outboundLimit.pushKV("target", connman.GetMaxOutboundTarget());
[ # # ][ # # ]
581 [ # # ][ # # ]: 0 : outboundLimit.pushKV("target_reached", connman.OutboundTargetReached(false));
[ # # ][ # # ]
582 [ # # ][ # # ]: 0 : outboundLimit.pushKV("serve_historical_blocks", !connman.OutboundTargetReached(true));
[ # # ][ # # ]
583 [ # # ][ # # ]: 0 : outboundLimit.pushKV("bytes_left_in_cycle", connman.GetOutboundTargetBytesLeft());
[ # # ][ # # ]
584 [ # # ][ # # ]: 0 : outboundLimit.pushKV("time_left_in_cycle", count_seconds(connman.GetMaxOutboundTimeLeftInCycle()));
[ # # ][ # # ]
585 [ # # ][ # # ]: 0 : obj.pushKV("uploadtarget", outboundLimit);
[ # # ]
586 : 0 : return obj;
587 [ # # ]: 0 : },
588 : : };
589 : 0 : }
590 : :
591 : 0 : static UniValue GetNetworksInfo()
592 : : {
593 [ # # ]: 0 : UniValue networks(UniValue::VARR);
594 [ # # ]: 0 : for (int n = 0; n < NET_MAX; ++n) {
595 : 0 : enum Network network = static_cast<enum Network>(n);
596 [ # # ][ # # ]: 0 : if (network == NET_UNROUTABLE || network == NET_INTERNAL) continue;
597 [ # # ]: 0 : Proxy proxy;
598 [ # # ]: 0 : UniValue obj(UniValue::VOBJ);
599 [ # # ]: 0 : GetProxy(network, proxy);
600 [ # # ][ # # ]: 0 : obj.pushKV("name", GetNetworkName(network));
[ # # ][ # # ]
601 [ # # ][ # # ]: 0 : obj.pushKV("limited", !g_reachable_nets.Contains(network));
[ # # ][ # # ]
602 [ # # ][ # # ]: 0 : obj.pushKV("reachable", g_reachable_nets.Contains(network));
[ # # ][ # # ]
603 [ # # ][ # # ]: 0 : obj.pushKV("proxy", proxy.IsValid() ? proxy.proxy.ToStringAddrPort() : std::string());
[ # # ][ # # ]
[ # # ][ # # ]
604 [ # # ][ # # ]: 0 : obj.pushKV("proxy_randomize_credentials", proxy.randomize_credentials);
[ # # ]
605 [ # # ][ # # ]: 0 : networks.push_back(obj);
606 : 0 : }
607 : 0 : return networks;
608 [ # # ]: 0 : }
609 : :
610 : 0 : static RPCHelpMan getnetworkinfo()
611 : : {
612 [ # # ][ # # ]: 0 : return RPCHelpMan{"getnetworkinfo",
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
613 [ # # ]: 0 : "Returns an object containing various state info regarding P2P networking.\n",
614 : 0 : {},
615 [ # # ][ # # ]: 0 : RPCResult{
616 [ # # ][ # # ]: 0 : RPCResult::Type::OBJ, "", "",
617 [ # # ]: 0 : {
618 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "version", "the server version"},
[ # # ]
619 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "subversion", "the server subversion string"},
[ # # ]
620 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "protocolversion", "the protocol version"},
[ # # ]
621 [ # # ][ # # ]: 0 : {RPCResult::Type::STR_HEX, "localservices", "the services we offer to the network"},
[ # # ]
622 [ # # ][ # # ]: 0 : {RPCResult::Type::ARR, "localservicesnames", "the services we offer to the network, in human-readable form",
[ # # ]
623 [ # # ]: 0 : {
624 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "SERVICE_NAME", "the service name"},
[ # # ]
625 : : }},
626 [ # # ][ # # ]: 0 : {RPCResult::Type::BOOL, "localrelay", "true if transaction relay is requested from peers"},
[ # # ]
627 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "timeoffset", "the time offset"},
[ # # ]
628 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "connections", "the total number of connections"},
[ # # ]
629 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "connections_in", "the number of inbound connections"},
[ # # ]
630 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "connections_out", "the number of outbound connections"},
[ # # ]
631 [ # # ][ # # ]: 0 : {RPCResult::Type::BOOL, "networkactive", "whether p2p networking is enabled"},
[ # # ]
632 [ # # ][ # # ]: 0 : {RPCResult::Type::ARR, "networks", "information per network",
[ # # ]
633 [ # # ]: 0 : {
634 [ # # ][ # # ]: 0 : {RPCResult::Type::OBJ, "", "",
[ # # ]
635 [ # # ]: 0 : {
636 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "name", "network (" + Join(GetNetworkNames(), ", ") + ")"},
[ # # ][ # # ]
[ # # ][ # # ]
637 [ # # ][ # # ]: 0 : {RPCResult::Type::BOOL, "limited", "is the network limited using -onlynet?"},
[ # # ]
638 [ # # ][ # # ]: 0 : {RPCResult::Type::BOOL, "reachable", "is the network reachable?"},
[ # # ]
639 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "proxy", "(\"host:port\") the proxy that is used for this network, or empty if none"},
[ # # ]
640 [ # # ][ # # ]: 0 : {RPCResult::Type::BOOL, "proxy_randomize_credentials", "Whether randomized credentials are used"},
[ # # ]
641 : : }},
642 : : }},
643 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "relayfee", "minimum relay fee rate for transactions in " + CURRENCY_UNIT + "/kvB"},
[ # # ][ # # ]
644 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "incrementalfee", "minimum fee rate increment for mempool limiting or replacement in " + CURRENCY_UNIT + "/kvB"},
[ # # ][ # # ]
645 [ # # ][ # # ]: 0 : {RPCResult::Type::ARR, "localaddresses", "list of local addresses",
[ # # ]
646 [ # # ]: 0 : {
647 [ # # ][ # # ]: 0 : {RPCResult::Type::OBJ, "", "",
[ # # ]
648 [ # # ]: 0 : {
649 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "address", "network address"},
[ # # ]
650 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "port", "network port"},
[ # # ]
651 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "score", "relative score"},
[ # # ]
652 : : }},
653 : : }},
654 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "warnings", "any network and blockchain warnings"},
[ # # ]
655 : : }
656 : : },
657 [ # # ]: 0 : RPCExamples{
658 [ # # ][ # # ]: 0 : HelpExampleCli("getnetworkinfo", "")
[ # # ]
659 [ # # ][ # # ]: 0 : + HelpExampleRpc("getnetworkinfo", "")
[ # # ][ # # ]
660 : : },
661 : 0 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
662 : : {
663 : 0 : LOCK(cs_main);
664 [ # # ]: 0 : UniValue obj(UniValue::VOBJ);
665 [ # # ][ # # ]: 0 : obj.pushKV("version", CLIENT_VERSION);
[ # # ]
666 [ # # ][ # # ]: 0 : obj.pushKV("subversion", strSubVersion);
[ # # ]
667 [ # # ][ # # ]: 0 : obj.pushKV("protocolversion",PROTOCOL_VERSION);
[ # # ]
668 [ # # ]: 0 : NodeContext& node = EnsureAnyNodeContext(request.context);
669 [ # # ]: 0 : if (node.connman) {
670 [ # # ]: 0 : ServiceFlags services = node.connman->GetLocalServices();
671 [ # # ][ # # ]: 0 : obj.pushKV("localservices", strprintf("%016x", services));
[ # # ][ # # ]
672 [ # # ][ # # ]: 0 : obj.pushKV("localservicesnames", GetServicesNames(services));
[ # # ]
673 : 0 : }
674 [ # # ]: 0 : if (node.peerman) {
675 [ # # ][ # # ]: 0 : obj.pushKV("localrelay", !node.peerman->IgnoresIncomingTxs());
[ # # ][ # # ]
676 : 0 : }
677 [ # # ][ # # ]: 0 : obj.pushKV("timeoffset", GetTimeOffset());
[ # # ][ # # ]
678 [ # # ]: 0 : if (node.connman) {
679 [ # # ][ # # ]: 0 : obj.pushKV("networkactive", node.connman->GetNetworkActive());
[ # # ][ # # ]
680 [ # # ][ # # ]: 0 : obj.pushKV("connections", node.connman->GetNodeCount(ConnectionDirection::Both));
[ # # ][ # # ]
681 [ # # ][ # # ]: 0 : obj.pushKV("connections_in", node.connman->GetNodeCount(ConnectionDirection::In));
[ # # ][ # # ]
682 [ # # ][ # # ]: 0 : obj.pushKV("connections_out", node.connman->GetNodeCount(ConnectionDirection::Out));
[ # # ][ # # ]
683 : 0 : }
684 [ # # ][ # # ]: 0 : obj.pushKV("networks", GetNetworksInfo());
[ # # ]
685 [ # # ]: 0 : if (node.mempool) {
686 : : // Those fields can be deprecated, to be replaced by the getmempoolinfo fields
687 [ # # ][ # # ]: 0 : obj.pushKV("relayfee", ValueFromAmount(node.mempool->m_min_relay_feerate.GetFeePerK()));
[ # # ][ # # ]
688 [ # # ][ # # ]: 0 : obj.pushKV("incrementalfee", ValueFromAmount(node.mempool->m_incremental_relay_feerate.GetFeePerK()));
[ # # ][ # # ]
689 : 0 : }
690 [ # # ]: 0 : UniValue localAddresses(UniValue::VARR);
691 : : {
692 [ # # ][ # # ]: 0 : LOCK(g_maplocalhost_mutex);
693 [ # # ]: 0 : for (const std::pair<const CNetAddr, LocalServiceInfo> &item : mapLocalHost)
694 : : {
695 [ # # ]: 0 : UniValue rec(UniValue::VOBJ);
696 [ # # ][ # # ]: 0 : rec.pushKV("address", item.first.ToStringAddr());
[ # # ][ # # ]
697 [ # # ][ # # ]: 0 : rec.pushKV("port", item.second.nPort);
[ # # ]
698 [ # # ][ # # ]: 0 : rec.pushKV("score", item.second.nScore);
[ # # ]
699 [ # # ][ # # ]: 0 : localAddresses.push_back(rec);
700 : 0 : }
701 : 0 : }
702 [ # # ][ # # ]: 0 : obj.pushKV("localaddresses", localAddresses);
[ # # ]
703 [ # # ][ # # ]: 0 : obj.pushKV("warnings", GetWarnings(false).original);
[ # # ][ # # ]
704 : 0 : return obj;
705 [ # # ]: 0 : },
706 : : };
707 : 0 : }
708 : :
709 : 0 : static RPCHelpMan setban()
710 : : {
711 [ # # ][ # # ]: 0 : return RPCHelpMan{"setban",
[ # # ]
712 [ # # ]: 0 : "\nAttempts to add or remove an IP/Subnet from the banned list.\n",
713 [ # # ]: 0 : {
714 [ # # ][ # # ]: 0 : {"subnet", RPCArg::Type::STR, RPCArg::Optional::NO, "The IP/Subnet (see getpeerinfo for nodes IP) with an optional netmask (default is /32 = single IP)"},
[ # # ]
715 [ # # ][ # # ]: 0 : {"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"},
[ # # ]
716 [ # # ][ # # ]: 0 : {"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)"},
[ # # ][ # # ]
717 [ # # ][ # # ]: 0 : {"absolute", RPCArg::Type::BOOL, RPCArg::Default{false}, "If set, the bantime must be an absolute timestamp expressed in " + UNIX_EPOCH_TIME},
[ # # ][ # # ]
718 : : },
719 [ # # ][ # # ]: 0 : RPCResult{RPCResult::Type::NONE, "", ""},
[ # # ][ # # ]
720 [ # # ]: 0 : RPCExamples{
721 [ # # ][ # # ]: 0 : HelpExampleCli("setban", "\"192.168.0.6\" \"add\" 86400")
[ # # ]
722 [ # # ][ # # ]: 0 : + HelpExampleCli("setban", "\"192.168.0.0/24\" \"add\"")
[ # # ][ # # ]
723 [ # # ][ # # ]: 0 : + HelpExampleRpc("setban", "\"192.168.0.6\", \"add\", 86400")
[ # # ][ # # ]
724 : : },
725 : 0 : [&](const RPCHelpMan& help, const JSONRPCRequest& request) -> UniValue
726 : : {
727 : 0 : std::string strCommand;
728 [ # # ][ # # ]: 0 : if (!request.params[1].isNull())
729 [ # # ][ # # ]: 0 : strCommand = request.params[1].get_str();
[ # # ]
730 [ # # ][ # # ]: 0 : if (strCommand != "add" && strCommand != "remove") {
[ # # ][ # # ]
731 [ # # ][ # # ]: 0 : throw std::runtime_error(help.ToString());
[ # # ]
732 : : }
733 [ # # ]: 0 : NodeContext& node = EnsureAnyNodeContext(request.context);
734 [ # # ]: 0 : BanMan& banman = EnsureBanman(node);
735 : :
736 [ # # ]: 0 : CSubNet subNet;
737 [ # # ]: 0 : CNetAddr netAddr;
738 : 0 : bool isSubnet = false;
739 : :
740 [ # # ][ # # ]: 0 : if (request.params[0].get_str().find('/') != std::string::npos)
[ # # ]
741 : 0 : isSubnet = true;
742 : :
743 [ # # ]: 0 : if (!isSubnet) {
744 [ # # ][ # # ]: 0 : const std::optional<CNetAddr> addr{LookupHost(request.params[0].get_str(), false)};
[ # # ][ # # ]
745 [ # # ]: 0 : if (addr.has_value()) {
746 [ # # ][ # # ]: 0 : netAddr = static_cast<CNetAddr>(MaybeFlipIPv6toCJDNS(CService{addr.value(), /*port=*/0}));
[ # # ]
747 : 0 : }
748 : 0 : }
749 : : else
750 [ # # ][ # # ]: 0 : subNet = LookupSubNet(request.params[0].get_str());
[ # # ]
751 : :
752 [ # # ][ # # ]: 0 : if (! (isSubnet ? subNet.IsValid() : netAddr.IsValid()) )
[ # # ][ # # ]
753 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_CLIENT_INVALID_IP_OR_SUBNET, "Error: Invalid IP/Subnet");
[ # # ]
754 : :
755 [ # # ][ # # ]: 0 : if (strCommand == "add")
756 : : {
757 [ # # ][ # # ]: 0 : if (isSubnet ? banman.IsBanned(subNet) : banman.IsBanned(netAddr)) {
[ # # ][ # # ]
758 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: IP/Subnet already banned");
[ # # ]
759 : : }
760 : :
761 : 0 : int64_t banTime = 0; //use standard bantime if not specified
762 [ # # ][ # # ]: 0 : if (!request.params[2].isNull())
763 [ # # ][ # # ]: 0 : banTime = request.params[2].getInt<int64_t>();
764 : :
765 [ # # ][ # # ]: 0 : const bool absolute{request.params[3].isNull() ? false : request.params[3].get_bool()};
[ # # ][ # # ]
766 : :
767 [ # # ][ # # ]: 0 : if (absolute && banTime < GetTime()) {
[ # # ]
768 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Error: Absolute timestamp is in the past");
[ # # ]
769 : : }
770 : :
771 [ # # ]: 0 : if (isSubnet) {
772 [ # # ]: 0 : banman.Ban(subNet, banTime, absolute);
773 [ # # ]: 0 : if (node.connman) {
774 [ # # ]: 0 : node.connman->DisconnectNode(subNet);
775 : 0 : }
776 : 0 : } else {
777 [ # # ]: 0 : banman.Ban(netAddr, banTime, absolute);
778 [ # # ]: 0 : if (node.connman) {
779 [ # # ]: 0 : node.connman->DisconnectNode(netAddr);
780 : 0 : }
781 : : }
782 : 0 : }
783 [ # # ][ # # ]: 0 : else if(strCommand == "remove")
784 : : {
785 [ # # ][ # # ]: 0 : if (!( isSubnet ? banman.Unban(subNet) : banman.Unban(netAddr) )) {
[ # # ][ # # ]
786 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_CLIENT_INVALID_IP_OR_SUBNET, "Error: Unban failed. Requested address/subnet was not previously manually banned.");
[ # # ]
787 : : }
788 : 0 : }
789 [ # # ]: 0 : return UniValue::VNULL;
790 : 0 : },
791 : : };
792 : 0 : }
793 : :
794 : 0 : static RPCHelpMan listbanned()
795 : : {
796 [ # # ][ # # ]: 0 : return RPCHelpMan{"listbanned",
[ # # ][ # # ]
797 [ # # ]: 0 : "\nList all manually banned IPs/Subnets.\n",
798 : 0 : {},
799 [ # # ][ # # ]: 0 : RPCResult{RPCResult::Type::ARR, "", "",
[ # # ][ # # ]
800 [ # # ]: 0 : {
801 [ # # ][ # # ]: 0 : {RPCResult::Type::OBJ, "", "",
[ # # ]
802 [ # # ]: 0 : {
803 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "address", "The IP/Subnet of the banned node"},
[ # # ]
804 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM_TIME, "ban_created", "The " + UNIX_EPOCH_TIME + " the ban was created"},
[ # # ][ # # ]
805 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM_TIME, "banned_until", "The " + UNIX_EPOCH_TIME + " the ban expires"},
[ # # ][ # # ]
806 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM_TIME, "ban_duration", "The ban duration, in seconds"},
[ # # ]
807 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM_TIME, "time_remaining", "The time remaining until the ban expires, in seconds"},
[ # # ]
808 : : }},
809 : : }},
810 [ # # ]: 0 : RPCExamples{
811 [ # # ][ # # ]: 0 : HelpExampleCli("listbanned", "")
[ # # ]
812 [ # # ][ # # ]: 0 : + HelpExampleRpc("listbanned", "")
[ # # ][ # # ]
813 : : },
814 : 0 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
815 : : {
816 : 0 : BanMan& banman = EnsureAnyBanman(request.context);
817 : :
818 : 0 : banmap_t banMap;
819 [ # # ]: 0 : banman.GetBanned(banMap);
820 [ # # ]: 0 : const int64_t current_time{GetTime()};
821 : :
822 [ # # ]: 0 : UniValue bannedAddresses(UniValue::VARR);
823 [ # # ]: 0 : for (const auto& entry : banMap)
824 : : {
825 : 0 : const CBanEntry& banEntry = entry.second;
826 [ # # ]: 0 : UniValue rec(UniValue::VOBJ);
827 [ # # ][ # # ]: 0 : rec.pushKV("address", entry.first.ToString());
[ # # ][ # # ]
828 [ # # ][ # # ]: 0 : rec.pushKV("ban_created", banEntry.nCreateTime);
[ # # ]
829 [ # # ][ # # ]: 0 : rec.pushKV("banned_until", banEntry.nBanUntil);
[ # # ]
830 [ # # ][ # # ]: 0 : rec.pushKV("ban_duration", (banEntry.nBanUntil - banEntry.nCreateTime));
[ # # ]
831 [ # # ][ # # ]: 0 : rec.pushKV("time_remaining", (banEntry.nBanUntil - current_time));
[ # # ]
832 : :
833 [ # # ][ # # ]: 0 : bannedAddresses.push_back(rec);
834 : 0 : }
835 : :
836 : 0 : return bannedAddresses;
837 [ # # ]: 0 : },
838 : : };
839 : 0 : }
840 : :
841 : 0 : static RPCHelpMan clearbanned()
842 : : {
843 [ # # ][ # # ]: 0 : return RPCHelpMan{"clearbanned",
844 [ # # ]: 0 : "\nClear all banned IPs.\n",
845 : 0 : {},
846 [ # # ][ # # ]: 0 : RPCResult{RPCResult::Type::NONE, "", ""},
[ # # ][ # # ]
847 [ # # ]: 0 : RPCExamples{
848 [ # # ][ # # ]: 0 : HelpExampleCli("clearbanned", "")
[ # # ]
849 [ # # ][ # # ]: 0 : + HelpExampleRpc("clearbanned", "")
[ # # ][ # # ]
850 : : },
851 : 0 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
852 : : {
853 : 0 : BanMan& banman = EnsureAnyBanman(request.context);
854 : :
855 : 0 : banman.ClearBanned();
856 : :
857 [ # # ]: 0 : return UniValue::VNULL;
858 : 0 : },
859 : : };
860 : 0 : }
861 : :
862 : 0 : static RPCHelpMan setnetworkactive()
863 : : {
864 [ # # ][ # # ]: 0 : return RPCHelpMan{"setnetworkactive",
[ # # ]
865 [ # # ]: 0 : "\nDisable/enable all p2p network activity.\n",
866 [ # # ]: 0 : {
867 [ # # ][ # # ]: 0 : {"state", RPCArg::Type::BOOL, RPCArg::Optional::NO, "true to enable networking, false to disable"},
[ # # ]
868 : : },
869 [ # # ][ # # ]: 0 : RPCResult{RPCResult::Type::BOOL, "", "The value that was passed in"},
[ # # ][ # # ]
870 [ # # ][ # # ]: 0 : RPCExamples{""},
871 : 0 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
872 : : {
873 : 0 : NodeContext& node = EnsureAnyNodeContext(request.context);
874 : 0 : CConnman& connman = EnsureConnman(node);
875 : :
876 : 0 : connman.SetNetworkActive(request.params[0].get_bool());
877 : :
878 : 0 : return connman.GetNetworkActive();
879 : : },
880 : : };
881 : 0 : }
882 : :
883 : 0 : static RPCHelpMan getnodeaddresses()
884 : : {
885 [ # # ][ # # ]: 0 : return RPCHelpMan{"getnodeaddresses",
[ # # ][ # # ]
[ # # ]
886 [ # # ]: 0 : "Return known addresses, after filtering for quality and recency.\n"
887 : : "These can potentially be used to find new peers in the network.\n"
888 : : "The total number of addresses known to the node may be higher.",
889 [ # # ]: 0 : {
890 [ # # ][ # # ]: 0 : {"count", RPCArg::Type::NUM, RPCArg::Default{1}, "The maximum number of addresses to return. Specify 0 to return all known addresses."},
[ # # ][ # # ]
891 [ # # ][ # # ]: 0 : {"network", RPCArg::Type::STR, RPCArg::DefaultHint{"all networks"}, "Return only addresses of the specified network. Can be one of: " + Join(GetNetworkNames(), ", ") + "."},
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
892 : : },
893 [ # # ][ # # ]: 0 : RPCResult{
894 [ # # ][ # # ]: 0 : RPCResult::Type::ARR, "", "",
895 [ # # ]: 0 : {
896 [ # # ][ # # ]: 0 : {RPCResult::Type::OBJ, "", "",
[ # # ]
897 [ # # ]: 0 : {
898 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM_TIME, "time", "The " + UNIX_EPOCH_TIME + " when the node was last seen"},
[ # # ][ # # ]
899 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "services", "The services offered by the node"},
[ # # ]
900 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "address", "The address of the node"},
[ # # ]
901 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "port", "The port number of the node"},
[ # # ]
902 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "network", "The network (" + Join(GetNetworkNames(), ", ") + ") the node connected through"},
[ # # ][ # # ]
[ # # ][ # # ]
903 : : }},
904 : : }
905 : : },
906 [ # # ]: 0 : RPCExamples{
907 [ # # ][ # # ]: 0 : HelpExampleCli("getnodeaddresses", "8")
[ # # ]
908 [ # # ][ # # ]: 0 : + HelpExampleCli("getnodeaddresses", "4 \"i2p\"")
[ # # ][ # # ]
909 [ # # ][ # # ]: 0 : + HelpExampleCli("-named getnodeaddresses", "network=onion count=12")
[ # # ][ # # ]
910 [ # # ][ # # ]: 0 : + HelpExampleRpc("getnodeaddresses", "8")
[ # # ][ # # ]
911 [ # # ][ # # ]: 0 : + HelpExampleRpc("getnodeaddresses", "4, \"i2p\"")
[ # # ][ # # ]
912 : : },
913 : 0 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
914 : : {
915 : 0 : NodeContext& node = EnsureAnyNodeContext(request.context);
916 : 0 : const CConnman& connman = EnsureConnman(node);
917 : :
918 [ # # ]: 0 : const int count{request.params[0].isNull() ? 1 : request.params[0].getInt<int>()};
919 [ # # ][ # # ]: 0 : if (count < 0) throw JSONRPCError(RPC_INVALID_PARAMETER, "Address count out of range");
[ # # ][ # # ]
920 : :
921 [ # # ]: 0 : const std::optional<Network> network{request.params[1].isNull() ? std::nullopt : std::optional<Network>{ParseNetwork(request.params[1].get_str())}};
922 [ # # ]: 0 : if (network == NET_UNROUTABLE) {
923 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Network not recognized: %s", request.params[1].get_str()));
[ # # ][ # # ]
[ # # ]
924 : : }
925 : :
926 : : // returns a shuffled list of CAddress
927 : 0 : const std::vector<CAddress> vAddr{connman.GetAddresses(count, /*max_pct=*/0, network)};
928 [ # # ]: 0 : UniValue ret(UniValue::VARR);
929 : :
930 [ # # ]: 0 : for (const CAddress& addr : vAddr) {
931 [ # # ]: 0 : UniValue obj(UniValue::VOBJ);
932 [ # # ][ # # ]: 0 : obj.pushKV("time", int64_t{TicksSinceEpoch<std::chrono::seconds>(addr.nTime)});
[ # # ][ # # ]
933 [ # # ][ # # ]: 0 : obj.pushKV("services", (uint64_t)addr.nServices);
[ # # ]
934 [ # # ][ # # ]: 0 : obj.pushKV("address", addr.ToStringAddr());
[ # # ][ # # ]
935 [ # # ][ # # ]: 0 : obj.pushKV("port", addr.GetPort());
[ # # ][ # # ]
936 [ # # ][ # # ]: 0 : obj.pushKV("network", GetNetworkName(addr.GetNetClass()));
[ # # ][ # # ]
[ # # ]
937 [ # # ][ # # ]: 0 : ret.push_back(obj);
938 : 0 : }
939 : 0 : return ret;
940 [ # # ]: 0 : },
941 : : };
942 : 0 : }
943 : :
944 : 0 : static RPCHelpMan addpeeraddress()
945 : : {
946 [ # # ][ # # ]: 0 : return RPCHelpMan{"addpeeraddress",
[ # # ][ # # ]
947 [ # # ]: 0 : "\nAdd the address of a potential peer to the address manager. This RPC is for testing only.\n",
948 [ # # ]: 0 : {
949 [ # # ][ # # ]: 0 : {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The IP address of the peer"},
[ # # ]
950 [ # # ][ # # ]: 0 : {"port", RPCArg::Type::NUM, RPCArg::Optional::NO, "The port of the peer"},
[ # # ]
951 [ # # ][ # # ]: 0 : {"tried", RPCArg::Type::BOOL, RPCArg::Default{false}, "If true, attempt to add the peer to the tried addresses table"},
[ # # ][ # # ]
952 : : },
953 [ # # ][ # # ]: 0 : RPCResult{
954 [ # # ][ # # ]: 0 : RPCResult::Type::OBJ, "", "",
955 [ # # ]: 0 : {
956 [ # # ][ # # ]: 0 : {RPCResult::Type::BOOL, "success", "whether the peer address was successfully added to the address manager"},
[ # # ]
957 : : },
958 : : },
959 [ # # ]: 0 : RPCExamples{
960 [ # # ][ # # ]: 0 : HelpExampleCli("addpeeraddress", "\"1.2.3.4\" 8333 true")
[ # # ]
961 [ # # ][ # # ]: 0 : + HelpExampleRpc("addpeeraddress", "\"1.2.3.4\", 8333, true")
[ # # ][ # # ]
962 : : },
963 : 0 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
964 : : {
965 : 0 : AddrMan& addrman = EnsureAnyAddrman(request.context);
966 : :
967 : 0 : const std::string& addr_string{request.params[0].get_str()};
968 : 0 : const auto port{request.params[1].getInt<uint16_t>()};
969 [ # # ]: 0 : const bool tried{request.params[2].isNull() ? false : request.params[2].get_bool()};
970 : :
971 [ # # ]: 0 : UniValue obj(UniValue::VOBJ);
972 [ # # ][ # # ]: 0 : std::optional<CNetAddr> net_addr{LookupHost(addr_string, false)};
973 : 0 : bool success{false};
974 : :
975 [ # # ]: 0 : if (net_addr.has_value()) {
976 [ # # ][ # # ]: 0 : CService service{net_addr.value(), port};
977 [ # # ][ # # ]: 0 : CAddress address{MaybeFlipIPv6toCJDNS(service), ServiceFlags{NODE_NETWORK | NODE_WITNESS}};
978 [ # # ]: 0 : address.nTime = Now<NodeSeconds>();
979 : : // The source address is set equal to the address. This is equivalent to the peer
980 : : // announcing itself.
981 [ # # ][ # # ]: 0 : if (addrman.Add({address}, address)) {
[ # # ][ # # ]
[ # # ]
982 : 0 : success = true;
983 [ # # ]: 0 : if (tried) {
984 : : // Attempt to move the address to the tried addresses table.
985 [ # # ][ # # ]: 0 : addrman.Good(address);
986 : 0 : }
987 : 0 : }
988 : 0 : }
989 : :
990 [ # # ][ # # ]: 0 : obj.pushKV("success", success);
[ # # ]
991 : 0 : return obj;
992 [ # # ]: 0 : },
993 : : };
994 : 0 : }
995 : :
996 : 0 : static RPCHelpMan sendmsgtopeer()
997 : : {
998 [ # # ][ # # ]: 0 : return RPCHelpMan{
999 [ # # ]: 0 : "sendmsgtopeer",
1000 [ # # ]: 0 : "Send a p2p message to a peer specified by id.\n"
1001 : : "The message type and body must be provided, the message header will be generated.\n"
1002 : : "This RPC is for testing only.",
1003 [ # # ]: 0 : {
1004 [ # # ][ # # ]: 0 : {"peer_id", RPCArg::Type::NUM, RPCArg::Optional::NO, "The peer to send the message to."},
[ # # ]
1005 [ # # ][ # # ]: 0 : {"msg_type", RPCArg::Type::STR, RPCArg::Optional::NO, strprintf("The message type (maximum length %i)", CMessageHeader::COMMAND_SIZE)},
[ # # ]
1006 [ # # ][ # # ]: 0 : {"msg", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The serialized message body to send, in hex, without a message header"},
[ # # ]
1007 : : },
1008 [ # # ][ # # ]: 0 : RPCResult{RPCResult::Type::OBJ, "", "", std::vector<RPCResult>{}},
[ # # ][ # # ]
1009 [ # # ]: 0 : RPCExamples{
1010 [ # # ][ # # ]: 0 : HelpExampleCli("sendmsgtopeer", "0 \"addr\" \"ffffff\"") + HelpExampleRpc("sendmsgtopeer", "0 \"addr\" \"ffffff\"")},
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
1011 : 0 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue {
1012 : 0 : const NodeId peer_id{request.params[0].getInt<int64_t>()};
1013 : 0 : const std::string& msg_type{request.params[1].get_str()};
1014 [ # # ]: 0 : if (msg_type.size() > CMessageHeader::COMMAND_SIZE) {
1015 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Error: msg_type too long, max length is %i", CMessageHeader::COMMAND_SIZE));
[ # # ]
1016 : : }
1017 : 0 : auto msg{TryParseHex<unsigned char>(request.params[2].get_str())};
1018 [ # # ]: 0 : if (!msg.has_value()) {
1019 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Error parsing input for msg");
[ # # ]
1020 : : }
1021 : :
1022 [ # # ]: 0 : NodeContext& node = EnsureAnyNodeContext(request.context);
1023 [ # # ]: 0 : CConnman& connman = EnsureConnman(node);
1024 : :
1025 : 0 : CSerializedNetMsg msg_ser;
1026 [ # # ][ # # ]: 0 : msg_ser.data = msg.value();
1027 [ # # ]: 0 : msg_ser.m_type = msg_type;
1028 : :
1029 [ # # ]: 0 : bool success = connman.ForNode(peer_id, [&](CNode* node) {
1030 : 0 : connman.PushMessage(node, std::move(msg_ser));
1031 : 0 : return true;
1032 : : });
1033 : :
1034 [ # # ]: 0 : if (!success) {
1035 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_MISC_ERROR, "Error: Could not send message to peer");
[ # # ]
1036 : : }
1037 : :
1038 [ # # ]: 0 : UniValue ret{UniValue::VOBJ};
1039 : 0 : return ret;
1040 [ # # ]: 0 : },
1041 : : };
1042 : 0 : }
1043 : :
1044 : 0 : static RPCHelpMan getaddrmaninfo()
1045 : : {
1046 [ # # ][ # # ]: 0 : return RPCHelpMan{
[ # # ]
1047 [ # # ]: 0 : "getaddrmaninfo",
1048 [ # # ]: 0 : "\nProvides information about the node's address manager by returning the number of "
1049 : : "addresses in the `new` and `tried` tables and their sum for all networks.\n",
1050 : 0 : {},
1051 [ # # ][ # # ]: 0 : RPCResult{
1052 [ # # ][ # # ]: 0 : RPCResult::Type::OBJ_DYN, "", "json object with network type as keys", {
[ # # ]
1053 [ # # ][ # # ]: 0 : {RPCResult::Type::OBJ, "network", "the network (" + Join(GetNetworkNames(), ", ") + ", all_networks)", {
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
1054 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "new", "number of addresses in the new table, which represent potential peers the node has discovered but hasn't yet successfully connected to."},
[ # # ]
1055 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "tried", "number of addresses in the tried table, which represent peers the node has successfully connected to in the past."},
[ # # ]
1056 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "total", "total number of addresses in both new/tried tables"},
[ # # ]
1057 : : }},
1058 : : }},
1059 [ # # ][ # # ]: 0 : RPCExamples{HelpExampleCli("getaddrmaninfo", "") + HelpExampleRpc("getaddrmaninfo", "")},
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
1060 : 0 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue {
1061 : 0 : AddrMan& addrman = EnsureAnyAddrman(request.context);
1062 : :
1063 [ # # ]: 0 : UniValue ret(UniValue::VOBJ);
1064 [ # # ]: 0 : for (int n = 0; n < NET_MAX; ++n) {
1065 : 0 : enum Network network = static_cast<enum Network>(n);
1066 [ # # ][ # # ]: 0 : if (network == NET_UNROUTABLE || network == NET_INTERNAL) continue;
1067 [ # # ]: 0 : UniValue obj(UniValue::VOBJ);
1068 [ # # ][ # # ]: 0 : obj.pushKV("new", addrman.Size(network, true));
[ # # ][ # # ]
1069 [ # # ][ # # ]: 0 : obj.pushKV("tried", addrman.Size(network, false));
[ # # ][ # # ]
1070 [ # # ][ # # ]: 0 : obj.pushKV("total", addrman.Size(network));
[ # # ][ # # ]
1071 [ # # ][ # # ]: 0 : ret.pushKV(GetNetworkName(network), obj);
[ # # ]
1072 : 0 : }
1073 [ # # ]: 0 : UniValue obj(UniValue::VOBJ);
1074 [ # # ][ # # ]: 0 : obj.pushKV("new", addrman.Size(std::nullopt, true));
[ # # ][ # # ]
1075 [ # # ][ # # ]: 0 : obj.pushKV("tried", addrman.Size(std::nullopt, false));
[ # # ][ # # ]
1076 [ # # ][ # # ]: 0 : obj.pushKV("total", addrman.Size());
[ # # ][ # # ]
1077 [ # # ][ # # ]: 0 : ret.pushKV("all_networks", obj);
[ # # ]
1078 : 0 : return ret;
1079 [ # # ]: 0 : },
1080 : : };
1081 : 0 : }
1082 : :
1083 : 0 : UniValue AddrmanEntryToJSON(const AddrInfo& info)
1084 : : {
1085 [ # # ]: 0 : UniValue ret(UniValue::VOBJ);
1086 [ # # ][ # # ]: 0 : ret.pushKV("address", info.ToStringAddr());
[ # # ][ # # ]
1087 [ # # ][ # # ]: 0 : ret.pushKV("port", info.GetPort());
[ # # ][ # # ]
1088 [ # # ][ # # ]: 0 : ret.pushKV("services", (uint64_t)info.nServices);
[ # # ]
1089 [ # # ][ # # ]: 0 : ret.pushKV("time", int64_t{TicksSinceEpoch<std::chrono::seconds>(info.nTime)});
[ # # ][ # # ]
1090 [ # # ][ # # ]: 0 : ret.pushKV("network", GetNetworkName(info.GetNetClass()));
[ # # ][ # # ]
[ # # ]
1091 [ # # ][ # # ]: 0 : ret.pushKV("source", info.source.ToStringAddr());
[ # # ][ # # ]
1092 [ # # ][ # # ]: 0 : ret.pushKV("source_network", GetNetworkName(info.source.GetNetClass()));
[ # # ][ # # ]
[ # # ]
1093 : 0 : return ret;
1094 [ # # ]: 0 : }
1095 : :
1096 : 0 : UniValue AddrmanTableToJSON(const std::vector<std::pair<AddrInfo, AddressPosition>>& tableInfos)
1097 : : {
1098 [ # # ]: 0 : UniValue table(UniValue::VOBJ);
1099 [ # # ]: 0 : for (const auto& e : tableInfos) {
1100 [ # # ]: 0 : AddrInfo info = e.first;
1101 : 0 : AddressPosition location = e.second;
1102 [ # # ]: 0 : std::ostringstream key;
1103 [ # # ][ # # ]: 0 : key << location.bucket << "/" << location.position;
[ # # ]
1104 : : // Address manager tables have unique entries so there is no advantage
1105 : : // in using UniValue::pushKV, which checks if the key already exists
1106 : : // in O(N). UniValue::pushKVEnd is used instead which currently is O(1).
1107 [ # # ][ # # ]: 0 : table.pushKVEnd(key.str(), AddrmanEntryToJSON(info));
[ # # ]
1108 : 0 : }
1109 : 0 : return table;
1110 [ # # ]: 0 : }
1111 : :
1112 : 0 : static RPCHelpMan getrawaddrman()
1113 : : {
1114 [ # # ][ # # ]: 0 : return RPCHelpMan{"getrawaddrman",
[ # # ][ # # ]
[ # # ]
1115 [ # # ]: 0 : "EXPERIMENTAL warning: this call may be changed in future releases.\n"
1116 : : "\nReturns information on all address manager entries for the new and tried tables.\n",
1117 : 0 : {},
1118 [ # # ][ # # ]: 0 : RPCResult{
1119 [ # # ][ # # ]: 0 : RPCResult::Type::OBJ_DYN, "", "", {
[ # # ]
1120 [ # # ][ # # ]: 0 : {RPCResult::Type::OBJ_DYN, "table", "buckets with addresses in the address manager table ( new, tried )", {
[ # # ][ # # ]
1121 [ # # ][ # # ]: 0 : {RPCResult::Type::OBJ, "bucket/position", "the location in the address manager table (<bucket>/<position>)", {
[ # # ][ # # ]
1122 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "address", "The address of the node"},
[ # # ]
1123 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "port", "The port number of the node"},
[ # # ]
1124 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "network", "The network (" + Join(GetNetworkNames(), ", ") + ") of the address"},
[ # # ][ # # ]
[ # # ][ # # ]
1125 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "services", "The services offered by the node"},
[ # # ]
1126 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM_TIME, "time", "The " + UNIX_EPOCH_TIME + " when the node was last seen"},
[ # # ][ # # ]
1127 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "source", "The address that relayed the address to us"},
[ # # ]
1128 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "source_network", "The network (" + Join(GetNetworkNames(), ", ") + ") of the source address"},
[ # # ][ # # ]
[ # # ][ # # ]
1129 : : }}
1130 : : }}
1131 : : }
1132 : : },
1133 [ # # ]: 0 : RPCExamples{
1134 [ # # ][ # # ]: 0 : HelpExampleCli("getrawaddrman", "")
[ # # ]
1135 [ # # ][ # # ]: 0 : + HelpExampleRpc("getrawaddrman", "")
[ # # ][ # # ]
1136 : : },
1137 : 0 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue {
1138 : 0 : AddrMan& addrman = EnsureAnyAddrman(request.context);
1139 : :
1140 [ # # ]: 0 : UniValue ret(UniValue::VOBJ);
1141 [ # # ][ # # ]: 0 : ret.pushKV("new", AddrmanTableToJSON(addrman.GetEntries(false)));
[ # # ][ # # ]
1142 [ # # ][ # # ]: 0 : ret.pushKV("tried", AddrmanTableToJSON(addrman.GetEntries(true)));
[ # # ][ # # ]
1143 : 0 : return ret;
1144 [ # # ]: 0 : },
1145 : : };
1146 : 0 : }
1147 : :
1148 : 0 : void RegisterNetRPCCommands(CRPCTable& t)
1149 : : {
1150 [ # # ][ # # ]: 0 : static const CRPCCommand commands[]{
[ # # ]
1151 [ # # ][ # # ]: 0 : {"network", &getconnectioncount},
1152 [ # # ][ # # ]: 0 : {"network", &ping},
1153 [ # # ][ # # ]: 0 : {"network", &getpeerinfo},
1154 [ # # ][ # # ]: 0 : {"network", &addnode},
1155 [ # # ][ # # ]: 0 : {"network", &disconnectnode},
1156 [ # # ][ # # ]: 0 : {"network", &getaddednodeinfo},
1157 [ # # ][ # # ]: 0 : {"network", &getnettotals},
1158 [ # # ][ # # ]: 0 : {"network", &getnetworkinfo},
1159 [ # # ][ # # ]: 0 : {"network", &setban},
1160 [ # # ][ # # ]: 0 : {"network", &listbanned},
1161 [ # # ][ # # ]: 0 : {"network", &clearbanned},
1162 [ # # ][ # # ]: 0 : {"network", &setnetworkactive},
1163 [ # # ][ # # ]: 0 : {"network", &getnodeaddresses},
1164 [ # # ][ # # ]: 0 : {"network", &getaddrmaninfo},
1165 [ # # ][ # # ]: 0 : {"hidden", &addconnection},
1166 [ # # ][ # # ]: 0 : {"hidden", &addpeeraddress},
1167 [ # # ][ # # ]: 0 : {"hidden", &sendmsgtopeer},
1168 [ # # ][ # # ]: 0 : {"hidden", &getrawaddrman},
1169 : : };
1170 [ # # ]: 0 : for (const auto& c : commands) {
1171 : 0 : t.appendCommand(c.name, &c);
1172 : : }
1173 : 0 : }
|