Fabcoin Core  0.16.2
P2P Digital Currency
Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
dev::p2p::Host Class Reference

The Host class Capabilities should be registered prior to startNetwork, since m_capabilities is not thread-safe. More...

#include <Host.h>

Inheritance diagram for dev::p2p::Host:
[legend]
Collaboration diagram for dev::p2p::Host:
[legend]

Public Member Functions

 Host (std::string const &_clientVersion, NetworkPreferences const &_n=NetworkPreferences(), bytesConstRef _restoreNetwork=bytesConstRef())
 Start server, listening for connections on the given port. More...
 
 Host (std::string const &_clientVersion, KeyPair const &_alias, NetworkPreferences const &_n=NetworkPreferences())
 Alternative constructor that allows providing the node key directly without restoring the network. More...
 
virtual ~Host ()
 Will block on network process events. More...
 
template<class T >
std::shared_ptr< TregisterCapability (std::shared_ptr< T > const &_t)
 Register a peer-capability; all new peer connections will have this capability. More...
 
template<class T >
void addCapability (std::shared_ptr< T > const &_p, std::string const &_name, u256 const &_version)
 
bool haveCapability (CapDesc const &_name) const
 
CapDescs caps () const
 
template<class T >
std::shared_ptr< Tcap () const
 
void addPeer (NodeSpec const &_s, PeerType _t)
 Add a potential peer. More...
 
void addNode (NodeID const &_node, NodeIPEndpoint const &_endpoint)
 Add node as a peer candidate. Node is added if discovery ping is successful and table has capacity. More...
 
void requirePeer (NodeID const &_node, NodeIPEndpoint const &_endpoint)
 Create Peer and attempt keeping peer connected. More...
 
void requirePeer (NodeID const &_node, bi::address const &_addr, unsigned short _udpPort, unsigned short _tcpPort)
 Create Peer and attempt keeping peer connected. More...
 
void relinquishPeer (NodeID const &_node)
 Note peer as no longer being required. More...
 
void setIdealPeerCount (unsigned _n)
 Set ideal number of peers. More...
 
void setPeerStretch (unsigned _n)
 Set multipier for max accepted connections. More...
 
PeerSessionInfos peerSessionInfo () const
 Get peer information. More...
 
size_t peerCount () const
 Get number of peers connected. More...
 
std::string listenAddress () const
 Get the address we're listening on currently. More...
 
unsigned short listenPort () const
 Get the port we're listening on currently. More...
 
bytes saveNetwork () const
 Serialise the set of known peers. More...
 
Peers getPeers () const
 
NetworkPreferences const & networkPreferences () const
 
void setNetworkPreferences (NetworkPreferences const &_p, bool _dropPeers=false)
 
void start ()
 Start network. . More...
 
void stop ()
 Stop network. More...
 
bool isStarted () const
 
ReputationManagerrepMan ()
 
bool haveNetwork () const
 
void startPeerSession (Public const &_id, RLP const &_hello, std::unique_ptr< RLPXFrameCoder > &&_io, std::shared_ptr< RLPXSocket > const &_s)
 Validates and starts peer session, taking ownership of _io. Disconnects and returns false upon error. More...
 
std::shared_ptr< SessionFacepeerSession (NodeID const &_id)
 Get session by id. More...
 
NodeID id () const
 Get our current node ID. More...
 
bi::tcp::endpoint const & tcpPublic () const
 Get the public TCP endpoint. More...
 
std::string enode () const
 Get the public endpoint information. More...
 
p2p::NodeInfo nodeInfo () const
 Get the node information. More...
 

Static Public Member Functions

static std::unordered_map< Public, std::string > pocHosts ()
 Default hosts for current version of client. More...
 

Protected Member Functions

void onNodeTableEvent (NodeID const &_n, NodeTableEventType const &_e)
 
void restoreNetwork (bytesConstRef _b)
 Deserialise the data and populate the set of known peers. More...
 
- Protected Member Functions inherited from dev::Worker
 Worker (std::string const &_name="anon", unsigned _idleWaitMs=30)
 
 Worker (Worker &&_m)
 Move-constructor. More...
 
