Skip to content

Commit 25feedf

Browse files
committed
First pass
1 parent 653126f commit 25feedf

29 files changed

+262
-130
lines changed

beacon_node/beacon_chain/src/beacon_chain.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1249,7 +1249,10 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
12491249
pub fn get_blobs(&self, block_root: &Hash256) -> Result<BlobSidecarList<T::EthSpec>, Error> {
12501250
match self.store.get_blobs(block_root)? {
12511251
Some(blobs) => Ok(blobs),
1252-
None => Ok(BlobSidecarList::default()),
1252+
None => Ok(BlobSidecarList::empty(
1253+
// TODO(pawan): fix this
1254+
self.spec.max_blobs_per_block(Epoch::new(0)) as usize,
1255+
)),
12531256
}
12541257
}
12551258

beacon_node/beacon_chain/src/blob_verification.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ use std::time::Duration;
1717
use tree_hash::TreeHash;
1818
use types::blob_sidecar::BlobIdentifier;
1919
use types::{
20-
BeaconStateError, BlobSidecar, Epoch, EthSpec, Hash256, SignedBeaconBlockHeader, Slot,
20+
BeaconStateError, BlobSidecar, Epoch, EthSpec, Hash256, RuntimeVariableList,
21+
SignedBeaconBlockHeader, Slot,
2122
};
2223

2324
/// An error occurred while validating a gossip blob.
@@ -172,10 +173,7 @@ impl<E: EthSpec> From<BeaconStateError> for GossipBlobError<E> {
172173
}
173174
}
174175

175-
pub type GossipVerifiedBlobList<T> = VariableList<
176-
GossipVerifiedBlob<T>,
177-
<<T as BeaconChainTypes>::EthSpec as EthSpec>::MaxBlobsPerBlock,
178-
>;
176+
pub type GossipVerifiedBlobList<T> = RuntimeVariableList<GossipVerifiedBlob<T>>;
179177

