@@ -39,8 +39,6 @@ static const char DEFAULT_RPCCONNECT[] = "127.0.0.1";
39
39
static const int DEFAULT_HTTP_CLIENT_TIMEOUT=900 ;
40
40
static const bool DEFAULT_NAMED=false ;
41
41
static const int CONTINUE_EXECUTION=-1 ;
42
- static const std::string ONION{" .onion" };
43
- static const size_t ONION_LEN{ONION.size ()};
44
42
45
43
/* * Default number of blocks to generate for RPC generatetoaddress. */
46
44
static const std::string DEFAULT_NBLOCKS = " 1" ;
@@ -298,30 +296,24 @@ class GetinfoRequestHandler: public BaseRequestHandler
298
296
class NetinfoRequestHandler : public BaseRequestHandler
299
297
{
300
298
private:
301
- bool IsAddrIPv6 (const std::string& addr) const
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
302
304
{
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);
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;
315
309
}
316
310
uint8_t m_details_level{0 }; // !< Optional user-supplied arg to set dashboard details level
317
311
bool DetailsRequested () const { return m_details_level > 0 && m_details_level < 5 ; }
318
312
bool IsAddressSelected () const { return m_details_level == 2 || m_details_level == 4 ; }
319
313
bool IsVersionSelected () const { return m_details_level == 3 || m_details_level == 4 ; }
320
- enum struct NetType {
321
- ipv4,
322
- ipv6,
323
- onion,
324
- };
314
+ bool m_is_asmap_on{false };
315
+ size_t m_max_addr_length{0 };
316
+ size_t m_max_id_length{2 };
325
317
struct Peer {
326
318
int id;
327
319
int mapped_as;
@@ -334,30 +326,24 @@ class NetinfoRequestHandler : public BaseRequestHandler
334
326
double min_ping;
335
327
double ping;
336
328
std::string addr;
329
+ std::string network;
337
330
std::string sub_version;
338
- NetType net_type;
339
331
bool is_block_relay;
340
332
bool is_outbound;
341
333
bool operator <(const Peer& rhs) const { return std::tie (is_outbound, min_ping) < std::tie (rhs.is_outbound , rhs.min_ping ); }
342
334
};
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
- }
335
+ std::vector<Peer> m_peers;
352
336
std::string ChainToString () const
353
337
{
354
338
if (gArgs .GetChainName () == CBaseChainParams::TESTNET) return " testnet" ;
355
339
if (gArgs .GetChainName () == CBaseChainParams::REGTEST) return " regtest" ;
356
340
return " " ;
357
341
}
342
+ const int64_t m_time_now{GetSystemTimeInSeconds ()};
343
+
358
344
public:
359
- const int ID_PEERINFO = 0 ;
360
- const int ID_NETWORKINFO = 1 ;
345
+ static constexpr int ID_PEERINFO = 0 ;
346
+ static constexpr int ID_NETWORKINFO = 1 ;
361
347
362
348
UniValue PrepareRequest (const std::string& method, const std::vector<std::string>& args) override
363
349
{
@@ -385,104 +371,81 @@ class NetinfoRequestHandler : public BaseRequestHandler
385
371
}
386
372
387
373
// Count peer connection totals, and if DetailsRequested(), store peer data in a vector of structs.
388
- const int64_t time_now{GetSystemTimeInSeconds ()};
389
- int ipv4_i{0 }, ipv6_i{0 }, onion_i{0 }, block_relay_i{0 }, total_i{0 }; // inbound conn counters
390
- int ipv4_o{0 }, ipv6_o{0 }, onion_o{0 }, block_relay_o{0 }, total_o{0 }; // outbound conn counters
391
- size_t max_peer_id_length{2 }, max_addr_length{0 };
392
- bool is_asmap_on{false };
393
- std::vector<Peer> peers;
394
- const UniValue& getpeerinfo{batch[ID_PEERINFO][" result" ]};
395
-
396
- 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 ()};
374
+ for (const UniValue& peer : batch[ID_PEERINFO][" result" ].getValues ()) {
375
+ const std::string network{peer[" network" ].get_str ()};
376
+ const int8_t network_id{NetworkStringToId (network)};
377
+ if (network_id == UNKNOWN_NETWORK) continue ;
378
+ const bool is_outbound{!peer[" inbound" ].get_bool ()};
400
379
const bool is_block_relay{!peer[" relaytxes" ].get_bool ()};
401
- const bool is_inbound{peer[" inbound" ].get_bool ()};
402
- NetType net_type{NetType::ipv4};
403
- if (is_inbound) {
404
- if (IsAddrIPv6 (addr)) {
405
- net_type = NetType::ipv6;
406
- ++ipv6_i;
407
- } else if (IsInboundOnion (addr_local, mapped_as)) {
408
- net_type = NetType::onion;
409
- ++onion_i;
410
- } else {
411
- ++ipv4_i;
412
- }
413
- if (is_block_relay) ++block_relay_i;
414
- } else {
415
- if (IsAddrIPv6 (addr)) {
416
- net_type = NetType::ipv6;
417
- ++ipv6_o;
418
- } else if (IsOutboundOnion (addr, mapped_as)) {
419
- net_type = NetType::onion;
420
- ++onion_o;
421
- } else {
422
- ++ipv4_o;
423
- }
424
- if (is_block_relay) ++block_relay_o;
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
425
387
}
426
388
if (DetailsRequested ()) {
427
389
// Push data for this peer to the peers vector.
428
390
const int peer_id{peer[" id" ].get_int ()};
391
+ const int mapped_as{peer[" mapped_as" ].isNull () ? 0 : peer[" mapped_as" ].get_int ()};
429
392
const int version{peer[" version" ].get_int ()};
430
- const std::string sub_version{peer[" subver" ].get_str ()};
431
393
const int64_t conn_time{peer[" conntime" ].get_int64 ()};
432
394
const int64_t last_blck{peer[" last_block" ].get_int64 ()};
433
395
const int64_t last_recv{peer[" lastrecv" ].get_int64 ()};
434
396
const int64_t last_send{peer[" lastsend" ].get_int64 ()};
435
397
const int64_t last_trxn{peer[" last_transaction" ].get_int64 ()};
436
398
const double min_ping{peer[" minping" ].isNull () ? -1 : peer[" minping" ].get_real ()};
437
399
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});
439
- max_peer_id_length = std::max (ToString (peer_id).length (), max_peer_id_length);
440
- max_addr_length = std::max (addr.length () + 1 , max_addr_length);
441
- is_asmap_on |= (mapped_as != 0 );
400
+ const std::string addr{peer[" addr" ].get_str ()};
401
+ const std::string sub_version{peer[" subver" ].get_str ()};
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});
403
+ m_max_id_length = std::max (ToString (peer_id).length (), m_max_id_length);
404
+ m_max_addr_length = std::max (addr.length () + 1 , m_max_addr_length);
405
+ m_is_asmap_on |= (mapped_as != 0 );
442
406
}
443
407
}
444
408
445
409
// Generate report header.
446
410
std::string result{strprintf (" %s %s%s - %i%s\n\n " , PACKAGE_NAME, FormatFullVersion (), ChainToString (), networkinfo[" protocolversion" ].get_int (), networkinfo[" subversion" ].get_str ())};
447
411
448
412
// Report detailed peer connections list sorted by direction and minimum ping time.
449
- if (DetailsRequested () && !peers .empty ()) {
450
- std::sort (peers .begin (), peers .end ());
413
+ if (DetailsRequested () && !m_peers .empty ()) {
414
+ std::sort (m_peers .begin (), m_peers .end ());
451
415
result += " Peer connections sorted by direction and min ping\n <-> relay net mping ping send recv txn blk uptime " ;
452
- if (is_asmap_on ) result += " asmap " ;
453
- result += strprintf (" %*s %-*s%s\n " , max_peer_id_length , " id" , IsAddressSelected () ? max_addr_length : 0 , IsAddressSelected () ? " address" : " " , IsVersionSelected () ? " version" : " " );
454
- for (const Peer& peer : peers ) {
416
+ if (m_is_asmap_on ) result += " asmap " ;
417
+ result += strprintf (" %*s %-*s%s\n " , m_max_id_length , " id" , IsAddressSelected () ? m_max_addr_length : 0 , IsAddressSelected () ? " address" : " " , IsVersionSelected () ? " version" : " " );
418
+ for (const Peer& peer : m_peers ) {
455
419
std::string version{ToString (peer.version ) + peer.sub_version };
456
420
result += strprintf (
457
421
" %3s %5s %5s%6s%7s%5s%5s%5s%5s%7s%*i %*s %-*s%s\n " ,
458
422
peer.is_outbound ? " out" : " in" ,
459
423
peer.is_block_relay ? " block" : " full" ,
460
- NetTypeEnumToString ( peer.net_type ) ,
424
+ peer.network ,
461
425
peer.min_ping == -1 ? " " : ToString (round (1000 * peer.min_ping )),
462
426
peer.ping == -1 ? " " : ToString (round (1000 * peer.ping )),
463
- peer.last_send == 0 ? " " : ToString (time_now - peer.last_send ),
464
- peer.last_recv == 0 ? " " : ToString (time_now - peer.last_recv ),
465
- peer.last_trxn == 0 ? " " : ToString ((time_now - peer.last_trxn ) / 60 ),
466
- peer.last_blck == 0 ? " " : ToString ((time_now - peer.last_blck ) / 60 ),
467
- peer.conn_time == 0 ? " " : ToString ((time_now - peer.conn_time ) / 60 ),
468
- is_asmap_on ? 7 : 0 , // variable spacing
469
- is_asmap_on && peer.mapped_as != 0 ? ToString (peer.mapped_as ) : " " ,
470
- max_peer_id_length , // variable spacing
427
+ peer.last_send == 0 ? " " : ToString (m_time_now - peer.last_send ),
428
+ peer.last_recv == 0 ? " " : ToString (m_time_now - peer.last_recv ),
429
+ peer.last_trxn == 0 ? " " : ToString ((m_time_now - peer.last_trxn ) / 60 ),
430
+ peer.last_blck == 0 ? " " : ToString ((m_time_now - peer.last_blck ) / 60 ),
431
+ peer.conn_time == 0 ? " " : ToString ((m_time_now - peer.conn_time ) / 60 ),
432
+ m_is_asmap_on ? 7 : 0 , // variable spacing
433
+ m_is_asmap_on && peer.mapped_as != 0 ? ToString (peer.mapped_as ) : " " ,
434
+ m_max_id_length , // variable spacing
471
435
peer.id ,
472
- IsAddressSelected () ? max_addr_length : 0 , // variable spacing
436
+ IsAddressSelected () ? m_max_addr_length : 0 , // variable spacing
473
437
IsAddressSelected () ? peer.addr : " " ,
474
438
IsVersionSelected () && version != " 0" ? version : " " );
475
439
}
476
440
result += " ms ms sec sec min min min\n\n " ;
477
441
}
478
442
479
443
// Report peer connection totals by type.
480
- total_i = ipv4_i + ipv6_i + onion_i;
481
- total_o = ipv4_o + ipv6_o + onion_o;
482
444
result += " ipv4 ipv6 onion total block-relay\n " ;
483
- result += strprintf (" in %5i %5i %5i %5i %5i\n " , ipv4_i, ipv6_i, onion_i, total_i, block_relay_i);
484
- result += strprintf (" out %5i %5i %5i %5i %5i\n " , ipv4_o, ipv6_o, onion_o, total_o, block_relay_o);
485
- 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
+ }
486
449
487
450
// Report local addresses, ports, and scores.
488
451
result += " \n Local addresses" ;
0 commit comments