Discover query grouping (#1364)

## Issue Addressed

#1281

## Proposed Changes

Groups queries for specific subnets into groups of up to 3.

## Additional Info
This commit is contained in:
realbigsean
2020-07-29 02:43:50 +00:00
parent 9ae9df806c
commit 09b40b7a5e
5 changed files with 256 additions and 139 deletions

View File

@@ -1,9 +1,10 @@
///! The subnet predicate used for searching for a particular subnet.
use super::*;
use std::ops::Deref;
/// Returns the predicate for a given subnet.
pub fn subnet_predicate<TSpec>(
subnet_id: SubnetId,
subnet_ids: Vec<SubnetId>,
log: &slog::Logger,
) -> impl Fn(&Enr) -> bool + Send
where
@@ -23,10 +24,27 @@ where
}
};
return bitfield.get(*subnet_id as usize).unwrap_or_else(|_| {
debug!(log_clone, "Peer found but not on desired subnet"; "peer_id" => format!("{}", enr.peer_id()));
false
});
let matches: Vec<&SubnetId> = subnet_ids
.iter()
.filter(|id| bitfield.get(**id.deref() as usize).unwrap_or(false))
.collect();
if matches.is_empty() {
debug!(
log_clone,
"Peer found but not on any of the desired subnets";
"peer_id" => format!("{}", enr.peer_id())
);
return false;
} else {
debug!(
log_clone,
"Peer found on desired subnet(s)";
"peer_id" => format!("{}", enr.peer_id()),
"subnets" => format!("{:?}", matches.as_slice())
);
return true;
}
}
false
}