Skip to content

Commit ce66582

Browse files
authored
Merge parent and current sync lookups (#5655)
* Drop lookup type trait for a simple arg * Drop reconstructed for processing * Send parent blocks one by one * Merge current and parent lookups * Merge current and parent lookups clean up todos * Merge current and parent lookups tests * Merge remote-tracking branch 'origin/unstable' into sync-merged-lookup * Merge branch 'unstable' of https://github.com/sigp/lighthouse into sync-merged-lookup * fix compile after merge * #5655 pr review (#26) * fix compile after merge * remove todos, fix typos etc * fix compile * stable rng * delete TODO and unfilled out test * make download result a struct * enums instead of bools as params * fix comment * Various fixes * Track ignored child components * Track dropped lookup reason as metric * fix test * add comment describing behavior of avail check error *  update ordering
1 parent 196d9fd commit ce66582

File tree

17 files changed

+1594
-2464
lines changed

17 files changed

+1594
-2464
lines changed

beacon_node/beacon_chain/src/blob_verification.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,14 @@ pub fn validate_blob_sidecar_for_gossip<T: BeaconChainTypes>(
571571
});
572572
}
573573

574+
// Kzg verification for gossip blob sidecar
575+
let kzg = chain
576+
.kzg
577+
.as_ref()
578+
.ok_or(GossipBlobError::KzgNotInitialized)?;
579+
let kzg_verified_blob = KzgVerifiedBlob::new(blob_sidecar.clone(), kzg, seen_timestamp)
580+
.map_err(GossipBlobError::KzgError)?;
581+
574582
chain
575583
.observed_slashable
576584
.write()
@@ -605,14 +613,6 @@ pub fn validate_blob_sidecar_for_gossip<T: BeaconChainTypes>(
605613
});
606614
}
607615

