Skip to content

Commit

Permalink
refactor: Use Ipv4Addr directly in config instead of Option<String>
Browse files Browse the repository at this point in the history
  • Loading branch information
ferranbt committed Dec 31, 2024
1 parent 821a88c commit 8af5564
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 32 deletions.
4 changes: 2 additions & 2 deletions crates/rbuilder/src/bin/dummy-builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use rbuilder::{
},
live_builder::{
base_config::{
DEFAULT_EL_NODE_IPC_PATH, DEFAULT_INCOMING_BUNDLES_PORT, DEFAULT_IP,
default_ip, DEFAULT_EL_NODE_IPC_PATH, DEFAULT_INCOMING_BUNDLES_PORT,
DEFAULT_RETH_DB_PATH,
},
config::create_provider_factory,
Expand Down Expand Up @@ -80,7 +80,7 @@ async fn main() -> eyre::Result<()> {
true,
DEFAULT_EL_NODE_IPC_PATH.parse().unwrap(),
DEFAULT_INCOMING_BUNDLES_PORT,
*DEFAULT_IP,
default_ip(),
DEFAULT_SERVE_MAX_CONNECTIONS,
DEFAULT_RESULTS_CHANNEL_TIMEOUT,
DEFAULT_INPUT_CHANNEL_BUFFER_SIZE,
Expand Down
44 changes: 15 additions & 29 deletions crates/rbuilder/src/live_builder/base_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use ahash::HashSet;
use alloy_primitives::{Address, B256};
use eyre::{eyre, Context};
use jsonrpsee::RpcModule;
use lazy_static::lazy_static;
use reth::chainspec::chain_value_parser;
use reth_chainspec::ChainSpec;
use reth_db::{Database, DatabaseEnv};
Expand Down Expand Up @@ -49,9 +48,13 @@ const ENV_PREFIX: &str = "env:";
#[serde(default, deny_unknown_fields)]
pub struct BaseConfig {
pub full_telemetry_server_port: u16,
pub full_telemetry_server_ip: Option<String>,
#[serde(default = "default_ip")]
pub full_telemetry_server_ip: Ipv4Addr,

pub redacted_telemetry_server_port: u16,
pub redacted_telemetry_server_ip: Option<String>,
#[serde(default = "default_ip")]
pub redacted_telemetry_server_ip: Ipv4Addr,

pub log_json: bool,
log_level: EnvOrValue<String>,
pub log_color: bool,
Expand All @@ -66,7 +69,8 @@ pub struct BaseConfig {

pub el_node_ipc_path: PathBuf,
pub jsonrpc_server_port: u16,
pub jsonrpc_server_ip: Option<String>,
#[serde(default = "default_ip")]
pub jsonrpc_server_ip: Ipv4Addr,

pub ignore_cancellable_orders: bool,
pub ignore_blobs: bool,
Expand Down Expand Up @@ -109,14 +113,8 @@ pub struct BaseConfig {
pub backtest_protect_bundle_signers: Vec<Address>,
}

lazy_static! {
pub static ref DEFAULT_IP: Ipv4Addr = Ipv4Addr::new(0, 0, 0, 0);
}

fn parse_ip(ip: &Option<String>) -> Ipv4Addr {
ip.as_ref().map_or(*DEFAULT_IP, |s| {
s.parse::<Ipv4Addr>().unwrap_or(*DEFAULT_IP)
})
pub fn default_ip() -> Ipv4Addr {
Ipv4Addr::new(0, 0, 0, 0)
}

/// Loads config from toml file, some values can be loaded from env variables with the following syntax
Expand Down Expand Up @@ -158,14 +156,14 @@ impl BaseConfig {

pub fn redacted_telemetry_server_address(&self) -> SocketAddr {
SocketAddr::V4(SocketAddrV4::new(
self.redacted_telemetry_server_ip(),
self.redacted_telemetry_server_ip,
self.redacted_telemetry_server_port,
))
}

pub fn full_telemetry_server_address(&self) -> SocketAddr {
SocketAddr::V4(SocketAddrV4::new(
self.full_telemetry_server_ip(),
self.full_telemetry_server_ip,
self.full_telemetry_server_port,
))
}
Expand Down Expand Up @@ -239,18 +237,6 @@ impl BaseConfig {
})
}

pub fn jsonrpc_server_ip(&self) -> Ipv4Addr {
parse_ip(&self.jsonrpc_server_ip)
}

pub fn redacted_telemetry_server_ip(&self) -> Ipv4Addr {
parse_ip(&self.redacted_telemetry_server_ip)
}

pub fn full_telemetry_server_ip(&self) -> Ipv4Addr {
parse_ip(&self.full_telemetry_server_ip)
}

pub fn chain_spec(&self) -> eyre::Result<Arc<ChainSpec>> {
chain_value_parser(&self.chain)
}
Expand Down Expand Up @@ -430,9 +416,9 @@ impl Default for BaseConfig {
fn default() -> Self {
Self {
full_telemetry_server_port: 6069,
full_telemetry_server_ip: None,
full_telemetry_server_ip: default_ip(),
redacted_telemetry_server_port: 6070,
redacted_telemetry_server_ip: None,
redacted_telemetry_server_ip: default_ip(),
log_json: false,
log_level: "info".into(),
log_color: false,
Expand All @@ -442,7 +428,7 @@ impl Default for BaseConfig {
flashbots_db: None,
el_node_ipc_path: "/tmp/reth.ipc".parse().unwrap(),
jsonrpc_server_port: DEFAULT_INCOMING_BUNDLES_PORT,
jsonrpc_server_ip: None,
jsonrpc_server_ip: default_ip(),
ignore_cancellable_orders: true,
ignore_blobs: false,
chain: "mainnet".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion crates/rbuilder/src/live_builder/order_input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl OrderInputConfig {
ignore_blobs: config.ignore_blobs,
ipc_path: el_node_ipc_path,
server_port: config.jsonrpc_server_port,
server_ip: config.jsonrpc_server_ip(),
server_ip: config.jsonrpc_server_ip,
serve_max_connections: 4096,
results_channel_timeout: Duration::from_millis(50),
input_channel_buffer_size: 10_000,
Expand Down

0 comments on commit 8af5564

Please sign in to comment.