Skip to content

Commit

Permalink
Tons of Changes..
Browse files Browse the repository at this point in the history
  • Loading branch information
ethDreamer committed Apr 30, 2024
1 parent 8231832 commit ca7a9ae
Show file tree
Hide file tree
Showing 16 changed files with 351 additions and 103 deletions.
92 changes: 79 additions & 13 deletions beacon_node/beacon_chain/src/beacon_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3820,7 +3820,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
continue;
}
};
slasher.accept_attestation(indexed_attestation.clone());
slasher.accept_attestation(indexed_attestation.clone_as_indexed_attestation());
}
}
}
Expand Down Expand Up @@ -5039,6 +5039,72 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
bls_to_execution_changes,
} = partial_beacon_block;

let (attester_slashings_base, attester_slashings_electra) =
attester_slashings.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),
}
(base, electra)
},
);
let (attestations_base, attestations_electra) = attester_slashings.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),
}
(base, electra)
},
);

// TODO(electra): figure out what should *actually* be done here when we have attestations / attester_slashings of the wrong type
match &state {
BeaconState::Base(_)
| BeaconState::Altair(_)
| BeaconState::Merge(_)
| BeaconState::Capella(_)
| BeaconState::Deneb(_) => {
if !attestations_electra.is_empty() {
error!(
self.log,
"Tried to produce block with attestations of the wrong type";
"slot" => slot,
"attestations" => attestations_electra.len(),
);
}
if !attester_slashings_electra.is_empty() {
error!(
self.log,
"Tried to produce block with attester slashings of the wrong type";
"slot" => slot,
"attester_slashings" => attester_slashings_electra.len(),
);
}
}
BeaconState::Electra(_) => {
if !attestations_base.is_empty() {
error!(
self.log,
"Tried to produce block with attestations of the wrong type";
"slot" => slot,
"attestations" => attestations_base.len(),
);
}
if !attester_slashings_base.is_empty() {
error!(
self.log,
"Tried to produce block with attester slashings of the wrong type";
"slot" => slot,
"attester_slashings" => attester_slashings_base.len(),
);
}
}
};

