mirror of
https://github.com/sigp/lighthouse.git
synced 2026-05-07 16:55:46 +00:00
Fix tree-states tests (#5277)
* Fix beta compiler lints * Fix fork choice tests * Fix op_pool tests --------- Co-authored-by: dapplion <35266934+dapplion@users.noreply.github.com>
This commit is contained in:
@@ -46,9 +46,9 @@ const MAX_FORK_CHOICE_DISTANCE: u64 = 256;
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum Error {
|
enum Error {
|
||||||
BeaconChain(BeaconChainError),
|
BeaconChain(BeaconChainError),
|
||||||
BeaconState(BeaconStateError),
|
|
||||||
Store(store::Error),
|
|
||||||
// We don't use the inner value directly, but it's used in the Debug impl.
|
// We don't use the inner value directly, but it's used in the Debug impl.
|
||||||
|
BeaconState(#[allow(dead_code)] BeaconStateError),
|
||||||
|
Store(#[allow(dead_code)] store::Error),
|
||||||
HeadMissingFromSnapshotCache(#[allow(dead_code)] Hash256),
|
HeadMissingFromSnapshotCache(#[allow(dead_code)] Hash256),
|
||||||
MaxDistanceExceeded {
|
MaxDistanceExceeded {
|
||||||
current_slot: Slot,
|
current_slot: Slot,
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ pub use persistence::{
|
|||||||
PersistedOperationPoolV15, PersistedOperationPoolV5,
|
PersistedOperationPoolV15, PersistedOperationPoolV5,
|
||||||
};
|
};
|
||||||
pub use reward_cache::RewardCache;
|
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::attestation_storage::{AttestationMap, CheckpointKey};
|
||||||
use crate::bls_to_execution_changes::BlsToExecutionChanges;
|
use crate::bls_to_execution_changes::BlsToExecutionChanges;
|
||||||
@@ -75,6 +77,8 @@ pub enum OpPoolError {
|
|||||||
RewardCacheValidatorUnknown(BeaconStateError),
|
RewardCacheValidatorUnknown(BeaconStateError),
|
||||||
RewardCacheOutOfBounds,
|
RewardCacheOutOfBounds,
|
||||||
IncorrectOpPoolVariant,
|
IncorrectOpPoolVariant,
|
||||||
|
EpochCacheNotInitialized,
|
||||||
|
EpochCacheError(EpochCacheError),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
@@ -252,6 +256,17 @@ impl<T: EthSpec> OperationPool<T> {
|
|||||||
curr_epoch_validity_filter: impl for<'a> FnMut(&AttestationRef<'a, T>) -> bool + Send,
|
curr_epoch_validity_filter: impl for<'a> FnMut(&AttestationRef<'a, T>) -> bool + Send,
|
||||||
spec: &ChainSpec,
|
spec: &ChainSpec,
|
||||||
) -> Result<Vec<Attestation<T>>, OpPoolError> {
|
) -> Result<Vec<Attestation<T>>, OpPoolError> {
|
||||||
|
if let BeaconState::Base(_) = state {
|
||||||
|
// Ok
|
||||||
|
} else {
|
||||||
|
// 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 Ok([]).
|
||||||
|
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.
|
// 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 (prev_epoch_key, curr_epoch_key) = CheckpointKey::keys_for_state(state);
|
||||||
let all_attestations = self.attestations.read();
|
let all_attestations = self.attestations.read();
|
||||||
@@ -773,6 +788,7 @@ mod release_tests {
|
|||||||
};
|
};
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use maplit::hashset;
|
use maplit::hashset;
|
||||||
|
use state_processing::epoch_cache::initialize_epoch_cache;
|
||||||
use state_processing::{common::get_attesting_indices_from_state, VerifyOperation};
|
use state_processing::{common::get_attesting_indices_from_state, VerifyOperation};
|
||||||
use std::collections::BTreeSet;
|
use std::collections::BTreeSet;
|
||||||
use types::consts::altair::SYNC_COMMITTEE_SUBNET_COUNT;
|
use types::consts::altair::SYNC_COMMITTEE_SUBNET_COUNT;
|
||||||
@@ -814,6 +830,15 @@ mod release_tests {
|
|||||||
(harness, spec)
|
(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.
|
/// Test state for sync contribution-related tests.
|
||||||
async fn sync_contribution_test_state<E: EthSpec>(
|
async fn sync_contribution_test_state<E: EthSpec>(
|
||||||
num_committees: usize,
|
num_committees: usize,
|
||||||
@@ -847,7 +872,7 @@ mod release_tests {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut state = harness.get_current_state();
|
let mut state = get_current_state_initialize_epoch_cache(&harness, &spec);
|
||||||
let slot = state.slot();
|
let slot = state.slot();
|
||||||
let committees = state
|
let committees = state
|
||||||
.get_beacon_committees_at_slot(slot)
|
.get_beacon_committees_at_slot(slot)
|
||||||
@@ -929,7 +954,7 @@ mod release_tests {
|
|||||||
let (harness, ref spec) = attestation_test_state::<MainnetEthSpec>(1);
|
let (harness, ref spec) = attestation_test_state::<MainnetEthSpec>(1);
|
||||||
|
|
||||||
let op_pool = OperationPool::<MainnetEthSpec>::new();
|
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 slot = state.slot();
|
||||||
let committees = state
|
let committees = state
|
||||||
@@ -1004,7 +1029,7 @@ mod release_tests {
|
|||||||
fn attestation_duplicate() {
|
fn attestation_duplicate() {
|
||||||
let (harness, ref spec) = attestation_test_state::<MainnetEthSpec>(1);
|
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();
|
let op_pool = OperationPool::<MainnetEthSpec>::new();
|
||||||
|
|
||||||
@@ -1044,7 +1069,7 @@ mod release_tests {
|
|||||||
fn attestation_pairwise_overlapping() {
|
fn attestation_pairwise_overlapping() {
|
||||||
let (harness, ref spec) = attestation_test_state::<MainnetEthSpec>(1);
|
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();
|
let op_pool = OperationPool::<MainnetEthSpec>::new();
|
||||||
|
|
||||||
@@ -1142,7 +1167,7 @@ mod release_tests {
|
|||||||
|
|
||||||
let (harness, ref spec) = attestation_test_state::<MainnetEthSpec>(num_committees);
|
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 op_pool = OperationPool::<MainnetEthSpec>::new();
|
||||||
|
|
||||||
@@ -1232,7 +1257,7 @@ mod release_tests {
|
|||||||
|
|
||||||
let (harness, ref spec) = attestation_test_state::<MainnetEthSpec>(num_committees);
|
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 op_pool = OperationPool::<MainnetEthSpec>::new();
|
||||||
|
|
||||||
let slot = state.slot();
|
let slot = state.slot();
|
||||||
|
|||||||
@@ -47,15 +47,7 @@ impl fmt::Debug for ForkChoiceTest {
|
|||||||
impl ForkChoiceTest {
|
impl ForkChoiceTest {
|
||||||
/// Creates a new tester.
|
/// Creates a new tester.
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
// Run fork choice tests against the latest fork.
|
Self::new_with_chain_config(ChainConfig::default())
|
||||||
let spec = ForkName::latest().make_genesis_spec(ChainSpec::default());
|
|
||||||
let harness = BeaconChainHarness::builder(MainnetEthSpec)
|
|
||||||
.spec(spec)
|
|
||||||
.deterministic_keypairs(VALIDATOR_COUNT)
|
|
||||||
.fresh_ephemeral_store()
|
|
||||||
.build();
|
|
||||||
|
|
||||||
Self { harness }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new tester with a custom chain config.
|
/// Creates a new tester with a custom chain config.
|
||||||
@@ -67,6 +59,7 @@ impl ForkChoiceTest {
|
|||||||
.chain_config(chain_config)
|
.chain_config(chain_config)
|
||||||
.deterministic_keypairs(VALIDATOR_COUNT)
|
.deterministic_keypairs(VALIDATOR_COUNT)
|
||||||
.fresh_ephemeral_store()
|
.fresh_ephemeral_store()
|
||||||
|
.mock_execution_layer()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Self { harness }
|
Self { harness }
|
||||||
|
|||||||
@@ -72,25 +72,35 @@ impl PreEpochCache {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn initialize_epoch_cache<E: EthSpec>(
|
pub fn is_epoch_cache_initialized<E: EthSpec>(
|
||||||
state: &mut BeaconState<E>,
|
state: &BeaconState<E>,
|
||||||
spec: &ChainSpec,
|
) -> Result<bool, EpochCacheError> {
|
||||||
) -> Result<(), EpochCacheError> {
|
|
||||||
let current_epoch = state.current_epoch();
|
let current_epoch = state.current_epoch();
|
||||||
let next_epoch = state.next_epoch().map_err(EpochCacheError::BeaconState)?;
|
|
||||||
let epoch_cache: &EpochCache = state.epoch_cache();
|
let epoch_cache: &EpochCache = state.epoch_cache();
|
||||||
let decision_block_root = state
|
let decision_block_root = state
|
||||||
.proposer_shuffling_decision_root(Hash256::zero())
|
.proposer_shuffling_decision_root(Hash256::zero())
|
||||||
.map_err(EpochCacheError::BeaconState)?;
|
.map_err(EpochCacheError::BeaconState)?;
|
||||||
|
|
||||||
if epoch_cache
|
Ok(epoch_cache
|
||||||
.check_validity::<E>(current_epoch, decision_block_root)
|
.check_validity::<E>(current_epoch, decision_block_root)
|
||||||
.is_ok()
|
.is_ok())
|
||||||
{
|
}
|
||||||
|
|
||||||
|
pub fn initialize_epoch_cache<E: EthSpec>(
|
||||||
|
state: &mut BeaconState<E>,
|
||||||
|
spec: &ChainSpec,
|
||||||
|
) -> Result<(), EpochCacheError> {
|
||||||
|
if is_epoch_cache_initialized(state)? {
|
||||||
// `EpochCache` has already been initialized and is valid, no need to initialize.
|
// `EpochCache` has already been initialized and is valid, no need to initialize.
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let current_epoch = state.current_epoch();
|
||||||
|
let next_epoch = state.next_epoch().map_err(EpochCacheError::BeaconState)?;
|
||||||
|
let decision_block_root = state
|
||||||
|
.proposer_shuffling_decision_root(Hash256::zero())
|
||||||
|
.map_err(EpochCacheError::BeaconState)?;
|
||||||
|
|
||||||
state.build_total_active_balance_cache_at(current_epoch, spec)?;
|
state.build_total_active_balance_cache_at(current_epoch, spec)?;
|
||||||
let total_active_balance = state.get_total_active_balance_at_epoch(current_epoch)?;
|
let total_active_balance = state.get_total_active_balance_at_epoch(current_epoch)?;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user