2021-03-09 22:24:35 +00:00
|
|
|
#include "nodedb.hpp"
|
2019-01-10 19:41:51 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include "crypto/crypto.hpp"
|
|
|
|
#include "crypto/types.hpp"
|
|
|
|
#include "router_contact.hpp"
|
|
|
|
#include "util/buffer.hpp"
|
|
|
|
#include "util/fs.hpp"
|
2022-07-16 00:41:14 +00:00
|
|
|
#include "util/logging.hpp"
|
|
|
|
#include "util/time.hpp"
|
2021-03-09 22:24:35 +00:00
|
|
|
#include "util/mem.hpp"
|
|
|
|
#include "util/str.hpp"
|
|
|
|
#include "dht/kademlia.hpp"
|
2018-05-31 13:08:06 +00:00
|
|
|
|
2020-02-13 22:19:12 +00:00
|
|
|
#include <algorithm>
|
2018-06-10 14:05:48 +00:00
|
|
|
#include <unordered_map>
|
2019-07-30 23:42:13 +00:00
|
|
|
#include <utility>
|
2018-05-30 00:40:02 +00:00
|
|
|
|
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
|
|
|
{
|
2022-09-22 16:22:43 +00:00
|
|
|
static auto logcat = log::Cat("nodedb");
|
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
NodeDB::Entry::Entry(RouterContact value) : rc(std::move(value)), insertedAt(llarp::time_now_ms())
|
|
|
|
{}
|
2018-04-08 12:18:16 +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
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
constexpr auto FlushInterval = 5min;
|
2018-06-13 12:58:51 +00:00
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
NodeDB::NodeDB(fs::path root, std::function<void(std::function<void()>)> diskCaller)
|
|
|
|
: m_Root{std::move(root)}
|
|
|
|
, disk(std::move(diskCaller))
|
|
|
|
, m_NextFlushAt{time_now_ms() + FlushInterval}
|
2018-06-13 11:37:44 +00:00
|
|
|
{
|
2021-02-02 14:35:40 +00:00
|
|
|
EnsureSkiplist(m_Root);
|
2018-05-30 00:40:02 +00:00
|
|
|
}
|
2021-04-02 15:10:37 +00:00
|
|
|
NodeDB::NodeDB() : m_Root{}, disk{[](auto) {}}, m_NextFlushAt{0s}
|
|
|
|
{}
|
2018-05-30 00:40:02 +00:00
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
void
|
|
|
|
NodeDB::Tick(llarp_time_t now)
|
2019-06-17 14:23:38 +00:00
|
|
|
{
|
2021-04-02 15:10:37 +00:00
|
|
|
if (m_NextFlushAt == 0s)
|
|
|
|
return;
|
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
if (now > m_NextFlushAt)
|
|
|
|
{
|
|
|
|
m_NextFlushAt += FlushInterval;
|
|
|
|
// make copy of all rcs
|
|
|
|
std::vector<RouterContact> copy;
|
|
|
|
for (const auto& item : m_Entries)
|
|
|
|
copy.push_back(item.second.rc);
|
|
|
|
// flush them to disk in one big job
|
|
|
|
// TODO: split this up? idk maybe some day...
|
|
|
|
disk([this, data = std::move(copy)]() {
|
|
|
|
for (const auto& rc : data)
|
|
|
|
{
|
|
|
|
rc.Write(GetPathForPubkey(rc.pubkey));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-06-17 14:23:38 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
fs::path
|
|
|
|
NodeDB::GetPathForPubkey(RouterID pubkey) const
|
|
|
|
{
|
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;
|
|
|
|
return m_Root / skiplistDir / fname;
|
2018-04-08 12:18:16 +00:00
|
|
|
}
|
2021-02-02 14:35:40 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
NodeDB::LoadFromDisk()
|
2018-05-30 00:40:02 +00:00
|
|
|
{
|
2021-04-02 15:10:37 +00:00
|
|
|
if (m_Root.empty())
|
|
|
|
return;
|
2022-07-26 15:05:18 +00:00
|
|
|
std::set<fs::path> purge;
|
2021-04-02 15:10:37 +00:00
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
for (const char& ch : skiplist_subdirs)
|
|
|
|
{
|
|
|
|
if (!ch)
|
|
|
|
continue;
|
|
|
|
std::string p;
|
|
|
|
p += ch;
|
|
|
|
fs::path sub = m_Root / p;
|
|
|
|
|
|
|
|
llarp::util::IterDir(sub, [&](const fs::path& f) -> bool {
|
2022-07-26 15:05:18 +00:00
|
|
|
// skip files that are not suffixed with .signed
|
|
|
|
if (not(fs::is_regular_file(f) and f.extension() == RC_FILE_EXT))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
RouterContact rc{};
|
|
|
|
|
|
|
|
if (not rc.Read(f))
|
|
|
|
{
|
|
|
|
// try loading it, purge it if it is junk
|
|
|
|
purge.emplace(f);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (not rc.FromOurNetwork())
|
|
|
|
{
|
|
|
|
// skip entries that are not from our network
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc.IsExpired(time_now_ms()))
|
2021-02-02 14:35:40 +00:00
|
|
|
{
|
2022-07-26 15:05:18 +00:00
|
|
|
// rc expired dont load it and purge it later
|
|
|
|
purge.emplace(f);
|
|
|
|
return true;
|
2021-02-02 14:35:40 +00:00
|
|
|
}
|
2022-07-26 15:05:18 +00:00
|
|
|
|
|
|
|
// validate signature and purge entries with invalid signatures
|
|
|
|
// load ones with valid signatures
|
|
|
|
if (rc.VerifySignature())
|
|
|
|
m_Entries.emplace(rc.pubkey, rc);
|
|
|
|
else
|
|
|
|
purge.emplace(f);
|
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
2022-07-26 15:05:18 +00:00
|
|
|
|
|
|
|
if (not purge.empty())
|
|
|
|
{
|
2022-09-22 16:22:43 +00:00
|
|
|
log::warning(logcat, "removing {} invalid RCs from disk", purge.size());
|
2022-07-26 15:05:18 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
NodeDB::SaveToDisk() const
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2021-04-02 15:10:37 +00:00
|
|
|
if (m_Root.empty())
|
|
|
|
return;
|
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
for (const auto& item : m_Entries)
|
|
|
|
{
|
|
|
|
item.second.rc.Write(GetPathForPubkey(item.first));
|
|
|
|
}
|
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
|
|
|
|
NodeDB::Has(RouterID pk) const
|
2018-09-11 15:53:54 +00:00
|
|
|
{
|
2021-02-02 14:35:40 +00:00
|
|
|
util::NullLock lock{m_Access};
|
|
|
|
return m_Entries.find(pk) != m_Entries.end();
|
2018-09-11 15:53:54 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
std::optional<RouterContact>
|
|
|
|
NodeDB::Get(RouterID pk) const
|
2018-06-25 15:12:08 +00:00
|
|
|
{
|
2021-02-02 14:35:40 +00:00
|
|
|
util::NullLock lock{m_Access};
|
|
|
|
const auto itr = m_Entries.find(pk);
|
|
|
|
if (itr == m_Entries.end())
|
|
|
|
return std::nullopt;
|
|
|
|
return itr->second.rc;
|
2018-06-21 09:33:23 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
void
|
|
|
|
NodeDB::Remove(RouterID pk)
|
2020-06-11 11:44:02 +00:00
|
|
|
{
|
2021-02-02 14:35:40 +00:00
|
|
|
util::NullLock lock{m_Access};
|
|
|
|
m_Entries.erase(pk);
|
|
|
|
AsyncRemoveManyFromDisk({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
|
|
|
|
NodeDB::RemoveStaleRCs(std::unordered_set<RouterID> keep, llarp_time_t cutoff)
|
2018-06-07 09:36:30 +00:00
|
|
|
{
|
2021-02-02 14:35:40 +00:00
|
|
|
util::NullLock lock{m_Access};
|
|
|
|
std::unordered_set<RouterID> removed;
|
|
|
|
auto itr = m_Entries.begin();
|
|
|
|
while (itr != m_Entries.end())
|
2020-01-12 12:29:36 +00:00
|
|
|
{
|
2021-02-02 14:35:40 +00:00
|
|
|
if (itr->second.insertedAt < cutoff and keep.count(itr->second.rc.pubkey) == 0)
|
|
|
|
{
|
|
|
|
removed.insert(itr->second.rc.pubkey);
|
|
|
|
itr = m_Entries.erase(itr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++itr;
|
2020-01-12 12:29:36 +00:00
|
|
|
}
|
2021-02-02 14:35:40 +00:00
|
|
|
if (not removed.empty())
|
|
|
|
AsyncRemoveManyFromDisk(std::move(removed));
|
2018-06-07 09:36:30 +00:00
|
|
|
}
|
2018-06-13 12:58:51 +00:00
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
void
|
|
|
|
NodeDB::Put(RouterContact rc)
|
2020-04-02 17:56:13 +00:00
|
|
|
{
|
2021-02-02 14:35:40 +00:00
|
|
|
util::NullLock lock{m_Access};
|
|
|
|
m_Entries.erase(rc.pubkey);
|
|
|
|
m_Entries.emplace(rc.pubkey, rc);
|
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
|
|
|
|
NodeDB::NumLoaded() const
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2021-02-02 14:35:40 +00:00
|
|
|
util::NullLock lock{m_Access};
|
|
|
|
return m_Entries.size();
|
2018-04-08 12:18:16 +00:00
|
|
|
}
|
2018-09-13 16:41:53 +00:00
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
void
|
|
|
|
NodeDB::PutIfNewer(RouterContact rc)
|
2018-11-14 18:02:27 +00:00
|
|
|
{
|
2021-02-02 14:35:40 +00:00
|
|
|
util::NullLock lock{m_Access};
|
|
|
|
auto itr = m_Entries.find(rc.pubkey);
|
|
|
|
if (itr == m_Entries.end() or itr->second.rc.OtherIsNewer(rc))
|
2018-11-14 18:02:27 +00:00
|
|
|
{
|
2021-02-02 14:35:40 +00:00
|
|
|
// delete if existing
|
|
|
|
if (itr != m_Entries.end())
|
|
|
|
m_Entries.erase(itr);
|
|
|
|
// add new entry
|
|
|
|
m_Entries.emplace(rc.pubkey, rc);
|
2018-11-14 18:02:27 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-02 14:35:40 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
NodeDB::AsyncRemoveManyFromDisk(std::unordered_set<RouterID> remove) const
|
2018-11-14 18:02:27 +00:00
|
|
|
{
|
2021-04-02 15:10:37 +00:00
|
|
|
if (m_Root.empty())
|
|
|
|
return;
|
2021-02-02 14:35:40 +00:00
|
|
|
// build file list
|
|
|
|
std::set<fs::path> files;
|
|
|
|
for (auto id : remove)
|
2018-11-14 18:02:27 +00:00
|
|
|
{
|
2021-02-02 14:35:40 +00:00
|
|
|
files.emplace(GetPathForPubkey(std::move(id)));
|
2018-11-14 18:02:27 +00:00
|
|
|
}
|
2021-02-02 14:35:40 +00:00
|
|
|
// remove them from the disk via the diskio thread
|
|
|
|
disk([files]() {
|
|
|
|
for (auto fpath : files)
|
|
|
|
fs::remove(fpath);
|
|
|
|
});
|
2018-11-14 18:02:27 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
llarp::RouterContact
|
|
|
|
NodeDB::FindClosestTo(llarp::dht::Key_t location) const
|
2019-03-25 13:52:22 +00:00
|
|
|
{
|
2021-02-02 14:35:40 +00:00
|
|
|
util::NullLock lock{m_Access};
|
|
|
|
llarp::RouterContact rc;
|
|
|
|
const llarp::dht::XorMetric compare(location);
|
|
|
|
VisitAll([&rc, compare](const auto& otherRC) {
|
|
|
|
if (rc.pubkey.IsZero())
|
|
|
|
{
|
|
|
|
rc = otherRC;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (compare(
|
|
|
|
llarp::dht::Key_t{otherRC.pubkey.as_array()},
|
|
|
|
llarp::dht::Key_t{rc.pubkey.as_array()}))
|
|
|
|
rc = otherRC;
|
|
|
|
});
|
|
|
|
return rc;
|
2019-03-25 13:52:22 +00:00
|
|
|
}
|
2019-03-11 13:58:31 +00:00
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
std::vector<RouterContact>
|
|
|
|
NodeDB::FindManyClosestTo(llarp::dht::Key_t location, uint32_t numRouters) const
|
2020-03-11 20:45:48 +00:00
|
|
|
{
|
2021-02-02 14:35:40 +00:00
|
|
|
util::NullLock lock{m_Access};
|
|
|
|
std::vector<const RouterContact*> all;
|
|
|
|
|
|
|
|
const auto& entries = m_Entries;
|
|
|
|
|
|
|
|
all.reserve(entries.size());
|
|
|
|
for (auto& entry : entries)
|
2019-03-11 13:58:31 +00:00
|
|
|
{
|
2021-02-02 14:35:40 +00:00
|
|
|
all.push_back(&entry.second.rc);
|
2019-03-11 13:58:31 +00:00
|
|
|
}
|
2021-02-02 14:35:40 +00:00
|
|
|
|
|
|
|
auto it_mid = numRouters < all.size() ? all.begin() + numRouters : all.end();
|
|
|
|
std::partial_sort(
|
|
|
|
all.begin(), it_mid, all.end(), [compare = dht::XorMetric{location}](auto* a, auto* b) {
|
|
|
|
return compare(*a, *b);
|
|
|
|
});
|
|
|
|
|
|
|
|
std::vector<RouterContact> closest;
|
|
|
|
closest.reserve(numRouters);
|
|
|
|
for (auto it = all.begin(); it != it_mid; ++it)
|
|
|
|
closest.push_back(**it);
|
|
|
|
|
|
|
|
return closest;
|
2019-03-11 13:58:31 +00:00
|
|
|
}
|
2021-02-02 14:35:40 +00:00
|
|
|
} // namespace llarp
|