Workeroperator= (Worker &&_m)
 Move-assignment. More...
 
virtual ~Worker ()
 
void setName (std::string _n)
 Allows changing worker name if work is stopped. More...
 
void startWorking ()
 Starts worker thread; causes startedWorking() to be called. More...
 
void stopWorking ()
 Stop worker thread; causes call to stopWorking(). More...
 
bool isWorking () const
 Returns if worker thread is present. More...
 
virtual void workLoop ()
 Overrides doWork(); should call shouldStop() often and exit when true. More...
 
bool shouldStop () const
 

Private Types

enum  PeerSlotType { Egress, Ingress }
 

Private Member Functions

unsigned peerSlots (PeerSlotType _type)
 
bool havePeerSession (NodeID const &_id)
 
void determinePublic ()
 Determines and sets m_tcpPublic to publicly advertised address. More...
 
void connect (std::shared_ptr< Peer > const &_p)
 
bool peerSlotsAvailable (PeerSlotType _type=Ingress)
 Returns true if pending and connected peer count is less than maximum. More...
 
void keepAlivePeers ()
 Ping the peers to update the latency information and disconnect peers which have timed out. More...
 
void disconnectLatePeers ()
 Disconnect peers which didn't respond to keepAlivePeers ping prior to c_keepAliveTimeOut. More...
 
void runAcceptor ()
 Called only from startedWorking(). More...
 
virtual void startedWorking ()
 Called by Worker. Not thread-safe; to be called only by worker. More...
 
void run (boost::system::error_code const &error)
 Called by startedWorking. Not thread-safe; to be called only be Worker. More...
 
virtual void doWork ()
 Run network. Not thread-safe; to be called only by worker. More...
 
virtual void doneWorking ()
 Shutdown network. Not thread-safe; to be called only by worker. More...
 

Static Private Member Functions

static KeyPair networkAlias (bytesConstRef _b)
 Get or create host identifier (KeyPair). More...
 

Private Attributes

bytes m_restoreNetwork
 Set by constructor and used to set Host key and restore network peers & nodes. More...
 
bool m_run = false
 Whether network is running. More...
 
std::mutex x_runTimer
 Start/stop mutex. More...
 
std::string m_clientVersion
 Our version string. More...
 
NetworkPreferences m_netPrefs
 Network settings. More...
 
std::set< bi::addressm_ifAddresses
 Interface addresses (private, public) More...
 
int m_listenPort = -1
 What port are we listening on. -1 means binding failed or acceptor hasn't been initialized. More...
 
ba::io_service m_ioService
 IOService for network stuff. More...
 
bi::tcp::acceptor m_tcp4Acceptor
 Listening acceptor. More...
 
std::unique_ptr< boost::asio::deadline_timer > m_timer
 Timer which, when network is running, calls scheduler() every c_timerInterval ms. More...
 
std::set< Peer * > m_pendingPeerConns
 
Mutex x_pendingNodeConns
 Used only by connect(Peer&) to limit concurrently connecting to same node. See connect(shared_ptr<Peer>const&). More...
 
bi::tcp::endpoint m_tcpPublic
 Our public listening endpoint. More...
 
KeyPair m_alias
 Alias for network communication. Network address is k*G. k is key material. TODO: Replace KeyPair. More...
 
std::shared_ptr< NodeTablem_nodeTable
 Node table (uses kademlia-like discovery). More...
 
std::unordered_map< NodeID, std::shared_ptr< Peer > > m_peers
 Shared storage of Peer objects. Peers are created or destroyed on demand by the Host. Active sessions maintain a shared_ptr to a Peer;. More...
 
std::set< NodeIDm_requiredPeers
 Peers we try to connect regardless of p2p network. More...
 
Mutex x_requiredPeers
 
std::unordered_map< NodeID, std::weak_ptr< SessionFace > > m_sessions
 The nodes to which we are currently connected. More...
 
RecursiveMutex x_sessions
 
std::list< std::weak_ptr< RLPXHandshake > > m_connecting
 Pending connections. More...
 
