From 387e67ee40c73006623481680c6e255d5ccedd60 Mon Sep 17 00:00:00 2001 From: caktux Date: Sat, 21 Mar 2015 15:57:40 -0400 Subject: [PATCH 01/11] apply fix for #1250 to neth, take some creds --- neth/main.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/neth/main.cpp b/neth/main.cpp index 275ac3ea4..80b3bc261 100644 --- a/neth/main.cpp +++ b/neth/main.cpp @@ -65,7 +65,7 @@ bool isFalse(std::string const& _m) void help() { cout - << "Usage neth [OPTIONS] " << endl + << "Usage neth [OPTIONS]" << endl << "Options:" << endl << " -a,--address Set the coinbase (mining payout) address to addr (default: auto)." << endl << " -c,--client-name Add a name to your client's version string (default: blank)." << endl @@ -122,7 +122,7 @@ string credits() std::ostringstream ccout; ccout << "NEthereum (++) " << dev::Version << endl - << " Code by Gav Wood & , (c) 2013, 2014." << endl + << " Code by Gav Wood & caktux, (c) 2013, 2014, 2015." << endl << " Based on a design by Vitalik Buterin." << endl << endl; ccout << "Type 'netstart 30303' to start networking" << endl; @@ -428,7 +428,10 @@ int main(int argc, char** argv) else if (arg == "-V" || arg == "--version") version(); else - remoteHost = argv[i]; + { + cerr << "Invalid argument: " << arg << endl; + exit(-1); + } } if (!clientName.empty()) From 360e2a32bc001803dddb4f607d1133a8b1c465fd Mon Sep 17 00:00:00 2001 From: subtly Date: Sun, 22 Mar 2015 13:59:47 +0100 Subject: [PATCH 02/11] update version number for save/restore of nodeTable --- libp2p/Host.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libp2p/Host.cpp b/libp2p/Host.cpp index 78a8232fd..082ff6a2f 100644 --- a/libp2p/Host.cpp +++ b/libp2p/Host.cpp @@ -708,7 +708,7 @@ bytes Host::saveNetwork() const } RLPStream ret(3); - ret << 1 << m_alias.secret(); + ret << 2 << m_alias.secret(); ret.appendList(count).appendRaw(network.out(), count); return ret.out(); } @@ -721,7 +721,7 @@ void Host::restoreNetwork(bytesConstRef _b) RecursiveGuard l(x_sessions); RLP r(_b); - if (r.itemCount() > 0 && r[0].isInt() && r[0].toInt() == 1) + if (r.itemCount() > 0 && r[0].isInt() && r[0].toInt() == 2) { // r[0] = version // r[1] = key From 11c1b13e272eb21be3825009ae331a0ddda6d21a Mon Sep 17 00:00:00 2001 From: subtly Date: Sun, 22 Mar 2015 17:12:14 +0100 Subject: [PATCH 03/11] clarify peersAdded log. Increment failedAttempts so fallback timer goes up. Temporarily exclude private addresses from network-restore. This exclusion will be removed when unit tests are written for local/private/public/failed-pnp scenarios. --- libp2p/Host.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/libp2p/Host.cpp b/libp2p/Host.cpp index 082ff6a2f..8226c35fb 100644 --- a/libp2p/Host.cpp +++ b/libp2p/Host.cpp @@ -260,7 +260,7 @@ void Host::onNodeTableEvent(NodeId const& _n, NodeTableEventType const& _e) p->required = n.required; m_peers[_n] = p; - clog(NetNote) << "p2p.host.peers.events.peersAdded " << _n << p->endpoint.tcp.address() << p->endpoint.udp.address(); + clog(NetNote) << "p2p.host.peers.events.peersAdded " << _n << "udp:" << p->endpoint.udp.address() << "tcp:" << p->endpoint.tcp.address(); } p->endpoint.tcp = n.endpoint.tcp; } @@ -474,6 +474,7 @@ void Host::connect(std::shared_ptr const& _p) clog(NetConnect) << "Connection refused to node" << _p->id.abridged() << "@" << _p->peerEndpoint() << "(" << ec.message() << ")"; _p->m_lastDisconnect = TCPError; _p->m_lastAttempted = std::chrono::system_clock::now(); + _p->m_failedAttempts++; } else { @@ -741,6 +742,13 @@ void Host::restoreNetwork(bytesConstRef _b) tcp = bi::tcp::endpoint(bi::address_v6(i[0].toArray()), i[1].toInt()); udp = bi::udp::endpoint(bi::address_v6(i[0].toArray()), i[1].toInt()); } + + // skip private addresses + // todo: to support private addresseses entries must be stored + // and managed externally by host rather than nodetable. + if (isPrivateAddress(tcp.address()) || isPrivateAddress(udp.address())) + continue; + auto id = (NodeId)i[2]; if (i.itemCount() == 3) m_nodeTable->addNode(id, udp, tcp); From b8f49b09292edccee50ed85feb8235ce2b54f463 Mon Sep 17 00:00:00 2001 From: subtly Date: Sun, 22 Mar 2015 17:41:35 +0100 Subject: [PATCH 04/11] Ignore 0.0.0.0 addresses. --- libp2p/NodeTable.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/libp2p/NodeTable.cpp b/libp2p/NodeTable.cpp index 0f51b29cf..a54f4acdd 100644 --- a/libp2p/NodeTable.cpp +++ b/libp2p/NodeTable.cpp @@ -70,6 +70,20 @@ shared_ptr NodeTable::addNode(Public const& _pubk, bi::udp::endpoint shared_ptr NodeTable::addNode(Node const& _node) { + if (_node.endpoint.udp.address().to_string() == "0.0.0.0" || _node.endpoint.tcp.address().to_string() == "0.0.0.0") + { + string ptype; + if (_node.endpoint.udp.address().to_string() != "0.0.0.0") + ptype = "TCP"; + else if (_node.endpoint.tcp.address().to_string() != "0.0.0.0") + ptype = "UDP"; + else + ptype = "TCP,UDP"; + + clog(NodeTableWarn) << "addNode Failed. Invalid" << ptype << "address 0.0.0.0 for" << _node.id.abridged(); + return move(shared_ptr()); + } + // ping address if nodeid is empty if (!_node.id) { From 3e2572e6a2a81409f88deaa82082d6adf61814e7 Mon Sep 17 00:00:00 2001 From: subtly Date: Sun, 22 Mar 2015 18:33:15 +0100 Subject: [PATCH 05/11] constant for discovery protocol version --- libp2p/Host.cpp | 4 ++-- libp2p/NodeTable.cpp | 2 +- libp2p/NodeTable.h | 4 ++++ test/peer.cpp | 2 +- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/libp2p/Host.cpp b/libp2p/Host.cpp index 8226c35fb..efa42cc17 100644 --- a/libp2p/Host.cpp +++ b/libp2p/Host.cpp @@ -709,7 +709,7 @@ bytes Host::saveNetwork() const } RLPStream ret(3); - ret << 2 << m_alias.secret(); + ret << NodeTable::s_version << m_alias.secret(); ret.appendList(count).appendRaw(network.out(), count); return ret.out(); } @@ -722,7 +722,7 @@ void Host::restoreNetwork(bytesConstRef _b) RecursiveGuard l(x_sessions); RLP r(_b); - if (r.itemCount() > 0 && r[0].isInt() && r[0].toInt() == 2) + if (r.itemCount() > 0 && r[0].isInt() && r[0].toInt() == NodeTable::s_version) { // r[0] = version // r[1] = key diff --git a/libp2p/NodeTable.cpp b/libp2p/NodeTable.cpp index a54f4acdd..288e89797 100644 --- a/libp2p/NodeTable.cpp +++ b/libp2p/NodeTable.cpp @@ -477,7 +477,7 @@ void NodeTable::onReceived(UDPSocketFace*, bi::udp::endpoint const& _from, bytes case PingNode::type: { PingNode in = PingNode::fromBytesConstRef(_from, rlpBytes); - if (in.version != 0x2) + if (in.version != s_version) { if (auto n = nodeEntry(nodeid)) dropNode(n); diff --git a/libp2p/NodeTable.h b/libp2p/NodeTable.h index fbeb53a2d..130f325ae 100644 --- a/libp2p/NodeTable.h +++ b/libp2p/NodeTable.h @@ -135,6 +135,10 @@ class NodeTable: UDPSocketEvents, public std::enable_shared_from_this using EvictionTimeout = std::pair, NodeId>; ///< First NodeId may be evicted and replaced with second NodeId. public: + /// Version of Discovery protocol + static unsigned const s_version = 2; + + /// Constructor requiring host for I/O, credentials, and IP Address,port to listen on. NodeTable(ba::io_service& _io, KeyPair _alias, uint16_t _udpPort = 30303); ~NodeTable(); diff --git a/test/peer.cpp b/test/peer.cpp index 14712d4f1..4732d319b 100644 --- a/test/peer.cpp +++ b/test/peer.cpp @@ -90,7 +90,7 @@ BOOST_AUTO_TEST_CASE(save_nodes) RLP r(firstHostNetwork); BOOST_REQUIRE(r.itemCount() == 3); - BOOST_REQUIRE(r[0].toInt() == 1); + BOOST_REQUIRE(r[0].toInt() == NodeTable::s_version); BOOST_REQUIRE_EQUAL(r[1].toBytes().size(), 32); // secret BOOST_REQUIRE_EQUAL(r[2].itemCount(), 5); } From 8cc7fb94ba3396fd169324d2d7588637cf93d6f9 Mon Sep 17 00:00:00 2001 From: subtly Date: Sun, 22 Mar 2015 18:35:34 +0100 Subject: [PATCH 06/11] codingstandards --- libp2p/Host.cpp | 4 ++-- libp2p/NodeTable.cpp | 2 +- libp2p/NodeTable.h | 2 +- test/peer.cpp | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/libp2p/Host.cpp b/libp2p/Host.cpp index efa42cc17..de4c19c2d 100644 --- a/libp2p/Host.cpp +++ b/libp2p/Host.cpp @@ -709,7 +709,7 @@ bytes Host::saveNetwork() const } RLPStream ret(3); - ret << NodeTable::s_version << m_alias.secret(); + ret << NodeTable::c_version << m_alias.secret(); ret.appendList(count).appendRaw(network.out(), count); return ret.out(); } @@ -722,7 +722,7 @@ void Host::restoreNetwork(bytesConstRef _b) RecursiveGuard l(x_sessions); RLP r(_b); - if (r.itemCount() > 0 && r[0].isInt() && r[0].toInt() == NodeTable::s_version) + if (r.itemCount() > 0 && r[0].isInt() && r[0].toInt() == NodeTable::c_version) { // r[0] = version // r[1] = key diff --git a/libp2p/NodeTable.cpp b/libp2p/NodeTable.cpp index 288e89797..a79eae12a 100644 --- a/libp2p/NodeTable.cpp +++ b/libp2p/NodeTable.cpp @@ -477,7 +477,7 @@ void NodeTable::onReceived(UDPSocketFace*, bi::udp::endpoint const& _from, bytes case PingNode::type: { PingNode in = PingNode::fromBytesConstRef(_from, rlpBytes); - if (in.version != s_version) + if (in.version != c_version) { if (auto n = nodeEntry(nodeid)) dropNode(n); diff --git a/libp2p/NodeTable.h b/libp2p/NodeTable.h index 130f325ae..4c2e52bd6 100644 --- a/libp2p/NodeTable.h +++ b/libp2p/NodeTable.h @@ -136,7 +136,7 @@ class NodeTable: UDPSocketEvents, public std::enable_shared_from_this public: /// Version of Discovery protocol - static unsigned const s_version = 2; + static unsigned const c_version = 2; /// Constructor requiring host for I/O, credentials, and IP Address,port to listen on. NodeTable(ba::io_service& _io, KeyPair _alias, uint16_t _udpPort = 30303); diff --git a/test/peer.cpp b/test/peer.cpp index 4732d319b..9a68401e0 100644 --- a/test/peer.cpp +++ b/test/peer.cpp @@ -90,7 +90,7 @@ BOOST_AUTO_TEST_CASE(save_nodes) RLP r(firstHostNetwork); BOOST_REQUIRE(r.itemCount() == 3); - BOOST_REQUIRE(r[0].toInt() == NodeTable::s_version); + BOOST_REQUIRE(r[0].toInt() == NodeTable::c_version); BOOST_REQUIRE_EQUAL(r[1].toBytes().size(), 32); // secret BOOST_REQUIRE_EQUAL(r[2].itemCount(), 5); } From ee7608fe603eea574c06cc90236df4089ae8d9ea Mon Sep 17 00:00:00 2001 From: subtly Date: Sun, 22 Mar 2015 18:39:56 +0100 Subject: [PATCH 07/11] NodeTable constructor requires and listens to explicit IP address instead of defaulting to 0.0.0.0 (all addresses). --- libp2p/Host.cpp | 5 ++--- libp2p/NodeTable.cpp | 6 +++--- libp2p/NodeTable.h | 4 ++-- libp2p/UDP.h | 4 ++++ test/net.cpp | 6 +++--- test/peer.cpp | 6 +++--- 6 files changed, 17 insertions(+), 14 deletions(-) diff --git a/libp2p/Host.cpp b/libp2p/Host.cpp index de4c19c2d..09c4db633 100644 --- a/libp2p/Host.cpp +++ b/libp2p/Host.cpp @@ -610,8 +610,7 @@ void Host::startedWorking() else clog(NetNote) << "p2p.start.notice id:" << id().abridged() << "Listen port is invalid or unavailable. Node Table using default port (30303)."; - // TODO: add m_tcpPublic endpoint; sort out endpoint stuff for nodetable - m_nodeTable.reset(new NodeTable(m_ioService, m_alias, m_listenPort > 0 ? m_listenPort : 30303)); + m_nodeTable.reset(new NodeTable(m_ioService, m_alias, bi::address::from_string(listenAddress()), listenPort() > 0 ? listenPort() : 30303)); m_nodeTable->setEventHandler(new HostNodeTableHandler(*this)); restoreNetwork(&m_restoreNetwork); @@ -675,7 +674,7 @@ bytes Host::saveNetwork() const // TODO: alpha: Figure out why it ever shares these ports.//p.address.port() >= 30300 && p.address.port() <= 30305 && // TODO: alpha: if/how to save private addresses // Only save peers which have connected within 2 days, with properly-advertised port and public IP address - if (chrono::system_clock::now() - p.m_lastConnected < chrono::seconds(3600 * 48) && p.peerEndpoint().port() > 0 && p.peerEndpoint().port() < /*49152*/32768 && p.id != id() && !isPrivateAddress(p.peerEndpoint().address())) + if (chrono::system_clock::now() - p.m_lastConnected < chrono::seconds(3600 * 48) && p.peerEndpoint().port() > 0 && p.peerEndpoint().port() < /*49152*/32768 && p.id != id() && !isPrivateAddress(p.endpoint.udp.address()) && !isPrivateAddress(p.endpoint.tcp.address())) { network.appendList(10); if (p.peerEndpoint().address().is_v4()) diff --git a/libp2p/NodeTable.cpp b/libp2p/NodeTable.cpp index a79eae12a..4710c5471 100644 --- a/libp2p/NodeTable.cpp +++ b/libp2p/NodeTable.cpp @@ -27,11 +27,11 @@ using namespace dev::p2p; NodeEntry::NodeEntry(Node _src, Public _pubk, NodeIPEndpoint _gw): Node(_pubk, _gw), distance(NodeTable::distance(_src.id,_pubk)) {} NodeEntry::NodeEntry(Node _src, Public _pubk, bi::udp::endpoint _udp): Node(_pubk, NodeIPEndpoint(_udp)), distance(NodeTable::distance(_src.id,_pubk)) {} -NodeTable::NodeTable(ba::io_service& _io, KeyPair _alias, uint16_t _udp): - m_node(Node(_alias.pub(), bi::udp::endpoint())), +NodeTable::NodeTable(ba::io_service& _io, KeyPair _alias, bi::address const& _udpAddress, uint16_t _udp): + m_node(Node(_alias.pub(), bi::udp::endpoint(_udpAddress, _udp))), m_secret(_alias.sec()), m_io(_io), - m_socket(new NodeSocket(m_io, *this, _udp)), + m_socket(new NodeSocket(m_io, *this, m_node.endpoint.udp)), m_socketPointer(m_socket.get()), m_bucketRefreshTimer(m_io), m_evictionCheckTimer(m_io) diff --git a/libp2p/NodeTable.h b/libp2p/NodeTable.h index 4c2e52bd6..49be27045 100644 --- a/libp2p/NodeTable.h +++ b/libp2p/NodeTable.h @@ -138,8 +138,8 @@ public: /// Version of Discovery protocol static unsigned const c_version = 2; - /// Constructor requiring host for I/O, credentials, and IP Address,port to listen on. - NodeTable(ba::io_service& _io, KeyPair _alias, uint16_t _udpPort = 30303); + /// Constructor requiring host for I/O, credentials, and IP Address and port to listen on. + NodeTable(ba::io_service& _io, KeyPair _alias, bi::address const& _udpAddress, uint16_t _udpPort = 30303); ~NodeTable(); /// Returns distance based on xor metric two node ids. Used by NodeEntry and NodeTable. diff --git a/libp2p/UDP.h b/libp2p/UDP.h index 3c3399acb..091f3cc2a 100644 --- a/libp2p/UDP.h +++ b/libp2p/UDP.h @@ -113,6 +113,10 @@ public: enum { maxDatagramSize = MaxDatagramSize }; static_assert(maxDatagramSize < 65507, "UDP datagrams cannot be larger than 65507 bytes"); + /// Create socket for specific endpoint. + UDPSocket(ba::io_service& _io, UDPSocketEvents& _host, bi::udp::endpoint _endpoint): m_host(_host), m_endpoint(_endpoint), m_socket(_io) { m_started.store(false); m_closed.store(true); }; + + /// Create socket which listens to all ports. UDPSocket(ba::io_service& _io, UDPSocketEvents& _host, unsigned _port): m_host(_host), m_endpoint(bi::udp::v4(), _port), m_socket(_io) { m_started.store(false); m_closed.store(true); }; virtual ~UDPSocket() { disconnect(); } diff --git a/test/net.cpp b/test/net.cpp index 3a69a627b..e5cca9b66 100644 --- a/test/net.cpp +++ b/test/net.cpp @@ -53,7 +53,7 @@ protected: struct TestNodeTable: public NodeTable { /// Constructor - TestNodeTable(ba::io_service& _io, KeyPair _alias, uint16_t _port = 30300): NodeTable(_io, _alias, _port) {} + TestNodeTable(ba::io_service& _io, KeyPair _alias, bi::address const& _addr, uint16_t _port = 30300): NodeTable(_io, _alias, _addr, _port) {} static std::vector> createTestNodes(unsigned _count) { @@ -106,10 +106,10 @@ struct TestNodeTable: public NodeTable */ struct TestNodeTableHost: public TestHost { - TestNodeTableHost(unsigned _count = 8): m_alias(KeyPair::create()), nodeTable(new TestNodeTable(m_io, m_alias)), testNodes(TestNodeTable::createTestNodes(_count)) {}; + TestNodeTableHost(unsigned _count = 8): m_alias(KeyPair::create()), nodeTable(new TestNodeTable(m_io, m_alias, bi::address::from_string("127.0.0.1"))), testNodes(TestNodeTable::createTestNodes(_count)) {}; ~TestNodeTableHost() { m_io.stop(); stopWorking(); } - void setup() { for (auto n: testNodes) nodeTables.push_back(make_shared(m_io,n.first,n.second)); } + void setup() { for (auto n: testNodes) nodeTables.push_back(make_shared(m_io,n.first, bi::address::from_string("127.0.0.1"),n.second)); } void pingAll() { for (auto& t: nodeTables) t->pingTestNodes(testNodes); } diff --git a/test/peer.cpp b/test/peer.cpp index 9a68401e0..3ad2e2946 100644 --- a/test/peer.cpp +++ b/test/peer.cpp @@ -35,8 +35,8 @@ BOOST_AUTO_TEST_CASE(host) auto oldLogVerbosity = g_logVerbosity; g_logVerbosity = 10; - NetworkPreferences host1prefs(30301, "127.0.0.1", true, true); - NetworkPreferences host2prefs(30302, "127.0.0.1", true, true); + NetworkPreferences host1prefs(30301, "127.0.0.1", false, true); + NetworkPreferences host2prefs(30302, "127.0.0.1", false, true); Host host1("Test", host1prefs); host1.start(); @@ -62,7 +62,7 @@ BOOST_AUTO_TEST_CASE(save_nodes) std::list hosts; for (auto i:{0,1,2,3,4,5}) { - Host* h = new Host("Test", NetworkPreferences(30300 + i, "127.0.0.1", true, true)); + Host* h = new Host("Test", NetworkPreferences(30300 + i, "127.0.0.1", false, true)); h->setIdealPeerCount(10); // starting host is required so listenport is available h->start(); From 14c2e4cab856ec67f8385787fe05de1b5e58fab9 Mon Sep 17 00:00:00 2001 From: subtly Date: Sun, 22 Mar 2015 19:05:39 +0100 Subject: [PATCH 08/11] fix net/ unit tests for nodetable updates --- libp2p/NodeTable.cpp | 4 ++-- test/net.cpp | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/libp2p/NodeTable.cpp b/libp2p/NodeTable.cpp index 4710c5471..7cac14938 100644 --- a/libp2p/NodeTable.cpp +++ b/libp2p/NodeTable.cpp @@ -340,7 +340,7 @@ void NodeTable::noteActiveNode(Public const& _pubk, bi::udp::endpoint const& _en s.nodes.push_back(node); s.touch(); - if (!removed) + if (!removed && m_nodeEventHandler) m_nodeEventHandler->appendEvent(node->id, NodeEntryAdded); } } @@ -349,7 +349,7 @@ void NodeTable::noteActiveNode(Public const& _pubk, bi::udp::endpoint const& _en s.nodes.push_back(node); s.touch(); - if (!removed) + if (!removed && m_nodeEventHandler) m_nodeEventHandler->appendEvent(node->id, NodeEntryAdded); } } diff --git a/test/net.cpp b/test/net.cpp index e5cca9b66..23ad0fb4a 100644 --- a/test/net.cpp +++ b/test/net.cpp @@ -89,7 +89,16 @@ struct TestNodeTable: public NodeTable bi::address ourIp = bi::address::from_string("127.0.0.1"); for (auto& n: _testNodes) if (_count--) + { + // manually add node for test + { + Guard ln(x_nodes); + shared_ptr node(new NodeEntry(m_node, n.first.pub(), NodeIPEndpoint(bi::udp::endpoint(ourIp, n.second), bi::tcp::endpoint(ourIp, n.second)))); + node->pending = false; + m_nodes[node->id] = node; + } noteActiveNode(n.first.pub(), bi::udp::endpoint(ourIp, n.second)); + } else break; } From 7445f9020efeac740b99c9656c7cbf5fd540dbbf Mon Sep 17 00:00:00 2001 From: subtly Date: Sun, 22 Mar 2015 20:08:32 +0100 Subject: [PATCH 09/11] Merge host and nodetable network protocol version. --- libp2p/Common.cpp | 2 ++ libp2p/Common.h | 3 +++ libp2p/Host.cpp | 11 +++-------- libp2p/Host.h | 3 --- libp2p/NodeTable.cpp | 2 +- libp2p/NodeTable.h | 5 +---- libp2p/RLPxHandshake.cpp | 2 +- test/peer.cpp | 2 +- 8 files changed, 12 insertions(+), 18 deletions(-) diff --git a/libp2p/Common.cpp b/libp2p/Common.cpp index e0f3b5629..0429c52ab 100644 --- a/libp2p/Common.cpp +++ b/libp2p/Common.cpp @@ -24,6 +24,8 @@ using namespace std; using namespace dev; using namespace dev::p2p; +const unsigned dev::p2p::c_protocolVersion = 3; + // Helper function to determine if an address falls within one of the reserved ranges // For V4: // Class A "10.*", Class B "172.[16->31].*", Class C "192.168.*" diff --git a/libp2p/Common.h b/libp2p/Common.h index ddf5f78b8..66b3f08e8 100644 --- a/libp2p/Common.h +++ b/libp2p/Common.h @@ -48,6 +48,9 @@ class RLPStream; namespace p2p { +/// Peer network protocol version. +extern const unsigned c_protocolVersion; + using NodeId = h512; bool isPrivateAddress(bi::address const& _addressToCheck); diff --git a/libp2p/Host.cpp b/libp2p/Host.cpp index 4f8ce7981..30cadb515 100644 --- a/libp2p/Host.cpp +++ b/libp2p/Host.cpp @@ -176,11 +176,6 @@ void Host::doneWorking() m_sessions.clear(); } -unsigned Host::protocolVersion() const -{ - return 3; -} - void Host::startPeerSession(Public const& _id, RLP const& _rlp, RLPXFrameIO* _io, bi::tcp::endpoint _endpoint) { shared_ptr p; @@ -211,7 +206,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 != this->protocolVersion()) + if (protocolVersion != dev::p2p::c_protocolVersion) { ps->disconnect(IncompatibleProtocol); return; @@ -714,7 +709,7 @@ bytes Host::saveNetwork() const } RLPStream ret(3); - ret << NodeTable::c_version << m_alias.secret(); + ret << c_protocolVersion << m_alias.secret(); ret.appendList(count).appendRaw(network.out(), count); return ret.out(); } @@ -727,7 +722,7 @@ void Host::restoreNetwork(bytesConstRef _b) RecursiveGuard l(x_sessions); RLP r(_b); - if (r.itemCount() > 0 && r[0].isInt() && r[0].toInt() == NodeTable::c_version) + if (r.itemCount() > 0 && r[0].isInt() && r[0].toInt() == c_protocolVersion) { // r[0] = version // r[1] = key diff --git a/libp2p/Host.h b/libp2p/Host.h index 50abad2af..0feda364f 100644 --- a/libp2p/Host.h +++ b/libp2p/Host.h @@ -97,9 +97,6 @@ public: /// Default host for current version of client. static std::string pocHost(); - /// Basic peer network protocol version. - unsigned protocolVersion() const; - /// Register a peer-capability; all new peer connections will have this capability. template std::shared_ptr registerCapability(T* _t) { _t->m_host = this; auto ret = std::shared_ptr(_t); m_capabilities[std::make_pair(T::staticName(), T::staticVersion())] = ret; return ret; } diff --git a/libp2p/NodeTable.cpp b/libp2p/NodeTable.cpp index 7cac14938..1f024a911 100644 --- a/libp2p/NodeTable.cpp +++ b/libp2p/NodeTable.cpp @@ -477,7 +477,7 @@ void NodeTable::onReceived(UDPSocketFace*, bi::udp::endpoint const& _from, bytes case PingNode::type: { PingNode in = PingNode::fromBytesConstRef(_from, rlpBytes); - if (in.version != c_version) + if (in.version != c_protocolVersion) { if (auto n = nodeEntry(nodeid)) dropNode(n); diff --git a/libp2p/NodeTable.h b/libp2p/NodeTable.h index 49be27045..10ba232c3 100644 --- a/libp2p/NodeTable.h +++ b/libp2p/NodeTable.h @@ -135,9 +135,6 @@ class NodeTable: UDPSocketEvents, public std::enable_shared_from_this using EvictionTimeout = std::pair, NodeId>; ///< First NodeId may be evicted and replaced with second NodeId. public: - /// Version of Discovery protocol - static unsigned const c_version = 2; - /// Constructor requiring host for I/O, credentials, and IP Address and port to listen on. NodeTable(ba::io_service& _io, KeyPair _alias, bi::address const& _udpAddress, uint16_t _udpPort = 30303); ~NodeTable(); @@ -319,7 +316,7 @@ struct PingNode: RLPXDatagram static const uint8_t type = 1; - unsigned version = 2; + unsigned version = c_protocolVersion; std::string ipAddress; unsigned port; unsigned expiration; diff --git a/libp2p/RLPxHandshake.cpp b/libp2p/RLPxHandshake.cpp index 4d8f2417f..1e42995ee 100644 --- a/libp2p/RLPxHandshake.cpp +++ b/libp2p/RLPxHandshake.cpp @@ -172,7 +172,7 @@ void RLPXHandshake::transition(boost::system::error_code _ech) // 5 arguments, HelloPacket RLPStream s; s.append((unsigned)0).appendList(5) - << m_host->protocolVersion() + << dev::p2p::c_protocolVersion << m_host->m_clientVersion << m_host->caps() << m_host->listenPort() diff --git a/test/peer.cpp b/test/peer.cpp index 3ad2e2946..ba8fb0b6e 100644 --- a/test/peer.cpp +++ b/test/peer.cpp @@ -90,7 +90,7 @@ BOOST_AUTO_TEST_CASE(save_nodes) RLP r(firstHostNetwork); BOOST_REQUIRE(r.itemCount() == 3); - BOOST_REQUIRE(r[0].toInt() == NodeTable::c_version); + BOOST_REQUIRE(r[0].toInt() == dev::p2p::c_protocolVersion); BOOST_REQUIRE_EQUAL(r[1].toBytes().size(), 32); // secret BOOST_REQUIRE_EQUAL(r[2].itemCount(), 5); } From 60931709905a582d60ce23792b907b827ad2f6ad Mon Sep 17 00:00:00 2001 From: subtly Date: Sun, 22 Mar 2015 20:18:08 +0100 Subject: [PATCH 10/11] consistent and explicit use of constant for devp2p protocol version --- libp2p/Host.cpp | 4 ++-- libp2p/NodeTable.cpp | 2 +- libp2p/NodeTable.h | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/libp2p/Host.cpp b/libp2p/Host.cpp index 30cadb515..13ea87ba6 100644 --- a/libp2p/Host.cpp +++ b/libp2p/Host.cpp @@ -709,7 +709,7 @@ bytes Host::saveNetwork() const } RLPStream ret(3); - ret << c_protocolVersion << m_alias.secret(); + ret << dev::p2p::c_protocolVersion << m_alias.secret(); ret.appendList(count).appendRaw(network.out(), count); return ret.out(); } @@ -722,7 +722,7 @@ void Host::restoreNetwork(bytesConstRef _b) RecursiveGuard l(x_sessions); RLP r(_b); - if (r.itemCount() > 0 && r[0].isInt() && r[0].toInt() == c_protocolVersion) + if (r.itemCount() > 0 && r[0].isInt() && r[0].toInt() == dev::p2p::c_protocolVersion) { // r[0] = version // r[1] = key diff --git a/libp2p/NodeTable.cpp b/libp2p/NodeTable.cpp index 1f024a911..0d6dda214 100644 --- a/libp2p/NodeTable.cpp +++ b/libp2p/NodeTable.cpp @@ -477,7 +477,7 @@ void NodeTable::onReceived(UDPSocketFace*, bi::udp::endpoint const& _from, bytes case PingNode::type: { PingNode in = PingNode::fromBytesConstRef(_from, rlpBytes); - if (in.version != c_protocolVersion) + if (in.version != dev::p2p::c_protocolVersion) { if (auto n = nodeEntry(nodeid)) dropNode(n); diff --git a/libp2p/NodeTable.h b/libp2p/NodeTable.h index 10ba232c3..58fa722c1 100644 --- a/libp2p/NodeTable.h +++ b/libp2p/NodeTable.h @@ -316,7 +316,7 @@ struct PingNode: RLPXDatagram static const uint8_t type = 1; - unsigned version = c_protocolVersion; + unsigned version = dev::p2p::c_protocolVersion; std::string ipAddress; unsigned port; unsigned expiration; From cddb2920a97db9a80bcb724e55e9bf5e7fe89166 Mon Sep 17 00:00:00 2001 From: subtly Date: Sun, 22 Mar 2015 20:38:27 +0100 Subject: [PATCH 11/11] namespace eth network-protocol version for implemenations --- alethzero/MainWin.cpp | 4 ++-- eth/main.cpp | 2 +- libethereum/Client.cpp | 4 ++-- libethereum/State.cpp | 2 +- neth/main.cpp | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/alethzero/MainWin.cpp b/alethzero/MainWin.cpp index dfd439816..d5e81a4f5 100644 --- a/alethzero/MainWin.cpp +++ b/alethzero/MainWin.cpp @@ -145,7 +145,7 @@ Main::Main(QWidget *parent) : cerr << "Block Hash: " << CanonBlockChain::genesis().hash << endl; cerr << "Block RLP: " << RLP(block) << endl; cerr << "Block Hex: " << toHex(block) << endl; - cerr << "Network protocol version: " << c_protocolVersion << endl; + cerr << "eth Network protocol version: " << eth::c_protocolVersion << endl; cerr << "Client database version: " << c_databaseVersion << endl; ui->configDock->close(); @@ -1059,7 +1059,7 @@ void Main::refreshBlockCount() { cwatch << "refreshBlockCount()"; auto d = ethereum()->blockChain().details(); - ui->blockCount->setText(QString("%4 #%1 PV%2 D%3 H%5").arg(d.number).arg(c_protocolVersion).arg(c_databaseVersion).arg(m_privateChain.size() ? "[" + m_privateChain + "] " : "testnet").arg(c_ethashVersion)); + ui->blockCount->setText(QString("%4 #%1 PV%2 D%3 H%5").arg(d.number).arg(eth::c_protocolVersion).arg(c_databaseVersion).arg(m_privateChain.size() ? "[" + m_privateChain + "] " : "testnet").arg(c_ethashVersion)); } void Main::on_turboMining_triggered() diff --git a/eth/main.cpp b/eth/main.cpp index ce89e8ddf..eda6e12ae 100644 --- a/eth/main.cpp +++ b/eth/main.cpp @@ -160,7 +160,7 @@ string credits(bool _interactive = false) void version() { cout << "eth version " << dev::Version << endl; - cout << "Network protocol version: " << dev::eth::c_protocolVersion << endl; + cout << "eth network protocol version: " << dev::eth::c_protocolVersion << endl; cout << "Client database version: " << dev::eth::c_databaseVersion << endl; cout << "Build: " << DEV_QUOTED(ETH_BUILD_PLATFORM) << "/" << DEV_QUOTED(ETH_BUILD_TYPE) << endl; exit(0); diff --git a/libethereum/Client.cpp b/libethereum/Client.cpp index 6725d1560..883b7f615 100644 --- a/libethereum/Client.cpp +++ b/libethereum/Client.cpp @@ -40,7 +40,7 @@ VersionChecker::VersionChecker(string const& _dbPath): { auto protocolContents = contents(m_path + "/protocol"); auto databaseContents = contents(m_path + "/database"); - m_ok = RLP(protocolContents).toInt(RLP::LaisezFaire) == c_protocolVersion && RLP(databaseContents).toInt(RLP::LaisezFaire) == c_databaseVersion; + m_ok = RLP(protocolContents).toInt(RLP::LaisezFaire) == eth::c_protocolVersion && RLP(databaseContents).toInt(RLP::LaisezFaire) == c_databaseVersion; } void VersionChecker::setOk() @@ -55,7 +55,7 @@ void VersionChecker::setOk() { cwarn << "Unhandled exception! Failed to create directory: " << m_path << "\n" << boost::current_exception_diagnostic_information(); } - writeFile(m_path + "/protocol", rlp(c_protocolVersion)); + writeFile(m_path + "/protocol", rlp(eth::c_protocolVersion)); writeFile(m_path + "/database", rlp(c_databaseVersion)); } } diff --git a/libethereum/State.cpp b/libethereum/State.cpp index 0d2b3a93e..c5732116d 100644 --- a/libethereum/State.cpp +++ b/libethereum/State.cpp @@ -1040,7 +1040,7 @@ LastHashes State::getLastHashes(BlockChain const& _bc, unsigned _n) const { LastHashes ret; ret.resize(256); - if (c_protocolVersion > 49) + if (eth::c_protocolVersion > 49) { ret[0] = _bc.numberHash(_n); for (unsigned i = 1; i < 256; ++i) diff --git a/neth/main.cpp b/neth/main.cpp index 275ac3ea4..9ea28a83e 100644 --- a/neth/main.cpp +++ b/neth/main.cpp @@ -134,7 +134,7 @@ string credits() void version() { cout << "neth version " << dev::Version << endl; - cout << "Network protocol version: " << dev::eth::c_protocolVersion << endl; + cout << "eth network protocol version: " << dev::eth::c_protocolVersion << endl; cout << "Client database version: " << dev::eth::c_databaseVersion << endl; cout << "Build: " << DEV_QUOTED(ETH_BUILD_PLATFORM) << "/" << DEV_QUOTED(ETH_BUILD_TYPE) << endl; exit(0);