2021-03-09 22:24:35 +00:00
|
|
|
#include "nodedb.hpp"
|
2019-01-10 19:41:51 +00:00
|
|
|
|
2023-10-24 13:18:03 +00:00
|
|
|
#include "crypto/types.hpp"
|
|
|
|
#include "dht/kademlia.hpp"
|
2023-11-30 21:53:41 +00:00
|
|
|
#include "messages/fetch.hpp"
|
2023-10-24 13:18:03 +00:00
|
|
|
#include "router_contact.hpp"
|
|
|
|
#include "util/time.hpp"
|
|
|
|
|
2023-10-19 21:59:57 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <utility>
|
|
|
|
|
2018-06-13 13:09:19 +00:00
|
|
|
static const char skiplist_subdirs[] = "0123456789abcdef";
|
2018-08-02 23:36:34 +00:00
|
|
|
static const std::string RC_FILE_EXT = ".signed";
|
2018-05-30 00:40:02 +00:00
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
namespace llarp
|
2018-12-10 23:29:58 +00:00
|
|
|
{
|
2021-02-02 14:35:40 +00:00
|
|
|
static void
|
|
|
|
EnsureSkiplist(fs::path nodedbDir)
|
2019-03-25 13:52:22 +00:00
|
|
|
{
|
2021-02-02 14:35:40 +00:00
|
|
|
if (not fs::exists(nodedbDir))
|
2019-03-25 13:52:22 +00:00
|
|
|
{
|
2021-02-02 14:35:40 +00:00
|
|
|
// if the old 'netdb' directory exists, move it to this one
|
|
|
|
fs::path parent = nodedbDir.parent_path();
|
|
|
|
fs::path old = parent / "netdb";
|
|
|
|
if (fs::exists(old))
|
|
|
|
fs::rename(old, nodedbDir);
|
2019-03-25 13:52:22 +00:00
|
|
|
else
|
2021-02-02 14:35:40 +00:00
|
|
|
fs::create_directory(nodedbDir);
|
2019-03-25 13:52:22 +00:00
|
|
|
}
|
2020-02-13 22:19:12 +00:00
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
if (not fs::is_directory(nodedbDir))
|
2022-07-16 00:41:14 +00:00
|
|
|
throw std::runtime_error{fmt::format("nodedb {} is not a directory", nodedbDir)};
|
2020-02-13 22:19:12 +00:00
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
for (const char& ch : skiplist_subdirs)
|
2019-09-12 18:19:25 +00:00
|
|
|
{
|
2021-02-02 14:35:40 +00:00
|
|
|
// this seems to be a problem on all targets
|
|
|
|
// perhaps cpp17::fs is just as screwed-up
|
|
|
|
// attempting to create a folder with no name
|
|
|
|
// what does this mean...?
|
|
|
|
if (!ch)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
fs::path sub = nodedbDir / std::string(&ch, 1);
|
|
|
|
fs::create_directory(sub);
|
2019-09-12 18:19:25 +00:00
|
|
|
}
|
2019-06-26 21:39:29 +00:00
|
|
|
}
|
2019-06-17 14:23:38 +00:00
|
|
|
|
2023-10-12 13:43:07 +00:00
|
|
|
NodeDB::NodeDB(fs::path root, std::function<void(std::function<void()>)> diskCaller, Router* r)
|
2023-11-28 12:55:01 +00:00
|
|
|
: _router{*r}
|
|
|
|
, _root{std::move(root)}
|
|
|
|
, _disk(std::move(diskCaller))
|
2023-11-29 14:03:54 +00:00
|
|
|
, _next_flush_time{time_now_ms() + FLUSH_INTERVAL}
|
2018-06-13 11:37:44 +00:00
|
|
|
{
|
2023-11-28 12:55:01 +00:00
|
|
|
EnsureSkiplist(_root);
|
2023-12-01 17:19:07 +00:00
|
|
|
fetch_counters.clear();
|
2018-05-30 00:40:02 +00:00
|
|
|
}
|
|
|
|
|
2023-12-04 15:27:42 +00:00
|
|
|
std::optional<RemoteRC>
|
|
|
|
NodeDB::get_random_rc() const
|
|
|
|
{
|
|
|
|
std::optional<RemoteRC> rand = std::nullopt;
|
|
|
|
|
|
|
|
std::sample(known_rcs.begin(), known_rcs.end(), &*rand, 1, csrng);
|
|
|
|
return rand;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<std::vector<RemoteRC>>
|
|
|
|
NodeDB::get_n_random_rcs(size_t n) const
|
|
|
|
{
|
|
|
|
std::vector<RemoteRC> rand{};
|
|
|
|
|
|
|
|
std::sample(known_rcs.begin(), known_rcs.end(), std::back_inserter(rand), n, csrng);
|
|
|
|
return rand.empty() ? std::nullopt : std::make_optional(rand);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<RemoteRC>
|
|
|
|
NodeDB::get_random_rc_conditional(std::function<bool(RemoteRC)> hook) const
|
|
|
|
{
|
|
|
|
std::optional<RemoteRC> rand = get_random_rc();
|
|
|
|
|
|
|
|
if (rand and hook(*rand))
|
|
|
|
return rand;
|
|
|
|
|
|
|
|
size_t i = 0;
|
|
|
|
|
|
|
|
for (const auto& rc : known_rcs)
|
|
|
|
{
|
|
|
|
if (not hook(rc))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (++i <= 1)
|
|
|
|
{
|
|
|
|
rand = rc;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t x = csrng() % (i + 1);
|
|
|
|
if (x <= 1)
|
|
|
|
rand = rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rand;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<std::vector<RemoteRC>>
|
|
|
|
NodeDB::get_n_random_rcs_conditional(size_t n, std::function<bool(RemoteRC)> hook) const
|
|
|
|
{
|
|
|
|
std::vector<RemoteRC> selected;
|
|
|
|
selected.reserve(n);
|
|
|
|
|
|
|
|
size_t i = 0;
|
|
|
|
|
|
|
|
for (const auto& rc : known_rcs)
|
|
|
|
{
|
|
|
|
// ignore any RC's that do not pass the condition
|
|
|
|
if (not hook(rc))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// load the first n RC's that pass the condition into selected
|
|
|
|
if (++i <= n)
|
|
|
|
{
|
|
|
|
selected.push_back(rc);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// replace selections with decreasing probability per iteration
|
|
|
|
size_t x = csrng() % (i + 1);
|
|
|
|
if (x < n)
|
|
|
|
selected[x] = rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
return selected.size() == n ? std::make_optional(selected) : std::nullopt;
|
|
|
|
}
|
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
void
|
|
|
|
NodeDB::Tick(llarp_time_t now)
|
2019-06-17 14:23:38 +00:00
|
|
|
{
|
2023-11-28 12:55:01 +00:00
|
|
|
if (_next_flush_time == 0s)
|
2021-04-02 15:10:37 +00:00
|
|
|
return;
|
|
|
|
|
2023-11-28 12:55:01 +00:00
|
|
|
if (now > _next_flush_time)
|
2021-02-02 14:35:40 +00:00
|
|
|
{
|
2023-11-28 12:55:01 +00:00
|
|
|
_router.loop()->call([this]() {
|
2023-11-29 14:03:54 +00:00
|
|
|
_next_flush_time += FLUSH_INTERVAL;
|
2023-10-12 18:46:31 +00:00
|
|
|
// make copy of all rcs
|
2023-11-02 12:30:38 +00:00
|
|
|
std::vector<RemoteRC> copy;
|
|
|
|
|
2023-12-01 17:19:07 +00:00
|
|
|
for (const auto& item : rc_lookup)
|
2023-11-15 19:27:54 +00:00
|
|
|
copy.push_back(item.second);
|
2023-11-02 12:30:38 +00:00
|
|
|
|
2023-10-12 18:46:31 +00:00
|
|
|
// flush them to disk in one big job
|
|
|
|
// TODO: split this up? idk maybe some day...
|
2023-11-28 12:55:01 +00:00
|
|
|
_disk([this, data = std::move(copy)]() {
|
2023-10-12 18:46:31 +00:00
|
|
|
for (const auto& rc : data)
|
2023-10-31 20:49:01 +00:00
|
|
|
rc.write(get_path_by_pubkey(rc.router_id()));
|
2023-10-12 18:46:31 +00:00
|
|
|
});
|
2021-02-02 14:35:40 +00:00
|
|
|
});
|
|
|
|
}
|
2019-06-17 14:23:38 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
fs::path
|
2023-10-12 13:43:07 +00:00
|
|
|
NodeDB::get_path_by_pubkey(RouterID pubkey) const
|
2021-02-02 14:35:40 +00:00
|
|
|
{
|
2022-02-17 18:44:31 +00:00
|
|
|
std::string hexString = oxenc::to_hex(pubkey.begin(), pubkey.end());
|
2021-02-02 14:35:40 +00:00
|
|
|
std::string skiplistDir;
|
2020-01-14 17:01:41 +00:00
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
const llarp::RouterID r{pubkey};
|
|
|
|
std::string fname = r.ToString();
|
2019-06-17 14:23:38 +00:00
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
skiplistDir += hexString[0];
|
|
|
|
fname += RC_FILE_EXT;
|
2023-11-28 12:55:01 +00:00
|
|
|
return _root / skiplistDir / fname;
|
2018-04-08 12:18:16 +00:00
|
|
|
}
|
2021-02-02 14:35:40 +00:00
|
|
|
|
2023-11-15 04:55:38 +00:00
|
|
|
bool
|
|
|
|
NodeDB::want_rc(const RouterID& rid) const
|
|
|
|
{
|
2023-11-28 12:55:01 +00:00
|
|
|
if (not _router.is_service_node())
|
2023-11-15 04:55:38 +00:00
|
|
|
return true;
|
|
|
|
return registered_routers.count(rid);
|
|
|
|
}
|
|
|
|
|
2023-11-15 02:53:19 +00:00
|
|
|
void
|
2023-11-30 21:53:41 +00:00
|
|
|
NodeDB::set_bootstrap_routers(std::unique_ptr<BootstrapList> from_router)
|
2023-11-15 02:53:19 +00:00
|
|
|
{
|
2023-11-30 21:53:41 +00:00
|
|
|
// TODO: if this needs to be called more than once (ex: drastic failures), then
|
|
|
|
// change this assert to a bootstraps.clear() call
|
|
|
|
assert(_bootstraps->empty());
|
2023-11-28 12:55:01 +00:00
|
|
|
|
2023-11-30 21:53:41 +00:00
|
|
|
_bootstraps = std::move(from_router);
|
2023-12-01 17:19:07 +00:00
|
|
|
_bootstraps->randomize();
|
2023-11-25 00:40:51 +00:00
|
|
|
}
|
2023-11-17 07:41:42 +00:00
|
|
|
|
2023-11-28 20:05:07 +00:00
|
|
|
bool
|
2023-12-04 15:27:42 +00:00
|
|
|
NodeDB::process_fetched_rcs(std::vector<RemoteRC>& rcs)
|
2023-11-17 07:41:42 +00:00
|
|
|
{
|
2023-12-04 15:27:42 +00:00
|
|
|
std::unordered_set<RemoteRC> inter_set;
|
2023-11-17 07:41:42 +00:00
|
|
|
|
2023-12-04 15:27:42 +00:00
|
|
|
std::set_intersection(
|
|
|
|
known_rcs.begin(),
|
|
|
|
known_rcs.end(),
|
|
|
|
rcs.begin(),
|
|
|
|
rcs.end(),
|
|
|
|
std::inserter(inter_set, inter_set.begin()));
|
2023-12-01 17:19:07 +00:00
|
|
|
|
2023-12-04 15:27:42 +00:00
|
|
|
// the total number of rcs received
|
|
|
|
const auto num_received = static_cast<double>(rcs.size());
|
|
|
|
// the number of returned "good" rcs (that are also found locally)
|
|
|
|
const auto inter_size = inter_set.size();
|
|
|
|
// the number of rcs currently held locally
|
|
|
|
const auto local_count = static_cast<double>(known_rcs.size());
|
|
|
|
|
|
|
|
const auto fetch_threshold = (double)inter_size / num_received;
|
|
|
|
const auto local_alignment = (double)inter_size / local_count;
|
|
|
|
|
|
|
|
/** We are checking 3 things here:
|
|
|
|
1) The number of "good" rcs is above MIN_GOOD_RC_FETCH_TOTAL
|
|
|
|
2) The ratio of "good" rcs to total received is above MIN_GOOD_RC_FETCH_THRESHOLD
|
|
|
|
3) The ratio of received and found locally to total found locally is above
|
|
|
|
LOCAL_RC_ALIGNMENT_THRESHOLD
|
|
|
|
*/
|
|
|
|
return inter_size > MIN_GOOD_RC_FETCH_TOTAL and fetch_threshold > MIN_GOOD_RC_FETCH_THRESHOLD
|
|
|
|
and local_alignment > LOCAL_RC_ALIGNMENT_THRESHOLD;
|
|
|
|
}
|
2023-12-01 17:19:07 +00:00
|
|
|
|
2023-12-04 15:27:42 +00:00
|
|
|
bool
|
|
|
|
NodeDB::ingest_fetched_rcs(std::vector<RemoteRC> rcs, rc_time timestamp)
|
|
|
|
{
|
|
|
|
// if we are not bootstrapping, we should check the rc's against the ones we currently hold
|
|
|
|
if (not _using_bootstrap_fallback)
|
|
|
|
{}
|
2023-11-17 07:41:42 +00:00
|
|
|
|
|
|
|
for (auto& rc : rcs)
|
|
|
|
put_rc_if_newer(std::move(rc), timestamp);
|
|
|
|
|
2023-11-28 20:05:07 +00:00
|
|
|
return true;
|
2023-11-17 07:41:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2023-12-01 17:19:07 +00:00
|
|
|
NodeDB::ingest_rid_fetch_responses(const RouterID& source, std::unordered_set<RouterID> rids)
|
2023-11-17 07:41:42 +00:00
|
|
|
{
|
2023-12-01 17:19:07 +00:00
|
|
|
if (rids.empty())
|
|
|
|
{
|
2023-11-30 21:53:41 +00:00
|
|
|
fail_sources.insert(source);
|
2023-12-01 17:19:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-11-30 21:53:41 +00:00
|
|
|
|
2023-12-01 17:19:07 +00:00
|
|
|
for (const auto& rid : rids)
|
|
|
|
fetch_counters[rid] += 1;
|
2023-11-17 07:41:42 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 21:53:41 +00:00
|
|
|
/** We only call into this function after ensuring two conditions:
|
2023-12-01 17:19:07 +00:00
|
|
|
1) We have received all 12 responses from the queried RouterID sources, whether that
|
|
|
|
response was a timeout or not
|
|
|
|
2) Of those responses, less than 4 were errors of any sorts
|
|
|
|
|
|
|
|
Upon receiving each response from the rid fetch sources, the returned rid's are incremented
|
|
|
|
in fetch_counters. This greatly simplifies the analysis required by this function to the
|
|
|
|
determine success or failure:
|
|
|
|
- If the frequency of each rid is above a threshold, it is accepted
|
|
|
|
- If the number of accepted rids is below a certain amount, the set is rejected
|
|
|
|
|
|
|
|
Logically, this function performs the following basic analysis of the returned RIDs:
|
|
|
|
1) All responses are coalesced into a union set with no repetitions
|
|
|
|
2) If we are bootstrapping:
|
|
|
|
- The routerID's returned
|
2023-11-30 21:53:41 +00:00
|
|
|
*/
|
2023-11-28 18:14:19 +00:00
|
|
|
bool
|
|
|
|
NodeDB::process_fetched_rids()
|
2023-11-28 12:55:01 +00:00
|
|
|
{
|
2023-12-04 15:27:42 +00:00
|
|
|
std::unordered_set<RouterID> union_set, intersection_set;
|
2023-11-30 21:53:41 +00:00
|
|
|
|
2023-12-01 17:19:07 +00:00
|
|
|
for (const auto& [rid, count] : fetch_counters)
|
2023-11-30 21:53:41 +00:00
|
|
|
{
|
2023-12-01 17:19:07 +00:00
|
|
|
if (count > MIN_RID_FETCH_FREQ)
|
|
|
|
union_set.insert(rid);
|
2023-11-30 21:53:41 +00:00
|
|
|
}
|
|
|
|
|
2023-12-04 15:27:42 +00:00
|
|
|
// get the intersection of accepted rids and local rids
|
|
|
|
std::set_intersection(
|
|
|
|
known_rids.begin(),
|
|
|
|
known_rids.end(),
|
|
|
|
union_set.begin(),
|
|
|
|
union_set.end(),
|
|
|
|
std::inserter(intersection_set, intersection_set.begin()));
|
|
|
|
|
|
|
|
// the total number of rids received
|
|
|
|
const auto num_received = (double)fetch_counters.size();
|
|
|
|
// the total number of received AND accepted rids
|
|
|
|
const auto union_size = union_set.size();
|
|
|
|
// the number of rids currently held locally
|
|
|
|
const auto local_count = (double)known_rids.size();
|
|
|
|
// the number of accepted rids that are also found locally
|
|
|
|
const auto inter_size = (double)intersection_set.size();
|
|
|
|
|
|
|
|
const auto fetch_threshold = (double)union_size / num_received;
|
|
|
|
const auto local_alignment = (double)inter_size / local_count;
|
|
|
|
|
|
|
|
/** We are checking 2, potentially 3 things here:
|
|
|
|
1) The ratio of received/accepted to total received is above GOOD_RID_FETCH_THRESHOLD.
|
|
|
|
This tells us how well the rid source's sets of rids "agree" with one another
|
|
|
|
2) The total number received is above MIN_RID_FETCH_TOTAL. This ensures that we are
|
|
|
|
receiving a sufficient amount to make a comparison of any sorts
|
|
|
|
3) If we are not bootstrapping, then the ratio of received/accepted found locally to
|
|
|
|
the total number locally held is above LOCAL_RID_ALIGNMENT_THRESHOLD. This gives us
|
|
|
|
an estimate of how "aligned" the rid source's set of rid's is to ours
|
|
|
|
*/
|
|
|
|
return (fetch_threshold > GOOD_RID_FETCH_THRESHOLD) and (union_size > MIN_GOOD_RID_FETCH_TOTAL)
|
|
|
|
and (not _using_bootstrap_fallback)
|
|
|
|
? local_alignment > LOCAL_RID_ALIGNMENT_THRESHOLD
|
|
|
|
: true;
|
2023-11-28 12:55:01 +00:00
|
|
|
}
|
|
|
|
|
2023-11-17 07:41:42 +00:00
|
|
|
void
|
2023-11-28 20:05:07 +00:00
|
|
|
NodeDB::fetch_initial()
|
|
|
|
{
|
2023-12-01 17:19:07 +00:00
|
|
|
if (known_rids.empty())
|
|
|
|
{
|
|
|
|
fallback_to_bootstrap();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Set fetch source as random selection of known active client routers
|
|
|
|
fetch_source = *std::next(known_rids.begin(), csrng() % known_rids.size());
|
|
|
|
fetch_rcs(true);
|
|
|
|
}
|
2023-11-28 20:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2023-11-30 21:53:41 +00:00
|
|
|
NodeDB::fetch_rcs(bool initial)
|
2023-11-28 20:05:07 +00:00
|
|
|
{
|
|
|
|
auto& num_failures = fetch_failures;
|
|
|
|
|
|
|
|
// base case; this function is called recursively
|
|
|
|
if (num_failures > MAX_FETCH_ATTEMPTS)
|
|
|
|
{
|
2023-11-30 21:53:41 +00:00
|
|
|
fetch_rcs_result(initial, true);
|
2023-11-28 20:05:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<RouterID> needed;
|
|
|
|
const auto now = time_point_now();
|
|
|
|
|
2023-12-01 17:19:07 +00:00
|
|
|
for (const auto& [rid, rc] : rc_lookup)
|
2023-11-28 20:05:07 +00:00
|
|
|
{
|
|
|
|
if (now - rc.timestamp() > RouterContact::OUTDATED_AGE)
|
|
|
|
needed.push_back(rid);
|
|
|
|
}
|
|
|
|
|
|
|
|
RouterID& src = fetch_source;
|
|
|
|
|
|
|
|
_router.link_manager().fetch_rcs(
|
|
|
|
src,
|
2023-12-01 17:19:07 +00:00
|
|
|
RCFetchMessage::serialize(_router.last_rc_fetch, needed),
|
2023-11-30 21:53:41 +00:00
|
|
|
[this, src, initial](oxen::quic::message m) mutable {
|
2023-11-28 20:05:07 +00:00
|
|
|
if (m.timed_out)
|
|
|
|
{
|
|
|
|
log::info(logcat, "RC fetch to {} timed out", src);
|
2023-11-30 21:53:41 +00:00
|
|
|
fetch_rcs_result(initial, true);
|
2023-11-28 20:05:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
oxenc::bt_dict_consumer btdc{m.body()};
|
2023-11-30 21:53:41 +00:00
|
|
|
|
2023-11-28 20:05:07 +00:00
|
|
|
if (not m)
|
|
|
|
{
|
|
|
|
auto reason = btdc.require<std::string_view>(messages::STATUS_KEY);
|
|
|
|
log::info(logcat, "RC fetch to {} returned error: {}", src, reason);
|
2023-11-30 21:53:41 +00:00
|
|
|
fetch_rcs_result(initial, true);
|
2023-11-28 20:05:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto btlc = btdc.require<oxenc::bt_list_consumer>("rcs"sv);
|
|
|
|
auto timestamp = rc_time{std::chrono::seconds{btdc.require<int64_t>("time"sv)}};
|
|
|
|
|
|
|
|
std::vector<RemoteRC> rcs;
|
|
|
|
|
|
|
|
while (not btlc.is_finished())
|
|
|
|
rcs.emplace_back(btlc.consume_dict_consumer());
|
|
|
|
|
|
|
|
// if process_fetched_rcs returns false, then the trust model rejected the fetched RC's
|
2023-12-04 15:27:42 +00:00
|
|
|
fetch_rcs_result(initial, not ingest_fetched_rcs(std::move(rcs), timestamp));
|
2023-11-28 20:05:07 +00:00
|
|
|
}
|
|
|
|
catch (const std::exception& e)
|
|
|
|
{
|
|
|
|
log::info(logcat, "Failed to parse RC fetch response from {}: {}", src, e.what());
|
2023-11-30 21:53:41 +00:00
|
|
|
fetch_rcs_result(initial, true);
|
2023-11-28 20:05:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2023-11-30 21:53:41 +00:00
|
|
|
NodeDB::fetch_rids(bool initial)
|
2023-11-28 20:05:07 +00:00
|
|
|
{
|
|
|
|
// base case; this function is called recursively
|
2023-11-28 20:50:07 +00:00
|
|
|
if (fetch_failures > MAX_FETCH_ATTEMPTS)
|
2023-11-28 20:05:07 +00:00
|
|
|
{
|
2023-11-30 21:53:41 +00:00
|
|
|
fetch_rids_result(initial);
|
2023-11-28 20:05:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rid_sources.empty())
|
2023-12-01 17:19:07 +00:00
|
|
|
{
|
|
|
|
reselect_router_id_sources(rid_sources);
|
|
|
|
}
|
2023-11-28 20:05:07 +00:00
|
|
|
|
2023-11-30 21:53:41 +00:00
|
|
|
if (not initial and rid_sources.empty())
|
2023-11-28 20:05:07 +00:00
|
|
|
{
|
|
|
|
log::error(logcat, "Attempting to fetch RouterIDs, but have no source from which to do so.");
|
2023-12-04 15:27:42 +00:00
|
|
|
fallback_to_bootstrap();
|
2023-11-28 20:05:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-01 17:19:07 +00:00
|
|
|
fetch_counters.clear();
|
2023-11-28 20:05:07 +00:00
|
|
|
|
|
|
|
RouterID& src = fetch_source;
|
|
|
|
|
|
|
|
for (const auto& target : rid_sources)
|
|
|
|
{
|
|
|
|
_router.link_manager().fetch_router_ids(
|
|
|
|
src,
|
2023-11-30 21:53:41 +00:00
|
|
|
FetchRIDMessage::serialize(target),
|
|
|
|
[this, src, target, initial](oxen::quic::message m) mutable {
|
|
|
|
if (not m)
|
2023-11-28 20:05:07 +00:00
|
|
|
{
|
|
|
|
log::info(link_cat, "RID fetch from {} via {} timed out", src, target);
|
2023-11-30 21:53:41 +00:00
|
|
|
ingest_rid_fetch_responses(target);
|
|
|
|
fetch_rids_result(initial);
|
2023-11-28 20:05:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
oxenc::bt_dict_consumer btdc{m.body()};
|
|
|
|
|
|
|
|
btdc.required("routers");
|
|
|
|
auto router_id_strings = btdc.consume_list<std::vector<ustring>>();
|
|
|
|
|
|
|
|
btdc.require_signature("signature", [&src](ustring_view msg, ustring_view sig) {
|
|
|
|
if (sig.size() != 64)
|
|
|
|
throw std::runtime_error{"Invalid signature: not 64 bytes"};
|
|
|
|
if (not crypto::verify(src, msg, sig))
|
|
|
|
throw std::runtime_error{
|
|
|
|
"Failed to verify signature for fetch RouterIDs response."};
|
|
|
|
});
|
|
|
|
|
2023-11-29 14:11:38 +00:00
|
|
|
std::unordered_set<RouterID> router_ids;
|
2023-11-28 20:05:07 +00:00
|
|
|
|
|
|
|
for (const auto& s : router_id_strings)
|
|
|
|
{
|
|
|
|
if (s.size() != RouterID::SIZE)
|
|
|
|
{
|
|
|
|
log::warning(
|
|
|
|
link_cat, "RID fetch from {} via {} returned bad RouterID", target, src);
|
2023-11-28 20:50:07 +00:00
|
|
|
ingest_rid_fetch_responses(target);
|
2023-11-30 21:53:41 +00:00
|
|
|
fetch_rids_result(initial);
|
2023-11-28 20:05:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-29 14:11:38 +00:00
|
|
|
router_ids.emplace(s.data());
|
2023-11-28 20:05:07 +00:00
|
|
|
}
|
|
|
|
|
2023-11-28 20:50:07 +00:00
|
|
|
ingest_rid_fetch_responses(target, std::move(router_ids));
|
2023-11-30 21:53:41 +00:00
|
|
|
fetch_rids_result(initial); // success
|
2023-11-28 20:05:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch (const std::exception& e)
|
|
|
|
{
|
|
|
|
log::info(link_cat, "Error handling fetch RouterIDs response: {}", e.what());
|
2023-11-28 20:50:07 +00:00
|
|
|
ingest_rid_fetch_responses(target);
|
2023-11-30 21:53:41 +00:00
|
|
|
fetch_rids_result(initial);
|
2023-11-28 20:05:07 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2023-11-30 21:53:41 +00:00
|
|
|
NodeDB::fetch_rcs_result(bool initial, bool error)
|
|
|
|
{
|
|
|
|
if (error)
|
|
|
|
{
|
2023-12-04 15:27:42 +00:00
|
|
|
auto& fail_count = (_using_bootstrap_fallback) ? bootstrap_failures : fetch_failures;
|
2023-12-01 17:19:07 +00:00
|
|
|
auto& THRESHOLD =
|
2023-12-04 15:27:42 +00:00
|
|
|
(_using_bootstrap_fallback) ? MAX_BOOTSTRAP_FETCH_ATTEMPTS : MAX_FETCH_ATTEMPTS;
|
2023-12-01 17:19:07 +00:00
|
|
|
|
|
|
|
// This catches three different failure cases;
|
|
|
|
// 1) bootstrap fetching and over failure threshold
|
|
|
|
// 2) bootstrap fetching and more failures to go
|
|
|
|
// 3) standard fetching and over threshold
|
2023-12-04 15:27:42 +00:00
|
|
|
if (++fail_count >= THRESHOLD || _using_bootstrap_fallback)
|
2023-11-30 21:53:41 +00:00
|
|
|
{
|
|
|
|
log::info(
|
|
|
|
logcat,
|
2023-12-01 17:19:07 +00:00
|
|
|
"RC fetching from {} reached failure threshold ({}); falling back to bootstrap...",
|
|
|
|
fetch_source,
|
|
|
|
THRESHOLD);
|
2023-11-30 21:53:41 +00:00
|
|
|
|
|
|
|
fallback_to_bootstrap();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-01 17:19:07 +00:00
|
|
|
// If we have passed the last last conditional, then it means we are not bootstrapping
|
|
|
|
// and the current fetch_source has more attempts before being rotated. As a result, we
|
2023-11-30 21:53:41 +00:00
|
|
|
// find new non-bootstrap RC fetch source and try again buddy
|
2023-12-01 17:19:07 +00:00
|
|
|
fetch_source = (initial) ? *std::next(known_rids.begin(), csrng() % known_rids.size())
|
|
|
|
: std::next(rc_lookup.begin(), csrng() % rc_lookup.size())->first;
|
2023-11-30 21:53:41 +00:00
|
|
|
|
|
|
|
fetch_rcs(initial);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
log::debug(logcat, "Successfully fetched RC's from {}", fetch_source);
|
|
|
|
post_fetch_rcs(initial);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NodeDB::fetch_rids_result(bool initial)
|
2023-11-28 20:05:07 +00:00
|
|
|
{
|
2023-11-28 20:50:07 +00:00
|
|
|
if (fetch_failures > MAX_FETCH_ATTEMPTS)
|
2023-11-28 20:05:07 +00:00
|
|
|
{
|
2023-11-28 20:50:07 +00:00
|
|
|
log::info(
|
|
|
|
logcat,
|
|
|
|
"Failed {} attempts to fetch RID's from {}; reverting to bootstrap...",
|
|
|
|
MAX_FETCH_ATTEMPTS,
|
|
|
|
fetch_source);
|
2023-11-28 20:05:07 +00:00
|
|
|
|
2023-11-30 21:53:41 +00:00
|
|
|
fallback_to_bootstrap();
|
2023-11-28 20:05:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-01 17:19:07 +00:00
|
|
|
auto n_responses = RID_SOURCE_COUNT - fail_sources.size();
|
2023-11-28 20:50:07 +00:00
|
|
|
|
2023-12-01 17:19:07 +00:00
|
|
|
if (n_responses < RID_SOURCE_COUNT)
|
2023-11-28 20:50:07 +00:00
|
|
|
{
|
2023-12-01 17:19:07 +00:00
|
|
|
log::debug(logcat, "Received {}/{} fetch RID requests", n_responses, RID_SOURCE_COUNT);
|
2023-11-28 20:50:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-28 20:05:07 +00:00
|
|
|
auto n_fails = fail_sources.size();
|
|
|
|
|
|
|
|
if (n_fails <= MAX_RID_ERRORS)
|
|
|
|
{
|
|
|
|
log::debug(
|
|
|
|
logcat, "RID fetching was successful ({}/{} acceptable errors)", n_fails, MAX_RID_ERRORS);
|
|
|
|
|
|
|
|
// this is where the trust model will do verification based on the similarity of the sets
|
|
|
|
if (process_fetched_rids())
|
|
|
|
{
|
|
|
|
log::debug(logcat, "Accumulated RID's accepted by trust model");
|
2023-11-30 21:53:41 +00:00
|
|
|
post_fetch_rids(initial);
|
2023-11-28 20:05:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log::debug(
|
|
|
|
logcat, "Accumulated RID's rejected by trust model, reselecting all RID sources...");
|
2023-12-01 17:19:07 +00:00
|
|
|
reselect_router_id_sources(rid_sources);
|
2023-11-28 20:05:07 +00:00
|
|
|
++fetch_failures;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// we had 4 or more failed requests, so we will need to rotate our rid sources
|
|
|
|
log::debug(
|
|
|
|
logcat, "RID fetching found {} failures; reselecting failed RID sources...", n_fails);
|
|
|
|
++fetch_failures;
|
2023-12-01 17:19:07 +00:00
|
|
|
reselect_router_id_sources(fail_sources);
|
2023-11-28 20:05:07 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 21:53:41 +00:00
|
|
|
fetch_rids(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NodeDB::post_fetch_rcs(bool initial)
|
|
|
|
{
|
|
|
|
_router.last_rc_fetch = llarp::time_point_now();
|
|
|
|
|
|
|
|
if (initial)
|
|
|
|
fetch_rids(initial);
|
2023-11-28 20:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2023-11-30 21:53:41 +00:00
|
|
|
NodeDB::post_fetch_rids(bool initial)
|
2023-11-28 20:05:07 +00:00
|
|
|
{
|
|
|
|
fail_sources.clear();
|
|
|
|
fetch_failures = 0;
|
|
|
|
_router.last_rid_fetch = llarp::time_point_now();
|
2023-12-01 17:19:07 +00:00
|
|
|
fetch_counters.clear();
|
2023-12-04 15:27:42 +00:00
|
|
|
_needs_rebootstrap = false;
|
2023-11-30 21:53:41 +00:00
|
|
|
|
|
|
|
if (initial)
|
2023-12-04 15:27:42 +00:00
|
|
|
_needs_initial_fetch = false;
|
2023-11-30 21:53:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NodeDB::fallback_to_bootstrap()
|
|
|
|
{
|
2023-12-01 17:19:07 +00:00
|
|
|
auto at_max_failures = bootstrap_failures >= MAX_BOOTSTRAP_FETCH_ATTEMPTS;
|
|
|
|
|
2023-12-04 15:27:42 +00:00
|
|
|
// base case: we have failed to query all bootstraps, or we received a sample of
|
|
|
|
// the network, but the sample was unusable or unreachable. We will also enter this
|
|
|
|
// if we are on our first fallback to bootstrap so we can set the fetch_source (by
|
|
|
|
// checking not using_bootstrap_fallback)
|
|
|
|
if (at_max_failures || not _using_bootstrap_fallback)
|
2023-11-30 21:53:41 +00:00
|
|
|
{
|
|
|
|
bootstrap_failures = 0;
|
|
|
|
|
2023-12-01 17:19:07 +00:00
|
|
|
// Fail case: if we have returned to the front of the bootstrap list, we're in a
|
|
|
|
// bad spot; we are unable to do anything
|
2023-12-04 15:27:42 +00:00
|
|
|
if (_using_bootstrap_fallback)
|
2023-11-30 21:53:41 +00:00
|
|
|
{
|
2023-12-04 15:27:42 +00:00
|
|
|
auto err = fmt::format(
|
|
|
|
"ERROR: ALL BOOTSTRAPS ARE BAD... REATTEMPTING IN {}...", BOOTSTRAP_COOLDOWN);
|
2023-11-30 21:53:41 +00:00
|
|
|
log::error(logcat, err);
|
2023-12-01 17:19:07 +00:00
|
|
|
|
2023-12-04 15:27:42 +00:00
|
|
|
bootstrap_cooldown();
|
|
|
|
return;
|
2023-11-30 21:53:41 +00:00
|
|
|
}
|
|
|
|
|
2023-12-04 15:27:42 +00:00
|
|
|
auto rc = _bootstraps->next();
|
2023-11-30 21:53:41 +00:00
|
|
|
fetch_source = rc.router_id();
|
|
|
|
}
|
|
|
|
|
2023-12-01 17:19:07 +00:00
|
|
|
// By passing the last conditional, we ensure this is set to true
|
2023-12-04 15:27:42 +00:00
|
|
|
_using_bootstrap_fallback = true;
|
|
|
|
_needs_rebootstrap = false;
|
2023-12-01 17:19:07 +00:00
|
|
|
|
2023-11-30 21:53:41 +00:00
|
|
|
_router.link_manager().fetch_bootstrap_rcs(
|
|
|
|
fetch_source,
|
|
|
|
BootstrapFetchMessage::serialize(BOOTSTRAP_SOURCE_COUNT),
|
|
|
|
[this](oxen::quic::message m) mutable {
|
|
|
|
if (not m)
|
|
|
|
{
|
|
|
|
++bootstrap_failures;
|
2023-12-01 17:19:07 +00:00
|
|
|
log::warning(
|
|
|
|
logcat,
|
|
|
|
"BootstrapRC fetch request to {} failed (error {}/{})",
|
|
|
|
fetch_source,
|
|
|
|
bootstrap_failures,
|
|
|
|
MAX_BOOTSTRAP_FETCH_ATTEMPTS);
|
2023-11-30 21:53:41 +00:00
|
|
|
fallback_to_bootstrap();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unordered_set<RouterID> rids;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
oxenc::bt_dict_consumer btdc{m.body()};
|
|
|
|
|
|
|
|
{
|
|
|
|
auto btlc = btdc.require<oxenc::bt_list_consumer>("rcs"sv);
|
|
|
|
|
|
|
|
while (not btlc.is_finished())
|
|
|
|
{
|
|
|
|
auto rc = RemoteRC{btlc.consume_dict_consumer()};
|
|
|
|
rids.emplace(rc.router_id());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const std::exception& e)
|
|
|
|
{
|
2023-12-01 17:19:07 +00:00
|
|
|
++bootstrap_failures;
|
|
|
|
log::warning(
|
2023-11-30 21:53:41 +00:00
|
|
|
logcat,
|
2023-12-01 17:19:07 +00:00
|
|
|
"Failed to parse BootstrapRC fetch response from {} (error {}/{}): {}",
|
2023-11-30 21:53:41 +00:00
|
|
|
fetch_source,
|
2023-12-01 17:19:07 +00:00
|
|
|
bootstrap_failures,
|
|
|
|
MAX_BOOTSTRAP_FETCH_ATTEMPTS,
|
2023-11-30 21:53:41 +00:00
|
|
|
e.what());
|
|
|
|
fallback_to_bootstrap();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-01 17:19:07 +00:00
|
|
|
// We set this to the max allowable value because if this result is bad, we won't
|
|
|
|
// try this bootstrap again. If this result is undersized, we roll right into the
|
|
|
|
// next call to fallback_to_bootstrap() and hit the base case, rotating sources
|
2023-11-30 21:53:41 +00:00
|
|
|
bootstrap_failures = MAX_BOOTSTRAP_FETCH_ATTEMPTS;
|
2023-12-01 17:19:07 +00:00
|
|
|
|
|
|
|
if (rids.size() == BOOTSTRAP_SOURCE_COUNT)
|
|
|
|
{
|
|
|
|
known_rids.swap(rids);
|
|
|
|
fetch_initial();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++bootstrap_failures;
|
|
|
|
log::warning(
|
|
|
|
logcat,
|
|
|
|
"BootstrapRC fetch response from {} returned insufficient number of RC's (error "
|
|
|
|
"{}/{})",
|
|
|
|
fetch_source,
|
|
|
|
bootstrap_failures,
|
|
|
|
MAX_BOOTSTRAP_FETCH_ATTEMPTS);
|
|
|
|
fallback_to_bootstrap();
|
|
|
|
}
|
2023-11-30 21:53:41 +00:00
|
|
|
});
|
2023-11-25 00:40:51 +00:00
|
|
|
}
|
|
|
|
|
2023-12-04 15:27:42 +00:00
|
|
|
void
|
|
|
|
NodeDB::bootstrap_cooldown()
|
|
|
|
{
|
|
|
|
_needs_rebootstrap = true;
|
|
|
|
_router.next_bootstrap_attempt = llarp::time_point_now() + BOOTSTRAP_COOLDOWN;
|
|
|
|
}
|
|
|
|
|
2023-11-25 00:40:51 +00:00
|
|
|
void
|
2023-12-01 17:19:07 +00:00
|
|
|
NodeDB::reselect_router_id_sources(std::unordered_set<RouterID> specific)
|
2023-11-25 00:40:51 +00:00
|
|
|
{
|
2023-12-01 17:19:07 +00:00
|
|
|
replace_subset(rid_sources, specific, known_rids, RID_SOURCE_COUNT, csrng);
|
2023-11-25 00:40:51 +00:00
|
|
|
}
|
|
|
|
|
2023-11-15 02:53:19 +00:00
|
|
|
void
|
|
|
|
NodeDB::set_router_whitelist(
|
|
|
|
const std::vector<RouterID>& whitelist,
|
|
|
|
const std::vector<RouterID>& greylist,
|
|
|
|
const std::vector<RouterID>& greenlist)
|
|
|
|
{
|
|
|
|
if (whitelist.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
registered_routers.clear();
|
|
|
|
registered_routers.insert(whitelist.begin(), whitelist.end());
|
|
|
|
registered_routers.insert(greylist.begin(), greylist.end());
|
|
|
|
registered_routers.insert(greenlist.begin(), greenlist.end());
|
|
|
|
|
|
|
|
router_whitelist.clear();
|
|
|
|
router_whitelist.insert(whitelist.begin(), whitelist.end());
|
|
|
|
router_greylist.clear();
|
|
|
|
router_greylist.insert(greylist.begin(), greylist.end());
|
|
|
|
router_greenlist.clear();
|
|
|
|
router_greenlist.insert(greenlist.begin(), greenlist.end());
|
|
|
|
|
|
|
|
log::info(
|
|
|
|
logcat, "lokinet service node list now has ", router_whitelist.size(), " active routers");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<RouterID>
|
|
|
|
NodeDB::get_random_whitelist_router() const
|
|
|
|
{
|
|
|
|
const auto sz = router_whitelist.size();
|
|
|
|
if (sz == 0)
|
|
|
|
return std::nullopt;
|
|
|
|
auto itr = router_whitelist.begin();
|
|
|
|
if (sz > 1)
|
|
|
|
std::advance(itr, randint() % sz);
|
|
|
|
return *itr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
NodeDB::is_connection_allowed(const RouterID& remote) const
|
|
|
|
{
|
2023-11-30 21:53:41 +00:00
|
|
|
if (_pinned_edges.size() && _pinned_edges.count(remote) == 0
|
|
|
|
&& not _bootstraps->contains(remote))
|
2023-11-15 02:53:19 +00:00
|
|
|
return false;
|
|
|
|
|
2023-11-28 12:55:01 +00:00
|
|
|
if (not _router.is_service_node())
|
2023-11-15 02:53:19 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return router_whitelist.count(remote) or router_greylist.count(remote);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
NodeDB::is_first_hop_allowed(const RouterID& remote) const
|
|
|
|
{
|
2023-11-30 21:53:41 +00:00
|
|
|
if (_pinned_edges.size() && _pinned_edges.count(remote) == 0)
|
2023-11-15 02:53:19 +00:00
|
|
|
return false;
|
2023-12-01 17:19:07 +00:00
|
|
|
|
2023-11-15 02:53:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
void
|
2023-10-12 13:43:07 +00:00
|
|
|
NodeDB::load_from_disk()
|
2018-05-30 00:40:02 +00:00
|
|
|
{
|
2023-11-28 12:55:01 +00:00
|
|
|
if (_root.empty())
|
2021-04-02 15:10:37 +00:00
|
|
|
return;
|
2022-07-26 15:05:18 +00:00
|
|
|
|
2023-11-27 16:31:43 +00:00
|
|
|
std::set<fs::path> purge;
|
2023-10-12 18:46:31 +00:00
|
|
|
|
2023-11-27 16:31:43 +00:00
|
|
|
const auto now = time_now_ms();
|
|
|
|
|
|
|
|
for (const char& ch : skiplist_subdirs)
|
|
|
|
{
|
|
|
|
if (!ch)
|
|
|
|
continue;
|
2023-12-01 17:19:07 +00:00
|
|
|
|
2023-11-27 16:31:43 +00:00
|
|
|
std::string p;
|
|
|
|
p += ch;
|
2023-11-28 12:55:01 +00:00
|
|
|
fs::path sub = _root / p;
|
2022-07-26 15:05:18 +00:00
|
|
|
|
2023-11-27 16:31:43 +00:00
|
|
|
llarp::util::IterDir(sub, [&](const fs::path& f) -> bool {
|
|
|
|
// skip files that are not suffixed with .signed
|
|
|
|
if (not(fs::is_regular_file(f) and f.extension() == RC_FILE_EXT))
|
2022-07-26 15:05:18 +00:00
|
|
|
return true;
|
|
|
|
|
2023-11-27 16:31:43 +00:00
|
|
|
RemoteRC rc{};
|
2022-07-26 15:05:18 +00:00
|
|
|
|
2023-11-27 16:31:43 +00:00
|
|
|
if (not rc.read(f))
|
|
|
|
{
|
|
|
|
// try loading it, purge it if it is junk
|
|
|
|
purge.emplace(f);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc.is_expired(now))
|
|
|
|
{
|
|
|
|
// rc expired dont load it and purge it later
|
|
|
|
purge.emplace(f);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-11-28 12:55:01 +00:00
|
|
|
const auto& rid = rc.router_id();
|
|
|
|
|
2023-12-01 17:19:07 +00:00
|
|
|
auto [itr, b] = known_rcs.emplace(std::move(rc));
|
|
|
|
rc_lookup.emplace(rid, *itr);
|
|
|
|
known_rids.insert(rid);
|
2023-11-27 16:31:43 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (not purge.empty())
|
|
|
|
{
|
|
|
|
log::warning(logcat, "removing {} invalid RCs from disk", purge.size());
|
|
|
|
|
|
|
|
for (const auto& fpath : purge)
|
|
|
|
fs::remove(fpath);
|
|
|
|
}
|
2018-05-30 00:40:02 +00:00
|
|
|
}
|
2021-02-02 14:35:40 +00:00
|
|
|
|
|
|
|
void
|
2023-10-12 13:43:07 +00:00
|
|
|
NodeDB::save_to_disk() const
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2023-11-28 12:55:01 +00:00
|
|
|
if (_root.empty())
|
2021-04-02 15:10:37 +00:00
|
|
|
return;
|
|
|
|
|
2023-11-28 12:55:01 +00:00
|
|
|
_router.loop()->call([this]() {
|
2023-12-01 17:19:07 +00:00
|
|
|
for (const auto& rc : rc_lookup)
|
|
|
|
{
|
|
|
|
rc.second.write(get_path_by_pubkey(rc.first));
|
|
|
|
}
|
2023-10-12 13:43:07 +00:00
|
|
|
});
|
2018-04-08 12:18:16 +00:00
|
|
|
}
|
2018-04-30 16:14:20 +00:00
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
bool
|
2023-11-27 18:28:45 +00:00
|
|
|
NodeDB::has_rc(RouterID pk) const
|
2018-09-11 15:53:54 +00:00
|
|
|
{
|
2023-12-01 17:19:07 +00:00
|
|
|
return rc_lookup.count(pk);
|
2018-09-11 15:53:54 +00:00
|
|
|
}
|
|
|
|
|
2023-11-02 12:30:38 +00:00
|
|
|
std::optional<RemoteRC>
|
2023-10-12 13:43:07 +00:00
|
|
|
NodeDB::get_rc(RouterID pk) const
|
2018-06-25 15:12:08 +00:00
|
|
|
{
|
2023-12-01 17:19:07 +00:00
|
|
|
if (auto itr = rc_lookup.find(pk); itr != rc_lookup.end())
|
|
|
|
return itr->second;
|
2023-10-12 18:46:31 +00:00
|
|
|
|
2023-12-01 17:19:07 +00:00
|
|
|
return std::nullopt;
|
2018-06-21 09:33:23 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
void
|
2023-10-12 13:43:07 +00:00
|
|
|
NodeDB::remove_router(RouterID pk)
|
2020-06-11 11:44:02 +00:00
|
|
|
{
|
2023-11-28 12:55:01 +00:00
|
|
|
_router.loop()->call([this, pk]() {
|
2023-12-01 17:19:07 +00:00
|
|
|
rc_lookup.erase(pk);
|
2023-10-12 13:43:07 +00:00
|
|
|
remove_many_from_disk_async({pk});
|
|
|
|
});
|
2020-06-11 11:44:02 +00:00
|
|
|
}
|
2018-06-07 09:36:30 +00:00
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
void
|
2023-11-27 16:31:43 +00:00
|
|
|
NodeDB::remove_stale_rcs()
|
2018-06-07 09:36:30 +00:00
|
|
|
{
|
2023-11-28 12:55:01 +00:00
|
|
|
auto cutoff_time = time_point_now();
|
|
|
|
|
|
|
|
cutoff_time -=
|
|
|
|
_router.is_service_node() ? RouterContact::OUTDATED_AGE : RouterContact::LIFETIME;
|
2023-12-01 17:19:07 +00:00
|
|
|
|
|
|
|
for (auto itr = rc_lookup.begin(); itr != rc_lookup.end();)
|
2023-11-27 16:31:43 +00:00
|
|
|
{
|
|
|
|
if (cutoff_time > itr->second.timestamp())
|
|
|
|
{
|
|
|
|
log::info(logcat, "Pruning RC for {}, as it is too old to keep.", itr->first);
|
2023-12-04 15:27:42 +00:00
|
|
|
known_rcs.erase(itr->second);
|
2023-12-01 17:19:07 +00:00
|
|
|
rc_lookup.erase(itr);
|
2023-11-27 16:31:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
itr++;
|
|
|
|
}
|
2018-06-07 09:36:30 +00:00
|
|
|
}
|
2018-06-13 12:58:51 +00:00
|
|
|
|
2023-11-15 04:55:38 +00:00
|
|
|
bool
|
2023-11-17 07:41:42 +00:00
|
|
|
NodeDB::put_rc(RemoteRC rc, rc_time now)
|
2020-04-02 17:56:13 +00:00
|
|
|
{
|
2023-11-15 04:55:38 +00:00
|
|
|
const auto& rid = rc.router_id();
|
2023-12-01 17:19:07 +00:00
|
|
|
|
2023-11-15 04:55:38 +00:00
|
|
|
if (not want_rc(rid))
|
|
|
|
return false;
|
2023-12-01 17:19:07 +00:00
|
|
|
|
2023-12-04 15:27:42 +00:00
|
|
|
known_rcs.erase(rc);
|
2023-12-01 17:19:07 +00:00
|
|
|
rc_lookup.erase(rid);
|
|
|
|
|
|
|
|
auto [itr, b] = known_rcs.emplace(std::move(rc));
|
|
|
|
rc_lookup.emplace(rid, *itr);
|
|
|
|
known_rids.insert(rid);
|
|
|
|
|
2023-11-17 07:41:42 +00:00
|
|
|
last_rc_update_times[rid] = now;
|
2023-11-15 04:55:38 +00:00
|
|
|
return true;
|
2020-04-02 17:56:13 +00:00
|
|
|
}
|
2018-04-08 12:18:16 +00:00
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
size_t
|
2023-10-12 13:43:07 +00:00
|
|
|
NodeDB::num_loaded() const
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2023-11-28 12:55:01 +00:00
|
|
|
return _router.loop()->call_get([this]() { return known_rcs.size(); });
|
2018-04-08 12:18:16 +00:00
|
|
|
}
|
2018-09-13 16:41:53 +00:00
|
|
|
|
2023-11-15 04:55:38 +00:00
|
|
|
bool
|
2023-11-17 07:41:42 +00:00
|
|
|
NodeDB::put_rc_if_newer(RemoteRC rc, rc_time now)
|
2018-11-14 18:02:27 +00:00
|
|
|
{
|
2023-12-01 17:19:07 +00:00
|
|
|
if (auto itr = rc_lookup.find(rc.router_id());
|
|
|
|
itr == rc_lookup.end() or itr->second.other_is_newer(rc))
|
2023-11-17 07:41:42 +00:00
|
|
|
return put_rc(std::move(rc), now);
|
2023-12-01 17:19:07 +00:00
|
|
|
|
2023-11-15 04:55:38 +00:00
|
|
|
return false;
|
2018-11-14 18:02:27 +00:00
|
|
|
}
|
2021-02-02 14:35:40 +00:00
|
|
|
|
|
|
|
void
|
2023-10-12 13:43:07 +00:00
|
|
|
NodeDB::remove_many_from_disk_async(std::unordered_set<RouterID> remove) const
|
2018-11-14 18:02:27 +00:00
|
|
|
{
|
2023-11-28 12:55:01 +00:00
|
|
|
if (_root.empty())
|
2021-04-02 15:10:37 +00:00
|
|
|
return;
|
2023-12-01 17:19:07 +00:00
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
// build file list
|
|
|
|
std::set<fs::path> files;
|
2023-12-01 17:19:07 +00:00
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
for (auto id : remove)
|
2023-10-12 13:43:07 +00:00
|
|
|
files.emplace(get_path_by_pubkey(std::move(id)));
|
2023-12-01 17:19:07 +00:00
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
// remove them from the disk via the diskio thread
|
2023-11-28 12:55:01 +00:00
|
|
|
_disk([files]() {
|
2021-02-02 14:35:40 +00:00
|
|
|
for (auto fpath : files)
|
|
|
|
fs::remove(fpath);
|
|
|
|
});
|
2018-11-14 18:02:27 +00:00
|
|
|
}
|
|
|
|
|
2023-11-02 12:30:38 +00:00
|
|
|
RemoteRC
|
2023-10-12 13:43:07 +00:00
|
|
|
NodeDB::find_closest_to(llarp::dht::Key_t location) const
|
2019-03-25 13:52:22 +00:00
|
|
|
{
|
2023-11-28 12:55:01 +00:00
|
|
|
return _router.loop()->call_get([this, location]() -> RemoteRC {
|
2023-12-04 15:27:42 +00:00
|
|
|
RemoteRC rc{};
|
2023-10-12 13:43:07 +00:00
|
|
|
const llarp::dht::XorMetric compare(location);
|
2023-11-02 12:30:38 +00:00
|
|
|
|
2023-10-12 13:43:07 +00:00
|
|
|
VisitAll([&rc, compare](const auto& otherRC) {
|
2023-11-02 12:30:38 +00:00
|
|
|
const auto& rid = rc.router_id();
|
|
|
|
|
|
|
|
if (rid.IsZero() || compare(dht::Key_t{otherRC.router_id()}, dht::Key_t{rid}))
|
2023-10-12 13:43:07 +00:00
|
|
|
{
|
|
|
|
rc = otherRC;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return rc;
|
2021-02-02 14:35:40 +00:00
|
|
|
});
|
2019-03-25 13:52:22 +00:00
|
|
|
}
|
2019-03-11 13:58:31 +00:00
|
|
|
|
2023-11-02 12:30:38 +00:00
|
|
|
std::vector<RemoteRC>
|
2023-10-12 13:43:07 +00:00
|
|
|
NodeDB::find_many_closest_to(llarp::dht::Key_t location, uint32_t numRouters) const
|
2020-03-11 20:45:48 +00:00
|
|
|
{
|
2023-11-28 12:55:01 +00:00
|
|
|
return _router.loop()->call_get([this, location, numRouters]() -> std::vector<RemoteRC> {
|
2023-11-02 12:30:38 +00:00
|
|
|
std::vector<const RemoteRC*> all;
|
2021-02-02 14:35:40 +00:00
|
|
|
|
2023-11-15 19:27:54 +00:00
|
|
|
all.reserve(known_rcs.size());
|
2023-12-01 17:19:07 +00:00
|
|
|
|
|
|
|
for (auto& entry : rc_lookup)
|
2023-10-12 13:43:07 +00:00
|
|
|
{
|
2023-11-15 19:27:54 +00:00
|
|
|
all.push_back(&entry.second);
|
2023-10-12 13:43:07 +00:00
|
|
|
}
|
2021-02-02 14:35:40 +00:00
|
|
|
|
2023-10-12 13:43:07 +00:00
|
|
|
auto it_mid = numRouters < all.size() ? all.begin() + numRouters : all.end();
|
2023-12-01 17:19:07 +00:00
|
|
|
|
2023-10-12 13:43:07 +00:00
|
|
|
std::partial_sort(
|
|
|
|
all.begin(), it_mid, all.end(), [compare = dht::XorMetric{location}](auto* a, auto* b) {
|
|
|
|
return compare(*a, *b);
|
|
|
|
});
|
2021-02-02 14:35:40 +00:00
|
|
|
|
2023-11-02 12:30:38 +00:00
|
|
|
std::vector<RemoteRC> closest;
|
2023-10-12 13:43:07 +00:00
|
|
|
closest.reserve(numRouters);
|
|
|
|
for (auto it = all.begin(); it != it_mid; ++it)
|
|
|
|
closest.push_back(**it);
|
2021-02-02 14:35:40 +00:00
|
|
|
|
2023-10-12 13:43:07 +00:00
|
|
|
return closest;
|
|
|
|
});
|
2019-03-11 13:58:31 +00:00
|
|
|
}
|
2021-02-02 14:35:40 +00:00
|
|
|
} // namespace llarp
|