Skip to content

Commit

Permalink
Upgrade consensus-spec-tests to v1.5.0-alpha.8
Browse files Browse the repository at this point in the history
  • Loading branch information
Tumas committed Oct 14, 2024
1 parent 309571d commit 4b4a708
Show file tree
Hide file tree
Showing 19 changed files with 413 additions and 268 deletions.
2 changes: 1 addition & 1 deletion consensus-spec-tests
11 changes: 8 additions & 3 deletions genesis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,19 @@ impl<'config, P: Preset> Incremental<'config, P> {
combined::process_deposit_data(self.config, &mut self.beacon_state, data)?
{
if let Some(state) = self.beacon_state.post_electra_mut() {
let pending_deposits = state.pending_balance_deposits().clone();
let pending_deposits = state.pending_deposits().clone();

for deposit in &pending_deposits {
let balance = state.balances_mut().get_mut(deposit.index)?;
let validator_index = accessors::index_of_public_key(state, deposit.pubkey)
.expect(
"public keys in state.pending_deposits are taken from state.validators",
);

let balance = state.balances_mut().get_mut(validator_index)?;
increase_balance(balance, deposit.amount);
}

*state.pending_balance_deposits_mut() = PersistentList::default();
*state.pending_deposits_mut() = PersistentList::default();
}

let balance = *self.beacon_state.balances().get(validator_index)?;
Expand Down
2 changes: 1 addition & 1 deletion grandine-snapshot-tests
14 changes: 0 additions & 14 deletions helper_functions/src/accessors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,20 +822,6 @@ pub fn get_consolidation_churn_limit<P: Preset>(
get_balance_churn_limit(config, state) - get_activation_exit_churn_limit(config, state)
}

pub fn get_active_balance<P: Preset>(
state: &impl BeaconState<P>,
validator_index: ValidatorIndex,
) -> Result<Gwei> {
let max_effective_balance =
misc::get_max_effective_balance::<P>(state.validators().get(validator_index)?);

core::cmp::min(
state.balances().get(validator_index).copied()?,
max_effective_balance,
)
.pipe(Ok)
}

#[must_use]
pub fn get_pending_balance_to_withdraw<P: Preset>(
state: &impl PostElectraBeaconState<P>,
Expand Down
32 changes: 27 additions & 5 deletions helper_functions/src/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use core::ops::BitOrAssign as _;
use std::sync::Arc;

use anyhow::Result;
use bls::SignatureBytes;
use itertools::Itertools as _;
use ssz::PersistentList;
use std_ext::ArcExt as _;
Expand All @@ -21,15 +22,17 @@ use types::{
containers::ExecutionPayloadHeader as DenebExecutionPayloadHeader,
},
electra::{
beacon_state::BeaconState as ElectraBeaconState, consts::UNSET_DEPOSIT_REQUESTS_START_INDEX,
beacon_state::BeaconState as ElectraBeaconState,
consts::UNSET_DEPOSIT_REQUESTS_START_INDEX, containers::PendingDeposit,
},
phase0::{
beacon_state::BeaconState as Phase0BeaconState,
consts::FAR_FUTURE_EPOCH,
consts::{FAR_FUTURE_EPOCH, GENESIS_SLOT},
containers::{Fork, PendingAttestation},
primitives::H256,
},
preset::Preset,
traits::{BeaconState as _, PostElectraBeaconState as _},
};

use crate::{accessors, misc, mutators, phase0, predicates};
Expand Down Expand Up @@ -639,7 +642,7 @@ pub fn upgrade_to_electra<P: Preset>(
earliest_exit_epoch,
consolidation_balance_to_consume: 0,
earliest_consolidation_epoch: misc::compute_activation_exit_epoch::<P>(epoch),
pending_balance_deposits: PersistentList::default(),
pending_deposits: PersistentList::default(),
pending_partial_withdrawals: PersistentList::default(),
pending_consolidations: PersistentList::default(),
// Cache
Expand All @@ -661,7 +664,26 @@ pub fn upgrade_to_electra<P: Preset>(
.map(|(_, index)| index);

for index in pre_activation {
mutators::queue_entire_balance_and_reset_validator(&mut post, index)?;
let balance = mutators::balance(&mut post, index)?;
let validator_balance = *balance;

*balance = 0;

let validator = post.validators_mut().get_mut(index)?;

validator.effective_balance = 0;
validator.activation_eligibility_epoch = FAR_FUTURE_EPOCH;

let withdrawal_credentials = validator.withdrawal_credentials;
let pubkey = validator.pubkey.to_bytes();

post.pending_deposits_mut().push(PendingDeposit {
pubkey,
withdrawal_credentials,
amount: validator_balance,
signature: SignatureBytes::empty(),
slot: GENESIS_SLOT,
})?;
}

for index in post
Expand All @@ -682,7 +704,7 @@ pub fn upgrade_to_electra<P: Preset>(
mod spec_tests {
use spec_test_utils::Case;
use test_generator::test_resources;
use types::preset::{Mainnet, Minimal};
use types::preset::{Mainnet, Minimal, Preset};

use super::*;

Expand Down
51 changes: 16 additions & 35 deletions helper_functions/src/mutators.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use core::cmp::Ordering;

use anyhow::Result;
use bls::SignatureBytes;
use types::{
config::Config,
electra::{consts::COMPOUNDING_WITHDRAWAL_PREFIX, containers::PendingBalanceDeposit},
electra::{consts::COMPOUNDING_WITHDRAWAL_PREFIX, containers::PendingDeposit},
phase0::{
consts::FAR_FUTURE_EPOCH,
consts::{FAR_FUTURE_EPOCH, GENESIS_SLOT},
primitives::{Epoch, Gwei, ValidatorIndex},
},
preset::Preset,
Expand All @@ -19,7 +20,6 @@ use crate::{
},
error::Error,
misc::compute_activation_exit_epoch,
predicates::has_eth1_withdrawal_credential,
};

pub fn balance<P: Preset>(
Expand Down Expand Up @@ -105,12 +105,10 @@ pub fn switch_to_compounding_validator<P: Preset>(
) -> Result<()> {
let validator = state.validators_mut().get_mut(index)?;

if has_eth1_withdrawal_credential(validator) {
validator.withdrawal_credentials[..COMPOUNDING_WITHDRAWAL_PREFIX.len()]
.copy_from_slice(COMPOUNDING_WITHDRAWAL_PREFIX);
validator.withdrawal_credentials[..COMPOUNDING_WITHDRAWAL_PREFIX.len()]
.copy_from_slice(COMPOUNDING_WITHDRAWAL_PREFIX);

queue_excess_active_balance(state, index)?;
}
queue_excess_active_balance(state, index)?;

Ok(())
}
Expand All @@ -126,36 +124,19 @@ pub fn queue_excess_active_balance<P: Preset>(

*state.balances_mut().get_mut(index)? = P::MIN_ACTIVATION_BALANCE;

state
.pending_balance_deposits_mut()
.push(PendingBalanceDeposit {
index,
amount: excess_balance,
})?;
}

Ok(())
}

pub fn queue_entire_balance_and_reset_validator<P: Preset>(
state: &mut impl PostElectraBeaconState<P>,
index: ValidatorIndex,
) -> Result<()> {
let validator_balance = *balance(state, index)?;

*balance(state, index)? = 0;
let validator = state.validators().get(index)?;

let validator = state.validators_mut().get_mut(index)?;
let pubkey = validator.pubkey.to_bytes();
let withdrawal_credentials = validator.withdrawal_credentials;

validator.effective_balance = 0;
validator.activation_eligibility_epoch = FAR_FUTURE_EPOCH;

state
.pending_balance_deposits_mut()
.push(PendingBalanceDeposit {
index,
amount: validator_balance,
state.pending_deposits_mut().push(PendingDeposit {
pubkey,
withdrawal_credentials,
amount: excess_balance,
signature: SignatureBytes::empty(),
slot: GENESIS_SLOT,
})?;
}

Ok(())
}
Expand Down
3 changes: 3 additions & 0 deletions transition_functions/src/altair/block_processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ pub fn process_deposit_data<P: Preset>(
validator_index,
withdrawal_credentials: vec![withdrawal_credentials],
amounts: smallvec![amount],
signatures: vec![signature],
};

apply_deposits(state, 1, core::iter::once(combined_deposit), NullSlotReport)?;
Expand All @@ -372,6 +373,7 @@ pub fn process_deposit_data<P: Preset>(
pubkey,
withdrawal_credentials,
amounts: smallvec![amount],
signatures: vec![signature],
};

apply_deposits(state, 1, core::iter::once(combined_deposit), NullSlotReport)?;
Expand Down Expand Up @@ -400,6 +402,7 @@ pub fn apply_deposits<P: Preset>(
pubkey,
withdrawal_credentials,
amounts,
..
} => {
let public_key_bytes = pubkey.to_bytes();
let first_amount = amounts[0];
Expand Down
Loading

0 comments on commit 4b4a708

Please sign in to comment.