mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-09 19:51:47 +00:00
Single-pass epoch processing and optimised block processing (#5279)
* Single-pass epoch processing (#4483, #4573) Co-authored-by: Michael Sproul <michael@sigmaprime.io> * Delete unused epoch processing code (#5170) * Delete unused epoch processing code * Compare total deltas * Remove unnecessary apply_pending * cargo fmt * Remove newline * Use epoch cache in block packing (#5223) * Remove progressive balances mode (#5224) * inline inactivity_penalty_quotient_for_state * drop previous_epoch_total_active_balance * fc lint * spec compliant process_sync_aggregate (#15) * spec compliant process_sync_aggregate * Update consensus/state_processing/src/per_block_processing/altair/sync_committee.rs Co-authored-by: Michael Sproul <micsproul@gmail.com> --------- Co-authored-by: Michael Sproul <micsproul@gmail.com> * Delete the participation cache (#16) * update help * Fix op_pool tests * Fix fork choice tests * Merge remote-tracking branch 'sigp/unstable' into epoch-single-pass * Simplify exit cache (#5280) * Fix clippy on exit cache * Clean up single-pass a bit (#5282) * Address Mark's review of single-pass (#5386) * Merge remote-tracking branch 'origin/unstable' into epoch-single-pass * Address Sean's review comments (#5414) * Address most of Sean's review comments * Simplify total balance cache building * Clean up unused junk * Merge remote-tracking branch 'origin/unstable' into epoch-single-pass * More self-review * Merge remote-tracking branch 'origin/unstable' into epoch-single-pass * Merge branch 'unstable' into epoch-single-pass * Fix imports for beta compiler * Fix tests, probably
This commit is contained in:
@@ -2,7 +2,7 @@ use crate::attestation_storage::AttestationRef;
|
||||
use crate::max_cover::MaxCover;
|
||||
use crate::reward_cache::RewardCache;
|
||||
use state_processing::common::{
|
||||
altair, base, get_attestation_participation_flag_indices, get_attesting_indices,
|
||||
base, get_attestation_participation_flag_indices, get_attesting_indices,
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
use types::{
|
||||
@@ -30,7 +30,7 @@ impl<'a, E: EthSpec> AttMaxCover<'a, E> {
|
||||
if let BeaconState::Base(ref base_state) = state {
|
||||
Self::new_for_base(att, state, base_state, total_active_balance, spec)
|
||||
} else {
|
||||
Self::new_for_altair_deneb(att, state, reward_cache, total_active_balance, spec)
|
||||
Self::new_for_altair_deneb(att, state, reward_cache, spec)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,18 +47,17 @@ impl<'a, E: EthSpec> AttMaxCover<'a, E> {
|
||||
.get_beacon_committee(att.data.slot, att.data.index)
|
||||
.ok()?;
|
||||
let indices = get_attesting_indices::<E>(committee.committee, &fresh_validators).ok()?;
|
||||
let sqrt_total_active_balance = base::SqrtTotalActiveBalance::new(total_active_balance);
|
||||
let fresh_validators_rewards: HashMap<u64, u64> = indices
|
||||
.iter()
|
||||
.copied()
|
||||
.flat_map(|validator_index| {
|
||||
let reward = base::get_base_reward(
|
||||
state,
|
||||
validator_index as usize,
|
||||
total_active_balance,
|
||||
spec,
|
||||
)
|
||||
.ok()?
|
||||
.checked_div(spec.proposer_reward_quotient)?;
|
||||
let effective_balance =
|
||||
state.get_effective_balance(validator_index as usize).ok()?;
|
||||
let reward =
|
||||
base::get_base_reward(effective_balance, sqrt_total_active_balance, spec)
|
||||
.ok()?
|
||||
.checked_div(spec.proposer_reward_quotient)?;
|
||||
Some((validator_index, reward))
|
||||
})
|
||||
.collect();
|
||||
@@ -73,7 +72,6 @@ impl<'a, E: EthSpec> AttMaxCover<'a, E> {
|
||||
att: AttestationRef<'a, E>,
|
||||
state: &BeaconState<E>,
|
||||
reward_cache: &'a RewardCache,
|
||||
total_active_balance: u64,
|
||||
spec: &ChainSpec,
|
||||
) -> Option<Self> {
|
||||
let att_data = att.attestation_data();
|
||||
@@ -82,8 +80,6 @@ impl<'a, E: EthSpec> AttMaxCover<'a, E> {
|
||||
let att_participation_flags =
|
||||
get_attestation_participation_flag_indices(state, &att_data, inclusion_delay, spec)
|
||||
.ok()?;
|
||||
let base_reward_per_increment =
|
||||
altair::BaseRewardPerIncrement::new(total_active_balance, spec).ok()?;
|
||||
|
||||
let fresh_validators_rewards = att
|
||||
.indexed
|
||||
@@ -99,9 +95,7 @@ impl<'a, E: EthSpec> AttMaxCover<'a, E> {
|
||||
|
||||
let mut proposer_reward_numerator = 0;
|
||||
|
||||
let base_reward =
|
||||
altair::get_base_reward(state, index as usize, base_reward_per_increment, spec)
|
||||
.ok()?;
|
||||
let base_reward = state.get_base_reward(index as usize).ok()?;
|
||||
|
||||
for (flag_index, weight) in PARTICIPATION_FLAG_WEIGHTS.iter().enumerate() {
|
||||
if att_participation_flags.contains(&flag_index) {
|
||||
|
||||
@@ -18,6 +18,8 @@ pub use persistence::{
|
||||
PersistedOperationPoolV15, PersistedOperationPoolV5,
|
||||
};
|
||||
pub use reward_cache::RewardCache;
|
||||
use state_processing::epoch_cache::is_epoch_cache_initialized;
|
||||
use types::EpochCacheError;
|
||||
|
||||
use crate::attestation_storage::{AttestationMap, CheckpointKey};
|
||||
use crate::bls_to_execution_changes::BlsToExecutionChanges;
|
||||
@@ -75,6 +77,8 @@ pub enum OpPoolError {
|
||||
RewardCacheValidatorUnknown(BeaconStateError),
|
||||
RewardCacheOutOfBounds,
|
||||
IncorrectOpPoolVariant,
|
||||
EpochCacheNotInitialized,
|
||||
EpochCacheError(EpochCacheError),
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
@@ -252,6 +256,15 @@ impl<E: EthSpec> OperationPool<E> {
|
||||
curr_epoch_validity_filter: impl for<'a> FnMut(&AttestationRef<'a, E>) -> bool + Send,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<Vec<Attestation<E>>, OpPoolError> {
|
||||
if !matches!(state, BeaconState::Base(_)) {
|
||||
// Epoch cache must be initialized to fetch base reward values in the max cover `score`
|
||||
// function. Currently max cover ignores items on errors. If epoch cache is not
|
||||
// initialized, this function returns an error.
|
||||
if !is_epoch_cache_initialized(state).map_err(OpPoolError::EpochCacheError)? {
|
||||
return Err(OpPoolError::EpochCacheNotInitialized);
|
||||
}
|
||||
}
|
||||
|
||||
// Attestations for the current fork, which may be from the current or previous epoch.
|
||||
let (prev_epoch_key, curr_epoch_key) = CheckpointKey::keys_for_state(state);
|
||||
let all_attestations = self.attestations.read();
|
||||
@@ -773,6 +786,7 @@ mod release_tests {
|
||||
};
|
||||
use lazy_static::lazy_static;
|
||||
use maplit::hashset;
|
||||
use state_processing::epoch_cache::initialize_epoch_cache;
|
||||
use state_processing::{common::get_attesting_indices_from_state, VerifyOperation};
|
||||
use std::collections::BTreeSet;
|
||||
use types::consts::altair::SYNC_COMMITTEE_SUBNET_COUNT;
|
||||
@@ -814,6 +828,15 @@ mod release_tests {
|
||||
(harness, spec)
|
||||
}
|
||||
|
||||
fn get_current_state_initialize_epoch_cache<E: EthSpec>(
|
||||
harness: &BeaconChainHarness<EphemeralHarnessType<E>>,
|
||||
spec: &ChainSpec,
|
||||
) -> BeaconState<E> {
|
||||
let mut state = harness.get_current_state();
|
||||
initialize_epoch_cache(&mut state, spec).unwrap();
|
||||
state
|
||||
}
|
||||
|
||||
/// Test state for sync contribution-related tests.
|
||||
async fn sync_contribution_test_state<E: EthSpec>(
|
||||
num_committees: usize,
|
||||
@@ -847,7 +870,7 @@ mod release_tests {
|
||||
return;
|
||||
}
|
||||
|
||||
let mut state = harness.get_current_state();
|
||||
let mut state = get_current_state_initialize_epoch_cache(&harness, &spec);
|
||||
let slot = state.slot();
|
||||
let committees = state
|
||||
.get_beacon_committees_at_slot(slot)
|
||||
@@ -929,7 +952,7 @@ mod release_tests {
|
||||
let (harness, ref spec) = attestation_test_state::<MainnetEthSpec>(1);
|
||||
|
||||
let op_pool = OperationPool::<MainnetEthSpec>::new();
|
||||
let mut state = harness.get_current_state();
|
||||
let mut state = get_current_state_initialize_epoch_cache(&harness, &spec);
|
||||
|
||||
let slot = state.slot();
|
||||
let committees = state
|
||||
@@ -1004,7 +1027,7 @@ mod release_tests {
|
||||
fn attestation_duplicate() {
|
||||
let (harness, ref spec) = attestation_test_state::<MainnetEthSpec>(1);
|
||||
|
||||
let state = harness.get_current_state();
|
||||
let state = get_current_state_initialize_epoch_cache(&harness, &spec);
|
||||
|
||||
let op_pool = OperationPool::<MainnetEthSpec>::new();
|
||||
|
||||
@@ -1044,7 +1067,7 @@ mod release_tests {
|
||||
fn attestation_pairwise_overlapping() {
|
||||
let (harness, ref spec) = attestation_test_state::<MainnetEthSpec>(1);
|
||||
|
||||
let state = harness.get_current_state();
|
||||
let state = get_current_state_initialize_epoch_cache(&harness, &spec);
|
||||
|
||||
let op_pool = OperationPool::<MainnetEthSpec>::new();
|
||||
|
||||
@@ -1142,7 +1165,7 @@ mod release_tests {
|
||||
|
||||
let (harness, ref spec) = attestation_test_state::<MainnetEthSpec>(num_committees);
|
||||
|
||||
let mut state = harness.get_current_state();
|
||||
let mut state = get_current_state_initialize_epoch_cache(&harness, &spec);
|
||||
|
||||
let op_pool = OperationPool::<MainnetEthSpec>::new();
|
||||
|
||||
@@ -1232,7 +1255,7 @@ mod release_tests {
|
||||
|
||||
let (harness, ref spec) = attestation_test_state::<MainnetEthSpec>(num_committees);
|
||||
|
||||
let mut state = harness.get_current_state();
|
||||
let mut state = get_current_state_initialize_epoch_cache(&harness, &spec);
|
||||
let op_pool = OperationPool::<MainnetEthSpec>::new();
|
||||
|
||||
let slot = state.slot();
|
||||
|
||||
Reference in New Issue
Block a user