Remove unwraps in Attestation construction

This commit is contained in:
dapplion
2024-06-17 16:17:04 +02:00
parent d87541c045
commit dd0d5e2d93
2 changed files with 48 additions and 19 deletions

View File

@@ -1,4 +1,5 @@
use crate::slot_data::SlotData; use crate::slot_data::SlotData;
use crate::ForkName;
use crate::{test_utils::TestRandom, Hash256, Slot}; use crate::{test_utils::TestRandom, Hash256, Slot};
use derivative::Derivative; use derivative::Derivative;
use rand::RngCore; use rand::RngCore;
@@ -22,6 +23,8 @@ pub enum Error {
AlreadySigned(usize), AlreadySigned(usize),
SubnetCountIsZero(ArithError), SubnetCountIsZero(ArithError),
IncorrectStateVariant, IncorrectStateVariant,
InvalidCommitteeLength,
InvalidCommitteeIndex,
} }
#[superstruct( #[superstruct(
@@ -104,6 +107,34 @@ impl<E: EthSpec> Hash for Attestation<E> {
} }
impl<E: EthSpec> Attestation<E> { impl<E: EthSpec> Attestation<E> {
pub fn empty_for_signing(
committee_index: usize,
committee_length: usize,
data: AttestationData,
spec: &ChainSpec,
) -> Result<Self, Error> {
if spec.fork_name_at_slot::<E>(data.slot) >= ForkName::Electra {
let mut committee_bits: BitVector<E::MaxCommitteesPerSlot> = BitVector::default();
committee_bits
.set(committee_index, true)
.map_err(|_| Error::InvalidCommitteeIndex)?;
Ok(Attestation::Electra(AttestationElectra {
aggregation_bits: BitList::with_capacity(committee_length)
.map_err(|_| Error::InvalidCommitteeLength)?,
data,
committee_bits,
signature: AggregateSignature::infinity(),
}))
} else {
Ok(Attestation::Base(AttestationBase {
aggregation_bits: BitList::with_capacity(committee_length)
.map_err(|_| Error::InvalidCommitteeLength)?,
data,
signature: AggregateSignature::infinity(),
}))
}
}
/// Aggregate another Attestation into this one. /// Aggregate another Attestation into this one.
/// ///
/// The aggregation bitfields must be disjoint, and the data must be the same. /// The aggregation bitfields must be disjoint, and the data must be the same.

View File

@@ -386,25 +386,23 @@ impl<T: SlotClock + 'static, E: EthSpec> AttestationService<T, E> {
return None; return None;
} }
let mut attestation = if fork_name >= ForkName::Electra { let mut attestation = match Attestation::<E>::empty_for_signing(
let mut committee_bits: BitVector<E::MaxCommitteesPerSlot> = BitVector::default(); duty.committee_index as usize,
committee_bits duty.committee_length as usize,
.set(duty.committee_index as usize, true) attestation_data.clone(),
.unwrap(); &self.context.eth2_config.spec,
Attestation::Electra(AttestationElectra { ) {
aggregation_bits: BitList::with_capacity(duty.committee_length as usize) Ok(attestation) => attestation,
.unwrap(), Err(err) => {
data: attestation_data.clone(), crit!(
committee_bits, log,
signature: AggregateSignature::infinity(), "Invalid validator duties during signing";
}) "validator" => ?duty.pubkey,
} else { "duty" => ?duty,
Attestation::Base(AttestationBase { "err" => ?err,
aggregation_bits: BitList::with_capacity(duty.committee_length as usize) );
.unwrap(), return None;
data: attestation_data.clone(), }
signature: AggregateSignature::infinity(),
})
}; };
match self match self