Skip to content

Commit

Permalink
Avoid building and publishing blob sidecars after PeerDAS.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmygchen committed May 10, 2024
1 parent fe9e5dd commit 5f09cfd
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 56 deletions.
65 changes: 36 additions & 29 deletions beacon_node/beacon_chain/src/block_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ use task_executor::JoinHandle;
use tree_hash::TreeHash;
use types::data_column_sidecar::DataColumnSidecarError;
use types::{
BeaconBlockRef, BeaconState, BeaconStateError, BlobSidecarList, ChainSpec, DataColumnSidecar,
BeaconBlockRef, BeaconState, BeaconStateError, BlobsList, ChainSpec, DataColumnSidecar,
DataColumnSubnetId, Epoch, EthSpec, ExecutionBlockHash, FullPayload, Hash256, InconsistentFork,
PublicKey, PublicKeyBytes, RelativeEpoch, SignedBeaconBlock, SignedBeaconBlockHeader, Slot,
KzgProofs, PublicKey, PublicKeyBytes, RelativeEpoch, SignedBeaconBlock,
SignedBeaconBlockHeader, Slot,
};
use types::{BlobSidecar, ExecPayload};

Expand Down Expand Up @@ -718,35 +719,20 @@ impl<T: BeaconChainTypes> IntoGossipVerifiedBlockContents<T> for PublishBlockReq
) -> Result<GossipVerifiedBlockContents<T>, BlockContentsError<T::EthSpec>> {
let (block, blobs) = self.deconstruct();

let gossip_verified_blobs = blobs
.map(|(kzg_proofs, blobs)| {
let mut gossip_verified_blobs = vec![];
for (i, (kzg_proof, blob)) in kzg_proofs.iter().zip(blobs).enumerate() {
let _timer =
metrics::start_timer(&metrics::BLOB_SIDECAR_INCLUSION_PROOF_COMPUTATION);
let blob = BlobSidecar::new(i, blob, &block, *kzg_proof)
.map_err(BlockContentsError::BlobSidecarError)?;
drop(_timer);
let gossip_verified_blob =
GossipVerifiedBlob::new(Arc::new(blob), i as u64, chain)?;
gossip_verified_blobs.push(gossip_verified_blob);
}
let gossip_verified_blobs = VariableList::from(gossip_verified_blobs);
Ok::<_, BlockContentsError<T::EthSpec>>(gossip_verified_blobs)
})
.transpose()?;

let peer_das_enabled = chain
.spec
.eip7594_fork_epoch
.map_or(false, |eip7594_fork_epoch| {
block.epoch() >= eip7594_fork_epoch
});

let gossip_verified_data_columns = if peer_das_enabled {
build_gossip_verified_data_columns(chain, &block, gossip_verified_blobs.as_ref())?
let (gossip_verified_blobs, gossip_verified_data_columns) = if peer_das_enabled {
let gossip_verified_data_columns =
build_gossip_verified_data_columns(chain, &block, blobs.map(|(_, blobs)| blobs))?;
(None, gossip_verified_data_columns)
} else {
None
let gossip_verified_blobs = build_gossip_verified_blobs(chain, &block, blobs)?;
(gossip_verified_blobs, None)
};

let gossip_verified_block = GossipVerifiedBlock::new(block, chain)?;
Expand All @@ -763,12 +749,36 @@ impl<T: BeaconChainTypes> IntoGossipVerifiedBlockContents<T> for PublishBlockReq
}
}

fn build_gossip_verified_blobs<T: BeaconChainTypes>(
chain: &BeaconChain<T>,
block: &Arc<SignedBeaconBlock<T::EthSpec, FullPayload<T::EthSpec>>>,
blobs: Option<(KzgProofs<T::EthSpec>, BlobsList<T::EthSpec>)>,
) -> Result<Option<GossipVerifiedBlobList<T>>, BlockContentsError<T::EthSpec>> {
blobs
.map(|(kzg_proofs, blobs)| {
let mut gossip_verified_blobs = vec![];
for (i, (kzg_proof, blob)) in kzg_proofs.iter().zip(blobs).enumerate() {
let _timer =
metrics::start_timer(&metrics::BLOB_SIDECAR_INCLUSION_PROOF_COMPUTATION);
let blob = BlobSidecar::new(i, blob, &block, *kzg_proof)
.map_err(BlockContentsError::BlobSidecarError)?;
drop(_timer);
let gossip_verified_blob =
GossipVerifiedBlob::new(Arc::new(blob), i as u64, chain)?;
gossip_verified_blobs.push(gossip_verified_blob);
}
let gossip_verified_blobs = VariableList::from(gossip_verified_blobs);
Ok::<_, BlockContentsError<T::EthSpec>>(gossip_verified_blobs)
})
.transpose()
}

