2021-03-09 22:24:35 +00:00
|
|
|
#pragma once
|
|
|
|
|
2023-10-24 13:18:03 +00:00
|
|
|
#include "crypto/crypto.hpp"
|
|
|
|
#include "dht/key.hpp"
|
2023-10-19 21:59:57 +00:00
|
|
|
#include "router_contact.hpp"
|
|
|
|
#include "router_id.hpp"
|
|
|
|
#include "util/common.hpp"
|
|
|
|
#include "util/fs.hpp"
|
2023-10-24 13:18:03 +00:00
|
|
|
#include "util/thread/threading.hpp"
|
|
|
|
|
|
|
|
#include <llarp/router/router.hpp>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <atomic>
|
2023-12-01 17:19:07 +00:00
|
|
|
#include <map>
|
2023-10-24 13:18:03 +00:00
|
|
|
#include <optional>
|
|
|
|
#include <set>
|
|
|
|
#include <unordered_set>
|
|
|
|
#include <utility>
|
2023-10-19 21:59:57 +00:00
|
|
|
|
2018-12-10 14:14:55 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2023-10-12 13:43:07 +00:00
|
|
|
struct Router;
|
|
|
|
|
2023-12-15 13:52:31 +00:00
|
|
|
// TESTNET: the following constants have been shortened for testing purposes
|
|
|
|
|
2023-12-04 15:27:42 +00:00
|
|
|
/* RC Fetch Constants */
|
2023-12-20 19:34:55 +00:00
|
|
|
// fallback to bootstrap if we have less than this many RCs
|
2023-12-13 19:13:22 +00:00
|
|
|
inline constexpr size_t MIN_ACTIVE_RCS{6};
|
2023-12-04 15:27:42 +00:00
|
|
|
// max number of attempts we make in non-bootstrap fetch requests
|
|
|
|
inline constexpr int MAX_FETCH_ATTEMPTS{10};
|
|
|
|
// the total number of returned rcs that are held locally should be at least this
|
|
|
|
inline constexpr size_t MIN_GOOD_RC_FETCH_TOTAL{};
|
|
|
|
// the ratio of returned rcs found locally to to total returned should be above this ratio
|
|
|
|
inline constexpr double MIN_GOOD_RC_FETCH_THRESHOLD{};
|
|
|
|
|
|
|
|
/* RID Fetch Constants */
|
|
|
|
// the number of rid sources that we make rid fetch requests to
|
2023-12-20 19:34:55 +00:00
|
|
|
inline constexpr size_t RID_SOURCE_COUNT{8};
|
2023-12-04 15:27:42 +00:00
|
|
|
// upper limit on how many rid fetch requests to rid sources can fail
|
2023-12-20 19:34:55 +00:00
|
|
|
inline constexpr size_t MAX_RID_ERRORS{2};
|
2023-12-04 15:27:42 +00:00
|
|
|
// each returned rid must appear this number of times across all responses
|
2023-12-05 20:04:24 +00:00
|
|
|
inline constexpr int MIN_RID_FETCH_FREQ{RID_SOURCE_COUNT - MAX_RID_ERRORS - 1};
|
2023-12-04 15:27:42 +00:00
|
|
|
// the total number of accepted returned rids should be above this number
|
|
|
|
inline constexpr size_t MIN_GOOD_RID_FETCH_TOTAL{};
|
|
|
|
// the ratio of accepted:rejected rids must be above this ratio
|
2023-12-01 17:19:07 +00:00
|
|
|
inline constexpr double GOOD_RID_FETCH_THRESHOLD{};
|
2023-12-15 23:12:59 +00:00
|
|
|
|
2023-12-04 15:27:42 +00:00
|
|
|
/* Bootstrap Constants */
|
2023-12-15 22:52:54 +00:00
|
|
|
// the number of rc's we query the bootstrap for; service nodes pass 0, which means
|
|
|
|
// gimme all dat RCs
|
|
|
|
inline constexpr size_t SERVICE_NODE_BOOTSTRAP_SOURCE_COUNT{0};
|
|
|
|
inline constexpr size_t CLIENT_BOOTSTRAP_SOURCE_COUNT{10};
|
2023-12-04 15:27:42 +00:00
|
|
|
// the maximum number of fetch requests we make across all bootstraps
|
2023-12-01 17:19:07 +00:00
|
|
|
inline constexpr int MAX_BOOTSTRAP_FETCH_ATTEMPTS{5};
|
2023-12-04 15:27:42 +00:00
|
|
|
// if all bootstraps fail, router will trigger re-bootstrapping after this cooldown
|
|
|
|
inline constexpr auto BOOTSTRAP_COOLDOWN{1min};
|
2023-11-28 12:55:01 +00:00
|
|
|
|
2023-12-05 20:04:24 +00:00
|
|
|
/* Other Constants */
|
|
|
|
// the maximum number of RC/RID fetches that can pass w/o an unconfirmed rc/rid appearing
|
|
|
|
inline constexpr int MAX_CONFIRMATION_ATTEMPTS{5};
|
|
|
|
// threshold amount of verifications to promote an unconfirmed rc/rid
|
|
|
|
inline constexpr int CONFIRMATION_THRESHOLD{3};
|
|
|
|
|
2023-11-29 14:03:54 +00:00
|
|
|
inline constexpr auto FLUSH_INTERVAL{5min};
|
|
|
|
|
2023-12-05 20:04:24 +00:00
|
|
|
template <
|
|
|
|
typename ID_t,
|
|
|
|
std::enable_if_t<std::is_same_v<ID_t, RouterID> || std::is_same_v<ID_t, RemoteRC>, int> = 0>
|
|
|
|
struct Unconfirmed
|
|
|
|
{
|
|
|
|
const ID_t id;
|
|
|
|
int attempts = 0;
|
|
|
|
int verifications = 0;
|
|
|
|
|
|
|
|
Unconfirmed() = delete;
|
|
|
|
Unconfirmed(const ID_t& obj) : id{obj}
|
|
|
|
{}
|
|
|
|
Unconfirmed(ID_t&& obj) : id{std::move(obj)}
|
|
|
|
{}
|
|
|
|
|
|
|
|
int
|
|
|
|
strikes() const
|
|
|
|
{
|
|
|
|
return attempts;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator bool() const
|
|
|
|
{
|
|
|
|
return verifications == CONFIRMATION_THRESHOLD;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator==(const Unconfirmed& other) const
|
|
|
|
{
|
|
|
|
return id == other.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator<(const Unconfirmed& other) const
|
|
|
|
{
|
|
|
|
return id < other.id;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
class NodeDB
|
2018-12-10 23:29:58 +00:00
|
|
|
{
|
2023-11-28 12:55:01 +00:00
|
|
|
Router& _router;
|
|
|
|
const fs::path _root;
|
|
|
|
const std::function<void(std::function<void()>)> _disk;
|
2021-02-02 14:35:40 +00:00
|
|
|
|
2023-11-28 12:55:01 +00:00
|
|
|
llarp_time_t _next_flush_time;
|
2021-02-02 14:35:40 +00:00
|
|
|
|
2023-11-29 14:03:54 +00:00
|
|
|
/******** RouterID/RouterContacts ********/
|
2021-02-02 14:35:40 +00:00
|
|
|
|
2023-11-29 14:03:54 +00:00
|
|
|
/** RouterID mappings
|
|
|
|
Both the following are populated in NodeDB startup with RouterID's stored on disk.
|
2023-12-01 17:19:07 +00:00
|
|
|
- known_rids: meant to persist between lokinet sessions, and is only
|
2023-11-29 14:03:54 +00:00
|
|
|
populated during startup and RouterID fetching. This is meant to represent the
|
2023-12-01 17:19:07 +00:00
|
|
|
client instance's most recent perspective of the network, and record which RouterID's
|
|
|
|
were recently "active" and connected to
|
2023-12-05 20:04:24 +00:00
|
|
|
- unconfirmed_rids: holds new rids returned in fetch requests to be verified by subsequent
|
|
|
|
fetch requests
|
2023-11-29 14:03:54 +00:00
|
|
|
- known_rcs: populated during startup and when RC's are updated both during gossip
|
|
|
|
and periodic RC fetching
|
2023-12-05 20:04:24 +00:00
|
|
|
- unconfirmed_rcs: holds new rcs to be verified by subsequent fetch requests, similar to
|
|
|
|
the unknown_rids container
|
2023-12-01 17:19:07 +00:00
|
|
|
- rc_lookup: holds all the same rc's as known_rcs, but can be used to look them up by
|
2023-12-05 20:04:24 +00:00
|
|
|
their rid
|
2023-12-12 15:45:38 +00:00
|
|
|
- bootstrap_seeds: if we are the seed node, we insert the rc's of bootstrap fetch requests
|
|
|
|
senders into this container to "introduce" them to each other
|
|
|
|
- _bootstraps: the standard container for bootstrap RemoteRCs
|
2023-11-29 14:03:54 +00:00
|
|
|
*/
|
2023-12-05 20:04:24 +00:00
|
|
|
std::set<RouterID> known_rids;
|
|
|
|
std::set<Unconfirmed<RouterID>> unconfirmed_rids;
|
|
|
|
|
|
|
|
std::set<RemoteRC> known_rcs;
|
|
|
|
std::set<Unconfirmed<RemoteRC>> unconfirmed_rcs;
|
|
|
|
|
|
|
|
std::map<RouterID, const RemoteRC&> rc_lookup;
|
2023-11-15 02:53:19 +00:00
|
|
|
|
2023-12-12 15:45:38 +00:00
|
|
|
BootstrapList _bootstraps{};
|
|
|
|
|
2023-12-06 19:34:37 +00:00
|
|
|
/** RouterID lists // TODO: get rid of all these, replace with better decom/not staked sets
|
2023-11-29 14:03:54 +00:00
|
|
|
- white: active routers
|
|
|
|
- gray: fully funded, but decommissioned routers
|
|
|
|
- green: registered, but not fully-staked routers
|
|
|
|
*/
|
2023-12-18 22:15:45 +00:00
|
|
|
std::set<RouterID> _router_whitelist{};
|
|
|
|
std::set<RouterID> _router_greylist{};
|
|
|
|
std::set<RouterID> _router_greenlist{};
|
2023-11-29 14:03:54 +00:00
|
|
|
|
|
|
|
// All registered relays (service nodes)
|
2023-12-15 21:47:14 +00:00
|
|
|
std::set<RouterID> _registered_routers;
|
2023-12-01 17:19:07 +00:00
|
|
|
// timing (note: Router holds the variables for last rc and rid request times)
|
2023-11-17 07:41:42 +00:00
|
|
|
std::unordered_map<RouterID, rc_time> last_rc_update_times;
|
2023-12-01 17:19:07 +00:00
|
|
|
// if populated from a config file, lists specific exclusively used as path first-hops
|
2023-12-05 20:04:24 +00:00
|
|
|
std::set<RouterID> _pinned_edges;
|
2023-11-29 14:03:54 +00:00
|
|
|
// source of "truth" for RC updating. This relay will also mediate requests to the
|
|
|
|
// 12 selected active RID's for RID fetching
|
2023-11-28 20:05:07 +00:00
|
|
|
RouterID fetch_source;
|
2023-11-30 21:53:41 +00:00
|
|
|
// set of 12 randomly selected RID's from the client's set of routers
|
2023-12-05 20:04:24 +00:00
|
|
|
std::set<RouterID> rid_sources{};
|
2023-11-29 14:03:54 +00:00
|
|
|
// logs the RID's that resulted in an error during RID fetching
|
2023-12-05 20:04:24 +00:00
|
|
|
std::set<RouterID> fail_sources{};
|
2023-12-01 17:19:07 +00:00
|
|
|
// tracks the number of times each rid appears in the above responses
|
|
|
|
std::unordered_map<RouterID, int> fetch_counters{};
|
|
|
|
|
2023-11-30 21:53:41 +00:00
|
|
|
/** Failure counters:
|
|
|
|
- fetch_failures: tracks errors fetching RC's from the RC node and requesting RID's
|
|
|
|
from the 12 RID sources. Errors in the individual RID sets are NOT counted towards
|
|
|
|
this, their performance as a group is evaluated wholistically
|
|
|
|
- bootstrap_failures: tracks errors fetching both RC's from bootstrasps and RID requests
|
|
|
|
they mediate. This is a different counter as we only bootstrap in problematic cases
|
|
|
|
*/
|
2023-12-11 18:17:46 +00:00
|
|
|
std::atomic<int> fetch_failures{0}, bootstrap_attempts{0};
|
2023-11-28 20:05:07 +00:00
|
|
|
|
2023-12-04 15:27:42 +00:00
|
|
|
std::atomic<bool> _using_bootstrap_fallback{false}, _needs_rebootstrap{false},
|
2023-12-08 01:07:32 +00:00
|
|
|
_needs_initial_fetch{true}, _initial_completed{false};
|
2023-11-17 07:41:42 +00:00
|
|
|
|
2023-11-15 04:55:38 +00:00
|
|
|
bool
|
|
|
|
want_rc(const RouterID& rid) const;
|
|
|
|
|
2023-11-29 14:03:54 +00:00
|
|
|
/// asynchronously remove the files for a set of rcs on disk given their public ident key
|
|
|
|
void
|
|
|
|
remove_many_from_disk_async(std::unordered_set<RouterID> idents) const;
|
|
|
|
|
|
|
|
/// get filename of an RC file given its public ident key
|
|
|
|
fs::path
|
2023-12-15 23:11:56 +00:00
|
|
|
get_path_by_pubkey(const RouterID& pk) const;
|
2023-11-29 14:03:54 +00:00
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
public:
|
2023-11-30 21:53:41 +00:00
|
|
|
explicit NodeDB(
|
|
|
|
fs::path rootdir, std::function<void(std::function<void()>)> diskCaller, Router* r);
|
2023-11-17 07:41:42 +00:00
|
|
|
|
2023-11-30 21:53:41 +00:00
|
|
|
/// in memory nodedb
|
|
|
|
NodeDB();
|
2023-11-28 12:55:01 +00:00
|
|
|
|
2023-12-06 19:34:37 +00:00
|
|
|
const std::set<RouterID>&
|
|
|
|
get_known_rids() const
|
|
|
|
{
|
|
|
|
return known_rids;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::set<RemoteRC>&
|
|
|
|
get_known_rcs() const
|
|
|
|
{
|
|
|
|
return known_rcs;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<RemoteRC>
|
|
|
|
get_rc_by_rid(const RouterID& rid);
|
|
|
|
|
2023-11-28 20:05:07 +00:00
|
|
|
bool
|
2023-12-04 15:27:42 +00:00
|
|
|
needs_initial_fetch() const
|
|
|
|
{
|
|
|
|
return _needs_initial_fetch;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
needs_rebootstrap() const
|
|
|
|
{
|
|
|
|
return _needs_rebootstrap;
|
|
|
|
}
|
|
|
|
|
2023-12-12 15:45:38 +00:00
|
|
|
void
|
|
|
|
ingest_bootstrap_seed();
|
|
|
|
|
2023-12-04 15:27:42 +00:00
|
|
|
bool
|
2023-12-15 17:45:00 +00:00
|
|
|
ingest_fetched_rcs(std::set<RemoteRC> rcs);
|
2023-12-04 15:27:42 +00:00
|
|
|
|
|
|
|
bool
|
2023-12-05 20:04:24 +00:00
|
|
|
process_fetched_rcs(std::set<RemoteRC>& rcs);
|
2023-11-17 07:41:42 +00:00
|
|
|
|
|
|
|
void
|
2023-12-05 20:04:24 +00:00
|
|
|
ingest_rid_fetch_responses(const RouterID& source, std::set<RouterID> ids = {});
|
2023-11-28 12:55:01 +00:00
|
|
|
|
2023-11-28 18:14:19 +00:00
|
|
|
bool
|
|
|
|
process_fetched_rids();
|
2023-11-28 12:55:01 +00:00
|
|
|
|
2023-11-28 20:05:07 +00:00
|
|
|
void
|
2023-12-15 13:52:31 +00:00
|
|
|
fetch_initial(bool is_snode = false);
|
2023-11-28 20:05:07 +00:00
|
|
|
|
2023-11-30 21:53:41 +00:00
|
|
|
// RouterContact fetching
|
2023-11-28 20:05:07 +00:00
|
|
|
void
|
2023-11-30 21:53:41 +00:00
|
|
|
fetch_rcs(bool initial = false);
|
2023-11-28 20:05:07 +00:00
|
|
|
void
|
2023-11-30 21:53:41 +00:00
|
|
|
post_fetch_rcs(bool initial = false);
|
2023-11-28 20:05:07 +00:00
|
|
|
void
|
2023-11-30 21:53:41 +00:00
|
|
|
fetch_rcs_result(bool initial = false, bool error = false);
|
2023-11-28 20:05:07 +00:00
|
|
|
|
2023-11-30 21:53:41 +00:00
|
|
|
// RouterID fetching
|
2023-11-28 20:05:07 +00:00
|
|
|
void
|
2023-11-30 21:53:41 +00:00
|
|
|
fetch_rids(bool initial = false);
|
2023-11-17 07:41:42 +00:00
|
|
|
void
|
2023-11-30 21:53:41 +00:00
|
|
|
post_fetch_rids(bool initial = false);
|
|
|
|
void
|
|
|
|
fetch_rids_result(bool initial = false);
|
2023-11-17 07:41:42 +00:00
|
|
|
|
2023-12-05 20:04:24 +00:00
|
|
|
// Bootstrap fallback fetching
|
2023-11-25 00:40:51 +00:00
|
|
|
void
|
2023-11-30 21:53:41 +00:00
|
|
|
fallback_to_bootstrap();
|
2023-12-04 15:27:42 +00:00
|
|
|
void
|
2023-12-15 13:52:31 +00:00
|
|
|
post_snode_bootstrap();
|
|
|
|
void
|
2023-12-04 15:27:42 +00:00
|
|
|
bootstrap_cooldown();
|
2023-11-25 00:40:51 +00:00
|
|
|
|
2023-12-01 17:19:07 +00:00
|
|
|
// Populate rid_sources with random sample from known_rids. A set of rids is passed
|
|
|
|
// if only specific RID's need to be re-selected; to re-select all, pass the member
|
|
|
|
// variable ::known_rids
|
2023-11-25 00:40:51 +00:00
|
|
|
void
|
2023-12-05 20:04:24 +00:00
|
|
|
reselect_router_id_sources(std::set<RouterID> specific);
|
2023-11-30 21:53:41 +00:00
|
|
|
|
2023-11-15 02:53:19 +00:00
|
|
|
void
|
|
|
|
set_router_whitelist(
|
|
|
|
const std::vector<RouterID>& whitelist,
|
|
|
|
const std::vector<RouterID>& greylist,
|
|
|
|
const std::vector<RouterID>& greenlist);
|
|
|
|
|
|
|
|
std::optional<RouterID>
|
|
|
|
get_random_whitelist_router() const;
|
|
|
|
|
|
|
|
// client:
|
|
|
|
// if pinned edges were specified, connections are allowed only to those and
|
|
|
|
// to the configured bootstrap nodes. otherwise, always allow.
|
|
|
|
//
|
|
|
|
// relay:
|
|
|
|
// outgoing connections are allowed only to other registered, funded relays
|
|
|
|
// (whitelist and greylist, respectively).
|
|
|
|
bool
|
|
|
|
is_connection_allowed(const RouterID& remote) const;
|
|
|
|
|
|
|
|
// client:
|
|
|
|
// same as is_connection_allowed
|
|
|
|
//
|
|
|
|
// server:
|
|
|
|
// we only build new paths through registered, not decommissioned relays
|
|
|
|
// (i.e. whitelist)
|
|
|
|
bool
|
|
|
|
is_path_allowed(const RouterID& remote) const
|
|
|
|
{
|
2023-12-06 19:34:37 +00:00
|
|
|
return known_rids.count(remote);
|
2023-11-15 02:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if pinned edges were specified, the remote must be in that set, else any remote
|
|
|
|
// is allowed as first hop.
|
|
|
|
bool
|
|
|
|
is_first_hop_allowed(const RouterID& remote) const;
|
|
|
|
|
2023-12-05 20:04:24 +00:00
|
|
|
std::set<RouterID>&
|
2023-11-30 21:53:41 +00:00
|
|
|
pinned_edges()
|
|
|
|
{
|
|
|
|
return _pinned_edges;
|
|
|
|
}
|
|
|
|
|
2023-12-12 15:45:38 +00:00
|
|
|
void
|
|
|
|
store_bootstraps();
|
|
|
|
|
2023-12-06 21:54:51 +00:00
|
|
|
size_t
|
|
|
|
num_bootstraps() const
|
|
|
|
{
|
2023-12-12 15:45:38 +00:00
|
|
|
return _bootstraps.size();
|
2023-12-06 21:54:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
has_bootstraps() const
|
|
|
|
{
|
2023-12-12 15:45:38 +00:00
|
|
|
return _bootstraps.empty();
|
2023-12-06 21:54:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const BootstrapList&
|
|
|
|
bootstrap_list() const
|
|
|
|
{
|
2023-12-12 15:45:38 +00:00
|
|
|
return _bootstraps;
|
2023-12-06 21:54:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BootstrapList&
|
2023-11-30 21:53:41 +00:00
|
|
|
bootstrap_list()
|
|
|
|
{
|
2023-12-12 15:45:38 +00:00
|
|
|
return _bootstraps;
|
|
|
|
}
|
|
|
|
|
2023-11-30 21:53:41 +00:00
|
|
|
void
|
2023-12-12 17:01:02 +00:00
|
|
|
set_bootstrap_routers(BootstrapList& from_router);
|
2023-11-30 21:53:41 +00:00
|
|
|
|
2023-12-06 19:34:37 +00:00
|
|
|
const std::set<RouterID>&
|
2023-11-30 21:53:41 +00:00
|
|
|
whitelist() const
|
2023-11-15 02:53:19 +00:00
|
|
|
{
|
2023-12-18 22:15:45 +00:00
|
|
|
return _router_whitelist;
|
2023-11-15 02:53:19 +00:00
|
|
|
}
|
|
|
|
|
2023-12-08 01:07:32 +00:00
|
|
|
const std::set<RouterID>&
|
2023-11-30 21:53:41 +00:00
|
|
|
greylist() const
|
|
|
|
{
|
2023-12-18 22:15:45 +00:00
|
|
|
return _router_greylist;
|
2023-11-30 21:53:41 +00:00
|
|
|
}
|
2021-02-02 14:35:40 +00:00
|
|
|
|
2023-12-15 21:47:14 +00:00
|
|
|
std::set<RouterID>&
|
|
|
|
registered_routers()
|
|
|
|
{
|
|
|
|
return _registered_routers;
|
|
|
|
}
|
|
|
|
|
2023-12-05 20:04:24 +00:00
|
|
|
const std::set<RouterID>&
|
2023-12-15 21:47:14 +00:00
|
|
|
registered_routers() const
|
2023-11-30 21:53:41 +00:00
|
|
|
{
|
2023-12-15 21:47:14 +00:00
|
|
|
return _registered_routers;
|
2023-11-30 21:53:41 +00:00
|
|
|
}
|
|
|
|
|
2023-12-05 20:04:24 +00:00
|
|
|
const std::set<RemoteRC>&
|
2023-11-30 21:53:41 +00:00
|
|
|
get_rcs() const
|
|
|
|
{
|
|
|
|
return known_rcs;
|
|
|
|
}
|
|
|
|
|
2023-12-01 17:19:07 +00:00
|
|
|
// const std::unordered_map<RouterID, RemoteRC>&
|
|
|
|
// get_rcs() const
|
|
|
|
// {
|
|
|
|
// return known_rcs;
|
|
|
|
// }
|
|
|
|
|
2023-11-30 21:53:41 +00:00
|
|
|
const std::unordered_map<RouterID, rc_time>&
|
|
|
|
get_last_rc_update_times() const
|
|
|
|
{
|
|
|
|
return last_rc_update_times;
|
|
|
|
}
|
2021-04-02 15:10:37 +00:00
|
|
|
|
2023-11-15 19:27:54 +00:00
|
|
|
/// load all known_rcs from disk syncrhonously
|
2021-02-02 14:35:40 +00:00
|
|
|
void
|
2023-10-12 13:43:07 +00:00
|
|
|
load_from_disk();
|
2021-02-02 14:35:40 +00:00
|
|
|
|
|
|
|
/// explicit save all RCs to disk synchronously
|
|
|
|
void
|
2023-10-12 13:43:07 +00:00
|
|
|
save_to_disk() const;
|
2021-02-02 14:35:40 +00:00
|
|
|
|
|
|
|
/// the number of RCs that are loaded from disk
|
|
|
|
size_t
|
2023-12-06 21:54:51 +00:00
|
|
|
num_rcs() const;
|
2021-02-02 14:35:40 +00:00
|
|
|
|
2023-12-13 17:41:56 +00:00
|
|
|
size_t
|
|
|
|
num_rids() const;
|
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
/// do periodic tasks like flush to disk and expiration
|
|
|
|
void
|
|
|
|
Tick(llarp_time_t now);
|
|
|
|
|
|
|
|
/// find the absolute closets router to a dht location
|
2023-11-02 12:30:38 +00:00
|
|
|
RemoteRC
|
2023-10-12 13:43:07 +00:00
|
|
|
find_closest_to(dht::Key_t location) const;
|
2021-02-02 14:35:40 +00:00
|
|
|
|
|
|
|
/// find many routers closest to dht key
|
2023-11-02 12:30:38 +00:00
|
|
|
std::vector<RemoteRC>
|
2023-10-12 13:43:07 +00:00
|
|
|
find_many_closest_to(dht::Key_t location, uint32_t numRouters) const;
|
2021-02-02 14:35:40 +00:00
|
|
|
|
|
|
|
/// return true if we have an rc by its ident pubkey
|
|
|
|
bool
|
2023-12-15 16:43:58 +00:00
|
|
|
has_rc(const RouterID& pk) const;
|
|
|
|
|
|
|
|
bool
|
|
|
|
has_rc(const RemoteRC& rc) const;
|
2021-02-02 14:35:40 +00:00
|
|
|
|
|
|
|
/// maybe get an rc by its ident pubkey
|
2023-11-02 12:30:38 +00:00
|
|
|
std::optional<RemoteRC>
|
2023-12-15 17:45:00 +00:00
|
|
|
get_rc(const RouterID& pk) const;
|
|
|
|
|
2023-12-04 15:27:42 +00:00
|
|
|
std::optional<RemoteRC>
|
|
|
|
get_random_rc() const;
|
|
|
|
|
2023-12-15 23:50:32 +00:00
|
|
|
// Get `n` random RCs from all RCs we know about. If `exact` is true then we require n matches
|
|
|
|
// (and otherwise return nullopt); otherwise we return whatever we found, or nullopt if we find
|
|
|
|
// nothing at all.
|
2023-12-04 15:27:42 +00:00
|
|
|
std::optional<std::vector<RemoteRC>>
|
2023-12-15 23:50:32 +00:00
|
|
|
get_n_random_rcs(size_t n, bool exact = false) const;
|
2023-12-04 15:27:42 +00:00
|
|
|
|
|
|
|
/** The following random conditional functions utilize a simple implementation of reservoir
|
|
|
|
sampling to return either 1 or n random RC's using only one pass through the set of RC's.
|
|
|
|
|
|
|
|
Pseudocode:
|
|
|
|
- begin iterating through the set
|
|
|
|
- load the first n (or 1) that pass hook(n) into a list Selected[]
|
|
|
|
- for all that pass the hook, increment i, tracking the number seen thus far
|
|
|
|
- generate a random integer x from 0 to i
|
|
|
|
- x < n ? Selected[x] = current : continue;
|
|
|
|
*/
|
|
|
|
|
|
|
|
std::optional<RemoteRC>
|
|
|
|
get_random_rc_conditional(std::function<bool(RemoteRC)> hook) const;
|
|
|
|
|
|
|
|
std::optional<std::vector<RemoteRC>>
|
2023-12-16 00:04:09 +00:00
|
|
|
get_n_random_rcs_conditional(
|
|
|
|
size_t n, std::function<bool(RemoteRC)> hook, bool exact = false) const;
|
2023-12-04 15:27:42 +00:00
|
|
|
|
2023-12-01 17:19:07 +00:00
|
|
|
// Updates `current` to not contain any of the elements of `replace` and resamples (up to
|
|
|
|
// `target_size`) from population to refill it.
|
|
|
|
template <typename T, typename RNG>
|
|
|
|
void
|
|
|
|
replace_subset(
|
2023-12-05 20:04:24 +00:00
|
|
|
std::set<T>& current,
|
|
|
|
const std::set<T>& replace,
|
|
|
|
std::set<T> population,
|
2023-12-01 17:19:07 +00:00
|
|
|
size_t target_size,
|
|
|
|
RNG&& rng)
|
|
|
|
{
|
|
|
|
// Remove the ones we are replacing from current:
|
|
|
|
current.erase(replace.begin(), replace.end());
|
|
|
|
|
|
|
|
// Remove ones we are replacing, and ones we already have, from the population so that we
|
|
|
|
// won't reselect them:
|
|
|
|
population.erase(replace.begin(), replace.end());
|
|
|
|
population.erase(current.begin(), current.end());
|
|
|
|
|
|
|
|
if (current.size() < target_size)
|
|
|
|
std::sample(
|
|
|
|
population.begin(),
|
|
|
|
population.end(),
|
|
|
|
std::inserter(current, current.end()),
|
|
|
|
target_size - current.size(),
|
|
|
|
rng);
|
|
|
|
}
|
|
|
|
|
2023-11-15 19:27:54 +00:00
|
|
|
/// visit all known_rcs
|
2021-02-02 14:35:40 +00:00
|
|
|
template <typename Visit>
|
|
|
|
void
|
2023-12-19 11:31:26 +00:00
|
|
|
visit_all(Visit visit) const
|
2021-02-02 14:35:40 +00:00
|
|
|
{
|
2023-11-28 12:55:01 +00:00
|
|
|
_router.loop()->call([this, visit]() {
|
2023-11-15 19:27:54 +00:00
|
|
|
for (const auto& item : known_rcs)
|
2023-12-01 17:19:07 +00:00
|
|
|
visit(item);
|
2023-10-12 13:43:07 +00:00
|
|
|
});
|
2021-02-02 14:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// remove an entry via its ident pubkey
|
|
|
|
void
|
2023-10-12 13:43:07 +00:00
|
|
|
remove_router(RouterID pk);
|
2021-02-02 14:35:40 +00:00
|
|
|
|
|
|
|
/// remove an entry given a filter that inspects the rc
|
|
|
|
template <typename Filter>
|
|
|
|
void
|
2023-12-19 11:31:26 +00:00
|
|
|
remove_if(Filter visit)
|
2021-02-02 14:35:40 +00:00
|
|
|
{
|
2023-11-28 12:55:01 +00:00
|
|
|
_router.loop()->call([this, visit]() {
|
2023-10-12 13:43:07 +00:00
|
|
|
std::unordered_set<RouterID> removed;
|
2023-12-01 17:19:07 +00:00
|
|
|
|
|
|
|
for (auto itr = rc_lookup.begin(); itr != rc_lookup.end();)
|
2021-02-02 14:35:40 +00:00
|
|
|
{
|
2023-11-15 19:27:54 +00:00
|
|
|
if (visit(itr->second))
|
2023-10-12 13:43:07 +00:00
|
|
|
{
|
2023-12-01 17:19:07 +00:00
|
|
|
removed.insert(itr->first);
|
2023-12-04 15:27:42 +00:00
|
|
|
known_rcs.erase(itr->second);
|
2023-12-01 17:19:07 +00:00
|
|
|
itr = rc_lookup.erase(itr);
|
2023-10-12 13:43:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
++itr;
|
2021-02-02 14:35:40 +00:00
|
|
|
}
|
2023-12-01 17:19:07 +00:00
|
|
|
|
2023-10-12 13:43:07 +00:00
|
|
|
if (not removed.empty())
|
|
|
|
remove_many_from_disk_async(std::move(removed));
|
|
|
|
});
|
2021-02-02 14:35:40 +00:00
|
|
|
}
|
|
|
|
|
2023-12-05 20:04:24 +00:00
|
|
|
template <
|
|
|
|
typename ID_t,
|
|
|
|
std::enable_if_t<std::is_same_v<ID_t, RouterID> || std::is_same_v<ID_t, RemoteRC>, int> = 0>
|
|
|
|
void
|
|
|
|
process_results(
|
|
|
|
std::set<ID_t> unconfirmed, std::set<Unconfirmed<ID_t>>& container, std::set<ID_t>& known)
|
|
|
|
{
|
|
|
|
// before we add the unconfirmed set, we check to see if our local set of unconfirmed
|
|
|
|
// rcs/rids appeared in the latest unconfirmed set; if so, we will increment their number
|
|
|
|
// of verifications and reset the attempts counter. Once appearing in 3 different requests,
|
|
|
|
// the rc/rid will be "verified" and promoted to the known_{rcs,rids} container
|
|
|
|
for (auto itr = container.begin(); itr != container.end();)
|
|
|
|
{
|
|
|
|
auto& id = itr->id;
|
|
|
|
auto& count = const_cast<int&>(itr->attempts);
|
|
|
|
auto& verifications = const_cast<int&>(itr->verifications);
|
|
|
|
|
|
|
|
if (auto found = unconfirmed.find(id); found != unconfirmed.end())
|
|
|
|
{
|
|
|
|
if (++verifications >= CONFIRMATION_THRESHOLD)
|
|
|
|
{
|
|
|
|
if constexpr (std::is_same_v<ID_t, RemoteRC>)
|
|
|
|
put_rc_if_newer(id);
|
|
|
|
else
|
|
|
|
known.emplace(id);
|
|
|
|
itr = container.erase(itr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// reset attempt counter and continue
|
|
|
|
count = 0;
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
|
|
|
|
unconfirmed.erase(found);
|
|
|
|
}
|
|
|
|
|
|
|
|
itr = (++count >= MAX_CONFIRMATION_ATTEMPTS) ? container.erase(itr) : ++itr;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& id : unconfirmed)
|
|
|
|
{
|
|
|
|
container.emplace(std::move(id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-27 16:31:43 +00:00
|
|
|
/// remove rcs that are older than we want to keep. For relays, this is when
|
|
|
|
/// they become "outdated" (i.e. 12hrs). Clients will hang on to them until
|
|
|
|
/// they are fully "expired" (i.e. 30 days), as the client may go offline for
|
|
|
|
/// some time and can still try to use those RCs to re-learn the network.
|
2021-02-02 14:35:40 +00:00
|
|
|
void
|
2023-11-27 16:31:43 +00:00
|
|
|
remove_stale_rcs();
|
2021-02-02 14:35:40 +00:00
|
|
|
|
2023-11-15 19:27:54 +00:00
|
|
|
/// put (or replace) the RC if we consider it valid (want_rc). returns true if put.
|
|
|
|
bool
|
2023-11-28 12:55:01 +00:00
|
|
|
put_rc(RemoteRC rc, rc_time now = time_point_now());
|
2023-11-15 19:27:54 +00:00
|
|
|
|
2023-11-15 04:55:38 +00:00
|
|
|
/// 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
|
2023-12-15 17:45:00 +00:00
|
|
|
put_rc_if_newer(RemoteRC rc);
|
2023-12-15 16:43:58 +00:00
|
|
|
|
2023-12-20 20:35:15 +00:00
|
|
|
void
|
|
|
|
verify_gossip_bfetch_rc(const RemoteRC& rc);
|
|
|
|
|
2023-12-15 16:43:58 +00:00
|
|
|
bool
|
|
|
|
verify_store_gossip_rc(const RemoteRC& rc);
|
2019-06-10 12:47:21 +00:00
|
|
|
};
|
2021-02-02 14:35:40 +00:00
|
|
|
} // namespace llarp
|
2023-12-05 20:04:24 +00:00
|
|
|
|
|
|
|
namespace std
|
|
|
|
{
|
|
|
|
template <>
|
|
|
|
struct hash<llarp::Unconfirmed<llarp::RemoteRC>> : public hash<llarp::RemoteRC>
|
|
|
|
{};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct hash<llarp::Unconfirmed<llarp::RouterID>> : hash<llarp::RouterID>
|
|
|
|
{};
|
|
|
|
} // namespace std
|