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        if request.limit == 0 {
18            return Ok(Vec::new());
19        }
20        let mut total_bytes = 0;
21        let mut block_hash = request.start_hash;
22        let mut headers = Vec::new();
23        while let Some(header) = self.header(block_hash)? {
24            block_hash = header.parent_hash;
25            total_bytes += header.length();
26            headers.push(header);
27            if headers.len() >= request.limit as usize ||
28                headers.len() >= MAX_HEADERS_SERVE ||
29                total_bytes > SOFT_RESPONSE_LIMIT
30            {
31                break
32            }
33        }
34        Ok(headers)
35    }
36
37    /// Return block body by hash.
38    fn block_body(&self, block_hash: B256) -> ProviderResult<Option<BlockBody>>;
39
40    /// Return block bodies.
41    fn block_bodies(&self, block_hashes: Vec<B256>) -> ProviderResult<Vec<BlockBody>> {
42        let mut total_bytes = 0;
43        let mut bodies = Vec::new();
44        for block_hash in block_hashes {
45            if let Some(body) = self.block_body(block_hash)? {
46                total_bytes += body.length();
47                bodies.push(body);
48                if bodies.len() >= MAX_BODIES_SERVE || total_bytes > SOFT_RESPONSE_LIMIT {
49                    break
50                }
51            } else {
52                break
53            }
54        }
55        Ok(bodies)
56    }
57
58    /// Return bytecode by code hash.
59    fn bytecode(&self, code_hash: B256) -> ProviderResult<Option<Bytes>>;
60
61    /// Return witness by block hash.
62    fn witness(&self, block_hash: B256) -> impl Future<Output = ProviderResult<Vec<Bytes>>> + Send;
63}