Mutex x_connecting
 Mutex for m_connecting. More...
 
unsigned m_idealPeerCount = 11
 Ideal number of peers to be connected to. More...
 
unsigned m_stretchPeers = 7
 Accepted connection multiplier (max peers = ideal*stretch). More...
 
std::map< CapDesc, std::shared_ptr< HostCapabilityFace > > m_capabilities
 Each of the capabilities we support. More...
 
std::list< std::shared_ptr< boost::asio::deadline_timer > > m_timers
 Deadline timers used for isolated network events. GC'd by run. More...
 
Mutex x_timers
 
std::chrono::steady_clock::time_point m_lastPing
 Time we sent the last ping to all peers. More...
 
bool m_accepting = false
 
bool m_dropPeers = false
 
ReputationManager m_repMan
 

Static Private Attributes

static const unsigned c_timerInterval = 100
 Interval which m_timer is run when network is connected. More...
 

Friends

class HostNodeTableHandler
 
class RLPXHandshake
 
class Session
 
class HostCapabilityFace
 

Detailed Description

The Host class Capabilities should be registered prior to startNetwork, since m_capabilities is not thread-safe.

Todo:

determinePublic: ipv6, udp

per-session keepalive/ping instead of broadcast; set ping-timeout via median-latency

Definition at line 129 of file Host.h.

Member Enumeration Documentation

Enumerator
Egress 
Ingress 

Definition at line 251 of file Host.h.

Constructor & Destructor Documentation

dev::p2p::Host::Host ( std::string const &  _clientVersion,
NetworkPreferences const &  _n = NetworkPreferences(),
bytesConstRef  _restoreNetwork = bytesConstRef() 
)

Start server, listening for connections on the given port.

Here is the caller graph for this function:

dev::p2p::Host::Host ( std::string const &  _clientVersion,
KeyPair const &  _alias,
NetworkPreferences const &  _n = NetworkPreferences() 
)

Alternative constructor that allows providing the node key directly without restoring the network.

Host::~Host ( )
virtual

Will block on network process events.

Definition at line 119 of file Host.cpp.

Here is the call graph for this function:

Member Function Documentation

template<class T >
void dev::p2p::Host::addCapability ( std::shared_ptr< T > const &  _p,
std::string const &  _name,
u256 const &  _version 
)
inline

Definition at line 161 of file Host.h.

Here is the caller graph for this function:

void Host::addNode ( NodeID const &  _node,
NodeIPEndpoint const &  _endpoint 
)

Add node as a peer candidate. Node is added if discovery ping is successful and table has capacity.

Definition at line 501 of file Host.cpp.

Here is the caller graph for this function:

void Host::addPeer ( NodeSpec const &  _s,
PeerType  _t 
)

Add a potential peer.

Definition at line 493 of file Host.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

template<class T >
std::shared_ptr<T> dev::p2p::Host::cap ( ) const
inline

Definition at line 165 of file Host.h.

CapDescs dev::p2p::Host::caps ( ) const
inline

Definition at line 164 of file Host.h.

void Host::connect ( std::shared_ptr< Peer > const &  _p)
private

Definition at line 569 of file Host.cpp.

void Host::determinePublic ( )
private

Determines and sets m_tcpPublic to publicly advertised address.

Definition at line 382 of file Host.cpp.

Here is the call graph for this function:

void Host::disconnectLatePeers ( )
private

Disconnect peers which didn't respond to keepAlivePeers ping prior to c_keepAliveTimeOut.

Definition at line 798 of file Host.cpp.

void Host::doneWorking ( )
privatevirtual

Shutdown network. Not thread-safe; to be called only by worker.

Reimplemented from dev::Worker.

Definition at line 165 of file Host.cpp.

void Host::doWork ( )
privatevirtual

Run network. Not thread-safe; to be called only by worker.

Reimplemented from dev::Worker.

Definition at line 766 of file Host.cpp.

std::string dev::p2p::Host::enode ( ) const
inline

Get the public endpoint information.

Definition at line 239 of file Host.h.

Here is the call graph for this function:

