Skip to content

Commit

Permalink
Merge pull request #1176 from majestrate/randomize-hop-selection-more…
Browse files Browse the repository at this point in the history
…-2020-03-10

fully randomize hop selection
  • Loading branch information
majestrate committed Mar 11, 2020
2 parents bc85082 + 9cdc7f4 commit c6fd007
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 55 deletions.
56 changes: 5 additions & 51 deletions llarp/nodedb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -525,51 +525,6 @@ llarp_nodedb::select_random_exit(llarp::RouterContact &result)
return false;
}

bool
llarp_nodedb::select_random_hop(const llarp::RouterContact &prev,
llarp::RouterContact &result, size_t N)
{
llarp::util::Lock lock(access);
/// checking for "guard" status for N = 0 is done by caller inside of
/// pathbuilder's scope
size_t sz = entries.size();
if(sz < 3)
return false;
if(!N)
return false;
llarp_time_t now = llarp::time_now_ms();

auto itr = entries.begin();
size_t pos = llarp::randint() % sz;
std::advance(itr, pos);
auto start = itr;
while(itr == entries.end())
{
if(prev.pubkey != itr->second.rc.pubkey)
{
if(itr->second.rc.addrs.size() && !itr->second.rc.IsExpired(now))
{
result = itr->second.rc;
return true;
}
}
itr++;
}
itr = entries.begin();
while(itr != start)
{
if(prev.pubkey != itr->second.rc.pubkey)
{
if(itr->second.rc.addrs.size() && !itr->second.rc.IsExpired(now))
{
result = itr->second.rc;
return true;
}
}
++itr;
}
return false;
}

bool
llarp_nodedb::select_random_hop_excluding(
Expand All @@ -583,17 +538,16 @@ llarp_nodedb::select_random_hop_excluding(
{
return false;
}
llarp_time_t now = llarp::time_now_ms();

auto itr = entries.begin();
size_t pos = llarp::randint() % sz;
auto itr = entries.begin();
const size_t pos = llarp::randint() % sz;
std::advance(itr, pos);
auto start = itr;
const auto start = itr;
while(itr == entries.end())
{
if(exclude.count(itr->first) == 0)
{
if(itr->second.rc.addrs.size() && !itr->second.rc.IsExpired(now))
if(itr->second.rc.IsPublicRouter())
{
result = itr->second.rc;
return true;
Expand All @@ -606,7 +560,7 @@ llarp_nodedb::select_random_hop_excluding(
{
if(exclude.count(itr->first) == 0)
{
if(itr->second.rc.addrs.size() && !itr->second.rc.IsExpired(now))
if(itr->second.rc.IsPublicRouter())
{
result = itr->second.rc;
return true;
Expand Down
4 changes: 0 additions & 4 deletions llarp/nodedb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,6 @@ struct llarp_nodedb
bool
select_random_exit(llarp::RouterContact &rc) EXCLUDES(access);

bool
select_random_hop(const llarp::RouterContact &prev,
llarp::RouterContact &result, size_t N) EXCLUDES(access);

bool
select_random_hop_excluding(llarp::RouterContact &result,
const std::set< llarp::RouterID > &exclude)
Expand Down

0 comments on commit c6fd007

Please sign in to comment.