|
| 1 | +use crate::blob_verification::{GossipBlobError, GossipVerifiedBlob}; |
| 2 | +use crate::fetch_blobs::{EngineGetBlobsOutput, FetchEngineBlobError}; |
| 3 | +use crate::observed_data_sidecars::DoNotObserve; |
| 4 | +use crate::{AvailabilityProcessingStatus, BeaconChain, BeaconChainTypes}; |
| 5 | +use execution_layer::json_structures::{BlobAndProofV1, BlobAndProofV2}; |
| 6 | +#[cfg(test)] |
| 7 | +use mockall::automock; |
| 8 | +use std::sync::Arc; |
| 9 | +use types::{BlobSidecar, ChainSpec, Hash256, Slot}; |
| 10 | + |
| 11 | +/// An adapter to the `BeaconChain` functionalities to remove `BeaconChain` from direct dependency to enable testing fetch blobs logic. |
| 12 | +pub(crate) struct FetchBlobsBeaconAdapter<T: BeaconChainTypes> { |
| 13 | + chain: Arc<BeaconChain<T>>, |
| 14 | + spec: Arc<ChainSpec>, |
| 15 | +} |
| 16 | + |
| 17 | +#[cfg_attr(test, automock, allow(dead_code))] |
| 18 | +impl<T: BeaconChainTypes> FetchBlobsBeaconAdapter<T> { |
| 19 | + pub(crate) fn new(chain: Arc<BeaconChain<T>>) -> Self { |
| 20 | + let spec = chain.spec.clone(); |
| 21 | + Self { chain, spec } |
| 22 | + } |
| 23 | + |
| 24 | + pub(crate) fn chain(&self) -> &Arc<BeaconChain<T>> { |
| 25 | + &self.chain |
| 26 | + } |
| 27 | + |
| 28 | + pub(crate) fn spec(&self) -> &Arc<ChainSpec> { |
| 29 | + &self.spec |
| 30 | + } |
| 31 | + |
| 32 | + pub(crate) async fn get_blobs_v1( |
| 33 | + &self, |
| 34 | + versioned_hashes: Vec<Hash256>, |
| 35 | + ) -> Result<Vec<Option<BlobAndProofV1<T::EthSpec>>>, FetchEngineBlobError> { |
| 36 | + let execution_layer = self |
| 37 | + .chain |
| 38 | + .execution_layer |
| 39 | + .as_ref() |
| 40 | + .ok_or(FetchEngineBlobError::ExecutionLayerMissing)?; |
| 41 | + |
| 42 | + execution_layer |
| 43 | + .get_blobs_v1(versioned_hashes) |
| 44 | + .await |
| 45 | + .map_err(FetchEngineBlobError::RequestFailed) |
| 46 | + } |
| 47 | + |
| 48 | + pub(crate) async fn get_blobs_v2( |
| 49 | + &self, |
| 50 | + versioned_hashes: Vec<Hash256>, |
| 51 | + ) -> Result<Option<Vec<BlobAndProofV2<T::EthSpec>>>, FetchEngineBlobError> { |
| 52 | + let execution_layer = self |
| 53 | + .chain |
| 54 | + .execution_layer |
| 55 | + .as_ref() |
| 56 | + .ok_or(FetchEngineBlobError::ExecutionLayerMissing)?; |
| 57 | + |
| 58 | + execution_layer |
| 59 | + .get_blobs_v2(versioned_hashes) |
| 60 | + .await |
| 61 | + .map_err(FetchEngineBlobError::RequestFailed) |
| 62 | + } |
| 63 | + |
| 64 | + pub(crate) fn verify_blob_for_gossip( |
| 65 | + &self, |
| 66 | + blob: &Arc<BlobSidecar<T::EthSpec>>, |
| 67 | + ) -> Result<GossipVerifiedBlob<T, DoNotObserve>, GossipBlobError> { |
| 68 | + GossipVerifiedBlob::<T, DoNotObserve>::new(blob.clone(), blob.index, &self.chain) |
| 69 | + } |
| 70 | + |
| 71 | + pub(crate) async fn process_engine_blobs( |
| 72 | + &self, |
| 73 | + slot: Slot, |
| 74 | + block_root: Hash256, |
| 75 | + blobs: EngineGetBlobsOutput<T::EthSpec>, |
| 76 | + ) -> Result<AvailabilityProcessingStatus, FetchEngineBlobError> { |
| 77 | + self.chain |
| 78 | + .process_engine_blobs(slot, block_root, blobs) |
| 79 | + .await |
| 80 | + .map_err(FetchEngineBlobError::BlobProcessingError) |
| 81 | + } |
| 82 | + |
| 83 | + pub(crate) fn fork_choice_contains_block(&self, block_root: &Hash256) -> bool { |
| 84 | + self.chain |
| 85 | + .canonical_head |
| 86 | + .fork_choice_read_lock() |
| 87 | + .contains_block(block_root) |
| 88 | + } |
| 89 | +} |
0 commit comments