Ensure /eth/v2/beacon/pool/attestations honors committee_index (#7298)

#7294


  Fix the filtering logic so that we actually filter by committee index for both `Base` and `Electra` attestations.

Added a tiny optimization when calculating committee_index to prevent unneeded memory allocations

Added a regression test
This commit is contained in:
Eitan Seri-Levi
2025-04-10 21:47:30 -07:00
committed by GitHub
parent ef8ec35ac5
commit af51d50b05
5 changed files with 143 additions and 19 deletions

View File

@@ -1,6 +1,6 @@
use crate::AttestationStats;
use itertools::Itertools;
use std::collections::{BTreeMap, HashMap};
use std::collections::{BTreeMap, HashMap, HashSet};
use types::{
attestation::{AttestationBase, AttestationElectra},
superstruct, AggregateSignature, Attestation, AttestationData, BeaconState, BitList, BitVector,
@@ -119,6 +119,18 @@ impl<E: EthSpec> CompactAttestationRef<'_, E> {
}
}
pub fn get_committee_indices_map(&self) -> HashSet<u64> {
match self.indexed {
CompactIndexedAttestation::Base(_) => HashSet::from([self.data.index]),
CompactIndexedAttestation::Electra(indexed_att) => indexed_att
.committee_bits
.iter()
.enumerate()
.filter_map(|(index, bit)| if bit { Some(index as u64) } else { None })
.collect(),
}
}
pub fn clone_as_attestation(&self) -> Attestation<E> {
match self.indexed {
CompactIndexedAttestation::Base(indexed_att) => Attestation::Base(AttestationBase {
@@ -268,7 +280,11 @@ impl<E: EthSpec> CompactIndexedAttestationElectra<E> {
}
pub fn committee_index(&self) -> Option<u64> {
self.get_committee_indices().first().copied()
self.committee_bits
.iter()
.enumerate()
.find(|&(_, bit)| bit)
.map(|(index, _)| index as u64)
}
pub fn get_committee_indices(&self) -> Vec<u64> {

View File

@@ -1,5 +1,5 @@
mod attestation;
mod attestation_storage;
pub mod attestation_storage;
mod attester_slashing;
mod bls_to_execution_changes;
mod max_cover;
@@ -47,7 +47,7 @@ type SyncContributions<E> = RwLock<HashMap<SyncAggregateId, Vec<SyncCommitteeCon
#[derive(Default, Debug)]
pub struct OperationPool<E: EthSpec + Default> {
/// Map from attestation ID (see below) to vectors of attestations.
attestations: RwLock<AttestationMap<E>>,
pub attestations: RwLock<AttestationMap<E>>,
/// Map from sync aggregate ID to the best `SyncCommitteeContribution`s seen for that ID.
sync_contributions: SyncContributions<E>,
/// Set of attester slashings, and the fork version they were verified against.
@@ -673,12 +673,12 @@ impl<E: EthSpec> OperationPool<E> {
/// This method may return objects that are invalid for block inclusion.
pub fn get_filtered_attestations<F>(&self, filter: F) -> Vec<Attestation<E>>
where
F: Fn(&AttestationData) -> bool,
F: Fn(&AttestationData, HashSet<u64>) -> bool,
{
self.attestations
.read()
.iter()
.filter(|att| filter(&att.attestation_data()))
.filter(|att| filter(&att.attestation_data(), att.get_committee_indices_map()))
.map(|att| att.clone_as_attestation())
.collect()
}