From 3184b432675de7b0ca048dfd9ebde2a698f51e9d Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Thu, 29 Oct 2020 12:04:08 +0100 Subject: [PATCH] Merge #20115: cli: -netinfo quick updates/fixups for 0.21 398045ba8b3694931069f88ec95553b3207dd1a6 cli -netinfo: print oversized/extreme ping times as "-" (Jon Atack) 773f4c99c00c0b1d8c1b53cb99ba571337100953 cli -netinfo: handle longer tor v3 local addresses (Jon Atack) 33e987452f869c279f2491499939e51e0af8364c cli -netinfo: make age column variable-width (Jon Atack) f8a1c4d9469cb496fdafaf6f4d94977687df9190 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 398045ba8b3694931069f88ec95553b3207dd1a6 Emzy: Tested ACK 398045ba8b3694931069f88ec95553b3207dd1a6 Tree-SHA512: 0625ee840141bafbfcaf8f1fce53f8f850ae91721b2bdad4279372da87c18a1fe3a214d90bfdbbabdf6da38d58290d7dd0f1109b4e2ca5d20cacf417d6ced0f9 --- src/bitcoin-cli.cpp | 63 ++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 23 deletions(-) diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp index f4a6cf6ae8..1ad8f189c8 100644 --- a/src/bitcoin-cli.cpp +++ b/src/bitcoin-cli.cpp @@ -16,11 +16,13 @@ #include #include #include +#include #include #include #include #include +#include #include #include #include @@ -306,12 +308,12 @@ class NetinfoRequestHandler : public BaseRequestHandler { private: static constexpr int8_t UNKNOWN_NETWORK{-1}; - static constexpr size_t m_networks_size{3}; + static constexpr uint8_t m_networks_size{3}; const std::array m_networks{{"ipv4", "ipv6", "onion"}}; std::array, 3> m_counts{{{}}}; //!< Peer counts by (in/out/total, networks/total/block-relay) int8_t NetworkStringToId(const std::string& str) const { - for (size_t i = 0; i < m_networks_size; ++i) { + for (uint8_t i = 0; i < m_networks_size; ++i) { if (str == m_networks.at(i)) return i; } return UNKNOWN_NETWORK; @@ -323,21 +325,22 @@ private: bool IsVersionSelected() const { return m_details_level == 3 || m_details_level == 4; } bool m_is_asmap_on{false}; size_t m_max_addr_length{0}; + size_t m_max_age_length{4}; size_t m_max_id_length{2}; struct Peer { - int id; - int mapped_as; - int version; - int64_t conn_time; + std::string addr; + std::string sub_version; + std::string network; + std::string age; + double min_ping; + double ping; int64_t last_blck; int64_t last_recv; int64_t last_send; int64_t last_trxn; - double min_ping; - double ping; - std::string addr; - std::string network; - std::string sub_version; + int id; + int mapped_as; + int version; 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); } @@ -346,9 +349,16 @@ private: std::string ChainToString() const { if (gArgs.GetChainName() == CBaseChainParams::TESTNET) return " testnet"; + if (gArgs.GetChainName() == CBaseChainParams::DEVNET) return " devnet"; if (gArgs.GetChainName() == CBaseChainParams::REGTEST) return " regtest"; return ""; } + std::string PingTimeToString(double seconds) const + { + if (seconds < 0) return ""; + const double milliseconds{round(1000 * seconds)}; + return milliseconds > 999999 ? "-" : ToString(milliseconds); + } const UniValue NetinfoHelp() { return std::string{ @@ -471,10 +481,12 @@ public: const double min_ping{peer["minping"].isNull() ? -1 : peer["minping"].get_real()}; const double ping{peer["pingtime"].isNull() ? -1 : peer["pingtime"].get_real()}; const std::string addr{peer["addr"].get_str()}; + const std::string age{conn_time == 0 ? "" : ToString((m_time_now - conn_time) / 60)}; const std::string sub_version{peer["subver"].get_str()}; - 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}); - m_max_id_length = std::max(ToString(peer_id).length(), m_max_id_length); + 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}); m_max_addr_length = std::max(addr.length() + 1, m_max_addr_length); + m_max_age_length = std::max(age.length(), m_max_age_length); + m_max_id_length = std::max(ToString(peer_id).length(), m_max_id_length); m_is_asmap_on |= (mapped_as != 0); } } @@ -485,23 +497,24 @@ public: // Report detailed peer connections list sorted by direction and minimum ping time. if (DetailsRequested() && !m_peers.empty()) { std::sort(m_peers.begin(), m_peers.end()); - result += "<-> relay net mping ping send recv txn blk uptime "; + result += strprintf("<-> relay net mping ping send recv txn blk %*s ", m_max_age_length, "age"); if (m_is_asmap_on) result += " asmap "; result += strprintf("%*s %-*s%s\n", m_max_id_length, "id", IsAddressSelected() ? m_max_addr_length : 0, IsAddressSelected() ? "address" : "", IsVersionSelected() ? "version" : ""); for (const Peer& peer : m_peers) { std::string version{ToString(peer.version) + peer.sub_version}; result += strprintf( - "%3s %5s %5s%6s%7s%5s%5s%5s%5s%7s%*i %*s %-*s%s\n", + "%3s %5s %5s%7s%7s%5s%5s%5s%5s %*s%*i %*s %-*s%s\n", peer.is_outbound ? "out" : "in", peer.is_block_relay ? "block" : "full", peer.network, - peer.min_ping == -1 ? "" : ToString(round(1000 * peer.min_ping)), - peer.ping == -1 ? "" : ToString(round(1000 * peer.ping)), + PingTimeToString(peer.min_ping), + PingTimeToString(peer.ping), peer.last_send == 0 ? "" : ToString(m_time_now - peer.last_send), peer.last_recv == 0 ? "" : ToString(m_time_now - peer.last_recv), peer.last_trxn == 0 ? "" : ToString((m_time_now - peer.last_trxn) / 60), peer.last_blck == 0 ? "" : ToString((m_time_now - peer.last_blck) / 60), - peer.conn_time == 0 ? "" : ToString((m_time_now - peer.conn_time) / 60), + m_max_age_length, // variable spacing + peer.age, m_is_asmap_on ? 7 : 0, // variable spacing m_is_asmap_on && peer.mapped_as != 0 ? ToString(peer.mapped_as) : "", m_max_id_length, // variable spacing @@ -510,24 +523,28 @@ public: IsAddressSelected() ? peer.addr : "", IsVersionSelected() && version != "0" ? version : ""); } - result += " ms ms sec sec min min min\n\n"; + result += strprintf(" ms ms sec sec min min %*s\n\n", m_max_age_length, "min"); } // Report peer connection totals by type. result += " ipv4 ipv6 onion total block-relay\n"; const std::array rows{{"in", "out", "total"}}; - for (size_t i = 0; i < m_networks_size; ++i) { + for (uint8_t i = 0; i < m_networks_size; ++i) { 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)); } // Report local addresses, ports, and scores. result += "\nLocal addresses"; - const UniValue& local_addrs{networkinfo["localaddresses"]}; + const std::vector& local_addrs{networkinfo["localaddresses"].getValues()}; if (local_addrs.empty()) { result += ": n/a\n"; } else { - for (const UniValue& addr : local_addrs.getValues()) { - result += strprintf("\n%-40i port %5i score %6i", addr["address"].get_str(), addr["port"].get_int(), addr["score"].get_int()); + size_t max_addr_size{0}; + for (const UniValue& addr : local_addrs) { + max_addr_size = std::max(addr["address"].get_str().length() + 1, max_addr_size); + } + for (const UniValue& addr : local_addrs) { + result += strprintf("\n%-*s port %6i score %6i", max_addr_size, addr["address"].get_str(), addr["port"].get_int(), addr["score"].get_int()); } }