Peers dev::p2p::Host::getPeers ( ) const
inline

Definition at line 204 of file Host.h.

Here is the caller graph for this function:

bool dev::p2p::Host::haveCapability ( CapDesc const &  _name) const
inline

Definition at line 163 of file Host.h.

bool dev::p2p::Host::haveNetwork ( ) const
inline
Returns
if network is started and interactive.

Definition at line 224 of file Host.h.

Here is the caller graph for this function:

bool dev::p2p::Host::havePeerSession ( NodeID const &  _id)
inlineprivate

Definition at line 255 of file Host.h.

NodeID dev::p2p::Host::id ( ) const
inline

Get our current node ID.

Definition at line 233 of file Host.h.

Here is the caller graph for this function:

bool dev::p2p::Host::isStarted ( ) const
inline
Returns
if network has been started.

Definition at line 218 of file Host.h.

Here is the caller graph for this function:

void Host::keepAlivePeers ( )
private

Ping the peers to update the latency information and disconnect peers which have timed out.

Definition at line 780 of file Host.cpp.

std::string dev::p2p::Host::listenAddress ( ) const
inline

Get the address we're listening on currently.

Definition at line 195 of file Host.h.

unsigned short dev::p2p::Host::listenPort ( ) const
inline

Get the port we're listening on currently.

Definition at line 198 of file Host.h.

Here is the call graph for this function:

Here is the caller graph for this function:

KeyPair Host::networkAlias ( bytesConstRef  _b)
staticprivate

Get or create host identifier (KeyPair).

Definition at line 944 of file Host.cpp.

Here is the call graph for this function:

NetworkPreferences const& dev::p2p::Host::networkPreferences ( ) const
inline

Definition at line 206 of file Host.h.

Here is the caller graph for this function:

p2p::NodeInfo dev::p2p::Host::nodeInfo ( ) const
inline

Get the node information.

Definition at line 242 of file Host.h.

void Host::onNodeTableEvent ( NodeID const &  _n,
NodeTableEventType const &  _e 
)
protected

Definition at line 346 of file Host.cpp.

Here is the caller graph for this function:

size_t Host::peerCount ( ) const

Get number of peers connected.

Definition at line 639 of file Host.cpp.

Here is the caller graph for this function:

std::shared_ptr<SessionFace> dev::p2p::Host::peerSession ( NodeID const &  _id)
inline

Get session by id.

Definition at line 230 of file Host.h.

Here is the caller graph for this function:

PeerSessionInfos Host::peerSessionInfo ( ) const

Get peer information.

Definition at line 625 of file Host.cpp.

Here is the caller graph for this function:

unsigned dev::p2p::Host::peerSlots ( PeerSlotType  _type)
inlineprivate

Definition at line 253 of file Host.h.

bool dev::p2p::Host::peerSlotsAvailable ( PeerSlotType  _type = Ingress)
inlineprivate

Returns true if pending and connected peer count is less than maximum.

Definition at line 263 of file Host.h.

Here is the call graph for this function:

std::unordered_map< Public, std::string > Host::pocHosts ( )
static

Default hosts for current version of client.

Definition at line 480 of file Host.cpp.

template<class T >
std::shared_ptr<T> dev::p2p::Host::registerCapability ( std::shared_ptr< T > const &  _t)
inline

Register a peer-capability; all new peer connections will have this capability.

Definition at line 160 of file Host.h.

Here is the caller graph for this function:

void Host::relinquishPeer ( NodeID const &  _node)

Note peer as no longer being required.

Definition at line 562 of file Host.cpp.

Here is the caller graph for this function:

ReputationManager& dev::p2p::Host::repMan ( )
inline
Returns
our reputation manager.

Definition at line 221 of file Host.h.

Here is the caller graph for this function:

void Host::requirePeer ( NodeID const &  _node,
NodeIPEndpoint const &  _endpoint 
)

Create Peer and attempt keeping peer connected.

Definition at line 517 of file Host.cpp.

Here is the caller graph for this function:

void dev::p2p::Host::requirePeer ( NodeID const &  _node,
bi::address const &  _addr,
unsigned short  _udpPort,
unsigned short  _tcpPort 
)
inline

