Skip to main content

reth_rpc_builder/
config.rs

1use jsonrpsee::server::ServerConfigBuilder;
2use reth_node_core::{args::RpcServerArgs, utils::get_or_create_jwt_secret_from_path};
3use reth_rpc::ValidationApiConfig;
4use reth_rpc_eth_types::{EthConfig, EthStateCacheConfig, GasPriceOracleConfig};
5use reth_rpc_layer::{JwtError, JwtSecret};
6use reth_rpc_server_types::RpcModuleSelection;
7use std::{net::SocketAddr, path::PathBuf};
8use tower::layer::util::Identity;
9use tracing::{debug, warn};
10
11use crate::{
12    auth::AuthServerConfig, error::RpcError, IpcServerBuilder, RpcModuleConfig, RpcServerConfig,
13    TransportRpcModuleConfig,
14};
15
16/// A trait that provides a configured RPC server.
17///
18/// This provides all basic config values for the RPC server and is implemented by the
19/// [`RpcServerArgs`] type.
20pub trait RethRpcServerConfig {
21    /// Returns whether ipc is enabled.
22    fn is_ipc_enabled(&self) -> bool;
23
24    /// Returns the path to the target ipc socket if enabled.
25    fn ipc_path(&self) -> &str;
26
27    /// The configured ethereum RPC settings.
28    fn eth_config(&self) -> EthConfig;
29
30    /// The configured ethereum RPC settings.
31    fn flashbots_config(&self) -> ValidationApiConfig;
32
33    /// Returns state cache configuration.
34    fn state_cache_config(&self) -> EthStateCacheConfig;
35
36    /// Returns the max request size in bytes.
37    fn rpc_max_request_size_bytes(&self) -> u32;
38
39    /// Returns the max response size in bytes.
40    fn rpc_max_response_size_bytes(&self) -> u32;
41
42    /// Extracts the gas price oracle config from the args.
43    fn gas_price_oracle_config(&self) -> GasPriceOracleConfig;
44
45    /// Creates the [`TransportRpcModuleConfig`] from cli args.
46    ///
47    /// This sets all the api modules, and configures additional settings like gas price oracle
48    /// settings in the [`TransportRpcModuleConfig`].
49    fn transport_rpc_module_config(&self) -> TransportRpcModuleConfig;
50
51    /// Returns the default server config for http/ws
52    fn http_ws_server_builder(&self) -> ServerConfigBuilder;
53
54    /// Returns the default ipc server builder
55    fn ipc_server_builder(&self) -> IpcServerBuilder<Identity, Identity>;
56
57    /// Creates the [`RpcServerConfig`] from cli args.
58    fn rpc_server_config(&self) -> RpcServerConfig;
59
60    /// Returns whether built-in RPC request metrics are enabled.
61    fn rpc_metrics_enabled(&self) -> bool;
62
63    /// Creates the [`AuthServerConfig`] from cli args.
64    fn auth_server_config(&self, jwt_secret: JwtSecret) -> Result<AuthServerConfig, RpcError>;
65
66    /// The execution layer and consensus layer clients SHOULD accept a configuration parameter:
67    /// jwt-secret, which designates a file containing the hex-encoded 256 bit secret key to be used
68    /// for verifying/generating JWT tokens.
69    ///
70    /// If such a parameter is given, but the file cannot be read, or does not contain a hex-encoded
71    /// key of 256 bits, the client SHOULD treat this as an error.
72    ///
73    /// If such a parameter is not given, the client SHOULD generate such a token, valid for the
74    /// duration of the execution, and SHOULD store the hex-encoded secret as a jwt.hex file on
75    /// the filesystem. This file can then be used to provision the counterpart client.
76    ///
77    /// The `default_jwt_path` provided as an argument will be used as the default location for the
78    /// jwt secret in case the `auth_jwtsecret` argument is not provided.
79    fn auth_jwt_secret(&self, default_jwt_path: PathBuf) -> Result<JwtSecret, JwtError>;
80
81    /// Returns the configured jwt secret key for the regular rpc servers, if any.
82    ///
83    /// Note: this is not used for the auth server (engine API).
84    fn rpc_secret_key(&self) -> Option<JwtSecret>;
85}
86
87impl RethRpcServerConfig for RpcServerArgs {
88    fn is_ipc_enabled(&self) -> bool {
89        // By default IPC is enabled therefore it is enabled if the `ipcdisable` is false.
90        !self.ipcdisable
91    }
92
93    fn ipc_path(&self) -> &str {
94        self.ipcpath.as_str()
95    }
96
97    fn eth_config(&self) -> EthConfig {
98        EthConfig::default()
99            .max_tracing_requests(self.rpc_max_tracing_requests)
100            .max_blocking_io_requests(self.rpc_max_blocking_io_requests)
101            .max_trace_filter_blocks(self.rpc_max_trace_filter_blocks)
102            .max_blocks_per_filter(self.rpc_max_blocks_per_filter.unwrap_or_max())
103            .max_logs_per_response(self.rpc_max_logs_per_response.unwrap_or_max() as usize)
104            .eth_proof_window(self.rpc_eth_proof_window)
105            .rpc_gas_cap(self.rpc_gas_cap)
106            .rpc_max_simulate_blocks(self.rpc_max_simulate_blocks)
107            .compute_state_root_for_eth_simulate(self.rpc_compute_state_root_for_eth_simulate)
108            .state_cache(self.state_cache_config())
109            .gpo_config(self.gas_price_oracle_config())
110            .proof_permits(self.rpc_proof_permits)
111            .pending_block_kind(self.rpc_pending_block)
112            .raw_tx_forwarder(self.rpc_forwarder.clone())
113            .rpc_evm_memory_limit(self.rpc_evm_memory_limit)
114            .force_blob_sidecar_upcasting(self.rpc_force_blob_sidecar_upcasting)
115    }
116
117    fn flashbots_config(&self) -> ValidationApiConfig {
118        ValidationApiConfig {
119            disallow: self.builder_disallow.clone().unwrap_or_default(),
120            validation_window: self.rpc_eth_proof_window,
121        }
122    }
123
124    fn state_cache_config(&self) -> EthStateCacheConfig {
125        EthStateCacheConfig {
126            max_blocks: self.rpc_state_cache.max_blocks,
127            max_receipts: self.rpc_state_cache.max_receipts,
128            max_headers: self.rpc_state_cache.max_headers,
129            max_bals: self.rpc_state_cache.max_bals,
130            max_concurrent_db_requests: self.rpc_state_cache.max_concurrent_db_requests,
131            max_cached_tx_hashes: self.rpc_state_cache.max_cached_tx_hashes,
132        }
133    }
134
135    fn rpc_max_request_size_bytes(&self) -> u32 {
136        self.rpc_max_request_size.get().saturating_mul(1024 * 1024)
137    }
138
139    fn rpc_max_response_size_bytes(&self) -> u32 {
140        self.rpc_max_response_size.get().saturating_mul(1024 * 1024)
141    }
142
143    fn gas_price_oracle_config(&self) -> GasPriceOracleConfig {
144        self.gas_price_oracle.gas_price_oracle_config()
145    }
146
147    fn transport_rpc_module_config(&self) -> TransportRpcModuleConfig {
148        let mut config = TransportRpcModuleConfig::default()
149            .with_config(RpcModuleConfig::new(self.eth_config()));
150
151        if self.http {
152            config = config.with_http(
153                self.http_api
154                    .clone()
155                    .unwrap_or_else(|| RpcModuleSelection::standard_modules().into()),
156            );
157        }
158
159        if self.ws {
160            config = config.with_ws(
161                self.ws_api
162                    .clone()
163                    .unwrap_or_else(|| RpcModuleSelection::standard_modules().into()),
164            );
165        }
166
167        if self.is_ipc_enabled() {
168            config = config.with_ipc(RpcModuleSelection::default_ipc_modules());
169        }
170
171        config
172    }
173
174    fn http_ws_server_builder(&self) -> ServerConfigBuilder {
175        ServerConfigBuilder::new()
176            .max_connections(self.rpc_max_connections.get())
177            .max_request_body_size(self.rpc_max_request_size_bytes())
178            .max_response_body_size(self.rpc_max_response_size_bytes())
179            .max_subscriptions_per_connection(self.rpc_max_subscriptions_per_connection.get())
180    }
181
182    fn ipc_server_builder(&self) -> IpcServerBuilder<Identity, Identity> {
183        IpcServerBuilder::default()
184            .max_subscriptions_per_connection(self.rpc_max_subscriptions_per_connection.get())
185            .max_request_body_size(self.rpc_max_request_size_bytes())
186            .max_response_body_size(self.rpc_max_response_size_bytes())
187            .max_connections(self.rpc_max_connections.get())
188            .set_ipc_socket_permissions(self.ipc_socket_permissions.clone())
189    }
190
191    fn rpc_server_config(&self) -> RpcServerConfig {
192        let mut config = RpcServerConfig::default()
193            .with_jwt_secret(self.rpc_secret_key())
194            .with_rpc_metrics_enabled(self.rpc_metrics_enabled());
195
196        if self.http_api.is_some() && !self.http {
197            warn!(
198                target: "reth::cli",
199                "The --http.api flag is set but --http is not enabled. HTTP RPC API will not be exposed."
200            );
201        }
202
203        if self.ws_api.is_some() && !self.ws {
204            warn!(
205                target: "reth::cli",
206                "The --ws.api flag is set but --ws is not enabled. WS RPC API will not be exposed."
207            );
208        }
209
210        if self.http {
211            let socket_address = SocketAddr::new(self.http_addr, self.http_port);
212            config = config
213                .with_http_address(socket_address)
214                .with_http(self.http_ws_server_builder())
215                .with_http_cors(self.http_corsdomain.clone())
216                .with_http_disable_compression(self.http_disable_compression);
217        }
218
219        if self.ws {
220            let socket_address = SocketAddr::new(self.ws_addr, self.ws_port);
221            // Ensure WS CORS is applied regardless of HTTP being enabled
222            config = config
223                .with_ws_address(socket_address)
224                .with_ws(self.http_ws_server_builder())
225                .with_ws_cors(self.ws_allowed_origins.clone());
226        }
227
228        if self.is_ipc_enabled() {
229            config =
230                config.with_ipc(self.ipc_server_builder()).with_ipc_endpoint(self.ipcpath.clone());
231        }
232
233        config
234    }
235
236    fn rpc_metrics_enabled(&self) -> bool {
237        !self.rpc_disable_metrics
238    }
239
240    fn auth_server_config(&self, jwt_secret: JwtSecret) -> Result<AuthServerConfig, RpcError> {
241        let address = SocketAddr::new(self.auth_addr, self.auth_port);
242
243        let mut builder = AuthServerConfig::builder(jwt_secret).socket_addr(address);
244        if self.auth_ipc {
245            builder = builder
246                .ipc_endpoint(self.auth_ipc_path.clone())
247                .with_ipc_config(self.ipc_server_builder());
248        }
249        Ok(builder.build())
250    }
251
252    fn auth_jwt_secret(&self, default_jwt_path: PathBuf) -> Result<JwtSecret, JwtError> {
253        match self.auth_jwtsecret.as_ref() {
254            Some(fpath) => {
255                debug!(target: "reth::cli", user_path=?fpath, "Reading JWT auth secret file");
256                JwtSecret::from_file(fpath)
257            }
258            None => get_or_create_jwt_secret_from_path(&default_jwt_path),
259        }
260    }
261
262    fn rpc_secret_key(&self) -> Option<JwtSecret> {
263        self.rpc_jwtsecret
264    }
265}
266
267#[cfg(test)]
268mod tests {
269    use clap::{Args, Parser};
270    use reth_node_core::args::RpcServerArgs;
271    use reth_rpc_eth_types::RPC_DEFAULT_GAS_CAP;
272    use reth_rpc_server_types::{constants, RethRpcModule, RpcModuleSelection};
273    use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
274
275    use crate::config::RethRpcServerConfig;
276
277    /// A helper type to parse Args more easily
278    #[derive(Parser)]
279    struct CommandParser<T: Args> {
280        #[command(flatten)]
281        args: T,
282    }
283
284    #[test]
285    fn test_rpc_gas_cap() {
286        let args = CommandParser::<RpcServerArgs>::parse_from(["reth"]).args;
287        let config = args.eth_config();
288        assert_eq!(config.rpc_gas_cap, u64::from(RPC_DEFAULT_GAS_CAP));
289
290        let args =
291            CommandParser::<RpcServerArgs>::parse_from(["reth", "--rpc.gascap", "1000"]).args;
292        let config = args.eth_config();
293        assert_eq!(config.rpc_gas_cap, 1000);
294
295        let args = CommandParser::<RpcServerArgs>::try_parse_from(["reth", "--rpc.gascap", "0"]);
296        assert!(args.is_err());
297    }
298
299    #[test]
300    fn test_transport_rpc_module_config() {
301        let args = CommandParser::<RpcServerArgs>::parse_from([
302            "reth",
303            "--http.api",
304            "eth,admin,debug",
305            "--http",
306            "--ws",
307        ])
308        .args;
309        let config = args.transport_rpc_module_config();
310        let expected = [RethRpcModule::Eth, RethRpcModule::Admin, RethRpcModule::Debug];
311        assert_eq!(config.http().cloned().unwrap().into_selection(), expected.into());
312        assert_eq!(
313            config.ws().cloned().unwrap().into_selection(),
314            RpcModuleSelection::standard_modules()
315        );
316    }
317
318    #[test]
319    fn test_transport_rpc_module_trim_config() {
320        let args = CommandParser::<RpcServerArgs>::parse_from([
321            "reth",
322            "--http.api",
323            " eth, admin, debug",
324            "--http",
325            "--ws",
326        ])
327        .args;
328        let config = args.transport_rpc_module_config();
329        let expected = [RethRpcModule::Eth, RethRpcModule::Admin, RethRpcModule::Debug];
330        assert_eq!(config.http().cloned().unwrap().into_selection(), expected.into());
331        assert_eq!(
332            config.ws().cloned().unwrap().into_selection(),
333            RpcModuleSelection::standard_modules()
334        );
335    }
336
337    #[test]
338    fn test_unique_rpc_modules() {
339        let args = CommandParser::<RpcServerArgs>::parse_from([
340            "reth",
341            "--http.api",
342            " eth, admin, debug, eth,admin",
343            "--http",
344            "--ws",
345        ])
346        .args;
347        let config = args.transport_rpc_module_config();
348        let expected = [RethRpcModule::Eth, RethRpcModule::Admin, RethRpcModule::Debug];
349        assert_eq!(config.http().cloned().unwrap().into_selection(), expected.into());
350        assert_eq!(
351            config.ws().cloned().unwrap().into_selection(),
352            RpcModuleSelection::standard_modules()
353        );
354    }
355
356    #[test]
357    fn test_rpc_server_config() {
358        let args = CommandParser::<RpcServerArgs>::parse_from([
359            "reth",
360            "--http.api",
361            "eth,admin,debug",
362            "--http",
363            "--ws",
364            "--ws.addr",
365            "127.0.0.1",
366            "--ws.port",
367            "8888",
368        ])
369        .args;
370        let config = args.rpc_server_config();
371        assert_eq!(
372            config.http_address().unwrap(),
373            SocketAddr::V4(SocketAddrV4::new(
374                Ipv4Addr::LOCALHOST,
375                constants::DEFAULT_HTTP_RPC_PORT
376            ))
377        );
378        assert_eq!(
379            config.ws_address().unwrap(),
380            SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8888))
381        );
382        assert_eq!(config.ipc_endpoint().unwrap(), constants::DEFAULT_IPC_ENDPOINT);
383        assert!(config.rpc_metrics_enabled());
384    }
385
386    #[test]
387    fn test_rpc_server_config_disable_metrics() {
388        let args =
389            CommandParser::<RpcServerArgs>::parse_from(["reth", "--rpc.disable-metrics"]).args;
390        let config = args.rpc_server_config();
391        assert!(!config.rpc_metrics_enabled());
392    }
393
394    #[test]
395    fn test_zero_filter_limits() {
396        let args = CommandParser::<RpcServerArgs>::parse_from([
397            "reth",
398            "--rpc-max-blocks-per-filter",
399            "0",
400            "--rpc-max-logs-per-response",
401            "0",
402        ])
403        .args;
404
405        let config = args.eth_config().filter_config();
406        assert_eq!(config.max_blocks_per_filter, Some(u64::MAX));
407        assert_eq!(config.max_logs_per_response, Some(usize::MAX));
408    }
409
410    #[test]
411    fn test_custom_filter_limits() {
412        let args = CommandParser::<RpcServerArgs>::parse_from([
413            "reth",
414            "--rpc-max-blocks-per-filter",
415            "100",
416            "--rpc-max-logs-per-response",
417            "200",
418        ])
419        .args;
420
421        let config = args.eth_config().filter_config();
422        assert_eq!(config.max_blocks_per_filter, Some(100));
423        assert_eq!(config.max_logs_per_response, Some(200));
424    }
425}