Browse Source

code review updates

cl-refactor
subtly 10 years ago
parent
commit
a54e2cee9a
  1. 23
      libp2p/NodeTable.cpp
  2. 10
      libp2p/NodeTable.h

23
libp2p/NodeTable.cpp

@ -90,10 +90,11 @@ void NodeTable::doFindNode(Address _node, unsigned _round, std::shared_ptr<std::
if (_round == s_maxSteps)
{
clog(NodeTableWarn) << "Terminating doFindNode after " << _round << " rounds.";
clog(NodeTableNote) << "Terminating doFindNode after " << _round << " rounds.";
return;
}
else
else if(!_round && !_tried)
// initialized _tried on first round
_tried.reset(new std::set<std::shared_ptr<NodeEntry>>());
auto nearest = findNearest(_node);
@ -110,7 +111,7 @@ void NodeTable::doFindNode(Address _node, unsigned _round, std::shared_ptr<std::
if (tried.empty())
{
clog(NodeTableWarn) << "Terminating doFindNode after " << _round << " rounds.";
clog(NodeTableNote) << "Terminating doFindNode after " << _round << " rounds.";
return;
}
@ -134,7 +135,7 @@ std::vector<std::shared_ptr<NodeTable::NodeEntry>> NodeTable::findNearest(Addres
{
// send s_alpha FindNode packets to nodes we know, closest to target
static unsigned lastBin = s_bins - 1;
unsigned head = dist(m_node.id, _target);
unsigned head = hammingDist(m_node.id, _target);
unsigned tail = head == 0 ? lastBin : (head - 1) % s_bins;
std::map<unsigned, std::list<std::shared_ptr<NodeEntry>>> found;
@ -149,7 +150,7 @@ std::vector<std::shared_ptr<NodeTable::NodeEntry>> NodeTable::findNearest(Addres
if (auto p = n.lock())
{
if (count < s_bucketSize)
found[dist(_target, p->id)].push_back(p);
found[hammingDist(_target, p->id)].push_back(p);
else
break;
}
@ -159,7 +160,7 @@ std::vector<std::shared_ptr<NodeTable::NodeEntry>> NodeTable::findNearest(Addres
if (auto p = n.lock())
{
if (count < s_bucketSize)
found[dist(_target, p->id)].push_back(p);
found[hammingDist(_target, p->id)].push_back(p);
else
break;
}
@ -176,7 +177,7 @@ std::vector<std::shared_ptr<NodeTable::NodeEntry>> NodeTable::findNearest(Addres
if (auto p = n.lock())
{
if (count < s_bucketSize)
found[dist(_target, p->id)].push_back(p);
found[hammingDist(_target, p->id)].push_back(p);
else
break;
}
@ -190,7 +191,7 @@ std::vector<std::shared_ptr<NodeTable::NodeEntry>> NodeTable::findNearest(Addres
if (auto p = n.lock())
{
if (count < s_bucketSize)
found[dist(_target, p->id)].push_back(p);
found[hammingDist(_target, p->id)].push_back(p);
else
break;
}
@ -309,8 +310,8 @@ NodeTable::NodeBucket& NodeTable::bucket(NodeEntry const* _n)
void NodeTable::onReceived(UDPSocketFace*, bi::udp::endpoint const& _from, bytesConstRef _packet)
{
// h256 + Signature + RLP
if (_packet.size() < 100)
// h256 + Signature + RLP (smallest possible packet is empty neighbors packet which is 3 bytes)
if (_packet.size() < h256::size + Signature::size + 3)
{
clog(NodeTableMessageSummary) << "Invalid Message size from " << _from.address().to_string() << ":" << _from.port();
return;
@ -392,7 +393,7 @@ void NodeTable::onReceived(UDPSocketFace*, bi::udp::endpoint const& _from, bytes
}
catch (...)
{
// likely culprit is invalid rlp encoding
clog(NodeTableWarn) << "Exception processing message from " << _from.address().to_string() << ":" << _from.port();
}
}

10
libp2p/NodeTable.h

@ -95,8 +95,8 @@ class NodeTable: UDPSocketEvents, public std::enable_shared_from_this<NodeTable>
*/
struct NodeEntry: public Node
{
NodeEntry(Node _src, Address _id, Public _pubk, NodeDefaultEndpoint _gw): Node(_id, _pubk, _gw), distance(dist(_src.id,_id)) {}
NodeEntry(Node _src, Address _id, Public _pubk, bi::udp::endpoint _udp): Node(_id, _pubk, NodeDefaultEndpoint(_udp)), distance(dist(_src.id,_id)) {}
NodeEntry(Node _src, Address _id, Public _pubk, NodeDefaultEndpoint _gw): Node(_id, _pubk, _gw), distance(hammingDist(_src.id,_id)) {}
NodeEntry(Node _src, Address _id, Public _pubk, bi::udp::endpoint _udp): Node(_id, _pubk, NodeDefaultEndpoint(_udp)), distance(hammingDist(_src.id,_id)) {}
const unsigned distance; ///< Node's distance from _src (see constructor).
};
@ -130,7 +130,7 @@ public:
std::chrono::milliseconds const c_reqTimeout = std::chrono::milliseconds(300); ///< How long to wait for requests (evict, find iterations).
std::chrono::seconds const c_bucketRefresh = std::chrono::seconds(3600); ///< Refresh interval prevents bucket from becoming stale. [Kademlia]
static unsigned dist(Address const& _a, Address const& _b) { u160 d = _a ^ _b; unsigned ret; for (ret = 0; d >>= 1; ++ret) {}; return ret; }
static unsigned hammingDist(Address const& _a, Address const& _b) { u160 d = _a ^ _b; unsigned ret; for (ret = 0; d >>= 1; ++ret) {}; return ret; }
void join();
@ -142,7 +142,7 @@ public:
protected:
/// Repeatedly sends s_alpha concurrent requests to nodes nearest to target, for nodes nearest to target, up to .
/// Repeatedly sends s_alpha concurrent requests to nodes nearest to target, for nodes nearest to target, up to s_maxSteps rounds.
void doFindNode(Address _node, unsigned _round = 0, std::shared_ptr<std::set<std::shared_ptr<NodeEntry>>> _tried = std::shared_ptr<std::set<std::shared_ptr<NodeEntry>>>());
/// Returns nodes nearest to target.
@ -179,7 +179,7 @@ private:
#else
protected:
#endif
/// Sends s_alpha concurrent FindNeighbor requests to nodes closest to target until
/// Sends FindNeighbor packet. See doFindNode.
void requestNeighbors(NodeEntry const& _node, Address _target) const;
Node m_node; ///< This node.

Loading…
Cancel
Save