Merge branch 'beacon-api-electra' of https://github.com/sigp/lighthouse into ef-tests-electra

This commit is contained in:
realbigsean
2024-07-15 14:53:24 -07:00
169 changed files with 4419 additions and 4468 deletions

View File

@@ -4,7 +4,6 @@ use super::signature_sets::{Error as SignatureSetError, *};
use crate::per_block_processing::errors::{AttestationInvalid, BlockOperationError};
use crate::{ConsensusContext, ContextError};
use bls::{verify_signature_sets, PublicKey, PublicKeyBytes, SignatureSet};
use rayon::prelude::*;
use std::borrow::Cow;
use types::{
AbstractExecPayload, BeaconState, BeaconStateError, ChainSpec, EthSpec, Hash256,
@@ -411,15 +410,10 @@ impl<'a> ParallelSignatureSets<'a> {
/// It is not possible to know exactly _which_ signature is invalid here, just that
/// _at least one_ was invalid.
///
/// Uses `rayon` to do a map-reduce of Vitalik's method across multiple cores.
/// Blst library spreads the signature verification work across multiple available cores, so
/// this function is already parallelized.
#[must_use]
pub fn verify(self) -> bool {
let num_sets = self.sets.len();
let num_chunks = std::cmp::max(1, num_sets / rayon::current_num_threads());
self.sets
.into_par_iter()
.chunks(num_chunks)
.map(|chunk| verify_signature_sets(chunk.iter()))
.reduce(|| true, |current, this| current && this)
verify_signature_sets(self.sets.iter())
}
}

View File

@@ -39,14 +39,14 @@ pub fn process_operations<E: EthSpec, Payload: AbstractExecPayload<E>>(
process_bls_to_execution_changes(state, bls_to_execution_changes, verify_signatures, spec)?;
}
if state.fork_name_unchecked() >= ForkName::Electra {
if state.fork_name_unchecked().electra_enabled() {
let requests = block_body.execution_payload()?.withdrawal_requests()?;
if let Some(requests) = requests {
process_execution_layer_withdrawal_requests(state, &requests, spec)?;
}
let receipts = block_body.execution_payload()?.deposit_receipts()?;
let receipts = block_body.execution_payload()?.deposit_requests()?;
if let Some(receipts) = receipts {
process_deposit_receipts(state, &receipts, spec)?;
process_deposit_requests(state, &receipts, spec)?;
}
process_consolidations(state, block_body.consolidations()?, verify_signatures, spec)?;
}
@@ -372,9 +372,9 @@ pub fn process_deposits<E: EthSpec>(
// [Modified in Electra:EIP6110]
// Disable former deposit mechanism once all prior deposits are processed
//
// If `deposit_receipts_start_index` does not exist as a field on `state`, electra is disabled
// If `deposit_requests_start_index` does not exist as a field on `state`, electra is disabled
// which means we always want to use the old check, so this field defaults to `u64::MAX`.
let eth1_deposit_index_limit = state.deposit_receipts_start_index().unwrap_or(u64::MAX);
let eth1_deposit_index_limit = state.deposit_requests_start_index().unwrap_or(u64::MAX);
if state.eth1_deposit_index() < eth1_deposit_index_limit {
let expected_deposit_len = std::cmp::min(
@@ -625,15 +625,15 @@ pub fn process_execution_layer_withdrawal_requests<E: EthSpec>(
Ok(())
}
pub fn process_deposit_receipts<E: EthSpec>(
pub fn process_deposit_requests<E: EthSpec>(
state: &mut BeaconState<E>,
receipts: &[DepositReceipt],
receipts: &[DepositRequest],
spec: &ChainSpec,
) -> Result<(), BlockProcessingError> {
for receipt in receipts {
// Set deposit receipt start index
if state.deposit_receipts_start_index()? == spec.unset_deposit_receipts_start_index {
*state.deposit_receipts_start_index_mut()? = receipt.index
if state.deposit_requests_start_index()? == spec.unset_deposit_requests_start_index {
*state.deposit_requests_start_index_mut()? = receipt.index
}
let deposit_data = DepositData {
pubkey: receipt.pubkey,

View File

@@ -161,7 +161,7 @@ pub fn process_epoch_single_pass<E: EthSpec>(
let mut next_epoch_cache = PreEpochCache::new_for_next_epoch(state)?;
let pending_balance_deposits_ctxt =
if fork_name >= ForkName::Electra && conf.pending_balance_deposits {
if fork_name.electra_enabled() && conf.pending_balance_deposits {
Some(PendingBalanceDepositsContext::new(state, spec)?)
} else {
None
@@ -197,7 +197,7 @@ pub fn process_epoch_single_pass<E: EthSpec>(
// Compute shared values required for different parts of epoch processing.
let rewards_ctxt = &RewardsAndPenaltiesContext::new(progressive_balances, state_ctxt, spec)?;
let mut activation_queues = if fork_name < ForkName::Electra {
let mut activation_queues = if !fork_name.electra_enabled() {
let activation_queue = epoch_cache
.activation_queue()?
.get_validators_eligible_for_activation(
@@ -325,7 +325,7 @@ pub fn process_epoch_single_pass<E: EthSpec>(
}
}
if conf.registry_updates && fork_name >= ForkName::Electra {
if conf.registry_updates && fork_name.electra_enabled() {
if let Ok(earliest_exit_epoch_state) = state.earliest_exit_epoch_mut() {
*earliest_exit_epoch_state =
earliest_exit_epoch.ok_or(Error::MissingEarliestExitEpoch)?;
@@ -354,7 +354,7 @@ pub fn process_epoch_single_pass<E: EthSpec>(
// Process consolidations outside the single-pass loop, as they depend on balances for multiple
// validators and cannot be computed accurately inside the loop.
if fork_name >= ForkName::Electra && conf.pending_consolidations {
if fork_name.electra_enabled() && conf.pending_consolidations {
process_pending_consolidations(
state,
&mut next_epoch_cache,
@@ -555,7 +555,7 @@ fn process_single_registry_update(
exit_balance_to_consume: Option<&mut u64>,
spec: &ChainSpec,
) -> Result<(), Error> {
if state_ctxt.fork_name < ForkName::Electra {
if !state_ctxt.fork_name.electra_enabled() {
let (activation_queue, next_epoch_activation_queue) =
activation_queues.ok_or(Error::SinglePassMissingActivationQueue)?;
process_single_registry_update_pre_electra(
@@ -665,7 +665,7 @@ fn initiate_validator_exit(
return Ok(());
}
let exit_queue_epoch = if state_ctxt.fork_name >= ForkName::Electra {
let exit_queue_epoch = if state_ctxt.fork_name.electra_enabled() {
compute_exit_epoch_and_update_churn(
validator,
state_ctxt,

View File

@@ -131,7 +131,7 @@ pub fn upgrade_state_to_electra<E: EthSpec>(
next_withdrawal_validator_index: pre.next_withdrawal_validator_index,
historical_summaries: pre.historical_summaries.clone(),
// Electra
deposit_receipts_start_index: spec.unset_deposit_receipts_start_index,
deposit_requests_start_index: spec.unset_deposit_requests_start_index,
deposit_balance_to_consume: 0,
exit_balance_to_consume: 0,
earliest_exit_epoch,

View File

@@ -7,17 +7,17 @@ use crate::per_block_processing::{
verify_proposer_slashing,
};
use crate::VerifySignatures;
use arbitrary::Arbitrary;
use derivative::Derivative;
use smallvec::{smallvec, SmallVec};
use ssz::{Decode, Encode};
use ssz_derive::{Decode, Encode};
use std::marker::PhantomData;
use test_random_derive::TestRandom;
use types::{
AttesterSlashing, AttesterSlashingBase, AttesterSlashingOnDisk, AttesterSlashingRefOnDisk,
};
use types::{
BeaconState, ChainSpec, Epoch, EthSpec, Fork, ForkVersion, ProposerSlashing,
SignedBlsToExecutionChange, SignedVoluntaryExit,
test_utils::TestRandom, AttesterSlashing, AttesterSlashingBase, AttesterSlashingOnDisk,
AttesterSlashingRefOnDisk, BeaconState, ChainSpec, Epoch, EthSpec, Fork, ForkVersion,
ProposerSlashing, SignedBlsToExecutionChange, SignedVoluntaryExit,
};
const MAX_FORKS_VERIFIED_AGAINST: usize = 2;
@@ -39,12 +39,13 @@ pub trait TransformPersist {
///
/// The inner `op` field is private, meaning instances of this type can only be constructed
/// by calling `validate`.
#[derive(Derivative, Debug, Clone)]
#[derive(Derivative, Debug, Clone, Arbitrary)]
#[derivative(
PartialEq,
Eq,
Hash(bound = "T: TransformPersist + std::hash::Hash, E: EthSpec")
)]
#[arbitrary(bound = "T: TransformPersist + Arbitrary<'arbitrary>, E: EthSpec")]
pub struct SigVerifiedOp<T: TransformPersist, E: EthSpec> {
op: T,
verified_against: VerifiedAgainst,
@@ -53,92 +54,68 @@ pub struct SigVerifiedOp<T: TransformPersist, E: EthSpec> {
impl<T: TransformPersist, E: EthSpec> Encode for SigVerifiedOp<T, E> {
fn is_ssz_fixed_len() -> bool {
<T::Persistable as Encode>::is_ssz_fixed_len()
&& <VerifiedAgainst as Encode>::is_ssz_fixed_len()
<SigVerifiedOpEncode<T::Persistable> as Encode>::is_ssz_fixed_len()
}
#[allow(clippy::expect_used)]
fn ssz_fixed_len() -> usize {
if <Self as Encode>::is_ssz_fixed_len() {
<T::Persistable as Encode>::ssz_fixed_len()
.checked_add(<VerifiedAgainst as Encode>::ssz_fixed_len())
.expect("encode ssz_fixed_len length overflow")
} else {
ssz::BYTES_PER_LENGTH_OFFSET
}
}
#[allow(clippy::expect_used)]
fn ssz_bytes_len(&self) -> usize {
if <Self as Encode>::is_ssz_fixed_len() {
<Self as Encode>::ssz_fixed_len()
} else {
let persistable = self.op.as_persistable_ref();
persistable
.ssz_bytes_len()
.checked_add(self.verified_against.ssz_bytes_len())
.expect("ssz_bytes_len length overflow")
}
<SigVerifiedOpEncode<T::Persistable> as Encode>::ssz_fixed_len()
}
fn ssz_append(&self, buf: &mut Vec<u8>) {
let mut encoder = ssz::SszEncoder::container(buf, <Self as Encode>::ssz_fixed_len());
let persistable = self.op.as_persistable_ref();
encoder.append(&persistable);
encoder.append(&self.verified_against);
encoder.finalize();
let persistable_ref = self.op.as_persistable_ref();
SigVerifiedOpEncode {
op: persistable_ref,
verified_against: &self.verified_against,
}
.ssz_append(buf)
}
fn ssz_bytes_len(&self) -> usize {
let persistable_ref = self.op.as_persistable_ref();
SigVerifiedOpEncode {
op: persistable_ref,
verified_against: &self.verified_against,
}
.ssz_bytes_len()
}
}
impl<T: TransformPersist, E: EthSpec> Decode for SigVerifiedOp<T, E> {
fn is_ssz_fixed_len() -> bool {
<T::Persistable as Decode>::is_ssz_fixed_len()
&& <VerifiedAgainst as Decode>::is_ssz_fixed_len()
<SigVerifiedOpDecode<T::Persistable> as Decode>::is_ssz_fixed_len()
}
#[allow(clippy::expect_used)]
fn ssz_fixed_len() -> usize {
if <Self as Decode>::is_ssz_fixed_len() {
<T::Persistable as Decode>::ssz_fixed_len()
.checked_add(<VerifiedAgainst as Decode>::ssz_fixed_len())
.expect("decode ssz_fixed_len length overflow")
} else {
ssz::BYTES_PER_LENGTH_OFFSET
}
<SigVerifiedOpDecode<T::Persistable> as Decode>::ssz_fixed_len()
}
fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, ssz::DecodeError> {
let mut builder = ssz::SszDecoderBuilder::new(bytes);
// Register types based on whether they are fixed or variable length
if <T::Persistable as Decode>::is_ssz_fixed_len() {
builder.register_type::<T::Persistable>()?;
} else {
builder.register_anonymous_variable_length_item()?;
}
if <VerifiedAgainst as Decode>::is_ssz_fixed_len() {
builder.register_type::<VerifiedAgainst>()?;
} else {
builder.register_anonymous_variable_length_item()?;
}
let mut decoder = builder.build()?;
// Decode each component
let persistable: T::Persistable = decoder.decode_next()?;
let verified_against: VerifiedAgainst = decoder.decode_next()?;
// Use TransformPersist to convert persistable back into the original type
let op = T::from_persistable(persistable);
let on_disk = SigVerifiedOpDecode::<T::Persistable>::from_ssz_bytes(bytes)?;
Ok(SigVerifiedOp {
op,
verified_against,
op: T::from_persistable(on_disk.op),
verified_against: on_disk.verified_against,
_phantom: PhantomData,
})
}
}
/// On-disk variant of `SigVerifiedOp` that implements `Encode`.
///
/// We use separate types for Encode and Decode so we can efficiently handle references: the Encode
/// type contains references, while the Decode type does not.
#[derive(Debug, Encode)]
struct SigVerifiedOpEncode<'a, P: Encode> {
op: P,
verified_against: &'a VerifiedAgainst,
}
/// On-disk variant of `SigVerifiedOp` that implements `Encode`.
#[derive(Debug, Decode)]
struct SigVerifiedOpDecode<P: Decode> {
op: P,
verified_against: VerifiedAgainst,
}
/// Information about the fork versions that this message was verified against.
///
/// In general it is not safe to assume that a `SigVerifiedOp` constructed at some point in the past
@@ -156,7 +133,7 @@ impl<T: TransformPersist, E: EthSpec> Decode for SigVerifiedOp<T, E> {
///
/// We need to store multiple `ForkVersion`s because attester slashings contain two indexed
/// attestations which may be signed using different versions.
#[derive(Debug, PartialEq, Eq, Clone, Hash, Encode, Decode)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Encode, Decode, TestRandom, Arbitrary)]
pub struct VerifiedAgainst {
fork_versions: SmallVec<[ForkVersion; MAX_FORKS_VERIFIED_AGAINST]>,
}
@@ -435,3 +412,56 @@ impl TransformPersist for SignedBlsToExecutionChange {
persistable
}
}
#[cfg(all(test, not(debug_assertions)))]
mod test {
use super::*;
use types::{
test_utils::{SeedableRng, TestRandom, XorShiftRng},
MainnetEthSpec,
};
type E = MainnetEthSpec;
fn roundtrip_test<T: TestRandom + TransformPersist + PartialEq + std::fmt::Debug>() {
let runs = 10;
let mut rng = XorShiftRng::seed_from_u64(0xff0af5a356af1123);
for _ in 0..runs {
let op = T::random_for_test(&mut rng);
let verified_against = VerifiedAgainst::random_for_test(&mut rng);
let verified_op = SigVerifiedOp {
op,
verified_against,
_phantom: PhantomData::<E>,
};
let serialized = verified_op.as_ssz_bytes();
let deserialized = SigVerifiedOp::from_ssz_bytes(&serialized).unwrap();
let reserialized = deserialized.as_ssz_bytes();
assert_eq!(verified_op, deserialized);
assert_eq!(serialized, reserialized);
}
}
#[test]
fn sig_verified_op_exit_roundtrip() {
roundtrip_test::<SignedVoluntaryExit>();
}
#[test]
fn proposer_slashing_roundtrip() {
roundtrip_test::<ProposerSlashing>();
}
#[test]
fn attester_slashing_roundtrip() {
roundtrip_test::<AttesterSlashing<E>>();
}
#[test]
fn bls_to_execution_roundtrip() {
roundtrip_test::<SignedBlsToExecutionChange>();
}
}