reth_ress_protocol/
provider.rs

1use crate::GetHeaders;
2use alloy_consensus::Header;
3use alloy_primitives::{Bytes, B256};
4use alloy_rlp::Encodable;
5use reth_ethereum_primitives::BlockBody;
6use reth_network::eth_requests::{MAX_BODIES_SERVE, MAX_HEADERS_SERVE, SOFT_RESPONSE_LIMIT};
7use reth_storage_errors::provider::ProviderResult;
8use std::future::Future;
9
10/// A provider trait for ress protocol.
11pub trait RessProtocolProvider: Send + Sync {
12    /// Return block header by hash.
13    fn header(&self, block_hash: B256) -> ProviderResult<Option<Header>>;
14
15    /// Return block headers.
16    fn headers(&self, request: GetHeaders) -> ProviderResult<Vec<Header>> {
17        let mut total_bytes = 0;
18        let mut block_hash = request.start_hash;
19        let mut headers = Vec::new();
20        while let Some(header) = self.header(block_hash)? {
21            block_hash = header.parent_hash;
22            total_bytes += header.length();
23            headers.push(header);
24            if headers.len() >= request.limit as usize ||
25                headers.len() >= MAX_HEADERS_SERVE ||
26                total_bytes > SOFT_RESPONSE_LIMIT
27            {
28                break
29            }
30        }
31        Ok(headers)
32    }
33
34    /// Return block body by hash.
35    fn block_body(&self, block_hash: B256) -> ProviderResult<Option<BlockBody>>;
36
37    /// Return block bodies.
38    fn block_bodies(&self, block_hashes: Vec<B256>) -> ProviderResult<Vec<BlockBody>> {
39        let mut total_bytes = 0;
40        let mut bodies = Vec::new();
41        for block_hash in block_hashes {
42            if let Some(body) = self.block_body(block_hash)? {
43                total_bytes += body.length();
44                bodies.push(body);
45                if bodies.len() >= MAX_BODIES_SERVE || total_bytes > SOFT_RESPONSE_LIMIT {
46                    break
47                }
48            } else {
49                break
50            }
51        }
52        Ok(bodies)
53    }
54
55    /// Return bytecode by code hash.
56    fn bytecode(&self, code_hash: B256) -> ProviderResult<Option<Bytes>>;
57
58    /// Return witness by block hash.
59    fn witness(&self, block_hash: B256) -> impl Future<Output = ProviderResult<Vec<Bytes>>> + Send;
60}