reth_engine_tree/
download.rs

1//! Handler that can download blocks on demand (e.g. from the network).
2
3use crate::{engine::DownloadRequest, metrics::BlockDownloaderMetrics};
4use alloy_consensus::BlockHeader;
5use alloy_primitives::B256;
6use futures::FutureExt;
7use reth_consensus::{Consensus, ConsensusError};
8use reth_network_p2p::{
9    full_block::{FetchFullBlockFuture, FetchFullBlockRangeFuture, FullBlockClient},
10    BlockClient,
11};
12use reth_primitives_traits::{Block, SealedBlock};
13use std::{
14    cmp::{Ordering, Reverse},
15    collections::{binary_heap::PeekMut, BinaryHeap, HashSet, VecDeque},
16    fmt::Debug,
17    sync::Arc,
18    task::{Context, Poll},
19};
20use tracing::trace;
21
22/// A trait that can download blocks on demand.
23pub trait BlockDownloader: Send + Sync {
24    /// Type of the block being downloaded.
25    type Block: Block;
26
27    /// Handle an action.
28    fn on_action(&mut self, action: DownloadAction);
29
30    /// Advance in progress requests if any
31    fn poll(&mut self, cx: &mut Context<'_>) -> Poll<DownloadOutcome<Self::Block>>;
32}
33
34/// Actions that can be performed by the block downloader.
35#[derive(Debug)]
36pub enum DownloadAction {
37    /// Stop downloading blocks.
38    Clear,
39    /// Download given blocks
40    Download(DownloadRequest),
41}
42
43/// Outcome of downloaded blocks.
44#[derive(Debug)]
45pub enum DownloadOutcome<B: Block> {
46    /// Downloaded blocks.
47    Blocks(Vec<SealedBlock<B>>),
48    /// New download started.
49    NewDownloadStarted {
50        /// How many blocks are pending in this download.
51        remaining_blocks: u64,
52        /// The hash of the highest block of this download.
53        target: B256,
54    },
55}
56
57/// Basic [`BlockDownloader`].
58#[expect(missing_debug_implementations)]
59pub struct BasicBlockDownloader<Client, B: Block>
60where
61    Client: BlockClient + 'static,
62{
63    /// A downloader that can download full blocks from the network.
64    full_block_client: FullBlockClient<Client>,
65    /// In-flight full block requests in progress.
66    inflight_full_block_requests: Vec<FetchFullBlockFuture<Client>>,
67    /// In-flight full block _range_ requests in progress.
68    inflight_block_range_requests: Vec<FetchFullBlockRangeFuture<Client>>,
69    /// Buffered blocks from downloads - this is a min-heap of blocks, using the block number for
70    /// ordering. This means the blocks will be popped from the heap with ascending block numbers.
71    set_buffered_blocks: BinaryHeap<Reverse<OrderedSealedBlock<B>>>,
72    /// Engine download metrics.
73    metrics: BlockDownloaderMetrics,
74    /// Pending events to be emitted.
75    pending_events: VecDeque<DownloadOutcome<B>>,
76}
77
78impl<Client, B> BasicBlockDownloader<Client, B>
79where
80    Client: BlockClient<Block = B> + 'static,
81    B: Block,
82{
83    /// Create a new instance
84    pub fn new(client: Client, consensus: Arc<dyn Consensus<B, Error = ConsensusError>>) -> Self {
85        Self {
86            full_block_client: FullBlockClient::new(client, consensus),
87            inflight_full_block_requests: Vec::new(),
88            inflight_block_range_requests: Vec::new(),
89            set_buffered_blocks: BinaryHeap::new(),
90            metrics: BlockDownloaderMetrics::default(),
91            pending_events: Default::default(),
92        }
93    }
94
95    /// Clears the stored inflight requests.
96    fn clear(&mut self) {
97        self.inflight_full_block_requests.clear();
98        self.inflight_block_range_requests.clear();
99        self.set_buffered_blocks.clear();
100        self.update_block_download_metrics();
101    }
102
103    /// Processes a download request.
104    fn download(&mut self, request: DownloadRequest) {
105        match request {
106            DownloadRequest::BlockSet(hashes) => self.download_block_set(hashes),
107            DownloadRequest::BlockRange(hash, count) => self.download_block_range(hash, count),
108        }
109    }
110
111    /// Processes a block set download request.
112    fn download_block_set(&mut self, hashes: HashSet<B256>) {
113        for hash in hashes {
114            self.download_full_block(hash);
115        }
116    }
117
118    /// Processes a block range download request.
119    fn download_block_range(&mut self, hash: B256, count: u64) {
120        if count == 1 {
121            self.download_full_block(hash);
122        } else {
123            trace!(
124                target: "engine::download",
125                ?hash,
126                ?count,
127                "start downloading full block range."
128            );
129
130            let request = self.full_block_client.get_full_block_range(hash, count);
131            self.push_pending_event(DownloadOutcome::NewDownloadStarted {
132                remaining_blocks: request.count(),
133                target: request.start_hash(),
134            });
135            self.inflight_block_range_requests.push(request);
136
137            self.update_block_download_metrics();
138        }
139    }
140
141    /// Starts requesting a full block from the network.
142    ///
143    /// Returns `true` if the request was started, `false` if there's already a request for the
144    /// given hash.
145    fn download_full_block(&mut self, hash: B256) -> bool {
146        if self.is_inflight_request(hash) {
147            return false
148        }
149        self.push_pending_event(DownloadOutcome::NewDownloadStarted {
150            remaining_blocks: 1,
151            target: hash,
152        });
153
154        trace!(
155            target: "engine::download",
156            ?hash,
157            "Start downloading full block"
158        );
159
160        let request = self.full_block_client.get_full_block(hash);
161        self.inflight_full_block_requests.push(request);
162
163        self.update_block_download_metrics();
164
165        true
166    }
167
168    /// Returns true if there's already a request for the given hash.
169    fn is_inflight_request(&self, hash: B256) -> bool {
170        self.inflight_full_block_requests.iter().any(|req| *req.hash() == hash)
171    }
172
173    /// Sets the metrics for the active downloads
174    fn update_block_download_metrics(&self) {
175        let blocks = self.inflight_full_block_requests.len() +
176            self.inflight_block_range_requests.iter().map(|r| r.count() as usize).sum::<usize>();
177        self.metrics.active_block_downloads.set(blocks as f64);
178    }
179
180    /// Adds a pending event to the FIFO queue.
181    fn push_pending_event(&mut self, pending_event: DownloadOutcome<B>) {
182        self.pending_events.push_back(pending_event);
183    }
184
185    /// Removes a pending event from the FIFO queue.
186    fn pop_pending_event(&mut self) -> Option<DownloadOutcome<B>> {
187        self.pending_events.pop_front()
188    }
189}
190
191impl<Client, B> BlockDownloader for BasicBlockDownloader<Client, B>
192where
193    Client: BlockClient<Block = B>,
194    B: Block,
195{
196    type Block = B;
197
198    /// Handles incoming download actions.
199    fn on_action(&mut self, action: DownloadAction) {
200        match action {
201            DownloadAction::Clear => self.clear(),
202            DownloadAction::Download(request) => self.download(request),
203        }
204    }
205
206    /// Advances the download process.
207    fn poll(&mut self, cx: &mut Context<'_>) -> Poll<DownloadOutcome<B>> {
208        if let Some(pending_event) = self.pop_pending_event() {
209            return Poll::Ready(pending_event);
210        }
211
212        // advance all full block requests
213        for idx in (0..self.inflight_full_block_requests.len()).rev() {
214            let mut request = self.inflight_full_block_requests.swap_remove(idx);
215            if let Poll::Ready(block) = request.poll_unpin(cx) {
216                trace!(target: "engine::download", block=?block.num_hash(), "Received single full block, buffering");
217                self.set_buffered_blocks.push(Reverse(block.into()));
218            } else {
219                // still pending
220                self.inflight_full_block_requests.push(request);
221            }
222        }
223
224        // advance all full block range requests
225        for idx in (0..self.inflight_block_range_requests.len()).rev() {
226            let mut request = self.inflight_block_range_requests.swap_remove(idx);
227            if let Poll::Ready(blocks) = request.poll_unpin(cx) {
228                trace!(target: "engine::download", len=?blocks.len(), first=?blocks.first().map(|b| b.num_hash()), last=?blocks.last().map(|b| b.num_hash()), "Received full block range, buffering");
229                self.set_buffered_blocks
230                    .extend(blocks.into_iter().map(OrderedSealedBlock).map(Reverse));
231            } else {
232                // still pending
233                self.inflight_block_range_requests.push(request);
234            }
235        }
236
237        self.update_block_download_metrics();
238
239        if self.set_buffered_blocks.is_empty() {
240            return Poll::Pending;
241        }
242
243        // drain all unique element of the block buffer if there are any
244        let mut downloaded_blocks = Vec::with_capacity(self.set_buffered_blocks.len());
245        while let Some(block) = self.set_buffered_blocks.pop() {
246            // peek ahead and pop duplicates
247            while let Some(peek) = self.set_buffered_blocks.peek_mut() {
248                if peek.0 .0.hash() == block.0 .0.hash() {
249                    PeekMut::pop(peek);
250                } else {
251                    break
252                }
253            }
254            downloaded_blocks.push(block.0.into());
255        }
256        Poll::Ready(DownloadOutcome::Blocks(downloaded_blocks))
257    }
258}
259
260/// A wrapper type around [`SealedBlock`] that implements the [Ord]
261/// trait by block number.
262#[derive(Debug, Clone, PartialEq, Eq)]
263struct OrderedSealedBlock<B: Block>(SealedBlock<B>);
264
265impl<B: Block> PartialOrd for OrderedSealedBlock<B> {
266    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
267        Some(self.cmp(other))
268    }
269}
270
271impl<B: Block> Ord for OrderedSealedBlock<B> {
272    fn cmp(&self, other: &Self) -> Ordering {
273        self.0.number().cmp(&other.0.number())
274    }
275}
276
277impl<B: Block> From<SealedBlock<B>> for OrderedSealedBlock<B> {
278    fn from(block: SealedBlock<B>) -> Self {
279        Self(block)
280    }
281}
282
283impl<B: Block> From<OrderedSealedBlock<B>> for SealedBlock<B> {
284    fn from(value: OrderedSealedBlock<B>) -> Self {
285        value.0
286    }
287}
288
289/// A [`BlockDownloader`] that does nothing.
290#[derive(Debug, Clone, Default)]
291#[non_exhaustive]
292pub struct NoopBlockDownloader<B>(core::marker::PhantomData<B>);
293
294impl<B: Block> BlockDownloader for NoopBlockDownloader<B> {
295    type Block = B;
296
297    fn on_action(&mut self, _event: DownloadAction) {}
298
299    fn poll(&mut self, _cx: &mut Context<'_>) -> Poll<DownloadOutcome<B>> {
300        Poll::Pending
301    }
302}
303
304#[cfg(test)]
305mod tests {
306    use super::*;
307    use crate::test_utils::insert_headers_into_client;
308    use alloy_consensus::Header;
309    use alloy_eips::eip1559::ETHEREUM_BLOCK_GAS_LIMIT_30M;
310    use assert_matches::assert_matches;
311    use reth_chainspec::{ChainSpecBuilder, MAINNET};
312    use reth_ethereum_consensus::EthBeaconConsensus;
313    use reth_network_p2p::test_utils::TestFullBlockClient;
314    use reth_primitives_traits::SealedHeader;
315    use std::{future::poll_fn, sync::Arc};
316
317    struct TestHarness {
318        block_downloader:
319            BasicBlockDownloader<TestFullBlockClient, reth_ethereum_primitives::Block>,
320        client: TestFullBlockClient,
321    }
322
323    impl TestHarness {
324        fn new(total_blocks: usize) -> Self {
325            let chain_spec = Arc::new(
326                ChainSpecBuilder::default()
327                    .chain(MAINNET.chain)
328                    .genesis(MAINNET.genesis.clone())
329                    .paris_activated()
330                    .build(),
331            );
332
333            let client = TestFullBlockClient::default();
334            let header = Header {
335                base_fee_per_gas: Some(7),
336                gas_limit: ETHEREUM_BLOCK_GAS_LIMIT_30M,
337                ..Default::default()
338            };
339            let header = SealedHeader::seal_slow(header);
340
341            insert_headers_into_client(&client, header, 0..total_blocks);
342            let consensus = Arc::new(EthBeaconConsensus::new(chain_spec));
343
344            let block_downloader = BasicBlockDownloader::new(client.clone(), consensus);
345            Self { block_downloader, client }
346        }
347    }
348
349    #[tokio::test]
350    async fn block_downloader_range_request() {
351        const TOTAL_BLOCKS: usize = 10;
352        let TestHarness { mut block_downloader, client } = TestHarness::new(TOTAL_BLOCKS);
353        let tip = client.highest_block().expect("there should be blocks here");
354
355        // send block range download request
356        block_downloader.on_action(DownloadAction::Download(DownloadRequest::BlockRange(
357            tip.hash(),
358            tip.number,
359        )));
360
361        // ensure we have one in flight range request
362        assert_eq!(block_downloader.inflight_block_range_requests.len(), 1);
363
364        // ensure the range request is made correctly
365        let first_req = block_downloader.inflight_block_range_requests.first().unwrap();
366        assert_eq!(first_req.start_hash(), tip.hash());
367        assert_eq!(first_req.count(), tip.number);
368
369        // poll downloader
370        let sync_future = poll_fn(|cx| block_downloader.poll(cx));
371        let next_ready = sync_future.await;
372
373        assert_matches!(next_ready, DownloadOutcome::NewDownloadStarted { remaining_blocks, .. } => {
374            assert_eq!(remaining_blocks, TOTAL_BLOCKS as u64);
375        });
376
377        let sync_future = poll_fn(|cx| block_downloader.poll(cx));
378        let next_ready = sync_future.await;
379
380        assert_matches!(next_ready, DownloadOutcome::Blocks(blocks) => {
381            // ensure all blocks were obtained
382            assert_eq!(blocks.len(), TOTAL_BLOCKS);
383
384            // ensure they are in ascending order
385            for num in 1..=TOTAL_BLOCKS {
386                assert_eq!(blocks[num-1].number(), num as u64);
387            }
388        });
389    }
390
391    #[tokio::test]
392    async fn block_downloader_set_request() {
393        const TOTAL_BLOCKS: usize = 2;
394        let TestHarness { mut block_downloader, client } = TestHarness::new(TOTAL_BLOCKS);
395
396        let tip = client.highest_block().expect("there should be blocks here");
397
398        // send block set download request
399        block_downloader.on_action(DownloadAction::Download(DownloadRequest::BlockSet(
400            HashSet::from([tip.hash(), tip.parent_hash]),
401        )));
402
403        // ensure we have TOTAL_BLOCKS in flight full block request
404        assert_eq!(block_downloader.inflight_full_block_requests.len(), TOTAL_BLOCKS);
405
406        // poll downloader
407        for _ in 0..TOTAL_BLOCKS {
408            let sync_future = poll_fn(|cx| block_downloader.poll(cx));
409            let next_ready = sync_future.await;
410
411            assert_matches!(next_ready, DownloadOutcome::NewDownloadStarted { remaining_blocks, .. } => {
412                assert_eq!(remaining_blocks, 1);
413            });
414        }
415
416        let sync_future = poll_fn(|cx| block_downloader.poll(cx));
417        let next_ready = sync_future.await;
418        assert_matches!(next_ready, DownloadOutcome::Blocks(blocks) => {
419            // ensure all blocks were obtained
420            assert_eq!(blocks.len(), TOTAL_BLOCKS);
421
422            // ensure they are in ascending order
423            for num in 1..=TOTAL_BLOCKS {
424                assert_eq!(blocks[num-1].number(), num as u64);
425            }
426        });
427    }
428
429    #[tokio::test]
430    async fn block_downloader_clear_request() {
431        const TOTAL_BLOCKS: usize = 10;
432        let TestHarness { mut block_downloader, client } = TestHarness::new(TOTAL_BLOCKS);
433
434        let tip = client.highest_block().expect("there should be blocks here");
435
436        // send block range download request
437        block_downloader.on_action(DownloadAction::Download(DownloadRequest::BlockRange(
438            tip.hash(),
439            tip.number,
440        )));
441
442        // send block set download request
443        let download_set = HashSet::from([tip.hash(), tip.parent_hash]);
444        block_downloader
445            .on_action(DownloadAction::Download(DownloadRequest::BlockSet(download_set.clone())));
446
447        // ensure we have one in flight range request
448        assert_eq!(block_downloader.inflight_block_range_requests.len(), 1);
449
450        // ensure the range request is made correctly
451        let first_req = block_downloader.inflight_block_range_requests.first().unwrap();
452        assert_eq!(first_req.start_hash(), tip.hash());
453        assert_eq!(first_req.count(), tip.number);
454
455        // ensure we have download_set.len() in flight full block request
456        assert_eq!(block_downloader.inflight_full_block_requests.len(), download_set.len());
457
458        // send clear request
459        block_downloader.on_action(DownloadAction::Clear);
460
461        // ensure we have no in flight range request
462        assert_eq!(block_downloader.inflight_block_range_requests.len(), 0);
463
464        // ensure we have no in flight full block request
465        assert_eq!(block_downloader.inflight_full_block_requests.len(), 0);
466    }
467}