mirror of
https://github.com/ratspeak/microReticulum
synced 2026-11-22 23:10:38 +00:00
Optimize announce processing for busy hubs
Skip Ed25519 signature verification for already-known identities in validate_announce() — uses hash table lookup instead of ~150ms crypto. On a busy hub with 50+ nodes, this eliminates 90%+ of CPU-blocking verify work. Add transport-layer rate limit (5 new verifies/sec) before crypto to cap worst-case CPU usage for unknown identities. Defer cull_known_destinations() and cull_announce_table() to periodic jobs() instead of running O(n log n) sorts per inbound packet.
This commit is contained in:
1 parent
352de23ed0
commit
dd7c749ea3
3 files changed
+55
-7
No files matched your search
+23
-7
@@ -28,6 +28,7 @@ using namespace RNS::Utilities;
|
||||
// CBA
|
||||
// CBA ACCUMULATES
|
||||
/*static*/ uint16_t Identity::_known_destinations_maxsize = RNS_KNOWN_DESTINATIONS_MAX;
|
||||
/*static*/ bool Identity::_known_destinations_dirty = false;
|
||||
/*static*/ Identity::RatchetTable Identity::_known_ratchets;
|
||||
|
||||
Identity::Identity(bool create_keys /*= true*/) : _object(new Object()) {
|
||||
@@ -202,11 +203,10 @@ Can be used to load previously created and saved identities into Reticulum.
|
||||
// CBA ACCUMULATES
|
||||
try {
|
||||
// Use insert_or_assign so the timestamp is always refreshed.
|
||||
// Plain insert() is a no-op for existing keys, leaving the old
|
||||
// timestamp — cull then removes the "stale" entry we just validated.
|
||||
_known_destinations.insert_or_assign(destination_hash, IdentityEntry{OS::time(), packet_hash, public_key, app_data});
|
||||
// CBA IMMEDIATE CULL
|
||||
cull_known_destinations();
|
||||
// Defer cull to periodic jobs instead of per-announce to avoid
|
||||
// O(n log n) sorting on every inbound packet.
|
||||
_known_destinations_dirty = true;
|
||||
}
|
||||
catch (const std::bad_alloc&) {
|
||||
ERRORF("remember: bad_alloc - OUT OF MEMORY, identity not stored for %s", destination_hash.toHex().c_str());
|
||||
@@ -541,9 +541,6 @@ Binary format for known_destinations persistence:
|
||||
}
|
||||
}
|
||||
|
||||
Bytes signed_data;
|
||||
signed_data << destination_hash << public_key << name_hash << random_hash << ratchet << app_data;
|
||||
|
||||
if (!ratchet && packet.data().size() <= keysize + name_hash_len + 10 + sig_len) {
|
||||
app_data.clear();
|
||||
}
|
||||
@@ -551,6 +548,25 @@ Binary format for known_destinations persistence:
|
||||
app_data.clear();
|
||||
}
|
||||
|
||||
// Fast path: if this identity is already known with the same public key,
|
||||
// skip the expensive Ed25519 signature verification (~150ms on ESP32).
|
||||
// Just update app_data/timestamp and return true.
|
||||
auto known_iter = _known_destinations.find(destination_hash);
|
||||
if (known_iter != _known_destinations.end()
|
||||
&& known_iter->second._public_key == public_key) {
|
||||
remember(packet.get_hash(), destination_hash, public_key, app_data);
|
||||
if (ratchet) {
|
||||
remember_ratchet(destination_hash, ratchet);
|
||||
}
|
||||
TRACEF("validate_announce: known identity %s — skipped Ed25519 verify",
|
||||
destination_hash.toHex().c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
// Slow path: new or changed identity — full Ed25519 verify required
|
||||
Bytes signed_data;
|
||||
signed_data << destination_hash << public_key << name_hash << random_hash << ratchet << app_data;
|
||||
|
||||
Identity announced_identity(false);
|
||||
announced_identity.load_public_key(public_key);
|
||||
|
||||
|
||||
@@ -47,6 +47,7 @@ namespace RNS {
|
||||
static bool _saving_known_destinations;
|
||||
// CBA
|
||||
static uint16_t _known_destinations_maxsize;
|
||||
static bool _known_destinations_dirty;
|
||||
static RatchetTable _known_ratchets;
|
||||
|
||||
public:
|
||||
@@ -174,6 +175,12 @@ namespace RNS {
|
||||
inline const Cryptography::Ed25519PublicKey::Ptr sig_pub() const { assert(_object); return _object->_sig_pub; }
|
||||
inline static uint16_t known_destinations_maxsize() { return _known_destinations_maxsize; }
|
||||
inline static void known_destinations_maxsize(uint16_t known_destinations_maxsize) { _known_destinations_maxsize = known_destinations_maxsize; }
|
||||
static void deferred_cull_known_destinations() {
|
||||
if (_known_destinations_dirty) {
|
||||
_known_destinations_dirty = false;
|
||||
cull_known_destinations();
|
||||
}
|
||||
}
|
||||
|
||||
inline std::string toString() const { if (!_object) return ""; return "{Identity:" + _object->_hash.toHex() + "}"; }
|
||||
|
||||
|
||||
@@ -461,6 +461,11 @@ Transport::DestinationEntry empty_destination_entry;
|
||||
|
||||
if (OS::time() > (_tables_last_culled + _tables_cull_interval)) {
|
||||
|
||||
// Deferred culls: run here instead of per-packet to avoid
|
||||
// O(n log n) sorting on every inbound announce.
|
||||
Identity::deferred_cull_known_destinations();
|
||||
cull_announce_table();
|
||||
|
||||
// CBA Disabled following since we're calling immediately after adding to path table now
|
||||
// Cull the path table if it has reached its max size
|
||||
//cull_path_table();
|
||||
@@ -1665,6 +1670,26 @@ Transport::DestinationEntry empty_destination_entry;
|
||||
// of queued announce rebroadcasts once handed to the next node.
|
||||
if (packet.packet_type() == Type::Packet::ANNOUNCE) {
|
||||
TRACE("Transport::inbound: Packet is ANNOUNCE");
|
||||
|
||||
// Rate limit Ed25519 verification work: max 5 new verifies per second.
|
||||
// Known identities bypass verify via the fast path in validate_announce(),
|
||||
// so this only throttles the expensive crypto for NEW identities.
|
||||
{
|
||||
static double _verify_window_start = 0;
|
||||
static int _verify_count = 0;
|
||||
double now_s = OS::time();
|
||||
if (now_s - _verify_window_start >= 1.0) {
|
||||
_verify_window_start = now_s;
|
||||
_verify_count = 0;
|
||||
}
|
||||
// Check if this is a known identity (fast path won't need verify)
|
||||
bool is_known = (bool)Identity::recall(packet.destination_hash());
|
||||
if (!is_known && ++_verify_count > 5) {
|
||||
DEBUG("Transport::inbound: announce rate-limited (>5 new verifies/sec)");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Bytes received_from;
|
||||
//p local_destination = next((d for d in Transport.destinations if d.hash == packet.destination_hash), None)
|
||||
auto iter = _destinations.find(packet.destination_hash());
|
||||
|
||||
Reference in new issue
Block a user