Skip to content

Commit 440e854

Browse files
committed
Move RuntimeFixedVector into module and rename
1 parent f66e179 commit 440e854

File tree

5 files changed

+102
-106
lines changed

5 files changed

+102
-106
lines changed

beacon_node/beacon_chain/src/data_availability_checker/overflow_lru_cache.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ use slog::{debug, Logger};
1313
use std::num::NonZeroUsize;
1414
use std::sync::Arc;
1515
use types::blob_sidecar::BlobIdentifier;
16-
use types::runtime_var_list::RuntimeFixedList;
1716
use types::{
1817
BlobSidecar, ChainSpec, ColumnIndex, DataColumnIdentifier, DataColumnSidecar, Epoch, EthSpec,
19-
Hash256, RuntimeVariableList, SignedBeaconBlock,
18+
Hash256, RuntimeFixedVector, RuntimeVariableList, SignedBeaconBlock,
2019
};
2120

2221
/// This represents the components of a partially available block
@@ -28,7 +27,7 @@ use types::{
2827
#[derive(Clone)]
2928
pub struct PendingComponents<E: EthSpec> {
3029
pub block_root: Hash256,
31-
pub verified_blobs: RuntimeFixedList<Option<KzgVerifiedBlob<E>>>,
30+
pub verified_blobs: RuntimeFixedVector<Option<KzgVerifiedBlob<E>>>,
3231
pub verified_data_columns: Vec<KzgVerifiedCustodyDataColumn<E>>,
3332
pub executed_block: Option<DietAvailabilityPendingExecutedBlock<E>>,
3433
pub reconstruction_started: bool,
@@ -41,7 +40,7 @@ impl<E: EthSpec> PendingComponents<E> {
4140
}
4241

4342
/// Returns an immutable reference to the fixed vector of cached blobs.
44-
pub fn get_cached_blobs(&self) -> &RuntimeFixedList<Option<KzgVerifiedBlob<E>>> {
43+
pub fn get_cached_blobs(&self) -> &RuntimeFixedVector<Option<KzgVerifiedBlob<E>>> {
4544
&self.verified_blobs
4645
}
4746

@@ -62,7 +61,7 @@ impl<E: EthSpec> PendingComponents<E> {
6261
}
6362

6463
/// Returns a mutable reference to the fixed vector of cached blobs.
65-
pub fn get_cached_blobs_mut(&mut self) -> &mut RuntimeFixedList<Option<KzgVerifiedBlob<E>>> {
64+
pub fn get_cached_blobs_mut(&mut self) -> &mut RuntimeFixedVector<Option<KzgVerifiedBlob<E>>> {
6665
&mut self.verified_blobs
6766
}
6867

@@ -134,7 +133,7 @@ impl<E: EthSpec> PendingComponents<E> {
134133
/// Blobs are only inserted if:
135134
/// 1. The blob entry at the index is empty and no block exists.
136135
/// 2. The block exists and its commitment matches the blob's commitment.
137-
pub fn merge_blobs(&mut self, blobs: RuntimeFixedList<Option<KzgVerifiedBlob<E>>>) {
136+
pub fn merge_blobs(&mut self, blobs: RuntimeFixedVector<Option<KzgVerifiedBlob<E>>>) {
138137
for (index, blob) in blobs.iter().cloned().enumerate() {
139138
let Some(blob) = blob else { continue };
140139
self.merge_single_blob(index, blob);
@@ -236,8 +235,7 @@ impl<E: EthSpec> PendingComponents<E> {
236235
pub fn empty(block_root: Hash256, max_len: usize) -> Self {
237236
Self {
238237
block_root,
239-
// TODO(pawan): just make this a vec potentially
240-
verified_blobs: RuntimeFixedList::new(vec![None; max_len]),
238+
verified_blobs: RuntimeFixedVector::new(vec![None; max_len]),
241239
verified_data_columns: vec![],
242240
executed_block: None,
243241
reconstruction_started: false,
@@ -466,7 +464,7 @@ impl<T: BeaconChainTypes> DataAvailabilityCheckerInner<T> {
466464
};
467465

468466
let mut fixed_blobs =
469-
RuntimeFixedList::new(vec![None; self.spec.max_blobs_per_block(epoch) as usize]);
467+
RuntimeFixedVector::new(vec![None; self.spec.max_blobs_per_block(epoch) as usize]);
470468

471469
for blob in kzg_verified_blobs {
472470
if let Some(blob_opt) = fixed_blobs.get_mut(blob.blob_index() as usize) {
@@ -1169,8 +1167,8 @@ mod pending_components_tests {
11691167

11701168
type Setup<E> = (
11711169
SignedBeaconBlock<E>,
1172-
RuntimeFixedList<Option<Arc<BlobSidecar<E>>>>,
1173-
RuntimeFixedList<Option<Arc<BlobSidecar<E>>>>,
1170+
RuntimeFixedVector<Option<Arc<BlobSidecar<E>>>>,
1171+
RuntimeFixedVector<Option<Arc<BlobSidecar<E>>>>,
11741172
usize,
11751173
);
11761174

@@ -1180,17 +1178,17 @@ mod pending_components_tests {
11801178
let (block, blobs_vec) =
11811179
generate_rand_block_and_blobs::<E>(ForkName::Deneb, NumBlobs::Random, &mut rng, &spec);
11821180
let max_len = spec.max_blobs_per_block(block.epoch()) as usize;
1183-
let mut blobs: RuntimeFixedList<Option<Arc<BlobSidecar<E>>>> =
1184-
RuntimeFixedList::default(max_len);
1181+
let mut blobs: RuntimeFixedVector<Option<Arc<BlobSidecar<E>>>> =
1182+
RuntimeFixedVector::default(max_len);
11851183

11861184
for blob in blobs_vec {
11871185
if let Some(b) = blobs.get_mut(blob.index as usize) {
11881186
*b = Some(Arc::new(blob));
11891187
}
11901188
}
11911189

1192-
let mut invalid_blobs: RuntimeFixedList<Option<Arc<BlobSidecar<E>>>> =
1193-
RuntimeFixedList::default(max_len);
1190+
let mut invalid_blobs: RuntimeFixedVector<Option<Arc<BlobSidecar<E>>>> =
1191+
RuntimeFixedVector::default(max_len);
11941192
for (index, blob) in blobs.iter().enumerate() {
11951193
if let Some(invalid_blob) = blob {
11961194
let mut blob_copy = invalid_blob.as_ref().clone();
@@ -1204,16 +1202,16 @@ mod pending_components_tests {
12041202

12051203
type PendingComponentsSetup<E> = (
12061204
DietAvailabilityPendingExecutedBlock<E>,
1207-
RuntimeFixedList<Option<KzgVerifiedBlob<E>>>,
1208-
RuntimeFixedList<Option<KzgVerifiedBlob<E>>>,
1205+
RuntimeFixedVector<Option<KzgVerifiedBlob<E>>>,
1206+
RuntimeFixedVector<Option<KzgVerifiedBlob<E>>>,
12091207
);
12101208

12111209
pub fn setup_pending_components(
12121210
block: SignedBeaconBlock<E>,
1213-
valid_blobs: RuntimeFixedList<Option<Arc<BlobSidecar<E>>>>,
1214-
invalid_blobs: RuntimeFixedList<Option<Arc<BlobSidecar<E>>>>,
1211+
valid_blobs: RuntimeFixedVector<Option<Arc<BlobSidecar<E>>>>,
1212+
invalid_blobs: RuntimeFixedVector<Option<Arc<BlobSidecar<E>>>>,
12151213
) -> PendingComponentsSetup<E> {
1216-
let blobs = RuntimeFixedList::new(
1214+
let blobs = RuntimeFixedVector::new(
12171215
valid_blobs
12181216
.iter()
12191217
.map(|blob_opt| {
@@ -1223,7 +1221,7 @@ mod pending_components_tests {
12231221
})
12241222
.collect::<Vec<_>>(),
12251223
);
1226-
let invalid_blobs = RuntimeFixedList::new(
1224+
let invalid_blobs = RuntimeFixedVector::new(
12271225
invalid_blobs
12281226
.iter()
12291227
.map(|blob_opt| {

consensus/types/src/blob_sidecar.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
use crate::test_utils::TestRandom;
22
use crate::{
33
beacon_block_body::BLOB_KZG_COMMITMENTS_INDEX, BeaconBlockHeader, BeaconStateError, Blob,
4-
Epoch, EthSpec, FixedVector, Hash256, SignedBeaconBlockHeader, Slot, VariableList,
5-
};
6-
use crate::{
7-
runtime_var_list::RuntimeFixedList, ForkVersionDeserialize, KzgProofs, RuntimeVariableList,
8-
SignedBeaconBlock,
4+
Epoch, EthSpec, FixedVector, ForkVersionDeserialize, Hash256, KzgProofs, RuntimeFixedVector,
5+
RuntimeVariableList, SignedBeaconBlock, SignedBeaconBlockHeader, Slot, VariableList,
96
};
107
use crate::{ChainSpec, ForkName};
118
use bls::Signature;
@@ -297,7 +294,7 @@ impl<E: EthSpec> BlobSidecar<E> {
297294

298295
pub type BlobSidecarList<E> = RuntimeVariableList<Arc<BlobSidecar<E>>>;
299296
/// Alias for a non length-constrained list of `BlobSidecar`s.
300-
pub type FixedBlobSidecarList<E> = RuntimeFixedList<Option<Arc<BlobSidecar<E>>>>;
297+
pub type FixedBlobSidecarList<E> = RuntimeFixedVector<Option<Arc<BlobSidecar<E>>>>;
301298
pub type BlobsList<E> = VariableList<Blob<E>, <E as EthSpec>::MaxBlobCommitmentsPerBlock>;
302299

303300
impl<E: EthSpec> ForkVersionDeserialize for BlobSidecarList<E> {

consensus/types/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ pub mod data_column_sidecar;
108108
pub mod data_column_subnet_id;
109109
pub mod light_client_header;
110110
pub mod non_zero_usize;
111+
pub mod runtime_fixed_vector;
111112
pub mod runtime_var_list;
112113

113114
pub use crate::activation_queue::ActivationQueue;
@@ -219,6 +220,7 @@ pub use crate::preset::{
219220
pub use crate::proposer_preparation_data::ProposerPreparationData;
220221
pub use crate::proposer_slashing::ProposerSlashing;
221222
pub use crate::relative_epoch::{Error as RelativeEpochError, RelativeEpoch};
223+
pub use crate::runtime_fixed_vector::RuntimeFixedVector;
222224
pub use crate::runtime_var_list::RuntimeVariableList;
223225
pub use crate::selection_proof::SelectionProof;
224226
pub use crate::shuffling_id::AttestationShufflingId;
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/// Emulates a SSZ `Vector`.
2+
#[derive(Clone, Debug)]
3+
pub struct RuntimeFixedVector<T> {
4+
vec: Vec<T>,
5+
len: usize,
6+
}
7+
8+
impl<T: Clone + Default> RuntimeFixedVector<T> {
9+
pub fn new(vec: Vec<T>) -> Self {
10+
let len = vec.len();
11+
Self { vec, len }
12+
}
13+
14+
pub fn to_vec(&self) -> Vec<T> {
15+
self.vec.clone()
16+
}
17+
18+
pub fn as_slice(&self) -> &[T] {
19+
self.vec.as_slice()
20+
}
21+
22+
#[allow(clippy::len_without_is_empty)]
23+
pub fn len(&self) -> usize {
24+
self.len
25+
}
26+
27+
pub fn into_vec(self) -> Vec<T> {
28+
self.vec
29+
}
30+
31+
pub fn default(max_len: usize) -> Self {
32+
Self {
33+
vec: vec![T::default(); max_len],
34+
len: max_len,
35+
}
36+
}
37+
38+
pub fn take(&mut self) -> Self {
39+
let new = std::mem::take(&mut self.vec);
40+
*self = Self::new(vec![T::default(); self.len]);
41+
Self {
42+
vec: new,
43+
len: self.len,
44+
}
45+
}
46+
}
47+
48+
impl<T> std::ops::Deref for RuntimeFixedVector<T> {
49+
type Target = [T];
50+
51+
fn deref(&self) -> &[T] {
52+
&self.vec[..]
53+
}
54+
}
55+
56+
impl<T> std::ops::DerefMut for RuntimeFixedVector<T> {
57+
fn deref_mut(&mut self) -> &mut [T] {
58+
&mut self.vec[..]
59+
}
60+
}
61+
62+
impl<T> IntoIterator for RuntimeFixedVector<T> {
63+
type Item = T;
64+
type IntoIter = std::vec::IntoIter<T>;
65+
66+
fn into_iter(self) -> Self::IntoIter {
67+
self.vec.into_iter()
68+
}
69+
}
70+
71+
impl<'a, T> IntoIterator for &'a RuntimeFixedVector<T> {
72+
type Item = &'a T;
73+
type IntoIter = std::slice::Iter<'a, T>;
74+
75+
fn into_iter(self) -> Self::IntoIter {
76+
self.vec.iter()
77+
}
78+
}

consensus/types/src/runtime_var_list.rs

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -262,85 +262,6 @@ where
262262
}
263263
}
264264

265-
/// Emulates a SSZ `Vector`.
266-
#[derive(Clone, Debug)]
267-
pub struct RuntimeFixedList<T> {
268-
vec: Vec<T>,
269-
len: usize,
270-
}
271-
272-
impl<T: Clone + Default> RuntimeFixedList<T> {
273-
pub fn new(vec: Vec<T>) -> Self {
274-
let len = vec.len();
275-
Self { vec, len }
276-
}
277-
278-
pub fn to_vec(&self) -> Vec<T> {
279-
self.vec.clone()
280-
}
281-
282-
pub fn as_slice(&self) -> &[T] {
283-
self.vec.as_slice()
284-
}
285-
286-
#[allow(clippy::len_without_is_empty)]
287-
pub fn len(&self) -> usize {
288-
self.len
289-
}
290-
291-
pub fn into_vec(self) -> Vec<T> {
292-
self.vec
293-
}
294-
295-
pub fn default(max_len: usize) -> Self {
296-
Self {
297-
vec: vec![T::default(); max_len],
298-
len: max_len,
299-
}
300-
}
301-
302-
pub fn take(&mut self) -> Self {
303-
let new = std::mem::take(&mut self.vec);
304-
*self = Self::new(vec![T::default(); self.len]);
305-
Self {
306-
vec: new,
307-
len: self.len,
308-
}
309-
}
310-
}
311-
312-
impl<T> std::ops::Deref for RuntimeFixedList<T> {
313-
type Target = [T];
314-
315-
fn deref(&self) -> &[T] {
316-
&self.vec[..]
317-
}
318-
}
319-
320-
impl<T> std::ops::DerefMut for RuntimeFixedList<T> {
321-
fn deref_mut(&mut self) -> &mut [T] {
322-
&mut self.vec[..]
323-
}
324-
}
325-
326-
impl<T> IntoIterator for RuntimeFixedList<T> {
327-
type Item = T;
328-
type IntoIter = std::vec::IntoIter<T>;
329-
330-
fn into_iter(self) -> Self::IntoIter {
331-
self.vec.into_iter()
332-
}
333-
}
334-
335-
impl<'a, T> IntoIterator for &'a RuntimeFixedList<T> {
336-
type Item = &'a T;
337-
type IntoIter = std::slice::Iter<'a, T>;
338-
339-
fn into_iter(self) -> Self::IntoIter {
340-
self.vec.iter()
341-
}
342-
}
343-
344265
#[cfg(test)]
345266
mod test {
346267
use super::*;

0 commit comments

Comments
 (0)