TARAXA
dev::p2p::Host Struct Referencefinal

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

#include <Host.h>

Collaboration diagram for dev::p2p::Host:

Classes

struct  KnownNode
 
struct  PersistentState
 Deserialise the data and populate the set of known peers. More...
 

Public Types

using CapabilityList = std::vector< std::shared_ptr< CapabilityFace > >
 
using CapabilitiesFactory = std::function< CapabilityList(std::weak_ptr< Host >)>
 

Public Member Functions

Capabilities getSupportedCapabilities ()
 
 ~Host ()
 
ba::io_context::count_type do_work ()
 
ba::io_context::count_type do_discov ()
 
bool isRunning ()
 
uint64_t peer_count () const
 
unsigned short listenPort () const
 Get the port we're listening on currently. More...
 
NodeID id () const
 Get our current node ID. More...
 
auto getNodeCount () const
 
std::list< NodeEntrygetNodes () const
 
void addNode (Node const &_node)
 
void invalidateNode (NodeID const &_node)
 
void disconnect (NodeID const &_nodeID, DisconnectReason _reason)
 
void send (NodeID const &node_id, std::string capability_name, unsigned packet_type, bytes payload, std::function< void()> &&on_done={})
 
std::string enode () const
 Get the endpoint information. More...
 
bool nodeTableHasNode (Public const &_id) const
 

Static Public Member Functions

static std::shared_ptr< Hostmake (std::string _clientVersion, CapabilitiesFactory const &cap_factory, KeyPair const &kp, NetworkConfig _n, TaraxaNetworkConfig taraxa_conf={}, std::filesystem::path state_file_path={})
 

Private Types

enum  PeerSlotType { Egress , Ingress }
 

Private Member Functions

 Host (std::string _clientVersion, KeyPair const &kp, NetworkConfig _n, TaraxaNetworkConfig taraxa_conf, std::filesystem::path state_file_path)
 
Node nodeFromNodeTable (Public const &_id) const
 
bool addKnownNodeToNodeTable (KnownNode const &node)
 
bool haveCapability (CapDesc const &_name) const
 
std::string listenAddress () const
 Get the address we're listening on currently. More...
 
NetworkConfig const & networkConfig () const
 
bi::tcp::endpoint const & tcpPublic () const
 Get the public TCP endpoint. More...
 
p2p::NodeInfo nodeInfo () const
 Get the node information. More...
 
ENR enr () const
 Get Ethereum Node Record of the host. More...
 
size_t peer_count_ () const
 Get number of peers connected. More...
 
PeerSessionInfos peerSessionInfos () const
 Get peer information. More...
 
std::shared_ptr< SessionpeerSession (NodeID const &_id) const
 Get session by id. More...
 
void startPeerSession (Public const &_id, RLP const &_hello, std::unique_ptr< RLPXFrameCoder > _io, std::shared_ptr< RLPXSocket > const &_s)
 
void onNodeTableEvent (NodeID const &_n, NodeTableEventType const &_e)
 
void save_state () const
 Serialise the set of known peers. More...
 
std::optional< PersistentStaterestore_state ()
 
unsigned peerSlots (PeerSlotType _type) const
 
bool havePeerSession (NodeID const &_id)
 
bool isHandshaking (NodeID const &_id) const
 
bi::tcp::endpoint determinePublic () const
 Determines publicly advertised address. More...
 
ENR updateENR (ENR const &_restoredENR, bi::tcp::endpoint const &_tcpPublic, uint16_t const &_listenPort)
 
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 ()
 
void logActivePeers ()
 Log count of active peers and information about each peer. More...
 
void runAcceptor ()
 
void main_loop_body ()
 
std::pair< Secret, ENRrestoreENR (bytesConstRef _b, NetworkConfig const &_networkConfig)
 Get or create host's Ethereum Node record. More...
 
bool isAllowedEndpoint (NodeIPEndpoint const &_endpointToCheck) const
 
std::shared_ptr< Peerpeer (NodeID const &_n) const
 

Private Attributes

std::atomic< bool > fully_initialized_ = false
 
ba::io_context ioc_
 
ba::executor_work_guard< ba::io_context::executor_type > ioc_w_
 
ba::io_context session_ioc_
 
ba::executor_work_guard< ba::io_context::executor_type > session_ioc_w_
 
ba::io_context::strand strand_
 
bi::tcp::acceptor m_tcp4Acceptor
 
ba::steady_timer m_runTimer
 
std::filesystem::path state_file_path_
 
std::string m_clientVersion
 Our version string. More...
 
NetworkConfig m_netConfig
 Network settings. More...
 
TaraxaNetworkConfig taraxa_conf_
 
unsigned m_idealPeerCount = 0
 Ideal number of peers to be connected to. More...
 
unsigned m_stretchPeers = 0
 
Capabilities m_capabilities
 Each of the capabilities we support. More...
 
unsigned m_listenPort
 
bi::tcp::endpoint m_tcpPublic
 
KeyPair m_alias
 Alias for network communication. More...
 
