Skip to content

Commit

Permalink
WIP: make number_of_columns configurable, fixed sampling size compa…
Browse files Browse the repository at this point in the history
…rison

* wip: make number_of_columns configurable

* fixed sampling_size comparison
  • Loading branch information
hangleang committed Jan 11, 2025
1 parent 0097d0a commit be575dc
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 94 deletions.
3 changes: 0 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion eip_7594/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ ssz = { workspace = true}
sha2 = { workspace = true }
thiserror = { workspace = true }
try_from_iterator = { workspace = true }
typenum = { workspace = true }
types = { workspace = true }
prometheus_metrics = { workspace = true, optional = true }

Expand Down
73 changes: 36 additions & 37 deletions eip_7594/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ use num_traits::One as _;
use sha2::{Digest as _, Sha256};
use ssz::{ByteVector, ContiguousList, ContiguousVector, SszHash as _, Uint256};
use try_from_iterator::TryFromIterator as _;
use typenum::Unsigned as _;
use types::{
combined::SignedBeaconBlock,
config::Config,
deneb::primitives::{Blob, KzgProof},
fulu::{
consts::NumberOfColumns,
containers::{DataColumnSidecar, MatrixEntry},
primitives::{Cell, ColumnIndex, CustodyIndex},
},
Expand All @@ -35,6 +33,10 @@ mod trusted_setup;
#[cfg(test)]
mod tests;

type ColumnCells = [Cell; CELLS_PER_EXT_BLOB];
type ColumnProofs = [KzgProof; CELLS_PER_EXT_BLOB];
type CellsAndKzgProofs = Vec<(ColumnCells, ColumnProofs)>;

#[cfg(feature = "metrics")]
use prometheus_metrics::METRICS;

Expand Down Expand Up @@ -98,9 +100,8 @@ pub fn compute_columns_for_custody_group(
},
);

