Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use data column batch verification consistently #6851

Merged
merged 2 commits into from
Feb 3, 2025
Merged
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
57 changes: 14 additions & 43 deletions beacon_node/beacon_chain/src/data_availability_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ mod overflow_lru_cache;
mod state_lru_cache;

use crate::data_column_verification::{
verify_kzg_for_data_column, verify_kzg_for_data_column_list, CustodyDataColumn,
GossipVerifiedDataColumn, KzgVerifiedCustodyDataColumn, KzgVerifiedDataColumn,
verify_kzg_for_data_column_list_with_scoring, CustodyDataColumn, GossipVerifiedDataColumn,
KzgVerifiedCustodyDataColumn, KzgVerifiedDataColumn,
};
use crate::metrics::{
KZG_DATA_COLUMN_RECONSTRUCTION_ATTEMPTS, KZG_DATA_COLUMN_RECONSTRUCTION_FAILURES,
Expand Down Expand Up @@ -230,19 +230,14 @@ impl<T: BeaconChainTypes> DataAvailabilityChecker<T> {
block_root: Hash256,
custody_columns: DataColumnSidecarList<T::EthSpec>,
) -> Result<Availability<T::EthSpec>, AvailabilityCheckError> {
// TODO(das): report which column is invalid for proper peer scoring
// TODO(das): batch KZG verification here, but fallback into checking each column
// individually to report which column(s) are invalid.
let verified_custody_columns = custody_columns
// Attributes fault to the specific peer that sent an invalid column
let kzg_verified_columns = KzgVerifiedDataColumn::from_batch(custody_columns, &self.kzg)
.map_err(|(index, e)| AvailabilityCheckError::InvalidColumn(index, e))?;

let verified_custody_columns = kzg_verified_columns
.into_iter()
.map(|column| {
let index = column.index;
Ok(KzgVerifiedCustodyDataColumn::from_asserted_custody(
KzgVerifiedDataColumn::new(column, &self.kzg)
.map_err(|e| AvailabilityCheckError::InvalidColumn(index, e))?,
))
})
.collect::<Result<Vec<_>, AvailabilityCheckError>>()?;
.map(KzgVerifiedCustodyDataColumn::from_asserted_custody)
.collect::<Vec<_>>();

self.availability_cache.put_kzg_verified_data_columns(
block_root,
Expand Down Expand Up @@ -365,7 +360,8 @@ impl<T: BeaconChainTypes> DataAvailabilityChecker<T> {
.iter()
.map(|custody_column| custody_column.as_data_column()),
&self.kzg,
)?;
)
.map_err(|(index, e)| AvailabilityCheckError::InvalidColumn(index, e))?;
Ok(MaybeAvailableBlock::Available(AvailableBlock {
block_root,
block,
Expand Down Expand Up @@ -432,8 +428,9 @@ impl<T: BeaconChainTypes> DataAvailabilityChecker<T> {

// verify kzg for all data columns at once
if !all_data_columns.is_empty() {
// TODO: Need to also attribute which specific block is faulty
verify_kzg_for_data_column_list_with_scoring(all_data_columns.iter(), &self.kzg)?;
// Attributes fault to the specific peer that sent an invalid column
verify_kzg_for_data_column_list_with_scoring(all_data_columns.iter(), &self.kzg)
.map_err(|(index, e)| AvailabilityCheckError::InvalidColumn(index, e))?;
}

for block in blocks {
Expand Down Expand Up @@ -716,32 +713,6 @@ async fn availability_cache_maintenance_service<T: BeaconChainTypes>(
}
}

fn verify_kzg_for_data_column_list_with_scoring<'a, E: EthSpec, I>(
data_column_iter: I,
kzg: &'a Kzg,
) -> Result<(), AvailabilityCheckError>
where
I: Iterator<Item = &'a Arc<DataColumnSidecar<E>>> + Clone,
{
let Err(batch_err) = verify_kzg_for_data_column_list(data_column_iter.clone(), kzg) else {
return Ok(());
};

let data_columns = data_column_iter.collect::<Vec<_>>();
// Find which column is invalid. If len is 1 or 0 continue to default case below.
// If len > 1 at least one column MUST fail.
if data_columns.len() > 1 {
for data_column in data_columns {
if let Err(e) = verify_kzg_for_data_column(data_column.clone(), kzg) {
return Err(AvailabilityCheckError::InvalidColumn(data_column.index, e));
}
}
}

// len 0 should never happen
Err(AvailabilityCheckError::InvalidColumn(0, batch_err))
}

/// A fully available block that is ready to be imported into fork choice.
#[derive(Clone, Debug, PartialEq)]
pub struct AvailableBlock<E: EthSpec> {
Expand Down
44 changes: 44 additions & 0 deletions beacon_node/beacon_chain/src/data_column_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,18 @@ impl<E: EthSpec> KzgVerifiedDataColumn<E> {
pub fn new(data_column: Arc<DataColumnSidecar<E>>, kzg: &Kzg) -> Result<Self, KzgError> {
verify_kzg_for_data_column(data_column, kzg)
}

pub fn from_batch(
data_columns: Vec<Arc<DataColumnSidecar<E>>>,
kzg: &Kzg,
) -> Result<Vec<Self>, (u64, KzgError)> {
jimmygchen marked this conversation as resolved.
Show resolved Hide resolved
verify_kzg_for_data_column_list_with_scoring(data_columns.iter(), kzg)?;
Ok(data_columns
.into_iter()
.map(|column| Self { data: column })
.collect())
}

pub fn to_data_column(self) -> Arc<DataColumnSidecar<E>> {
self.data
}
Expand Down Expand Up @@ -378,6 +390,38 @@ where
Ok(())
}

/// Complete kzg verification for a list of `DataColumnSidecar`s.
///
/// If there's at least one invalid column, it re-verifies all columns individually to identify the
/// first column that is invalid. This is necessary to attribute fault to the specific peer that
/// sent bad data. The re-verification cost should not be significant. If a peer sends invalid data it
/// will be quickly banned.
pub fn verify_kzg_for_data_column_list_with_scoring<'a, E: EthSpec, I>(
data_column_iter: I,
kzg: &'a Kzg,
) -> Result<(), (u64, KzgError)>
jimmygchen marked this conversation as resolved.
Show resolved Hide resolved
where
I: Iterator<Item = &'a Arc<DataColumnSidecar<E>>> + Clone,
{
let Err(batch_err) = verify_kzg_for_data_column_list(data_column_iter.clone(), kzg) else {
return Ok(());
};

let data_columns = data_column_iter.collect::<Vec<_>>();
// Find which column is invalid. If len is 1 or 0 continue to default case below.
// If len > 1 at least one column MUST fail.
if data_columns.len() > 1 {
for data_column in data_columns {
if let Err(e) = verify_kzg_for_data_column(data_column.clone(), kzg) {
return Err((data_column.index, e));
}
}
}

// len 0 should never happen
Err((0, batch_err))
}

pub fn validate_data_column_sidecar_for_gossip<T: BeaconChainTypes, O: ObservationStrategy>(
data_column: Arc<DataColumnSidecar<T::EthSpec>>,
subnet: u64,
Expand Down
Loading