mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-09 03:31:45 +00:00
Clean capella (#4019)
## Issue Addressed Cleans up all the remnants of 4844 in capella. This makes sure when 4844 is reviewed there is nothing we are missing because it got included here ## Proposed Changes drop a bomb on every 4844 thing ## Additional Info Merge process I did (locally) is as follows: - squash merge to produce one commit - in new branch off unstable with the squashed commit create a `git revert HEAD` commit - merge that new branch onto 4844 with `--strategy ours` - compare local 4844 to remote 4844 and make sure the diff is empty - enjoy Co-authored-by: Paul Hauner <paul@paulhauner.com>
This commit is contained in:
@@ -50,12 +50,11 @@ pub fn slash_validator<T: EthSpec>(
|
||||
validator_effective_balance.safe_div(spec.whistleblower_reward_quotient)?;
|
||||
let proposer_reward = match state {
|
||||
BeaconState::Base(_) => whistleblower_reward.safe_div(spec.proposer_reward_quotient)?,
|
||||
BeaconState::Altair(_)
|
||||
| BeaconState::Merge(_)
|
||||
| BeaconState::Capella(_)
|
||||
| BeaconState::Eip4844(_) => whistleblower_reward
|
||||
.safe_mul(PROPOSER_WEIGHT)?
|
||||
.safe_div(WEIGHT_DENOMINATOR)?,
|
||||
BeaconState::Altair(_) | BeaconState::Merge(_) | BeaconState::Capella(_) => {
|
||||
whistleblower_reward
|
||||
.safe_mul(PROPOSER_WEIGHT)?
|
||||
.safe_div(WEIGHT_DENOMINATOR)?
|
||||
}
|
||||
};
|
||||
|
||||
// Ensure the whistleblower index is in the validator registry.
|
||||
|
||||
@@ -2,9 +2,7 @@ use super::per_block_processing::{
|
||||
errors::BlockProcessingError, process_operations::process_deposit,
|
||||
};
|
||||
use crate::common::DepositDataTree;
|
||||
use crate::upgrade::{
|
||||
upgrade_to_altair, upgrade_to_bellatrix, upgrade_to_capella, upgrade_to_eip4844,
|
||||
};
|
||||
use crate::upgrade::{upgrade_to_altair, upgrade_to_bellatrix, upgrade_to_capella};
|
||||
use safe_arith::{ArithError, SafeArith};
|
||||
use tree_hash::TreeHash;
|
||||
use types::DEPOSIT_TREE_DEPTH;
|
||||
@@ -93,23 +91,6 @@ pub fn initialize_beacon_state_from_eth1<T: EthSpec>(
|
||||
}
|
||||
}
|
||||
|
||||
// Upgrade to eip4844 if configured from genesis
|
||||
if spec
|
||||
.eip4844_fork_epoch
|
||||
.map_or(false, |fork_epoch| fork_epoch == T::genesis_epoch())
|
||||
{
|
||||
upgrade_to_eip4844(&mut state, spec)?;
|
||||
|
||||
// Remove intermediate Capella fork from `state.fork`.
|
||||
state.fork_mut().previous_version = spec.eip4844_fork_version;
|
||||
|
||||
// Override latest execution payload header.
|
||||
// See https://github.com/ethereum/consensus-specs/blob/dev/specs/eip4844/beacon-chain.md#testing
|
||||
if let Some(ExecutionPayloadHeader::Eip4844(header)) = execution_payload_header {
|
||||
*state.latest_execution_payload_header_eip4844_mut()? = header;
|
||||
}
|
||||
}
|
||||
|
||||
// Now that we have our validators, initialize the caches (including the committees)
|
||||
state.build_all_caches(spec)?;
|
||||
|
||||
|
||||
@@ -13,7 +13,6 @@ pub use self::verify_attester_slashing::{
|
||||
pub use self::verify_proposer_slashing::verify_proposer_slashing;
|
||||
pub use altair::sync_committee::process_sync_aggregate;
|
||||
pub use block_signature_verifier::{BlockSignatureVerifier, ParallelSignatureSets};
|
||||
pub use eip4844::eip4844::process_blob_kzg_commitments;
|
||||
pub use is_valid_indexed_attestation::is_valid_indexed_attestation;
|
||||
pub use process_operations::process_operations;
|
||||
pub use verify_attestation::{
|
||||
@@ -27,7 +26,6 @@ pub use verify_exit::verify_exit;
|
||||
|
||||
pub mod altair;
|
||||
pub mod block_signature_verifier;
|
||||
pub mod eip4844;
|
||||
pub mod errors;
|
||||
mod is_valid_indexed_attestation;
|
||||
pub mod process_operations;
|
||||
@@ -180,12 +178,6 @@ pub fn per_block_processing<T: EthSpec, Payload: AbstractExecPayload<T>>(
|
||||
)?;
|
||||
}
|
||||
|
||||
// Eip4844 specifications are not yet released so additional care is taken
|
||||
// to ensure the code does not run in production.
|
||||
if matches!(block, BeaconBlockRef::Eip4844(_)) {
|
||||
process_blob_kzg_commitments(block.body())?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -409,12 +401,6 @@ pub fn process_execution_payload<T: EthSpec, Payload: AbstractExecPayload<T>>(
|
||||
_ => return Err(BlockProcessingError::IncorrectStateType),
|
||||
}
|
||||
}
|
||||
ExecutionPayloadHeaderRefMut::Eip4844(header_mut) => {
|
||||
match payload.to_execution_payload_header() {
|
||||
ExecutionPayloadHeader::Eip4844(header) => *header_mut = header,
|
||||
_ => return Err(BlockProcessingError::IncorrectStateType),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -527,7 +513,7 @@ pub fn process_withdrawals<T: EthSpec, Payload: AbstractExecPayload<T>>(
|
||||
) -> Result<(), BlockProcessingError> {
|
||||
match state {
|
||||
BeaconState::Merge(_) => Ok(()),
|
||||
BeaconState::Capella(_) | BeaconState::Eip4844(_) => {
|
||||
BeaconState::Capella(_) => {
|
||||
let expected_withdrawals = get_expected_withdrawals(state, spec)?;
|
||||
let expected_root = expected_withdrawals.tree_hash_root();
|
||||
let withdrawals_root = payload.withdrawals_root()?;
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
#[allow(clippy::module_inception)]
|
||||
pub mod eip4844;
|
||||
@@ -1,122 +0,0 @@
|
||||
use crate::BlockProcessingError;
|
||||
use eth2_hashing::hash_fixed;
|
||||
use itertools::{EitherOrBoth, Itertools};
|
||||
use safe_arith::SafeArith;
|
||||
use ssz::Decode;
|
||||
use ssz_types::VariableList;
|
||||
use types::consts::eip4844::{BLOB_TX_TYPE, VERSIONED_HASH_VERSION_KZG};
|
||||
use types::{
|
||||
AbstractExecPayload, BeaconBlockBodyRef, EthSpec, ExecPayload, KzgCommitment, Transaction,
|
||||
Transactions, VersionedHash,
|
||||
};
|
||||
|
||||
pub fn process_blob_kzg_commitments<T: EthSpec, Payload: AbstractExecPayload<T>>(
|
||||
block_body: BeaconBlockBodyRef<T, Payload>,
|
||||
) -> Result<(), BlockProcessingError> {
|
||||
if let (Ok(payload), Ok(kzg_commitments)) = (
|
||||
block_body.execution_payload(),
|
||||
block_body.blob_kzg_commitments(),
|
||||
) {
|
||||
if let Some(transactions) = payload.transactions() {
|
||||
if !verify_kzg_commitments_against_transactions::<T>(transactions, kzg_commitments)? {
|
||||
return Err(BlockProcessingError::BlobVersionHashMismatch);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn verify_kzg_commitments_against_transactions<T: EthSpec>(
|
||||
transactions: &Transactions<T>,
|
||||
kzg_commitments: &VariableList<KzgCommitment, T::MaxBlobsPerBlock>,
|
||||
) -> Result<bool, BlockProcessingError> {
|
||||
let nested_iter = transactions
|
||||
.into_iter()
|
||||
.filter(|tx| {
|
||||
tx.first()
|
||||
.map(|tx_type| *tx_type == BLOB_TX_TYPE)
|
||||
.unwrap_or(false)
|
||||
})
|
||||
.map(|tx| tx_peek_blob_versioned_hashes::<T>(tx));
|
||||
|
||||
itertools::process_results(nested_iter, |iter| {
|
||||
let zipped_iter = iter
|
||||
.flatten()
|
||||
// Need to use `itertools::zip_longest` here because just zipping hides if one iter is shorter
|
||||
// and `itertools::zip_eq` panics.
|
||||
.zip_longest(kzg_commitments.into_iter())
|
||||
.enumerate()
|
||||
.map(|(index, next)| match next {
|
||||
EitherOrBoth::Both(hash, commitment) => Ok((hash?, commitment)),
|
||||
// The number of versioned hashes from the blob transactions exceeds the number of
|
||||
// commitments in the block.
|
||||
EitherOrBoth::Left(_) => Err(BlockProcessingError::BlobNumCommitmentsMismatch {
|
||||
commitments_processed_in_block: index,
|
||||
commitments_processed_in_transactions: index.safe_add(1)?,
|
||||
}),
|
||||
// The number of commitments in the block exceeds the number of versioned hashes
|
||||
// in the blob transactions.
|
||||
EitherOrBoth::Right(_) => Err(BlockProcessingError::BlobNumCommitmentsMismatch {
|
||||
commitments_processed_in_block: index.safe_add(1)?,
|
||||
commitments_processed_in_transactions: index,
|
||||
}),
|
||||
});
|
||||
|
||||
itertools::process_results(zipped_iter, |mut iter| {
|
||||
iter.all(|(tx_versioned_hash, commitment)| {
|
||||
tx_versioned_hash == kzg_commitment_to_versioned_hash(commitment)
|
||||
})
|
||||
})
|
||||
})?
|
||||
}
|
||||
|
||||
/// Only transactions of type `BLOB_TX_TYPE` should be passed into this function.
|
||||
fn tx_peek_blob_versioned_hashes<T: EthSpec>(
|
||||
opaque_tx: &Transaction<T::MaxBytesPerTransaction>,
|
||||
) -> Result<
|
||||
impl IntoIterator<Item = Result<VersionedHash, BlockProcessingError>> + '_,
|
||||
BlockProcessingError,
|
||||
> {
|
||||
let tx_len = opaque_tx.len();
|
||||
let message_offset = 1.safe_add(u32::from_ssz_bytes(opaque_tx.get(1..5).ok_or(
|
||||
BlockProcessingError::BlobVersionHashIndexOutOfBounds {
|
||||
length: tx_len,
|
||||
index: 5,
|
||||
},
|
||||
)?)?)?;
|
||||
|
||||
let message_offset_usize = message_offset as usize;
|
||||
|
||||
// field offset: 32 + 8 + 32 + 32 + 8 + 4 + 32 + 4 + 4 + 32 = 188
|
||||
let blob_versioned_hashes_offset = message_offset.safe_add(u32::from_ssz_bytes(
|
||||
opaque_tx
|
||||
.get(message_offset_usize.safe_add(188)?..message_offset_usize.safe_add(192)?)
|
||||
.ok_or(BlockProcessingError::BlobVersionHashIndexOutOfBounds {
|
||||
length: tx_len,
|
||||
index: message_offset_usize.safe_add(192)?,
|
||||
})?,
|
||||
)?)?;
|
||||
|
||||
let num_hashes = tx_len
|
||||
.safe_sub(blob_versioned_hashes_offset as usize)?
|
||||
.safe_div(32)?;
|
||||
|
||||
Ok((0..num_hashes).into_iter().map(move |i| {
|
||||
let next_version_hash_index =
|
||||
(blob_versioned_hashes_offset as usize).safe_add(i.safe_mul(32)?)?;
|
||||
let bytes = opaque_tx
|
||||
.get(next_version_hash_index..next_version_hash_index.safe_add(32)?)
|
||||
.ok_or(BlockProcessingError::BlobVersionHashIndexOutOfBounds {
|
||||
length: tx_len,
|
||||
index: (next_version_hash_index).safe_add(32)?,
|
||||
})?;
|
||||
Ok(VersionedHash::from_slice(bytes))
|
||||
}))
|
||||
}
|
||||
|
||||
fn kzg_commitment_to_versioned_hash(kzg_commitment: &KzgCommitment) -> VersionedHash {
|
||||
let mut hashed_commitment = hash_fixed(&kzg_commitment.0);
|
||||
hashed_commitment[0] = VERSIONED_HASH_VERSION_KZG;
|
||||
VersionedHash::from(hashed_commitment)
|
||||
}
|
||||
@@ -82,18 +82,6 @@ pub enum BlockProcessingError {
|
||||
expected: Hash256,
|
||||
found: Hash256,
|
||||
},
|
||||
BlobVersionHashMismatch,
|
||||
/// The number of commitments in blob transactions in the payload does not match the number
|
||||
/// of commitments in the block.
|
||||
BlobNumCommitmentsMismatch {
|
||||
commitments_processed_in_block: usize,
|
||||
/// This number depic
|
||||
commitments_processed_in_transactions: usize,
|
||||
},
|
||||
BlobVersionHashIndexOutOfBounds {
|
||||
index: usize,
|
||||
length: usize,
|
||||
},
|
||||
WithdrawalCredentialsInvalid,
|
||||
}
|
||||
|
||||
|
||||
@@ -256,8 +256,7 @@ pub fn process_attestations<T: EthSpec, Payload: AbstractExecPayload<T>>(
|
||||
}
|
||||
BeaconBlockBodyRef::Altair(_)
|
||||
| BeaconBlockBodyRef::Merge(_)
|
||||
| BeaconBlockBodyRef::Capella(_)
|
||||
| BeaconBlockBodyRef::Eip4844(_) => {
|
||||
| BeaconBlockBodyRef::Capella(_) => {
|
||||
altair::process_attestations(
|
||||
state,
|
||||
block_body.attestations(),
|
||||
|
||||
@@ -40,7 +40,7 @@ pub fn process_epoch<T: EthSpec>(
|
||||
match state {
|
||||
BeaconState::Base(_) => base::process_epoch(state, spec),
|
||||
BeaconState::Altair(_) | BeaconState::Merge(_) => altair::process_epoch(state, spec),
|
||||
BeaconState::Capella(_) | BeaconState::Eip4844(_) => capella::process_epoch(state, spec),
|
||||
BeaconState::Capella(_) => capella::process_epoch(state, spec),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
use crate::upgrade::{
|
||||
upgrade_to_altair, upgrade_to_bellatrix, upgrade_to_capella, upgrade_to_eip4844,
|
||||
};
|
||||
use crate::upgrade::{upgrade_to_altair, upgrade_to_bellatrix, upgrade_to_capella};
|
||||
use crate::{per_epoch_processing::EpochProcessingSummary, *};
|
||||
use safe_arith::{ArithError, SafeArith};
|
||||
use types::*;
|
||||
@@ -61,10 +59,6 @@ pub fn per_slot_processing<T: EthSpec>(
|
||||
if spec.capella_fork_epoch == Some(state.current_epoch()) {
|
||||
upgrade_to_capella(state, spec)?;
|
||||
}
|
||||
// Eip4844
|
||||
if spec.eip4844_fork_epoch == Some(state.current_epoch()) {
|
||||
upgrade_to_eip4844(state, spec)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(summary)
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
pub mod altair;
|
||||
pub mod capella;
|
||||
pub mod eip4844;
|
||||
pub mod merge;
|
||||
|
||||
pub use altair::upgrade_to_altair;
|
||||
pub use capella::upgrade_to_capella;
|
||||
pub use eip4844::upgrade_to_eip4844;
|
||||
pub use merge::upgrade_to_bellatrix;
|
||||
|
||||
@@ -1,75 +0,0 @@
|
||||
use std::mem;
|
||||
use types::{BeaconState, BeaconStateEip4844, BeaconStateError as Error, ChainSpec, EthSpec, Fork};
|
||||
|
||||
/// Transform a `Capella` state into an `Eip4844` state.
|
||||
pub fn upgrade_to_eip4844<E: EthSpec>(
|
||||
pre_state: &mut BeaconState<E>,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
let epoch = pre_state.current_epoch();
|
||||
let pre = pre_state.as_capella_mut()?;
|
||||
|
||||
let previous_fork_version = pre.fork.current_version;
|
||||
|
||||
// Where possible, use something like `mem::take` to move fields from behind the &mut
|
||||
// reference. For other fields that don't have a good default value, use `clone`.
|
||||
//
|
||||
// Fixed size vectors get cloned because replacing them would require the same size
|
||||
// allocation as cloning.
|
||||
let post = BeaconState::Eip4844(BeaconStateEip4844 {
|
||||
// Versioning
|
||||
genesis_time: pre.genesis_time,
|
||||
genesis_validators_root: pre.genesis_validators_root,
|
||||
slot: pre.slot,
|
||||
fork: Fork {
|
||||
previous_version: previous_fork_version,
|
||||
current_version: spec.eip4844_fork_version,
|
||||
epoch,
|
||||
},
|
||||
// History
|
||||
latest_block_header: pre.latest_block_header.clone(),
|
||||
block_roots: pre.block_roots.clone(),
|
||||
state_roots: pre.state_roots.clone(),
|
||||
historical_roots: mem::take(&mut pre.historical_roots),
|
||||
// Eth1
|
||||
eth1_data: pre.eth1_data.clone(),
|
||||
eth1_data_votes: mem::take(&mut pre.eth1_data_votes),
|
||||
eth1_deposit_index: pre.eth1_deposit_index,
|
||||
// Registry
|
||||
validators: mem::take(&mut pre.validators),
|
||||
balances: mem::take(&mut pre.balances),
|
||||
// Randomness
|
||||
randao_mixes: pre.randao_mixes.clone(),
|
||||
// Slashings
|
||||
slashings: pre.slashings.clone(),
|
||||
// `Participation
|
||||
previous_epoch_participation: mem::take(&mut pre.previous_epoch_participation),
|
||||
current_epoch_participation: mem::take(&mut pre.current_epoch_participation),
|
||||
// Finality
|
||||
justification_bits: pre.justification_bits.clone(),
|
||||
previous_justified_checkpoint: pre.previous_justified_checkpoint,
|
||||
current_justified_checkpoint: pre.current_justified_checkpoint,
|
||||
finalized_checkpoint: pre.finalized_checkpoint,
|
||||
// Inactivity
|
||||
inactivity_scores: mem::take(&mut pre.inactivity_scores),
|
||||
// Sync committees
|
||||
current_sync_committee: pre.current_sync_committee.clone(),
|
||||
next_sync_committee: pre.next_sync_committee.clone(),
|
||||
// Execution
|
||||
latest_execution_payload_header: pre.latest_execution_payload_header.upgrade_to_eip4844(),
|
||||
// Capella
|
||||
next_withdrawal_index: pre.next_withdrawal_index,
|
||||
next_withdrawal_validator_index: pre.next_withdrawal_validator_index,
|
||||
historical_summaries: pre.historical_summaries.clone(),
|
||||
// Caches
|
||||
total_active_balance: pre.total_active_balance,
|
||||
committee_caches: mem::take(&mut pre.committee_caches),
|
||||
pubkey_cache: mem::take(&mut pre.pubkey_cache),
|
||||
exit_cache: mem::take(&mut pre.exit_cache),
|
||||
tree_hash_cache: mem::take(&mut pre.tree_hash_cache),
|
||||
});
|
||||
|
||||
*pre_state = post;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user