Browse Source

PoC-7: versioned subprotocols.

cl-refactor
Gav Wood 10 years ago
parent
commit
20ad46c481
  1. 2
      libdevcore/Common.cpp
  2. 2
      libethcore/CommonEth.cpp
  3. 3
      libethereum/EthereumPeer.h
  4. 1
      libp2p/Capability.h
  5. 8
      libp2p/Common.h
  6. 3
      libp2p/Host.cpp
  7. 13
      libp2p/Host.h
  8. 2
      libp2p/HostCapability.cpp
  9. 4
      libp2p/HostCapability.h
  10. 6
      libp2p/Session.cpp
  11. 4
      libp2p/Session.h
  12. 1
      libwhisper/WhisperPeer.h

2
libdevcore/Common.cpp

@ -27,7 +27,7 @@ using namespace dev;
namespace dev
{
char const* Version = "0.6.11";
char const* Version = "0.7.0";
}

2
libethcore/CommonEth.cpp

@ -34,7 +34,7 @@ namespace dev
namespace eth
{
const unsigned c_protocolVersion = 33;
const unsigned c_protocolVersion = 34;
const unsigned c_databaseVersion = 2;
static const vector<pair<u256, string>> g_units =

3
libethereum/EthereumPeer.h

@ -58,6 +58,9 @@ public:
/// What is our name?
static std::string name() { return "eth"; }
/// What is our version?
static u256 version() { return c_protocolVersion; }
/// What is the ethereum subprotocol host object.
EthereumHost* host() const;

1
libp2p/Capability.h

@ -39,6 +39,7 @@ public:
/// Must return the capability name.
static std::string name() { return ""; }
static u256 version() { return 0; }
Session* session() const { return m_session; }
HostCapabilityFace* hostCapability() const { return m_host; }

8
libp2p/Common.h

@ -24,6 +24,8 @@
#pragma once
#include <string>
#include <set>
#include <vector>
#include <boost/asio.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <chrono>
@ -88,13 +90,17 @@ enum DisconnectReason
/// @returns the string form of the given disconnection reason.
std::string reasonOf(DisconnectReason _r);
typedef std::pair<std::string, u256> CapDesc;
typedef std::set<CapDesc> CapDescSet;
typedef std::vector<CapDesc> CapDescs;
struct PeerInfo
{
std::string clientVersion;
std::string host;
unsigned short port;
std::chrono::steady_clock::duration lastPing;
std::set<std::string> caps;
std::set<CapDesc> caps;
unsigned socket;
std::map<std::string, std::string> notes;
};

3
libp2p/Host.cpp

@ -38,6 +38,7 @@
#include <libdevcore/Common.h>
#include <libethcore/Exceptions.h>
#include "Session.h"
#include "Common.h"
#include "Capability.h"
#include "UPnP.h"
using namespace std;
@ -145,7 +146,7 @@ unsigned Host::protocolVersion() const
return 0;
}
void Host::registerPeer(std::shared_ptr<Session> _s, vector<string> const& _caps)
void Host::registerPeer(std::shared_ptr<Session> _s, CapDescs const& _caps)
{
{
Guard l(x_peers);

13
libp2p/Host.h

@ -31,6 +31,7 @@
#include <libdevcore/Guards.h>
#include <libdevcore/Worker.h>
#include "HostCapability.h"
#include "Common.h"
namespace ba = boost::asio;
namespace bi = boost::asio::ip;
@ -75,11 +76,11 @@ public:
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[T::staticName()] = ret; return ret; }
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; }
bool haveCapability(std::string const& _name) const { return m_capabilities.count(_name) != 0; }
std::vector<std::string> caps() const { std::vector<std::string> ret; for (auto const& i: m_capabilities) ret.push_back(i.first); return ret; }
template <class T> std::shared_ptr<T> cap() const { try { return std::static_pointer_cast<T>(m_capabilities.at(T::staticName())); } catch (...) { return nullptr; } }
bool haveCapability(CapDesc const& _name) const { return m_capabilities.count(_name) != 0; }
CapDescs caps() const { CapDescs ret; for (auto const& i: m_capabilities) ret.push_back(i.first); return ret; }
template <class T> std::shared_ptr<T> cap() const { try { return std::static_pointer_cast<T>(m_capabilities.at(std::make_pair(T::staticName(), T::staticVersion()))); } catch (...) { return nullptr; } }
/// Connect to a peer explicitly.
static std::string pocHost();
@ -118,7 +119,7 @@ public:
h512 id() const { return m_id; }
void registerPeer(std::shared_ptr<Session> _s, std::vector<std::string> const& _caps);
void registerPeer(std::shared_ptr<Session> _s, CapDescs const& _caps);
private:
/// Called when the session has provided us with a new peer we can connect to.
@ -166,7 +167,7 @@ private:
std::vector<bi::address_v4> m_addresses;
std::vector<bi::address_v4> m_peerAddresses;
std::map<std::string, std::shared_ptr<HostCapabilityFace>> m_capabilities;
std::map<CapDesc, std::shared_ptr<HostCapabilityFace>> m_capabilities;
bool m_accepting = false;
};

