reth_rpc_eth_types/builder/
config.rs

1//! Configuration for `eth` namespace APIs.
2
3use std::time::Duration;
4
5use crate::{
6    EthStateCacheConfig, FeeHistoryCacheConfig, ForwardConfig, GasPriceOracleConfig,
7    RPC_DEFAULT_GAS_CAP,
8};
9use reqwest::Url;
10use reth_rpc_server_types::constants::{
11    default_max_tracing_requests, DEFAULT_ETH_PROOF_WINDOW, DEFAULT_MAX_BLOCKS_PER_FILTER,
12    DEFAULT_MAX_LOGS_PER_RESPONSE, DEFAULT_MAX_SIMULATE_BLOCKS, DEFAULT_MAX_TRACE_FILTER_BLOCKS,
13    DEFAULT_PROOF_PERMITS, RPC_DEFAULT_SEND_RAW_TX_SYNC_TIMEOUT_SECS,
14};
15use serde::{Deserialize, Serialize};
16
17/// Default value for stale filter ttl
18pub const DEFAULT_STALE_FILTER_TTL: Duration = Duration::from_secs(5 * 60);
19
20/// Config for the locally built pending block
21#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize, Default)]
22#[serde(rename_all = "lowercase")]
23pub enum PendingBlockKind {
24    /// Return a pending block with header only, no transactions included
25    Empty,
26    /// Return null/no pending block
27    None,
28    /// Return a pending block with all transactions from the mempool (default behavior)
29    #[default]
30    Full,
31}
32
33impl std::str::FromStr for PendingBlockKind {
34    type Err = String;
35
36    fn from_str(s: &str) -> Result<Self, Self::Err> {
37        match s.to_lowercase().as_str() {
38            "empty" => Ok(Self::Empty),
39            "none" => Ok(Self::None),
40            "full" => Ok(Self::Full),
41            _ => Err(format!(
42                "Invalid pending block kind: {s}. Valid options are: empty, none, full"
43            )),
44        }
45    }
46}
47
48impl PendingBlockKind {
49    /// Returns true if the pending block kind is `None`
50    pub const fn is_none(&self) -> bool {
51        matches!(self, Self::None)
52    }
53
54    /// Returns true if the pending block kind is `Empty`
55    pub const fn is_empty(&self) -> bool {
56        matches!(self, Self::Empty)
57    }
58}
59
60/// Additional config values for the eth namespace.
61#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
62pub struct EthConfig {
63    /// Settings for the caching layer
64    pub cache: EthStateCacheConfig,
65    /// Settings for the gas price oracle
66    pub gas_oracle: GasPriceOracleConfig,
67    /// The maximum number of blocks into the past for generating state proofs.
68    pub eth_proof_window: u64,
69    /// The maximum number of tracing calls that can be executed in concurrently.
70    pub max_tracing_requests: usize,
71    /// Maximum number of blocks for `trace_filter` requests.
72    pub max_trace_filter_blocks: u64,
73    /// Maximum number of blocks that could be scanned per filter request in `eth_getLogs` calls.
74    pub max_blocks_per_filter: u64,
75    /// Maximum number of logs that can be returned in a single response in `eth_getLogs` calls.
76    pub max_logs_per_response: usize,
77    /// Gas limit for `eth_call` and call tracing RPC methods.
78    ///
79    /// Defaults to [`RPC_DEFAULT_GAS_CAP`]
80    pub rpc_gas_cap: u64,
81    /// Max number of blocks for `eth_simulateV1`.
82    pub rpc_max_simulate_blocks: u64,
83    ///
84    /// Sets TTL for stale filters
85    pub stale_filter_ttl: Duration,
86    /// Settings for the fee history cache
87    pub fee_history_cache: FeeHistoryCacheConfig,
88    /// The maximum number of getproof calls that can be executed concurrently.
89    pub proof_permits: usize,
90    /// Maximum batch size for transaction pool insertions.
91    pub max_batch_size: usize,
92    /// Controls how pending blocks are built when requested via RPC methods
93    pub pending_block_kind: PendingBlockKind,
94    /// The raw transaction forwarder.
95    pub raw_tx_forwarder: ForwardConfig,
96    /// Timeout duration for `send_raw_transaction_sync` RPC method.
97    pub send_raw_transaction_sync_timeout: Duration,
98    /// Maximum memory the EVM can allocate per RPC request.
99    pub rpc_evm_memory_limit: u64,
100}
101
102impl EthConfig {
103    /// Returns the filter config for the `eth_filter` handler.
104    pub fn filter_config(&self) -> EthFilterConfig {
105        EthFilterConfig::default()
106            .max_blocks_per_filter(self.max_blocks_per_filter)
107            .max_logs_per_response(self.max_logs_per_response)
108            .stale_filter_ttl(self.stale_filter_ttl)
109    }
110}
111
112impl Default for EthConfig {
113    fn default() -> Self {
114        Self {
115            cache: EthStateCacheConfig::default(),
116            gas_oracle: GasPriceOracleConfig::default(),
117            eth_proof_window: DEFAULT_ETH_PROOF_WINDOW,
118            max_tracing_requests: default_max_tracing_requests(),
119            max_trace_filter_blocks: DEFAULT_MAX_TRACE_FILTER_BLOCKS,
120            max_blocks_per_filter: DEFAULT_MAX_BLOCKS_PER_FILTER,
121            max_logs_per_response: DEFAULT_MAX_LOGS_PER_RESPONSE,
122            rpc_gas_cap: RPC_DEFAULT_GAS_CAP.into(),
123            rpc_max_simulate_blocks: DEFAULT_MAX_SIMULATE_BLOCKS,
124            stale_filter_ttl: DEFAULT_STALE_FILTER_TTL,
125            fee_history_cache: FeeHistoryCacheConfig::default(),
126            proof_permits: DEFAULT_PROOF_PERMITS,
127            max_batch_size: 1,
128            pending_block_kind: PendingBlockKind::Full,
129            raw_tx_forwarder: ForwardConfig::default(),
130            send_raw_transaction_sync_timeout: RPC_DEFAULT_SEND_RAW_TX_SYNC_TIMEOUT_SECS,
131            rpc_evm_memory_limit: (1 << 32) - 1,
132        }
133    }
134}
135
136impl EthConfig {
137    /// Configures the caching layer settings
138    pub const fn state_cache(mut self, cache: EthStateCacheConfig) -> Self {
139        self.cache = cache;
140        self
141    }
142
143    /// Configures the gas price oracle settings
144    pub const fn gpo_config(mut self, gas_oracle_config: GasPriceOracleConfig) -> Self {
145        self.gas_oracle = gas_oracle_config;
146        self
147    }
148
149    /// Configures the maximum number of tracing requests
150    pub const fn max_tracing_requests(mut self, max_requests: usize) -> Self {
151        self.max_tracing_requests = max_requests;
152        self
153    }
154
155    /// Configures the maximum block length to scan per `eth_getLogs` request
156    pub const fn max_blocks_per_filter(mut self, max_blocks: u64) -> Self {
157        self.max_blocks_per_filter = max_blocks;
158        self
159    }
160
161    /// Configures the maximum number of blocks for `trace_filter` requests
162    pub const fn max_trace_filter_blocks(mut self, max_blocks: u64) -> Self {
163        self.max_trace_filter_blocks = max_blocks;
164        self
165    }
166
167    /// Configures the maximum number of logs per response
168    pub const fn max_logs_per_response(mut self, max_logs: usize) -> Self {
169        self.max_logs_per_response = max_logs;
170        self
171    }
172
173    /// Configures the maximum gas limit for `eth_call` and call tracing RPC methods
174    pub const fn rpc_gas_cap(mut self, rpc_gas_cap: u64) -> Self {
175        self.rpc_gas_cap = rpc_gas_cap;
176        self
177    }
178
179    /// Configures the maximum gas limit for `eth_call` and call tracing RPC methods
180    pub const fn rpc_max_simulate_blocks(mut self, max_blocks: u64) -> Self {
181        self.rpc_max_simulate_blocks = max_blocks;
182        self
183    }
184
185    /// Configures the maximum proof window for historical proof generation.
186    pub const fn eth_proof_window(mut self, window: u64) -> Self {
187        self.eth_proof_window = window;
188        self
189    }
190
191    /// Configures the number of getproof requests
192    pub const fn proof_permits(mut self, permits: usize) -> Self {
193        self.proof_permits = permits;
194        self
195    }
196
197    /// Configures the maximum batch size for transaction pool insertions
198    pub const fn max_batch_size(mut self, max_batch_size: usize) -> Self {
199        self.max_batch_size = max_batch_size;
200        self
201    }
202
203    /// Configures the pending block config
204    pub const fn pending_block_kind(mut self, pending_block_kind: PendingBlockKind) -> Self {
205        self.pending_block_kind = pending_block_kind;
206        self
207    }
208
209    /// Configures the raw transaction forwarder.
210    pub fn raw_tx_forwarder(mut self, tx_forwarder: Option<Url>) -> Self {
211        if let Some(tx_forwarder) = tx_forwarder {
212            self.raw_tx_forwarder.tx_forwarder = Some(tx_forwarder);
213        }
214        self
215    }
216
217    /// Configures the timeout duration for `send_raw_transaction_sync` RPC method.
218    pub const fn send_raw_transaction_sync_timeout(mut self, timeout: Duration) -> Self {
219        self.send_raw_transaction_sync_timeout = timeout;
220        self
221    }
222
223    /// Configures the maximum memory the EVM can allocate per RPC request.
224    pub const fn rpc_evm_memory_limit(mut self, memory_limit: u64) -> Self {
225        self.rpc_evm_memory_limit = memory_limit;
226        self
227    }
228}
229
230/// Config for the filter
231#[derive(Debug, Clone, PartialEq, Eq)]
232pub struct EthFilterConfig {
233    /// Maximum number of blocks that a filter can scan for logs.
234    ///
235    /// If `None` then no limit is enforced.
236    pub max_blocks_per_filter: Option<u64>,
237    /// Maximum number of logs that can be returned in a single response in `eth_getLogs` calls.
238    ///
239    /// If `None` then no limit is enforced.
240    pub max_logs_per_response: Option<usize>,
241    /// How long a filter remains valid after the last poll.
242    ///
243    /// A filter is considered stale if it has not been polled for longer than this duration and
244    /// will be removed.
245    pub stale_filter_ttl: Duration,
246}
247
248impl EthFilterConfig {
249    /// Sets the maximum number of blocks that a filter can scan for logs.
250    pub const fn max_blocks_per_filter(mut self, num: u64) -> Self {
251        self.max_blocks_per_filter = Some(num);
252        self
253    }
254
255    /// Sets the maximum number of logs that can be returned in a single response in `eth_getLogs`
256    /// calls.
257    pub const fn max_logs_per_response(mut self, num: usize) -> Self {
258        self.max_logs_per_response = Some(num);
259        self
260    }
261
262    /// Sets how long a filter remains valid after the last poll before it will be removed.
263    pub const fn stale_filter_ttl(mut self, duration: Duration) -> Self {
264        self.stale_filter_ttl = duration;
265        self
266    }
267}
268
269impl Default for EthFilterConfig {
270    fn default() -> Self {
271        Self {
272            max_blocks_per_filter: None,
273            max_logs_per_response: None,
274            // 5min
275            stale_filter_ttl: Duration::from_secs(5 * 60),
276        }
277    }
278}