Skip to content

Commit 6272604

Browse files
committed
refactor: enable -netinfo to add future networks (i2p, cjdns)
After this commit, a new network may be added by changing 4 lines: - increment the value of `m_networks_size` - add the network name to `m_networks` - add the network name to this line: `result += " ipv4 ipv6 onion total block-relay\n";` - add "counts.at(i).at(<m_networks pos>)" to this line: `result += strprintf("%-5s %5i %5i %5i %5i %5i\n...`
1 parent 82fd402 commit 6272604

File tree

1 file changed

+28
-31
lines changed

1 file changed

+28
-31
lines changed

src/bitcoin-cli.cpp

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,17 @@ class GetinfoRequestHandler: public BaseRequestHandler
296296
class NetinfoRequestHandler : public BaseRequestHandler
297297
{
298298
private:
299+
static constexpr int8_t UNKNOWN_NETWORK{-1};
300+
static constexpr size_t m_networks_size{3};
301+
const std::array<std::string, m_networks_size> m_networks{{"ipv4", "ipv6", "onion"}};
302+
std::array<std::array<uint16_t, m_networks_size + 2>, 3> m_counts{{{}}}; //!< Peer counts by (in/out/total, networks/total/block-relay)
303+
int8_t NetworkStringToId(const std::string& str) const
304+
{
305+
for (size_t i = 0; i < m_networks_size; ++i) {
306+
if (str == m_networks.at(i)) return i;
307+
}
308+
return UNKNOWN_NETWORK;
309+
}
299310
uint8_t m_details_level{0}; //!< Optional user-supplied arg to set dashboard details level
300311
bool DetailsRequested() const { return m_details_level > 0 && m_details_level < 5; }
301312
bool IsAddressSelected() const { return m_details_level == 2 || m_details_level == 4; }
@@ -360,32 +371,19 @@ class NetinfoRequestHandler : public BaseRequestHandler
360371
}
361372

362373
// Count peer connection totals, and if DetailsRequested(), store peer data in a vector of structs.
363-
int ipv4_i{0}, ipv6_i{0}, onion_i{0}, block_relay_i{0}, total_i{0}; // inbound conn counters
364-
int ipv4_o{0}, ipv6_o{0}, onion_o{0}, block_relay_o{0}, total_o{0}; // outbound conn counters
365-
const UniValue& getpeerinfo{batch[ID_PEERINFO]["result"]};
366-
367-
for (const UniValue& peer : getpeerinfo.getValues()) {
368-
const bool is_block_relay{!peer["relaytxes"].get_bool()};
369-
const bool is_inbound{peer["inbound"].get_bool()};
374+
for (const UniValue& peer : batch[ID_PEERINFO]["result"].getValues()) {
370375
const std::string network{peer["network"].get_str()};
371-
if (is_inbound) {
372-
if (network == "ipv6") {
373-
++ipv6_i;
374-
} else if (network == "onion") {
375-
++onion_i;
376-
} else {
377-
++ipv4_i;
378-
}
379-
if (is_block_relay) ++block_relay_i;
380-
} else {
381-
if (network == "ipv6") {
382-
++ipv6_o;
383-
} else if (network == "onion") {
384-
++onion_o;
385-
} else {
386-
++ipv4_o;
387-
}
388-
if (is_block_relay) ++block_relay_o;
376+
const int8_t network_id{NetworkStringToId(network)};
377+
if (network_id == UNKNOWN_NETWORK) continue;
378+
const bool is_outbound{!peer["inbound"].get_bool()};
379+
const bool is_block_relay{!peer["relaytxes"].get_bool()};
380+
++m_counts.at(is_outbound).at(network_id); // in/out by network
381+
++m_counts.at(is_outbound).at(m_networks_size); // in/out overall
382+
++m_counts.at(2).at(network_id); // total by network
383+
++m_counts.at(2).at(m_networks_size); // total overall
384+
if (is_block_relay) {
385+
++m_counts.at(is_outbound).at(m_networks_size + 1); // in/out block-relay
386+
++m_counts.at(2).at(m_networks_size + 1); // total block-relay
389387
}
390388
if (DetailsRequested()) {
391389
// Push data for this peer to the peers vector.
@@ -401,7 +399,7 @@ class NetinfoRequestHandler : public BaseRequestHandler
401399
const double ping{peer["pingtime"].isNull() ? -1 : peer["pingtime"].get_real()};
402400
const std::string addr{peer["addr"].get_str()};
403401
const std::string sub_version{peer["subver"].get_str()};
404-
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_inbound});
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});
405403
m_max_id_length = std::max(ToString(peer_id).length(), m_max_id_length);
406404
m_max_addr_length = std::max(addr.length() + 1, m_max_addr_length);
407405
m_is_asmap_on |= (mapped_as != 0);
@@ -443,12 +441,11 @@ class NetinfoRequestHandler : public BaseRequestHandler
443441
}
444442

445443
// Report peer connection totals by type.
446-
total_i = ipv4_i + ipv6_i + onion_i;
447-
total_o = ipv4_o + ipv6_o + onion_o;
448444
result += " ipv4 ipv6 onion total block-relay\n";
449-
result += strprintf("in %5i %5i %5i %5i %5i\n", ipv4_i, ipv6_i, onion_i, total_i, block_relay_i);
450-
result += strprintf("out %5i %5i %5i %5i %5i\n", ipv4_o, ipv6_o, onion_o, total_o, block_relay_o);
451-
result += strprintf("total %5i %5i %5i %5i %5i\n", ipv4_i + ipv4_o, ipv6_i + ipv6_o, onion_i + onion_o, total_i + total_o, block_relay_i + block_relay_o);
445+
const std::array<std::string, 3> rows{{"in", "out", "total"}};
446+
for (size_t i = 0; i < m_networks_size; ++i) {
447+
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));
448+
}
452449

453450
// Report local addresses, ports, and scores.
454451
result += "\nLocal addresses";

0 commit comments

Comments
 (0)