Merge pull request #1176 from majestrate/randomize-hop-selection-more-2020-03-10

fully randomize hop selection
pull/1178/head
Jeff 4 years ago committed by GitHub
commit c6fd007688
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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(
@ -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;
@ -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;

@ -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)

Loading…
Cancel
Save