mirror of
https://github.com/sigp/lighthouse.git
synced 2026-05-08 01:05:47 +00:00
Move RuntimeFixedVector into module and rename
This commit is contained in:
@@ -13,10 +13,9 @@ use slog::{debug, Logger};
|
|||||||
use std::num::NonZeroUsize;
|
use std::num::NonZeroUsize;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use types::blob_sidecar::BlobIdentifier;
|
use types::blob_sidecar::BlobIdentifier;
|
||||||
use types::runtime_var_list::RuntimeFixedList;
|
|
||||||
use types::{
|
use types::{
|
||||||
BlobSidecar, ChainSpec, ColumnIndex, DataColumnIdentifier, DataColumnSidecar, Epoch, EthSpec,
|
BlobSidecar, ChainSpec, ColumnIndex, DataColumnIdentifier, DataColumnSidecar, Epoch, EthSpec,
|
||||||
Hash256, RuntimeVariableList, SignedBeaconBlock,
|
Hash256, RuntimeFixedVector, RuntimeVariableList, SignedBeaconBlock,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// This represents the components of a partially available block
|
/// This represents the components of a partially available block
|
||||||
@@ -28,7 +27,7 @@ use types::{
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct PendingComponents<E: EthSpec> {
|
pub struct PendingComponents<E: EthSpec> {
|
||||||
pub block_root: Hash256,
|
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 verified_data_columns: Vec<KzgVerifiedCustodyDataColumn<E>>,
|
||||||
pub executed_block: Option<DietAvailabilityPendingExecutedBlock<E>>,
|
pub executed_block: Option<DietAvailabilityPendingExecutedBlock<E>>,
|
||||||
pub reconstruction_started: bool,
|
pub reconstruction_started: bool,
|
||||||
@@ -41,7 +40,7 @@ impl<E: EthSpec> PendingComponents<E> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Returns an immutable reference to the fixed vector of cached blobs.
|
/// 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
|
&self.verified_blobs
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,7 +61,7 @@ impl<E: EthSpec> PendingComponents<E> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a mutable reference to the fixed vector of cached blobs.
|
/// 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
|
&mut self.verified_blobs
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,7 +133,7 @@ impl<E: EthSpec> PendingComponents<E> {
|
|||||||
/// Blobs are only inserted if:
|
/// Blobs are only inserted if:
|
||||||
/// 1. The blob entry at the index is empty and no block exists.
|
/// 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.
|
/// 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() {
|
for (index, blob) in blobs.iter().cloned().enumerate() {
|
||||||
let Some(blob) = blob else { continue };
|
let Some(blob) = blob else { continue };
|
||||||
self.merge_single_blob(index, blob);
|
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 {
|
pub fn empty(block_root: Hash256, max_len: usize) -> Self {
|
||||||
Self {
|
Self {
|
||||||
block_root,
|
block_root,
|
||||||
// TODO(pawan): just make this a vec potentially
|
verified_blobs: RuntimeFixedVector::new(vec![None; max_len]),
|
||||||
verified_blobs: RuntimeFixedList::new(vec![None; max_len]),
|
|
||||||
verified_data_columns: vec![],
|
verified_data_columns: vec![],
|
||||||
executed_block: None,
|
executed_block: None,
|
||||||
reconstruction_started: false,
|
reconstruction_started: false,
|
||||||
@@ -466,7 +464,7 @@ impl<T: BeaconChainTypes> DataAvailabilityCheckerInner<T> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let mut fixed_blobs =
|
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 {
|
for blob in kzg_verified_blobs {
|
||||||
if let Some(blob_opt) = fixed_blobs.get_mut(blob.blob_index() as usize) {
|
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> = (
|
type Setup<E> = (
|
||||||
SignedBeaconBlock<E>,
|
SignedBeaconBlock<E>,
|
||||||
RuntimeFixedList<Option<Arc<BlobSidecar<E>>>>,
|
RuntimeFixedVector<Option<Arc<BlobSidecar<E>>>>,
|
||||||
RuntimeFixedList<Option<Arc<BlobSidecar<E>>>>,
|
RuntimeFixedVector<Option<Arc<BlobSidecar<E>>>>,
|
||||||
usize,
|
usize,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -1180,8 +1178,8 @@ mod pending_components_tests {
|
|||||||
let (block, blobs_vec) =
|
let (block, blobs_vec) =
|
||||||
generate_rand_block_and_blobs::<E>(ForkName::Deneb, NumBlobs::Random, &mut rng, &spec);
|
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 max_len = spec.max_blobs_per_block(block.epoch()) as usize;
|
||||||
let mut blobs: RuntimeFixedList<Option<Arc<BlobSidecar<E>>>> =
|
let mut blobs: RuntimeFixedVector<Option<Arc<BlobSidecar<E>>>> =
|
||||||
RuntimeFixedList::default(max_len);
|
RuntimeFixedVector::default(max_len);
|
||||||
|
|
||||||
for blob in blobs_vec {
|
for blob in blobs_vec {
|
||||||
if let Some(b) = blobs.get_mut(blob.index as usize) {
|
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>>>> =
|
let mut invalid_blobs: RuntimeFixedVector<Option<Arc<BlobSidecar<E>>>> =
|
||||||
RuntimeFixedList::default(max_len);
|
RuntimeFixedVector::default(max_len);
|
||||||
for (index, blob) in blobs.iter().enumerate() {
|
for (index, blob) in blobs.iter().enumerate() {
|
||||||
if let Some(invalid_blob) = blob {
|
if let Some(invalid_blob) = blob {
|
||||||
let mut blob_copy = invalid_blob.as_ref().clone();
|
let mut blob_copy = invalid_blob.as_ref().clone();
|
||||||
@@ -1204,16 +1202,16 @@ mod pending_components_tests {
|
|||||||
|
|
||||||
type PendingComponentsSetup<E> = (
|
type PendingComponentsSetup<E> = (
|
||||||
DietAvailabilityPendingExecutedBlock<E>,
|
DietAvailabilityPendingExecutedBlock<E>,
|
||||||
RuntimeFixedList<Option<KzgVerifiedBlob<E>>>,
|
RuntimeFixedVector<Option<KzgVerifiedBlob<E>>>,
|
||||||
RuntimeFixedList<Option<KzgVerifiedBlob<E>>>,
|
RuntimeFixedVector<Option<KzgVerifiedBlob<E>>>,
|
||||||
);
|
);
|
||||||
|
|
||||||
pub fn setup_pending_components(
|
pub fn setup_pending_components(
|
||||||
block: SignedBeaconBlock<E>,
|
block: SignedBeaconBlock<E>,
|
||||||
valid_blobs: RuntimeFixedList<Option<Arc<BlobSidecar<E>>>>,
|
valid_blobs: RuntimeFixedVector<Option<Arc<BlobSidecar<E>>>>,
|
||||||
invalid_blobs: RuntimeFixedList<Option<Arc<BlobSidecar<E>>>>,
|
invalid_blobs: RuntimeFixedVector<Option<Arc<BlobSidecar<E>>>>,
|
||||||
) -> PendingComponentsSetup<E> {
|
) -> PendingComponentsSetup<E> {
|
||||||
let blobs = RuntimeFixedList::new(
|
let blobs = RuntimeFixedVector::new(
|
||||||
valid_blobs
|
valid_blobs
|
||||||
.iter()
|
.iter()
|
||||||
.map(|blob_opt| {
|
.map(|blob_opt| {
|
||||||
@@ -1223,7 +1221,7 @@ mod pending_components_tests {
|
|||||||
})
|
})
|
||||||
.collect::<Vec<_>>(),
|
.collect::<Vec<_>>(),
|
||||||
);
|
);
|
||||||
let invalid_blobs = RuntimeFixedList::new(
|
let invalid_blobs = RuntimeFixedVector::new(
|
||||||
invalid_blobs
|
invalid_blobs
|
||||||
.iter()
|
.iter()
|
||||||
.map(|blob_opt| {
|
.map(|blob_opt| {
|
||||||
|
|||||||
@@ -1,11 +1,8 @@
|
|||||||
use crate::test_utils::TestRandom;
|
use crate::test_utils::TestRandom;
|
||||||
use crate::{
|
use crate::{
|
||||||
beacon_block_body::BLOB_KZG_COMMITMENTS_INDEX, BeaconBlockHeader, BeaconStateError, Blob,
|
beacon_block_body::BLOB_KZG_COMMITMENTS_INDEX, BeaconBlockHeader, BeaconStateError, Blob,
|
||||||
Epoch, EthSpec, FixedVector, Hash256, SignedBeaconBlockHeader, Slot, VariableList,
|
Epoch, EthSpec, FixedVector, ForkVersionDeserialize, Hash256, KzgProofs, RuntimeFixedVector,
|
||||||
};
|
RuntimeVariableList, SignedBeaconBlock, SignedBeaconBlockHeader, Slot, VariableList,
|
||||||
use crate::{
|
|
||||||
runtime_var_list::RuntimeFixedList, ForkVersionDeserialize, KzgProofs, RuntimeVariableList,
|
|
||||||
SignedBeaconBlock,
|
|
||||||
};
|
};
|
||||||
use crate::{ChainSpec, ForkName};
|
use crate::{ChainSpec, ForkName};
|
||||||
use bls::Signature;
|
use bls::Signature;
|
||||||
@@ -297,7 +294,7 @@ impl<E: EthSpec> BlobSidecar<E> {
|
|||||||
|
|
||||||
pub type BlobSidecarList<E> = RuntimeVariableList<Arc<BlobSidecar<E>>>;
|
pub type BlobSidecarList<E> = RuntimeVariableList<Arc<BlobSidecar<E>>>;
|
||||||
/// Alias for a non length-constrained list of `BlobSidecar`s.
|
/// 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>;
|
pub type BlobsList<E> = VariableList<Blob<E>, <E as EthSpec>::MaxBlobCommitmentsPerBlock>;
|
||||||
|
|
||||||
impl<E: EthSpec> ForkVersionDeserialize for BlobSidecarList<E> {
|
impl<E: EthSpec> ForkVersionDeserialize for BlobSidecarList<E> {
|
||||||
|
|||||||
@@ -108,6 +108,7 @@ pub mod data_column_sidecar;
|
|||||||
pub mod data_column_subnet_id;
|
pub mod data_column_subnet_id;
|
||||||
pub mod light_client_header;
|
pub mod light_client_header;
|
||||||
pub mod non_zero_usize;
|
pub mod non_zero_usize;
|
||||||
|
pub mod runtime_fixed_vector;
|
||||||
pub mod runtime_var_list;
|
pub mod runtime_var_list;
|
||||||
|
|
||||||
pub use crate::activation_queue::ActivationQueue;
|
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_preparation_data::ProposerPreparationData;
|
||||||
pub use crate::proposer_slashing::ProposerSlashing;
|
pub use crate::proposer_slashing::ProposerSlashing;
|
||||||
pub use crate::relative_epoch::{Error as RelativeEpochError, RelativeEpoch};
|
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::runtime_var_list::RuntimeVariableList;
|
||||||
pub use crate::selection_proof::SelectionProof;
|
pub use crate::selection_proof::SelectionProof;
|
||||||
pub use crate::shuffling_id::AttestationShufflingId;
|
pub use crate::shuffling_id::AttestationShufflingId;
|
||||||
|
|||||||
78
consensus/types/src/runtime_fixed_vector.rs
Normal file
78
consensus/types/src/runtime_fixed_vector.rs
Normal 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()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|||||||
Reference in New Issue
Block a user