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