From ee1339c0cc1166e9ae10d7a7f056fca8a6459cde Mon Sep 17 00:00:00 2001 From: subtly Date: Wed, 6 May 2015 21:50:43 +0200 Subject: [PATCH] Add nodes directly to node table when creating network. --- libp2p/Host.cpp | 6 +++--- libp2p/NodeTable.cpp | 11 ++++++++++- libp2p/NodeTable.h | 6 ++++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/libp2p/Host.cpp b/libp2p/Host.cpp index 78c1102c1..56c111d3d 100644 --- a/libp2p/Host.cpp +++ b/libp2p/Host.cpp @@ -208,7 +208,7 @@ void Host::startPeerSession(Public const& _id, RLP const& _rlp, RLPXFrameIO* _io // create session so disconnects are managed auto ps = make_shared(this, _io, p, PeerSessionInfo({_id, clientVersion, _endpoint.address().to_string(), listenPort, chrono::steady_clock::duration(), _rlp[2].toSet(), 0, map()})); - if (protocolVersion != dev::p2p::c_protocolVersion) + if (protocolVersion < dev::p2p::c_protocolVersion) { ps->disconnect(IncompatibleProtocol); return; @@ -759,7 +759,7 @@ void Host::restoreNetwork(bytesConstRef _b) // todo: ipv6, bi::address_v6(i[0].toArray() Node n((NodeId)i[2], NodeIPEndpoint(bi::address_v4(i[0].toArray()), i[1].toInt(), i[1].toInt())); if (i.itemCount() == 3 && n.endpoint.isAllowed()) - m_nodeTable->addNode(n); + m_nodeTable->addNode(n, NodeTable::NodeRelation::Known); else if (i.itemCount() == 10) { n.required = i[3].toInt(); @@ -776,7 +776,7 @@ void Host::restoreNetwork(bytesConstRef _b) if (p->required) requirePeer(p->id, n.endpoint); else - m_nodeTable->addNode(*p.get()); + m_nodeTable->addNode(*p.get(), NodeTable::NodeRelation::Known); } } } diff --git a/libp2p/NodeTable.cpp b/libp2p/NodeTable.cpp index e324b8f86..9002ccee1 100644 --- a/libp2p/NodeTable.cpp +++ b/libp2p/NodeTable.cpp @@ -81,8 +81,17 @@ shared_ptr NodeTable::addNode(Public const& _pubk, NodeIPEndpoint con return addNode(node); } -shared_ptr NodeTable::addNode(Node const& _node) +shared_ptr NodeTable::addNode(Node const& _node, NodeRelation _relation) { + if (_relation) + { + shared_ptr ret(new NodeEntry(m_node, _node.id, _node.endpoint)); + ret->pending = false; + m_nodes[_node.id] = ret; + noteActiveNode(_node.id, _node.endpoint); + return ret; + } + // re-enable tcp checks when NAT hosts are handled by discover // we handle when tcp endpoint is 0 below if (_node.endpoint.address.to_string() == "0.0.0.0") diff --git a/libp2p/NodeTable.h b/libp2p/NodeTable.h index 07247462f..b1e6fc907 100644 --- a/libp2p/NodeTable.h +++ b/libp2p/NodeTable.h @@ -133,6 +133,8 @@ class NodeTable: UDPSocketEvents, public std::enable_shared_from_this using EvictionTimeout = std::pair; ///< First NodeId (NodeIdTimePoint) may be evicted and replaced with second NodeId. public: + enum NodeRelation { Unknown = 0, Known }; + /// Constructor requiring host for I/O, credentials, and IP Address and port to listen on. NodeTable(ba::io_service& _io, KeyPair const& _alias, NodeIPEndpoint const& _endpoint); ~NodeTable(); @@ -149,8 +151,8 @@ public: /// Add node. Node will be pinged and empty shared_ptr is returned if NodeId is uknown. std::shared_ptr addNode(Public const& _pubk, NodeIPEndpoint const& _ep); - /// Add node. Node will be pinged and empty shared_ptr is returned if node has never been seen. - std::shared_ptr addNode(Node const& _node); + /// Add node. Node will be pinged and empty shared_ptr is returned if node has never been seen or NodeId is empty. + std::shared_ptr addNode(Node const& _node, NodeRelation _relation = NodeRelation::Unknown); /// To be called when node table is empty. Runs node discovery with m_node.id as the target in order to populate node-table. void discover();