Skip to content

Commit

Permalink
Write helpers for electra
Browse files Browse the repository at this point in the history
  • Loading branch information
ralexstokes committed Apr 18, 2024
1 parent 419bace commit a627a5b
Show file tree
Hide file tree
Showing 9 changed files with 4,949 additions and 47 deletions.
114 changes: 114 additions & 0 deletions ethereum-consensus/src/electra/block_processing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
use crate::{
electra::{Attestation, BeaconBlockBody, BeaconState},
state_transition::Context,
Error,
};

pub fn process_attestation<
const SLOTS_PER_HISTORICAL_ROOT: usize,
const HISTORICAL_ROOTS_LIMIT: usize,
const ETH1_DATA_VOTES_BOUND: usize,
const VALIDATOR_REGISTRY_LIMIT: usize,
const EPOCHS_PER_HISTORICAL_VECTOR: usize,
const EPOCHS_PER_SLASHINGS_VECTOR: usize,
const MAX_VALIDATORS_PER_COMMITTEE: usize,
const SYNC_COMMITTEE_SIZE: usize,
const BYTES_PER_LOGS_BLOOM: usize,
const MAX_EXTRA_DATA_BYTES: usize,
const PENDING_BALANCE_DEPOSITS_LIMIT: usize,
const PENDING_PARTIAL_WITHDRAWALS_LIMIT: usize,
const PENDING_CONSOLIDATIONS_LIMIT: usize,
const MAX_VALIDATORS_PER_SLOT: usize,
const MAX_COMMITTEES_PER_SLOT: usize,
>(
_state: &mut BeaconState<
SLOTS_PER_HISTORICAL_ROOT,
HISTORICAL_ROOTS_LIMIT,
ETH1_DATA_VOTES_BOUND,
VALIDATOR_REGISTRY_LIMIT,
EPOCHS_PER_HISTORICAL_VECTOR,
EPOCHS_PER_SLASHINGS_VECTOR,
MAX_VALIDATORS_PER_COMMITTEE,
SYNC_COMMITTEE_SIZE,
BYTES_PER_LOGS_BLOOM,
MAX_EXTRA_DATA_BYTES,
PENDING_BALANCE_DEPOSITS_LIMIT,
PENDING_PARTIAL_WITHDRAWALS_LIMIT,
PENDING_CONSOLIDATIONS_LIMIT,
>,
_attestation: &Attestation<MAX_VALIDATORS_PER_SLOT, MAX_COMMITTEES_PER_SLOT>,
_context: &Context,
) -> Result<(), Error> {
todo!()
}

