This commit is contained in:
John Smith 2023-03-05 18:16:01 +00:00
parent 0daffab4a7
commit 9bc907fddd
3 changed files with 15 additions and 11 deletions

View File

@ -16,7 +16,7 @@ fn _get_route_permutation_count(hop_count: usize) -> usize {
(3..hop_count).into_iter().fold(2usize, |acc, x| acc * x) (3..hop_count).into_iter().fold(2usize, |acc, x| acc * x)
} }
pub type PermReturnType = (Vec<usize>, bool); pub type PermReturnType = (Vec<usize>, bool);
pub type PermFunc<'t> = Box<dyn Fn(&[usize]) -> Option<PermReturnType> + Send + 't>; pub type PermFunc<'t> = Box<dyn FnMut(&[usize]) -> Option<PermReturnType> + Send + 't>;
/// get the route permutation at particular 'perm' index, starting at the 'start' index /// get the route permutation at particular 'perm' index, starting at the 'start' index
/// for a set of 'hop_count' nodes. the first node is always fixed, and the maximum /// for a set of 'hop_count' nodes. the first node is always fixed, and the maximum
@ -25,7 +25,7 @@ pub type PermFunc<'t> = Box<dyn Fn(&[usize]) -> Option<PermReturnType> + Send +
pub fn with_route_permutations( pub fn with_route_permutations(
hop_count: usize, hop_count: usize,
start: usize, start: usize,
f: &PermFunc, f: &mut PermFunc,
) -> Option<PermReturnType> { ) -> Option<PermReturnType> {
if hop_count == 0 { if hop_count == 0 {
unreachable!(); unreachable!();
@ -44,7 +44,7 @@ pub fn with_route_permutations(
fn heaps_permutation( fn heaps_permutation(
permutation: &mut [usize], permutation: &mut [usize],
size: usize, size: usize,
f: &PermFunc, f: &mut PermFunc,
) -> Option<PermReturnType> { ) -> Option<PermReturnType> {
if size == 1 { if size == 1 {
return f(&permutation); return f(&permutation);

View File

@ -168,7 +168,7 @@ impl RouteSpecStore {
fn allocate_route_inner( fn allocate_route_inner(
&self, &self,
inner: &mut RouteSpecStoreInner, inner: &mut RouteSpecStoreInner,
rti: &RoutingTableInner, rti: &mut RoutingTableInner,
crypto_kinds: &[CryptoKind], crypto_kinds: &[CryptoKind],
stability: Stability, stability: Stability,
sequencing: Sequencing, sequencing: Sequencing,
@ -351,7 +351,7 @@ impl RouteSpecStore {
let nodes_pi: Vec<PeerInfo> = nodes.iter().map(|nr| nr.locked(rti).make_peer_info(RoutingDomain::PublicInternet).unwrap()).collect(); let nodes_pi: Vec<PeerInfo> = nodes.iter().map(|nr| nr.locked(rti).make_peer_info(RoutingDomain::PublicInternet).unwrap()).collect();
// Now go through nodes and try to build a route we haven't seen yet // Now go through nodes and try to build a route we haven't seen yet
let perm_func = Box::new(|permutation: &[usize]| { let mut perm_func = Box::new(|permutation: &[usize]| {
/// Get the hop cache key for a particular route permutation /// Get the hop cache key for a particular route permutation
/// uses the same algorithm as RouteSetSpecDetail::make_cache_key /// uses the same algorithm as RouteSetSpecDetail::make_cache_key
@ -372,7 +372,7 @@ impl RouteSpecStore {
// Ensure the route doesn't contain both a node and its relay // Ensure the route doesn't contain both a node and its relay
let mut seen_nodes: HashSet<TypedKey> = HashSet::new(); let mut seen_nodes: HashSet<TypedKey> = HashSet::new();
for n in permutation { for n in permutation {
let node = nodes.get(*n).unwrap().locked(rti); let node = nodes.get(*n).unwrap().locked_mut(rti);
if !seen_nodes.insert(node.best_node_id()) { if !seen_nodes.insert(node.best_node_id()) {
// Already seen this node, should not be in the route twice // Already seen this node, should not be in the route twice
return None; return None;
@ -471,7 +471,7 @@ impl RouteSpecStore {
for start in 0..(nodes.len() - hop_count) { for start in 0..(nodes.len() - hop_count) {
// Try the permutations available starting with 'start' // Try the permutations available starting with 'start'
if let Some((rn, cds)) = with_route_permutations(hop_count, start, &perm_func) { if let Some((rn, cds)) = with_route_permutations(hop_count, start, &mut perm_func) {
route_nodes = rn; route_nodes = rn;
can_do_sequenced = cds; can_do_sequenced = cds;
break; break;
@ -1070,7 +1070,7 @@ impl RouteSpecStore {
fn get_route_for_safety_spec_inner( fn get_route_for_safety_spec_inner(
&self, &self,
inner: &mut RouteSpecStoreInner, inner: &mut RouteSpecStoreInner,
rti: &RoutingTableInner, rti: &mut RoutingTableInner,
crypto_kind: CryptoKind, crypto_kind: CryptoKind,
safety_spec: &SafetySpec, safety_spec: &SafetySpec,
direction: DirectionSet, direction: DirectionSet,
@ -1146,7 +1146,7 @@ impl RouteSpecStore {
) -> EyreResult<Option<PublicKey>> { ) -> EyreResult<Option<PublicKey>> {
let inner = &mut *self.inner.lock(); let inner = &mut *self.inner.lock();
let routing_table = self.unlocked_inner.routing_table.clone(); let routing_table = self.unlocked_inner.routing_table.clone();
let rti = &*routing_table.inner.read(); let rti = &mut *routing_table.inner.write();
Ok(self.get_route_for_safety_spec_inner( Ok(self.get_route_for_safety_spec_inner(
inner, inner,

View File

@ -993,12 +993,16 @@ impl RoutingTableInner {
// add all nodes that match filter // add all nodes that match filter
self.with_entries(cur_ts, BucketEntryState::Unreliable, |rti, v| { self.with_entries(cur_ts, BucketEntryState::Unreliable, |rti, v| {
// Apply filter // Apply filter
let mut filtered = false;
for filter in &mut filters { for filter in &mut filters {
if filter(rti, Some(v.clone())) { if !filter(rti, Some(v.clone())) {
nodes.push(Some(v.clone())); filtered = true;
break; break;
} }
} }
if !filtered {
nodes.push(Some(v.clone()));
}
Option::<()>::None Option::<()>::None
}); });