180178
/// A wrapper around a `BlobSidecar` that indicates it has been approved for re-gossiping on
181179
/// the p2p network.
@@ -399,7 +397,7 @@ pub fn validate_blob_sidecar_for_gossip<T: BeaconChainTypes>(
399397
// since we only subscribe to `MaxBlobsPerBlock` subnets over gossip network.
400398
// We include this check only for completeness.
401399
// Getting this error would imply something very wrong with our networking decoding logic.
402-
if blob_index >= T::EthSpec::max_blobs_per_block() as u64 {
400+
if blob_index >= chain.spec.max_blobs_per_block(blob_epoch) {
403401
return Err(GossipBlobError::InvalidSubnet {
404402
expected: subnet,
405403
received: blob_index,

beacon_node/beacon_chain/src/block_verification.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,9 @@ fn build_gossip_verified_blobs<T: BeaconChainTypes>(
779779
GossipVerifiedBlob::new(Arc::new(blob), i as u64, chain)?;
780780
gossip_verified_blobs.push(gossip_verified_blob);
781781
}
782-
let gossip_verified_blobs = VariableList::from(gossip_verified_blobs);
782+
let max_len = chain.spec.max_blobs_per_block(block.epoch()) as usize;
783+
let gossip_verified_blobs =
784+
RuntimeVariableList::from_vec(gossip_verified_blobs, max_len);
783785
Ok::<_, BlockContentsError<T::EthSpec>>(gossip_verified_blobs)
784786
})
785787
.transpose()

beacon_node/beacon_chain/src/block_verification_types.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -181,23 +181,6 @@ impl<E: EthSpec> RpcBlock<E> {
181181
})
182182
}
183183

184-
pub fn new_from_fixed(
185-
block_root: Hash256,
186-
block: Arc<SignedBeaconBlock<E>>,
187-
blobs: FixedBlobSidecarList<E>,
188-
) -> Result<Self, AvailabilityCheckError> {
189-
let filtered = blobs
190-
.into_iter()
191-
.filter_map(|b| b.clone())
192-
.collect::<Vec<_>>();
193-
let blobs = if filtered.is_empty() {
194-
None
195-
} else {
196-
Some(VariableList::from(filtered))
197-
};
198-
Self::new(Some(block_root), block, blobs)
199-
}
200-
201184
#[allow(clippy::type_complexity)]
202185
pub fn deconstruct(
203186
self,

beacon_node/beacon_chain/src/data_availability_checker.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ impl<T: BeaconChainTypes> DataAvailabilityChecker<T> {
200200
.ok_or(AvailabilityCheckError::SlotClockError)?;
201201

202202
let verified_blobs =
203-
KzgVerifiedBlobList::new(Vec::from(blobs).into_iter().flatten(), kzg, seen_timestamp)
203+
KzgVerifiedBlobList::new(blobs.into_vec().into_iter().flatten(), kzg, seen_timestamp)
204204
.map_err(AvailabilityCheckError::Kzg)?;
205205

206206
self.availability_cache
@@ -384,14 +384,13 @@ impl<T: BeaconChainTypes> DataAvailabilityChecker<T> {
384384
blocks: Vec<RpcBlock<T::EthSpec>>,
385385
) -> Result<Vec<MaybeAvailableBlock<T::EthSpec>>, AvailabilityCheckError> {
386386
let mut results = Vec::with_capacity(blocks.len());
387-
let all_blobs: BlobSidecarList<T::EthSpec> = blocks
387+
let all_blobs = blocks
388388
.iter()
389389
.filter(|block| self.blobs_required_for_block(block.as_block()))
390390
// this clone is cheap as it's cloning an Arc
391391
.filter_map(|block| block.blobs().cloned())
392392
.flatten()
393-
.collect::<Vec<_>>()
394-
.into();
393+
.collect::<Vec<_>>();
395394

396395
// verify kzg for all blobs at once
397396
if !all_blobs.is_empty() {

beacon_node/beacon_chain/src/data_availability_checker/overflow_lru_cache.rs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ use std::collections::HashSet;
1616
use std::num::NonZeroUsize;
1717
use std::sync::Arc;
1818
use types::blob_sidecar::BlobIdentifier;
19+
use types::runtime_var_list::RuntimeFixedList;
1920
use types::{
2021
BlobSidecar, ChainSpec, ColumnIndex, DataColumnIdentifier, DataColumnSidecar,
21-
DataColumnSidecarList, Epoch, EthSpec, Hash256, SignedBeaconBlock,
22+
DataColumnSidecarList, Epoch, EthSpec, Hash256, RuntimeVariableList, SignedBeaconBlock,
2223
};
2324

2425
pub type DataColumnsToPublish<E> = Option<DataColumnSidecarList<E>>;
@@ -32,7 +33,7 @@ pub type DataColumnsToPublish<E> = Option<DataColumnSidecarList<E>>;
3233
#[derive(Clone)]
3334
pub struct PendingComponents<E: EthSpec> {
3435
pub block_root: Hash256,
35-
pub verified_blobs: FixedVector<Option<KzgVerifiedBlob<E>>, E::MaxBlobsPerBlock>,
36+
pub verified_blobs: RuntimeFixedList<Option<KzgVerifiedBlob<E>>>,
3637
pub verified_data_columns: Vec<KzgVerifiedCustodyDataColumn<E>>,
3738
pub executed_block: Option<DietAvailabilityPendingExecutedBlock<E>>,
3839
pub reconstruction_started: bool,
@@ -50,9 +51,7 @@ impl<E: EthSpec> PendingComponents<E> {
5051
}
5152

5253
/// Returns an immutable reference to the fixed vector of cached blobs.
53-
pub fn get_cached_blobs(
54-
&self,
55-
) -> &FixedVector<Option<KzgVerifiedBlob<E>>, E::MaxBlobsPerBlock> {
54+
pub fn get_cached_blobs(&self) -> &RuntimeFixedList<Option<KzgVerifiedBlob<E>>> {
5655
&self.verified_blobs
5756
}
5857

@@ -73,9 +72,7 @@ impl<E: EthSpec> PendingComponents<E> {
7372
}
7473

7574
/// Returns a mutable reference to the fixed vector of cached blobs.
76-
pub fn get_cached_blobs_mut(
77-
&mut self,
78-
) -> &mut FixedVector<Option<KzgVerifiedBlob<E>>, E::MaxBlobsPerBlock> {
75+
pub fn get_cached_blobs_mut(&mut self) -> &mut RuntimeFixedList<Option<KzgVerifiedBlob<E>>> {
7976
&mut self.verified_blobs
8077
}
8178

@@ -147,10 +144,7 @@ impl<E: EthSpec> PendingComponents<E> {
147144
/// Blobs are only inserted if:
148145
/// 1. The blob entry at the index is empty and no block exists.
149146
/// 2. The block exists and its commitment matches the blob's commitment.
150-
pub fn merge_blobs(
151-
&mut self,
152-
blobs: FixedVector<Option<KzgVerifiedBlob<E>>, E::MaxBlobsPerBlock>,
153-
) {
147+
pub fn merge_blobs(&mut self, blobs: RuntimeFixedList<Option<KzgVerifiedBlob<E>>>) {
154148
for (index, blob) in blobs.iter().cloned().enumerate() {
155149
let Some(blob) = blob else { continue };
156150
self.merge_single_blob(index, blob);
@@ -194,7 +188,7 @@ impl<E: EthSpec> PendingComponents<E> {
194188
/// Blobs that don't match the new block's commitments are evicted.
195189
pub fn merge_block(&mut self, block: DietAvailabilityPendingExecutedBlock<E>) {
196190
self.insert_block(block);
197-
let reinsert = std::mem::take(self.get_cached_blobs_mut());
191+
let reinsert = self.get_cached_blobs_mut().take();
198192
self.merge_blobs(reinsert);
199193
}
200194

@@ -223,10 +217,11 @@ impl<E: EthSpec> PendingComponents<E> {
223217
}
224218

225219
/// Returns an empty `PendingComponents` object with the given block root.
226-
pub fn empty(block_root: Hash256) -> Self {
220+
pub fn empty(block_root: Hash256, max_len: usize) -> Self {
227221
Self {
228222
block_root,
229-
verified_blobs: FixedVector::default(),
223+
// TODO(pawan): just make this a vec potentially
224+
verified_blobs: RuntimeFixedList::new(vec![None; max_len]),
230225
verified_data_columns: vec![],
231226
executed_block: None,
232227
reconstruction_started: false,
@@ -280,7 +275,12 @@ impl<E: EthSpec> PendingComponents<E> {
280275
else {
281276
return Err(AvailabilityCheckError::Unexpected);
282277
};
283-
(Some(VariableList::new(verified_blobs)?), None)
278+
let max_len =
279+
spec.max_blobs_per_block(diet_executed_block.as_block().epoch()) as usize;
280+
(
281+
Some(RuntimeVariableList::new(verified_blobs, max_len)?),
282+
None,
283+
)
284284
}
285285
BlockImportRequirement::CustodyColumns(_) => {
286286
let verified_data_columns = verified_data_columns
@@ -477,7 +477,8 @@ impl<T: BeaconChainTypes> DataAvailabilityCheckerInner<T> {
477477
epoch: Epoch,
478478
kzg_verified_blobs: I,
479479
) -> Result<Availability<T::EthSpec>, AvailabilityCheckError> {
480-
let mut fixed_blobs = FixedVector::default();
480+
let mut fixed_blobs =
481+
RuntimeFixedList::new(vec![None; self.spec.max_blobs_per_block(epoch) as usize]);
481482

482483
for blob in kzg_verified_blobs {
483484
if let Some(blob_opt) = fixed_blobs.get_mut(blob.blob_index() as usize) {
@@ -491,7 +492,9 @@ impl<T: BeaconChainTypes> DataAvailabilityCheckerInner<T> {
491492
let mut pending_components = write_lock
492493
.pop_entry(&block_root)
493494
.map(|(_, v)| v)
494-
.unwrap_or_else(|| PendingComponents::empty(block_root));
495+
.unwrap_or_else(|| {
496+
PendingComponents::empty(block_root, self.spec.max_blobs_per_block(epoch) as usize)
497+
});
495498

496499
// Merge in the blobs.
497500
pending_components.merge_blobs(fixed_blobs);
@@ -527,7 +530,9 @@ impl<T: BeaconChainTypes> DataAvailabilityCheckerInner<T> {
527530
let mut pending_components = write_lock
528531
.pop_entry(&block_root)
529532
.map(|(_, v)| v)
530-
.unwrap_or_else(|| PendingComponents::empty(block_root));
533+
.unwrap_or_else(|| {
534+
PendingComponents::empty(block_root, self.spec.max_blobs_per_block(epoch) as usize)
535+
});
531536

532537
// Merge in the data columns.
533538
pending_components.merge_data_columns(kzg_verified_data_columns)?;
@@ -614,7 +619,9 @@ impl<T: BeaconChainTypes> DataAvailabilityCheckerInner<T> {
614619
let mut pending_components = write_lock
615620
.pop_entry(&block_root)
616621
.map(|(_, v)| v)
617-
.unwrap_or_else(|| PendingComponents::empty(block_root));
622+
.unwrap_or_else(|| {
623+
PendingComponents::empty(block_root, self.spec.max_blobs_per_block(epoch) as usize)
624+
});
618625

619626
// Merge in the block.
620627
pending_components.merge_block(diet_executed_block);

beacon_node/beacon_chain/src/kzg_utils.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,10 @@ fn build_data_column_sidecars<E: EthSpec>(
188188
spec: &ChainSpec,
189189
) -> Result<DataColumnSidecarList<E>, String> {
190190
let number_of_columns = spec.number_of_columns;
191-
let mut columns = vec![Vec::with_capacity(E::max_blobs_per_block()); number_of_columns];
192-
let mut column_kzg_proofs =
193-
vec![Vec::with_capacity(E::max_blobs_per_block()); number_of_columns];
191+
let max_blobs_per_block =
192+
spec.max_blobs_per_block(signed_block_header.message.slot.epoch(E::slots_per_epoch())) as usize;
193+
let mut columns = vec![Vec::with_capacity(max_blobs_per_block); number_of_columns];
194+
let mut column_kzg_proofs = vec![Vec::with_capacity(max_blobs_per_block); number_of_columns];
194195

195196
for (blob_cells, blob_cell_proofs) in blob_cells_and_proofs_vec {
196197
// we iterate over each column, and we construct the column from "top to bottom",

beacon_node/beacon_chain/src/observed_data_sidecars.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub trait ObservableDataSidecar {
2323
fn slot(&self) -> Slot;
2424
fn block_proposer_index(&self) -> u64;
2525
fn index(&self) -> u64;
26-
fn max_num_of_items(spec: &ChainSpec) -> usize;
26+
fn max_num_of_items(spec: &ChainSpec, slot: Slot) -> usize;
2727
}
2828

2929
impl<E: EthSpec> ObservableDataSidecar for BlobSidecar<E> {
@@ -39,8 +39,8 @@ impl<E: EthSpec> ObservableDataSidecar for BlobSidecar<E> {
3939
self.index
4040
}
4141

42-
fn max_num_of_items(_spec: &ChainSpec) -> usize {
43-
E::max_blobs_per_block()
42+
fn max_num_of_items(spec: &ChainSpec, slot: Slot) -> usize {
43+
spec.max_blobs_per_block(slot.epoch(E::slots_per_epoch())) as usize
4444
}
4545
}
4646

@@ -57,7 +57,7 @@ impl<E: EthSpec> ObservableDataSidecar for DataColumnSidecar<E> {
5757
self.index
5858
}
5959

60-
fn max_num_of_items(spec: &ChainSpec) -> usize {
60+
fn max_num_of_items(spec: &ChainSpec, _slot: Slot) -> usize {
6161
spec.number_of_columns
6262
}
6363
}
@@ -102,7 +102,9 @@ impl<T: ObservableDataSidecar> ObservedDataSidecars<T> {
102102
slot: data_sidecar.slot(),
103103
proposer: data_sidecar.block_proposer_index(),
104104
})
105-
.or_insert_with(|| HashSet::with_capacity(T::max_num_of_items(&self.spec)));
105+
.or_insert_with(|| {
106+
HashSet::with_capacity(T::max_num_of_items(&self.spec, data_sidecar.slot()))
107+
});
106108
let did_not_exist = data_indices.insert(data_sidecar.index());
107109

108110
Ok(!did_not_exist)
@@ -122,7 +124,7 @@ impl<T: ObservableDataSidecar> ObservedDataSidecars<T> {
122124
}
123125

124126
fn sanitize_data_sidecar(&self, data_sidecar: &T) -> Result<(), Error> {
125-
if data_sidecar.index() >= T::max_num_of_items(&self.spec) as u64 {
127+
if data_sidecar.index() >= T::max_num_of_items(&self.spec, data_sidecar.slot()) as u64 {
126128
return Err(Error::InvalidDataIndex(data_sidecar.index()));
127129
}
128130
let finalized_slot = self.finalized_slot;

beacon_node/beacon_chain/src/test_utils.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1991,7 +1991,7 @@ where
19911991
let (block, blob_items) = block_contents;
19921992

19931993
let sidecars = blob_items
1994-
.map(|(proofs, blobs)| BlobSidecar::build_sidecars(blobs, &block, proofs))
1994+
.map(|(proofs, blobs)| BlobSidecar::build_sidecars(blobs, &block, proofs, &self.spec))
19951995
.transpose()
19961996
.unwrap();
19971997
let block_hash: SignedBeaconBlockHash = self
@@ -2017,7 +2017,7 @@ where
20172017
let (block, blob_items) = block_contents;
20182018

20192019
let sidecars = blob_items
2020-
.map(|(proofs, blobs)| BlobSidecar::build_sidecars(blobs, &block, proofs))
2020+
.map(|(proofs, blobs)| BlobSidecar::build_sidecars(blobs, &block, proofs, &self.spec))
20212021
.transpose()
20222022
.unwrap();
20232023
let block_root = block.canonical_root();
@@ -2636,7 +2636,8 @@ pub fn generate_rand_block_and_blobs<E: EthSpec>(
26362636
// Get either zero blobs or a random number of blobs between 1 and Max Blobs.
26372637
let payload: &mut FullPayloadDeneb<E> = &mut message.body.execution_payload;
26382638
let num_blobs = match num_blobs {
2639-
NumBlobs::Random => rng.gen_range(1..=E::max_blobs_per_block()),
2639+
// TODO(pawan): thread the chainspec value here
2640+
NumBlobs::Random => rng.gen_range(1..=6),
26402641
NumBlobs::Number(n) => n,
26412642
NumBlobs::None => 0,
26422643
};
@@ -2656,7 +2657,8 @@ pub fn generate_rand_block_and_blobs<E: EthSpec>(
26562657
// Get either zero blobs or a random number of blobs between 1 and Max Blobs.
26572658
let payload: &mut FullPayloadElectra<E> = &mut message.body.execution_payload;
26582659
let num_blobs = match num_blobs {
2659-
NumBlobs::Random => rng.gen_range(1..=E::max_blobs_per_block()),
2660+
// TODO(pawan): thread the chainspec value here
2661+
NumBlobs::Random => rng.gen_range(1..=6),
26602662
NumBlobs::Number(n) => n,
26612663
NumBlobs::None => 0,
26622664
};

beacon_node/client/src/builder.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,11 @@ where
352352
let anchor_block = SignedBeaconBlock::from_ssz_bytes(&anchor_block_bytes, &spec)
353353
.map_err(|e| format!("Unable to parse weak subj block SSZ: {:?}", e))?;
354354
let anchor_blobs = if anchor_block.message().body().has_blobs() {
355+
let max_blobs_len = spec.max_blobs_per_block(anchor_block.epoch()) as usize;
355356
let anchor_blobs_bytes = anchor_blobs_bytes
356357
.ok_or("Blobs for checkpoint must be provided using --checkpoint-blobs")?;
357358
Some(
358-
BlobSidecarList::from_ssz_bytes(&anchor_blobs_bytes)
359+
BlobSidecarList::from_ssz_bytes(&anchor_blobs_bytes, max_blobs_len)
359360
.map_err(|e| format!("Unable to parse weak subj blobs SSZ: {e:?}"))?,
360361
)
361362
} else {

0 commit comments

Comments
 (0)