pub fn process_execution_payload<
const SLOTS_PER_HISTORICAL_ROOT: usize,
const HISTORICAL_ROOTS_LIMIT: usize,
const ETH1_DATA_VOTES_BOUND: usize,
const VALIDATOR_REGISTRY_LIMIT: usize,
const EPOCHS_PER_HISTORICAL_VECTOR: usize,
const EPOCHS_PER_SLASHINGS_VECTOR: usize,
const MAX_VALIDATORS_PER_COMMITTEE: usize,
const SYNC_COMMITTEE_SIZE: usize,
const BYTES_PER_LOGS_BLOOM: usize,
const MAX_EXTRA_DATA_BYTES: usize,
const PENDING_BALANCE_DEPOSITS_LIMIT: usize,
const PENDING_PARTIAL_WITHDRAWALS_LIMIT: usize,
const PENDING_CONSOLIDATIONS_LIMIT: usize,
const MAX_PROPOSER_SLASHINGS: usize,
const MAX_ATTESTER_SLASHINGS: usize,
const MAX_ATTESTATIONS: usize,
const MAX_DEPOSITS: usize,
const MAX_VOLUNTARY_EXITS: usize,
const MAX_BYTES_PER_TRANSACTION: usize,
const MAX_TRANSACTIONS_PER_PAYLOAD: usize,
const MAX_WITHDRAWALS_PER_PAYLOAD: usize,
const MAX_BLS_TO_EXECUTION_CHANGES: usize,
const MAX_BLOB_COMMITMENTS_PER_BLOCK: usize,
const MAX_COMMITTEES_PER_SLOT: usize,
const MAX_VALIDATORS_PER_SLOT: usize,
const MAX_DEPOSIT_RECEIPTS_PER_PAYLOAD: usize,
const MAX_WITHDRAWAL_REQUESTS_PER_PAYLOAD: usize,
const MAX_CONSOLIDATIONS: usize,
>(
_state: &mut BeaconState<
SLOTS_PER_HISTORICAL_ROOT,
HISTORICAL_ROOTS_LIMIT,
ETH1_DATA_VOTES_BOUND,
VALIDATOR_REGISTRY_LIMIT,
EPOCHS_PER_HISTORICAL_VECTOR,
EPOCHS_PER_SLASHINGS_VECTOR,
MAX_VALIDATORS_PER_COMMITTEE,
SYNC_COMMITTEE_SIZE,
BYTES_PER_LOGS_BLOOM,
MAX_EXTRA_DATA_BYTES,
PENDING_BALANCE_DEPOSITS_LIMIT,
PENDING_PARTIAL_WITHDRAWALS_LIMIT,
PENDING_CONSOLIDATIONS_LIMIT,
>,
_body: &BeaconBlockBody<
MAX_PROPOSER_SLASHINGS,
MAX_VALIDATORS_PER_COMMITTEE,
MAX_ATTESTER_SLASHINGS,
MAX_ATTESTATIONS,
MAX_DEPOSITS,
MAX_VOLUNTARY_EXITS,
SYNC_COMMITTEE_SIZE,
BYTES_PER_LOGS_BLOOM,
MAX_EXTRA_DATA_BYTES,
MAX_BYTES_PER_TRANSACTION,
MAX_TRANSACTIONS_PER_PAYLOAD,
MAX_WITHDRAWALS_PER_PAYLOAD,
MAX_BLS_TO_EXECUTION_CHANGES,
MAX_BLOB_COMMITMENTS_PER_BLOCK,
MAX_COMMITTEES_PER_SLOT,
MAX_VALIDATORS_PER_SLOT,
MAX_DEPOSIT_RECEIPTS_PER_PAYLOAD,
MAX_WITHDRAWAL_REQUESTS_PER_PAYLOAD,
MAX_CONSOLIDATIONS,
>,
_context: &Context,
) -> Result<(), Error> {
todo!();
}
62 changes: 39 additions & 23 deletions ethereum-consensus/src/electra/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{
phase0::{helpers::compute_activation_exit_epoch, Fork},
primitives::FAR_FUTURE_EPOCH,
state_transition::Context,
Error,
};

