Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AsReference trait #15

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions beacon_node/beacon_chain/src/attestation_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ impl<'a, T: BeaconChainTypes> IndexedAggregatedAttestation<'a, T> {
if chain
.observed_attestations
.write()
.is_known_subset(attestation, attestation_data_root)
.is_known_subset(attestation.to_ref(), attestation_data_root)
.map_err(|e| Error::BeaconChainError(e.into()))?
{
metrics::inc_counter(&metrics::AGGREGATED_ATTESTATION_SUBSETS);
Expand Down Expand Up @@ -597,7 +597,7 @@ impl<'a, T: BeaconChainTypes> VerifiedAggregatedAttestation<'a, T> {
if let ObserveOutcome::Subset = chain
.observed_attestations
.write()
.observe_item(attestation, Some(attestation_data_root))
.observe_item(attestation.to_ref(), Some(attestation_data_root))
.map_err(|e| Error::BeaconChainError(e.into()))?
{
metrics::inc_counter(&metrics::AGGREGATED_ATTESTATION_SUBSETS);
Expand Down
17 changes: 9 additions & 8 deletions beacon_node/beacon_chain/src/beacon_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3839,7 +3839,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
if block.slot() + 2 * T::EthSpec::slots_per_epoch() >= current_slot {
metrics::observe(
&metrics::OPERATIONS_PER_BLOCK_ATTESTATION,
block.body().attestations().len() as f64,
block.body().attestations().count() as f64,
);

if let Ok(sync_aggregate) = block.body().sync_aggregate() {
Expand Down Expand Up @@ -4859,7 +4859,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
metrics::start_timer(&metrics::BLOCK_PRODUCTION_UNAGGREGATED_TIMES);
for attestation in self.naive_aggregation_pool.read().iter() {
let import = |attestation: &Attestation<T::EthSpec>| {
let attesting_indices = get_attesting_indices_from_state(&state, attestation)?;
let attesting_indices =
get_attesting_indices_from_state(&state, attestation.to_ref())?;
self.op_pool
.insert_attestation(attestation.clone(), attesting_indices)
};
Expand Down Expand Up @@ -4909,7 +4910,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
attestations.retain(|att| {
verify_attestation_for_block_inclusion(
&state,
att,
att.to_ref(),
&mut tmp_ctxt,
VerifySignatures::True,
&self.spec,
Expand Down Expand Up @@ -5050,12 +5051,12 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
(base, electra)
},
);
let (attestations_base, attestations_electra) = attester_slashings.into_iter().fold(
let (attestations_base, attestations_electra) = attestations.into_iter().fold(
(Vec::new(), Vec::new()),
|(mut base, mut electra), slashing| {
match slashing {
AttesterSlashing::Base(slashing) => base.push(slashing),
AttesterSlashing::Electra(slashing) => electra.push(slashing),
|(mut base, mut electra), attestation| {
match attestation {
Attestation::Base(attestation) => base.push(attestation),
Attestation::Electra(attestation) => electra.push(attestation),
}
(base, electra)
},
Expand Down
68 changes: 52 additions & 16 deletions beacon_node/beacon_chain/src/observed_aggregates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use types::consts::altair::{
SYNC_COMMITTEE_SUBNET_COUNT, TARGET_AGGREGATORS_PER_SYNC_SUBCOMMITTEE,
};
use types::slot_data::SlotData;
use types::{Attestation, EthSpec, Hash256, Slot, SyncCommitteeContribution};
use types::{Attestation, AttestationRef, EthSpec, Hash256, Slot, SyncCommitteeContribution};

pub type ObservedSyncContributions<E> = ObservedAggregates<
SyncCommitteeContribution<E>,
Expand Down Expand Up @@ -102,30 +102,30 @@ pub trait SubsetItem {
fn root(&self) -> Hash256;
}

impl<E: EthSpec> SubsetItem for Attestation<E> {
impl<'a, E: EthSpec> SubsetItem for AttestationRef<'a, E> {
type Item = BitList<E::MaxValidatorsPerCommittee>;
fn is_subset(&self, other: &Self::Item) -> bool {
match self {
Attestation::Base(att) => att.aggregation_bits.is_subset(other),
Self::Base(att) => att.aggregation_bits.is_subset(other),
// TODO(electra) implement electra variant
Attestation::Electra(_) => todo!(),
Self::Electra(_) => todo!(),
}
}

fn is_superset(&self, other: &Self::Item) -> bool {
match self {
Attestation::Base(att) => other.is_subset(&att.aggregation_bits),
Self::Base(att) => other.is_subset(&att.aggregation_bits),
// TODO(electra) implement electra variant
Attestation::Electra(_) => todo!(),
Self::Electra(_) => todo!(),
}
}

/// Returns the sync contribution aggregation bits.
fn get_item(&self) -> Self::Item {
match self {
Attestation::Base(att) => att.aggregation_bits.clone(),
Self::Base(att) => att.aggregation_bits.clone(),
// TODO(electra) implement electra variant
Attestation::Electra(_) => todo!(),
Self::Electra(_) => todo!(),
}
}

Expand All @@ -135,7 +135,7 @@ impl<E: EthSpec> SubsetItem for Attestation<E> {
}
}

impl<E: EthSpec> SubsetItem for SyncCommitteeContribution<E> {
impl<'a, E: EthSpec> SubsetItem for &'a SyncCommitteeContribution<E> {
type Item = BitVector<E::SyncSubcommitteeSize>;
fn is_subset(&self, other: &Self::Item) -> bool {
self.aggregation_bits.is_subset(other)
Expand Down Expand Up @@ -208,7 +208,7 @@ impl<I> SlotHashSet<I> {
/// Store the items in self so future observations recognise its existence.
pub fn observe_item<S: SlotData + SubsetItem<Item = I>>(
&mut self,
item: &S,
item: S,
root: Hash256,
) -> Result<ObserveOutcome, Error> {
if item.get_slot() != self.slot {
Expand Down Expand Up @@ -254,7 +254,7 @@ impl<I> SlotHashSet<I> {
/// the given root and slot.
pub fn is_known_subset<S: SlotData + SubsetItem<Item = I>>(
&self,
item: &S,
item: S,
root: Hash256,
) -> Result<bool, Error> {
if item.get_slot() != self.slot {
Expand All @@ -276,16 +276,43 @@ impl<I> SlotHashSet<I> {
}
}

/// Trait for observable items that can be observed from their reference type.
///
/// This is used to make observations for `Attestation`s from `AttestationRef`s.
pub trait AsReference {
type Reference<'a>
where
Self: 'a;

fn as_reference(&self) -> Self::Reference<'_>;
}

impl<E: EthSpec> AsReference for Attestation<E> {
type Reference<'a> = AttestationRef<'a, E>;

fn as_reference(&self) -> AttestationRef<'_, E> {
self.to_ref()
}
}

impl<E: EthSpec> AsReference for SyncCommitteeContribution<E> {
type Reference<'a> = &'a Self;

fn as_reference(&self) -> &Self {
self
}
}

/// Stores the roots of objects for some number of `Slots`, so we can determine if
/// these have previously been seen on the network.
pub struct ObservedAggregates<T: SlotData + Consts, E: EthSpec, I> {
pub struct ObservedAggregates<T: Consts + AsReference, E: EthSpec, I> {
lowest_permissible_slot: Slot,
sets: Vec<SlotHashSet<I>>,
_phantom_spec: PhantomData<E>,
_phantom_tree_hash: PhantomData<T>,
}

impl<T: SlotData + Consts, E: EthSpec, I> Default for ObservedAggregates<T, E, I> {
impl<T: Consts + AsReference, E: EthSpec, I> Default for ObservedAggregates<T, E, I> {
fn default() -> Self {
Self {
lowest_permissible_slot: Slot::new(0),
Expand All @@ -296,13 +323,18 @@ impl<T: SlotData + Consts, E: EthSpec, I> Default for ObservedAggregates<T, E, I
}
}

impl<T: SlotData + Consts + SubsetItem<Item = I>, E: EthSpec, I> ObservedAggregates<T, E, I> {
impl<T, E, I> ObservedAggregates<T, E, I>
where
T: Consts + AsReference,
E: EthSpec,
for<'a> T::Reference<'a>: SubsetItem<Item = I> + SlotData,
{
/// Store `item` in `self` keyed at `root`.
///
/// `root` must equal `item.root::<SubsetItem>()`.
pub fn observe_item(
&mut self,
item: &T,
item: T::Reference<'_>,
root_opt: Option<Hash256>,
) -> Result<ObserveOutcome, Error> {
let index = self.get_set_index(item.get_slot())?;
Expand All @@ -319,7 +351,11 @@ impl<T: SlotData + Consts + SubsetItem<Item = I>, E: EthSpec, I> ObservedAggrega
///
/// `root` must equal `item.root::<SubsetItem>()`.
#[allow(clippy::wrong_self_convention)]
pub fn is_known_subset(&mut self, item: &T, root: Hash256) -> Result<bool, Error> {
pub fn is_known_subset(
&mut self,
item: T::Reference<'_>,
root: Hash256,
) -> Result<bool, Error> {
let index = self.get_set_index(item.get_slot())?;

self.sets
Expand Down
6 changes: 6 additions & 0 deletions consensus/types/src/attestation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,12 @@ impl<E: EthSpec> SlotData for Attestation<E> {
}
}

impl<'a, E: EthSpec> SlotData for AttestationRef<'a, E> {
fn get_slot(&self) -> Slot {
self.data().slot
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
6 changes: 6 additions & 0 deletions consensus/types/src/sync_committee_contribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ impl<E: EthSpec> SlotData for SyncCommitteeContribution<E> {
}
}

impl<E: EthSpec> SlotData for &SyncCommitteeContribution<E> {
fn get_slot(&self) -> Slot {
self.slot
}
}

impl SlotData for SyncContributionData {
fn get_slot(&self) -> Slot {
self.slot
Expand Down
Loading