Remove expect usage in kzg_utils (#7957)

Remove `expect` usage in `kzg_utils` to handle the case where EL sends us invalid proof size instead of crashing.
This commit is contained in:
Jimmy Chen
2025-09-01 19:21:26 +10:00
committed by GitHub
parent 9cc3c0553b
commit 979ed2557c

View File

@@ -1,6 +1,6 @@
use kzg::{ use kzg::{
Blob as KzgBlob, Bytes48, Cell as KzgCell, CellRef as KzgCellRef, CellsAndKzgProofs, Blob as KzgBlob, Bytes48, Cell as KzgCell, CellRef as KzgCellRef, CellsAndKzgProofs,
Error as KzgError, Kzg, Error as KzgError, Kzg, KzgBlobRef,
}; };
use rayon::prelude::*; use rayon::prelude::*;
use ssz_types::{FixedVector, VariableList}; use ssz_types::{FixedVector, VariableList};
@@ -28,9 +28,9 @@ fn ssz_blob_to_crypto_blob_boxed<E: EthSpec>(blob: &Blob<E>) -> Result<Box<KzgBl
/// crypto library. /// crypto library.
fn ssz_cell_to_crypto_cell<E: EthSpec>(cell: &Cell<E>) -> Result<KzgCellRef<'_>, KzgError> { fn ssz_cell_to_crypto_cell<E: EthSpec>(cell: &Cell<E>) -> Result<KzgCellRef<'_>, KzgError> {
let cell_bytes: &[u8] = cell.as_ref(); let cell_bytes: &[u8] = cell.as_ref();
Ok(cell_bytes cell_bytes
.try_into() .try_into()
.expect("expected cell to have size {BYTES_PER_CELL}. This should be guaranteed by the `FixedVector type")) .map_err(|e| KzgError::InconsistentArrayLength(format!("expected cell to have size BYTES_PER_CELL. This should be guaranteed by the `FixedVector` type: {e:?}")))
} }
/// Validate a single blob-commitment-proof triplet from a `BlobSidecar`. /// Validate a single blob-commitment-proof triplet from a `BlobSidecar`.
@@ -183,18 +183,19 @@ pub fn blobs_to_data_column_sidecars<E: EthSpec>(
let blob_cells_and_proofs_vec = zipped let blob_cells_and_proofs_vec = zipped
.into_par_iter() .into_par_iter()
.map(|(blob, proofs)| { .map(|(blob, proofs)| {
let blob = blob let blob = blob.as_ref().try_into().map_err(|e| {
.as_ref() KzgError::InconsistentArrayLength(format!(
.try_into() "blob should have a guaranteed size due to FixedVector: {e:?}"
.expect("blob should have a guaranteed size due to FixedVector"); ))
})?;
kzg.compute_cells(blob).map(|cells| { kzg.compute_cells(blob).and_then(|cells| {
( let proofs = proofs.try_into().map_err(|e| {
cells, KzgError::InconsistentArrayLength(format!(
proofs "proof chunks should have exactly `number_of_columns` proofs: {e:?}"
.try_into() ))
.expect("proof chunks should have exactly `number_of_columns` proofs"), })?;
) Ok((cells, proofs))
}) })
}) })
.collect::<Result<Vec<_>, KzgError>>()?; .collect::<Result<Vec<_>, KzgError>>()?;
@@ -213,10 +214,11 @@ pub fn compute_cells<E: EthSpec>(blobs: &[&Blob<E>], kzg: &Kzg) -> Result<Vec<Kz
let cells_vec = blobs let cells_vec = blobs
.into_par_iter() .into_par_iter()
.map(|blob| { .map(|blob| {
let blob = blob let blob: KzgBlobRef<'_> = blob.as_ref().try_into().map_err(|e| {
.as_ref() KzgError::InconsistentArrayLength(format!(
.try_into() "blob should have a guaranteed size due to FixedVector: {e:?}",
.expect("blob should have a guaranteed size due to FixedVector"); ))
})?;
kzg.compute_cells(blob) kzg.compute_cells(blob)
}) })