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

Commit 64f120c

Browse files
committed
Add Peer::uselessPeer / log error when no peer found
Rather than use a magic number to indicate a useless peer (numeric_limits<unsigned>::max()), add a function to Peer (Peer::uselessPeer) which can be called to detect this. Also update Peer::fallbackSeconds() to return a number which is large enough to prevent us from realistically ever connecting to a useless peer but which is small enough to not overflow when added to Peer::m_lastAttempted. Also, log a message with error verbosity in Host::handshakeFailed() if a peer is not found rather than asserting, since while we never expect users to hit this case, the EIP8 handshake tests can.
1 parent a587575 commit 64f120c

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

libp2p/Host.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,11 @@ std::shared_ptr<Peer> Host::peer(NodeID const& _n) const
211211
void Host::handshakeFailed(NodeID const& _n, HandshakeFailureReason _r)
212212
{
213213
std::shared_ptr<Peer> p = peer(_n);
214-
assert(p);
214+
if (!p)
215+
{
216+
cerror << "Peer " << _n << " not found";
217+
return;
218+
}
215219
p->m_lastHandshakeFailure = _r;
216220
}
217221

@@ -812,7 +816,7 @@ void Host::run(boost::system::error_code const& _ec)
812816
reqConn++;
813817
else if (!haveSession)
814818
{
815-
if (p->second->fallbackSeconds() == numeric_limits<unsigned>::max())
819+
if (p->second->uselessPeer())
816820
p = m_peers.erase(p);
817821
else if (p->second->shouldReconnect() && (!m_netConfig.pin || required))
818822
toConnect.push_back(p->second);

libp2p/Peer.cpp

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,45 @@ bool Peer::shouldReconnect() const
4949
fallbackSeconds() != numeric_limits<unsigned>::max() &&
5050
chrono::system_clock::now() > m_lastAttempted + chrono::seconds(fallbackSeconds());
5151
}
52-
52+
53+
bool Peer::uselessPeer() const
54+
{
55+
switch (m_lastHandshakeFailure)
56+
{
57+
case FrameDecryptionFailure:
58+
case ProtocolError:
59+
return true;
60+
default:
61+
break;
62+
}
63+
64+
switch (m_lastDisconnect)
65+
{
66+
case BadProtocol:
67+
case UselessPeer:
68+
case IncompatibleProtocol:
69+
case UnexpectedIdentity:
70+
case UserReason:
71+
return true;
72+
default:
73+
break;
74+
}
75+
return false;
76+
}
77+
78+
5379
unsigned Peer::fallbackSeconds() const
5480
{
81+
constexpr unsigned oneYearInSeconds{60 * 60 * 24 * 360};
82+
5583
if (peerType == PeerType::Required)
5684
return 5;
5785

5886
switch (m_lastHandshakeFailure)
5987
{
6088
case FrameDecryptionFailure:
6189
case ProtocolError:
62-
return numeric_limits<unsigned>::max();
90+
return oneYearInSeconds;
6391
default:
6492
break;
6593
}
@@ -71,7 +99,7 @@ unsigned Peer::fallbackSeconds() const
7199
case IncompatibleProtocol:
72100
case UnexpectedIdentity:
73101
case UserReason:
74-
return numeric_limits<unsigned>::max();
102+
return oneYearInSeconds;
75103
case TooManyPeers:
76104
return 25 * (m_failedAttempts + 1);
77105
case ClientQuit:

libp2p/Peer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ class Peer: public Node
7070
/// Return true if connection attempt should be made to this peer or false if
7171
bool shouldReconnect() const;
7272

73+
/// A peer which should never be reconnected to - e.g. it's running on a different network, we
74+
/// don't have any common capabilities
75+
bool uselessPeer() const;
76+
7377
/// Number of times connection has been attempted to peer.
7478
int failedAttempts() const { return m_failedAttempts; }
7579

0 commit comments

Comments
 (0)