Create Peer and attempt keeping peer connected.

Definition at line 177 of file Host.h.

Here is the call graph for this function:

Here is the caller graph for this function:

void Host::restoreNetwork ( bytesConstRef  _b)
protected

Deserialise the data and populate the set of known peers.

Definition at line 865 of file Host.cpp.

Here is the call graph for this function:

void Host::run ( boost::system::error_code const &  error)
private

Called by startedWorking. Not thread-safe; to be called only be Worker.

Run network. Called serially via ASIO deadline timer. Manages connection state transitions.

Definition at line 650 of file Host.cpp.

Here is the call graph for this function:

void Host::runAcceptor ( )
private

Called only from startedWorking().

Definition at line 428 of file Host.cpp.

Here is the call graph for this function:

bytes Host::saveNetwork ( ) const

Serialise the set of known peers.

Definition at line 811 of file Host.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void dev::p2p::Host::setIdealPeerCount ( unsigned  _n)
inline

Set ideal number of peers.

Definition at line 183 of file Host.h.

Here is the caller graph for this function:

void dev::p2p::Host::setNetworkPreferences ( NetworkPreferences const &  _p,
bool  _dropPeers = false 
)
inline

Definition at line 208 of file Host.h.

Here is the call graph for this function:

Here is the caller graph for this function:

void dev::p2p::Host::setPeerStretch ( unsigned  _n)
inline

Set multipier for max accepted connections.

Definition at line 186 of file Host.h.

Here is the caller graph for this function:

void Host::start ( )

Start network. .

Definition at line 124 of file Host.cpp.

Here is the caller graph for this function:

void Host::startedWorking ( )
privatevirtual

Called by Worker. Not thread-safe; to be called only by worker.

Reimplemented from dev::Worker.

Definition at line 722 of file Host.cpp.

Here is the call graph for this function:

void Host::startPeerSession ( Public const &  _id,
RLP const &  _hello,
std::unique_ptr< RLPXFrameCoder > &&  _io,
std::shared_ptr< RLPXSocket > const &  _s 
)

Validates and starts peer session, taking ownership of _io. Disconnects and returns false upon error.

Definition at line 233 of file Host.cpp.

Here is the call graph for this function:

void Host::stop ( )

Stop network.

Resets acceptor, socket, and IO service. Called by deallocator.

Definition at line 139 of file Host.cpp.

Here is the caller graph for this function:

bi::tcp::endpoint const& dev::p2p::Host::tcpPublic ( ) const
inline

Get the public TCP endpoint.

Definition at line 236 of file Host.h.

Friends And Related Function Documentation

friend class HostCapabilityFace
friend

Definition at line 135 of file Host.h.

friend class HostNodeTableHandler
friend

Definition at line 131 of file Host.h.

friend class RLPXHandshake
friend

Definition at line 132 of file Host.h.

friend class Session
friend

Definition at line 134 of file Host.h.

Member Data Documentation

const unsigned dev::p2p::Host::c_timerInterval = 100
staticprivate

Interval which m_timer is run when network is connected.

Definition at line 306 of file Host.h.

bool dev::p2p::Host::m_accepting = false
private

Definition at line 340 of file Host.h.

KeyPair dev::p2p::Host::m_alias
private

Alias for network communication. Network address is k*G. k is key material. TODO: Replace KeyPair.

Definition at line 312 of file Host.h.

std::map<CapDesc, std::shared_ptr<HostCapabilityFace> > dev::p2p::Host::m_capabilities
private

Each of the capabilities we support.

Definition at line 333 of file Host.h.

std::string dev::p2p::Host::m_clientVersion
private

Our version string.

Definition at line 293 of file Host.h.

std::list<std::weak_ptr<RLPXHandshake> > dev::p2p::Host::m_connecting
private

Pending connections.

Definition at line 327 of file Host.h.

bool dev::p2p::Host::m_dropPeers = false
private

Definition at line 341 of file Host.h.

unsigned dev::p2p::Host::m_idealPeerCount = 11
private

