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...`
pull/764/head
Jon Atack 4 years ago
parent 82fd40216c
commit 6272604bef
No known key found for this signature in database
GPG Key ID: 4F5721B3D0E3921D

@ -296,6 +296,17 @@ public:
class NetinfoRequestHandler : public BaseRequestHandler class NetinfoRequestHandler : public BaseRequestHandler
{ {
private: private:
static constexpr int8_t UNKNOWN_NETWORK{-1};
static constexpr size_t m_networks_size{3};
const std::array<std::string, m_networks_size> m_networks{{"ipv4", "ipv6", "onion"}};
std::array<std::array<uint16_t, m_networks_size + 2>, 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) {
if (str == m_networks.at(i)) return i;
}
return UNKNOWN_NETWORK;
}
uint8_t m_details_level{0}; //!< Optional user-supplied arg to set dashboard details level uint8_t m_details_level{0}; //!< Optional user-supplied arg to set dashboard details level
bool DetailsRequested() const { return m_details_level > 0 && m_details_level < 5; } bool DetailsRequested() const { return m_details_level > 0 && m_details_level < 5; }
bool IsAddressSelected() const { return m_details_level == 2 || m_details_level == 4; } bool IsAddressSelected() const { return m_details_level == 2 || m_details_level == 4; }
@ -360,32 +371,19 @@ public:
} }
// Count peer connection totals, and if DetailsRequested(), store peer data in a vector of structs. // Count peer connection totals, and if DetailsRequested(), 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 for (const UniValue& peer : batch[ID_PEERINFO]["result"].getValues()) {
int ipv4_o{0}, ipv6_o{0}, onion_o{0}, block_relay_o{0}, total_o{0}; // outbound conn counters
const UniValue& getpeerinfo{batch[ID_PEERINFO]["result"]};
for (const UniValue& peer : getpeerinfo.getValues()) {
const bool is_block_relay{!peer["relaytxes"].get_bool()};
const bool is_inbound{peer["inbound"].get_bool()};
const std::string network{peer["network"].get_str()}; const std::string network{peer["network"].get_str()};
if (is_inbound) { const int8_t network_id{NetworkStringToId(network)};
if (network == "ipv6") { if (network_id == UNKNOWN_NETWORK) continue;
++ipv6_i; const bool is_outbound{!peer["inbound"].get_bool()};
} else if (network == "onion") { const bool is_block_relay{!peer["relaytxes"].get_bool()};
++onion_i; ++m_counts.at(is_outbound).at(network_id); // in/out by network
} else { ++m_counts.at(is_outbound).at(m_networks_size); // in/out overall
++ipv4_i; ++m_counts.at(2).at(network_id); // total by network
} ++m_counts.at(2).at(m_networks_size); // total overall
if (is_block_relay) ++block_relay_i; if (is_block_relay) {
} else { ++m_counts.at(is_outbound).at(m_networks_size + 1); // in/out block-relay
if (network == "ipv6") { ++m_counts.at(2).at(m_networks_size + 1); // total block-relay
++ipv6_o;
} else if (network == "onion") {
++onion_o;
} else {
++ipv4_o;
}
if (is_block_relay) ++block_relay_o;
} }
if (DetailsRequested()) { if (DetailsRequested()) {
// Push data for this peer to the peers vector. // Push data for this peer to the peers vector.
@ -401,7 +399,7 @@ public:
const double ping{peer["pingtime"].isNull() ? -1 : peer["pingtime"].get_real()}; const double ping{peer["pingtime"].isNull() ? -1 : peer["pingtime"].get_real()};
const std::string addr{peer["addr"].get_str()}; const std::string addr{peer["addr"].get_str()};
const std::string sub_version{peer["subver"].get_str()}; 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_inbound}); 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_max_id_length = std::max(ToString(peer_id).length(), m_max_id_length);
m_max_addr_length = std::max(addr.length() + 1, m_max_addr_length); m_max_addr_length = std::max(addr.length() + 1, m_max_addr_length);
m_is_asmap_on |= (mapped_as != 0); m_is_asmap_on |= (mapped_as != 0);
@ -443,12 +441,11 @@ public:
} }
// Report peer connection totals by type. // Report peer connection totals by type.
total_i = ipv4_i + ipv6_i + onion_i;
total_o = ipv4_o + ipv6_o + onion_o;
result += " ipv4 ipv6 onion total block-relay\n"; result += " ipv4 ipv6 onion total block-relay\n";
result += strprintf("in %5i %5i %5i %5i %5i\n", ipv4_i, ipv6_i, onion_i, total_i, block_relay_i); const std::array<std::string, 3> rows{{"in", "out", "total"}};
result += strprintf("out %5i %5i %5i %5i %5i\n", ipv4_o, ipv6_o, onion_o, total_o, block_relay_o); for (size_t i = 0; i < m_networks_size; ++i) {
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); 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. // Report local addresses, ports, and scores.
result += "\nLocal addresses"; result += "\nLocal addresses";

Loading…
Cancel
Save