Skip to content

Commit 2e24197

Browse files
committed
Merge #20115: cli: -netinfo quick updates/fixups for 0.21
398045b cli -netinfo: print oversized/extreme ping times as "-" (Jon Atack) 773f4c9 cli -netinfo: handle longer tor v3 local addresses (Jon Atack) 33e9874 cli -netinfo: make age column variable-width (Jon Atack) f8a1c4d cli -netinfo: various quick updates and fixes (Jon Atack) Pull request description: Quick fixups and updates for v0.21.0: - [x] handle larger BIP155 `addrv2` addresses - [x] add Signet chain - [x] add an additional space between the `net` and `mping` columns; add missing `tinyformat` and `algorithm` headers - [x] s/uptime/age/ per 0xB10C suggestion, and make the column auto-adjusting variable width - [x] display `-` for oversized mping/ping times like `1.17348e+06`, as reported by practicalswift Edit: removed the release note commit, as this PR was not merged before the notes were moved to the wiki. It's here: ``` - A new `bitcoin-cli -netinfo` command returns a network peer connections dashboard that displays data from the `getpeerinfo` and `getnetworkinfo` RPCs in a human-readable format. An optional integer argument from `0` to `4` may be passed to see various levels of detail. (#19643) ``` ACKs for top commit: michaelfolkson: ACK 398045b Emzy: Tested ACK 398045b Tree-SHA512: 0625ee840141bafbfcaf8f1fce53f8f850ae91721b2bdad4279372da87c18a1fe3a214d90bfdbbabdf6da38d58290d7dd0f1109b4e2ca5d20cacf417d6ced0f9
2 parents f3727fd + 398045b commit 2e24197

File tree

1 file changed

+40
-23
lines changed

1 file changed

+40
-23
lines changed

src/bitcoin-cli.cpp

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
#include <rpc/mining.h>
1515
#include <rpc/protocol.h>
1616
#include <rpc/request.h>
17+
#include <tinyformat.h>
1718
#include <util/strencodings.h>
1819
#include <util/system.h>
1920
#include <util/translation.h>
2021
#include <util/url.h>
2122

