From ab8bbe3931f80c3f533f80b8a91de6c6187e51a6 Mon Sep 17 00:00:00 2001 From: PatStiles Date: Mon, 18 Sep 2023 17:48:26 -0500 Subject: [PATCH] cleanup and ci --- .../src/capella/block_processing.rs | 95 ++++--------------- ethereum-consensus/src/capella/spec/mod.rs | 70 +++++++++++++- 2 files changed, 86 insertions(+), 79 deletions(-) diff --git a/ethereum-consensus/src/capella/block_processing.rs b/ethereum-consensus/src/capella/block_processing.rs index 7ff2616fd..c07f16bc4 100644 --- a/ethereum-consensus/src/capella/block_processing.rs +++ b/ethereum-consensus/src/capella/block_processing.rs @@ -1,21 +1,20 @@ -use std::cmp::min; - use crate::{ capella::{ - compute_timestamp_at_slot, get_current_epoch, get_randao_mix, process_attestation, - process_attester_slashing, process_block_header, process_deposit, process_eth1_data, - process_proposer_slashing, process_randao, process_sync_aggregate, process_voluntary_exit, - BeaconBlock, BeaconBlockBody, BeaconState, ExecutionEngine, ExecutionPayload, - ExecutionPayloadHeader, NewPayloadRequest, SignedBlsToExecutionChange, decrease_balance, + compute_timestamp_at_slot, decrease_balance, get_current_epoch, get_expected_withdrawals, + get_randao_mix, process_attestation, process_attester_slashing, process_block_header, + process_deposit, process_eth1_data, process_proposer_slashing, process_randao, + process_sync_aggregate, process_voluntary_exit, BeaconBlock, BeaconBlockBody, BeaconState, + ExecutionEngine, ExecutionPayload, ExecutionPayloadHeader, NewPayloadRequest, + SignedBlsToExecutionChange, }, state_transition::{ invalid_operation_error, Context, InvalidDeposit, InvalidExecutionPayload, InvalidOperation, Result, - }, phase0::mainnet::MAX_EFFECTIVE_BALANCE, + }, }; use ssz_rs::prelude::*; -use super::{mainnet::{MAX_VALIDATORS_PER_WITHDRAWALS_SWEEP, MAX_WITHDRAWALS_PER_PAYLOAD}, is_fully_withdrawable_validator, Withdrawal, ExecutionAddress, is_partially_withdrawable_validator, ValidatorIndex}; +use super::mainnet::MAX_VALIDATORS_PER_WITHDRAWALS_SWEEP; pub fn process_bls_to_execution_change< const SLOTS_PER_HISTORICAL_ROOT: usize, @@ -267,93 +266,33 @@ pub fn process_withdrawals< ) -> Result<()> { let expected_withdrawals = get_expected_withdrawals(state, context).unwrap(); assert_eq!(execution_payload.withdrawals.len(), expected_withdrawals.len()); - for (expected_withdrawal, withdrawal) in expected_withdrawals.iter().zip(execution_payload.withdrawals.iter()) { + for (expected_withdrawal, withdrawal) in + expected_withdrawals.iter().zip(execution_payload.withdrawals.iter()) + { assert_eq!(withdrawal, expected_withdrawal); decrease_balance(state, withdrawal.validator_index, withdrawal.amount); } - // Update the next withdrawal index if this block contrained withdrawals + // Update the next withdrawal index if this block contained withdrawals if let Some(latest_withdrawal) = expected_withdrawals.last() { state.next_withdrawal_index = latest_withdrawal.index + 1; } - + // Update the next validator index to start the next withdrawal sweep if expected_withdrawals.len() == MAX_WITHDRAWALS_PER_PAYLOAD { // Next sweep starts after the latest withdrawal's validator index + let latest_withdrawal = expected_withdrawals.last().unwrap(); let next_validator_index = (latest_withdrawal.validator_index + 1) % state.validators.len(); state.next_withdrawal_validator_index = next_validator_index; } else { // Advance sweep by the max length of the sweep if there was not a full set of withdrawals - let next_index = state.next_withdrawal_validator_index + MAX_VALIDATORS_PER_WITHDRAWALS_SWEEP; - let next_validator_index = next_index % state.validators.len(); - state.next_withdrawal_validator_index = next_validator_index; + let next_index = + state.next_withdrawal_validator_index + 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: &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, - >, - context: &Context, -) -> Result> { - 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::new(); - let bound = min(state.validators.len(), 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) { - //TODO remove unwrap - let address = ExecutionAddress::try_from(&validator.withdrawal_credentials.as_slice()[12..]).unwrap(); - withdrawals.push(Withdrawal { - index: withdrawal_index, - validator_index, - address, - amount: balance, - }); - withdrawal_index += 1; - } else if is_partially_withdrawable_validator(validator, balance, context) { - //TODO remove unwrap - let address = ExecutionAddress::try_from(&validator.withdrawal_credentials.as_slice()[12..]).unwrap(); - withdrawals.push(Withdrawal { - index: withdrawal_index, - validator_index, - address, - amount: balance - MAX_EFFECTIVE_BALANCE, - }); - withdrawal_index += 1; - } - if withdrawals.len() == MAX_WITHDRAWALS_PER_PAYLOAD { - break - } - validator_index = (validator_index + 1) % state.validators.len(); - } - Ok(withdrawals) -} - pub fn process_block< const SLOTS_PER_HISTORICAL_ROOT: usize, const HISTORICAL_ROOTS_LIMIT: usize, diff --git a/ethereum-consensus/src/capella/spec/mod.rs b/ethereum-consensus/src/capella/spec/mod.rs index c2d18868a..e02fd5d03 100644 --- a/ethereum-consensus/src/capella/spec/mod.rs +++ b/ethereum-consensus/src/capella/spec/mod.rs @@ -66,12 +66,13 @@ use crate::{ eth_aggregate_public_keys, eth_fast_aggregate_verify, fast_aggregate_verify, hash, verify_signature, }, + phase0::mainnet::MAX_EFFECTIVE_BALANCE, ssz::*, }; use integer_sqrt::IntegerSquareRoot; use ssz_rs::prelude::*; use std::{ - cmp, + cmp::{self, min}, collections::{HashMap, HashSet}, iter::zip, mem, @@ -3312,3 +3313,70 @@ pub fn state_transition< state_transition_block_in_slot(state, signed_block, execution_engine, validation, context) } pub use crate::capella::execution_engine::ExecutionEngine; + +use super::mainnet::{MAX_VALIDATORS_PER_WITHDRAWALS_SWEEP, MAX_WITHDRAWALS_PER_PAYLOAD}; + +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, +) -> Result> { + 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::new(); + let bound = min(state.validators.len(), 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..]) + .unwrap(); + 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..]) + .unwrap(); + withdrawals.push(Withdrawal { + index: withdrawal_index, + validator_index, + address, + amount: balance - MAX_EFFECTIVE_BALANCE, + }); + withdrawal_index += 1; + } + if withdrawals.len() == MAX_WITHDRAWALS_PER_PAYLOAD { + break + } + validator_index = (validator_index + 1) % state.validators.len(); + } + Ok(withdrawals) +}