Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions beacon_node/lighthouse_network/src/types/topics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use strum::AsRefStr;
use typenum::Unsigned;
use types::{ChainSpec, DataColumnSubnetId, EthSpec, ForkName, SubnetId, SyncSubnetId};
use types::{
ChainSpec, EthSpec,
attestation::SubnetId,
data::{DataColumnSubnetId, all_data_column_sidecar_subnets_from_spec},
fork::ForkName,
sync_committee::SyncSubnetId,
};

use crate::Subnet;

Expand Down Expand Up @@ -115,7 +121,7 @@ pub fn is_fork_non_core_topic(topic: &GossipTopic, _fork_name: ForkName) -> bool

pub fn all_topics_at_fork<E: EthSpec>(fork: ForkName, spec: &ChainSpec) -> Vec<GossipKind> {
// Compute the worst case of all forks
let sampling_subnets = HashSet::from_iter(spec.all_data_column_sidecar_subnets());
let sampling_subnets = HashSet::from_iter(all_data_column_sidecar_subnets_from_spec(spec));
let opts = TopicConfig {
enable_light_client_server: true,
subscribe_all_subnets: true,
Expand Down
50 changes: 15 additions & 35 deletions consensus/types/src/core/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ use safe_arith::{ArithError, SafeArith};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_utils::quoted_u64::MaybeQuoted;
use ssz::Encode;
use ssz_types::{RuntimeVariableList, VariableList};
use ssz_types::RuntimeVariableList;
use tree_hash::TreeHash;

use crate::{
core::{
APPLICATION_DOMAIN_BUILDER, Address, ApplicationDomain, EnrForkId, Epoch, EthSpec,
EthSpecId, ExecutionBlockHash, Hash256, MainnetEthSpec, Slot, Uint256,
},
data::{BlobIdentifier, DataColumnSubnetId, DataColumnsByRootIdentifier},
fork::{Fork, ForkData, ForkName},
};

Expand Down Expand Up @@ -823,10 +822,6 @@ impl ChainSpec {
}
}

pub fn all_data_column_sidecar_subnets(&self) -> impl Iterator<Item = DataColumnSubnetId> {
(0..self.data_column_sidecar_subnet_count).map(DataColumnSubnetId::new)
}

/// Worst-case compressed length for a given payload of size n when using snappy.
///
/// https://github.com/google/snappy/blob/32ded457c0b1fe78ceb8397632c416568d6714a0/snappy.cc#L218C1-L218C47
Expand Down Expand Up @@ -2110,37 +2105,22 @@ fn max_blocks_by_root_request_common(max_request_blocks: u64) -> usize {
.len()
}

fn max_blobs_by_root_request_common(max_request_blob_sidecars: u64) -> usize {
let max_request_blob_sidecars = max_request_blob_sidecars as usize;
let empty_blob_identifier = BlobIdentifier {
block_root: Hash256::zero(),
index: 0,
};

RuntimeVariableList::<BlobIdentifier>::new(
vec![empty_blob_identifier; max_request_blob_sidecars],
max_request_blob_sidecars,
)
.expect("creating a RuntimeVariableList of size `max_request_blob_sidecars` should succeed")
.as_ssz_bytes()
.len()
pub(crate) fn max_blobs_by_root_request_common(max_request_blob_sidecars: u64) -> usize {
(max_request_blob_sidecars as usize)
.safe_mul(40)
.expect("should not overflow")
}

fn max_data_columns_by_root_request_common<E: EthSpec>(max_request_blocks: u64) -> usize {
let max_request_blocks = max_request_blocks as usize;

let empty_data_columns_by_root_id = DataColumnsByRootIdentifier {
block_root: Hash256::zero(),
columns: VariableList::repeat_full(0),
};

RuntimeVariableList::<DataColumnsByRootIdentifier<E>>::new(
vec![empty_data_columns_by_root_id; max_request_blocks],
max_request_blocks,
)
.expect("creating a RuntimeVariableList of size `max_request_blocks` should succeed")
.as_ssz_bytes()
.len()
pub(crate) fn max_data_columns_by_root_request_common<E: EthSpec>(
max_request_blocks: u64,
) -> usize {
let bytes_per_element = 8_usize
.safe_mul(E::number_of_columns())
.and_then(|b| b.safe_add(40))
.expect("should not overflow");
(max_request_blocks as usize)
.safe_mul(bytes_per_element)
.expect("should not overflow")
}

fn default_max_blocks_by_root_request() -> usize {
Expand Down
5 changes: 5 additions & 0 deletions consensus/types/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ pub use signing_data::{SignedRoot, SigningData};
pub use slot_data::SlotData;
pub use slot_epoch::{Epoch, Slot};

#[cfg(test)]
pub(crate) use chain_spec::{
max_blobs_by_root_request_common, max_data_columns_by_root_request_common,
};

pub type Hash256 = alloy_primitives::B256;
pub type Uint256 = alloy_primitives::U256;
pub type Hash64 = alloy_primitives::B64;
Expand Down
36 changes: 36 additions & 0 deletions consensus/types/src/data/blob_sidecar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,39 @@ pub type BlobSidecarList<E> = RuntimeVariableList<Arc<BlobSidecar<E>>>;
/// Alias for a non length-constrained list of `BlobSidecar`s.
pub type FixedBlobSidecarList<E> = RuntimeFixedVector<Option<Arc<BlobSidecar<E>>>>;
pub type BlobsList<E> = VariableList<Blob<E>, <E as EthSpec>::MaxBlobCommitmentsPerBlock>;

#[cfg(test)]
mod tests {
use super::*;
use crate::core::max_blobs_by_root_request_common;
use fixed_bytes::FixedBytesExtended;

// This is the "correct" implementation of max_blobs_by_root_request.
// This test ensures that the simplified implementation doesn't deviate from it.
fn max_blobs_by_root_request_implementation(max_request_blob_sidecars: u64) -> usize {
let max_request_blob_sidecars = max_request_blob_sidecars as usize;
let empty_blob_identifier = BlobIdentifier {
block_root: Hash256::zero(),
index: 0,
};

RuntimeVariableList::<BlobIdentifier>::new(
vec![empty_blob_identifier; max_request_blob_sidecars],
max_request_blob_sidecars,
)
.expect("creating a RuntimeVariableList of size `max_request_blob_sidecars` should succeed")
.as_ssz_bytes()
.len()
}

#[test]
fn max_blobs_by_root_request_matches_simplified() {
for n in [0, 1, 2, 8, 16, 32, 64, 128, 256, 512, 768, 1024, 1152] {
assert_eq!(
max_blobs_by_root_request_common(n),
max_blobs_by_root_request_implementation(n),
"Mismatch at n={n}"
);
}
}
}
40 changes: 40 additions & 0 deletions consensus/types/src/data/data_column_sidecar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,43 @@ impl From<SszError> for DataColumnSidecarError {
Self::SszError(e)
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::core::{MainnetEthSpec, max_data_columns_by_root_request_common};
use fixed_bytes::FixedBytesExtended;
use ssz_types::RuntimeVariableList;

// This is the "correct" implementation of max_data_columns_by_root_request.
// This test ensures that the simplified implementation doesn't deviate from it.
fn max_data_columns_by_root_request_implementation<E: EthSpec>(
max_request_blocks: u64,
) -> usize {
let max_request_blocks = max_request_blocks as usize;

let empty_data_columns_by_root_id = DataColumnsByRootIdentifier {
block_root: Hash256::zero(),
columns: VariableList::repeat_full(0),
};

RuntimeVariableList::<DataColumnsByRootIdentifier<E>>::new(
vec![empty_data_columns_by_root_id; max_request_blocks],
max_request_blocks,
)
.expect("creating a RuntimeVariableList of size `max_request_blocks` should succeed")
.as_ssz_bytes()
.len()
}

#[test]
fn max_data_columns_by_root_request_matches_simplified() {
for n in [0, 1, 2, 8, 16, 32, 64, 128, 256, 512, 1024] {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can make sure it also works for default_max_request_blocks too since that's what's actually gets used?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the value that actually gets used 128?

assert_eq!(
max_data_columns_by_root_request_common::<MainnetEthSpec>(n),
max_data_columns_by_root_request_implementation::<MainnetEthSpec>(n),
"Mismatch at n={n}"
);
}
}
}
6 changes: 6 additions & 0 deletions consensus/types/src/data/data_column_subnet_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,9 @@ impl From<&DataColumnSubnetId> for u64 {
val.0
}
}

pub fn all_data_column_sidecar_subnets_from_spec(
spec: &ChainSpec,
) -> impl Iterator<Item = DataColumnSubnetId> {
(0..spec.data_column_sidecar_subnet_count).map(DataColumnSubnetId::new)
}
2 changes: 1 addition & 1 deletion consensus/types/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub use data_column_sidecar::{
Cell, ColumnIndex, DataColumn, DataColumnSidecar, DataColumnSidecarError,
DataColumnSidecarList, DataColumnsByRootIdentifier,
};
pub use data_column_subnet_id::DataColumnSubnetId;
pub use data_column_subnet_id::{DataColumnSubnetId, all_data_column_sidecar_subnets_from_spec};

use crate::core::EthSpec;
use ssz_types::FixedVector;
Expand Down