2
libp2p/HostCapability.cpp

@ -38,7 +38,7 @@ std::vector<std::shared_ptr<Session> > HostCapabilityFace::peers() const
std::vector<std::shared_ptr<Session> > ret;
for (auto const& i: m_host->m_peers)
if (std::shared_ptr<Session> p = i.second.lock())
if (p->m_capabilities.count(name()))
if (p->m_capabilities.count(capDesc()))
ret.push_back(p);
return ret;
}

4
libp2p/HostCapability.h

@ -47,6 +47,8 @@ public:
protected:
virtual std::string name() const = 0;
virtual u256 version() const = 0;
CapDesc capDesc() const { return std::make_pair(name(), version()); }
virtual Capability* newPeerCapability(Session* _s) = 0;
virtual void onStarting() {}
@ -66,9 +68,11 @@ public:
virtual ~HostCapability() {}
static std::string staticName() { return PeerCap::name(); }
static u256 staticVersion() { return PeerCap::version(); }
protected:
virtual std::string name() const { return PeerCap::name(); }
virtual u256 version() const { return PeerCap::version(); }
virtual Capability* newPeerCapability(Session* _s) { return new PeerCap(_s, this); }
};

6
libp2p/Session.cpp

@ -40,7 +40,7 @@ Session::Session(Host* _s, bi::tcp::socket _socket, bi::address _peerAddress, un
{
m_disconnect = std::chrono::steady_clock::time_point::max();
m_connect = std::chrono::steady_clock::now();
m_info = PeerInfo({"?", _peerAddress.to_string(), m_listenPort, std::chrono::steady_clock::duration(0), set<string>(), 0, map<string, string>()});
m_info = PeerInfo({"?", _peerAddress.to_string(), m_listenPort, std::chrono::steady_clock::duration(0), CapDescSet(), 0, map<string, string>()});
}
Session::~Session()
@ -78,7 +78,7 @@ bool Session::interpret(RLP const& _r)
{
m_protocolVersion = _r[1].toInt<unsigned>();
auto clientVersion = _r[2].toString();
auto caps = _r[3].toVector<string>();
auto caps = _r[3].toVector<CapDesc>();
m_listenPort = _r[4].toInt<unsigned short>();
m_id = _r[5].toHash<h512>();
@ -102,7 +102,7 @@ bool Session::interpret(RLP const& _r)
return false;
}
try
{ m_info = PeerInfo({clientVersion, m_socket.remote_endpoint().address().to_string(), m_listenPort, std::chrono::steady_clock::duration(), _r[3].toSet<string>(), (unsigned)m_socket.native_handle(), map<string, string>() }); }
{ m_info = PeerInfo({clientVersion, m_socket.remote_endpoint().address().to_string(), m_listenPort, std::chrono::steady_clock::duration(), _r[3].toSet<CapDesc>(), (unsigned)m_socket.native_handle(), map<string, string>() }); }
catch (...)
{
disconnect(BadProtocol);

4
libp2p/Session.h

@ -62,7 +62,7 @@ public:
bi::tcp::endpoint endpoint() const; ///< for other peers to connect to.
template <class PeerCap>
std::shared_ptr<PeerCap> cap() const { try { return std::static_pointer_cast<PeerCap>(m_capabilities.at(PeerCap::name())); } catch (...) { return nullptr; } }
std::shared_ptr<PeerCap> cap() const { try { return std::static_pointer_cast<PeerCap>(m_capabilities.at(std::make_pair(PeerCap::name(), PeerCap::version()))); } catch (...) { return nullptr; } }
static RLPStream& prep(RLPStream& _s);
void sealAndSend(RLPStream& _s);
@ -108,7 +108,7 @@ private:
unsigned m_rating;
std::map<std::string, std::shared_ptr<Capability>> m_capabilities;
std::map<CapDesc, std::shared_ptr<Capability>> m_capabilities;
bool m_willBeDeleted = false; ///< True if we already posted a deleter on the strand.
};

1
libwhisper/WhisperPeer.h

@ -53,6 +53,7 @@ public:
virtual ~WhisperPeer();
static std::string name() { return "shh"; }
static u256 version() { return 1; }
WhisperHost* host() const;

Loading…
Cancel
Save