diff --git a/libp2p/Host.cpp b/libp2p/Host.cpp index 45ebd6db1..357fc4a02 100644 --- a/libp2p/Host.cpp +++ b/libp2p/Host.cpp @@ -182,10 +182,8 @@ void Host::startPeerSession(Public const& _id, RLP const& _rlp, RLPXFrameIO* _io } else p = m_peers[_id]; - p->m_lastDisconnect = NoDisconnect; if (p->isOffline()) p->m_lastConnected = std::chrono::system_clock::now(); - p->m_failedAttempts = 0; p->endpoint.tcp.address(_endpoint.address()); auto protocolVersion = _rlp[0].toInt(); @@ -484,12 +482,14 @@ void Host::connect(std::shared_ptr const& _p) auto socket = make_shared(new bi::tcp::socket(m_ioService)); socket->ref().async_connect(_p->peerEndpoint(), [=](boost::system::error_code const& ec) { + _p->m_lastAttempted = std::chrono::system_clock::now(); + _p->m_failedAttempts++; + if (ec) { clog(NetConnect) << "Connection refused to node" << _p->id.abridged() << "@" << _p->peerEndpoint() << "(" << ec.message() << ")"; + // Manually set error (session not present) _p->m_lastDisconnect = TCPError; - _p->m_lastAttempted = std::chrono::system_clock::now(); - _p->m_failedAttempts++; } else { @@ -499,9 +499,7 @@ void Host::connect(std::shared_ptr const& _p) Guard l(x_connecting); m_connecting.push_back(handshake); } - - // preempt setting failedAttempts; this value is cleared upon success - _p->m_failedAttempts++; + handshake->start(); } diff --git a/libp2p/Session.cpp b/libp2p/Session.cpp index dac588149..9a82a38a0 100644 --- a/libp2p/Session.cpp +++ b/libp2p/Session.cpp @@ -358,16 +358,13 @@ void Session::drop(DisconnectReason _reason) } catch (...) {} - if (m_peer) + if (m_peer->m_lastDisconnect == NoDisconnect && (_reason == ClientQuit || _reason == DisconnectRequested)) + m_peer->m_failedAttempts = 0; + m_peer->m_lastDisconnect = _reason; + if (_reason == BadProtocol) { - if (_reason != m_peer->m_lastDisconnect || _reason == NoDisconnect || _reason == ClientQuit || _reason == DisconnectRequested) - m_peer->m_failedAttempts = 0; - m_peer->m_lastDisconnect = _reason; - if (_reason == BadProtocol) - { - m_peer->m_rating /= 2; - m_peer->m_score /= 2; - } + m_peer->m_rating /= 2; + m_peer->m_score /= 2; } m_dropped = true; }