From 2425652696421b1277bdb298f5fe6f4d4cc5ba41 Mon Sep 17 00:00:00 2001 From: Thomas Winget Date: Wed, 15 Nov 2023 14:27:54 -0500 Subject: [PATCH] NodeDB RCs don't need insertion time We will want some notion of "when did we receive it" for RCs (or RouterIDs, details tbd), but that will be per-source as a means to form some metric of consensus/trust on which relays are *actually* on the network. Clients don't have a blockchain daemon to pull this from, so they have to ask many relays for the full list of relays and form a trust model on that (bootstrapping problem notwithstanding). --- llarp/link/link_manager.hpp | 2 +- llarp/nodedb.cpp | 60 +++++++++++++--------------------- llarp/nodedb.hpp | 65 ++++++++++++------------------------- 3 files changed, 44 insertions(+), 83 deletions(-) diff --git a/llarp/link/link_manager.hpp b/llarp/link/link_manager.hpp index c2be7dcd6..9967123cc 100644 --- a/llarp/link/link_manager.hpp +++ b/llarp/link/link_manager.hpp @@ -26,7 +26,7 @@ namespace namespace llarp { struct LinkManager; - struct NodeDB; + class NodeDB; namespace link { diff --git a/llarp/nodedb.cpp b/llarp/nodedb.cpp index ffc6d268c..5f47acf6e 100644 --- a/llarp/nodedb.cpp +++ b/llarp/nodedb.cpp @@ -14,9 +14,6 @@ static const std::string RC_FILE_EXT = ".signed"; namespace llarp { - NodeDB::Entry::Entry(RemoteRC value) : rc(std::move(value)), insertedAt(llarp::time_now_ms()) - {} - static void EnsureSkiplist(fs::path nodedbDir) { @@ -72,8 +69,8 @@ namespace llarp // make copy of all rcs std::vector copy; - for (const auto& item : entries) - copy.push_back(item.second.rc); + for (const auto& item : known_rcs) + copy.push_back(item.second); // flush them to disk in one big job // TODO: split this up? idk maybe some day... @@ -212,10 +209,10 @@ namespace llarp return true; } - // validate signature and purge entries with invalid signatures + // validate signature and purge known_rcs with invalid signatures // load ones with valid signatures if (rc.verify()) - entries.emplace(rc.router_id(), rc); + known_rcs.emplace(rc.router_id(), rc); else purge.emplace(f); @@ -240,33 +237,33 @@ namespace llarp return; router.loop()->call([this]() { - for (const auto& item : entries) - item.second.rc.write(get_path_by_pubkey(item.first)); + for (const auto& item : known_rcs) + item.second.write(get_path_by_pubkey(item.first)); }); } bool NodeDB::has_router(RouterID pk) const { - return entries.count(pk); + return known_rcs.count(pk); } std::optional NodeDB::get_rc(RouterID pk) const { - const auto itr = entries.find(pk); + const auto itr = known_rcs.find(pk); - if (itr == entries.end()) + if (itr == known_rcs.end()) return std::nullopt; - return itr->second.rc; + return itr->second; } void NodeDB::remove_router(RouterID pk) { router.loop()->call([this, pk]() { - entries.erase(pk); + known_rcs.erase(pk); remove_many_from_disk_async({pk}); }); } @@ -274,22 +271,9 @@ namespace llarp void NodeDB::remove_stale_rcs(std::unordered_set keep, llarp_time_t cutoff) { - router.loop()->call([this, keep, cutoff]() { - std::unordered_set removed; - auto itr = entries.begin(); - while (itr != entries.end()) - { - if (itr->second.insertedAt < cutoff and keep.count(itr->second.rc.router_id()) == 0) - { - removed.insert(itr->second.rc.router_id()); - itr = entries.erase(itr); - } - else - ++itr; - } - if (not removed.empty()) - remove_many_from_disk_async(std::move(removed)); - }); + (void)keep; + (void)cutoff; + // TODO: handling of "stale" is pending change, removing here for now. } bool @@ -298,22 +282,22 @@ namespace llarp const auto& rid = rc.router_id(); if (not want_rc(rid)) return false; - entries.erase(rid); - entries.emplace(rid, rc); + known_rcs.erase(rid); + known_rcs.emplace(rid, std::move(rc)); return true; } size_t NodeDB::num_loaded() const { - return router.loop()->call_get([this]() { return entries.size(); }); + return router.loop()->call_get([this]() { return known_rcs.size(); }); } bool NodeDB::put_rc_if_newer(RemoteRC rc) { - auto itr = entries.find(rc.router_id()); - if (itr == entries.end() or itr->second.rc.other_is_newer(rc)) + auto itr = known_rcs.find(rc.router_id()); + if (itr == known_rcs.end() or itr->second.other_is_newer(rc)) { return put_rc(std::move(rc)); } @@ -364,10 +348,10 @@ namespace llarp return router.loop()->call_get([this, location, numRouters]() -> std::vector { std::vector all; - all.reserve(entries.size()); - for (auto& entry : entries) + all.reserve(known_rcs.size()); + for (auto& entry : known_rcs) { - all.push_back(&entry.second.rc); + all.push_back(&entry.second); } auto it_mid = numRouters < all.size() ? all.begin() + numRouters : all.end(); diff --git a/llarp/nodedb.hpp b/llarp/nodedb.hpp index 76a635316..286224848 100644 --- a/llarp/nodedb.hpp +++ b/llarp/nodedb.hpp @@ -24,16 +24,7 @@ namespace llarp class NodeDB { - struct Entry - { - const RemoteRC rc; - llarp_time_t insertedAt; - explicit Entry(RemoteRC rc); - }; - - using NodeMap = std::unordered_map; - - NodeMap entries; + std::unordered_map known_rcs; const Router& router; const fs::path m_Root; @@ -137,7 +128,7 @@ namespace llarp /// in memory nodedb NodeDB(); - /// load all entries from disk syncrhonously + /// load all known_rcs from disk syncrhonously void load_from_disk(); @@ -174,44 +165,30 @@ namespace llarp GetRandom(Filter visit) const { return router.loop()->call_get([visit]() -> std::optional { - std::vector entries; - for (const auto& entry : entries) - entries.push_back(entry); + std::vector known_rcs; + for (const auto& entry : known_rcs) + known_rcs.push_back(entry); - std::shuffle(entries.begin(), entries.end(), llarp::csrng); + std::shuffle(known_rcs.begin(), known_rcs.end(), llarp::csrng); - for (const auto entry : entries) + for (const auto entry : known_rcs) { - if (visit(entry->second.rc)) - return entry->second.rc; + if (visit(entry->second)) + return entry->second; } return std::nullopt; }); } - /// visit all entries + /// visit all known_rcs template void VisitAll(Visit visit) const { router.loop()->call([this, visit]() { - for (const auto& item : entries) - visit(item.second.rc); - }); - } - - /// visit all entries inserted before a timestamp - template - void - VisitInsertedBefore(Visit visit, llarp_time_t insertedBefore) - { - router.loop()->call([this, visit, insertedBefore]() { - for (const auto& item : entries) - { - if (item.second.insertedAt < insertedBefore) - visit(item.second.rc); - } + for (const auto& item : known_rcs) + visit(item.second); }); } @@ -226,13 +203,13 @@ namespace llarp { router.loop()->call([this, visit]() { std::unordered_set removed; - auto itr = entries.begin(); - while (itr != entries.end()) + auto itr = known_rcs.begin(); + while (itr != known_rcs.end()) { - if (visit(itr->second.rc)) + if (visit(itr->second)) { - removed.insert(itr->second.rc.router_id()); - itr = entries.erase(itr); + removed.insert(itr->second.router_id()); + itr = known_rcs.erase(itr); } else ++itr; @@ -246,14 +223,14 @@ namespace llarp void remove_stale_rcs(std::unordered_set keep, llarp_time_t cutoff); + /// put (or replace) the RC if we consider it valid (want_rc). returns true if put. + bool + put_rc(RemoteRC rc); + /// if we consider it valid (want_rc), /// put this rc into the cache if it is not there or is newer than the one there already /// returns true if the rc was inserted bool put_rc_if_newer(RemoteRC rc); - - /// put (or replace) the RC if we consider it valid (want_rc). returns true if put. - bool - put_rc(RemoteRC rc); }; } // namespace llarp