From 5d2238d179c0da4f7b88feee263649d0d67453e3 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Fri, 10 Oct 2014 17:03:46 +0200 Subject: [PATCH] Manage dead nodes. --- alethzero/MainWin.cpp | 5 +++-- libp2p/Host.cpp | 11 ++++------- libp2p/Host.h | 1 + libp2p/Session.cpp | 25 ++++++++++++++++--------- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/alethzero/MainWin.cpp b/alethzero/MainWin.cpp index 77384d7ea..f55bf2d9f 100644 --- a/alethzero/MainWin.cpp +++ b/alethzero/MainWin.cpp @@ -766,10 +766,11 @@ void Main::refreshNetwork() auto ns = web3()->nodes(); ui->nodes->clear(); for (p2p::Node const& i: ns) - ui->nodes->addItem(QString("[%1%3] %2 - ( =%5s | /%4s | %6 | %7x ) - *%8 $%9") + if (!i.dead) + ui->nodes->addItem(QString("[%1%3] %2 - ( =%5s | /%4s | %6 | %7x ) - *%8 $%9") .arg(QString::fromStdString(i.id.abridged())) .arg(QString::fromStdString(toString(i.address))) - .arg(i.id == web3()->id() ? " self" : i.isOffline() ? "" : " PEER") + .arg(i.id == web3()->id() ? " self" : i.isOffline() ? " ripe" : " ----") .arg(i.secondsSinceLastAttempted()) .arg(i.secondsSinceLastConnected()) .arg(QString::fromStdString(reasonOf(i.lastDisconnect))) diff --git a/libp2p/Host.cpp b/libp2p/Host.cpp index 82de1727e..44fa137e5 100644 --- a/libp2p/Host.cpp +++ b/libp2p/Host.cpp @@ -354,24 +354,21 @@ shared_ptr Host::noteNode(NodeId _id, bi::tcp::endpoint const& _a, Origin unsigned i; if (!m_nodes.count(_id)) { - shared_ptr old; if (m_nodes.count(_oldId)) { - old = m_nodes[_oldId]; - i = old->index; + i = m_nodes[_oldId]->index; m_nodes.erase(_oldId); m_nodesList[i] = _id; - m_nodes[id()] = make_shared(); } else { i = m_nodesList.size(); m_nodesList.push_back(_id); - m_nodes[_id] = make_shared(); } + m_nodes[_id] = make_shared(); + m_nodes[_id]->id = _id; m_nodes[_id]->address = _a; m_nodes[_id]->index = i; - m_nodes[_id]->id = _id; m_nodes[_id]->idOrigin = _o; } else @@ -665,7 +662,7 @@ bytes Host::saveNodes() const for (auto const& i: m_nodes) { Node const& n = *(i.second); - if (n.id != id() && !isPrivateAddress(n.address.address())) + if (!n.dead && n.id != id() && !isPrivateAddress(n.address.address())) { nodes.appendList(10); if (n.address.address().is_v4()) diff --git a/libp2p/Host.h b/libp2p/Host.h index 43a5afb25..a7beb8c35 100644 --- a/libp2p/Host.h +++ b/libp2p/Host.h @@ -64,6 +64,7 @@ struct Node bi::tcp::endpoint address; ///< As reported from the node itself. int score = 0; ///< All time cumulative. int rating = 0; ///< Trending. + bool dead = false; ///< If true, we believe this node is permanently dead - forget all about it. std::chrono::system_clock::time_point lastConnected; std::chrono::system_clock::time_point lastAttempted; unsigned failedAttempts = 0; diff --git a/libp2p/Session.cpp b/libp2p/Session.cpp index d647d97dc..3f224a72d 100644 --- a/libp2p/Session.cpp +++ b/libp2p/Session.cpp @@ -62,7 +62,7 @@ Session::Session(Host* _s, bi::tcp::socket _socket, std::shared_ptr const& Session::~Session() { - if (id() && !isPermanentProblem(m_node->lastDisconnect)) + if (id() && (!m_node || (!isPermanentProblem(m_node->lastDisconnect) && !m_node->dead))) m_server->m_ready += m_node->index; // Read-chain finished for one reason or another. @@ -201,14 +201,6 @@ bool Session::interpret(RLP const& _r) return false; } - if (m_server->havePeer(id)) - { - // Already connected. - clogS(NetWarn) << "Already connected to a peer with id" << id.abridged(); - disconnect(DuplicatePeer); - return false; - } - if (m_node && m_node->id != id) { if (m_force || m_node->idOrigin <= Origin::SelfThird) @@ -220,6 +212,21 @@ bool Session::interpret(RLP const& _r) disconnect(UnexpectedIdentity); return false; } + + if (m_server->havePeer(id)) + { + m_node->dead = true; + disconnect(DuplicatePeer); + return false; + } + } + + if (m_server->havePeer(id)) + { + // Already connected. + clogS(NetWarn) << "Already connected to a peer with id" << id.abridged(); + disconnect(DuplicatePeer); + return false; } if (!id)