Skip to content

Commit 7b65287

Browse files
committed
cli: hoist networks class data members to a constant
1 parent 5bd40a3 commit 7b65287

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

src/bitcoin-cli.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ static constexpr int DEFAULT_WAIT_CLIENT_TIMEOUT = 0;
4949
static const bool DEFAULT_NAMED=false;
5050
static const int CONTINUE_EXECUTION=-1;
5151
static constexpr int8_t UNKNOWN_NETWORK{-1};
52+
static constexpr std::array NETWORKS{"ipv4", "ipv6", "onion", "i2p", "cjdns"};
5253

5354
/** Default number of blocks to generate for RPC generatetoaddress. */
5455
static const std::string DEFAULT_NBLOCKS = "1";
@@ -242,11 +243,10 @@ class BaseRequestHandler
242243
class AddrinfoRequestHandler : public BaseRequestHandler
243244
{
244245
private:
245-
static constexpr std::array m_networks{"ipv4", "ipv6", "onion", "i2p", "cjdns"};
246246
int8_t NetworkStringToId(const std::string& str) const
247247
{
248-
for (size_t i = 0; i < m_networks.size(); ++i) {
249-
if (str == m_networks.at(i)) return i;
248+
for (size_t i = 0; i < NETWORKS.size(); ++i) {
249+
if (str == NETWORKS[i]) return i;
250250
}
251251
return UNKNOWN_NETWORK;
252252
}
@@ -269,7 +269,7 @@ class AddrinfoRequestHandler : public BaseRequestHandler
269269
throw std::runtime_error("-addrinfo requires bitcoind server to be running v22.0 and up");
270270
}
271271
// Count the number of peers known to our node, by network.
272-
std::array<uint64_t, m_networks.size()> counts{{}};
272+
std::array<uint64_t, NETWORKS.size()> counts{{}};
273273
for (const UniValue& node : nodes) {
274274
std::string network_name{node["network"].get_str()};
275275
const int8_t network_id{NetworkStringToId(network_name)};
@@ -279,8 +279,8 @@ class AddrinfoRequestHandler : public BaseRequestHandler
279279
// Prepare result to return to user.
280280
UniValue result{UniValue::VOBJ}, addresses{UniValue::VOBJ};
281281
uint64_t total{0}; // Total address count
282-
for (size_t i = 0; i < m_networks.size(); ++i) {
283-
addresses.pushKV(m_networks.at(i), counts.at(i));
282+
for (size_t i = 0; i < NETWORKS.size(); ++i) {
283+
addresses.pushKV(NETWORKS[i], counts.at(i));
284284
total += counts.at(i);
285285
}
286286
addresses.pushKV("total", total);
@@ -363,14 +363,13 @@ class NetinfoRequestHandler : public BaseRequestHandler
363363
{
364364
private:
365365
static constexpr uint8_t MAX_DETAIL_LEVEL{4};
366-
static constexpr std::array m_networks{"ipv4", "ipv6", "onion", "i2p", "cjdns"};
367-
std::array<std::array<uint16_t, m_networks.size() + 1>, 3> m_counts{{{}}}; //!< Peer counts by (in/out/total, networks/total)
366+
std::array<std::array<uint16_t, NETWORKS.size() + 1>, 3> m_counts{{{}}}; //!< Peer counts by (in/out/total, networks/total)
368367
uint8_t m_block_relay_peers_count{0};
369368
uint8_t m_manual_peers_count{0};
370369
int8_t NetworkStringToId(const std::string& str) const
371370
{
372-
for (size_t i = 0; i < m_networks.size(); ++i) {
373-
if (str == m_networks.at(i)) return i;
371+
for (size_t i = 0; i < NETWORKS.size(); ++i) {
372+
if (str == NETWORKS[i]) return i;
374373
}
375374
return UNKNOWN_NETWORK;
376375
}
@@ -471,10 +470,10 @@ class NetinfoRequestHandler : public BaseRequestHandler
471470
const bool is_outbound{!peer["inbound"].get_bool()};
472471
const bool is_block_relay{!peer["relaytxes"].get_bool()};
473472
const std::string conn_type{peer["connection_type"].get_str()};
474-
++m_counts.at(is_outbound).at(network_id); // in/out by network
475-
++m_counts.at(is_outbound).at(m_networks.size()); // in/out overall
476-
++m_counts.at(2).at(network_id); // total by network
477-
++m_counts.at(2).at(m_networks.size()); // total overall
473+
++m_counts.at(is_outbound).at(network_id); // in/out by network
474+
++m_counts.at(is_outbound).at(NETWORKS.size()); // in/out overall
475+
++m_counts.at(2).at(network_id); // total by network
476+
++m_counts.at(2).at(NETWORKS.size()); // total overall
478477
if (conn_type == "block-relay-only") ++m_block_relay_peers_count;
479478
if (conn_type == "manual") ++m_manual_peers_count;
480479
if (DetailsRequested()) {
@@ -571,7 +570,7 @@ class NetinfoRequestHandler : public BaseRequestHandler
571570
for (int8_t n : reachable_networks) {
572571
result += strprintf("%8i", m_counts.at(i).at(n)); // network peers count
573572
}
574-
result += strprintf(" %5i", m_counts.at(i).at(m_networks.size())); // total peers count
573+
result += strprintf(" %5i", m_counts.at(i).at(NETWORKS.size())); // total peers count
575574
if (i == 1) { // the outbound row has two extra columns for block relay and manual peer counts
576575
result += strprintf(" %5i", m_block_relay_peers_count);
577576
if (m_manual_peers_count) result += strprintf(" %5i", m_manual_peers_count);

0 commit comments

Comments
 (0)