Browse Source

Merge host and nodetable network protocol version.

cl-refactor
subtly 10 years ago
parent
commit
7445f9020e
  1. 2
      libp2p/Common.cpp
  2. 3
      libp2p/Common.h
  3. 11
      libp2p/Host.cpp
  4. 3
      libp2p/Host.h
  5. 2
      libp2p/NodeTable.cpp
  6. 5
      libp2p/NodeTable.h
  7. 2
      libp2p/RLPxHandshake.cpp
  8. 2
      test/peer.cpp

2
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.*"

3
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);

11
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<Peer> 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<Session>(this, _io, p, PeerSessionInfo({_id, clientVersion, _endpoint.address().to_string(), listenPort, chrono::steady_clock::duration(), _rlp[2].toSet<CapDesc>(), 0, map<string, string>()}));
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<int>() == NodeTable::c_version)
if (r.itemCount() > 0 && r[0].isInt() && r[0].toInt<unsigned>() == c_protocolVersion)
{
// r[0] = version
// r[1] = key

3
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 <class T> std::shared_ptr<T> registerCapability(T* _t) { _t->m_host = this; auto ret = std::shared_ptr<T>(_t); m_capabilities[std::make_pair(T::staticName(), T::staticVersion())] = ret; return ret; }

2
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);

5
libp2p/NodeTable.h

@ -135,9 +135,6 @@ class NodeTable: UDPSocketEvents, public std::enable_shared_from_this<NodeTable>
using EvictionTimeout = std::pair<std::pair<NodeId, TimePoint>, 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<PingNode>
static const uint8_t type = 1;
unsigned version = 2;
unsigned version = c_protocolVersion;
std::string ipAddress;
unsigned port;
unsigned expiration;

2
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()

2
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<int>() == NodeTable::c_version);
BOOST_REQUIRE(r[0].toInt<int>() == dev::p2p::c_protocolVersion);
BOOST_REQUIRE_EQUAL(r[1].toBytes().size(), 32); // secret
BOOST_REQUIRE_EQUAL(r[2].itemCount(), 5);
}

Loading…
Cancel
Save