mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-18 12:22:51 +00:00
## Issue Addressed N/A ## Proposed Changes Refactor attestation service to send out requests to find peers for subnets as soon as we get attestation duties. Earlier, we had much more involved logic to send the discovery requests to the discovery service only 6 slots before the attestation slot. Now that discovery is much smarter with grouped queries, the complexity in attestation service can be reduced considerably. Co-authored-by: Age Manning <Age@AgeManning.com>
53 lines
1.6 KiB
Rust
53 lines
1.6 KiB
Rust
///! The subnet predicate used for searching for a particular subnet.
|
|
use super::*;
|
|
use slog::{debug, trace};
|
|
use std::ops::Deref;
|
|
|
|
/// Returns the predicate for a given subnet.
|
|
pub fn subnet_predicate<TSpec>(
|
|
subnet_ids: Vec<SubnetId>,
|
|
log: &slog::Logger,
|
|
) -> impl Fn(&Enr) -> bool + Send
|
|
where
|
|
TSpec: EthSpec,
|
|
{
|
|
let log_clone = log.clone();
|
|
|
|
move |enr: &Enr| {
|
|
if let Some(bitfield_bytes) = enr.get(BITFIELD_ENR_KEY) {
|
|
let bitfield = match BitVector::<TSpec::SubnetBitfieldLength>::from_ssz_bytes(
|
|
bitfield_bytes,
|
|
) {
|
|
Ok(v) => v,
|
|
Err(e) => {
|
|
warn!(log_clone, "Could not decode ENR bitfield for peer"; "peer_id" => format!("{}", enr.peer_id()), "error" => format!("{:?}", e));
|
|
return false;
|
|
}
|
|
};
|
|
|
|
let matches: Vec<&SubnetId> = subnet_ids
|
|
.iter()
|
|
.filter(|id| bitfield.get(**id.deref() as usize).unwrap_or(false))
|
|
.collect();
|
|
|
|
if matches.is_empty() {
|
|
trace!(
|
|
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
|
|
}
|
|
}
|