23+
#include <algorithm>
2224
#include <functional>
2325
#include <memory>
2426
#include <stdio.h>
@@ -297,12 +299,12 @@ class NetinfoRequestHandler : public BaseRequestHandler
297299
{
298300
private:
299301
static constexpr int8_t UNKNOWN_NETWORK{-1};
300-
static constexpr size_t m_networks_size{3};
302+
static constexpr uint8_t m_networks_size{3};
301303
const std::array<std::string, m_networks_size> m_networks{{"ipv4", "ipv6", "onion"}};
302304
std::array<std::array<uint16_t, m_networks_size + 2>, 3> m_counts{{{}}}; //!< Peer counts by (in/out/total, networks/total/block-relay)
303305
int8_t NetworkStringToId(const std::string& str) const
304306
{
305-
for (size_t i = 0; i < m_networks_size; ++i) {
307+
for (uint8_t i = 0; i < m_networks_size; ++i) {
306308
if (str == m_networks.at(i)) return i;
307309
}
308310
return UNKNOWN_NETWORK;
@@ -313,21 +315,22 @@ class NetinfoRequestHandler : public BaseRequestHandler
313315
bool IsVersionSelected() const { return m_details_level == 3 || m_details_level == 4; }
314316
bool m_is_asmap_on{false};
315317
size_t m_max_addr_length{0};
318+
size_t m_max_age_length{4};
316319
size_t m_max_id_length{2};
317320
struct Peer {
318-
int id;
319-
int mapped_as;
320-
int version;
321-
int64_t conn_time;
321+
std::string addr;
322+
std::string sub_version;
323+
std::string network;
324+
std::string age;
325+
double min_ping;
326+
double ping;
322327
int64_t last_blck;
323328
int64_t last_recv;
324329
int64_t last_send;
325330
int64_t last_trxn;
326-
double min_ping;
327-
double ping;
328-
std::string addr;
329-
std::string network;
330-
std::string sub_version;
331+
int id;
332+
int mapped_as;
333+
int version;
331334
bool is_block_relay;
332335
bool is_outbound;
333336
bool operator<(const Peer& rhs) const { return std::tie(is_outbound, min_ping) < std::tie(rhs.is_outbound, rhs.min_ping); }
@@ -336,9 +339,16 @@ class NetinfoRequestHandler : public BaseRequestHandler
336339
std::string ChainToString() const
337340
{
338341
if (gArgs.GetChainName() == CBaseChainParams::TESTNET) return " testnet";
342+
if (gArgs.GetChainName() == CBaseChainParams::SIGNET) return " signet";
339343
if (gArgs.GetChainName() == CBaseChainParams::REGTEST) return " regtest";
340344
return "";
341345
}
346+
std::string PingTimeToString(double seconds) const
347+
{
348+
if (seconds < 0) return "";
349+
const double milliseconds{round(1000 * seconds)};
350+
return milliseconds > 999999 ? "-" : ToString(milliseconds);
351+
}
342352
const int64_t m_time_now{GetSystemTimeInSeconds()};
343353

344354
public:
@@ -398,10 +408,12 @@ class NetinfoRequestHandler : public BaseRequestHandler
398408
const double min_ping{peer["minping"].isNull() ? -1 : peer["minping"].get_real()};
399409
const double ping{peer["pingtime"].isNull() ? -1 : peer["pingtime"].get_real()};
400410
const std::string addr{peer["addr"].get_str()};
411+
const std::string age{conn_time == 0 ? "" : ToString((m_time_now - conn_time) / 60)};
401412
const std::string sub_version{peer["subver"].get_str()};
402-
m_peers.push_back({peer_id, mapped_as, version, conn_time, last_blck, last_recv, last_send, last_trxn, min_ping, ping, addr, network, sub_version, is_block_relay, is_outbound});
403-
m_max_id_length = std::max(ToString(peer_id).length(), m_max_id_length);
413+
m_peers.push_back({addr, sub_version, network, age, min_ping, ping, last_blck, last_recv, last_send, last_trxn, peer_id, mapped_as, version, is_block_relay, is_outbound});
404414
m_max_addr_length = std::max(addr.length() + 1, m_max_addr_length);
415+
m_max_age_length = std::max(age.length(), m_max_age_length);
416+
m_max_id_length = std::max(ToString(peer_id).length(), m_max_id_length);
405417
m_is_asmap_on |= (mapped_as != 0);
406418
}
407419
}
@@ -412,23 +424,24 @@ class NetinfoRequestHandler : public BaseRequestHandler
412424
// Report detailed peer connections list sorted by direction and minimum ping time.
413425
if (DetailsRequested() && !m_peers.empty()) {
414426
std::sort(m_peers.begin(), m_peers.end());
415-
result += "Peer connections sorted by direction and min ping\n<-> relay net mping ping send recv txn blk uptime ";
427+
result += strprintf("Peer connections sorted by direction and min ping\n<-> relay net mping ping send recv txn blk %*s ", m_max_age_length, "age");
416428
if (m_is_asmap_on) result += " asmap ";
417429
result += strprintf("%*s %-*s%s\n", m_max_id_length, "id", IsAddressSelected() ? m_max_addr_length : 0, IsAddressSelected() ? "address" : "", IsVersionSelected() ? "version" : "");
418430
for (const Peer& peer : m_peers) {
419431
std::string version{ToString(peer.version) + peer.sub_version};
420432
result += strprintf(
421-
"%3s %5s %5s%6s%7s%5s%5s%5s%5s%7s%*i %*s %-*s%s\n",
433+
"%3s %5s %5s%7s%7s%5s%5s%5s%5s %*s%*i %*s %-*s%s\n",
422434
peer.is_outbound ? "out" : "in",
423435
peer.is_block_relay ? "block" : "full",
424436
peer.network,
425-
peer.min_ping == -1 ? "" : ToString(round(1000 * peer.min_ping)),
426-
peer.ping == -1 ? "" : ToString(round(1000 * peer.ping)),
437+
PingTimeToString(peer.min_ping),
438+
PingTimeToString(peer.ping),
427439
peer.last_send == 0 ? "" : ToString(m_time_now - peer.last_send),
428440
peer.last_recv == 0 ? "" : ToString(m_time_now - peer.last_recv),
429441
peer.last_trxn == 0 ? "" : ToString((m_time_now - peer.last_trxn) / 60),
430442
peer.last_blck == 0 ? "" : ToString((m_time_now - peer.last_blck) / 60),
431-
peer.conn_time == 0 ? "" : ToString((m_time_now - peer.conn_time) / 60),
443+
m_max_age_length, // variable spacing
444+
peer.age,
432445
m_is_asmap_on ? 7 : 0, // variable spacing
433446
m_is_asmap_on && peer.mapped_as != 0 ? ToString(peer.mapped_as) : "",
434447
m_max_id_length, // variable spacing
@@ -437,24 +450,28 @@ class NetinfoRequestHandler : public BaseRequestHandler
437450
IsAddressSelected() ? peer.addr : "",
438451
IsVersionSelected() && version != "0" ? version : "");
439452
}
440-
result += " ms ms sec sec min min min\n\n";
453+
result += strprintf(" ms ms sec sec min min %*s\n\n", m_max_age_length, "min");
441454
}
442455

443456
// Report peer connection totals by type.
444457
result += " ipv4 ipv6 onion total block-relay\n";
445458
const std::array<std::string, 3> rows{{"in", "out", "total"}};
446-
for (size_t i = 0; i < m_networks_size; ++i) {
459+
for (uint8_t i = 0; i < m_networks_size; ++i) {
447460
result += strprintf("%-5s %5i %5i %5i %5i %5i\n", rows.at(i), m_counts.at(i).at(0), m_counts.at(i).at(1), m_counts.at(i).at(2), m_counts.at(i).at(m_networks_size), m_counts.at(i).at(m_networks_size + 1));
448461
}
449462

450463
// Report local addresses, ports, and scores.
451464
result += "\nLocal addresses";
452-
const UniValue& local_addrs{networkinfo["localaddresses"]};
465+
const std::vector<UniValue>& local_addrs{networkinfo["localaddresses"].getValues()};
453466
if (local_addrs.empty()) {
454467
result += ": n/a\n";
455468
} else {
456-
for (const UniValue& addr : local_addrs.getValues()) {
457-
result += strprintf("\n%-40i port %5i score %6i", addr["address"].get_str(), addr["port"].get_int(), addr["score"].get_int());
469+
size_t max_addr_size{0};
470+
for (const UniValue& addr : local_addrs) {
471+
max_addr_size = std::max(addr["address"].get_str().length() + 1, max_addr_size);
472+
}
473+
for (const UniValue& addr : local_addrs) {
474+
result += strprintf("\n%-*s port %6i score %6i", max_addr_size, addr["address"].get_str(), addr["port"].get_int(), addr["score"].get_int());
458475
}
459476
}
460477

0 commit comments

Comments
 (0)