Upgrade to EIP-7732

This commit is contained in:
Mark Mackey
2024-09-03 11:33:37 -05:00
parent fedc6d6b0a
commit de1535789c
11 changed files with 164 additions and 6 deletions

View File

@@ -5,6 +5,7 @@ use crate::common::DepositDataTree;
use crate::upgrade::electra::upgrade_state_to_electra;
use crate::upgrade::{
upgrade_to_altair, upgrade_to_bellatrix, upgrade_to_capella, upgrade_to_deneb,
upgrade_to_eip7732,
};
use safe_arith::{ArithError, SafeArith};
use std::sync::Arc;
@@ -140,6 +141,20 @@ pub fn initialize_beacon_state_from_eth1<E: EthSpec>(
}
}
// Upgrade to EIP-7732 if configured from genesis.
if spec
.eip7732_fork_epoch
.map_or(false, |fork_epoch| fork_epoch == E::genesis_epoch())
{
upgrade_to_eip7732(&mut state, spec)?;
// Remove intermediate Electra fork from `state.fork`.
state.fork_mut().previous_version = spec.eip7732_fork_version;
// Here's where we *would* clone the header but there is no header here so..
// TODO(EIP7732): check this
}
// Now that we have our validators, initialize the caches (including the committees)
state.build_caches(spec)?;

View File

@@ -1,6 +1,6 @@
use crate::upgrade::{
upgrade_to_altair, upgrade_to_bellatrix, upgrade_to_capella, upgrade_to_deneb,
upgrade_to_electra,
upgrade_to_eip7732, upgrade_to_electra,
};
use crate::{per_epoch_processing::EpochProcessingSummary, *};
use safe_arith::{ArithError, SafeArith};
@@ -70,6 +70,10 @@ pub fn per_slot_processing<E: EthSpec>(
if spec.electra_fork_epoch == Some(state.current_epoch()) {
upgrade_to_electra(state, spec)?;
}
// EIP-7732.
if spec.eip7732_fork_epoch == Some(state.current_epoch()) {
upgrade_to_eip7732(state, spec)?;
}
// Additionally build all caches so that all valid states that are advanced always have
// committee caches built, and we don't have to worry about initialising them at higher

View File

@@ -2,10 +2,12 @@ pub mod altair;
pub mod bellatrix;
pub mod capella;
pub mod deneb;
pub mod eip7732;
pub mod electra;
pub use altair::upgrade_to_altair;
pub use bellatrix::upgrade_to_bellatrix;
pub use capella::upgrade_to_capella;
pub use deneb::upgrade_to_deneb;
pub use eip7732::upgrade_to_eip7732;
pub use electra::upgrade_to_electra;

View File

@@ -0,0 +1,96 @@
use bls::Hash256;
use std::mem;
use types::{
BeaconState, BeaconStateEIP7732, BeaconStateError as Error, ChainSpec, EpochCache, EthSpec,
ExecutionBid, Fork,
};
/// Transform an `Electra` state into an `EIP-7732` state.
pub fn upgrade_to_eip7732<E: EthSpec>(
pre_state: &mut BeaconState<E>,
spec: &ChainSpec,
) -> Result<(), Error> {
let epoch = pre_state.current_epoch();
let pre = pre_state.as_electra_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::EIP7732(BeaconStateEIP7732 {
// 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.eip7732_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 Bid
latest_execution_bid: ExecutionBid::default(),
// Capella
next_withdrawal_index: pre.next_withdrawal_index,
next_withdrawal_validator_index: pre.next_withdrawal_validator_index,
historical_summaries: pre.historical_summaries.clone(),
// Deneb
// Electra
deposit_requests_start_index: pre.deposit_requests_start_index,
deposit_balance_to_consume: pre.deposit_balance_to_consume,
exit_balance_to_consume: pre.exit_balance_to_consume,
earliest_exit_epoch: pre.earliest_exit_epoch,
consolidation_balance_to_consume: pre.consolidation_balance_to_consume,
earliest_consolidation_epoch: pre.earliest_consolidation_epoch,
pending_balance_deposits: mem::take(&mut pre.pending_balance_deposits),
pending_partial_withdrawals: mem::take(&mut pre.pending_partial_withdrawals),
pending_consolidations: mem::take(&mut pre.pending_consolidations),
// EIP-7732
latest_block_hash: pre.latest_execution_payload_header.block_hash,
latest_full_slot: pre.slot,
latest_withdrawals_root: Hash256::default(),
// Caches
total_active_balance: pre.total_active_balance,
progressive_balances_cache: mem::take(&mut pre.progressive_balances_cache),
committee_caches: mem::take(&mut pre.committee_caches),
pubkey_cache: mem::take(&mut pre.pubkey_cache),
exit_cache: mem::take(&mut pre.exit_cache),
slashings_cache: mem::take(&mut pre.slashings_cache),
epoch_cache: EpochCache::default(),
});
*pre_state = post;
Ok(())
}