1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00

Minor RC load/store/prune fixups

This commit is contained in:
Thomas Winget 2023-11-27 11:31:43 -05:00
parent c30a4dd44a
commit b353fd4095
3 changed files with 74 additions and 60 deletions

View file

@ -109,8 +109,10 @@ namespace llarp
{
bootstraps.clear(); // this function really shouldn't be called more than once, but...
for (const auto& rc : rcs)
{
bootstraps.emplace(rc.router_id(), rc);
}
}
void
NodeDB::rotate_rc_source()
@ -315,9 +317,10 @@ namespace llarp
if (m_Root.empty())
return;
router.loop()->call([this]() {
std::set<fs::path> purge;
const auto now = time_now_ms();
for (const char& ch : skiplist_subdirs)
{
if (!ch)
@ -340,19 +343,18 @@ namespace llarp
return true;
}
if (rc.is_expired(time_now_ms()))
if (rc.is_expired(now))
{
// rc expired dont load it and purge it later
purge.emplace(f);
return true;
}
// validate signature and purge known_rcs with invalid signatures
// load ones with valid signatures
if (rc.verify())
known_rcs.emplace(rc.router_id(), rc);
else
purge.emplace(f);
// TODO: the list of relays should be maintained and stored separately from
// the RCs, as we keep older RCs around in case we go offline and need to
// bootstrap, but they shouldn't be in the "good relays" list.
client_known_routers.insert(rc.router_id());
return true;
});
@ -365,7 +367,6 @@ namespace llarp
for (const auto& fpath : purge)
fs::remove(fpath);
}
});
}
void
@ -407,11 +408,21 @@ namespace llarp
}
void
NodeDB::remove_stale_rcs(std::unordered_set<RouterID> keep, llarp_time_t cutoff)
NodeDB::remove_stale_rcs()
{
(void)keep;
(void)cutoff;
// TODO: handling of "stale" is pending change, removing here for now.
auto cutoff_time =
std::chrono::time_point_cast<std::chrono::seconds>(std::chrono::system_clock::now());
cutoff_time -= router.is_service_node() ? RouterContact::OUTDATED_AGE : RouterContact::LIFETIME;
for (auto itr = known_rcs.begin(); itr != known_rcs.end();)
{
if (cutoff_time > itr->second.timestamp())
{
log::info(logcat, "Pruning RC for {}, as it is too old to keep.", itr->first);
known_rcs.erase(itr);
continue;
}
itr++;
}
}
bool

View file

@ -271,9 +271,12 @@ namespace llarp
});
}
/// remove rcs that are not in keep and have been inserted before cutoff
/// 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.
void
remove_stale_rcs(std::unordered_set<RouterID> keep, llarp_time_t cutoff);
remove_stale_rcs();
/// put (or replace) the RC if we consider it valid (want_rc). returns true if put.
bool

View file

@ -52,8 +52,8 @@ namespace llarp
if (sig.size() != 64)
throw std::runtime_error{"Invalid signature: not 64 bytes"};
if (is_expired(time_now_ms()) and reject_expired)
throw std::runtime_error{"Unable to verify expired RemoteRC!"};
if (reject_expired and is_expired(time_now_ms()))
throw std::runtime_error{"Rejecting expired RemoteRC!"};
// TODO: revisit if this is needed; detail from previous implementation
const auto* net = net::Platform::Default_ptr();
@ -79,18 +79,18 @@ namespace llarp
try
{
util::file_to_buffer(fname, buf.data(), MAX_RC_SIZE);
}
catch (const std::exception& e)
{
log::error(logcat, "Failed to read RC from {}: {}", fname, e.what());
return false;
}
oxenc::bt_dict_consumer btdc{buf};
bt_load(btdc);
bt_verify(btdc);
_payload = buf;
}
catch (const std::exception& e)
{
log::error(logcat, "Failed to read or validate RC from {}: {}", fname, e.what());
return false;
}
return true;
}