let columns_per_custody_group = NumberOfColumns::U64 / number_of_custody_groups;
let mut columns = Vec::new();
for i in 0..columns_per_custody_group {
for i in 0..config.columns_per_group() {
columns.push(ColumnIndex::from(
number_of_custody_groups * i + custody_group,
));
Expand Down Expand Up @@ -139,7 +140,10 @@ pub fn compute_subnets_for_node(
}

/// Verify if the data column sidecar is valid.
pub fn verify_data_column_sidecar<P: Preset>(data_column_sidecar: &DataColumnSidecar<P>) -> bool {
pub fn verify_data_column_sidecar<P: Preset>(
data_column_sidecar: &DataColumnSidecar<P>,
config: &Config,
) -> bool {
let DataColumnSidecar {
index,
column,
Expand All @@ -149,7 +153,7 @@ pub fn verify_data_column_sidecar<P: Preset>(data_column_sidecar: &DataColumnSid
} = data_column_sidecar;

// The sidecar index must be within the valid range
if *index >= NumberOfColumns::U64 {
if *index >= config.number_of_columns {
return false;
}

Expand Down Expand Up @@ -255,7 +259,7 @@ pub fn compute_matrix(blobs: &[CKzgBlob]) -> Result<Vec<MatrixEntry>> {
let (cells, proofs) = CKzgCell::compute_cells_and_kzg_proofs(blob, kzg_settings)?;
for (cell_index, (cell, proof)) in cells.into_iter().zip(proofs.into_iter()).enumerate() {
matrix.push(MatrixEntry {
cell: try_convert_ckzg_cell_to_cell(&cell)?,
cell: try_convert_to_cell(&cell)?,
kzg_proof: KzgProof::from(proof.to_bytes().into_inner()),
row_index: blob_index as u64,
column_index: cell_index as u64,
Expand Down Expand Up @@ -303,7 +307,7 @@ pub fn recover_matrix(
.enumerate()
{
matrix.push(MatrixEntry {
cell: try_convert_ckzg_cell_to_cell(&cell)?,
cell: try_convert_to_cell(&cell)?,
kzg_proof: KzgProof::from(proof.to_bytes().into_inner()),
row_index: blob_index as u64,
column_index: cell_index as u64,
Expand All @@ -314,10 +318,10 @@ pub fn recover_matrix(
Ok(matrix)
}

// TODO(peerdas-fulu): refactor params
pub fn construct_data_column_sidecars<P: Preset>(
signed_block: &SignedBeaconBlock<P>,
cells_and_kzg_proofs: &[([Cell; CELLS_PER_EXT_BLOB], [KzgProof; CELLS_PER_EXT_BLOB])],
cells_and_kzg_proofs: &CellsAndKzgProofs,
config: &Config,
) -> Result<Vec<DataColumnSidecar<P>>> {
let signed_block_header = signed_block.to_header();

Expand All @@ -341,7 +345,7 @@ pub fn construct_data_column_sidecars<P: Preset>(
let kzg_commitments_inclusion_proof =
misc::kzg_commitments_inclusion_proof(post_electra_beacon_block_body);

for column_index in 0..NumberOfColumns::USIZE {
for column_index in 0..config.number_of_columns() {
let column = ContiguousList::try_from_iter(
(0..blob_count)
.map(|row_index| cells_and_kzg_proofs[row_index].0[column_index].clone()),
Expand All @@ -366,7 +370,7 @@ pub fn construct_data_column_sidecars<P: Preset>(

pub fn try_convert_to_cells_and_kzg_proofs<P: Preset>(
blobs: impl Iterator<Item = Blob<P>>,
) -> Result<Vec<([Cell; CELLS_PER_EXT_BLOB], [KzgProof; CELLS_PER_EXT_BLOB])>> {
) -> Result<CellsAndKzgProofs> {
let kzg_settings = kzg_settings();
let cells_and_kzg_proofs = blobs
.map(|blob| {
Expand All @@ -375,51 +379,46 @@ pub fn try_convert_to_cells_and_kzg_proofs<P: Preset>(
})
.collect::<Result<Vec<_>>>()?;

let mut result: Vec<([Cell; CELLS_PER_EXT_BLOB], [KzgProof; CELLS_PER_EXT_BLOB])> = vec![];
for (column_cells, column_proofs) in cells_and_kzg_proofs {
let cells = column_cells
let mut result = vec![];
for (cells, proofs) in cells_and_kzg_proofs {
let cells = cells
.iter()
.map(try_convert_ckzg_cell_to_cell)
.collect::<Result<Vec<Cell>>>()?;
.map(try_convert_to_cell)
.collect::<Result<Vec<_>>>()?;
let column_cells = cells
.try_into()
.expect("column cells should have length of CELLS_PER_EXT_BLOB");

let proofs = column_proofs
let proofs = proofs
.iter()
.map(|proof| KzgProof::from(proof.to_bytes().into_inner()))
.collect::<Vec<KzgProof>>();

let column: [Cell; CELLS_PER_EXT_BLOB] = cells
.try_into()
.expect("cells should not be more than number of columns");
let kzg_proofs: [KzgProof; CELLS_PER_EXT_BLOB] = proofs
.collect::<Vec<_>>();
let column_proofs = proofs
.try_into()
.expect("kzg_proofs should not be more than number of columns");
result.push((column, kzg_proofs));
.expect("column proofs should have length of CELLS_PER_EXT_BLOB");

result.push((column_cells, column_proofs));
}

Ok(result)
}

fn try_convert_ckzg_cell_to_cell(cell: &CKzgCell) -> Result<Cell> {
let bytes = cell.to_bytes();

ContiguousVector::try_from_iter(bytes)
fn try_convert_to_cell(cell: &CKzgCell) -> Result<Cell> {
ContiguousVector::try_from_iter(cell.to_bytes())
.map(ByteVector::from)
.map(Box::new)
.map(Cell::from)
.map_err(Into::into)
}

pub fn construct_cells_and_kzg_proofs(
full_matrix: Vec<MatrixEntry>,
blob_count: usize,
) -> Result<Vec<([Cell; CELLS_PER_EXT_BLOB], [KzgProof; CELLS_PER_EXT_BLOB])>> {
) -> Result<CellsAndKzgProofs> {
let default_cell = Cell::default();
let mut cells_and_kzg_proofs: Vec<(
[Cell; NumberOfColumns::USIZE],
[KzgProof; NumberOfColumns::USIZE],
)> = vec![
let mut cells_and_kzg_proofs = vec![
(
core::array::from_fn(|_| default_cell.clone()),
[KzgProof::repeat_byte(u8::MAX); NumberOfColumns::USIZE],
[KzgProof::zero(); CELLS_PER_EXT_BLOB],
);
blob_count
];
Expand Down
1 change: 0 additions & 1 deletion fork_choice_control/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ tap = { workspace = true }
thiserror = { workspace = true }
transition_functions = { workspace = true }
tynm = { workspace = true }
typenum = { workspace = true }
types = { workspace = true }

[dev-dependencies]
Expand Down
16 changes: 9 additions & 7 deletions fork_choice_control/src/mutator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,10 @@ use num_traits::identities::Zero as _;
use prometheus_metrics::Metrics;
use ssz::SszHash as _;
use std_ext::ArcExt as _;
use typenum::Unsigned as _;
use types::{
combined::{BeaconState, ExecutionPayloadParams, SignedBeaconBlock},
deneb::containers::{BlobIdentifier, BlobSidecar},
fulu::{
consts::NumberOfColumns,
containers::{DataColumnIdentifier, DataColumnSidecar, MatrixEntry},
primitives::ColumnIndex,
},
Expand Down Expand Up @@ -555,7 +553,8 @@ where
let missing_column_indices =
self.store.indices_of_missing_data_columns(&parent.block);

if missing_column_indices.len() * 2 < NumberOfColumns::USIZE
let number_of_columns = self.store.chain_config().number_of_columns();
if missing_column_indices.len() * 2 < number_of_columns
|| !self.store.is_forward_synced()
{
let body = parent
Expand All @@ -572,7 +571,7 @@ where

// TODO(feature/fulu): random delay reconstruction as stated in the [specs]
// (https://github.com/ethereum/consensus-specs/blob/8696fbf75387fb37a32fc08a6b934653198c6c0c/specs/fulu/das-core.md?plain=1#L257)
if available_columns.len() > NumberOfColumns::USIZE / 2
if available_columns.len() > number_of_columns / 2
&& !self
.store
.has_reconstructed_data_column_sidecars(parent.block_root)
Expand Down Expand Up @@ -1456,15 +1455,18 @@ where
blob_count: usize,
full_matrix: Vec<MatrixEntry>,
) -> Result<()> {
let config = self.store.chain_config();
let chain_link = self.store.chain_link(block_root).expect(
"block must be available in the store during data column sidecars reconstruction",
);

let cells_and_kzg_proofs =
eip_7594::construct_cells_and_kzg_proofs(full_matrix, blob_count)?;
for data_column_sidecar in
eip_7594::construct_data_column_sidecars(&chain_link.block, &cells_and_kzg_proofs)?
{
for data_column_sidecar in eip_7594::construct_data_column_sidecars(
&chain_link.block,
&cells_and_kzg_proofs,
config,
)? {
let data_column_sidecar = Arc::new(data_column_sidecar);
let data_column_identifier: DataColumnIdentifier = data_column_sidecar.as_ref().into();

Expand Down
8 changes: 3 additions & 5 deletions fork_choice_control/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ use fork_choice_store::{
use helper_functions::misc;
use itertools::Itertools as _;
use serde::Serialize;
use ssz::ContiguousList;
use std_ext::ArcExt;
use thiserror::Error;
use types::{
combined::{BeaconState, SignedAggregateAndProof, SignedBeaconBlock},
deneb::containers::{BlobIdentifier, BlobSidecar},
fulu::{
consts::NumberOfColumns,
containers::{DataColumnIdentifier, DataColumnSidecar},
primitives::ColumnIndex,
},
Expand Down Expand Up @@ -509,7 +507,7 @@ where
pub fn data_column_sidecars_by_range(
&self,
range: Range<Slot>,
columns: &ContiguousList<ColumnIndex, NumberOfColumns>,
columns: &[ColumnIndex],
max_request_data_column_sidecars: usize,
) -> Result<Vec<Arc<DataColumnSidecar<P>>>> {
let canonical_chain_blocks = self.blocks_by_range(range)?;
Expand All @@ -518,8 +516,8 @@ where
.iter()
.filter_map(|BlockWithRoot { block, root }| {
block.message().body().post_deneb().map(|_| {
columns.iter().map(|index| DataColumnIdentifier {
index: *index,
columns.iter().copied().map(|index| DataColumnIdentifier {
index,
block_root: *root,
})
})
Expand Down
37 changes: 16 additions & 21 deletions fork_choice_control/src/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ use helper_functions::{
};
use log::{debug, warn};
use prometheus_metrics::Metrics;
use typenum::Unsigned as _;
use types::{
combined::{AttesterSlashing, SignedAggregateAndProof, SignedBeaconBlock},
deneb::containers::BlobSidecar,
fulu::{consts::NumberOfColumns, containers::DataColumnSidecar},
fulu::containers::DataColumnSidecar,
nonstandard::{RelativeEpoch, ValidationOutcome},
phase0::{
containers::Checkpoint,
Expand Down Expand Up @@ -509,27 +508,23 @@ impl<P: Preset, W> Run for ReconstructDataColumnSidecarsTask<P, W> {

let available_columns = store_snapshot.available_columns_at_block(block_root);

if available_columns.len() < NumberOfColumns::USIZE {
let partial_matrix = available_columns
.into_iter()
.flat_map(|sidecar| misc::compute_matrix_for_data_column_sidecar(&sidecar))
.collect::<Vec<_>>();

match eip_7594::recover_matrix(&partial_matrix, blob_count) {
Ok(full_matrix) => {
MutatorMessage::ReconstructedMissingColumns {
block_root,
blob_count,
full_matrix,
}
.send(&mutator_tx);
}
Err(error) => {
warn!("failed to reconstruct missing data column sidecars: {error:?}");
let partial_matrix = available_columns
.into_iter()
.flat_map(|sidecar| misc::compute_matrix_for_data_column_sidecar(&sidecar))
.collect::<Vec<_>>();

match eip_7594::recover_matrix(&partial_matrix, blob_count) {
Ok(full_matrix) => {
MutatorMessage::ReconstructedMissingColumns {
block_root,
blob_count,
full_matrix,
}
.send(&mutator_tx);
}
Err(error) => {
warn!("failed to reconstruct missing data column sidecars: {error:?}");
}
} else {
debug!("all required sample columns are available, dismiss recontruction");
}
}
}
Expand Down
1 change: 0 additions & 1 deletion fork_choice_store/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,5 @@ tap = { workspace = true }
thiserror = { workspace = true }
transition_functions = { workspace = true }
tynm = { workspace = true }
typenum = { workspace = true }
types = { workspace = true }
unwrap_none = { workspace = true }
Loading

0 comments on commit be575dc

Please sign in to comment.