proto/discovery: home the recent-request cap and inject request_id from the shell

Two behavior-neutral discovery cleanups:

- Move MAX_RECENT_DISCOVERY_REQUESTS out of the node discovery handler into the
  discovery subsystem limits module and re-export it, keeping the two use sites
  unchanged. Same value and semantics; only its home moves.

- Remove the ambient rand draw from LookupRequest by deleting generate() and
  having the shell draw the random request_id and pass it into the existing
  new() constructor. Same per-request u64 draw, now at the shell, leaving the
  discovery codec free of ambient RNG reads.
This commit is contained in:
Johnathan Corgan
2026-07-08 19:00:24 +00:00
parent b53db662c3
commit c1ddbf053c
6 changed files with 27 additions and 25 deletions
+8
View File
@@ -16,6 +16,14 @@
use crate::NodeAddr;
use alloc::collections::BTreeMap;
// ============================================================================
// Receive-side: Request dedup cache bound
// ============================================================================
/// Maximum number of recent LookupRequests retained for dedup and
/// reverse-path routing before the cache is treated as full.
pub(crate) const MAX_RECENT_DISCOVERY_REQUESTS: usize = 4096;
// ============================================================================
// Originator-side: Discovery Backoff
// ============================================================================
+3 -1
View File
@@ -26,7 +26,9 @@ pub(crate) use core::{
initiate_gate, on_response_accepted, plan_forward, plan_initiate, plan_response_route,
poll_pending,
};
pub(crate) use limits::{DiscoveryBackoff, DiscoveryForwardRateLimiter};
pub(crate) use limits::{
DiscoveryBackoff, DiscoveryForwardRateLimiter, MAX_RECENT_DISCOVERY_REQUESTS,
};
#[cfg(test)]
pub(crate) use state::RecentRequest;
pub(crate) use state::{Discovery, PendingLookup};
+3 -2
View File
@@ -36,8 +36,9 @@ fn test_lookup_request_generate() {
let origin = make_node_addr(2);
let coords = make_coords(&[2, 0]);
let req1 = LookupRequest::generate(target, origin, coords.clone(), 5, 0);
let req2 = LookupRequest::generate(target, origin, coords, 5, 0);
use rand::RngExt;
let req1 = LookupRequest::new(rand::rng().random(), target, origin, coords.clone(), 5, 0);
let req2 = LookupRequest::new(rand::rng().random(), target, origin, coords, 5, 0);
// Random IDs should differ
assert_ne!(req1.request_id, req2.request_id);
-13
View File
@@ -48,19 +48,6 @@ impl LookupRequest {
}
}
/// Generate a new request with a random ID.
pub fn generate(
target: NodeAddr,
origin: NodeAddr,
origin_coords: TreeCoordinate,
ttl: u8,
min_mtu: u16,
) -> Self {
use rand::RngExt;
let request_id = rand::rng().random();
Self::new(request_id, target, origin, origin_coords, ttl, min_mtu)
}
/// Decrement TTL for forwarding.
///
/// Returns false if TTL was already 0.