Browse Source

More peer/node cleanup.

cl-refactor
subtly 10 years ago
parent
commit
acffe3687d
  1. 46
      libp2p/Host.cpp
  2. 2
      libp2p/Peer.cpp

46
libp2p/Host.cpp

@ -174,29 +174,25 @@ void Host::doneWorking()
void Host::startPeerSession(Public const& _id, RLP const& _rlp, RLPXFrameIO* _io, bi::tcp::endpoint _endpoint) void Host::startPeerSession(Public const& _id, RLP const& _rlp, RLPXFrameIO* _io, bi::tcp::endpoint _endpoint)
{ {
// session maybe ingress or egress so m_peers and node table entries may not exist
shared_ptr<Peer> p; shared_ptr<Peer> p;
ETH_RECURSIVE_GUARDED(x_sessions)
{ {
RecursiveGuard l(x_sessions);
if (m_peers.count(_id)) if (m_peers.count(_id))
{
p = m_peers[_id]; p = m_peers[_id];
p->endpoint.address = _endpoint.address();
}
else else
{ {
// peer doesn't exist, try to get info from node table // peer doesn't exist, try to get port info from node table
Node n = m_nodeTable->node(_id); if (Node n = m_nodeTable->node(_id))
if (!n) p.reset(new Peer(n));
{ else
n.id = _id; p.reset(new Peer(Node(_id, UnspecifiedNodeIPEndpoint)));
n.endpoint = NodeIPEndpoint(_endpoint.address(), 0, 0); m_peers[_id] = p;
}
p.reset(new Peer(n));
} }
} }
if (p->isOffline()) if (p->isOffline())
p->m_lastConnected = std::chrono::system_clock::now(); p->m_lastConnected = std::chrono::system_clock::now();
p->endpoint.address = _endpoint.address();
auto protocolVersion = _rlp[0].toInt<unsigned>(); auto protocolVersion = _rlp[0].toInt<unsigned>();
auto clientVersion = _rlp[1].toString(); auto clientVersion = _rlp[1].toString();
@ -248,17 +244,15 @@ void Host::startPeerSession(Public const& _id, RLP const& _rlp, RLPXFrameIO* _io
void Host::onNodeTableEvent(NodeId const& _n, NodeTableEventType const& _e) void Host::onNodeTableEvent(NodeId const& _n, NodeTableEventType const& _e)
{ {
if (_e == NodeEntryAdded) if (_e == NodeEntryAdded)
{ {
clog(NetNote) << "p2p.host.nodeTable.events.nodeEntryAdded " << _n; clog(NetNote) << "p2p.host.nodeTable.events.nodeEntryAdded " << _n;
// only add iff node is in node table
auto n = m_nodeTable->node(_n); if (Node n = m_nodeTable->node(_n))
if (n)
{ {
shared_ptr<Peer> p; shared_ptr<Peer> p;
ETH_RECURSIVE_GUARDED(x_sessions)
{ {
RecursiveGuard l(x_sessions);
if (m_peers.count(_n)) if (m_peers.count(_n))
{ {
p = m_peers[_n]; p = m_peers[_n];
@ -268,18 +262,9 @@ void Host::onNodeTableEvent(NodeId const& _n, NodeTableEventType const& _e)
{ {
p.reset(new Peer(n)); p.reset(new Peer(n));
m_peers[_n] = p; m_peers[_n] = p;
clog(NetNote) << "p2p.host.peers.events.peersAdded " << _n << p->endpoint; clog(NetNote) << "p2p.host.peers.events.peerAdded " << _n << p->endpoint;
} }
} }
// TODO: Implement similar to discover. Attempt connecting to nodes
// until ideal peer count is reached; if all nodes are tried,
// repeat. Notably, this is an integrated process such that
// when onNodeTableEvent occurs we should also update +/-
// the list of nodes to be tried. Thus:
// 1) externalize connection attempts
// 2) attempt copies potentialPeer list
// 3) replace this logic w/maintenance of potentialPeers
if (peerCount() < m_idealPeerCount) if (peerCount() < m_idealPeerCount)
connect(p); connect(p);
} }
@ -287,7 +272,6 @@ void Host::onNodeTableEvent(NodeId const& _n, NodeTableEventType const& _e)
else if (_e == NodeEntryDropped) else if (_e == NodeEntryDropped)
{ {
clog(NetNote) << "p2p.host.nodeTable.events.NodeEntryDropped " << _n; clog(NetNote) << "p2p.host.nodeTable.events.NodeEntryDropped " << _n;
RecursiveGuard l(x_sessions); RecursiveGuard l(x_sessions);
m_peers.erase(_n); m_peers.erase(_n);
} }
@ -411,10 +395,10 @@ void Host::requirePeer(NodeId const& _n, NodeIPEndpoint const& _endpoint)
Node node(_n, _endpoint, true); Node node(_n, _endpoint, true);
if (_n) if (_n)
{ {
// add or replace peer // create or update m_peers entry
shared_ptr<Peer> p; shared_ptr<Peer> p;
ETH_RECURSIVE_GUARDED(x_sessions)
{ {
RecursiveGuard l(x_sessions);
if (m_peers.count(_n)) if (m_peers.count(_n))
{ {
p = m_peers[_n]; p = m_peers[_n];

2
libp2p/Peer.cpp

@ -33,7 +33,7 @@ namespace p2p
bool Peer::shouldReconnect() const bool Peer::shouldReconnect() const
{ {
return chrono::system_clock::now() > m_lastAttempted + chrono::seconds(fallbackSeconds()); return id && endpoint && chrono::system_clock::now() > m_lastAttempted + chrono::seconds(fallbackSeconds());
} }
unsigned Peer::fallbackSeconds() const unsigned Peer::fallbackSeconds() const

Loading…
Cancel
Save