let (inner_block, maybe_blobs_and_proofs, execution_payload_value) = match &state {
BeaconState::Base(_) => (
BeaconBlock::Base(BeaconBlockBase {
Expand All @@ -5051,8 +5117,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
eth1_data,
graffiti,
proposer_slashings: proposer_slashings.into(),
attester_slashings: attester_slashings.into(),
attestations: attestations.into(),
attester_slashings: attester_slashings_base.into(),
attestations: attestations_base.into(),
deposits: deposits.into(),
voluntary_exits: voluntary_exits.into(),
_phantom: PhantomData,
Expand All @@ -5072,8 +5138,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
eth1_data,
graffiti,
proposer_slashings: proposer_slashings.into(),
attester_slashings: attester_slashings.into(),
attestations: attestations.into(),
attester_slashings: attester_slashings_base.into(),
attestations: attestations_base.into(),
deposits: deposits.into(),
voluntary_exits: voluntary_exits.into(),
sync_aggregate: sync_aggregate
Expand All @@ -5099,8 +5165,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
eth1_data,
graffiti,
proposer_slashings: proposer_slashings.into(),
attester_slashings: attester_slashings.into(),
attestations: attestations.into(),
attester_slashings: attester_slashings_base.into(),
attestations: attestations_base.into(),
deposits: deposits.into(),
voluntary_exits: voluntary_exits.into(),
sync_aggregate: sync_aggregate
Expand Down Expand Up @@ -5131,8 +5197,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
eth1_data,
graffiti,
proposer_slashings: proposer_slashings.into(),
attester_slashings: attester_slashings.into(),
attestations: attestations.into(),
attester_slashings: attester_slashings_base.into(),
attestations: attestations_base.into(),
deposits: deposits.into(),
voluntary_exits: voluntary_exits.into(),
sync_aggregate: sync_aggregate
Expand Down Expand Up @@ -5165,8 +5231,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
eth1_data,
graffiti,
proposer_slashings: proposer_slashings.into(),
attester_slashings: attester_slashings.into(),
attestations: attestations.into(),
attester_slashings: attester_slashings_base.into(),
attestations: attestations_base.into(),
deposits: deposits.into(),
voluntary_exits: voluntary_exits.into(),
sync_aggregate: sync_aggregate
Expand Down Expand Up @@ -5203,8 +5269,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
eth1_data,
graffiti,
proposer_slashings: proposer_slashings.into(),
attester_slashings: attester_slashings.into(),
attestations: attestations.into(),
attester_slashings: attester_slashings_electra.into(),
attestations: attestations_electra.into(),
deposits: deposits.into(),
voluntary_exits: voluntary_exits.into(),
sync_aggregate: sync_aggregate
Expand Down
8 changes: 4 additions & 4 deletions beacon_node/beacon_chain/src/validator_monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ use types::consts::altair::{
};
use types::{
Attestation, AttestationData, AttesterSlashingRef, BeaconBlockRef, BeaconState,
BeaconStateError, ChainSpec, Epoch, EthSpec, Hash256, IndexedAttestation, ProposerSlashing,
PublicKeyBytes, SignedAggregateAndProof, SignedContributionAndProof, Slot,
SyncCommitteeMessage, VoluntaryExit,
BeaconStateError, ChainSpec, Epoch, EthSpec, Hash256, IndexedAttestation,
IndexedAttestationRef, ProposerSlashing, PublicKeyBytes, SignedAggregateAndProof,
SignedContributionAndProof, Slot, SyncCommitteeMessage, VoluntaryExit,
};

/// Used for Prometheus labels.
Expand Down Expand Up @@ -1411,7 +1411,7 @@ impl<E: EthSpec> ValidatorMonitor<E> {
/// Note: Blocks that get orphaned will skew the inclusion distance calculation.
pub fn register_attestation_in_block(
&self,
indexed_attestation: &IndexedAttestation<E>,
indexed_attestation: IndexedAttestationRef<'_, E>,
parent_slot: Slot,
spec: &ChainSpec,
) {
Expand Down
6 changes: 3 additions & 3 deletions consensus/fork_choice/src/fork_choice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use std::time::Duration;
use types::{
consts::merge::INTERVALS_PER_SLOT, AbstractExecPayload, AttestationShufflingId,
AttesterSlashingRef, BeaconBlockRef, BeaconState, BeaconStateError, ChainSpec, Checkpoint,
Epoch, EthSpec, ExecPayload, ExecutionBlockHash, Hash256, IndexedAttestation, RelativeEpoch,
SignedBeaconBlock, Slot,
Epoch, EthSpec, ExecPayload, ExecutionBlockHash, Hash256, IndexedAttestation,
IndexedAttestationRef, RelativeEpoch, SignedBeaconBlock, Slot,
};

#[derive(Debug)]
Expand Down Expand Up @@ -1087,7 +1087,7 @@ where
///
/// We assume that the attester slashing provided to this function has already been verified.
pub fn on_attester_slashing(&mut self, slashing: AttesterSlashingRef<'_, E>) {
let attesting_indices_set = |att: &IndexedAttestation<E>| {
let attesting_indices_set = |att: IndexedAttestationRef<'_, E>| {
att.attesting_indices_iter()
.copied()
.collect::<BTreeSet<_>>()
Expand Down
18 changes: 10 additions & 8 deletions consensus/state_processing/src/consensus_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use crate::EpochCacheError;
use std::collections::{hash_map::Entry, HashMap};
use tree_hash::TreeHash;
use types::{
AbstractExecPayload, Attestation, AttestationData, BeaconState, BeaconStateError, BitList,
ChainSpec, Epoch, EthSpec, Hash256, IndexedAttestation, SignedBeaconBlock, Slot,
AbstractExecPayload, AttestationData, AttestationRef, BeaconState, BeaconStateError, BitList,
ChainSpec, Epoch, EthSpec, Hash256, IndexedAttestation, IndexedAttestationRef,
SignedBeaconBlock, Slot,
};

#[derive(Debug, PartialEq, Clone)]
Expand Down Expand Up @@ -153,13 +154,13 @@ impl<E: EthSpec> ConsensusContext<E> {
}
}

pub fn get_indexed_attestation(
&mut self,
pub fn get_indexed_attestation<'a>(
&'a mut self,
state: &BeaconState<E>,
attestation: &Attestation<E>,
) -> Result<&IndexedAttestation<E>, BlockOperationError<AttestationInvalid>> {
attestation: AttestationRef<'a, E>,
) -> Result<IndexedAttestationRef<E>, BlockOperationError<AttestationInvalid>> {
let aggregation_bits = match attestation {
Attestation::Base(attn) => {
AttestationRef::Base(attn) => {
let mut extended_aggregation_bits: BitList<E::MaxValidatorsPerCommitteePerSlot> =
BitList::with_capacity(attn.aggregation_bits.len())
.map_err(BeaconStateError::from)?;
Expand All @@ -171,7 +172,7 @@ impl<E: EthSpec> ConsensusContext<E> {
}
extended_aggregation_bits
}
Attestation::Electra(attn) => attn.aggregation_bits.clone(),
AttestationRef::Electra(attn) => attn.aggregation_bits.clone(),
};

let key = (attestation.data().clone(), aggregation_bits);
Expand All @@ -186,6 +187,7 @@ impl<E: EthSpec> ConsensusContext<E> {
Ok(vacant.insert(indexed_attestation))
}
}
.map(|indexed_attestation| (*indexed_attestation).to_ref())
}

pub fn num_cached_indexed_attestations(&self) -> usize {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,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)?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ fn error(reason: Invalid) -> BlockOperationError<Invalid> {
}

/// Verify an `IndexedAttestation`.
pub fn is_valid_indexed_attestation<E: EthSpec>(
pub fn is_valid_indexed_attestation<'a, E: EthSpec>(
state: &BeaconState<E>,
indexed_attestation: &IndexedAttestation<E>,
indexed_attestation: IndexedAttestationRef<'a, E>,
verify_signatures: VerifySignatures,
spec: &ChainSpec,
) -> Result<()> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use tree_hash::TreeHash;
use types::{
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,
};
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -346,14 +346,14 @@ where
indexed_attestation_signature_set(
state,
get_pubkey.clone(),
&attester_slashing.attestation_1().signature,
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().signature(),
attester_slashing.attestation_2(),
spec,
)?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn verify_attestation_for_block_inclusion<'ctxt, E: EthSpec>(
ctxt: &'ctxt mut ConsensusContext<E>,
verify_signatures: VerifySignatures,
spec: &ChainSpec,
) -> Result<&'ctxt IndexedAttestation<E>> {
) -> Result<IndexedAttestationRef<'ctxt, E>> {
let data = attestation.data();

verify!(
Expand Down Expand Up @@ -65,7 +65,7 @@ pub fn verify_attestation_for_state<'ctxt, E: EthSpec>(
ctxt: &'ctxt mut ConsensusContext<E>,
verify_signatures: VerifySignatures,
spec: &ChainSpec,
) -> Result<&'ctxt IndexedAttestation<E>> {
) -> Result<IndexedAttestationRef<'ctxt, E>> {
let data = attestation.data();

verify!(
Expand Down
26 changes: 0 additions & 26 deletions consensus/state_processing/src/verify_operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,32 +267,6 @@ impl<E: EthSpec> VerifyOperation<E> for AttesterSlashing<E> {
}
}

impl<E: EthSpec> VerifyOperation<E> for AttesterSlashingElectra<E> {
type Error = AttesterSlashingValidationError;

fn validate(
self,
state: &BeaconState<E>,
spec: &ChainSpec,
) -> Result<SigVerifiedOp<Self, E>, Self::Error> {
verify_attester_slashing(
state,
AttesterSlashingRef::Electra(&self),
VerifySignatures::True,
spec,
)?;
Ok(SigVerifiedOp::new(self, state))
}

#[allow(clippy::arithmetic_side_effects)]
fn verification_epochs(&self) -> SmallVec<[Epoch; MAX_FORKS_VERIFIED_AGAINST]> {
smallvec![
self.attestation_1().data().target.epoch,
self.attestation_2().data().target.epoch
]
}
}

impl<E: EthSpec> VerifyOperation<E> for ProposerSlashing {
type Error = ProposerSlashingValidationError;

Expand Down
Loading

0 comments on commit ca7a9ae

Please sign in to comment.