paths: a restored path skips blackholed identities and filtered types

This commit is contained in:
DeFiDude committed 2026-11-18 13:05:27 -07:00
1 parent 4845bc1f93
commit b0125cea65
3 files changed
+117 -1

No files matched your search

+20 -1
View File
@@ -1060,6 +1060,8 @@ impl TransportActor {
) {
if !self.pending_path_entries.is_empty() {
let mut promoted = 0usize;
let mut filtered = 0usize;
let mut blackholed = 0usize;
let hashes = [
crate::persistence::interface_hash_from_name(upstream_name),
crate::persistence::interface_hash_from_name(name),
@@ -1081,6 +1083,21 @@ impl TransportActor {
}
let mut hash = [0u8; 16];
hash.copy_from_slice(&pe.destination_hash);
// Transport.py: a restored path skips blackholed identities
// and filtered destination types.
let known = self.recent_announces.get(&hash);
if known.and_then(|k| k.public_key).is_some_and(|key| {
self.blackhole_table
.is_blackholed(&rns_crypto::sha::truncated_hash(&key))
}) {
blackholed += 1;
return false;
}
let name_hash = known.filter(|k| k.facts_known).map(|k| &k.name_hash);
if !self.destination_filter.admits_stored(name_hash) {
filtered += 1;
return false;
}
let next_hop = pe.next_hop.as_ref().and_then(|h| {
if h.len() == 16 {
let mut arr = [0u8; 16];
@@ -1126,11 +1143,13 @@ impl TransportActor {
}
false
});
if promoted > 0 {
if promoted > 0 || filtered > 0 || blackholed > 0 {
debug!(
interface_id = id,
name = %name,
promoted,
filtered,
blackholed,
"rebound persisted path entries to live interface"
);
}
@@ -181,3 +181,72 @@ fn a_ratchet_heard_again_after_a_restart_keeps_its_stored_receive_time() {
restarted.on_shutdown();
let _ = std::fs::remove_dir_all(dir);
}
/// Restart with `prepare` run after the snapshot is staged and before the
/// interface binds it, as a blackhole list loaded late would be.
fn restarted_with(
dir: &std::path::Path,
prepare: impl FnOnce(&mut TransportActor),
) -> TransportActor {
let (mut actor, _) = TransportActor::new();
actor.initialize_storage(dir.to_path_buf());
prepare(&mut actor);
let (mut entry, _rx) = make_test_interface("uplink");
entry.ingress = crate::ingress::IngressController::disabled();
actor.handle_message(TransportMessage::RegisterInterface { id: 1, entry });
actor
}
/// Save paths to three destinations, the third's identity to be blackholed.
fn three_saved_paths(dir: &std::path::Path) -> ([[u8; 16]; 3], [u8; 16]) {
let now = crate::now_f64() as u64;
let names = ["restored.kept", "restored.spam", "restored.blackholed"];
let identities: [_; 3] = std::array::from_fn(|_| rns_identity::identity::Identity::new());
let mut before = node(dir);
let dests = std::array::from_fn(|i| {
let (raw, dest) = announce(&identities[i], names[i], None, now, PacketContext::None);
inbound(&mut before, &raw);
assert!(before.path_table.has_path(&dest), "{}", names[i]);
dest
});
before.on_shutdown();
(dests, identities[2].hash)
}
/// Transport.py 1.6.1 (20e25a9f, 350b7e26): a restored path skips a
/// blackholed identity and a filtered destination type; under
/// `required_only` only allowed types bind.
#[test]
fn restored_paths_skip_blackholed_identities_and_filtered_types() {
let dir = unique_temp_dir("restored-filtered");
let (dests, blackholed) = three_saved_paths(&dir);
let mut restarted = restarted_with(&dir, |actor| {
actor.handle_message(TransportMessage::SetFilterPolicy {
policy: crate::destination_filter::FilterPolicy {
ignored_types: vec!["restored.spam".into()],
..Default::default()
},
});
actor.blackhole_table.add(blackholed, None);
});
let bound = dests.map(|dest| restarted.path_table.has_path(&dest));
assert_eq!(bound, [true, false, false]);
restarted.on_shutdown();
let _ = std::fs::remove_dir_all(dir);
let dir = unique_temp_dir("restored-required");
let (dests, _) = three_saved_paths(&dir);
let mut required = restarted_with(&dir, |actor| {
actor.handle_message(TransportMessage::SetFilterPolicy {
policy: crate::destination_filter::FilterPolicy {
required_only: true,
allowed_types: vec!["restored.blackholed".into()],
..Default::default()
},
});
});
let bound = dests.map(|dest| required.path_table.has_path(&dest));
assert_eq!(bound, [false, false, true]);
required.on_shutdown();
let _ = std::fs::remove_dir_all(dir);
}
@@ -210,6 +210,19 @@ impl DestinationTypeFilter {
self.blocked.contains(hash)
}
/// Python's rule for a path restored from storage (Transport.py): a
/// blocked type goes, and under `required_only` so does any type not
/// allowed, client wants included. An unknown type goes only under
/// `required_only`.
pub fn admits_stored(&self, hash: Option<&NameHash>) -> bool {
match hash {
Some(hash) => {
!self.blocked.contains(hash) && (!self.required_only || self.allowed.contains(hash))
}
None => !self.required_only,
}
}
/// The decision inside Python `Transport.inbound` (Transport.py),
/// given the wire hop count before the inbound increment.
pub fn admits(&self, hash: &NameHash, source: AnnounceSource, wire_hops: u8) -> bool {
@@ -301,6 +314,21 @@ mod tests {
);
}
/// Transport.py 1.6.1 (20e25a9f): the load rule ignores client wants.
#[test]
fn stored_paths_follow_the_blocked_and_allowed_types_only() {
let mut f = DestinationTypeFilter::default();
f.block("a.blocked");
f.allow("a.allowed");
assert!(f.wants("a.wanted", vec![1]));
let [blocked, allowed, wanted] = ["a.blocked", "a.allowed", "a.wanted"].map(name_hash);
assert!(!f.admits_stored(Some(&blocked)));
assert!(f.admits_stored(Some(&wanted)) && f.admits_stored(None));
f.set_required_only(true);
assert!(f.admits_stored(Some(&allowed)));
assert!(!f.admits_stored(Some(&wanted)) && !f.admits_stored(None));
}
#[test]
fn unwants_reports_registration_and_last_requester_drops_type() {
let mut f = DestinationTypeFilter::default();