Move RuntimeFixedVector into module and rename

This commit is contained in:
Michael Sproul
2025-01-06 17:24:50 +11:00
parent f66e179a40
commit 440e854199
5 changed files with 102 additions and 106 deletions

View File

@@ -13,10 +13,9 @@ use slog::{debug, Logger};
use std::num::NonZeroUsize;
use std::sync::Arc;
use types::blob_sidecar::BlobIdentifier;
use types::runtime_var_list::RuntimeFixedList;
use types::{
BlobSidecar, ChainSpec, ColumnIndex, DataColumnIdentifier, DataColumnSidecar, Epoch, EthSpec,
Hash256, RuntimeVariableList, SignedBeaconBlock,
Hash256, RuntimeFixedVector, RuntimeVariableList, SignedBeaconBlock,
};
/// This represents the components of a partially available block
@@ -28,7 +27,7 @@ use types::{
#[derive(Clone)]
pub struct PendingComponents<E: EthSpec> {
pub block_root: Hash256,
pub verified_blobs: RuntimeFixedList<Option<KzgVerifiedBlob<E>>>,
pub verified_blobs: RuntimeFixedVector<Option<KzgVerifiedBlob<E>>>,
pub verified_data_columns: Vec<KzgVerifiedCustodyDataColumn<E>>,
pub executed_block: Option<DietAvailabilityPendingExecutedBlock<E>>,
pub reconstruction_started: bool,
@@ -41,7 +40,7 @@ impl<E: EthSpec> PendingComponents<E> {
}
/// Returns an immutable reference to the fixed vector of cached blobs.
pub fn get_cached_blobs(&self) -> &RuntimeFixedList<Option<KzgVerifiedBlob<E>>> {
pub fn get_cached_blobs(&self) -> &RuntimeFixedVector<Option<KzgVerifiedBlob<E>>> {
&self.verified_blobs
}
@@ -62,7 +61,7 @@ impl<E: EthSpec> PendingComponents<E> {
}
/// Returns a mutable reference to the fixed vector of cached blobs.
pub fn get_cached_blobs_mut(&mut self) -> &mut RuntimeFixedList<Option<KzgVerifiedBlob<E>>> {
pub fn get_cached_blobs_mut(&mut self) -> &mut RuntimeFixedVector<Option<KzgVerifiedBlob<E>>> {
&mut self.verified_blobs
}
@@ -134,7 +133,7 @@ impl<E: EthSpec> PendingComponents<E> {
/// Blobs are only inserted if:
/// 1. The blob entry at the index is empty and no block exists.
/// 2. The block exists and its commitment matches the blob's commitment.
pub fn merge_blobs(&mut self, blobs: RuntimeFixedList<Option<KzgVerifiedBlob<E>>>) {
pub fn merge_blobs(&mut self, blobs: RuntimeFixedVector<Option<KzgVerifiedBlob<E>>>) {
for (index, blob) in blobs.iter().cloned().enumerate() {
let Some(blob) = blob else { continue };
self.merge_single_blob(index, blob);
@@ -236,8 +235,7 @@ impl<E: EthSpec> PendingComponents<E> {
pub fn empty(block_root: Hash256, max_len: usize) -> Self {
Self {
block_root,
// TODO(pawan): just make this a vec potentially
verified_blobs: RuntimeFixedList::new(vec![None; max_len]),
verified_blobs: RuntimeFixedVector::new(vec![None; max_len]),
verified_data_columns: vec![],
executed_block: None,
reconstruction_started: false,
@@ -466,7 +464,7 @@ impl<T: BeaconChainTypes> DataAvailabilityCheckerInner<T> {
};
let mut fixed_blobs =
RuntimeFixedList::new(vec![None; self.spec.max_blobs_per_block(epoch) as usize]);
RuntimeFixedVector::new(vec![None; self.spec.max_blobs_per_block(epoch) as usize]);
for blob in kzg_verified_blobs {
if let Some(blob_opt) = fixed_blobs.get_mut(blob.blob_index() as usize) {
@@ -1169,8 +1167,8 @@ mod pending_components_tests {
type Setup<E> = (
SignedBeaconBlock<E>,
RuntimeFixedList<Option<Arc<BlobSidecar<E>>>>,
RuntimeFixedList<Option<Arc<BlobSidecar<E>>>>,
RuntimeFixedVector<Option<Arc<BlobSidecar<E>>>>,
RuntimeFixedVector<Option<Arc<BlobSidecar<E>>>>,
usize,
);
@@ -1180,8 +1178,8 @@ mod pending_components_tests {
let (block, blobs_vec) =
generate_rand_block_and_blobs::<E>(ForkName::Deneb, NumBlobs::Random, &mut rng, &spec);
let max_len = spec.max_blobs_per_block(block.epoch()) as usize;
let mut blobs: RuntimeFixedList<Option<Arc<BlobSidecar<E>>>> =
RuntimeFixedList::default(max_len);
let mut blobs: RuntimeFixedVector<Option<Arc<BlobSidecar<E>>>> =
RuntimeFixedVector::default(max_len);
for blob in blobs_vec {
if let Some(b) = blobs.get_mut(blob.index as usize) {
@@ -1189,8 +1187,8 @@ mod pending_components_tests {
}
}
let mut invalid_blobs: RuntimeFixedList<Option<Arc<BlobSidecar<E>>>> =
RuntimeFixedList::default(max_len);
let mut invalid_blobs: RuntimeFixedVector<Option<Arc<BlobSidecar<E>>>> =
RuntimeFixedVector::default(max_len);
for (index, blob) in blobs.iter().enumerate() {
if let Some(invalid_blob) = blob {
let mut blob_copy = invalid_blob.as_ref().clone();
@@ -1204,16 +1202,16 @@ mod pending_components_tests {
type PendingComponentsSetup<E> = (
DietAvailabilityPendingExecutedBlock<E>,
RuntimeFixedList<Option<KzgVerifiedBlob<E>>>,
RuntimeFixedList<Option<KzgVerifiedBlob<E>>>,
RuntimeFixedVector<Option<KzgVerifiedBlob<E>>>,
RuntimeFixedVector<Option<KzgVerifiedBlob<E>>>,
);
pub fn setup_pending_components(
block: SignedBeaconBlock<E>,
valid_blobs: RuntimeFixedList<Option<Arc<BlobSidecar<E>>>>,
invalid_blobs: RuntimeFixedList<Option<Arc<BlobSidecar<E>>>>,
valid_blobs: RuntimeFixedVector<Option<Arc<BlobSidecar<E>>>>,
invalid_blobs: RuntimeFixedVector<Option<Arc<BlobSidecar<E>>>>,
) -> PendingComponentsSetup<E> {
let blobs = RuntimeFixedList::new(
let blobs = RuntimeFixedVector::new(
valid_blobs
.iter()
.map(|blob_opt| {
@@ -1223,7 +1221,7 @@ mod pending_components_tests {
})
.collect::<Vec<_>>(),
);
let invalid_blobs = RuntimeFixedList::new(
let invalid_blobs = RuntimeFixedVector::new(
invalid_blobs
.iter()
.map(|blob_opt| {

View File

@@ -1,11 +1,8 @@
use crate::test_utils::TestRandom;
use crate::{
beacon_block_body::BLOB_KZG_COMMITMENTS_INDEX, BeaconBlockHeader, BeaconStateError, Blob,
Epoch, EthSpec, FixedVector, Hash256, SignedBeaconBlockHeader, Slot, VariableList,
};
use crate::{
runtime_var_list::RuntimeFixedList, ForkVersionDeserialize, KzgProofs, RuntimeVariableList,
SignedBeaconBlock,
Epoch, EthSpec, FixedVector, ForkVersionDeserialize, Hash256, KzgProofs, RuntimeFixedVector,
RuntimeVariableList, SignedBeaconBlock, SignedBeaconBlockHeader, Slot, VariableList,
};
use crate::{ChainSpec, ForkName};
use bls::Signature;
@@ -297,7 +294,7 @@ impl<E: EthSpec> BlobSidecar<E> {
pub type BlobSidecarList<E> = RuntimeVariableList<Arc<BlobSidecar<E>>>;
/// Alias for a non length-constrained list of `BlobSidecar`s.
pub type FixedBlobSidecarList<E> = RuntimeFixedList<Option<Arc<BlobSidecar<E>>>>;
pub type FixedBlobSidecarList<E> = RuntimeFixedVector<Option<Arc<BlobSidecar<E>>>>;
pub type BlobsList<E> = VariableList<Blob<E>, <E as EthSpec>::MaxBlobCommitmentsPerBlock>;
impl<E: EthSpec> ForkVersionDeserialize for BlobSidecarList<E> {

View File

@@ -108,6 +108,7 @@ pub mod data_column_sidecar;
pub mod data_column_subnet_id;
pub mod light_client_header;
pub mod non_zero_usize;
pub mod runtime_fixed_vector;
pub mod runtime_var_list;
pub use crate::activation_queue::ActivationQueue;
@@ -219,6 +220,7 @@ pub use crate::preset::{
pub use crate::proposer_preparation_data::ProposerPreparationData;
pub use crate::proposer_slashing::ProposerSlashing;
pub use crate::relative_epoch::{Error as RelativeEpochError, RelativeEpoch};
pub use crate::runtime_fixed_vector::RuntimeFixedVector;
pub use crate::runtime_var_list::RuntimeVariableList;
pub use crate::selection_proof::SelectionProof;
pub use crate::shuffling_id::AttestationShufflingId;

View File

@@ -0,0 +1,78 @@
/// Emulates a SSZ `Vector`.
#[derive(Clone, Debug)]
pub struct RuntimeFixedVector<T> {
vec: Vec<T>,
len: usize,
}
impl<T: Clone + Default> RuntimeFixedVector<T> {
pub fn new(vec: Vec<T>) -> Self {
let len = vec.len();
Self { vec, len }
}
pub fn to_vec(&self) -> Vec<T> {
self.vec.clone()
}
pub fn as_slice(&self) -> &[T] {
self.vec.as_slice()
}
#[allow(clippy::len_without_is_empty)]
pub fn len(&self) -> usize {
self.len
}
pub fn into_vec(self) -> Vec<T> {
self.vec
}
pub fn default(max_len: usize) -> Self {
Self {
vec: vec![T::default(); max_len],
len: max_len,
}
}
pub fn take(&mut self) -> Self {
let new = std::mem::take(&mut self.vec);
*self = Self::new(vec![T::default(); self.len]);
Self {
vec: new,
len: self.len,
}
}
}
impl<T> std::ops::Deref for RuntimeFixedVector<T> {
type Target = [T];
fn deref(&self) -> &[T] {
&self.vec[..]
}
}
impl<T> std::ops::DerefMut for RuntimeFixedVector<T> {
fn deref_mut(&mut self) -> &mut [T] {
&mut self.vec[..]
}
}
impl<T> IntoIterator for RuntimeFixedVector<T> {
type Item = T;
type IntoIter = std::vec::IntoIter<T>;
fn into_iter(self) -> Self::IntoIter {
self.vec.into_iter()
}
}
impl<'a, T> IntoIterator for &'a RuntimeFixedVector<T> {
type Item = &'a T;
type IntoIter = std::slice::Iter<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.vec.iter()
}
}

View File

@@ -262,85 +262,6 @@ where
}
}
/// Emulates a SSZ `Vector`.
#[derive(Clone, Debug)]
pub struct RuntimeFixedList<T> {
vec: Vec<T>,
len: usize,
}
impl<T: Clone + Default> RuntimeFixedList<T> {
pub fn new(vec: Vec<T>) -> Self {
let len = vec.len();
Self { vec, len }
}
pub fn to_vec(&self) -> Vec<T> {
self.vec.clone()
}
pub fn as_slice(&self) -> &[T] {
self.vec.as_slice()
}
#[allow(clippy::len_without_is_empty)]
pub fn len(&self) -> usize {
self.len
}
pub fn into_vec(self) -> Vec<T> {
self.vec
}
pub fn default(max_len: usize) -> Self {
Self {
vec: vec![T::default(); max_len],
len: max_len,
}
}
pub fn take(&mut self) -> Self {
let new = std::mem::take(&mut self.vec);
*self = Self::new(vec![T::default(); self.len]);
Self {
vec: new,
len: self.len,
}
}
}
impl<T> std::ops::Deref for RuntimeFixedList<T> {
type Target = [T];
fn deref(&self) -> &[T] {
&self.vec[..]
}
}
impl<T> std::ops::DerefMut for RuntimeFixedList<T> {
fn deref_mut(&mut self) -> &mut [T] {
&mut self.vec[..]
}
}
impl<T> IntoIterator for RuntimeFixedList<T> {
type Item = T;
type IntoIter = std::vec::IntoIter<T>;
fn into_iter(self) -> Self::IntoIter {
self.vec.into_iter()
}
}
impl<'a, T> IntoIterator for &'a RuntimeFixedList<T> {
type Item = &'a T;
type IntoIter = std::slice::Iter<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.vec.iter()
}
}
#[cfg(test)]
mod test {
use super::*;