Skip to content

Commit

Permalink
make test_dht deterministic
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidn committed Aug 1, 2019
1 parent a869af0 commit bd0d011
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
35 changes: 17 additions & 18 deletions simulation/setup_dht.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ namespace {
// this is the node ID assigned to node 'idx'
dht::node_id id_from_addr(lt::address const& addr)
{
return dht::generate_id(addr);
return dht::generate_id_impl(addr, 0);
}

std::shared_ptr<lt::aux::listen_socket_t> sim_listen_socket(tcp::endpoint ep)
Expand Down Expand Up @@ -174,7 +174,7 @@ struct dht_node final : lt::dht::socket_manager
// expensive. instead. pick a random subset of nodes proportionate to the
// bucket it would fall into

dht::node_id id = dht().nid();
dht::node_id const id = dht().nid();

// the number of slots left per bucket
std::array<int, 160> nodes_per_bucket;
Expand All @@ -186,8 +186,18 @@ struct dht_node final : lt::dht::socket_manager
nodes_per_bucket[2] = 32;
nodes_per_bucket[3] = 16;

for (auto const& n : nodes)
// pick nodes in random order to provide good connectivity
std::vector<std::size_t> order(nodes.size());
for (size_t i = 0; i < order.size(); ++i) order[i] = i;

while (!order.empty())
{
auto const idx = lt::random(static_cast<uint32_t>(order.size() - 1));
assert(idx >= 0 && idx < order.size());
auto const& n = nodes[order[idx]];
if (idx < order.size() - 1) order[idx] = order.back();
order.pop_back();

if (n.first == id) continue;
int const bucket = 159 - dht::distance_exp(id, n.first);

Expand All @@ -206,10 +216,10 @@ struct dht_node final : lt::dht::socket_manager
{
// generate a random node ID that would fall in `bucket`
dht::node_id const mask = dht::generate_prefix_mask(bucket + 1);
dht::node_id target = dht::generate_random_id() & ~mask;
udp::endpoint const ep = rand_udp_ep(m_ipv6 ? rand_v6 : rand_v4);
dht::node_id target = dht::generate_id_impl(ep.address(), 0) & ~mask;
target |= id & mask;
dht().m_table.node_seen(target, rand_udp_ep(m_ipv6 ? rand_v6 : rand_v4)
, lt::random(300) + 10);
dht().m_table.node_seen(target, ep, lt::random(300) + 10);
}
}
/*
Expand Down Expand Up @@ -260,18 +270,7 @@ dht_network::dht_network(sim::simulation& sim, int num_nodes, std::uint32_t flag
all_nodes.push_back(m_nodes.back().node_info());
}

int cnt = 0;
for (auto& n : m_nodes)
{
n.bootstrap(all_nodes);
if (++cnt == 25)
{
// every now and then, shuffle all_nodes to make the
// routing tables more randomly distributed
lt::aux::random_shuffle(all_nodes);
cnt = 0;
}
}
for (auto& n : m_nodes) n.bootstrap(all_nodes);
}

dht_network::~dht_network() = default;
Expand Down
2 changes: 2 additions & 0 deletions simulation/test_dht.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ void bootstrap_session(std::vector<dht_network*> networks, lt::session& ses)
{
lt::dht::dht_settings sett;
sett.ignore_dark_internet = false;
sett.restrict_routing_ips = false;
ses.set_dht_settings(sett);

lt::entry state;
Expand Down Expand Up @@ -89,6 +90,7 @@ void bootstrap_session(std::vector<dht_network*> networks, lt::session& ses)
ses.load_state(e);
lt::settings_pack pack;
pack.set_bool(lt::settings_pack::enable_dht, true);
pack.set_int(lt::settings_pack::alert_mask, lt::alert::all_categories);
ses.apply_settings(pack);
}
#endif // TORRENT_DISABLE_DHT
Expand Down
4 changes: 2 additions & 2 deletions test/test_dht.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1656,15 +1656,15 @@ void test_routing_table(address(&rand_addr)())

{
auto const ep = rand_udp_ep(rand_addr);
auto const id = generate_id(ep.address());
auto const id = generate_id_impl(ep.address(), 2);
table.node_seen(id, ep, 10);
}

nodes.clear();
for (int i = 0; i < 10000; ++i)
{
auto const ep = rand_udp_ep(rand_addr);
auto const id = generate_id(ep.address());
auto const id = generate_id_impl(ep.address(), 6);
table.node_seen(id, ep, 20 + (id[19] & 0xff));
}
std::printf("active buckets: %d\n", table.num_active_buckets());
Expand Down

0 comments on commit bd0d011

Please sign in to comment.