Skip to content

Commit 053525e

Browse files
authored
Remove DataAvailabilityView trait from ChildComponents (#5421)
* Remove DataAvailabilityView trait from ChildComponents * PR reviews * Update beacon_node/network/src/sync/block_lookups/common.rs Co-authored-by: realbigsean <[email protected]> * Merge branch 'unstable' of https://github.com/sigp/lighthouse into child_components_independent
1 parent feb531f commit 053525e

File tree

7 files changed

+73
-96
lines changed

7 files changed

+73
-96
lines changed

beacon_node/beacon_chain/src/data_availability_checker.rs

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub use processing_cache::ProcessingComponents;
1515
use slasher::test_utils::E;
1616
use slog::{debug, error, Logger};
1717
use slot_clock::SlotClock;
18+
use ssz_types::FixedVector;
1819
use std::fmt;
1920
use std::fmt::Debug;
2021
use std::num::NonZeroUsize;
@@ -112,10 +113,11 @@ impl<T: BeaconChainTypes> DataAvailabilityChecker<T> {
112113

113114
/// If there's no block, all possible ids will be returned that don't exist in the given blobs.
114115
/// If there no blobs, all possible ids will be returned.
115-
pub fn get_missing_blob_ids<V: AvailabilityView<T::EthSpec>>(
116+
pub fn get_missing_blob_ids<V>(
116117
&self,
117118
block_root: Hash256,
118-
availability_view: &V,
119+
block: &Option<Arc<SignedBeaconBlock<T::EthSpec>>>,
120+
blobs: &FixedVector<Option<V>, <T::EthSpec as EthSpec>::MaxBlobsPerBlock>,
119121
) -> MissingBlobs {
120122
let Some(current_slot) = self.slot_clock.now_or_genesis() else {
121123
error!(
@@ -128,49 +130,20 @@ impl<T: BeaconChainTypes> DataAvailabilityChecker<T> {
128130
let current_epoch = current_slot.epoch(T::EthSpec::slots_per_epoch());
129131

130132
if self.da_check_required_for_epoch(current_epoch) {
131-
match availability_view.get_cached_block() {
133+
match block {
132134
Some(cached_block) => {
133135
let block_commitments = cached_block.get_commitments();
134-
let blob_commitments = availability_view.get_cached_blobs();
135-
136-
let num_blobs_expected = block_commitments.len();
137-
let mut blob_ids = Vec::with_capacity(num_blobs_expected);
138-
139-
// Zip here will always limit the number of iterations to the size of
140-
// `block_commitment` because `blob_commitments` will always be populated
141-
// with `Option` values up to `MAX_BLOBS_PER_BLOCK`.
142-
for (index, (block_commitment, blob_commitment_opt)) in block_commitments
143-
.into_iter()
144-
.zip(blob_commitments.iter())
136+
let blob_ids = blobs
137+
.iter()
138+
.take(block_commitments.len())
145139
.enumerate()
146-
{
147-
// Always add a missing blob.
148-
let Some(blob_commitment) = blob_commitment_opt else {
149-
blob_ids.push(BlobIdentifier {
140+
.filter_map(|(index, blob_commitment_opt)| {
141+
blob_commitment_opt.is_none().then_some(BlobIdentifier {
150142
block_root,
151143
index: index as u64,
152-
});
153-
continue;
154-
};
155-
156-
let blob_commitment = *blob_commitment.get_commitment();
157-
158-
// Check for consistency, but this shouldn't happen, an availability view
159-
// should guaruntee consistency.
160-
if blob_commitment != block_commitment {
161-
error!(self.log,
162-
"Inconsistent availability view";
163-
"block_root" => ?block_root,
164-
"block_commitment" => ?block_commitment,
165-
"blob_commitment" => ?blob_commitment,
166-
"index" => index
167-
);
168-
blob_ids.push(BlobIdentifier {
169-
block_root,
170-
index: index as u64,
171-
});
172-
}
173-
}
144+
})
145+
})
146+
.collect();
174147
MissingBlobs::KnownMissing(blob_ids)
175148
}
176149
None => {

beacon_node/beacon_chain/src/data_availability_checker/availability_view.rs

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use super::child_components::ChildComponents;
21
use super::state_lru_cache::DietAvailabilityPendingExecutedBlock;
32
use crate::blob_verification::KzgVerifiedBlob;
43
use crate::block_verification_types::AsBlock;
@@ -195,14 +194,6 @@ impl_availability_view!(
195194
verified_blobs
196195
);
197196

198-
impl_availability_view!(
199-
ChildComponents,
200-
Arc<SignedBeaconBlock<E>>,
201-
Arc<BlobSidecar<E>>,
202-
downloaded_block,
203-
downloaded_blobs
204-
);
205-
206197
pub trait GetCommitments<E: EthSpec> {
207198
fn get_commitments(&self) -> KzgCommitments<E>;
208199
}
@@ -381,23 +372,6 @@ pub mod tests {
381372
(block.into(), blobs, invalid_blobs)
382373
}
383374

384-
type ChildComponentsSetup<E> = (
385-
Arc<SignedBeaconBlock<E>>,
386-
FixedVector<Option<Arc<BlobSidecar<E>>>, <E as EthSpec>::MaxBlobsPerBlock>,
387-
FixedVector<Option<Arc<BlobSidecar<E>>>, <E as EthSpec>::MaxBlobsPerBlock>,
388-
);
389-
390-
pub fn setup_child_components(
391-
block: SignedBeaconBlock<E>,
392-
valid_blobs: FixedVector<Option<Arc<BlobSidecar<E>>>, <E as EthSpec>::MaxBlobsPerBlock>,
393-
invalid_blobs: FixedVector<Option<Arc<BlobSidecar<E>>>, <E as EthSpec>::MaxBlobsPerBlock>,
394-
) -> ChildComponentsSetup<E> {
395-
let blobs = FixedVector::from(valid_blobs.into_iter().cloned().collect::<Vec<_>>());
396-
let invalid_blobs =
397-
FixedVector::from(invalid_blobs.into_iter().cloned().collect::<Vec<_>>());
398-
(Arc::new(block), blobs, invalid_blobs)
399-
}
400-
401375
pub fn assert_cache_consistent<V: AvailabilityView<E>>(cache: V) {
402376
if let Some(cached_block) = cache.get_cached_block() {
403377
let cached_block_commitments = cached_block.get_commitments();
@@ -530,11 +504,4 @@ pub mod tests {
530504
verified_blobs,
531505
setup_pending_components
532506
);
533-
generate_tests!(
534-
child_component_tests,
535-
ChildComponents::<E>,
536-
downloaded_block,
537-
downloaded_blobs,
538-
setup_child_components
539-
);
540507
}

beacon_node/beacon_chain/src/data_availability_checker/child_components.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use crate::block_verification_types::RpcBlock;
2-
use crate::data_availability_checker::AvailabilityView;
32
use bls::Hash256;
43
use std::sync::Arc;
54
use types::blob_sidecar::FixedBlobSidecarList;
6-
use types::{EthSpec, SignedBeaconBlock};
5+
use types::{BlobSidecar, EthSpec, SignedBeaconBlock};
76

87
/// For requests triggered by an `UnknownBlockParent` or `UnknownBlobParent`, this struct
98
/// is used to cache components as they are sent to the network service. We can't use the
@@ -48,6 +47,22 @@ impl<E: EthSpec> ChildComponents<E> {
4847
cache
4948
}
5049

50+
pub fn merge_block(&mut self, block: Arc<SignedBeaconBlock<E>>) {
51+
self.downloaded_block = Some(block);
52+
}
53+
54+
pub fn merge_blob(&mut self, blob: Arc<BlobSidecar<E>>) {
55+
if let Some(blob_ref) = self.downloaded_blobs.get_mut(blob.index as usize) {
56+
*blob_ref = Some(blob);
57+
}
58+
}
59+
60+
pub fn merge_blobs(&mut self, blobs: FixedBlobSidecarList<E>) {
61+
for blob in blobs.iter().flatten() {
62+
self.merge_blob(blob.clone());
63+
}
64+
}
65+
5166
pub fn clear_blobs(&mut self) {
5267
self.downloaded_blobs = FixedBlobSidecarList::default();
5368
}

beacon_node/network/src/sync/block_lookups/common.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::sync::block_lookups::{
88
use crate::sync::manager::{BlockProcessType, Id, SingleLookupReqId};
99
use crate::sync::network_context::SyncNetworkContext;
1010
use beacon_chain::block_verification_types::RpcBlock;
11-
use beacon_chain::data_availability_checker::{AvailabilityView, ChildComponents};
11+
use beacon_chain::data_availability_checker::ChildComponents;
1212
use beacon_chain::{get_block_root, BeaconChainTypes};
1313
use lighthouse_network::rpc::methods::BlobsByRootRequest;
1414
use lighthouse_network::rpc::BlocksByRootRequest;
@@ -17,7 +17,7 @@ use std::ops::IndexMut;
1717
use std::sync::Arc;
1818
use std::time::Duration;
1919
use types::blob_sidecar::{BlobIdentifier, FixedBlobSidecarList};
20-
use types::{BlobSidecar, ChainSpec, EthSpec, Hash256, SignedBeaconBlock};
20+
use types::{BlobSidecar, ChainSpec, Hash256, SignedBeaconBlock};
2121

2222
#[derive(Debug, Copy, Clone)]
2323
pub enum ResponseType {
@@ -371,27 +371,35 @@ impl<L: Lookup, T: BeaconChainTypes> RequestState<L, T> for BlobRequestState<L,
371371

372372
fn verify_response_inner(
373373
&mut self,
374-
_expected_block_root: Hash256,
374+
expected_block_root: Hash256,
375375
blob: Option<Self::ResponseType>,
376376
peer_id: PeerId,
377377
) -> Result<Option<FixedBlobSidecarList<T::EthSpec>>, LookupVerifyError> {
378378
match blob {
379379
Some(blob) => {
380380
let received_id = blob.id();
381+
381382
if !self.requested_ids.contains(&received_id) {
382-
self.state.register_failure_downloading();
383383
Err(LookupVerifyError::UnrequestedBlobId(received_id))
384+
} else if !blob.verify_blob_sidecar_inclusion_proof().unwrap_or(false) {
385+
Err(LookupVerifyError::InvalidInclusionProof)
386+
} else if blob.block_root() != expected_block_root {
387+
Err(LookupVerifyError::UnrequestedHeader)
384388
} else {
385-
// State should remain downloading until we receive the stream terminator.
386-
self.requested_ids.remove(&received_id);
387-
let blob_index = blob.index;
388-
389-
if blob_index >= T::EthSpec::max_blobs_per_block() as u64 {
390-
return Err(LookupVerifyError::InvalidIndex(blob.index));
391-
}
392-
*self.blob_download_queue.index_mut(blob_index as usize) = Some(blob);
393-
Ok(None)
389+
Ok(())
394390
}
391+
.map_err(|e| {
392+
self.state.register_failure_downloading();
393+
e
394+
})?;
395+
396+
// State should remain downloading until we receive the stream terminator.
397+
self.requested_ids.remove(&received_id);
398+
399+
// The inclusion proof check above ensures `blob.index` is < MAX_BLOBS_PER_BLOCK
400+
let blob_index = blob.index;
401+
*self.blob_download_queue.index_mut(blob_index as usize) = Some(blob);
402+
Ok(None)
395403
}
396404
None => {
397405
self.state.state = State::Processing { peer_id };

beacon_node/network/src/sync/block_lookups/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,8 @@ impl<T: BeaconChainTypes> BlockLookups<T> {
570570
| ParentVerifyError::NotEnoughBlobsReturned
571571
| ParentVerifyError::ExtraBlocksReturned
572572
| ParentVerifyError::UnrequestedBlobId(_)
573+
| ParentVerifyError::InvalidInclusionProof
574+
| ParentVerifyError::UnrequestedHeader
573575
| ParentVerifyError::ExtraBlobsReturned
574576
| ParentVerifyError::InvalidIndex(_) => {
575577
let e = e.into();

beacon_node/network/src/sync/block_lookups/parent_lookup.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ pub enum ParentVerifyError {
3838
NotEnoughBlobsReturned,
3939
ExtraBlocksReturned,
4040
UnrequestedBlobId(BlobIdentifier),
41+
InvalidInclusionProof,
42+
UnrequestedHeader,
4143
ExtraBlobsReturned,
4244
InvalidIndex(u64),
4345
PreviousFailure { parent_root: Hash256 },
@@ -244,6 +246,8 @@ impl From<LookupVerifyError> for ParentVerifyError {
244246
E::NoBlockReturned => ParentVerifyError::NoBlockReturned,
245247
E::ExtraBlocksReturned => ParentVerifyError::ExtraBlocksReturned,
246248
E::UnrequestedBlobId(blob_id) => ParentVerifyError::UnrequestedBlobId(blob_id),
249+
E::InvalidInclusionProof => ParentVerifyError::InvalidInclusionProof,
250+
E::UnrequestedHeader => ParentVerifyError::UnrequestedHeader,
247251
E::ExtraBlobsReturned => ParentVerifyError::ExtraBlobsReturned,
248252
E::InvalidIndex(index) => ParentVerifyError::InvalidIndex(index),
249253
E::NotEnoughBlobsReturned => ParentVerifyError::NotEnoughBlobsReturned,

beacon_node/network/src/sync/block_lookups/single_block_lookup.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ use crate::sync::block_lookups::common::{Lookup, RequestState};
33
use crate::sync::block_lookups::Id;
44
use crate::sync::network_context::SyncNetworkContext;
55
use beacon_chain::block_verification_types::RpcBlock;
6+
use beacon_chain::data_availability_checker::ChildComponents;
67
use beacon_chain::data_availability_checker::{
78
AvailabilityCheckError, DataAvailabilityChecker, MissingBlobs,
89
};
9-
use beacon_chain::data_availability_checker::{AvailabilityView, ChildComponents};
1010
use beacon_chain::BeaconChainTypes;
1111
use lighthouse_network::PeerAction;
1212
use slog::{trace, Logger};
@@ -32,6 +32,8 @@ pub enum LookupVerifyError {
3232
NoBlockReturned,
3333
ExtraBlocksReturned,
3434
UnrequestedBlobId(BlobIdentifier),
35+
InvalidInclusionProof,
36+
UnrequestedHeader,
3537
ExtraBlobsReturned,
3638
NotEnoughBlobsReturned,
3739
InvalidIndex(u64),
@@ -247,7 +249,7 @@ impl<L: Lookup, T: BeaconChainTypes> SingleBlockLookup<L, T> {
247249
/// Returns `true` if the block has already been downloaded.
248250
pub(crate) fn block_already_downloaded(&self) -> bool {
249251
if let Some(components) = self.child_components.as_ref() {
250-
components.block_exists()
252+
components.downloaded_block.is_some()
251253
} else {
252254
self.da_checker.has_block(&self.block_root())
253255
}
@@ -274,19 +276,25 @@ impl<L: Lookup, T: BeaconChainTypes> SingleBlockLookup<L, T> {
274276
pub(crate) fn missing_blob_ids(&self) -> MissingBlobs {
275277
let block_root = self.block_root();
276278
if let Some(components) = self.child_components.as_ref() {
277-
self.da_checker.get_missing_blob_ids(block_root, components)
279+
self.da_checker.get_missing_blob_ids(
280+
block_root,
281+
&components.downloaded_block,
282+
&components.downloaded_blobs,
283+
)
278284
} else {
279-
let Some(processing_availability_view) =
280-
self.da_checker.get_processing_components(block_root)
285+
let Some(processing_components) = self.da_checker.get_processing_components(block_root)
281286
else {
282287
return MissingBlobs::new_without_block(block_root, self.da_checker.is_deneb());
283288
};
284-
self.da_checker
285-
.get_missing_blob_ids(block_root, &processing_availability_view)
289+
self.da_checker.get_missing_blob_ids(
290+
block_root,
291+
&processing_components.block,
292+
&processing_components.blob_commitments,
293+
)
286294
}
287295
}
288296

289-
/// Penalizes a blob peer if it should have blobs but didn't return them to us.
297+
/// Penalizes a blob peer if it should have blobs but didn't return them to us.
290298
pub fn penalize_blob_peer(&mut self, cx: &SyncNetworkContext<T>) {
291299
if let Ok(blob_peer) = self.blob_request_state.state.processing_peer() {
292300
cx.report_peer(

0 commit comments

Comments
 (0)