std::shared_ptr< RLPXHandshake::HostContexthandshake_ctx_
 
std::set< Peer * > m_pendingPeerConns
 
std::unordered_map< NodeID, std::shared_ptr< Peer > > m_peers
 
std::unordered_map< NodeID, std::weak_ptr< Session > > m_sessions
 
std::list< std::weak_ptr< RLPXHandshake > > m_connecting
 
std::chrono::steady_clock::time_point m_lastPing
 Time we sent the last ping to all peers. More...
 
std::chrono::steady_clock::time_point m_lastPeerLogMessage
 
std::unique_ptr< NodeTablem_nodeTable
 Node table (uses kademlia-like discovery). More...
 
std::atomic< uint64_t > peer_count_snapshot_ = 0
 
Logger m_logger {createLogger(VerbosityDebug, "net")}
 
Logger m_detailsLogger {createLogger(VerbosityTrace, "net")}
 
Logger m_infoLogger {createLogger(VerbosityInfo, "net")}
 

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


Class Documentation

◆ dev::p2p::Host::KnownNode

struct dev::p2p::Host::KnownNode
Collaboration diagram for dev::p2p::Host::KnownNode:
Class Members
uint32_t lastPongReceivedTime
uint32_t lastPongSentTime
Node node

◆ dev::p2p::Host::PersistentState

struct dev::p2p::Host::PersistentState

Deserialise the data and populate the set of known peers.

Collaboration diagram for dev::p2p::Host::PersistentState:
Class Members
ENR enr
vector< KnownNode > known_nodes
vector< shared_ptr< Peer > > peers

Member Typedef Documentation

◆ CapabilitiesFactory

using dev::p2p::Host::CapabilitiesFactory = std::function<CapabilityList(std::weak_ptr<Host>)>

◆ CapabilityList

using dev::p2p::Host::CapabilityList = std::vector<std::shared_ptr<CapabilityFace> >

Member Enumeration Documentation

◆ PeerSlotType

Enumerator
Egress 
Ingress 

Constructor & Destructor Documentation

◆ Host()

Host::Host ( std::string  _clientVersion,
KeyPair const &  kp,
NetworkConfig  _n,
TaraxaNetworkConfig  taraxa_conf,
std::filesystem::path  state_file_path 
)
private

!! this needs to be post to session_ioc_ as main_loop_body handles peer/session related stuff

◆ ~Host()

Host::~Host ( )

Member Function Documentation

◆ addKnownNodeToNodeTable()

bool Host::addKnownNodeToNodeTable ( KnownNode const &  node)
private

◆ addNode()

void Host::addNode ( Node const &  _node)

◆ connect()

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

◆ determinePublic()

bi::tcp::endpoint Host::determinePublic ( ) const
private

Determines publicly advertised address.

◆ disconnect()

void dev::p2p::Host::disconnect ( NodeID const &  _nodeID,
DisconnectReason  _reason 
)
inline

◆ do_discov()

ba::io_context::count_type Host::do_discov ( )

◆ do_work()

ba::io_context::count_type Host::do_work ( )

◆ enode()

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

Get the endpoint information.

◆ enr()

ENR dev::p2p::Host::enr ( ) const
inlineprivate

Get Ethereum Node Record of the host.

◆ getNodeCount()

auto dev::p2p::Host::getNodeCount ( ) const
inline

◆ getNodes()

std::list<NodeEntry> dev::p2p::Host::getNodes ( ) const
inline

◆ getSupportedCapabilities()

Capabilities dev::p2p::Host::getSupportedCapabilities ( )
inline

◆ haveCapability()

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

◆ havePeerSession()

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

◆ id()

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

Get our current node ID.

◆ invalidateNode()

void Host::invalidateNode ( NodeID const &  _node)

◆ isAllowedEndpoint()

bool dev::p2p::Host::isAllowedEndpoint ( NodeIPEndpoint const &  _endpointToCheck) const
inlineprivate

Determines if a node with the supplied endpoint should be included in or restored from the serialized network configuration data

◆ isHandshaking()

bool Host::isHandshaking ( NodeID const &  _id) const
private

◆ isRunning()

bool dev::p2p::Host::isRunning ( )
inline

◆ keepAlivePeers()

void Host::keepAlivePeers ( )
private

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

◆ listenAddress()

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

Get the address we're listening on currently.

◆ listenPort()

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

Get the port we're listening on currently.

◆ logActivePeers()

void Host::logActivePeers ( )
private

Log count of active peers and information about each peer.

◆ main_loop_body()

void Host::main_loop_body ( )
private

◆ make()

std::shared_ptr< Host > Host::make ( std::string  _clientVersion,
CapabilitiesFactory const &  cap_factory,
KeyPair const &  kp,
NetworkConfig  _n,
TaraxaNetworkConfig  taraxa_conf = {},
std::filesystem::path  state_file_path = {} 
)
static

◆ networkConfig()

NetworkConfig const& dev::p2p::Host::networkConfig ( ) const
inlineprivate

◆ nodeFromNodeTable()

