pass in data availability boundary to the get_blobs method

This commit is contained in:
realbigsean 2023-01-24 14:35:07 +01:00
parent a4ea1761bb
commit 2225e6ac89
2 changed files with 67 additions and 87 deletions

View File

@ -963,18 +963,28 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
)>,
Error,
> {
// If there is no data availability boundary, the Eip4844 fork is disabled.
if let Some(finalized_data_availability_boundary) =
self.finalized_data_availability_boundary()
{
// Only use the attester cache if we can find both the block and blob
if let (Some(block), Some(blobs)) = (
self.early_attester_cache.get_block(*block_root),
self.early_attester_cache.get_blobs(*block_root),
) {
return Ok(Some((block, blobs)));
}
if let Some(block) = self.get_block(block_root).await?.map(Arc::new) {
let blobs = self.get_blobs(block_root)?.map(Arc::new);
Ok(Some((block, blobs)))
// Attempt to get the block and blobs from the database
} else if let Some(block) = self.get_block(block_root).await?.map(Arc::new) {
let blobs = self
.get_blobs(block_root, finalized_data_availability_boundary)?
.map(Arc::new);
Ok(blobs.map(|blobs| (block, blobs)))
} else {
Ok(None)
}
} else {
Ok(None)
}
}
/// Returns the block at the given root, if any.
@ -1060,6 +1070,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
pub fn get_blobs(
&self,
block_root: &Hash256,
data_availability_boundary: Epoch,
) -> Result<Option<BlobsSidecar<T::EthSpec>>, Error> {
match self.store.get_blobs(block_root)? {
Some(blobs) => Ok(Some(blobs)),
@ -1076,13 +1087,11 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
};
if expected_kzg_commitments.is_empty() {
Ok(BlobsSidecar::empty_from_parts(*block_root, block.slot()))
} else {
if let Some(boundary) = self.data_availability_boundary() {
} else if data_availability_boundary <= block.epoch() {
// We should have blobs for all blocks younger than the boundary.
if boundary <= block.epoch() {
return Err(Error::BlobsUnavailable);
}
}
Err(Error::BlobsUnavailable)
} else {
// We shouldn't have blobs for blocks older than the boundary.
Err(Error::BlobsOlderThanDataAvailabilityBoundary(block.epoch()))
}
})

View File

@ -267,9 +267,9 @@ impl<T: BeaconChainTypes> Worker<T> {
break;
}
Err(BeaconChainError::NoKzgCommitmentsFieldOnBlock) => {
error!(
debug!(
self.log,
"No kzg_commitments field in block";
"Peer requested blobs for a pre-eip4844 block";
"peer" => %peer_id,
"block_root" => ?root,
);
@ -283,18 +283,13 @@ impl<T: BeaconChainTypes> Worker<T> {
break;
}
Err(BeaconChainError::BlobsOlderThanDataAvailabilityBoundary(block_epoch)) => {
let finalized_data_availability_boundary = self.chain.finalized_data_availability_boundary();
match finalized_data_availability_boundary {
Some(boundary_epoch) => {
if block_epoch >= boundary_epoch {
error!(
debug!(
self.log,
"Peer requested block and blob that should be available, but no blob found";
"request" => %request,
"Peer requested block and blobs older than the data availability \
boundary for ByRoot request, no blob found";
"peer" => %peer_id,
"request_root" => ?root,
"finalized_data_availability_boundary" => %boundary_epoch,
"block_epoch" => ?block_epoch,
);
self.send_error_response(
peer_id,
@ -304,28 +299,6 @@ impl<T: BeaconChainTypes> Worker<T> {
);
send_response = false;
break;
} else {
debug!(
self.log,
"Peer requested block and blob older than the data availability \
boundary for ByRoot request, no blob found";
"peer" => %peer_id,
"request_root" => ?root,
"finalized_data_availability_boundary" => ?finalized_data_availability_boundary,
);
}
}
None => {
debug!(self.log, "Eip4844 fork is disabled");
self.send_error_response(
peer_id,
RPCResponseErrorCode::ResourceUnavailable,
"Eip4844 fork is disabled".into(),
request_id,
);
return;
}
}
}
Err(BeaconChainError::BlockHashMissingFromExecutionLayer(_)) => {
debug!(
@ -670,11 +643,21 @@ impl<T: BeaconChainTypes> Worker<T> {
let start_slot = Slot::from(req.start_slot);
let start_epoch = start_slot.epoch(T::EthSpec::slots_per_epoch());
let data_availability_boundary = self.chain.data_availability_boundary();
let data_availability_boundary = match self.chain.data_availability_boundary() {
Some(boundary) => boundary,
None => {
debug!(self.log, "Eip4844 fork is disabled");
self.send_error_response(
peer_id,
RPCResponseErrorCode::ServerError,
"Eip4844 fork is disabled".into(),
request_id,
);
return;
}
};
let serve_blobs_from_slot = match data_availability_boundary {
Some(data_availability_boundary_epoch) => {
if Some(start_epoch) < data_availability_boundary {
let serve_blobs_from_slot = if start_epoch < data_availability_boundary {
let oldest_blob_slot = self
.chain
.store
@ -689,21 +672,9 @@ impl<T: BeaconChainTypes> Worker<T> {
"data_availability_boundary" => data_availability_boundary
);
data_availability_boundary_epoch.start_slot(T::EthSpec::slots_per_epoch())
data_availability_boundary.start_slot(T::EthSpec::slots_per_epoch())
} else {
start_slot
}
}
None => {
debug!(self.log, "Eip4844 fork is disabled");
self.send_error_response(
peer_id,
RPCResponseErrorCode::ResourceUnavailable,
"Eip4844 fork is disabled".into(),
request_id,
);
return;
}
};
// Should not send more than max request blocks
@ -785,7 +756,7 @@ impl<T: BeaconChainTypes> Worker<T> {
let mut send_response = true;
for root in block_roots {
match self.chain.get_blobs(&root) {
match self.chain.get_blobs(&root, data_availability_boundary) {
Ok(Some(blobs)) => {
blobs_sent += 1;
self.send_network_message(NetworkMessage::SendResponse {