Browse Source

cleanup, sanity checks, and last pass of noting todos.

cl-refactor
subtly 10 years ago
parent
commit
f63e53f735
  1. 2
      libp2p/Host.cpp
  2. 8
      libp2p/Host.h
  3. 10
      libp2p/NodeTable.cpp
  4. 63
      libp2p/Session.cpp
  5. 1
      libp2p/Session.h

2
libp2p/Host.cpp

@ -644,7 +644,7 @@ bytes Host::saveNodes() const
{ {
PeerInfo const& n = *(i.second); PeerInfo const& n = *(i.second);
// TODO: PoC-7: Figure out why it ever shares these ports.//n.address.port() >= 30300 && n.address.port() <= 30305 && // TODO: PoC-7: Figure out why it ever shares these ports.//n.address.port() >= 30300 && n.address.port() <= 30305 &&
if (!n.dead && chrono::system_clock::now() - n.lastConnected < chrono::seconds(3600 * 48) && n.address.port() > 0 && n.address.port() < /*49152*/32768 && n.id != id() && !isPrivateAddress(n.address.address())) if (chrono::system_clock::now() - n.lastConnected < chrono::seconds(3600 * 48) && n.address.port() > 0 && n.address.port() < /*49152*/32768 && n.id != id() && !isPrivateAddress(n.address.address()))
{ {
nodes.appendList(10); nodes.appendList(10);
if (n.address.address().is_v4()) if (n.address.address().is_v4())

8
libp2p/Host.h

@ -54,7 +54,6 @@ class Host;
struct PeerInfo struct PeerInfo
{ {
NodeId id; ///< Their id/public key. NodeId id; ///< Their id/public key.
unsigned index; ///< Index into m_nodesList
// p2p: move to NodeIPEndpoint // p2p: move to NodeIPEndpoint
bi::tcp::endpoint address; ///< As reported from the node itself. bi::tcp::endpoint address; ///< As reported from the node itself.
@ -65,10 +64,6 @@ struct PeerInfo
unsigned failedAttempts = 0; unsigned failedAttempts = 0;
DisconnectReason lastDisconnect = NoDisconnect; ///< Reason for disconnect that happened last. DisconnectReason lastDisconnect = NoDisconnect; ///< Reason for disconnect that happened last.
// p2p: just remove node
// p2p: revisit logic in see Session.cpp#210
bool dead = false; ///< If true, we believe this node is permanently dead - forget all about it.
// p2p: move to protocol-specific map // p2p: move to protocol-specific map
int score = 0; ///< All time cumulative. int score = 0; ///< All time cumulative.
int rating = 0; ///< Trending. int rating = 0; ///< Trending.
@ -125,7 +120,8 @@ class HostNodeTableHandler: public NodeTableEventHandler
* @todo gracefully disconnect peer if peer already connected * @todo gracefully disconnect peer if peer already connected
* @todo determinePublic: ipv6, udp * @todo determinePublic: ipv6, udp
* @todo handle conflict if addNode/requireNode called and Node already exists w/conflicting tcp or udp port * @todo handle conflict if addNode/requireNode called and Node already exists w/conflicting tcp or udp port
* @todo writing host identifier * @todo write host identifier to disk along w/nodes
* @todo move Session::addRating into Host and implement via sender-tagged events
*/ */
class Host: public Worker class Host: public Worker
{ {

10
libp2p/NodeTable.cpp

@ -63,7 +63,15 @@ shared_ptr<NodeEntry> NodeTable::addNode(Node const& _node)
{ {
Guard l(x_nodes); Guard l(x_nodes);
shared_ptr<NodeEntry> ret = m_nodes[_node.id]; shared_ptr<NodeEntry> ret = m_nodes[_node.id];
if (!ret) if (ret)
{
// TODO: p2p robust percolation of node-endpoint changes
// // SECURITY: remove this in beta - it's only for lazy connections and presents an easy attack vector.
// if (m_server->m_peers.count(id) && isPrivateAddress(m_server->m_peers.at(id)->address.address()) && ep.port() != 0)
// // Update address if the node if we now have a public IP for it.
// m_server->m_peers[id]->address = ep;
}
else
{ {
clog(NodeTableNote) << "p2p.nodes.add " << _node.id.abridged(); clog(NodeTableNote) << "p2p.nodes.add " << _node.id.abridged();
if (m_nodeEvents) if (m_nodeEvents)

63
libp2p/Session.cpp

@ -47,9 +47,7 @@ Session::Session(Host* _s, bi::tcp::socket _socket, std::shared_ptr<PeerInfo> co
Session::~Session() Session::~Session()
{ {
// TODO: P2P revisit (refactored from previous logic) m_peer->lastConnected = m_peer->lastAttempted - chrono::seconds(1);
if (m_peer && !(id() && !isPermanentProblem(m_peer->lastDisconnect) && !m_peer->dead))
m_peer->lastConnected = m_peer->lastAttempted - chrono::seconds(1);
// Read-chain finished for one reason or another. // Read-chain finished for one reason or another.
for (auto& i: m_capabilities) for (auto& i: m_capabilities)
@ -136,8 +134,6 @@ void Session::serviceNodesRequest()
s.appendList(3) << bytesConstRef(i.address.address().to_v4().to_bytes().data(), 4) << i.address.port() << i.id; s.appendList(3) << bytesConstRef(i.address.address().to_v4().to_bytes().data(), 4) << i.address.port() << i.id;
else// if (i.second.address().is_v6()) - assumed else// if (i.second.address().is_v6()) - assumed
s.appendList(3) << bytesConstRef(i.address.address().to_v6().to_bytes().data(), 16) << i.address.port() << i.id; s.appendList(3) << bytesConstRef(i.address.address().to_v6().to_bytes().data(), 16) << i.address.port() << i.id;
m_knownNodes.extendAll(i.index);
m_knownNodes.unionWith(i.index);
} }
sealAndSend(s); sealAndSend(s);
m_theyRequestedNodes = false; m_theyRequestedNodes = false;
@ -183,15 +179,6 @@ bool Session::interpret(RLP const& _r)
return true; return true;
} }
// TODO: P2P ensure disabled logic is considered
if (false /* m_server->havePeer(id) */)
{
// Already connected.
clogS(NetWarn) << "Already connected to a peer with id" << id.abridged();
disconnect(DuplicatePeer);
return true;
}
// if peer and connection have id, check for UnexpectedIdentity // if peer and connection have id, check for UnexpectedIdentity
if (!id) if (!id)
{ {
@ -208,16 +195,18 @@ bool Session::interpret(RLP const& _r)
disconnect(UnexpectedIdentity); disconnect(UnexpectedIdentity);
return true; return true;
} }
if (m_server->havePeerSession(id))
{
// Already connected.
clogS(NetWarn) << "Already connected to a peer with id" << id.abridged();
disconnect(DuplicatePeer);
return true;
}
assert(!!m_peer);
assert(!!m_peer->id);
if (m_peer->isOffline()) if (m_peer->isOffline())
m_peer->lastConnected = chrono::system_clock::now(); m_peer->lastConnected = chrono::system_clock::now();
// // TODO: P2P introduce map of nodes we've given to this node (if GetPeers/Peers stays in TCP)
m_knownNodes.extendAll(m_peer->index);
m_knownNodes.unionWith(m_peer->index);
if (m_protocolVersion != m_server->protocolVersion()) if (m_protocolVersion != m_server->protocolVersion())
{ {
disconnect(IncompatibleProtocol); disconnect(IncompatibleProtocol);
@ -262,12 +251,20 @@ bool Session::interpret(RLP const& _r)
break; break;
case GetPeersPacket: case GetPeersPacket:
{ {
// Disabled for interop testing.
// GetPeers/PeersPacket will be modified to only exchange new nodes which it's peers are interested in.
break;
clogS(NetTriviaSummary) << "GetPeers"; clogS(NetTriviaSummary) << "GetPeers";
m_theyRequestedNodes = true; m_theyRequestedNodes = true;
serviceNodesRequest(); serviceNodesRequest();
break; break;
} }
case PeersPacket: case PeersPacket:
// Disabled for interop testing.
// GetPeers/PeersPacket will be modified to only exchange new nodes which it's peers are interested in.
break;
clogS(NetTriviaSummary) << "Peers (" << dec << (_r.itemCount() - 1) << " entries)"; clogS(NetTriviaSummary) << "Peers (" << dec << (_r.itemCount() - 1) << " entries)";
m_weRequestedNodes = false; m_weRequestedNodes = false;
for (unsigned i = 1; i < _r.itemCount(); ++i) for (unsigned i = 1; i < _r.itemCount(); ++i)
@ -304,41 +301,15 @@ bool Session::interpret(RLP const& _r)
if (id == this->id()) if (id == this->id())
goto LAMEPEER; // Just their info - we already have that. goto LAMEPEER; // Just their info - we already have that.
// we don't worry about m_peers.count(id) now because node table will handle this and
// by default we will not blindly connect to nodes received via tcp; instead they will
// be pinged, as-is standard, by the node table and added if appropriate. unless flagged
// as required, nodes aren't connected to unless they respond via discovery; no matter if
// a node is relayed via udp or tcp.
// check that it's not us or one we already know:
// if (m_server->m_peers.count(id))
// {
// /* MEH. Far from an ideal solution. Leave alone for now.
// // Already got this node.
// // See if it's any better that ours or not...
// // This could be the public address of a known node.
// // SECURITY: remove this in beta - it's only for lazy connections and presents an easy attack vector.
// if (m_server->m_peers.count(id) && isPrivateAddress(m_server->m_peers.at(id)->address.address()) && ep.port() != 0)
// // Update address if the node if we now have a public IP for it.
// m_server->m_peers[id]->address = ep;
// */
// goto CONTINUE;
// }
if (!ep.port()) if (!ep.port())
goto LAMEPEER; // Zero port? Don't think so. goto LAMEPEER; // Zero port? Don't think so.
if (ep.port() >= /*49152*/32768) if (ep.port() >= /*49152*/32768)
goto LAMEPEER; // Private port according to IANA. goto LAMEPEER; // Private port according to IANA.
// node table handles another node giving us a node which represents one of our other local network interfaces
// node table handles another node giving us a node we already know about
// OK passed all our checks. Assume it's good. // OK passed all our checks. Assume it's good.
addRating(1000); addRating(1000);
// TODO: P2P test
m_server->addNode(Node(id, NodeIPEndpoint(bi::udp::endpoint(ep.address(), 30303), ep))); m_server->addNode(Node(id, NodeIPEndpoint(bi::udp::endpoint(ep.address(), 30303), ep)));
clogS(NetTriviaDetail) << "New peer: " << ep << "(" << id .abridged()<< ")"; clogS(NetTriviaDetail) << "New peer: " << ep << "(" << id .abridged()<< ")";
CONTINUE:; CONTINUE:;
LAMEPEER:; LAMEPEER:;

1
libp2p/Session.h

@ -121,7 +121,6 @@ private:
std::chrono::steady_clock::time_point m_lastReceived; ///< Time point of last message. std::chrono::steady_clock::time_point m_lastReceived; ///< Time point of last message.
std::map<CapDesc, std::shared_ptr<Capability>> m_capabilities; ///< The peer's capability set. std::map<CapDesc, std::shared_ptr<Capability>> m_capabilities; ///< The peer's capability set.
RangeMask<unsigned> m_knownNodes; ///< Nodes we already know about as indices into Host's nodesList. These shouldn't be resent to peer.
}; };
} }

Loading…
Cancel
Save