add missing fields to get blob sidecars request (#5987)

* add missing fields to get blob sidecars request

* add fork versioned  response impl

* only compute the block root once

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into add-missing-fields-get-blob-sidecars

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into add-missing-fields-get-blob-sidecars

* fetch root first

fetch from cache if its a head block

* fmt

* always load from db
This commit is contained in:
Eitan Seri-Levi
2024-08-19 01:28:45 -07:00
committed by GitHub
parent b6d15bc299
commit 042915859d
4 changed files with 89 additions and 51 deletions

View File

@@ -123,6 +123,15 @@ impl BlockId {
}
}
pub fn blinded_block_by_root<T: BeaconChainTypes>(
root: &Hash256,
chain: &BeaconChain<T>,
) -> Result<Option<SignedBlindedBeaconBlock<T::EthSpec>>, warp::Rejection> {
chain
.get_blinded_block(root)
.map_err(warp_utils::reject::beacon_chain_error)
}
/// Return the `SignedBeaconBlock` identified by `self`.
pub fn blinded_block<T: BeaconChainTypes>(
&self,
@@ -149,38 +158,32 @@ impl BlockId {
}
CoreBlockId::Slot(slot) => {
let (root, execution_optimistic, finalized) = self.root(chain)?;
chain
.get_blinded_block(&root)
.map_err(warp_utils::reject::beacon_chain_error)
.and_then(|block_opt| match block_opt {
Some(block) => {
if block.slot() != *slot {
return Err(warp_utils::reject::custom_not_found(format!(
"slot {} was skipped",
slot
)));
}
Ok((block, execution_optimistic, finalized))
BlockId::blinded_block_by_root(&root, chain).and_then(|block_opt| match block_opt {
Some(block) => {
if block.slot() != *slot {
return Err(warp_utils::reject::custom_not_found(format!(
"slot {} was skipped",
slot
)));
}
None => Err(warp_utils::reject::custom_not_found(format!(
"beacon block with root {}",
root
))),
})
Ok((block, execution_optimistic, finalized))
}
None => Err(warp_utils::reject::custom_not_found(format!(
"beacon block with root {}",
root
))),
})
}
_ => {
let (root, execution_optimistic, finalized) = self.root(chain)?;
let block = chain
.get_blinded_block(&root)
.map_err(warp_utils::reject::beacon_chain_error)
.and_then(|root_opt| {
root_opt.ok_or_else(|| {
warp_utils::reject::custom_not_found(format!(
"beacon block with root {}",
root
))
})
})?;
let block = BlockId::blinded_block_by_root(&root, chain).and_then(|root_opt| {
root_opt.ok_or_else(|| {
warp_utils::reject::custom_not_found(format!(
"beacon block with root {}",
root
))
})
})?;
Ok((block, execution_optimistic, finalized))
}
}
@@ -252,23 +255,30 @@ impl BlockId {
}
}
/// Return the `BlobSidecarList` identified by `self`.
pub fn blob_sidecar_list<T: BeaconChainTypes>(
&self,
chain: &BeaconChain<T>,
) -> Result<BlobSidecarList<T::EthSpec>, warp::Rejection> {
let root = self.root(chain)?.0;
chain
.get_blobs(&root)
.map_err(warp_utils::reject::beacon_chain_error)
}
pub fn blob_sidecar_list_filtered<T: BeaconChainTypes>(
#[allow(clippy::type_complexity)]
pub fn get_blinded_block_and_blob_list_filtered<T: BeaconChainTypes>(
&self,
indices: BlobIndicesQuery,
chain: &BeaconChain<T>,
) -> Result<BlobSidecarList<T::EthSpec>, warp::Rejection> {
let blob_sidecar_list = self.blob_sidecar_list(chain)?;
) -> Result<
(
SignedBlindedBeaconBlock<T::EthSpec>,
BlobSidecarList<T::EthSpec>,
ExecutionOptimistic,
Finalized,
),
warp::Rejection,
> {
let (root, execution_optimistic, finalized) = self.root(chain)?;
let block = BlockId::blinded_block_by_root(&root, chain)?.ok_or_else(|| {
warp_utils::reject::custom_not_found(format!("beacon block with root {}", root))
})?;
// Return the `BlobSidecarList` identified by `self`.
let blob_sidecar_list = chain
.get_blobs(&root)
.map_err(warp_utils::reject::beacon_chain_error)?;
let blob_sidecar_list_filtered = match indices.indices {
Some(vec) => {
let list = blob_sidecar_list
@@ -280,7 +290,12 @@ impl BlockId {
}
None => blob_sidecar_list,
};
Ok(blob_sidecar_list_filtered)
Ok((
block,
blob_sidecar_list_filtered,
execution_optimistic,
finalized,
))
}
}

View File

@@ -1736,8 +1736,12 @@ pub fn serve<T: BeaconChainTypes>(
accept_header: Option<api_types::Accept>| {
task_spawner.blocking_response_task(Priority::P1, move || {
let indices = indices_res?;
let blob_sidecar_list_filtered =
block_id.blob_sidecar_list_filtered(indices, &chain)?;
let (block, blob_sidecar_list_filtered, execution_optimistic, finalized) =
block_id.get_blinded_block_and_blob_list_filtered(indices, &chain)?;
let fork_name = block
.fork_name(&chain.spec)
.map_err(inconsistent_fork_rejection)?;
match accept_header {
Some(api_types::Accept::Ssz) => Response::builder()
.status(200)
@@ -1749,11 +1753,19 @@ pub fn serve<T: BeaconChainTypes>(
e
))
}),
_ => Ok(warp::reply::json(&api_types::GenericResponse::from(
blob_sidecar_list_filtered,
))
.into_response()),
_ => {
// Post as a V2 endpoint so we return the fork version.
let res = execution_optimistic_finalized_fork_versioned_response(
V2,
fork_name,
execution_optimistic,
finalized,
&blob_sidecar_list_filtered,
)?;
Ok(warp::reply::json(&res).into_response())
}
}
.map(|resp| add_consensus_version_header(resp, fork_name))
})
},
);