alloy_provider/
blocks.rs

1use alloy_network::{Ethereum, Network};
2use alloy_primitives::{BlockNumber, U64};
3use alloy_rpc_client::{NoParams, PollerBuilder, WeakClient};
4use alloy_transport::RpcError;
5use async_stream::stream;
6use futures::{Stream, StreamExt};
7use lru::LruCache;
8use std::{marker::PhantomData, num::NonZeroUsize};
9
10#[cfg(feature = "pubsub")]
11use futures::{future::Either, FutureExt};
12
13/// The size of the block cache.
14const BLOCK_CACHE_SIZE: NonZeroUsize = NonZeroUsize::new(10).unwrap();
15
16/// Maximum number of retries for fetching a block.
17const MAX_RETRIES: usize = 3;
18
19/// Default block number for when we don't have a block yet.
20const NO_BLOCK_NUMBER: BlockNumber = BlockNumber::MAX;
21
22/// Streams new blocks from the client.
23pub(crate) struct NewBlocks<N: Network = Ethereum> {
24    client: WeakClient,
25    /// The next block to yield.
26    /// [`NO_BLOCK_NUMBER`] indicates that it will be updated on the first poll.
27    /// Only used by the polling task.
28    next_yield: BlockNumber,
29    /// LRU cache of known blocks. Only used by the polling task.
30    known_blocks: LruCache<BlockNumber, N::BlockResponse>,
31    _phantom: PhantomData<N>,
32}
33
34impl<N: Network> NewBlocks<N> {
35    pub(crate) fn new(client: WeakClient) -> Self {
36        Self {
37            client,
38            next_yield: NO_BLOCK_NUMBER,
39            known_blocks: LruCache::new(BLOCK_CACHE_SIZE),
40            _phantom: PhantomData,
41        }
42    }
43
44    #[cfg(test)]
45    const fn with_next_yield(mut self, next_yield: u64) -> Self {
46        self.next_yield = next_yield;
47        self
48    }
49
50    pub(crate) fn into_stream(self) -> impl Stream<Item = N::BlockResponse> + 'static {
51        // Return a stream that lazily subscribes to `newHeads` on the first poll.
52        #[cfg(feature = "pubsub")]
53        if let Some(client) = self.client.upgrade() {
54            if client.pubsub_frontend().is_some() {
55                let subscriber = self.into_subscription_stream().map(futures::stream::iter);
56                let subscriber = futures::stream::once(subscriber);
57                return Either::Left(subscriber.flatten().flatten());
58            }
59        }
60
61        // Returns a stream that lazily initializes an `eth_blockNumber` polling task on the first
62        // poll, mapped with `eth_getBlockByNumber`.
63        #[cfg(feature = "pubsub")]
64        let right = Either::Right;
65        #[cfg(not(feature = "pubsub"))]
66        let right = std::convert::identity;
67        right(self.into_poll_stream())
68    }
69
70    #[cfg(feature = "pubsub")]
71    async fn into_subscription_stream(
72        self,
73    ) -> Option<impl Stream<Item = N::BlockResponse> + 'static> {
74        use alloy_consensus::BlockHeader;
75
76        let Some(client) = self.client.upgrade() else {
77            debug!("client dropped");
78            return None;
79        };
80        let Some(pubsub) = client.pubsub_frontend() else {
81            error!("pubsub_frontend returned None after being Some");
82            return None;
83        };
84        let id = match client.request("eth_subscribe", ("newHeads",)).await {
85            Ok(id) => id,
86            Err(err) => {
87                error!(%err, "failed to subscribe to newHeads");
88                return None;
89            }
90        };
91        let sub = match pubsub.get_subscription(id).await {
92            Ok(sub) => sub,
93            Err(err) => {
94                error!(%err, "failed to get subscription");
95                return None;
96            }
97        };
98        let stream =
99            sub.into_typed::<N::HeaderResponse>().into_stream().map(|header| header.number());
100        Some(self.into_block_stream(stream))
101    }
102
103    fn into_poll_stream(self) -> impl Stream<Item = N::BlockResponse> + 'static {
104        // Spawned lazily on the first `poll`.
105        let stream =
106            PollerBuilder::<NoParams, U64>::new(self.client.clone(), "eth_blockNumber", [])
107                .into_stream()
108                .map(|n| n.to());
109
110        self.into_block_stream(stream)
111    }
112
113    fn into_block_stream(
114        mut self,
115        mut numbers_stream: impl Stream<Item = u64> + Unpin + 'static,
116    ) -> impl Stream<Item = N::BlockResponse> + 'static {
117        stream! {
118        'task: loop {
119            // Clear any buffered blocks.
120            while let Some(known_block) = self.known_blocks.pop(&self.next_yield) {
121                debug!(number=self.next_yield, "yielding block");
122                self.next_yield += 1;
123                yield known_block;
124            }
125
126            // Get the tip.
127            let Some(block_number) = numbers_stream.next().await else {
128                debug!("polling stream ended");
129                break 'task;
130            };
131            trace!(%block_number, "got block number");
132            if self.next_yield == NO_BLOCK_NUMBER {
133                assert!(block_number < NO_BLOCK_NUMBER, "too many blocks");
134                self.next_yield = block_number;
135            } else if block_number < self.next_yield {
136                debug!(block_number, self.next_yield, "not advanced yet");
137                continue 'task;
138            }
139
140            // Upgrade the provider.
141            let Some(client) = self.client.upgrade() else {
142                debug!("client dropped");
143                break 'task;
144            };
145
146            // Then try to fill as many blocks as possible.
147            // TODO: Maybe use `join_all`
148            let mut retries = MAX_RETRIES;
149            for number in self.next_yield..=block_number {
150                debug!(number, "fetching block");
151                let block = match client.request("eth_getBlockByNumber", (U64::from(number), false)).await {
152                    Ok(Some(block)) => block,
153                    Err(RpcError::Transport(err)) if retries > 0 && err.recoverable() => {
154                        debug!(number, %err, "failed to fetch block, retrying");
155                        retries -= 1;
156                        continue;
157                    }
158                    Ok(None) if retries > 0 => {
159                        debug!(number, "failed to fetch block (doesn't exist), retrying");
160                        retries -= 1;
161                        continue;
162                    }
163                    Err(err) => {
164                        error!(number, %err, "failed to fetch block");
165                        break;
166                    }
167                    Ok(None) => {
168                        error!(number, "failed to fetch block (doesn't exist)");
169                        break;
170                    }
171                };
172                self.known_blocks.put(number, block);
173                if self.known_blocks.len() == BLOCK_CACHE_SIZE.get() {
174                    // Cache is full, should be consumed before filling more blocks.
175                    debug!(number, "cache full");
176                    break;
177                }
178            }
179        }
180        }
181    }
182}
183
184#[cfg(all(test, feature = "anvil-api"))] // Tests rely heavily on ability to mine blocks on demand.
185mod tests {
186    use super::*;
187    use crate::{ext::AnvilApi, Provider, ProviderBuilder};
188    use alloy_node_bindings::Anvil;
189    use std::{future::Future, time::Duration};
190
191    async fn timeout<T: Future>(future: T) -> T::Output {
192        try_timeout(future).await.expect("Timeout")
193    }
194
195    async fn try_timeout<T: Future>(future: T) -> Option<T::Output> {
196        tokio::time::timeout(Duration::from_secs(2), future).await.ok()
197    }
198
199    #[tokio::test]
200    async fn yield_block_http() {
201        yield_block(false).await;
202    }
203    #[tokio::test]
204    #[cfg(feature = "ws")]
205    async fn yield_block_ws() {
206        yield_block(true).await;
207    }
208    async fn yield_block(ws: bool) {
209        let anvil = Anvil::new().spawn();
210
211        let url = if ws { anvil.ws_endpoint() } else { anvil.endpoint() };
212        let provider = ProviderBuilder::new().connect(&url).await.unwrap();
213
214        let new_blocks = NewBlocks::<Ethereum>::new(provider.weak_client()).with_next_yield(1);
215        let mut stream = Box::pin(new_blocks.into_stream());
216        if ws {
217            let _ = try_timeout(stream.next()).await; // Subscribe to newHeads.
218        }
219
220        // We will also use provider to manipulate anvil instance via RPC.
221        provider.anvil_mine(Some(1), None).await.unwrap();
222
223        let block = timeout(stream.next()).await.expect("Block wasn't fetched");
224        assert_eq!(block.header.number, 1);
225    }
226
227    #[tokio::test]
228    async fn yield_many_blocks_http() {
229        yield_many_blocks(false).await;
230    }
231    #[tokio::test]
232    #[cfg(feature = "ws")]
233    async fn yield_many_blocks_ws() {
234        yield_many_blocks(true).await;
235    }
236    async fn yield_many_blocks(ws: bool) {
237        // Make sure that we can process more blocks than fits in the cache.
238        const BLOCKS_TO_MINE: usize = BLOCK_CACHE_SIZE.get() + 1;
239
240        let anvil = Anvil::new().spawn();
241
242        let url = if ws { anvil.ws_endpoint() } else { anvil.endpoint() };
243        let provider = ProviderBuilder::new().connect(&url).await.unwrap();
244
245        let new_blocks = NewBlocks::<Ethereum>::new(provider.weak_client()).with_next_yield(1);
246        let mut stream = Box::pin(new_blocks.into_stream());
247        if ws {
248            let _ = try_timeout(stream.next()).await; // Subscribe to newHeads.
249        }
250
251        // We will also use provider to manipulate anvil instance via RPC.
252        provider.anvil_mine(Some(BLOCKS_TO_MINE as u64), None).await.unwrap();
253
254        let blocks = timeout(stream.take(BLOCKS_TO_MINE).collect::<Vec<_>>()).await;
255        assert_eq!(blocks.len(), BLOCKS_TO_MINE);
256        let first = blocks[0].header.number;
257        assert_eq!(first, 1);
258        for (i, block) in blocks.iter().enumerate() {
259            assert_eq!(block.header.number, first + i as u64);
260        }
261    }
262}