bitcoind_request/command/get_peer_info.rs
1/*
2getpeerinfo
3
4Returns data about each connected network node as a json array of objects.
5
6Result:
7[ (json array)
8 { (json object)
9 "id" : n, (numeric) Peer index
10 "addr" : "str", (string) (host:port) The IP address and port of the peer
11 "addrbind" : "str", (string) (ip:port) Bind address of the connection to the peer
12 "addrlocal" : "str", (string) (ip:port) Local address as reported by the peer
13 "network" : "str", (string) Network (ipv4, ipv6, or onion) the peer connected through
14 "mapped_as" : n, (numeric) The AS in the BGP route to the peer used for diversifying
15 peer selection (only available if the asmap config flag is set)
16 "services" : "hex", (string) The services offered
17 "servicesnames" : [ (json array) the services offered, in human-readable form
18 "str", (string) the service name if it is recognised
19 ...
20 ],
21 "relaytxes" : true|false, (boolean) Whether peer has asked us to relay transactions to it
22 "lastsend" : xxx, (numeric) The UNIX epoch time of the last send
23 "lastrecv" : xxx, (numeric) The UNIX epoch time of the last receive
24 "last_transaction" : xxx, (numeric) The UNIX epoch time of the last valid transaction received from this peer
25 "last_block" : xxx, (numeric) The UNIX epoch time of the last block received from this peer
26 "bytessent" : n, (numeric) The total bytes sent
27 "bytesrecv" : n, (numeric) The total bytes received
28 "conntime" : xxx, (numeric) The UNIX epoch time of the connection
29 "timeoffset" : n, (numeric) The time offset in seconds
30 "pingtime" : n, (numeric) ping time (if available)
31 "minping" : n, (numeric) minimum observed ping time (if any at all)
32 "pingwait" : n, (numeric) ping wait (if non-zero)
33 "version" : n, (numeric) The peer version, such as 70001
34 "subver" : "str", (string) The string version
35 "inbound" : true|false, (boolean) Inbound (true) or Outbound (false)
36 "addnode" : true|false, (boolean) Whether connection was due to addnode/-connect or if it was an automatic/inbound connection
37 (DEPRECATED, returned only if the config option -deprecatedrpc=getpeerinfo_addnode is passed)
38 "connection_type" : "str", (string) Type of connection:
39 outbound-full-relay (default automatic connections),
40 block-relay-only (does not relay transactions or addresses),
41 inbound (initiated by the peer),
42 manual (added via addnode RPC or -addnode/-connect configuration options),
43 addr-fetch (short-lived automatic connection for soliciting addresses),
44 feeler (short-lived automatic connection for testing addresses).
45 Please note this output is unlikely to be stable in upcoming releases as we iterate to
46 best capture connection behaviors.
47 "startingheight" : n, (numeric) The starting height (block) of the peer
48 "banscore" : n, (numeric) The ban score (DEPRECATED, returned only if config option -deprecatedrpc=banscore is passed)
49 "synced_headers" : n, (numeric) The last header we have in common with this peer
50 "synced_blocks" : n, (numeric) The last block we have in common with this peer
51 "inflight" : [ (json array)
52 n, (numeric) The heights of blocks we're currently asking from this peer
53 ...
54 ],
55 "whitelisted" : true|false, (boolean, optional) Whether the peer is whitelisted with default permissions
56 (DEPRECATED, returned only if config option -deprecatedrpc=whitelisted is passed)
57 "permissions" : [ (json array) Any special permissions that have been granted to this peer
58 "str", (string) bloomfilter (allow requesting BIP37 filtered blocks and transactions),
59 noban (do not ban for misbehavior; implies download),
60 forcerelay (relay transactions that are already in the mempool; implies relay),
61 relay (relay even in -blocksonly mode, and unlimited transaction announcements),
62 mempool (allow requesting BIP35 mempool contents),
63 download (allow getheaders during IBD, no disconnect after maxuploadtarget limit),
64 addr (responses to GETADDR avoid hitting the cache and contain random records with the most up-to-date info).
65
66 ...
67 ],
68 "minfeefilter" : n, (numeric) The minimum fee rate for transactions this peer accepts
69 "bytessent_per_msg" : { (json object)
70 "msg" : n, (numeric) The total bytes sent aggregated by message type
71 When a message type is not listed in this json object, the bytes sent are 0.
72 Only known message types can appear as keys in the object.
73 ...
74 },
75 "bytesrecv_per_msg" : { (json object)
76 "msg" : n (numeric) The total bytes received aggregated by message type
77 When a message type is not listed in this json object, the bytes received are 0.
78 Only known message types can appear as keys in the object and all bytes received
79 of unknown message types are listed under '*other*'.
80 }
81 },
82 ...
83]
84
85Examples:
86> bitcoin-cli getpeerinfo
87> curl --user myusername --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getpeerinfo", "params": []}' -H 'content-type: text/plain;' http://127.0.0.1:8332/
88*/
89use crate::command::CallableCommand;
90use crate::{client::Client, command::request::request};
91use serde::{Deserialize, Serialize};
92use std::collections::HashMap;
93
94const GET_PEER_INFO_COMMAND: &str = "getpeerinfo";
95
96#[derive(Serialize, Deserialize, Debug)]
97pub struct ConnectedNetworkNode {
98 id: u64, // Peer index
99 addr: String, // (host:port) The IP address and port of the peer
100 addrbind: String, // (ip:port) Bind address of the connection to the peer
101 addrlocal: Option<String>, // (ip:port) Local address as reported by the peer
102 network: String, // Network (ipv4, ipv6, or onion) the peer connected through
103 mapped_as: Option<u64>, // The AS in the BGP route to the peer used for diversifying peer selection (only available if the asmap config flag is set)
104 services: String, // (hex) The services offered
105 servicesnames: Vec<String>, // (json array) the services offered, in human-readable form. Each element of the array is (string) the service name if it is recognised
106 relaytxes: bool, // Whether peer has asked us to relay transactions to it
107 lastsend: u64, // (unix) The UNIX epoch time of the last send
108 lastrecv: u64, // (unix) The UNIX epoch time of the last receive
109 last_transaction: u64, // (unix) The UNIX epoch time of the last valid transaction received from this peer
110 last_block: u64, // (unix) The UNIX epoch time of the last block received from this peer
111 bytessent: u64, // The total bytes sent
112 bytesrecv: u64, // The total bytes received
113 conntime: u64, // (unix) The UNIX epoch time of the connection
114 timeoffset: i64, // The time offset in seconds
115 pingtime: f64, // ping time (if available)
116 minping: f64, // minimum observed ping time (if any at all)
117 pingwait: Option<u64>, // ping wait (if non-zero)
118 version: u64, // The peer version, such as 70001
119 subver: String, // The string version
120 inbound: bool, // Inbound (true) or Outbound (false)
121 addnode: Option<bool>, // Whether connection was due to addnode/-connect or if it was an automatic/inbound connection. (DEPRECATED, returned only if the config option -deprecatedrpc=getpeerinfo_addnode is passed)
122 //
123 // TODO: Make this an enum
124 connection_type: String, // Type of connection:
125 //outbound-full-relay (default automatic connections),
126 //block-relay-only (does not relay transactions or addresses),
127 //inbound (initiated by the peer),
128 //manual (added via addnode RPC or -addnode/-connect configuration options),
129 //addr-fetch (short-lived automatic connection for soliciting addresses),
130 //feeler (short-lived automatic connection for testing addresses).
131 //Please note this output is unlikely to be stable in upcoming releases as we iterate to
132 //best capture connection behaviors.
133 startingheight: u64, // The starting height (block) of the peer
134 banscore: Option<u64>, // The ban score (DEPRECATED, returned only if config option -deprecatedrpc=banscore is passed)
135 synced_headers: i64, // The last header we have in common with this peer
136 synced_blocks: i64, // The last block we have in common with this peer
137 inflight: Vec<u64>, // (json array) each element of array is (numeric) The heights of blocks we're currently asking from this peer
138 whitelisted: Option<bool>, // (boolean, optional) Whether the peer is whitelisted with default permissions
139 // (DEPRECATED, returned only if config option -deprecatedrpc=whitelisted is passed)
140 // TODO: Make this an enum
141 permissions: Vec<String>, //(json array) Any special permissions that have been granted to this peer
142 // (string) bloomfilter (allow requesting BIP37 filtered blocks and transactions),
143 // noban (do not ban for misbehavior; implies download),
144 // forcerelay (relay transactions that are already in the mempool; implies relay),
145 // relay (relay even in -blocksonly mode, and unlimited transaction announcements),
146 // mempool (allow requesting BIP35 mempool contents),
147 // download (allow getheaders during IBD, no disconnect after maxuploadtarget limit),
148 // addr (responses to GETADDR avoid hitting the cache and contain random records with the most up-to-date info).
149 minfeefilter: f64, // The minimum fee rate for transactions this peer accepts
150 bytessent_per_msg: HashMap<String, u64>, // The total bytes sent aggregated by message type When a message type is not listed in this json object, the bytes sent are 0. Only known message types can appear as keys in the object.
151 bytesrecv_per_msg: HashMap<String, u64>, // The total bytes received aggregated by message type
152 //When a message type is not listed in this json object, the bytes received are 0.
153 //Only known message types can appear as keys in the object and all bytes received
154 //of unknown message types are listed under '*other*'.
155}
156pub struct GetPeerInfoCommand {}
157impl GetPeerInfoCommand {
158 pub fn new() -> Self {
159 GetPeerInfoCommand {}
160 }
161}
162
163// TODO: struct GetPeerInfoCommandResponse(String);
164#[derive(Serialize, Deserialize, Debug)]
165pub struct GetPeerInfoCommandResponse(pub Vec<ConnectedNetworkNode>);
166
167impl CallableCommand for GetPeerInfoCommand {
168 type Response = GetPeerInfoCommandResponse;
169 fn call(&self, client: &Client) -> Result<Self::Response, jsonrpc::Error> {
170 let params = vec![];
171 let r = request(client, GET_PEER_INFO_COMMAND, params);
172 let response: GetPeerInfoCommandResponse = r.result()?;
173 Ok(response)
174 }
175}