Dedup attestation constructor logic in attester cache

This commit is contained in:
dapplion
2024-06-17 22:17:48 +02:00
parent 2634a1f1a6
commit dec7cff9c7
4 changed files with 39 additions and 43 deletions

View File

@@ -1,5 +1,6 @@
use crate::slot_data::SlotData;
use crate::{test_utils::TestRandom, Hash256, Slot};
use crate::{Checkpoint, ForkName};
use derivative::Derivative;
use safe_arith::ArithError;
use serde::{Deserialize, Serialize};
@@ -89,20 +90,29 @@ impl<E: EthSpec> Hash for Attestation<E> {
impl<E: EthSpec> Attestation<E> {
pub fn empty_for_signing(
committee_index: usize,
committee_index: u64,
committee_length: usize,
data: AttestationData,
slot: Slot,
beacon_block_root: Hash256,
source: Checkpoint,
target: Checkpoint,
spec: &ChainSpec,
) -> Result<Self, Error> {
if spec.fork_name_at_slot::<E>(data.slot).electra_enabled() {
if spec.fork_name_at_slot::<E>(slot) >= ForkName::Electra {
let mut committee_bits: BitVector<E::MaxCommitteesPerSlot> = BitVector::default();
committee_bits
.set(committee_index, true)
.set(committee_index as usize, true)
.map_err(|_| Error::InvalidCommitteeIndex)?;
Ok(Attestation::Electra(AttestationElectra {
aggregation_bits: BitList::with_capacity(committee_length)
.map_err(|_| Error::InvalidCommitteeLength)?,
data,
data: AttestationData {
slot,
index: 0u64,
beacon_block_root,
source,
target,
},
committee_bits,
signature: AggregateSignature::infinity(),
}))
@@ -110,7 +120,13 @@ impl<E: EthSpec> Attestation<E> {
Ok(Attestation::Base(AttestationBase {
aggregation_bits: BitList::with_capacity(committee_length)
.map_err(|_| Error::InvalidCommitteeLength)?,
data,
data: AttestationData {
slot,
index: committee_index,
beacon_block_root,
source,
target,
},
signature: AggregateSignature::infinity(),
}))
}