608-
// Kzg verification for gossip blob sidecar
609-
let kzg = chain
610-
.kzg
611-
.as_ref()
612-
.ok_or(GossipBlobError::KzgNotInitialized)?;
613-
let kzg_verified_blob = KzgVerifiedBlob::new(blob_sidecar, kzg, seen_timestamp)
614-
.map_err(GossipBlobError::KzgError)?;
615-
616616
Ok(GossipVerifiedBlob {
617617
block_root,
618618
blob: kzg_verified_blob,

beacon_node/beacon_chain/src/data_availability_checker.rs

Lines changed: 28 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@ use crate::blob_verification::{verify_kzg_for_blob_list, GossipVerifiedBlob, Kzg
22
use crate::block_verification_types::{
33
AvailabilityPendingExecutedBlock, AvailableExecutedBlock, RpcBlock,
44
};
5-
pub use crate::data_availability_checker::child_components::ChildComponents;
65
use crate::data_availability_checker::overflow_lru_cache::OverflowLRUCache;
76
use crate::{BeaconChain, BeaconChainTypes, BeaconStore};
87
use kzg::Kzg;
9-
use slasher::test_utils::E;
108
use slog::{debug, error, Logger};
119
use slot_clock::SlotClock;
12-
use ssz_types::FixedVector;
1310
use std::fmt;
1411
use std::fmt::Debug;
1512
use std::num::NonZeroUsize;
@@ -19,7 +16,6 @@ use task_executor::TaskExecutor;
1916
use types::blob_sidecar::{BlobIdentifier, BlobSidecar, FixedBlobSidecarList};
2017
use types::{BlobSidecarList, ChainSpec, Epoch, EthSpec, Hash256, SignedBeaconBlock};
2118

22-
mod child_components;
2319
mod error;
2420
mod overflow_lru_cache;
2521
mod state_lru_cache;
@@ -94,68 +90,27 @@ impl<T: BeaconChainTypes> DataAvailabilityChecker<T> {
9490
self.availability_cache.has_block(block_root)
9591
}
9692

97-
pub fn get_missing_blob_ids_with(&self, block_root: Hash256) -> MissingBlobs {
93+
/// Return the required blobs `block_root` expects if the block is currenlty in the cache.
94+
pub fn num_expected_blobs(&self, block_root: &Hash256) -> Option<usize> {
9895
self.availability_cache
99-
.with_pending_components(&block_root, |pending_components| match pending_components {
100-
Some(pending_components) => self.get_missing_blob_ids(
101-
block_root,
102-
pending_components
103-
.get_cached_block()
104-
.as_ref()
105-
.map(|b| b.as_block()),
106-
&pending_components.verified_blobs,
107-
),
108-
None => MissingBlobs::new_without_block(block_root, self.is_deneb()),
96+
.peek_pending_components(block_root, |components| {
97+
components.and_then(|components| components.num_expected_blobs())
10998
})
11099
}
111100

112-
/// If there's no block, all possible ids will be returned that don't exist in the given blobs.
113-
/// If there no blobs, all possible ids will be returned.
114-
pub fn get_missing_blob_ids<V>(
115-
&self,
116-
block_root: Hash256,
117-
block: Option<&SignedBeaconBlock<T::EthSpec>>,
118-
blobs: &FixedVector<Option<V>, <T::EthSpec as EthSpec>::MaxBlobsPerBlock>,
119-
) -> MissingBlobs {
120-
let Some(current_slot) = self.slot_clock.now_or_genesis() else {
121-
error!(
122-
self.log,
123-
"Failed to read slot clock when checking for missing blob ids"
124-
);
125-
return MissingBlobs::BlobsNotRequired;
126-
};
127-
128-
let current_epoch = current_slot.epoch(T::EthSpec::slots_per_epoch());
129-
130-
if self.da_check_required_for_epoch(current_epoch) {
131-
match block {
132-
Some(cached_block) => {
133-
let block_commitments_len = cached_block
134-
.message()
135-
.body()
136-
.blob_kzg_commitments()
137-
.map(|v| v.len())
138-
.unwrap_or(0);
139-
let blob_ids = blobs
101+
/// Return the set of imported blob indexes for `block_root`. Returns None if there is no block
102+
/// component for `block_root`.
103+
pub fn imported_blob_indexes(&self, block_root: &Hash256) -> Option<Vec<u64>> {
104+
self.availability_cache
105+
.peek_pending_components(block_root, |components| {
106+
components.map(|components| {
107+
components
108+
.get_cached_blobs()
140109
.iter()
141-
.take(block_commitments_len)
142-
.enumerate()
143-
.filter_map(|(index, blob_commitment_opt)| {
144-
blob_commitment_opt.is_none().then_some(BlobIdentifier {
145-
block_root,
146-
index: index as u64,
147-
})
148-
})
149-
.collect();
150-
MissingBlobs::KnownMissing(blob_ids)
151-
}
152-
None => {
153-
MissingBlobs::PossibleMissing(BlobIdentifier::get_all_blob_ids::<E>(block_root))
154-
}
155-
}
156-
} else {
157-
MissingBlobs::BlobsNotRequired
158-
}
110+
.filter_map(|blob| blob.as_ref().map(|blob| blob.blob_index()))
111+
.collect::<Vec<_>>()
112+
})
113+
})
159114
}
160115

161116
/// Get a blob from the availability cache.
@@ -351,6 +306,18 @@ impl<T: BeaconChainTypes> DataAvailabilityChecker<T> {
351306
.map_or(false, |da_epoch| block_epoch >= da_epoch)
352307
}
353308

309+
pub fn da_check_required_for_current_epoch(&self) -> bool {
310+
let Some(current_slot) = self.slot_clock.now_or_genesis() else {
311+
error!(
312+
self.log,
313+
"Failed to read slot clock when checking for missing blob ids"
314+
);
315+
return false;
316+
};
317+
318+
self.da_check_required_for_epoch(current_slot.epoch(T::EthSpec::slots_per_epoch()))
319+
}
320+
354321
/// Returns `true` if the current epoch is greater than or equal to the `Deneb` epoch.
355322
pub fn is_deneb(&self) -> bool {
356323
self.slot_clock.now().map_or(false, |slot| {
@@ -544,61 +511,3 @@ impl<E: EthSpec> MaybeAvailableBlock<E> {
544511
}
545512
}
546513
}
547-
548-
#[derive(Debug, Clone)]
549-
pub enum MissingBlobs {
550-
/// We know for certain these blobs are missing.
551-
KnownMissing(Vec<BlobIdentifier>),
552-
/// We think these blobs might be missing.
553-
PossibleMissing(Vec<BlobIdentifier>),
554-
/// Blobs are not required.
555-
BlobsNotRequired,
556-
}
557-
558-
impl MissingBlobs {
559-
pub fn new_without_block(block_root: Hash256, is_deneb: bool) -> Self {
560-
if is_deneb {
561-
MissingBlobs::PossibleMissing(BlobIdentifier::get_all_blob_ids::<E>(block_root))
562-
} else {
563-
MissingBlobs::BlobsNotRequired
564-
}
565-
}
566-
pub fn is_empty(&self) -> bool {
567-
match self {
568-
MissingBlobs::KnownMissing(v) => v.is_empty(),
569-
MissingBlobs::PossibleMissing(v) => v.is_empty(),
570-
MissingBlobs::BlobsNotRequired => true,
571-
}
572-
}
573-
pub fn contains(&self, blob_id: &BlobIdentifier) -> bool {
574-
match self {
575-
MissingBlobs::KnownMissing(v) => v.contains(blob_id),
576-
MissingBlobs::PossibleMissing(v) => v.contains(blob_id),
577-
MissingBlobs::BlobsNotRequired => false,
578-
}
579-
}
580-
pub fn remove(&mut self, blob_id: &BlobIdentifier) {
581-
match self {
582-
MissingBlobs::KnownMissing(v) => v.retain(|id| id != blob_id),
583-
MissingBlobs::PossibleMissing(v) => v.retain(|id| id != blob_id),
584-
MissingBlobs::BlobsNotRequired => {}
585-
}
586-
}
587-
pub fn indices(&self) -> Vec<u64> {
588-
match self {
589-
MissingBlobs::KnownMissing(v) => v.iter().map(|id| id.index).collect(),
590-
MissingBlobs::PossibleMissing(v) => v.iter().map(|id| id.index).collect(),
591-
MissingBlobs::BlobsNotRequired => vec![],
592-
}
593-
}
594-
}
595-
596-
impl Into<Vec<BlobIdentifier>> for MissingBlobs {
597-
fn into(self) -> Vec<BlobIdentifier> {
598-
match self {
599-
MissingBlobs::KnownMissing(v) => v,
600-
MissingBlobs::PossibleMissing(v) => v,
601-
MissingBlobs::BlobsNotRequired => vec![],
602-
}
603-
}
604-
}

beacon_node/beacon_chain/src/data_availability_checker/child_components.rs

Lines changed: 0 additions & 69 deletions
This file was deleted.

beacon_node/beacon_chain/src/data_availability_checker/error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub enum Error {
2222
SlotClockError,
2323
}
2424

25+
#[derive(PartialEq, Eq)]
2526
pub enum ErrorCategory {
2627
/// Internal Errors (not caused by peers)
2728
Internal,

beacon_node/beacon_chain/src/data_availability_checker/overflow_lru_cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ impl<T: BeaconChainTypes> OverflowLRUCache<T> {
569569
}
570570
}
571571

572-
pub fn with_pending_components<R, F: FnOnce(Option<&PendingComponents<T::EthSpec>>) -> R>(
572+
pub fn peek_pending_components<R, F: FnOnce(Option<&PendingComponents<T::EthSpec>>) -> R>(
573573
&self,
574574
block_root: &Hash256,
575575
f: F,

beacon_node/network/src/metrics.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@ lazy_static! {
244244
"sync_parent_block_lookups",
245245
"Number of parent block lookups underway"
246246
);
247+
pub static ref SYNC_LOOKUP_DROPPED: Result<IntCounterVec> = try_create_int_counter_vec(
248+
"sync_lookups_dropped_total",
249+
"Total count of sync lookups dropped by reason",
250+
&["reason"]
251+
);
247252

248253
/*
249254
* Block Delay Metrics

beacon_node/network/src/network_beacon_processor/sync_methods.rs

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ pub enum ChainSegmentProcessId {
3333
RangeBatchId(ChainId, Epoch),
3434
/// Processing ID for a backfill syncing batch.
3535
BackSyncBatchId(Epoch),
36-
/// Processing Id of the parent lookup of a block.
37-
ParentLookup(Hash256),
3836
}
3937

4038
/// Returned when a chain segment import fails.
@@ -396,41 +394,6 @@ impl<T: BeaconChainTypes> NetworkBeaconProcessor<T> {
396394
}
397395
}
398396
}
399-
// this is a parent lookup request from the sync manager
400-
ChainSegmentProcessId::ParentLookup(chain_head) => {
401-
debug!(
402-
self.log, "Processing parent lookup";
403-
"chain_hash" => %chain_head,
404-
"blocks" => downloaded_blocks.len()
405-
);
406-
// parent blocks are ordered from highest slot to lowest, so we need to process in
407-
// reverse
408-
match self
409-
.process_blocks(downloaded_blocks.iter().rev(), notify_execution_layer)
410-
.await
411-
{
412-
(imported_blocks, Err(e)) => {
413-
debug!(self.log, "Parent lookup failed"; "error" => %e.message);
414-
match e.peer_action {
415-
Some(penalty) => BatchProcessResult::FaultyFailure {
416-
imported_blocks: imported_blocks > 0,
417-
penalty,
418-
},
419-
None => BatchProcessResult::NonFaultyFailure,
420-
}
421-
}
422-
(imported_blocks, Ok(_)) => {
423-
debug!(
424-
self.log, "Parent lookup processed successfully";
425-
"chain_hash" => %chain_head,
426-
"imported_blocks" => imported_blocks
427-
);
428-
BatchProcessResult::Success {
429-
was_non_empty: imported_blocks > 0,
430-
}
431-
}
432-
}
433-
}
434397
};
435398

436399
self.send_sync_message(SyncMessage::BatchProcessed { sync_type, result });

beacon_node/network/src/network_beacon_processor/tests.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,7 @@ impl TestRig {
311311
block_root,
312312
RpcBlock::new_without_blobs(Some(block_root), self.next_block.clone()),
313313
std::time::Duration::default(),
314-
BlockProcessType::ParentLookup {
315-
chain_hash: Hash256::random(),
316-
},
314+
BlockProcessType::SingleBlock { id: 0 },
317315
)
318316
.unwrap();
319317
}

0 commit comments

Comments
 (0)