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

Avoid attempting to sync with disconnected peers #5644

Merged
merged 3 commits into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
- Fixed: [#5581](https://github.com/ethereum/aleth/pull/5581) Fixed finding neighbour nodes in Discovery.
- Fixed: [#5599](https://github.com/ethereum/aleth/pull/5600) Prevent aleth from attempting concurrent connection to node which results in disconnect of original connection.
- Fixed: [#5609](https://github.com/ethereum/aleth/pull/5609) Log valid local enode-address when external IP is not known.
- Fixed: [#5627](https://github.com/ethereum/aleth/pull/5627) Correct testeth --help log output indentation
- Fixed: [#5627](https://github.com/ethereum/aleth/pull/5627) Correct testeth --help log output indentation.
- Fixed: [#5644](https://github.com/ethereum/aleth/pull/5644) Avoid attempting to sync with disconnected peers.


## [1.6.0] - 2019-04-16

Expand Down
33 changes: 28 additions & 5 deletions libp2p/Host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,20 @@ void Host::startPeerSession(Public const& _id, RLP const& _hello,
<< _s->remoteEndpoint();
}

/// Get session by id
shared_ptr<SessionFace> Host::peerSession(NodeID const& _id) const
{
RecursiveGuard l(x_sessions);
auto const it = m_sessions.find(_id);
if (it != m_sessions.end())
{
auto const s = it->second.lock();
if (s && s->isConnected())
return s;
}
return {};
}

void Host::onHandshakeFailed(NodeID const& _n, HandshakeFailureReason _r)
{
std::shared_ptr<Peer> p = peer(_n);
Expand Down Expand Up @@ -751,7 +765,7 @@ void Host::connect(shared_ptr<Peer> const& _p)
PeerSessionInfos Host::peerSessionInfos() const
{
if (!m_run)
return PeerSessionInfos();
return {};

vector<PeerSessionInfo> ret;
RecursiveGuard l(x_sessions);
Expand Down Expand Up @@ -918,13 +932,16 @@ void Host::keepAlivePeers()
RecursiveGuard l(x_sessions);
{
for (auto it = m_sessions.begin(); it != m_sessions.end();)
if (auto p = it->second.lock())
{
auto p = it->second.lock();
if (p && p->isConnected())
{
p->ping();
++it;
}
else
it = m_sessions.erase(it);
}
}

m_lastPing = chrono::steady_clock::now();
Expand All @@ -950,10 +967,13 @@ void Host::disconnectLatePeers()
return;

RecursiveGuard l(x_sessions);
for (auto p: m_sessions)
if (auto pp = p.second.lock())
for (auto p : m_sessions)
{
auto pp = p.second.lock();
if (pp && pp->isConnected())
if (now - c_keepAliveTimeOut > m_lastPing && pp->lastReceived() < m_lastPing)
pp->disconnect(PingTimeout);
}
}

bytes Host::saveNetwork() const
Expand Down Expand Up @@ -1155,13 +1175,16 @@ void Host::forEachPeer(
RecursiveGuard l(x_sessions);
vector<shared_ptr<SessionFace>> sessions;
for (auto const& i : m_sessions)
if (shared_ptr<SessionFace> s = i.second.lock())
{
auto const s = i.second.lock();
if (s && s->isConnected())
{
vector<CapDesc> capabilities = s->capabilities();
for (auto const& cap : capabilities)
if (cap.first == _capabilityName)
sessions.emplace_back(move(s));
}
}

// order peers by rating, connection age
auto sessionLess = [](shared_ptr<SessionFace> const& _left,
Expand Down
6 changes: 1 addition & 5 deletions libp2p/Host.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,7 @@ class Host: public Worker
std::unique_ptr<RLPXFrameCoder>&& _io, std::shared_ptr<RLPXSocket> const& _s);

/// Get session by id
std::shared_ptr<SessionFace> peerSession(NodeID const& _id) const
{
RecursiveGuard l(x_sessions);
return m_sessions.count(_id) ? m_sessions[_id].lock() : std::shared_ptr<SessionFace>();
}
std::shared_ptr<SessionFace> peerSession(NodeID const& _id) const;

/// Set a handshake failure reason for a peer
void onHandshakeFailed(NodeID const& _n, HandshakeFailureReason _r);
Expand Down