Skip to content

Commit 5133fab

Browse files
committed
cli: simplify -netinfo using getpeerinfo network field
1 parent 4938a10 commit 5133fab

File tree

1 file changed

+13
-49
lines changed

1 file changed

+13
-49
lines changed

src/bitcoin-cli.cpp

Lines changed: 13 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ static const char DEFAULT_RPCCONNECT[] = "127.0.0.1";
3939
static const int DEFAULT_HTTP_CLIENT_TIMEOUT=900;
4040
static const bool DEFAULT_NAMED=false;
4141
static const int CONTINUE_EXECUTION=-1;
42-
static const std::string ONION{".onion"};
43-
static const size_t ONION_LEN{ONION.size()};
4442

4543
/** Default number of blocks to generate for RPC generatetoaddress. */
4644
static const std::string DEFAULT_NBLOCKS = "1";
@@ -298,30 +296,10 @@ class GetinfoRequestHandler: public BaseRequestHandler
298296
class NetinfoRequestHandler : public BaseRequestHandler
299297
{
300298
private:
301-
bool IsAddrIPv6(const std::string& addr) const
302-
{
303-
return !addr.empty() && addr.front() == '[';
304-
}
305-
bool IsInboundOnion(const std::string& addr_local, int mapped_as) const
306-
{
307-
return mapped_as == 0 && addr_local.find(ONION) != std::string::npos;
308-
}
309-
bool IsOutboundOnion(const std::string& addr, int mapped_as) const
310-
{
311-
const size_t addr_len{addr.size()};
312-
const size_t onion_pos{addr.rfind(ONION)};
313-
return mapped_as == 0 && onion_pos != std::string::npos && addr_len > ONION_LEN &&
314-
(onion_pos == addr_len - ONION_LEN || onion_pos == addr.find_last_of(":") - ONION_LEN);
315-
}
316299
uint8_t m_details_level{0}; //!< Optional user-supplied arg to set dashboard details level
317300
bool DetailsRequested() const { return m_details_level > 0 && m_details_level < 5; }
318301
bool IsAddressSelected() const { return m_details_level == 2 || m_details_level == 4; }
319302
bool IsVersionSelected() const { return m_details_level == 3 || m_details_level == 4; }
320-
enum struct NetType {
321-
ipv4,
322-
ipv6,
323-
onion,
324-
};
325303
struct Peer {
326304
int id;
327305
int mapped_as;
@@ -334,30 +312,21 @@ class NetinfoRequestHandler : public BaseRequestHandler
334312
double min_ping;
335313
double ping;
336314
std::string addr;
315+
std::string network;
337316
std::string sub_version;
338-
NetType net_type;
339317
bool is_block_relay;
340318
bool is_outbound;
341319
bool operator<(const Peer& rhs) const { return std::tie(is_outbound, min_ping) < std::tie(rhs.is_outbound, rhs.min_ping); }
342320
};
343-
std::string NetTypeEnumToString(NetType t)
344-
{
345-
switch (t) {
346-
case NetType::ipv4: return "ipv4";
347-
case NetType::ipv6: return "ipv6";
348-
case NetType::onion: return "onion";
349-
} // no default case, so the compiler can warn about missing cases
350-
assert(false);
351-
}
352321
std::string ChainToString() const
353322
{
354323
if (gArgs.GetChainName() == CBaseChainParams::TESTNET) return " testnet";
355324
if (gArgs.GetChainName() == CBaseChainParams::REGTEST) return " regtest";
356325
return "";
357326
}
358327
public:
359-
const int ID_PEERINFO = 0;
360-
const int ID_NETWORKINFO = 1;
328+
static constexpr int ID_PEERINFO = 0;
329+
static constexpr int ID_NETWORKINFO = 1;
361330

362331
UniValue PrepareRequest(const std::string& method, const std::vector<std::string>& args) override
363332
{
@@ -394,29 +363,22 @@ class NetinfoRequestHandler : public BaseRequestHandler
394363
const UniValue& getpeerinfo{batch[ID_PEERINFO]["result"]};
395364

396365
for (const UniValue& peer : getpeerinfo.getValues()) {
397-
const std::string addr{peer["addr"].get_str()};
398-
const std::string addr_local{peer["addrlocal"].isNull() ? "" : peer["addrlocal"].get_str()};
399-
const int mapped_as{peer["mapped_as"].isNull() ? 0 : peer["mapped_as"].get_int()};
400366
const bool is_block_relay{!peer["relaytxes"].get_bool()};
401367
const bool is_inbound{peer["inbound"].get_bool()};
402-
NetType net_type{NetType::ipv4};
368+
const std::string network{peer["network"].get_str()};
403369
if (is_inbound) {
404-
if (IsAddrIPv6(addr)) {
405-
net_type = NetType::ipv6;
370+
if (network == "ipv6") {
406371
++ipv6_i;
407-
} else if (IsInboundOnion(addr_local, mapped_as)) {
408-
net_type = NetType::onion;
372+
} else if (network == "onion") {
409373
++onion_i;
410374
} else {
411375
++ipv4_i;
412376
}
413377
if (is_block_relay) ++block_relay_i;
414378
} else {
415-
if (IsAddrIPv6(addr)) {
416-
net_type = NetType::ipv6;
379+
if (network == "ipv6") {
417380
++ipv6_o;
418-
} else if (IsOutboundOnion(addr, mapped_as)) {
419-
net_type = NetType::onion;
381+
} else if (network == "onion") {
420382
++onion_o;
421383
} else {
422384
++ipv4_o;
@@ -426,16 +388,18 @@ class NetinfoRequestHandler : public BaseRequestHandler
426388
if (DetailsRequested()) {
427389
// Push data for this peer to the peers vector.
428390
const int peer_id{peer["id"].get_int()};
391+
const int mapped_as{peer["mapped_as"].isNull() ? 0 : peer["mapped_as"].get_int()};
429392
const int version{peer["version"].get_int()};
430-
const std::string sub_version{peer["subver"].get_str()};
431393
const int64_t conn_time{peer["conntime"].get_int64()};
432394
const int64_t last_blck{peer["last_block"].get_int64()};
433395
const int64_t last_recv{peer["lastrecv"].get_int64()};
434396
const int64_t last_send{peer["lastsend"].get_int64()};
435397
const int64_t last_trxn{peer["last_transaction"].get_int64()};
436398
const double min_ping{peer["minping"].isNull() ? -1 : peer["minping"].get_real()};
437399
const double ping{peer["pingtime"].isNull() ? -1 : peer["pingtime"].get_real()};
438-
peers.push_back({peer_id, mapped_as, version, conn_time, last_blck, last_recv, last_send, last_trxn, min_ping, ping, addr, sub_version, net_type, is_block_relay, !is_inbound});
400+
const std::string addr{peer["addr"].get_str()};
401+
const std::string sub_version{peer["subver"].get_str()};
402+
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});
439403
max_peer_id_length = std::max(ToString(peer_id).length(), max_peer_id_length);
440404
max_addr_length = std::max(addr.length() + 1, max_addr_length);
441405
is_asmap_on |= (mapped_as != 0);
@@ -457,7 +421,7 @@ class NetinfoRequestHandler : public BaseRequestHandler
457421
"%3s %5s %5s%6s%7s%5s%5s%5s%5s%7s%*i %*s %-*s%s\n",
458422
peer.is_outbound ? "out" : "in",
459423
peer.is_block_relay ? "block" : "full",
460-
NetTypeEnumToString(peer.net_type),
424+
peer.network,
461425
peer.min_ping == -1 ? "" : ToString(round(1000 * peer.min_ping)),
462426
peer.ping == -1 ? "" : ToString(round(1000 * peer.ping)),
463427
peer.last_send == 0 ? "" : ToString(time_now - peer.last_send),

0 commit comments

Comments
 (0)