aboutsummaryrefslogtreecommitdiff
path: root/src/bitcoin-cli.cpp
diff options
context:
space:
mode:
authorJon Atack <jon@atack.com>2020-08-13 16:59:57 +0200
committerJon Atack <jon@atack.com>2020-08-31 16:12:36 +0200
commitf5edd66e5d136b229c805af9e6ea73218f6cedde (patch)
treeee5db663195a3d620fd160469c266db9f4818e84 /src/bitcoin-cli.cpp
parent3a0ab93e1ce8d91235a6d46a57c6cb110fc5bf03 (diff)
downloadbitcoin-f5edd66e5d136b229c805af9e6ea73218f6cedde.tar.xz
cli: create vector of Peer structs for peers data
Diffstat (limited to 'src/bitcoin-cli.cpp')
-rw-r--r--src/bitcoin-cli.cpp31
1 files changed, 30 insertions, 1 deletions
diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp
index b9877f5126..e50d9c43fa 100644
--- a/src/bitcoin-cli.cpp
+++ b/src/bitcoin-cli.cpp
@@ -320,6 +320,22 @@ private:
ipv6,
onion,
};
+ struct Peer {
+ int id;
+ int mapped_as;
+ int version;
+ int64_t conn_time;
+ int64_t last_recv;
+ int64_t last_send;
+ double min_ping;
+ double ping;
+ std::string addr;
+ std::string sub_version;
+ NetType net_type;
+ bool is_block_relay;
+ bool is_outbound;
+ bool operator<(const Peer& rhs) const { return std::tie(is_outbound, min_ping) < std::tie(rhs.is_outbound, rhs.min_ping); }
+ };
std::string NetTypeEnumToString(NetType t)
{
switch (t) {
@@ -357,9 +373,10 @@ public:
if (!batch[ID_PEERINFO]["error"].isNull()) return batch[ID_PEERINFO];
if (!batch[ID_NETWORKINFO]["error"].isNull()) return batch[ID_NETWORKINFO];
- // Count peer connection totals.
+ // Count peer connection totals, and if m_verbose is true, store peer data in a vector of structs.
int ipv4_i{0}, ipv6_i{0}, onion_i{0}, block_relay_i{0}, total_i{0}; // inbound conn counters
int ipv4_o{0}, ipv6_o{0}, onion_o{0}, block_relay_o{0}, total_o{0}; // outbound conn counters
+ std::vector<Peer> peers;
const UniValue& getpeerinfo{batch[ID_PEERINFO]["result"]};
for (const UniValue& peer : getpeerinfo.getValues()) {
@@ -392,6 +409,18 @@ public:
}
if (is_block_relay) ++block_relay_o;
}
+ if (m_verbose) {
+ // Push data for this peer to the peers vector.
+ const int peer_id{peer["id"].get_int()};
+ const int version{peer["version"].get_int()};
+ const std::string sub_version{peer["subver"].get_str()};
+ const int64_t conn_time{peer["conntime"].get_int64()};
+ const int64_t last_recv{peer["lastrecv"].get_int64()};
+ const int64_t last_send{peer["lastsend"].get_int64()};
+ const double min_ping{peer["minping"].isNull() ? -1 : peer["minping"].get_real()};
+ const double ping{peer["pingtime"].isNull() ? -1 : peer["pingtime"].get_real()};
+ 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});
+ }
}
// Generate report header.