Node Host::nodeFromNodeTable ( Public const &  _id) const
private

◆ nodeInfo()

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

Get the node information.

◆ nodeTableHasNode()

bool Host::nodeTableHasNode ( Public const &  _id) const

◆ onNodeTableEvent()

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

◆ peer()

std::shared_ptr< Peer > Host::peer ( NodeID const &  _n) const
private

◆ peer_count()

uint64_t dev::p2p::Host::peer_count ( ) const
inline

◆ peer_count_()

size_t Host::peer_count_ ( ) const
private

Get number of peers connected.

◆ peerSession()

shared_ptr< Session > Host::peerSession ( NodeID const &  _id) const
private

Get session by id.

◆ peerSessionInfos()

PeerSessionInfos Host::peerSessionInfos ( ) const
private

Get peer information.

◆ peerSlots()

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

◆ peerSlotsAvailable()

bool Host::peerSlotsAvailable ( Host::PeerSlotType  _type = Ingress)
private

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

◆ restore_state()

std::optional< Host::PersistentState > Host::restore_state ( )
private

◆ restoreENR()

std::pair< Secret, ENR > Host::restoreENR ( bytesConstRef  _b,
NetworkConfig const &  _networkConfig 
)
private

Get or create host's Ethereum Node record.

◆ runAcceptor()

void Host::runAcceptor ( )
private

◆ save_state()

void Host::save_state ( ) const
private

Serialise the set of known peers.

◆ send()

void dev::p2p::Host::send ( NodeID const &  node_id,
std::string  capability_name,
unsigned  packet_type,
bytes  payload,
std::function< void()> &&  on_done = {} 
)
inline

◆ startPeerSession()

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

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

◆ tcpPublic()

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

Get the public TCP endpoint.

◆ updateENR()

ENR Host::updateENR ( ENR const &  _restoredENR,
bi::tcp::endpoint const &  _tcpPublic,
uint16_t const &  _listenPort 
)
private

Member Data Documentation

◆ fully_initialized_

std::atomic<bool> dev::p2p::Host::fully_initialized_ = false
private

◆ handshake_ctx_

std::shared_ptr<RLPXHandshake::HostContext> dev::p2p::Host::handshake_ctx_
private

◆ ioc_

ba::io_context dev::p2p::Host::ioc_
private

◆ ioc_w_

ba::executor_work_guard<ba::io_context::executor_type> dev::p2p::Host::ioc_w_
private

◆ m_alias

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

Alias for network communication.

◆ m_capabilities

Capabilities dev::p2p::Host::m_capabilities
private

Each of the capabilities we support.

◆ m_clientVersion

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

Our version string.

◆ m_connecting

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

Pending connections. Completed handshakes are garbage-collected in run() (a handshake is complete when there are no more shared_ptrs in handlers)

◆ m_detailsLogger

Logger dev::p2p::Host::m_detailsLogger {createLogger(VerbosityTrace, "net")}
private

◆ m_idealPeerCount

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

Ideal number of peers to be connected to.

◆ m_infoLogger

Logger dev::p2p::Host::m_infoLogger {createLogger(VerbosityInfo, "net")}
private

◆ m_lastPeerLogMessage

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

When the last "active peers" message was logged - used to throttle logging to once every c_logActivePeersInterval seconds

◆ m_lastPing

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

Time we sent the last ping to all peers.

◆ m_listenPort

unsigned dev::p2p::Host::m_listenPort
private

◆ m_logger

Logger dev::p2p::Host::m_logger {createLogger(VerbosityDebug, "net")}
mutableprivate

◆ m_netConfig

NetworkConfig dev::p2p::Host::m_netConfig
private

Network settings.

◆ m_nodeTable

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

Node table (uses kademlia-like discovery).

◆ m_peers

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

Used only by connect(Peer&) to limit concurrently connecting to same node. See connect(shared_ptr<Peer>const&). Shared storage of Peer objects. Peers are created or destroyed on demand by the Host. Active sessions maintain a shared_ptr to a Peer;

◆ m_pendingPeerConns

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

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.

◆ m_runTimer

ba::steady_timer dev::p2p::Host::m_runTimer
private

◆ m_sessions

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

◆ m_stretchPeers

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

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

◆ m_tcp4Acceptor

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

◆ m_tcpPublic

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

Our public listening endpoint.

◆ peer_count_snapshot_

std::atomic<uint64_t> dev::p2p::Host::peer_count_snapshot_ = 0
private

◆ session_ioc_

ba::io_context dev::p2p::Host::session_ioc_
private

◆ session_ioc_w_

ba::executor_work_guard<ba::io_context::executor_type> dev::p2p::Host::session_ioc_w_
private

◆ state_file_path_

std::filesystem::path dev::p2p::Host::state_file_path_
private

◆ strand_

ba::io_context::strand dev::p2p::Host::strand_
private

Listening acceptor. Timer which, when network is running, calls run() every c_timerInterval ms.

◆ taraxa_conf_

TaraxaNetworkConfig dev::p2p::Host::taraxa_conf_
private

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