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