mirror of https://github.com/oxen-io/lokinet
Merge pull request #1091 from majestrate/redundant-dht-lookups-2020-02-10
Redundant dht lookupspull/1102/head
commit
2bd783f743
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,2 @@
|
||||
#define CATCH_CONFIG_MAIN
|
||||
#include "catch.hpp"
|
@ -0,0 +1,68 @@
|
||||
#include "catch.hpp"
|
||||
#include "config/config.hpp"
|
||||
|
||||
#include <router_contact.hpp>
|
||||
#include <nodedb.hpp>
|
||||
|
||||
TEST_CASE("FindClosestTo returns correct number of elements", "[nodedb][dht]")
|
||||
{
|
||||
|
||||
llarp_nodedb nodeDB(nullptr, "");
|
||||
|
||||
constexpr uint64_t numRCs = 3;
|
||||
for (uint64_t i=0; i<numRCs; ++i)
|
||||
{
|
||||
llarp::RouterContact rc;
|
||||
rc.pubkey[0] = i;
|
||||
nodeDB.Insert(rc);
|
||||
}
|
||||
|
||||
REQUIRE(numRCs == nodeDB.num_loaded());
|
||||
|
||||
llarp::dht::Key_t key;
|
||||
|
||||
std::vector< llarp::RouterContact > results = nodeDB.FindClosestTo(key, 4);
|
||||
|
||||
// we asked for more entries than nodedb had
|
||||
REQUIRE(numRCs == results.size());
|
||||
}
|
||||
|
||||
TEST_CASE("FindClosestTo returns properly ordered set", "[nodedb][dht]")
|
||||
{
|
||||
|
||||
llarp_nodedb nodeDB(nullptr, "");
|
||||
|
||||
// insert some RCs: a < b < c
|
||||
llarp::RouterContact a;
|
||||
a.pubkey[0] = 1;
|
||||
nodeDB.Insert(a);
|
||||
|
||||
llarp::RouterContact b;
|
||||
b.pubkey[0] = 2;
|
||||
nodeDB.Insert(b);
|
||||
|
||||
llarp::RouterContact c;
|
||||
c.pubkey[0] = 3;
|
||||
nodeDB.Insert(c);
|
||||
|
||||
REQUIRE(3 == nodeDB.num_loaded());
|
||||
|
||||
llarp::dht::Key_t key;
|
||||
|
||||
std::vector< llarp::RouterContact > results = nodeDB.FindClosestTo(key, 2);
|
||||
REQUIRE(2 == results.size());
|
||||
|
||||
// we xor'ed with 0x0, so order should be a,b,c
|
||||
REQUIRE(a.pubkey == results[0].pubkey);
|
||||
REQUIRE(b.pubkey == results[1].pubkey);
|
||||
|
||||
llarp::dht::Key_t compKey;
|
||||
compKey.Fill(0xFF);
|
||||
|
||||
results = nodeDB.FindClosestTo(compKey, 2);
|
||||
|
||||
// we xor'ed with 0xF...F, so order should be inverted (c,b,a)
|
||||
REQUIRE(c.pubkey == results[0].pubkey);
|
||||
REQUIRE(b.pubkey == results[1].pubkey);
|
||||
}
|
||||
|
@ -0,0 +1,39 @@
|
||||
#include <path/path.hpp>
|
||||
#include "catch.hpp"
|
||||
|
||||
using Path_t = llarp::path::Path;
|
||||
using Path_ptr = llarp::path::Path_ptr;
|
||||
using Set_t = llarp::path::Path::UniqueEndpointSet_t;
|
||||
using RC_t = llarp::RouterContact;
|
||||
|
||||
static RC_t
|
||||
MakeHop(const char name)
|
||||
{
|
||||
RC_t rc;
|
||||
rc.pubkey.Fill(name);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static Path_ptr
|
||||
MakePath(std::vector< char > hops)
|
||||
{
|
||||
std::vector< RC_t > pathHops;
|
||||
for(const auto& hop : hops)
|
||||
pathHops.push_back(MakeHop(hop));
|
||||
return std::make_shared< Path_t >(pathHops, nullptr, 0);
|
||||
}
|
||||
|
||||
TEST_CASE("UniqueEndpointSet_t has unique endpoints", "[path]")
|
||||
{
|
||||
Set_t set;
|
||||
REQUIRE(set.empty());
|
||||
const auto inserted_first =
|
||||
set.emplace(MakePath({'a', 'b', 'c', 'd'})).second;
|
||||
REQUIRE(inserted_first);
|
||||
const auto inserted_again =
|
||||
set.emplace(MakePath({'a', 'b', 'c', 'd'})).second;
|
||||
REQUIRE(not inserted_again);
|
||||
const auto inserted_second =
|
||||
set.emplace(MakePath({'d', 'c', 'b', 'a'})).second;
|
||||
REQUIRE(inserted_second);
|
||||
}
|
Loading…
Reference in New Issue