Skip to content
This repository was archived by the owner on Oct 28, 2021. It is now read-only.

Commit 1b1160b

Browse files
committed
Simplify code in Host::startPeerSession
1 parent bc7187e commit 1b1160b

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

libp2p/Host.cpp

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -238,26 +238,22 @@ bool Host::isRequiredPeer(NodeID const& _id) const
238238
void Host::startPeerSession(Public const& _id, RLP const& _rlp, unique_ptr<RLPXFrameCoder>&& _io, std::shared_ptr<RLPXSocket> const& _s)
239239
{
240240
// session maybe ingress or egress so m_peers and node table entries may not exist
241-
shared_ptr<Peer> p;
241+
shared_ptr<Peer> peer;
242242
DEV_RECURSIVE_GUARDED(x_sessions)
243243
{
244-
if (m_peers.count(_id))
245-
p = m_peers[_id];
244+
auto const itPeer = m_peers.find(_id);
245+
auto const remoteAddress = _s->remoteEndpoint().address();
246+
if (itPeer != m_peers.end() && itPeer->second->endpoint.address() == remoteAddress)
247+
peer = itPeer->second;
246248
else
247249
{
248-
// peer doesn't exist, try to get port info from node table
249-
if (Node n = nodeFromNodeTable(_id))
250-
p = make_shared<Peer>(n);
251-
252-
if (!p)
253-
p = make_shared<Peer>(Node(_id, UnspecifiedNodeIPEndpoint));
254-
255-
m_peers[_id] = p;
250+
// TODO port numbers are not used in Peer, remove them
251+
peer = make_shared<Peer>(Node{_id, NodeIPEndpoint{remoteAddress, 0, 0}});
252+
m_peers[_id] = peer;
256253
}
257254
}
258-
if (p->isOffline())
259-
p->m_lastConnected = std::chrono::system_clock::now();
260-
p->endpoint.setAddress(_s->remoteEndpoint().address());
255+
if (peer->isOffline())
256+
peer->m_lastConnected = std::chrono::system_clock::now();
261257

262258
auto protocolVersion = _rlp[0].toInt<unsigned>();
263259
auto clientVersion = _rlp[1].toString();
@@ -285,8 +281,8 @@ void Host::startPeerSession(Public const& _id, RLP const& _rlp, unique_ptr<RLPXF
285281
<< " " << _id << " " << showbase << capslog.str() << " " << dec << listenPort;
286282

287283
// create session so disconnects are managed
288-
shared_ptr<SessionFace> ps = make_shared<Session>(this, move(_io), _s, p,
289-
PeerSessionInfo({_id, clientVersion, p->endpoint.address().to_string(), listenPort,
284+
shared_ptr<SessionFace> ps = make_shared<Session>(this, move(_io), _s, peer,
285+
PeerSessionInfo({_id, clientVersion, peer->endpoint.address().to_string(), listenPort,
290286
chrono::steady_clock::duration(), _rlp[2].toSet<CapDesc>(), 0, map<string, string>(),
291287
protocolVersion}));
292288
if (protocolVersion < dev::p2p::c_protocolVersion - 1)

0 commit comments

Comments
 (0)