Skip to content

Commit f5edd66

Browse files
committed
cli: create vector of Peer structs for peers data
1 parent 3a0ab93 commit f5edd66

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/bitcoin-cli.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,22 @@ class NetinfoRequestHandler : public BaseRequestHandler
320320
ipv6,
321321
onion,
322322
};
323+
struct Peer {
324+
int id;
325+
int mapped_as;
326+
int version;
327+
int64_t conn_time;
328+
int64_t last_recv;
329+
int64_t last_send;
330+
double min_ping;
331+
double ping;
332+
std::string addr;
333+
std::string sub_version;
334+
NetType net_type;
335+
bool is_block_relay;
336+
bool is_outbound;
337+
bool operator<(const Peer& rhs) const { return std::tie(is_outbound, min_ping) < std::tie(rhs.is_outbound, rhs.min_ping); }
338+
};
323339
std::string NetTypeEnumToString(NetType t)
324340
{
325341
switch (t) {
@@ -357,9 +373,10 @@ class NetinfoRequestHandler : public BaseRequestHandler
357373
if (!batch[ID_PEERINFO]["error"].isNull()) return batch[ID_PEERINFO];
358374
if (!batch[ID_NETWORKINFO]["error"].isNull()) return batch[ID_NETWORKINFO];
359375

360-
// Count peer connection totals.
376+
// Count peer connection totals, and if m_verbose is true, store peer data in a vector of structs.
361377
int ipv4_i{0}, ipv6_i{0}, onion_i{0}, block_relay_i{0}, total_i{0}; // inbound conn counters
362378
int ipv4_o{0}, ipv6_o{0}, onion_o{0}, block_relay_o{0}, total_o{0}; // outbound conn counters
379+
std::vector<Peer> peers;
363380
const UniValue& getpeerinfo{batch[ID_PEERINFO]["result"]};
364381

365382
for (const UniValue& peer : getpeerinfo.getValues()) {
@@ -392,6 +409,18 @@ class NetinfoRequestHandler : public BaseRequestHandler
392409
}
393410
if (is_block_relay) ++block_relay_o;
394411
}
412+
if (m_verbose) {
413+
// Push data for this peer to the peers vector.
414+
const int peer_id{peer["id"].get_int()};
415+
const int version{peer["version"].get_int()};
416+
const std::string sub_version{peer["subver"].get_str()};
417+
const int64_t conn_time{peer["conntime"].get_int64()};
418+
const int64_t last_recv{peer["lastrecv"].get_int64()};
419+
const int64_t last_send{peer["lastsend"].get_int64()};
420+
const double min_ping{peer["minping"].isNull() ? -1 : peer["minping"].get_real()};
421+
const double ping{peer["pingtime"].isNull() ? -1 : peer["pingtime"].get_real()};
422+
peers.push_back({peer_id, mapped_as, version, conn_time, last_recv, last_send, min_ping, ping, addr, sub_version, net_type, is_block_relay, !is_inbound});
423+
}
395424
}
396425

397426
// Generate report header.

0 commit comments

Comments
 (0)