Skip to content

Commit

Permalink
factor out and unit test parts of the DHT routing table logic that ha…
Browse files Browse the repository at this point in the history
…ndles the affinity of nodes to insert. Specifically, make sure the even distribution within routing table buckets works correctly
  • Loading branch information
arvidn committed Jul 30, 2019
1 parent c5d0ea6 commit b1b03a0
Show file tree
Hide file tree
Showing 3 changed files with 406 additions and 220 deletions.
21 changes: 20 additions & 1 deletion include/libtorrent/kademlia/routing_table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ struct ipv6_hash
}
};

struct ip_set
struct TORRENT_EXTRA_EXPORT ip_set
{
void insert(address const& addr);
bool exists(address const& addr) const;
Expand All @@ -97,12 +97,23 @@ struct ip_set
return m_ip4s == rh.m_ip4s && m_ip6s == rh.m_ip6s;
}

std::size_t size() const { return m_ip4s.size() + m_ip6s.size(); }

// these must be multisets because there can be multiple routing table
// entries for a single IP when restrict_routing_ips is set to false
std::unordered_multiset<address_v4::bytes_type, ipv4_hash> m_ip4s;
std::unordered_multiset<address_v6::bytes_type, ipv6_hash> m_ip6s;
};

// Each routing table bucket represents node IDs with a certain number of bits
// of prefix in common with our own node ID. Each bucket fits 8 nodes (and
// sometimes more, closer to the top). In order to minimize the number of hops
// necessary to traverse the DHT, we want the nodes in our buckets to be spread
// out across all possible "sub-branches". This is what the "classify" refers
// to. The 3 (or more) bits following the shared bit prefix.
TORRENT_EXTRA_EXPORT std::uint8_t classify_prefix(int bucket_idx, bool last_bucket
, int bucket_size, node_id nid);

// differences in the implementation from the description in
// the paper:
//
Expand Down Expand Up @@ -308,6 +319,14 @@ class TORRENT_EXTRA_EXPORT routing_table
int const m_bucket_size;
};

TORRENT_EXTRA_EXPORT routing_table::add_node_status_t
replace_node_impl(node_entry const& e, bucket_t& b, ip_set& ips
, int bucket_index, int bucket_size_limit, bool last_bucket
#ifndef TORRENT_DISABLE_LOGGING
, dht_logger* log
#endif
);

} } // namespace libtorrent::dht

#endif // ROUTING_TABLE_HPP
Loading

0 comments on commit b1b03a0

Please sign in to comment.