Skip to content

Commit

Permalink
Merge pull request #278 from ralexstokes/spec-gen
Browse files Browse the repository at this point in the history
generate spec after latest updates for capella
  • Loading branch information
ralexstokes authored Oct 5, 2023
2 parents 5d393ed + fce3114 commit 45959b3
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 10 deletions.
4 changes: 2 additions & 2 deletions ethereum-consensus/src/capella/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ pub use crate::{
BlindedBeaconBlock, BlindedBeaconBlockBody, SignedBlindedBeaconBlock,
},
block_processing::{
process_block, process_bls_to_execution_change, process_execution_payload,
process_operations, process_withdrawals,
get_expected_withdrawals, process_block, process_bls_to_execution_change,
process_execution_payload, process_operations, process_withdrawals,
},
bls_to_execution_change::{BlsToExecutionChange, SignedBlsToExecutionChange},
epoch_processing::{process_epoch, process_historical_summaries_update},
Expand Down
135 changes: 127 additions & 8 deletions ethereum-consensus/src/deneb/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub fn process_bls_to_execution_change<
const BYTES_PER_LOGS_BLOOM: usize,
const MAX_EXTRA_DATA_BYTES: usize,
>(
_state: &mut BeaconState<
state: &mut BeaconState<
SLOTS_PER_HISTORICAL_ROOT,
HISTORICAL_ROOTS_LIMIT,
ETH1_DATA_VOTES_BOUND,
Expand All @@ -105,10 +105,41 @@ pub fn process_bls_to_execution_change<
BYTES_PER_LOGS_BLOOM,
MAX_EXTRA_DATA_BYTES,
>,
_signed_address_change: &mut SignedBlsToExecutionChange,
_context: &Context,
signed_address_change: &mut SignedBlsToExecutionChange,
context: &Context,
) -> Result<()> {
unimplemented!()
let address_change = &mut signed_address_change.message;
let signature = &signed_address_change.signature;
if address_change.validator_index >= state.validators.len() {
return Err(invalid_operation_error(InvalidOperation::BlsToExecutionChange(
InvalidBlsToExecutionChange::ValidatorIndexOutOfBounds(address_change.validator_index),
)))
}
let withdrawal_credentials =
&mut state.validators[address_change.validator_index].withdrawal_credentials;
if withdrawal_credentials[0] != BLS_WITHDRAWAL_PREFIX {
return Err(invalid_operation_error(InvalidOperation::BlsToExecutionChange(
InvalidBlsToExecutionChange::WithdrawalCredentialsPrefix(withdrawal_credentials[0]),
)))
}
let domain = compute_domain(
DomainType::BlsToExecutionChange,
None,
Some(state.genesis_validators_root),
context,
)?;
let signing_root = compute_signing_root(address_change, domain)?;
let public_key = &address_change.from_bls_public_key;
if withdrawal_credentials[1..] != hash(public_key.as_ref())[1..] {
return Err(invalid_operation_error(InvalidOperation::BlsToExecutionChange(
InvalidBlsToExecutionChange::PublicKeyMismatch(public_key.clone()),
)))
}
verify_signature(public_key, signing_root.as_ref(), signature)?;
withdrawal_credentials[0] = ETH1_ADDRESS_WITHDRAWAL_PREFIX;
withdrawal_credentials[1..12].fill(0);
withdrawal_credentials[12..].copy_from_slice(address_change.to_execution_address.as_ref());
Ok(())
}
pub fn process_operations<
const SLOTS_PER_HISTORICAL_ROOT: usize,
Expand Down Expand Up @@ -205,7 +236,7 @@ pub fn process_withdrawals<
const MAX_TRANSACTIONS_PER_PAYLOAD: usize,
const MAX_WITHDRAWALS_PER_PAYLOAD: usize,
>(
_state: &mut BeaconState<
state: &mut BeaconState<
SLOTS_PER_HISTORICAL_ROOT,
HISTORICAL_ROOTS_LIMIT,
ETH1_DATA_VOTES_BOUND,
Expand All @@ -217,16 +248,104 @@ pub fn process_withdrawals<
BYTES_PER_LOGS_BLOOM,
MAX_EXTRA_DATA_BYTES,
>,
_execution_payload: &ExecutionPayload<
execution_payload: &ExecutionPayload<
BYTES_PER_LOGS_BLOOM,
MAX_EXTRA_DATA_BYTES,
MAX_BYTES_PER_TRANSACTION,
MAX_TRANSACTIONS_PER_PAYLOAD,
MAX_WITHDRAWALS_PER_PAYLOAD,
>,
_context: &Context,
context: &Context,
) -> Result<()> {
unimplemented!()
let expected_withdrawals = get_expected_withdrawals(state, context);
if execution_payload.withdrawals.as_ref() != expected_withdrawals {
return Err(invalid_operation_error(InvalidOperation::Withdrawal(
InvalidWithdrawals::IncorrectWithdrawals {
provided: execution_payload.withdrawals.to_vec(),
expected: expected_withdrawals,
},
)))
}
for withdrawal in &expected_withdrawals {
decrease_balance(state, withdrawal.validator_index, withdrawal.amount);
}
if let Some(latest_withdrawal) = expected_withdrawals.last() {
state.next_withdrawal_index = latest_withdrawal.index + 1;
}
if expected_withdrawals.len() == context.max_withdrawals_per_payload {
let latest_withdrawal = expected_withdrawals.last().expect("empty withdrawals");
let next_validator_index = (latest_withdrawal.validator_index + 1) % state.validators.len();
state.next_withdrawal_validator_index = next_validator_index;
} else {
let next_index =
state.next_withdrawal_validator_index + context.max_validators_per_withdrawals_sweep;
state.next_withdrawal_validator_index = next_index % state.validators.len();
}
Ok(())
}
pub fn get_expected_withdrawals<
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,
>(
state: &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,
>,
context: &Context,
) -> Vec<Withdrawal> {
let epoch = get_current_epoch(state, context);
let mut withdrawal_index = state.next_withdrawal_index;
let mut validator_index = state.next_withdrawal_validator_index;
let mut withdrawals = vec![];
let bound = state.validators.len().min(context.max_validators_per_withdrawals_sweep);
for _ in 0..bound {
let validator = &state.validators[validator_index];
let balance = state.balances[validator_index];
if is_fully_withdrawable_validator(validator, balance, epoch) {
let address =
ExecutionAddress::try_from(&validator.withdrawal_credentials.as_slice()[12..])
.expect("providing the correct amount of input to type");
withdrawals.push(Withdrawal {
index: withdrawal_index,
validator_index,
address,
amount: balance,
});
withdrawal_index += 1;
} else if is_partially_withdrawable_validator(validator, balance, context) {
let address =
ExecutionAddress::try_from(&validator.withdrawal_credentials.as_slice()[12..])
.expect("providing the correct amount of input to type");
withdrawals.push(Withdrawal {
index: withdrawal_index,
validator_index,
address,
amount: balance - context.max_effective_balance,
});
withdrawal_index += 1;
}
if withdrawals.len() == context.max_withdrawals_per_payload {
break
}
validator_index = (validator_index + 1) % state.validators.len();
}
withdrawals
}
pub fn process_deposit<
const SLOTS_PER_HISTORICAL_ROOT: usize,
Expand Down

0 comments on commit 45959b3

Please sign in to comment.