Ideal number of peers to be connected to.

Definition at line 330 of file Host.h.

std::set<bi::address> dev::p2p::Host::m_ifAddresses
private

Interface addresses (private, public)

Interface addresses.

Definition at line 298 of file Host.h.

ba::io_service dev::p2p::Host::m_ioService
private

IOService for network stuff.

Definition at line 302 of file Host.h.

std::chrono::steady_clock::time_point dev::p2p::Host::m_lastPing
private

Time we sent the last ping to all peers.

Definition at line 339 of file Host.h.

int dev::p2p::Host::m_listenPort = -1
private

What port are we listening on. -1 means binding failed or acceptor hasn't been initialized.

Definition at line 300 of file Host.h.

NetworkPreferences dev::p2p::Host::m_netPrefs
private

Network settings.

Definition at line 295 of file Host.h.

std::shared_ptr<NodeTable> dev::p2p::Host::m_nodeTable
private

Node table (uses kademlia-like discovery).

Definition at line 313 of file Host.h.

std::unordered_map<NodeID, std::shared_ptr<Peer> > dev::p2p::Host::m_peers
private

Shared storage of Peer objects. Peers are created or destroyed on demand by the Host. Active sessions maintain a shared_ptr to a Peer;.

Definition at line 316 of file Host.h.

std::set<Peer*> dev::p2p::Host::m_pendingPeerConns
private

Definition at line 308 of file Host.h.

ReputationManager dev::p2p::Host::m_repMan
private

Definition at line 343 of file Host.h.

std::set<NodeID> dev::p2p::Host::m_requiredPeers
private

Peers we try to connect regardless of p2p network.

Definition at line 319 of file Host.h.

bytes dev::p2p::Host::m_restoreNetwork
private

Set by constructor and used to set Host key and restore network peers & nodes.

Definition at line 288 of file Host.h.

bool dev::p2p::Host::m_run = false
private

Whether network is running.

Definition at line 290 of file Host.h.

std::unordered_map<NodeID, std::weak_ptr<SessionFace> > dev::p2p::Host::m_sessions
mutableprivate

The nodes to which we are currently connected.

Used by host to service peer requests and keepAlivePeers and for shutdown. (see run()) Mutable because we flush zombie entries (null-weakptrs) as regular maintenance from a const method.

Definition at line 324 of file Host.h.

unsigned dev::p2p::Host::m_stretchPeers = 7
private

Accepted connection multiplier (max peers = ideal*stretch).

Definition at line 331 of file Host.h.

bi::tcp::acceptor dev::p2p::Host::m_tcp4Acceptor
private

Listening acceptor.

Definition at line 303 of file Host.h.

bi::tcp::endpoint dev::p2p::Host::m_tcpPublic
private

Our public listening endpoint.

Definition at line 311 of file Host.h.

std::unique_ptr<boost::asio::deadline_timer> dev::p2p::Host::m_timer
private

Timer which, when network is running, calls scheduler() every c_timerInterval ms.

Definition at line 305 of file Host.h.

std::list<std::shared_ptr<boost::asio::deadline_timer> > dev::p2p::Host::m_timers
private

Deadline timers used for isolated network events. GC'd by run.

Definition at line 336 of file Host.h.

Mutex dev::p2p::Host::x_connecting
private

Mutex for m_connecting.

Definition at line 328 of file Host.h.

Mutex dev::p2p::Host::x_pendingNodeConns
private

Used only by connect(Peer&) to limit concurrently connecting to same node. See connect(shared_ptr<Peer>const&).

Definition at line 309 of file Host.h.

Mutex dev::p2p::Host::x_requiredPeers
private

Definition at line 320 of file Host.h.

std::mutex dev::p2p::Host::x_runTimer
mutableprivate

Start/stop mutex.

Definition at line 291 of file Host.h.

RecursiveMutex dev::p2p::Host::x_sessions
mutableprivate

Definition at line 325 of file Host.h.

Mutex dev::p2p::Host::x_timers
private

Definition at line 337 of file Host.h.


The documentation for this class was generated from the following files: