superstruct the AttesterSlashing (#5636)

* `superstruct` Attester Fork Variants

* Push a little further

* Deal with Encode / Decode of AttesterSlashing

* not so sure about this..

* Stop Encode/Decode Bounds from Propagating Out

* Tons of Changes..

* More Conversions to AttestationRef

* Add AsReference trait (#15)

* Add AsReference trait

* Fix some snafus

* Got it Compiling! :D

* Got Tests Building

* Get beacon chain tests compiling

---------

Co-authored-by: Michael Sproul <micsproul@gmail.com>
This commit is contained in:
ethDreamer
2024-05-02 18:00:21 -05:00
committed by GitHub
parent 3b7132bc0d
commit e6c7f145dd
53 changed files with 1405 additions and 437 deletions

View File

@@ -247,13 +247,12 @@ where
) -> Result<()> {
self.sets
.sets
.reserve(block.message().body().attester_slashings().len() * 2);
.reserve(block.message().body().attester_slashings_len() * 2);
block
.message()
.body()
.attester_slashings()
.iter()
.try_for_each(|attester_slashing| {
let (set_1, set_2) = attester_slashing_signature_sets(
self.state,
@@ -277,13 +276,12 @@ where
) -> Result<()> {
self.sets
.sets
.reserve(block.message().body().attestations().len());
.reserve(block.message().body().attestations_len());
block
.message()
.body()
.attestations()
.iter()
.try_for_each(|attestation| {
let indexed_attestation = ctxt.get_indexed_attestation(self.state, attestation)?;

View File

@@ -13,7 +13,7 @@ fn error(reason: Invalid) -> BlockOperationError<Invalid> {
/// Verify an `IndexedAttestation`.
pub fn is_valid_indexed_attestation<E: EthSpec>(
state: &BeaconState<E>,
indexed_attestation: &IndexedAttestation<E>,
indexed_attestation: IndexedAttestationRef<E>,
verify_signatures: VerifySignatures,
spec: &ChainSpec,
) -> Result<()> {

View File

@@ -46,13 +46,16 @@ pub mod base {
///
/// Returns `Ok(())` if the validation and state updates completed successfully, otherwise returns
/// an `Err` describing the invalid object or cause of failure.
pub fn process_attestations<E: EthSpec>(
pub fn process_attestations<'a, E: EthSpec, I>(
state: &mut BeaconState<E>,
attestations: &[Attestation<E>],
attestations: I,
verify_signatures: VerifySignatures,
ctxt: &mut ConsensusContext<E>,
spec: &ChainSpec,
) -> Result<(), BlockProcessingError> {
) -> Result<(), BlockProcessingError>
where
I: Iterator<Item = AttestationRef<'a, E>>,
{
// Ensure required caches are all built. These should be no-ops during regular operation.
state.build_committee_cache(RelativeEpoch::Current, spec)?;
state.build_committee_cache(RelativeEpoch::Previous, spec)?;
@@ -63,7 +66,7 @@ pub mod base {
let proposer_index = ctxt.get_proposer_index(state, spec)?;
// Verify and apply each attestation.
for (i, attestation) in attestations.iter().enumerate() {
for (i, attestation) in attestations.enumerate() {
verify_attestation_for_block_inclusion(
state,
attestation,
@@ -74,7 +77,7 @@ pub mod base {
.map_err(|e| e.into_with_index(i))?;
match attestation {
Attestation::Base(att) => {
AttestationRef::Base(att) => {
let pending_attestation = PendingAttestation {
aggregation_bits: att.aggregation_bits.clone(),
data: att.data.clone(),
@@ -94,7 +97,7 @@ pub mod base {
.push(pending_attestation)?;
}
}
Attestation::Electra(_) => {
AttestationRef::Electra(_) => {
// TODO(electra) pending attestations are only phase 0
// so we should just raise a relevant error here
todo!()
@@ -110,24 +113,24 @@ pub mod altair_deneb {
use super::*;
use crate::common::update_progressive_balances_cache::update_progressive_balances_on_attestation;
pub fn process_attestations<E: EthSpec>(
pub fn process_attestations<'a, E: EthSpec, I>(
state: &mut BeaconState<E>,
attestations: &[Attestation<E>],
attestations: I,
verify_signatures: VerifySignatures,
ctxt: &mut ConsensusContext<E>,
spec: &ChainSpec,
) -> Result<(), BlockProcessingError> {
attestations
.iter()
.enumerate()
.try_for_each(|(i, attestation)| {
process_attestation(state, attestation, i, ctxt, verify_signatures, spec)
})
) -> Result<(), BlockProcessingError>
where
I: Iterator<Item = AttestationRef<'a, E>>,
{
attestations.enumerate().try_for_each(|(i, attestation)| {
process_attestation(state, attestation, i, ctxt, verify_signatures, spec)
})
}
pub fn process_attestation<E: EthSpec>(
state: &mut BeaconState<E>,
attestation: &Attestation<E>,
attestation: AttestationRef<E>,
att_index: usize,
ctxt: &mut ConsensusContext<E>,
verify_signatures: VerifySignatures,
@@ -238,16 +241,19 @@ pub fn process_proposer_slashings<E: EthSpec>(
///
/// Returns `Ok(())` if the validation and state updates completed successfully, otherwise returns
/// an `Err` describing the invalid object or cause of failure.
pub fn process_attester_slashings<E: EthSpec>(
pub fn process_attester_slashings<'a, E: EthSpec, I>(
state: &mut BeaconState<E>,
attester_slashings: &[AttesterSlashing<E>],
attester_slashings: I,
verify_signatures: VerifySignatures,
ctxt: &mut ConsensusContext<E>,
spec: &ChainSpec,
) -> Result<(), BlockProcessingError> {
) -> Result<(), BlockProcessingError>
where
I: Iterator<Item = AttesterSlashingRef<'a, E>>,
{
state.build_slashings_cache()?;
for (i, attester_slashing) in attester_slashings.iter().enumerate() {
for (i, attester_slashing) in attester_slashings.enumerate() {
let slashable_indices =
verify_attester_slashing(state, attester_slashing, verify_signatures, spec)
.map_err(|e| e.into_with_index(i))?;

View File

@@ -7,10 +7,10 @@ use ssz::DecodeError;
use std::borrow::Cow;
use tree_hash::TreeHash;
use types::{
AbstractExecPayload, AggregateSignature, AttesterSlashing, BeaconBlockRef, BeaconState,
AbstractExecPayload, AggregateSignature, AttesterSlashingRef, BeaconBlockRef, BeaconState,
BeaconStateError, ChainSpec, DepositData, Domain, Epoch, EthSpec, Fork, Hash256,
InconsistentFork, IndexedAttestation, ProposerSlashing, PublicKey, PublicKeyBytes, Signature,
SignedAggregateAndProof, SignedBeaconBlock, SignedBeaconBlockHeader,
InconsistentFork, IndexedAttestation, IndexedAttestationRef, ProposerSlashing, PublicKey,
PublicKeyBytes, Signature, SignedAggregateAndProof, SignedBeaconBlock, SignedBeaconBlockHeader,
SignedBlsToExecutionChange, SignedContributionAndProof, SignedRoot, SignedVoluntaryExit,
SigningData, Slot, SyncAggregate, SyncAggregatorSelectionData, Unsigned,
};
@@ -272,7 +272,7 @@ pub fn indexed_attestation_signature_set<'a, 'b, E, F>(
state: &'a BeaconState<E>,
get_pubkey: F,
signature: &'a AggregateSignature,
indexed_attestation: &'b IndexedAttestation<E>,
indexed_attestation: IndexedAttestationRef<'b, E>,
spec: &'a ChainSpec,
) -> Result<SignatureSet<'a>>
where
@@ -335,7 +335,7 @@ where
pub fn attester_slashing_signature_sets<'a, E, F>(
state: &'a BeaconState<E>,
get_pubkey: F,
attester_slashing: &'a AttesterSlashing<E>,
attester_slashing: AttesterSlashingRef<'a, E>,
spec: &'a ChainSpec,
) -> Result<(SignatureSet<'a>, SignatureSet<'a>)>
where
@@ -346,15 +346,15 @@ where
indexed_attestation_signature_set(
state,
get_pubkey.clone(),
attester_slashing.attestation_1.signature(),
&attester_slashing.attestation_1,
attester_slashing.attestation_1().signature(),
attester_slashing.attestation_1(),
spec,
)?,
indexed_attestation_signature_set(
state,
get_pubkey,
attester_slashing.attestation_2.signature(),
&attester_slashing.attestation_2,
attester_slashing.attestation_2().signature(),
attester_slashing.attestation_2(),
spec,
)?,
))

View File

@@ -388,7 +388,12 @@ async fn invalid_attestation_no_committee_for_index() {
.clone()
.deconstruct()
.0;
head_block.to_mut().body_mut().attestations_mut()[0]
head_block
.to_mut()
.body_mut()
.attestations_mut()
.next()
.unwrap()
.data_mut()
.index += 1;
let mut ctxt = ConsensusContext::new(state.slot());
@@ -423,10 +428,21 @@ async fn invalid_attestation_wrong_justified_checkpoint() {
.clone()
.deconstruct()
.0;
let old_justified_checkpoint = head_block.body().attestations()[0].data().source;
let old_justified_checkpoint = head_block
.body()
.attestations()
.next()
.unwrap()
.data()
.source;
let mut new_justified_checkpoint = old_justified_checkpoint;
new_justified_checkpoint.epoch += Epoch::new(1);
head_block.to_mut().body_mut().attestations_mut()[0]
head_block
.to_mut()
.body_mut()
.attestations_mut()
.next()
.unwrap()
.data_mut()
.source = new_justified_checkpoint;
@@ -467,7 +483,12 @@ async fn invalid_attestation_bad_aggregation_bitfield_len() {
.clone()
.deconstruct()
.0;
*head_block.to_mut().body_mut().attestations_mut()[0]
*head_block
.to_mut()
.body_mut()
.attestations_mut()
.next()
.unwrap()
.aggregation_bits_base_mut()
.unwrap() = Bitfield::with_capacity(spec.target_committee_size).unwrap();
@@ -502,8 +523,13 @@ async fn invalid_attestation_bad_signature() {
.clone()
.deconstruct()
.0;
*head_block.to_mut().body_mut().attestations_mut()[0].signature_mut() =
AggregateSignature::empty();
*head_block
.to_mut()
.body_mut()
.attestations_mut()
.next()
.unwrap()
.signature_mut() = AggregateSignature::empty();
let mut ctxt = ConsensusContext::new(state.slot());
let result = process_operations::process_attestations(
@@ -538,9 +564,14 @@ async fn invalid_attestation_included_too_early() {
.clone()
.deconstruct()
.0;
let new_attesation_slot = head_block.body().attestations()[0].data().slot
let new_attesation_slot = head_block.body().attestations().next().unwrap().data().slot
+ Slot::new(MainnetEthSpec::slots_per_epoch());
head_block.to_mut().body_mut().attestations_mut()[0]
head_block
.to_mut()
.body_mut()
.attestations_mut()
.next()
.unwrap()
.data_mut()
.slot = new_attesation_slot;
@@ -581,9 +612,14 @@ async fn invalid_attestation_included_too_late() {
.clone()
.deconstruct()
.0;
let new_attesation_slot = head_block.body().attestations()[0].data().slot
let new_attesation_slot = head_block.body().attestations().next().unwrap().data().slot
- Slot::new(MainnetEthSpec::slots_per_epoch());
head_block.to_mut().body_mut().attestations_mut()[0]
head_block
.to_mut()
.body_mut()
.attestations_mut()
.next()
.unwrap()
.data_mut()
.slot = new_attesation_slot;
@@ -621,7 +657,12 @@ async fn invalid_attestation_target_epoch_slot_mismatch() {
.clone()
.deconstruct()
.0;
head_block.to_mut().body_mut().attestations_mut()[0]
head_block
.to_mut()
.body_mut()
.attestations_mut()
.next()
.unwrap()
.data_mut()
.target
.epoch += Epoch::new(1);
@@ -657,7 +698,7 @@ async fn valid_insert_attester_slashing() {
let mut ctxt = ConsensusContext::new(state.slot());
let result = process_operations::process_attester_slashings(
&mut state,
&[attester_slashing],
[attester_slashing.to_ref()].into_iter(),
VerifySignatures::True,
&mut ctxt,
&spec,
@@ -673,13 +714,20 @@ async fn invalid_attester_slashing_not_slashable() {
let harness = get_harness::<MainnetEthSpec>(EPOCH_OFFSET, VALIDATOR_COUNT).await;
let mut attester_slashing = harness.make_attester_slashing(vec![1, 2]);
attester_slashing.attestation_1 = attester_slashing.attestation_2.clone();
match &mut attester_slashing {
AttesterSlashing::Base(ref mut attester_slashing) => {
attester_slashing.attestation_1 = attester_slashing.attestation_2.clone();
}
AttesterSlashing::Electra(ref mut attester_slashing) => {
attester_slashing.attestation_1 = attester_slashing.attestation_2.clone();
}
}
let mut state = harness.get_current_state();
let mut ctxt = ConsensusContext::new(state.slot());
let result = process_operations::process_attester_slashings(
&mut state,
&[attester_slashing],
[attester_slashing.to_ref()].into_iter(),
VerifySignatures::True,
&mut ctxt,
&spec,
@@ -701,16 +749,20 @@ async fn invalid_attester_slashing_1_invalid() {
let harness = get_harness::<MainnetEthSpec>(EPOCH_OFFSET, VALIDATOR_COUNT).await;
let mut attester_slashing = harness.make_attester_slashing(vec![1, 2]);
*attester_slashing
.attestation_1
.attesting_indices_base_mut()
.unwrap() = VariableList::from(vec![2, 1]);
match &mut attester_slashing {
AttesterSlashing::Base(ref mut attester_slashing) => {
attester_slashing.attestation_1.attesting_indices = VariableList::from(vec![2, 1]);
}
AttesterSlashing::Electra(ref mut attester_slashing) => {
attester_slashing.attestation_1.attesting_indices = VariableList::from(vec![2, 1]);
}
}
let mut state = harness.get_current_state();
let mut ctxt = ConsensusContext::new(state.slot());
let result = process_operations::process_attester_slashings(
&mut state,
&[attester_slashing],
[attester_slashing.to_ref()].into_iter(),
VerifySignatures::True,
&mut ctxt,
&spec,
@@ -735,16 +787,20 @@ async fn invalid_attester_slashing_2_invalid() {
let harness = get_harness::<MainnetEthSpec>(EPOCH_OFFSET, VALIDATOR_COUNT).await;
let mut attester_slashing = harness.make_attester_slashing(vec![1, 2]);
*attester_slashing
.attestation_2
.attesting_indices_base_mut()
.unwrap() = VariableList::from(vec![2, 1]);
match &mut attester_slashing {
AttesterSlashing::Base(ref mut attester_slashing) => {
attester_slashing.attestation_2.attesting_indices = VariableList::from(vec![2, 1]);
}
AttesterSlashing::Electra(ref mut attester_slashing) => {
attester_slashing.attestation_2.attesting_indices = VariableList::from(vec![2, 1]);
}
}
let mut state = harness.get_current_state();
let mut ctxt = ConsensusContext::new(state.slot());
let result = process_operations::process_attester_slashings(
&mut state,
&[attester_slashing],
[attester_slashing.to_ref()].into_iter(),
VerifySignatures::True,
&mut ctxt,
&spec,

View File

@@ -17,11 +17,11 @@ fn error(reason: Invalid) -> BlockOperationError<Invalid> {
/// Optionally verifies the aggregate signature, depending on `verify_signatures`.
pub fn verify_attestation_for_block_inclusion<'ctxt, E: EthSpec>(
state: &BeaconState<E>,
attestation: &Attestation<E>,
attestation: AttestationRef<'ctxt, E>,
ctxt: &'ctxt mut ConsensusContext<E>,
verify_signatures: VerifySignatures,
spec: &ChainSpec,
) -> Result<&'ctxt IndexedAttestation<E>> {
) -> Result<IndexedAttestationRef<'ctxt, E>> {
let data = attestation.data();
verify!(
@@ -61,11 +61,11 @@ pub fn verify_attestation_for_block_inclusion<'ctxt, E: EthSpec>(
/// Spec v0.12.1
pub fn verify_attestation_for_state<'ctxt, E: EthSpec>(
state: &BeaconState<E>,
attestation: &Attestation<E>,
attestation: AttestationRef<'ctxt, E>,
ctxt: &'ctxt mut ConsensusContext<E>,
verify_signatures: VerifySignatures,
spec: &ChainSpec,
) -> Result<&'ctxt IndexedAttestation<E>> {
) -> Result<IndexedAttestationRef<'ctxt, E>> {
let data = attestation.data();
verify!(
@@ -87,7 +87,7 @@ pub fn verify_attestation_for_state<'ctxt, E: EthSpec>(
///
/// Spec v0.12.1
fn verify_casper_ffg_vote<E: EthSpec>(
attestation: &Attestation<E>,
attestation: AttestationRef<E>,
state: &BeaconState<E>,
) -> Result<()> {
let data = attestation.data();

View File

@@ -18,12 +18,12 @@ fn error(reason: Invalid) -> BlockOperationError<Invalid> {
/// invalidity.
pub fn verify_attester_slashing<E: EthSpec>(
state: &BeaconState<E>,
attester_slashing: &AttesterSlashing<E>,
attester_slashing: AttesterSlashingRef<'_, E>,
verify_signatures: VerifySignatures,
spec: &ChainSpec,
) -> Result<Vec<u64>> {
let attestation_1 = &attester_slashing.attestation_1;
let attestation_2 = &attester_slashing.attestation_2;
let attestation_1 = attester_slashing.attestation_1();
let attestation_2 = attester_slashing.attestation_2();
// Spec: is_slashable_attestation_data
verify!(
@@ -45,7 +45,7 @@ pub fn verify_attester_slashing<E: EthSpec>(
/// Returns Ok(indices) if `indices.len() > 0`
pub fn get_slashable_indices<E: EthSpec>(
state: &BeaconState<E>,
attester_slashing: &AttesterSlashing<E>,
attester_slashing: AttesterSlashingRef<'_, E>,
) -> Result<Vec<u64>> {
get_slashable_indices_modular(state, attester_slashing, |_, validator| {
validator.is_slashable_at(state.current_epoch())
@@ -56,14 +56,14 @@ pub fn get_slashable_indices<E: EthSpec>(
/// for determining whether a given validator should be considered slashable.
pub fn get_slashable_indices_modular<F, E: EthSpec>(
state: &BeaconState<E>,
attester_slashing: &AttesterSlashing<E>,
attester_slashing: AttesterSlashingRef<'_, E>,
is_slashable: F,
) -> Result<Vec<u64>>
where
F: Fn(u64, &Validator) -> bool,
{
let attestation_1 = &attester_slashing.attestation_1;
let attestation_2 = &attester_slashing.attestation_2;
let attestation_1 = attester_slashing.attestation_1();
let attestation_2 = attester_slashing.attestation_2();
let attesting_indices_1 = attestation_1
.attesting_indices_iter()