Skip to content
This repository was archived by the owner on Oct 28, 2021. It is now read-only.

Commit 14c3526

Browse files
authored
Merge pull request #5575 from ethereum/log-active-peer-info
Log active peer count and peer list
2 parents f5db298 + 7552ad1 commit 14c3526

File tree

6 files changed

+54
-14
lines changed

6 files changed

+54
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Added: [#5537](https://github.com/ethereum/aleth/pull/5537) Creating Ethereum Node Record (ENR) at program start.
66
- Added: [#5557](https://github.com/ethereum/aleth/pull/5557) Improved debug logging of full sync.
77
- Added: [#5564](https://github.com/ethereum/aleth/pull/5564) Improved help output of Aleth by adding list of channels.
8+
- Added: [#5575](https://github.com/ethereum/aleth/pull/5575) Log active peer count and peer list every 30 seconds.
89
- Changed: [#5559](https://github.com/ethereum/aleth/pull/5559) Update peer validation error messages.
910
- Fixed: [#5562](https://github.com/ethereum/aleth/pull/5562) Don't send header request messages to peers that haven't sent us Status yet.
1011
- Changed: [#5568](https://github.com/ethereum/aleth/pull/5568) Improve rlpx handshake log messages and create new rlpx log channel.

libp2p/Common.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,16 @@ inline std::ostream& operator<<(std::ostream& _strm, NodeID const& _id)
267267
return _strm;
268268
}
269269

270+
inline boost::log::formatting_ostream& operator<<(
271+
boost::log::formatting_ostream& _strm, PeerSessionInfo const& _peerSessionInfo)
272+
{
273+
_strm << _peerSessionInfo.id << "|" << _peerSessionInfo.clientVersion << "|"
274+
<< _peerSessionInfo.host << "|" << _peerSessionInfo.port << "|";
275+
for (auto const& cap : _peerSessionInfo.caps)
276+
_strm << "(" << cap.first << "," << cap.second << ")";
277+
return _strm;
278+
}
279+
270280
/// Simple stream output for a NodeIPEndpoint.
271281
std::ostream& operator<<(std::ostream& _out, NodeIPEndpoint const& _ep);
272282

libp2p/Host.cpp

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ constexpr chrono::seconds c_keepAliveTimeOut{1};
3434

3535
/// Interval which m_runTimer is run when network is connected.
3636
constexpr chrono::milliseconds c_runTimerInterval{100};
37+
38+
/// Interval at which active peer info is logged
39+
constexpr chrono::seconds c_logActivePeersInterval{30};
3740
} // namespace
3841

3942
HostNodeTableHandler::HostNodeTableHandler(Host& _host): m_host(_host) {}
@@ -95,7 +98,8 @@ Host::Host(
9598
m_alias{_secretAndENR.first},
9699
m_enr{_secretAndENR.second},
97100
m_lastPing(chrono::steady_clock::time_point::min()),
98-
m_capabilityHost(createCapabilityHost(*this))
101+
m_capabilityHost(createCapabilityHost(*this)),
102+
m_lastPeerLogMessage(chrono::steady_clock::time_point::min())
99103
{
100104
cnetnote << "Id: " << id();
101105
cnetnote << "ENR: " << m_enr;
@@ -683,7 +687,7 @@ void Host::connect(shared_ptr<Peer> const& _p)
683687
});
684688
}
685689

686-
PeerSessionInfos Host::peerSessionInfo() const
690+
PeerSessionInfos Host::peerSessionInfos() const
687691
{
688692
if (!m_run)
689693
return PeerSessionInfos();
@@ -722,6 +726,7 @@ void Host::run(boost::system::error_code const& _ec)
722726
m_connecting.remove_if([](weak_ptr<RLPXHandshake> h){ return h.expired(); });
723727

724728
keepAlivePeers();
729+
logActivePeers();
725730

726731
// At this time peers will be disconnected based on natural TCP timeout.
727732
// disconnectLatePeers needs to be updated for the assumption that Session
@@ -841,18 +846,33 @@ void Host::keepAlivePeers()
841846
return;
842847

843848
RecursiveGuard l(x_sessions);
844-
for (auto it = m_sessions.begin(); it != m_sessions.end();)
845-
if (auto p = it->second.lock())
846-
{
847-
p->ping();
848-
++it;
849-
}
850-
else
851-
it = m_sessions.erase(it);
849+
{
850+
for (auto it = m_sessions.begin(); it != m_sessions.end();)
851+
if (auto p = it->second.lock())
852+
{
853+
p->ping();
854+
++it;
855+
}
856+
else
857+
it = m_sessions.erase(it);
858+
}
852859

853860
m_lastPing = chrono::steady_clock::now();
854861
}
855862

863+
void Host::logActivePeers()
864+
{
865+
if (!m_run || chrono::steady_clock::now() - c_logActivePeersInterval < m_lastPeerLogMessage)
866+
return;
867+
868+
LOG(m_infoLogger) << "Active peer count: " << peerCount();
869+
if (m_netConfig.discovery)
870+
LOG(m_infoLogger) << "Looking for peers...";
871+
872+
LOG(m_detailsLogger) << "Peers: " << peerSessionInfos();
873+
m_lastPeerLogMessage = chrono::steady_clock::now();
874+
}
875+
856876
void Host::disconnectLatePeers()
857877
{
858878
auto now = chrono::steady_clock::now();

libp2p/Host.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class Host: public Worker
168168
void setPeerStretch(unsigned _n) { m_stretchPeers = _n; }
169169

170170
/// Get peer information.
171-
PeerSessionInfos peerSessionInfo() const;
171+
PeerSessionInfos peerSessionInfos() const;
172172

173173
/// Get number of peers connected.
174174
size_t peerCount() const;
@@ -270,6 +270,9 @@ class Host: public Worker
270270
/// Ping the peers to update the latency information and disconnect peers which have timed out.
271271
void keepAlivePeers();
272272

273+
/// Log count of active peers and information about each peer
274+
void logActivePeers();
275+
273276
/// Disconnect peers which didn't respond to keepAlivePeers ping prior to c_keepAliveTimeOut.
274277
void disconnectLatePeers();
275278

@@ -371,7 +374,13 @@ class Host: public Worker
371374

372375
std::shared_ptr<CapabilityHostFace> m_capabilityHost;
373376

377+
/// When the last "active peers" message was logged - used to throttle
378+
/// logging to once every c_logActivePeersInterval seconds
379+
std::chrono::steady_clock::time_point m_lastPeerLogMessage;
380+
374381
Logger m_logger{createLogger(VerbosityDebug, "net")};
382+
Logger m_detailsLogger{createLogger(VerbosityTrace, "net")};
383+
Logger m_infoLogger{createLogger(VerbosityInfo, "net")};
375384
};
376385

377386
}

libwebthree/WebThree.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ std::string WebThreeDirect::composeClientVersion(std::string const& _client)
8080

8181
std::vector<PeerSessionInfo> WebThreeDirect::peers()
8282
{
83-
return m_net.peerSessionInfo();
83+
return m_net.peerSessionInfos();
8484
}
8585

8686
size_t WebThreeDirect::peerCount() const

test/unittests/libp2p/peer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ BOOST_AUTO_TEST_CASE(requirePeer)
274274
BOOST_REQUIRE_EQUAL(host1peerCount, 1);
275275
BOOST_REQUIRE_EQUAL(host2peerCount, 1);
276276

277-
PeerSessionInfos sis1 = host1.peerSessionInfo();
278-
PeerSessionInfos sis2 = host2.peerSessionInfo();
277+
PeerSessionInfos sis1 = host1.peerSessionInfos();
278+
PeerSessionInfos sis2 = host2.peerSessionInfos();
279279

280280
BOOST_REQUIRE_EQUAL(sis1.size(), 1);
281281
BOOST_REQUIRE_EQUAL(sis2.size(), 1);

0 commit comments

Comments
 (0)