Skip to content

Commit c303d3a

Browse files
committed
Refactor fetch blobs to be testable and add tests.
1 parent ef88e87 commit c303d3a

File tree

6 files changed

+448
-35
lines changed

6 files changed

+448
-35
lines changed

Cargo.lock

Lines changed: 84 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ logroller = "0.1.4"
163163
lru = "0.12"
164164
maplit = "1"
165165
milhouse = "0.5"
166+
mockall = "0.13"
167+
mockall_double = "0.3"
166168
mockito = "1.5.0"
167169
num_cpus = "1"
168170
once_cell = "1.17.1"

beacon_node/beacon_chain/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ test_backfill = []
2121
criterion = { workspace = true }
2222
maplit = { workspace = true }
2323
serde_json = { workspace = true }
24+
mockall = { workspace = true }
25+
mockall_double = { workspace = true }
2426

2527
[dependencies]
2628
alloy-primitives = { workspace = true }
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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

Comments
 (0)