pub fn upgrade_to_electra<
Expand Down Expand Up @@ -43,20 +44,23 @@ pub fn upgrade_to_electra<
MAX_EXTRA_DATA_BYTES,
>,
context: &Context,
) -> BeaconState<
SLOTS_PER_HISTORICAL_ROOT,
HISTORICAL_ROOTS_LIMIT,
ETH1_DATA_VOTES_BOUND,
VALIDATOR_REGISTRY_LIMIT,
EPOCHS_PER_HISTORICAL_VECTOR,
EPOCHS_PER_SLASHINGS_VECTOR,
MAX_VALIDATORS_PER_COMMITTEE,
SYNC_COMMITTEE_SIZE,
BYTES_PER_LOGS_BLOOM,
MAX_EXTRA_DATA_BYTES,
PENDING_BALANCE_DEPOSITS_LIMIT,
PENDING_PARTIAL_WITHDRAWALS_LIMIT,
PENDING_CONSOLIDATIONS_LIMIT,
) -> Result<
BeaconState<
SLOTS_PER_HISTORICAL_ROOT,
HISTORICAL_ROOTS_LIMIT,
ETH1_DATA_VOTES_BOUND,
VALIDATOR_REGISTRY_LIMIT,
EPOCHS_PER_HISTORICAL_VECTOR,
EPOCHS_PER_SLASHINGS_VECTOR,
MAX_VALIDATORS_PER_COMMITTEE,
SYNC_COMMITTEE_SIZE,
BYTES_PER_LOGS_BLOOM,
MAX_EXTRA_DATA_BYTES,
PENDING_BALANCE_DEPOSITS_LIMIT,
PENDING_PARTIAL_WITHDRAWALS_LIMIT,
PENDING_CONSOLIDATIONS_LIMIT,
>,
Error,
> {
let epoch = deneb::get_current_epoch(state, context);
let latest_execution_payload_header = &state.latest_execution_payload_header;
Expand Down Expand Up @@ -90,7 +94,7 @@ pub fn upgrade_to_electra<
.or(Some(epoch));
let earliest_exit_epoch = exit_epoch.unwrap() + 1;

let post = BeaconState {
let mut post = BeaconState {
genesis_time: state.genesis_time,
genesis_validators_root: state.genesis_validators_root,
slot: state.slot,
Expand Down Expand Up @@ -125,14 +129,16 @@ pub fn upgrade_to_electra<
historical_summaries: state.historical_summaries.clone(),
deposit_receipts_start_index: UNSET_DEPOSIT_RECEIPTS_START_INDEX,
deposit_balance_to_consume: 0,
exit_balance_to_consume: get_activation_exit_churn_limit(),
exit_balance_to_consume: 0,
earliest_exit_epoch,
consolidation_balance_to_consume: get_consolidation_churn_limit(),
consolidation_balance_to_consume: 0,
earliest_consolidation_epoch: compute_activation_exit_epoch(epoch, context),
pending_balance_deposits: Default::default(),
pending_partial_withdrawals: Default::default(),
pending_consolidations: Default::default(),
};
post.exit_balance_to_consume = get_activation_exit_churn_limit(&post, context)?;
post.consolidation_balance_to_consume = get_consolidation_churn_limit(&post, context)?;

let mut pre_activation_validators = post
.validators
Expand All @@ -149,14 +155,24 @@ pub fn upgrade_to_electra<
pre_activation_validators.sort();

for (_, index) in pre_activation_validators {
queue_entire_balance_and_reset_validator(index);
queue_entire_balance_and_reset_validator(&mut post, index);
}

for (index, validator) in post.validators.iter().enumerate() {
if has_compounding_withdrawal_credential(validator) {
queue_excess_active_balance(index);
}
let indices_to_queue = post
.validators
.iter()
.enumerate()
.filter_map(|(index, validator)| {
if has_compounding_withdrawal_credential(validator) {
Some(index)
} else {
None
}
})
.collect::<Vec<_>>();
for index in indices_to_queue {
queue_excess_active_balance(&mut post, index, context)?;
}

post
Ok(post)
}
57 changes: 57 additions & 0 deletions ethereum-consensus/src/electra/genesis.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use crate::{
electra::{BeaconState, Deposit, ExecutionPayloadHeader},
primitives::Hash32,
state_transition::{Context, Result},
};

pub fn initialize_beacon_state_from_eth1<
const SLOTS_PER_HISTORICAL_ROOT: usize,
const HISTORICAL_ROOTS_LIMIT: usize,
const ETH1_DATA_VOTES_BOUND: usize,
const VALIDATOR_REGISTRY_LIMIT: usize,
const EPOCHS_PER_HISTORICAL_VECTOR: usize,
const EPOCHS_PER_SLASHINGS_VECTOR: usize,
const MAX_VALIDATORS_PER_COMMITTEE: usize,
const SYNC_COMMITTEE_SIZE: usize,
const PENDING_BALANCE_DEPOSITS_LIMIT: usize,
const PENDING_PARTIAL_WITHDRAWALS_LIMIT: usize,
const PENDING_CONSOLIDATIONS_LIMIT: usize,
const MAX_PROPOSER_SLASHINGS: usize,
const MAX_ATTESTER_SLASHINGS: usize,
const MAX_ATTESTATIONS: usize,
const MAX_DEPOSITS: usize,
const MAX_VOLUNTARY_EXITS: usize,
const BYTES_PER_LOGS_BLOOM: usize,
const MAX_EXTRA_DATA_BYTES: usize,
const MAX_BYTES_PER_TRANSACTION: usize,
const MAX_TRANSACTIONS_PER_PAYLOAD: usize,
const MAX_WITHDRAWALS_PER_PAYLOAD: usize,
const MAX_BLS_TO_EXECUTION_CHANGES: usize,
const MAX_BLOB_COMMITMENTS_PER_BLOCK: usize,
>(
_eth1_block_hash: Hash32,
_eth1_timestamp: u64,
_deposits: &[Deposit],
_execution_payload_header: Option<
&ExecutionPayloadHeader<BYTES_PER_LOGS_BLOOM, MAX_EXTRA_DATA_BYTES>,
>,
_context: &Context,
) -> Result<
BeaconState<
SLOTS_PER_HISTORICAL_ROOT,
HISTORICAL_ROOTS_LIMIT,
ETH1_DATA_VOTES_BOUND,
VALIDATOR_REGISTRY_LIMIT,
EPOCHS_PER_HISTORICAL_VECTOR,
EPOCHS_PER_SLASHINGS_VECTOR,
MAX_VALIDATORS_PER_COMMITTEE,
SYNC_COMMITTEE_SIZE,
BYTES_PER_LOGS_BLOOM,
MAX_EXTRA_DATA_BYTES,
PENDING_BALANCE_DEPOSITS_LIMIT,
PENDING_PARTIAL_WITHDRAWALS_LIMIT,
PENDING_CONSOLIDATIONS_LIMIT,
>,
> {
todo!()
}
Loading

0 comments on commit a627a5b

Please sign in to comment.