mirror of
https://github.com/ratspeak/rsReticulum
synced 2026-11-22 23:10:33 +00:00
transport: prefilter announces that cannot update a path, and cull them from the held queue
This commit is contained in:
1 parent
3d04714a3b
commit
20ed5a07b1
5 files changed
+366
-11
No files matched your search
@@ -427,7 +427,7 @@ impl TransportActor {
|
||||
|
||||
/// Python's announce ingress in `preprocess_inbound`, in its order: the
|
||||
/// frame and payload sizes (protocol violations), a blocked type (silent),
|
||||
/// then an unrequired type (filtered) (Transport.py).
|
||||
/// an unrequired type (filtered), then the prefilter (Transport.py).
|
||||
fn admit_announce_ingress(
|
||||
&mut self,
|
||||
raw: &[u8],
|
||||
@@ -484,9 +484,92 @@ impl TransportActor {
|
||||
self.filtered_announce(interface_id);
|
||||
return false;
|
||||
}
|
||||
let blob_at = KEY_LEN + NAME_HASH_LEN;
|
||||
let random_blob: [u8; RANDOM_BLOB_LEN] = payload[blob_at..blob_at + RANDOM_BLOB_LEN]
|
||||
.try_into()
|
||||
.expect("payload length checked");
|
||||
if !self.prefilter_admits(header, &random_blob, interface_id) {
|
||||
trace!(
|
||||
interface_id,
|
||||
dest = hex::encode(header.destination_hash),
|
||||
"announce prefiltered"
|
||||
);
|
||||
self.record_interface_event(interface_id, InterfaceEvent::ReceivedAnnounce, 0);
|
||||
self.filtered_announce(interface_id);
|
||||
return false;
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// Python's prefilter conditions: an announce for a known path that is no
|
||||
/// path response, answers no path request and has no queued rebroadcast
|
||||
/// must pass `announce_prefilter` (Transport.py). Reads the raw path table,
|
||||
/// as upstream does, so an expired path not yet culled counts.
|
||||
fn prefilter_admits(
|
||||
&self,
|
||||
header: &rns_wire::header::PacketHeader,
|
||||
random_blob: &[u8; 10],
|
||||
interface_id: InterfaceId,
|
||||
) -> bool {
|
||||
let destination = &header.destination_hash;
|
||||
let Some(path) = self.path_table.get(destination) else {
|
||||
return true;
|
||||
};
|
||||
header.context == rns_wire::context::PacketContext::PathResponse
|
||||
|| self.path_requests.contains_key(destination)
|
||||
|| self.discovery_path_requests.contains_key(destination)
|
||||
|| self.announce_table.get(destination).is_some()
|
||||
|| self.announce_prefilter(destination, path, random_blob, interface_id)
|
||||
}
|
||||
|
||||
/// Python `Transport.announce_prefilter`: whether an announce could still
|
||||
/// update this path (Transport.py). A path whose interface is gone keeps
|
||||
/// it, as upstream's `None` interface does.
|
||||
fn announce_prefilter(
|
||||
&self,
|
||||
destination: &[u8; 16],
|
||||
path: &crate::path_table::PathEntry,
|
||||
random_blob: &[u8; 10],
|
||||
receiving_interface: InterfaceId,
|
||||
) -> bool {
|
||||
let Some(path_interface) = self.interfaces.get(&path.interface_id) else {
|
||||
return true;
|
||||
};
|
||||
let emitted = announce_timebase(random_blob);
|
||||
match emitted.cmp(&path_timebase_from_random_blobs(path.random_blobs.iter())) {
|
||||
std::cmp::Ordering::Greater => true,
|
||||
std::cmp::Ordering::Less => false,
|
||||
std::cmp::Ordering::Equal => {
|
||||
self.interfaces
|
||||
.get(&receiving_interface)
|
||||
.is_some_and(|entry| entry.gravity > path_interface.gravity)
|
||||
|| self.path_table.get_state(destination)
|
||||
== crate::constants::PathState::Unresponsive
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Interface.py culls a held announce for a destination now known when
|
||||
/// `announce_prefilter` rejects it, with none of the ingress exemptions.
|
||||
pub(super) fn held_announce_is_useful(&self, held: &crate::ingress::HeldAnnounce) -> bool {
|
||||
let Some(path) = self.path_table.get(&held.destination_hash) else {
|
||||
return true;
|
||||
};
|
||||
let Some(random_blob) = rns_wire::header::PacketHeader::unpack(&held.raw)
|
||||
.ok()
|
||||
.and_then(|(_, offset)| held.raw.get(offset + 74..offset + 84))
|
||||
.and_then(|blob| <[u8; 10]>::try_from(blob).ok())
|
||||
else {
|
||||
return true;
|
||||
};
|
||||
self.announce_prefilter(
|
||||
&held.destination_hash,
|
||||
path,
|
||||
&random_blob,
|
||||
held.receiving_interface_id,
|
||||
)
|
||||
}
|
||||
|
||||
fn process_announce(
|
||||
&mut self,
|
||||
raw: &[u8],
|
||||
|
||||
@@ -557,15 +557,29 @@ impl TransportActor {
|
||||
}
|
||||
|
||||
/// Python's interface job (Transport.py): re-evaluate both burst
|
||||
/// limiters, then release at most one held announce per interface.
|
||||
/// limiters, then, when a release is due, cull the held announces that
|
||||
/// can no longer update a path and release at most one per interface.
|
||||
fn process_held_announces(&mut self, _now: f64) {
|
||||
let iface_ids: Vec<InterfaceId> = self.interfaces.keys().copied().collect();
|
||||
for iface_id in iface_ids {
|
||||
let released = self.interfaces.get_mut(&iface_id).and_then(|iface| {
|
||||
let due = self.interfaces.get_mut(&iface_id).is_some_and(|iface| {
|
||||
iface.ingress.should_ingress_limit();
|
||||
iface.ingress.should_ingress_limit_pr();
|
||||
iface.ingress.try_release_held()
|
||||
iface.ingress.release_due()
|
||||
});
|
||||
if !due {
|
||||
continue;
|
||||
}
|
||||
let culled: Vec<[u8; 16]> = self.interfaces[&iface_id]
|
||||
.ingress
|
||||
.held_announces()
|
||||
.filter(|held| !self.held_announce_is_useful(held))
|
||||
.map(|held| held.destination_hash)
|
||||
.collect();
|
||||
let released = self
|
||||
.interfaces
|
||||
.get_mut(&iface_id)
|
||||
.and_then(|iface| iface.ingress.cull_and_release(&culled));
|
||||
|
||||
if let Some(announce) = released {
|
||||
pathing!(
|
||||
|
||||
@@ -6521,6 +6521,8 @@ mod tests {
|
||||
assert!(path.has_random_blob(&higher_new_blob));
|
||||
}
|
||||
|
||||
/// Python 1.6.1 prefilters an older emission even for an expired path
|
||||
/// (Transport.py `announce_prefilter`); only an awaited path takes it.
|
||||
#[test]
|
||||
fn test_older_higher_hop_announce_waits_for_expiry_or_unresponsive_path() {
|
||||
let (mut actor, _tx) = TransportActor::new();
|
||||
@@ -6557,6 +6559,17 @@ mod tests {
|
||||
assert!(!path.has_random_blob(&older_blob));
|
||||
|
||||
actor.path_table.get_mut(&dest_hash).unwrap().expires = 0.0;
|
||||
actor.on_inbound(InboundPacket {
|
||||
raw: raw_older.clone(),
|
||||
interface_id: 2,
|
||||
rssi: None,
|
||||
snr: None,
|
||||
q: None,
|
||||
});
|
||||
assert_eq!(actor.path_table.get(&dest_hash).unwrap().hops, 2);
|
||||
assert_eq!(actor.interfaces[&2].ingress.announces_filtered(), 2);
|
||||
|
||||
actor.path_requests.insert(dest_hash, now_f64());
|
||||
actor.on_inbound(InboundPacket {
|
||||
raw: raw_older,
|
||||
interface_id: 2,
|
||||
|
||||
@@ -45,6 +45,21 @@ fn subscribe(actor: &mut TransportActor) -> mpsc::Receiver<crate::messages::Anno
|
||||
rx
|
||||
}
|
||||
|
||||
/// An announce emitted after any made this second; Python 1.6.1 prefilters a
|
||||
/// same-emission copy of a known path (Transport.py).
|
||||
fn newer_announce(
|
||||
identity: &rns_identity::identity::Identity,
|
||||
app_name: &str,
|
||||
hops: u8,
|
||||
) -> (Bytes, [u8; 16]) {
|
||||
make_announce_for_with_random_blob(
|
||||
identity,
|
||||
app_name,
|
||||
hops,
|
||||
random_blob(0x5E, now_f64() as u64 + 1),
|
||||
)
|
||||
}
|
||||
|
||||
/// The same announce answered as a path response, as a transport hub replays
|
||||
/// it from its cache.
|
||||
fn as_path_response(announce: &Bytes) -> Bytes {
|
||||
@@ -125,8 +140,7 @@ fn a_fresh_announce_completes_a_restored_entry() {
|
||||
let restored = actor.recent_announces.get_mut(&dest).unwrap();
|
||||
(restored.facts_known, restored.hops, restored.name_hash) = (false, 0, [0; 10]);
|
||||
|
||||
let (fresh, _) =
|
||||
make_announce_for_context(&identity, "identity.restored", 3, PacketContext::None);
|
||||
let (fresh, _) = newer_announce(&identity, "identity.restored", 3);
|
||||
inbound(&mut actor, &fresh);
|
||||
|
||||
let entry = &actor.recent_announces[&dest];
|
||||
@@ -145,8 +159,7 @@ fn a_fresh_announce_after_a_replay_is_not_a_path_response() {
|
||||
inbound(&mut actor, &as_path_response(&first));
|
||||
assert!(actor.recent_announces[&dest].is_path_response);
|
||||
|
||||
let (fresh, _) =
|
||||
make_announce_for_context(&identity, "identity.first-replay", 2, PacketContext::None);
|
||||
let (fresh, _) = newer_announce(&identity, "identity.first-replay", 2);
|
||||
inbound(&mut actor, &fresh);
|
||||
|
||||
let entry = &actor.recent_announces[&dest];
|
||||
|
||||
@@ -1,8 +1,15 @@
|
||||
//! Python 1.6.1 announce ingress (Transport.py `preprocess_inbound`): the size
|
||||
//! gates, the type filters, the filtered count `afrxc`, and the accounting
|
||||
//! of held announces (Interface.py).
|
||||
//! gates, the type filters, the prefilter that drops announces which cannot
|
||||
//! update a known path before their signature is checked, the filtered count
|
||||
//! `afrxc`, and the accounting of held announces (Interface.py).
|
||||
//!
|
||||
//! Expected prefilter verdicts were checked against upstream 1.6.1
|
||||
//! `Transport.announce_prefilter`, called directly on stand-in packets and
|
||||
//! path entries (older False, newer True, same emission False, higher
|
||||
//! gravity True, unresponsive True, path interface `None` True).
|
||||
use super::*;
|
||||
use crate::ingress::{IngressController, IngressOverrides};
|
||||
use crate::announce_validation::CachedAnnounce;
|
||||
use crate::ingress::{HeldAnnounce, IngressController, IngressOverrides};
|
||||
use rns_wire::context::PacketContext;
|
||||
|
||||
const NOW: f64 = 1_800_000_000.0;
|
||||
@@ -116,6 +123,11 @@ fn totals(actor: &mut TransportActor) -> crate::messages::TrafficTotalsRpcEntry
|
||||
totals
|
||||
}
|
||||
|
||||
fn hash(raw: &Bytes) -> [u8; 32] {
|
||||
let (header, _) = rns_wire::header::PacketHeader::unpack(raw).unwrap();
|
||||
rns_wire::hash::packet_hash(raw, header.flags.header_type)
|
||||
}
|
||||
|
||||
/// `(arxc, arxb, afrxc)` on an interface.
|
||||
fn counts(actor: &mut TransportActor, id: InterfaceId) -> (u64, u64, u64) {
|
||||
let entry = stats(actor, id);
|
||||
@@ -126,6 +138,140 @@ fn counts(actor: &mut TransportActor, id: InterfaceId) -> (u64, u64, u64) {
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_older_copy_of_a_known_path_is_dropped_before_validation() {
|
||||
let clock = crate::test_clock::Clock::at(NOW);
|
||||
let mut node = node(false);
|
||||
let identity = rns_identity::identity::Identity::new();
|
||||
let current = plain(&identity, 200);
|
||||
inbound(&mut node.actor, ¤t.raw, 1);
|
||||
assert!(node.actor.path_table.get(¤t.destination).is_some());
|
||||
let remembered_at = node.actor.recent_announces[¤t.destination].timestamp;
|
||||
let (arxc, arxb, afrxc) = counts(&mut node.actor, 1);
|
||||
let before = totals(&mut node.actor);
|
||||
|
||||
clock.set(NOW + 5.0);
|
||||
let older = plain(&identity, 199);
|
||||
inbound(&mut node.actor, &older.raw, 1);
|
||||
|
||||
assert_eq!(
|
||||
counts(&mut node.actor, 1),
|
||||
(arxc + 1, arxb, afrxc + 1),
|
||||
"received_announce(size=0) and filtered_announce()"
|
||||
);
|
||||
let after = totals(&mut node.actor);
|
||||
assert_eq!(after.announce_rx_packets, before.announce_rx_packets);
|
||||
assert_eq!(
|
||||
after.announce_filtered_packets,
|
||||
before.announce_filtered_packets + 1
|
||||
);
|
||||
assert!(matches!(
|
||||
node.actor
|
||||
.announce_validation
|
||||
.lookup(&hash(&older.raw), 0.0, false),
|
||||
CachedAnnounce::Miss
|
||||
));
|
||||
assert!(!node.actor.packet_hashlist.contains(&hash(&older.raw)));
|
||||
assert_eq!(
|
||||
node.actor.recent_announces[¤t.destination].timestamp, remembered_at,
|
||||
"a prefiltered copy is never remembered"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_same_emission_copy_needs_more_gravity_or_an_unresponsive_path() {
|
||||
let _clock = crate::test_clock::Clock::at(NOW);
|
||||
let mut node = node(false);
|
||||
node.actor.interfaces.get_mut(&2).unwrap().gravity = 1;
|
||||
let identity = rns_identity::identity::Identity::new();
|
||||
let announce = plain(&identity, 300);
|
||||
inbound(&mut node.actor, &announce.raw, 1);
|
||||
|
||||
inbound(&mut node.actor, &announce.raw, 2);
|
||||
assert_eq!(counts(&mut node.actor, 2).2, 0, "higher gravity is kept");
|
||||
assert_eq!(
|
||||
node.actor
|
||||
.path_table
|
||||
.get(&announce.destination)
|
||||
.unwrap()
|
||||
.interface_id,
|
||||
2
|
||||
);
|
||||
|
||||
inbound(&mut node.actor, &announce.raw, 1);
|
||||
let (arxc, arxb, afrxc) = counts(&mut node.actor, 1);
|
||||
assert_eq!(afrxc, 1, "lower gravity is filtered");
|
||||
|
||||
node.actor.path_table.set_state(
|
||||
announce.destination,
|
||||
crate::constants::PathState::Unresponsive,
|
||||
);
|
||||
inbound(&mut node.actor, &announce.raw, 1);
|
||||
assert_eq!(
|
||||
counts(&mut node.actor, 1),
|
||||
(arxc + 1, arxb + announce.raw.len() as u64, afrxc),
|
||||
"an unresponsive path is kept"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn path_responses_awaited_and_queued_destinations_skip_the_prefilter() {
|
||||
let _clock = crate::test_clock::Clock::at(NOW);
|
||||
let mut node = node(true);
|
||||
let identity = rns_identity::identity::Identity::new();
|
||||
let current = plain(&identity, 400);
|
||||
let destination = current.destination;
|
||||
inbound(&mut node.actor, ¤t.raw, 1);
|
||||
let older = plain(&identity, 399);
|
||||
let older_response = signed(&identity, 398, 1, PacketContext::PathResponse, &[], None);
|
||||
let filtered = |node: &mut Node| counts(&mut node.actor, 2).2;
|
||||
|
||||
assert!(node.actor.announce_table.get(&destination).is_some());
|
||||
inbound(&mut node.actor, &older.raw, 2);
|
||||
assert_eq!(filtered(&mut node), 0, "a queued rebroadcast");
|
||||
|
||||
node.actor.announce_table.remove(&destination);
|
||||
inbound(&mut node.actor, &older_response.raw, 2);
|
||||
assert_eq!(filtered(&mut node), 0, "a path response");
|
||||
|
||||
node.actor.path_requests.insert(destination, NOW);
|
||||
inbound(&mut node.actor, &older.raw, 2);
|
||||
assert_eq!(filtered(&mut node), 0, "an awaited path");
|
||||
node.actor.path_requests.remove(&destination);
|
||||
|
||||
node.actor.path_table.get_mut(&destination).unwrap().expires = 0.0;
|
||||
inbound(&mut node.actor, &older.raw, 2);
|
||||
assert_eq!(
|
||||
filtered(&mut node),
|
||||
1,
|
||||
"an expired path not yet culled still counts"
|
||||
);
|
||||
|
||||
node.actor
|
||||
.request_recursive_discovery(destination, 1)
|
||||
.expect("discovery for the expired path");
|
||||
inbound(&mut node.actor, &older.raw, 2);
|
||||
assert_eq!(filtered(&mut node), 1, "a discovery request");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_path_on_a_detached_interface_takes_any_announce() {
|
||||
let _clock = crate::test_clock::Clock::at(NOW);
|
||||
let mut node = node(false);
|
||||
let identity = rns_identity::identity::Identity::new();
|
||||
let current = plain(&identity, 500);
|
||||
inbound(&mut node.actor, ¤t.raw, 1);
|
||||
node.actor
|
||||
.path_table
|
||||
.get_mut(¤t.destination)
|
||||
.unwrap()
|
||||
.interface_id = 9;
|
||||
|
||||
inbound(&mut node.actor, &plain(&identity, 499).raw, 2);
|
||||
|
||||
assert_eq!(counts(&mut node.actor, 2).2, 0);
|
||||
}
|
||||
|
||||
/// Python 1.6.1: a frame over the MTU, a HEADER_1 announce that leaves no
|
||||
/// room for a transport id, and a payload under 84 bytes are protocol
|
||||
/// violations, checked before the type filters (Transport.py). 317 bytes of
|
||||
@@ -273,3 +419,89 @@ fn refused_and_replaced_holds_count_as_filtered() {
|
||||
assert_eq!(counts(&mut node.actor, 1).2, 2, "replaced");
|
||||
assert_eq!(node.actor.interfaces[&1].ingress.held_count(), 1);
|
||||
}
|
||||
|
||||
fn learned_path(actor: &mut TransportActor, destination: [u8; 16], emitted: u64) {
|
||||
let mut path = crate::path_table::PathEntry::new(None, 1, 1, InterfaceMode::Gateway);
|
||||
path.random_blobs.push_back(random_blob(0x99, emitted));
|
||||
actor.path_table.insert(destination, path);
|
||||
}
|
||||
|
||||
fn hold(actor: &mut TransportActor, announce: &Announce) {
|
||||
actor
|
||||
.interfaces
|
||||
.get_mut(&1)
|
||||
.unwrap()
|
||||
.ingress
|
||||
.hold_announce(HeldAnnounce {
|
||||
raw: announce.raw.clone(),
|
||||
destination_hash: announce.destination,
|
||||
hops: 2,
|
||||
receiving_interface_id: 1,
|
||||
});
|
||||
}
|
||||
|
||||
fn released(actor: &mut TransportActor) -> usize {
|
||||
actor.last_held_announce_check = 0.0;
|
||||
actor.on_tick();
|
||||
let mut cx = std::task::Context::from_waker(std::task::Waker::noop());
|
||||
let mut count = 0;
|
||||
while actor
|
||||
.inbound_rx
|
||||
.poll_recv(&mut cx, &actor.inbound_queues)
|
||||
.is_ready()
|
||||
{
|
||||
count += 1;
|
||||
}
|
||||
count
|
||||
}
|
||||
|
||||
/// Interface.py culls held announces the prefilter rejects, but only on a
|
||||
/// pass where a release is due, and leaves them out of the release.
|
||||
#[test]
|
||||
fn a_held_announce_a_newer_path_outdates_is_culled_when_a_release_is_due() {
|
||||
let _clock = crate::test_clock::Clock::at(NOW);
|
||||
let mut node = node(false);
|
||||
node.actor.interfaces.get_mut(&1).unwrap().ingress = IngressController::new();
|
||||
let outdated = plain(&rns_identity::identity::Identity::new(), 1000);
|
||||
let useful = plain(&rns_identity::identity::Identity::new(), 1000);
|
||||
hold(&mut node.actor, &outdated);
|
||||
hold(&mut node.actor, &useful);
|
||||
learned_path(&mut node.actor, outdated.destination, 1001);
|
||||
learned_path(&mut node.actor, useful.destination, 999);
|
||||
|
||||
assert_eq!(released(&mut node.actor), 1);
|
||||
let ingress = &node.actor.interfaces[&1].ingress;
|
||||
assert_eq!(ingress.held_count(), 0, "the outdated one is culled");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn held_announces_are_not_culled_while_no_release_is_due() {
|
||||
let _clock = crate::test_clock::Clock::at(NOW);
|
||||
let mut node = node(false);
|
||||
bursting(&mut node.actor, IngressOverrides::default());
|
||||
let outdated = plain(&rns_identity::identity::Identity::new(), 1100);
|
||||
hold(&mut node.actor, &outdated);
|
||||
learned_path(&mut node.actor, outdated.destination, 1101);
|
||||
|
||||
assert_eq!(released(&mut node.actor), 0);
|
||||
assert_eq!(node.actor.interfaces[&1].ingress.held_count(), 1);
|
||||
}
|
||||
|
||||
/// A release re-enters `preprocess_inbound` (Interface.py), so the
|
||||
/// prefilter applies to it.
|
||||
#[test]
|
||||
fn a_released_announce_for_a_newer_path_is_prefiltered() {
|
||||
let _clock = crate::test_clock::Clock::at(NOW);
|
||||
let mut node = node(false);
|
||||
let announce = plain(&rns_identity::identity::Identity::new(), 1200);
|
||||
learned_path(&mut node.actor, announce.destination, 1201);
|
||||
|
||||
node.actor.on_released_announce(HeldAnnounce {
|
||||
raw: announce.raw.clone(),
|
||||
destination_hash: announce.destination,
|
||||
hops: 2,
|
||||
receiving_interface_id: 1,
|
||||
});
|
||||
|
||||
assert_eq!(counts(&mut node.actor, 1), (1, 0, 1));
|
||||
}
|
||||
Reference in new issue
Block a user