Skip to content

Commit

Permalink
cli: tally peer connections by type
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatack committed Aug 31, 2020
1 parent 54799b6 commit a3653c1
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/bitcoin-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,39 @@ class NetinfoRequestHandler : public BaseRequestHandler
const std::vector<UniValue> batch{JSONRPCProcessBatchReply(batch_in)};
if (!batch[ID_PEERINFO]["error"].isNull()) return batch[ID_PEERINFO];
if (!batch[ID_NETWORKINFO]["error"].isNull()) return batch[ID_NETWORKINFO];

// Count peer connection totals.
int ipv4_i{0}, ipv6_i{0}, onion_i{0}, block_relay_i{0}; // inbound conn counters
int ipv4_o{0}, ipv6_o{0}, onion_o{0}, block_relay_o{0}; // outbound conn counters
const UniValue& getpeerinfo{batch[ID_PEERINFO]["result"]};

for (const UniValue& peer : getpeerinfo.getValues()) {
const std::string addr{peer["addr"].get_str()};
const std::string addr_local{peer["addrlocal"].isNull() ? "" : peer["addrlocal"].get_str()};
const int mapped_as{peer["mapped_as"].isNull() ? 0 : peer["mapped_as"].get_int()};
const bool is_block_relay{!peer["relaytxes"].get_bool()};
const bool is_inbound{peer["inbound"].get_bool()};
if (is_inbound) {
if (IsAddrIPv6(addr)) {
++ipv6_i;
} else if (IsInboundOnion(addr_local, mapped_as)) {
++onion_i;
} else {
++ipv4_i;
}
if (is_block_relay) ++block_relay_i;
} else {
if (IsAddrIPv6(addr)) {
++ipv6_o;
} else if (IsOutboundOnion(addr, mapped_as)) {
++onion_o;
} else {
++ipv4_o;
}
if (is_block_relay) ++block_relay_o;
}
}

std::string result;
return JSONRPCReplyObj(UniValue{result}, NullUniValue, 1);
}
Expand Down

0 comments on commit a3653c1

Please sign in to comment.