Skip to content

Commit 5d2063d

Browse files
Single-pass epoch processing (sigp#4483)
1 parent 079cd67 commit 5d2063d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1559
-731
lines changed

beacon_node/beacon_chain/src/beacon_chain.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4299,6 +4299,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
42994299
let attestation_packing_timer =
43004300
metrics::start_timer(&metrics::BLOCK_PRODUCTION_ATTESTATION_TIMES);
43014301

4302+
state.build_total_active_balance_cache_at(state.current_epoch(), &self.spec)?;
43024303
let mut prev_filter_cache = HashMap::new();
43034304
let prev_attestation_filter = |att: &AttestationRef<T::EthSpec>| {
43044305
self.filter_op_pool_attestation(&mut prev_filter_cache, att, &state)

beacon_node/beacon_chain/src/block_verification.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ use state_processing::{
7878
block_signature_verifier::{BlockSignatureVerifier, Error as BlockSignatureVerifierError},
7979
per_block_processing, per_slot_processing,
8080
state_advance::partial_state_advance,
81-
BlockProcessingError, BlockSignatureStrategy, ConsensusContext, SlotProcessingError,
81+
AllCaches, BlockProcessingError, BlockSignatureStrategy, ConsensusContext, SlotProcessingError,
8282
StateProcessingStrategy, VerifyBlockRoot,
8383
};
8484
use std::borrow::Cow;
@@ -1712,6 +1712,15 @@ fn load_parent<T: BeaconChainTypes>(
17121712
BeaconChainError::DBInconsistent(format!("Missing state {:?}", parent_state_root))
17131713
})?;
17141714

1715+
if !state.all_caches_built() {
1716+
slog::warn!(
1717+
chain.log,
1718+
"Parent state lacks built caches";
1719+
"block_slot" => block.slot(),
1720+
"state_slot" => state.slot(),
1721+
);
1722+
}
1723+
17151724
if block.slot() != state.slot() {
17161725
slog::warn!(
17171726
chain.log,

beacon_node/beacon_chain/src/builder.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use proto_array::{DisallowedReOrgOffsets, ReOrgThreshold};
2424
use slasher::Slasher;
2525
use slog::{crit, error, info, Logger};
2626
use slot_clock::{SlotClock, TestingSlotClock};
27+
use state_processing::AllCaches;
2728
use std::marker::PhantomData;
2829
use std::sync::Arc;
2930
use std::time::Duration;
@@ -447,7 +448,7 @@ where
447448
// Prime all caches before storing the state in the database and computing the tree hash
448449
// root.
449450
weak_subj_state
450-
.build_caches(&self.spec)
451+
.build_all_caches(&self.spec)
451452
.map_err(|e| format!("Error building caches on checkpoint state: {e:?}"))?;
452453

453454
let computed_state_root = weak_subj_state

beacon_node/beacon_chain/src/canonical_head.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ use itertools::process_results;
5050
use parking_lot::{Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard};
5151
use slog::{crit, debug, error, warn, Logger};
5252
use slot_clock::SlotClock;
53+
use state_processing::AllCaches;
5354
use std::sync::Arc;
5455
use std::time::Duration;
5556
use store::{iter::StateRootsIterator, KeyValueStoreOp, StoreItem};
@@ -666,7 +667,10 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
666667

667668
// Regardless of where we got the state from, attempt to build all the
668669
// caches except the tree hash cache.
669-
new_snapshot.beacon_state.build_caches(&self.spec)?;
670+
new_snapshot
671+
.beacon_state
672+
.build_all_caches(&self.spec)
673+
.map_err(Error::HeadCacheError)?;
670674

671675
let new_cached_head = CachedHead {
672676
snapshot: Arc::new(new_snapshot),

beacon_node/beacon_chain/src/errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ pub enum BeaconChainError {
5353
SlotClockDidNotStart,
5454
NoStateForSlot(Slot),
5555
BeaconStateError(BeaconStateError),
56+
HeadCacheError(EpochCacheError),
5657
DBInconsistent(String),
5758
DBError(store::Error),
5859
ForkChoiceError(ForkChoiceError),

beacon_node/http_api/src/validator_inclusion.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ use eth2::{
44
lighthouse::{GlobalValidatorInclusionData, ValidatorInclusionData},
55
types::ValidatorId,
66
};
7-
use state_processing::per_epoch_processing::{
8-
altair::participation_cache::Error as ParticipationCacheError, process_epoch,
9-
EpochProcessingSummary,
10-
};
11-
use types::{BeaconState, ChainSpec, Epoch, EthSpec};
7+
use state_processing::per_epoch_processing::{process_epoch, EpochProcessingSummary};
8+
use types::{BeaconState, BeaconStateError, ChainSpec, Epoch, EthSpec};
129

1310
/// Returns the state in the last slot of `epoch`.
1411
fn end_of_epoch_state<T: BeaconChainTypes>(
@@ -35,7 +32,7 @@ fn get_epoch_processing_summary<T: EthSpec>(
3532
.map_err(|e| warp_utils::reject::custom_server_error(format!("{:?}", e)))
3633
}
3734

38-
fn convert_cache_error(error: ParticipationCacheError) -> warp::reject::Rejection {
35+
fn convert_cache_error(error: BeaconStateError) -> warp::reject::Rejection {
3936
warp_utils::reject::custom_server_error(format!("{:?}", error))
4037
}
4138

beacon_node/store/src/errors.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::hdiff;
33
use crate::hot_cold_store::HotColdDBError;
44
use ssz::DecodeError;
55
use state_processing::BlockReplayError;
6-
use types::{milhouse, BeaconStateError, Epoch, Hash256, InconsistentFork, Slot};
6+
use types::{milhouse, BeaconStateError, Epoch, EpochCacheError, Hash256, InconsistentFork, Slot};
77

88
pub type Result<T> = std::result::Result<T, Error>;
99

@@ -71,6 +71,7 @@ pub enum Error {
7171
Hdiff(hdiff::Error),
7272
InconsistentFork(InconsistentFork),
7373
ZeroCacheSize,
74+
CacheBuildError(EpochCacheError),
7475
}
7576

7677
pub trait HandleUnavailable<T> {
@@ -141,6 +142,12 @@ impl From<InconsistentFork> for Error {
141142
}
142143
}
143144

145+
impl From<EpochCacheError> for Error {
146+
fn from(e: EpochCacheError) -> Error {
147+
Error::CacheBuildError(e)
148+
}
149+
}
150+
144151
#[derive(Debug)]
145152
pub struct DBError {
146153
pub message: String,

beacon_node/store/src/hot_cold_store.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ use slog::{debug, error, info, warn, Logger};
3030
use ssz::{Decode, Encode};
3131
use ssz_derive::{Decode, Encode};
3232
use state_processing::{
33-
block_replayer::PreSlotHook, BlockProcessingError, BlockReplayer, SlotProcessingError,
33+
block_replayer::PreSlotHook, AllCaches, BlockProcessingError, BlockReplayer,
34+
SlotProcessingError,
3435
};
3536
use std::cmp::min;
3637
use std::collections::VecDeque;
@@ -1152,7 +1153,7 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> HotColdDB<E, Hot, Cold>
11521153
let state_cacher_hook = |opt_state_root: Option<Hash256>, state: &mut BeaconState<_>| {
11531154
// Ensure all caches are built before attempting to cache.
11541155
state.update_tree_hash_cache()?;
1155-
state.build_caches(&self.spec)?;
1156+
state.build_all_caches(&self.spec)?;
11561157

11571158
if let Some(state_root) = opt_state_root {
11581159
// Cache
@@ -1246,7 +1247,7 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> HotColdDB<E, Hot, Cold>
12461247
)?;
12471248

12481249
state.update_tree_hash_cache()?;
1249-
state.build_caches(&self.spec)?;
1250+
state.build_all_caches(&self.spec)?;
12501251
}
12511252

12521253
// Apply state diff. Block replay should have ensured that the diff is now applicable.
@@ -1280,7 +1281,7 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> HotColdDB<E, Hot, Cold>
12801281
let tree_hash_ms = t.elapsed().as_millis();
12811282

12821283
let t = std::time::Instant::now();
1283-
state.build_caches(&self.spec)?;
1284+
state.build_all_caches(&self.spec)?;
12841285
let cache_ms = t.elapsed().as_millis();
12851286

12861287
debug!(
@@ -1358,7 +1359,7 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> HotColdDB<E, Hot, Cold>
13581359

13591360
// Do a tree hash here so that the cache is fully built.
13601361
state.update_tree_hash_cache()?;
1361-
state.build_caches(&self.spec)?;
1362+
state.build_all_caches(&self.spec)?;
13621363

13631364
let latest_block_root = state.get_latest_block_root(*state_root);
13641365
Ok((state, latest_block_root))

0 commit comments

Comments
 (0)