2021-03-09 22:24:35 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "router_contact.hpp"
|
|
|
|
#include "router_id.hpp"
|
|
|
|
#include "util/common.hpp"
|
|
|
|
#include "util/fs.hpp"
|
|
|
|
#include "util/thread/threading.hpp"
|
|
|
|
#include "util/thread/annotations.hpp"
|
|
|
|
#include "dht/key.hpp"
|
|
|
|
#include "crypto/crypto.hpp"
|
2019-03-03 20:51:47 +00:00
|
|
|
|
2019-03-11 13:58:31 +00:00
|
|
|
#include <set>
|
2021-02-02 14:35:40 +00:00
|
|
|
#include <optional>
|
|
|
|
#include <unordered_set>
|
|
|
|
#include <unordered_map>
|
2019-07-30 23:42:13 +00:00
|
|
|
#include <utility>
|
2021-02-02 14:35:40 +00:00
|
|
|
#include <atomic>
|
2021-02-18 17:00:03 +00:00
|
|
|
#include <algorithm>
|
2019-03-11 13:58:31 +00:00
|
|
|
|
2018-12-10 14:14:55 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2021-02-02 14:35:40 +00:00
|
|
|
class NodeDB
|
2018-12-10 23:29:58 +00:00
|
|
|
{
|
2021-02-02 14:35:40 +00:00
|
|
|
struct Entry
|
|
|
|
{
|
|
|
|
const RouterContact rc;
|
|
|
|
llarp_time_t insertedAt;
|
|
|
|
explicit Entry(RouterContact rc);
|
|
|
|
};
|
|
|
|
using NodeMap = std::unordered_map<RouterID, Entry>;
|
|
|
|
|
|
|
|
NodeMap m_Entries;
|
|
|
|
|
|
|
|
const fs::path m_Root;
|
|
|
|
|
|
|
|
const std::function<void(std::function<void()>)> disk;
|
|
|
|
|
|
|
|
llarp_time_t m_NextFlushAt;
|
|
|
|
|
|
|
|
mutable util::NullMutex m_Access;
|
|
|
|
|
|
|
|
/// asynchronously remove the files for a set of rcs on disk given their public ident key
|
|
|
|
void
|
|
|
|
AsyncRemoveManyFromDisk(std::unordered_set<RouterID> idents) const;
|
|
|
|
|
|
|
|
/// get filename of an RC file given its public ident key
|
|
|
|
fs::path
|
|
|
|
GetPathForPubkey(RouterID pk) const;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit NodeDB(fs::path rootdir, std::function<void(std::function<void()>)> diskCaller);
|
|
|
|
|
2021-04-02 15:10:37 +00:00
|
|
|
/// in memory nodedb
|
|
|
|
NodeDB();
|
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
/// load all entries from disk syncrhonously
|
|
|
|
void
|
|
|
|
LoadFromDisk();
|
|
|
|
|
|
|
|
/// explicit save all RCs to disk synchronously
|
|
|
|
void
|
|
|
|
SaveToDisk() const;
|
|
|
|
|
|
|
|
/// the number of RCs that are loaded from disk
|
|
|
|
size_t
|
|
|
|
NumLoaded() const;
|
|
|
|
|
|
|
|
/// do periodic tasks like flush to disk and expiration
|
|
|
|
void
|
|
|
|
Tick(llarp_time_t now);
|
|
|
|
|
|
|
|
/// find the absolute closets router to a dht location
|
|
|
|
RouterContact
|
|
|
|
FindClosestTo(dht::Key_t location) const;
|
|
|
|
|
|
|
|
/// find many routers closest to dht key
|
|
|
|
std::vector<RouterContact>
|
|
|
|
FindManyClosestTo(dht::Key_t location, uint32_t numRouters) const;
|
|
|
|
|
|
|
|
/// return true if we have an rc by its ident pubkey
|
|
|
|
bool
|
|
|
|
Has(RouterID pk) const;
|
|
|
|
|
|
|
|
/// maybe get an rc by its ident pubkey
|
|
|
|
std::optional<RouterContact>
|
|
|
|
Get(RouterID pk) const;
|
|
|
|
|
|
|
|
template <typename Filter>
|
|
|
|
std::optional<RouterContact>
|
2021-02-03 21:38:31 +00:00
|
|
|
GetRandom(Filter visit) const
|
2021-02-02 14:35:40 +00:00
|
|
|
{
|
|
|
|
util::NullLock lock{m_Access};
|
2021-02-03 19:49:00 +00:00
|
|
|
|
2021-02-19 13:37:46 +00:00
|
|
|
std::vector<const decltype(m_Entries)::value_type*> entries;
|
2021-02-18 17:00:03 +00:00
|
|
|
for (const auto& entry : m_Entries)
|
2021-02-19 13:37:46 +00:00
|
|
|
entries.push_back(&entry);
|
2021-02-18 17:00:03 +00:00
|
|
|
|
2021-02-25 19:42:49 +00:00
|
|
|
std::shuffle(entries.begin(), entries.end(), llarp::CSRNG{});
|
2021-02-18 17:00:03 +00:00
|
|
|
|
2021-02-19 13:37:46 +00:00
|
|
|
for (const auto entry : entries)
|
2021-02-02 14:35:40 +00:00
|
|
|
{
|
2021-02-19 13:37:46 +00:00
|
|
|
if (visit(entry->second.rc))
|
|
|
|
return entry->second.rc;
|
2021-02-02 14:35:40 +00:00
|
|
|
}
|
2021-02-18 17:00:03 +00:00
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// visit all entries
|
|
|
|
template <typename Visit>
|
|
|
|
void
|
|
|
|
VisitAll(Visit visit) const
|
|
|
|
{
|
|
|
|
util::NullLock lock{m_Access};
|
|
|
|
for (const auto& item : m_Entries)
|
|
|
|
{
|
|
|
|
visit(item.second.rc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// visit all entries inserted before a timestamp
|
|
|
|
template <typename Visit>
|
|
|
|
void
|
|
|
|
VisitInsertedBefore(Visit visit, llarp_time_t insertedBefore)
|
|
|
|
{
|
|
|
|
util::NullLock lock{m_Access};
|
|
|
|
for (const auto& item : m_Entries)
|
|
|
|
{
|
|
|
|
if (item.second.insertedAt < insertedBefore)
|
|
|
|
visit(item.second.rc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// remove an entry via its ident pubkey
|
|
|
|
void
|
|
|
|
Remove(RouterID pk);
|
|
|
|
|
|
|
|
/// remove an entry given a filter that inspects the rc
|
|
|
|
template <typename Filter>
|
|
|
|
void
|
|
|
|
RemoveIf(Filter visit)
|
|
|
|
{
|
|
|
|
util::NullLock lock{m_Access};
|
|
|
|
std::unordered_set<RouterID> removed;
|
|
|
|
auto itr = m_Entries.begin();
|
|
|
|
while (itr != m_Entries.end())
|
|
|
|
{
|
|
|
|
if (visit(itr->second.rc))
|
|
|
|
{
|
|
|
|
removed.insert(itr->second.rc.pubkey);
|
|
|
|
itr = m_Entries.erase(itr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
if (not removed.empty())
|
|
|
|
AsyncRemoveManyFromDisk(std::move(removed));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// remove rcs that are not in keep and have been inserted before cutoff
|
|
|
|
void
|
|
|
|
RemoveStaleRCs(std::unordered_set<RouterID> keep, llarp_time_t cutoff);
|
|
|
|
|
|
|
|
/// put this rc into the cache if it is not there or newer than the one there already
|
|
|
|
void
|
|
|
|
PutIfNewer(RouterContact rc);
|
|
|
|
|
|
|
|
/// unconditional put of rc into cache
|
|
|
|
void
|
|
|
|
Put(RouterContact rc);
|
2019-06-10 12:47:21 +00:00
|
|
|
};
|
2021-02-02 14:35:40 +00:00
|
|
|
} // namespace llarp
|