|
| 1 | +use reth::args::RpcServerArgs; |
| 2 | +use signet_rpc::{ServeConfig, StorageRpcConfig}; |
| 3 | +use std::net::SocketAddr; |
| 4 | + |
| 5 | +/// Extract [`StorageRpcConfig`] values from reth's host RPC settings. |
| 6 | +/// |
| 7 | +/// Fields with no reth equivalent retain their defaults. |
| 8 | +pub fn rpc_config_from_args(args: &RpcServerArgs) -> StorageRpcConfig { |
| 9 | + let gpo = &args.gas_price_oracle; |
| 10 | + StorageRpcConfig::builder() |
| 11 | + .rpc_gas_cap(args.rpc_gas_cap) |
| 12 | + .max_tracing_requests(args.rpc_max_tracing_requests) |
| 13 | + .gas_oracle_block_count(u64::from(gpo.blocks)) |
| 14 | + .gas_oracle_percentile(f64::from(gpo.percentile)) |
| 15 | + .ignore_price(Some(u128::from(gpo.ignore_price))) |
| 16 | + .max_price(Some(u128::from(gpo.max_price))) |
| 17 | + .build() |
| 18 | +} |
| 19 | + |
| 20 | +/// Convert reth [`RpcServerArgs`] into a reth-free [`ServeConfig`]. |
| 21 | +pub fn serve_config_from_args(args: &RpcServerArgs) -> ServeConfig { |
| 22 | + let http = |
| 23 | + if args.http { vec![SocketAddr::from((args.http_addr, args.http_port))] } else { vec![] }; |
| 24 | + let ws = if args.ws { vec![SocketAddr::from((args.ws_addr, args.ws_port))] } else { vec![] }; |
| 25 | + let ipc = if !args.ipcdisable { Some(args.ipcpath.clone()) } else { None }; |
| 26 | + |
| 27 | + ServeConfig { |
| 28 | + http, |
| 29 | + http_cors: args.http_corsdomain.clone(), |
| 30 | + ws, |
| 31 | + ws_cors: args.ws_allowed_origins.clone(), |
| 32 | + ipc, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +#[cfg(test)] |
| 37 | +mod tests { |
| 38 | + use crate::config::{rpc_config_from_args, serve_config_from_args}; |
| 39 | + use reth::args::RpcServerArgs; |
| 40 | + |
| 41 | + #[test] |
| 42 | + fn rpc_config_from_default_args() { |
| 43 | + let args = RpcServerArgs::default(); |
| 44 | + let gpo = &args.gas_price_oracle; |
| 45 | + let config = rpc_config_from_args(&args); |
| 46 | + |
| 47 | + assert_eq!(config.rpc_gas_cap, args.rpc_gas_cap); |
| 48 | + assert_eq!(config.max_tracing_requests, args.rpc_max_tracing_requests); |
| 49 | + assert_eq!(config.gas_oracle_block_count, u64::from(gpo.blocks)); |
| 50 | + assert_eq!(config.gas_oracle_percentile, f64::from(gpo.percentile)); |
| 51 | + assert_eq!(config.ignore_price, Some(u128::from(gpo.ignore_price))); |
| 52 | + assert_eq!(config.max_price, Some(u128::from(gpo.max_price))); |
| 53 | + } |
| 54 | + |
| 55 | + #[test] |
| 56 | + fn serve_config_http_disabled_by_default() { |
| 57 | + let args = RpcServerArgs::default(); |
| 58 | + let config = serve_config_from_args(&args); |
| 59 | + |
| 60 | + assert!(config.http.is_empty()); |
| 61 | + assert!(config.ws.is_empty()); |
| 62 | + } |
| 63 | + |
| 64 | + #[test] |
| 65 | + fn serve_config_http_enabled() { |
| 66 | + let args = RpcServerArgs { http: true, ..Default::default() }; |
| 67 | + let config = serve_config_from_args(&args); |
| 68 | + |
| 69 | + assert_eq!(config.http.len(), 1); |
| 70 | + assert_eq!(config.http[0].port(), args.http_port); |
| 71 | + } |
| 72 | + |
| 73 | + #[test] |
| 74 | + fn serve_config_ws_enabled() { |
| 75 | + let args = RpcServerArgs { ws: true, ..Default::default() }; |
| 76 | + let config = serve_config_from_args(&args); |
| 77 | + |
| 78 | + assert_eq!(config.ws.len(), 1); |
| 79 | + assert_eq!(config.ws[0].port(), args.ws_port); |
| 80 | + } |
| 81 | + |
| 82 | + #[test] |
| 83 | + fn serve_config_ipc_enabled_by_default() { |
| 84 | + let args = RpcServerArgs::default(); |
| 85 | + let config = serve_config_from_args(&args); |
| 86 | + |
| 87 | + assert!(config.ipc.is_some()); |
| 88 | + } |
| 89 | +} |
0 commit comments