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 2 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
32 changes: 27 additions & 5 deletions libp2p/Host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,19 @@ 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);
if (m_sessions.count(_id))
halfalicious marked this conversation as resolved.
Show resolved Hide resolved
{
auto const s = m_sessions[_id].lock();
if (s && s->isConnected())
return s;
}
return{};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, still need to get git clang-format up and running again after a reinstall, will reformat manually for the time-being.

}

void Host::onHandshakeFailed(NodeID const& _n, HandshakeFailureReason _r)
{
std::shared_ptr<Peer> p = peer(_n);
Expand Down Expand Up @@ -751,7 +764,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 +931,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 +966,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 +1174,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())
{
shared_ptr<SessionFace> s = i.second.lock();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use auto as in other cases?

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