fn build_gossip_verified_data_columns<T: BeaconChainTypes>(
chain: &BeaconChain<T>,
block: &SignedBeaconBlock<T::EthSpec, FullPayload<T::EthSpec>>,
gossip_verified_blobs: Option<&GossipVerifiedBlobList<T>>,
blobs: Option<BlobsList<T::EthSpec>>,
) -> Result<Option<GossipVerifiedDataColumnList<T>>, BlockContentsError<T::EthSpec>> {
gossip_verified_blobs
blobs
// Only attempt to build data columns if blobs is non empty to avoid skewing the metrics.
.filter(|b| !b.is_empty())
.map(|blobs| {
Expand All @@ -780,11 +790,8 @@ fn build_gossip_verified_data_columns<T: BeaconChainTypes>(
GossipDataColumnError::<T::EthSpec>::KzgNotInitialized,
))?;

let blob_sidecar_list: Vec<_> = blobs.iter().map(|blob| blob.clone_blob()).collect();
let blob_sidecar_list = BlobSidecarList::new(blob_sidecar_list)
.map_err(DataColumnSidecarError::SszError)?;
let timer = metrics::start_timer(&metrics::DATA_COLUMN_SIDECAR_COMPUTATION);
let sidecars = DataColumnSidecar::build_sidecars(&blob_sidecar_list, block, kzg)?;
let sidecars = DataColumnSidecar::build_sidecars(&blobs, block, kzg)?;
drop(timer);
let mut gossip_verified_data_columns = vec![];
for sidecar in sidecars {
Expand Down
4 changes: 4 additions & 0 deletions beacon_node/http_api/src/publish_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ pub async fn publish_block<T: BeaconChainTypes, B: IntoGossipVerifiedBlockConten
.collect::<Vec<_>>();
VariableList::from(blobs)
});
// TODO(das): We could potentially get rid of these conversions and pass `GossipVerified` types
// to `publish_block`, i.e. have `GossipVerified` types in `PubsubMessage`?
// This saves us from extra code and provides guarantee that published
// components are verified.
let data_cols_opt = gossip_verified_data_columns
.as_ref()
.map(|gossip_verified_data_columns| {
Expand Down
43 changes: 16 additions & 27 deletions consensus/types/src/data_column_sidecar.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::beacon_block_body::KzgCommitments;
use crate::test_utils::TestRandom;
use crate::BeaconStateError;
use crate::{
BeaconBlockHeader, BlobSidecarList, EthSpec, Hash256, KzgProofs, SignedBeaconBlock,
SignedBeaconBlockHeader, Slot,
BeaconBlockHeader, EthSpec, Hash256, KzgProofs, SignedBeaconBlock, SignedBeaconBlockHeader,
Slot,
};
use crate::{BeaconStateError, BlobsList};
use bls::Signature;
use derivative::Derivative;
#[cfg_attr(test, double)]
Expand Down Expand Up @@ -83,7 +83,7 @@ impl<E: EthSpec> DataColumnSidecar<E> {
}

pub fn build_sidecars(
blobs: &BlobSidecarList<E>,
blobs: &BlobsList<E>,
block: &SignedBeaconBlock<E>,
kzg: &Kzg,
) -> Result<DataColumnSidecarList<E>, DataColumnSidecarError> {
Expand All @@ -106,7 +106,7 @@ impl<E: EthSpec> DataColumnSidecar<E> {

// NOTE: assumes blob sidecars are ordered by index
for blob in blobs {
let blob = KzgBlob::from_bytes(&blob.blob).map_err(KzgError::from)?;
let blob = KzgBlob::from_bytes(blob).map_err(KzgError::from)?;
let (blob_cells, blob_cell_proofs) = kzg.compute_cells_and_proofs(&blob)?;

// we iterate over each column, and we construct the column from "top to bottom",
Expand Down Expand Up @@ -279,20 +279,19 @@ mod test {
use crate::beacon_block_body::KzgCommitments;
use crate::eth_spec::EthSpec;
use crate::{
BeaconBlock, BeaconBlockDeneb, Blob, BlobSidecar, BlobSidecarList, ChainSpec,
DataColumnSidecar, MainnetEthSpec, SignedBeaconBlock,
BeaconBlock, BeaconBlockDeneb, Blob, ChainSpec, DataColumnSidecar, MainnetEthSpec,
SignedBeaconBlock,
};
use bls::Signature;
use kzg::{KzgCommitment, KzgProof};
use kzg::KzgCommitment;
use std::sync::Arc;

#[test]
fn test_build_sidecars_empty() {
type E = MainnetEthSpec;
let num_of_blobs = 0;
let spec = E::default_spec();
let (signed_block, blob_sidecars) =
create_test_block_and_blob_sidecars::<E>(num_of_blobs, &spec);
let (signed_block, blob_sidecars) = create_test_block_and_blobs::<E>(num_of_blobs, &spec);

let mock_kzg = Arc::new(Kzg::default());
let column_sidecars =
Expand All @@ -306,8 +305,7 @@ mod test {
type E = MainnetEthSpec;
let num_of_blobs = 6;
let spec = E::default_spec();
let (signed_block, blob_sidecars) =
create_test_block_and_blob_sidecars::<E>(num_of_blobs, &spec);
let (signed_block, blob_sidecars) = create_test_block_and_blobs::<E>(num_of_blobs, &spec);

let mut mock_kzg = Kzg::default();
mock_kzg
Expand Down Expand Up @@ -345,10 +343,10 @@ mod test {
}
}

fn create_test_block_and_blob_sidecars<E: EthSpec>(
fn create_test_block_and_blobs<E: EthSpec>(
num_of_blobs: usize,
spec: &ChainSpec,
) -> (SignedBeaconBlock<E>, BlobSidecarList<E>) {
) -> (SignedBeaconBlock<E>, BlobsList<E>) {
let mut block = BeaconBlock::Deneb(BeaconBlockDeneb::empty(spec));
let mut body = block.body_mut();
let blob_kzg_commitments = body.blob_kzg_commitments_mut().unwrap();
Expand All @@ -358,20 +356,11 @@ mod test {

let signed_block = SignedBeaconBlock::from_block(block, Signature::empty());

let sidecars = (0..num_of_blobs)
.map(|index| {
BlobSidecar::new(
index,
Blob::<E>::default(),
&signed_block,
KzgProof::empty(),
)
.map(Arc::new)
})
.collect::<Result<Vec<_>, _>>()
.unwrap()
let blobs = (0..num_of_blobs)
.map(|_| Blob::<E>::default())
.collect::<Vec<_>>()
.into();

(signed_block, sidecars)
(signed_block, blobs)
}
}

0 comments on commit 5f09cfd

Please sign in to comment.