From 1d809726de694680ccec63a91150e97635c61047 Mon Sep 17 00:00:00 2001 From: MoeMahhouk Date: Thu, 11 Jul 2024 23:14:55 +0000 Subject: [PATCH] feat: Add the ability to read the cl_node_urls from env variables --- config-live-example.toml | 4 +++- .../rbuilder/src/live_builder/base_config.rs | 23 +++++++++++++++++-- crates/rbuilder/src/live_builder/config.rs | 7 ++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/config-live-example.toml b/config-live-example.toml index 65c4e4fc..69add4ff 100644 --- a/config-live-example.toml +++ b/config-live-example.toml @@ -8,7 +8,9 @@ reth_datadir = "/mnt/data/reth" coinbase_secret_key = "env:COINBASE_SECRET_KEY" -cl_node_url = ["http://localhost:3500"] +#cl_node_url can be a single value, array of values, or passed by an environment variables with values separated with a comma +#cl_node_url = "http://localhost:3500" +cl_node_url = ["env:CL_NODE_URL"] jsonrpc_server_port = 8645 jsonrpc_server_ip = "0.0.0.0" el_node_ipc_path = "/tmp/reth.ipc" diff --git a/crates/rbuilder/src/live_builder/base_config.rs b/crates/rbuilder/src/live_builder/base_config.rs index 398cde00..39a2d78a 100644 --- a/crates/rbuilder/src/live_builder/base_config.rs +++ b/crates/rbuilder/src/live_builder/base_config.rs @@ -29,7 +29,7 @@ use reth::{ use reth_db::DatabaseEnv; use reth_primitives::format_ether; use serde::Deserialize; -use serde_with::{serde_as, OneOrMany}; +use serde_with::{serde_as, DisplayFromStr, OneOrMany}; use sqlx::PgPool; use std::{ env::var, @@ -66,7 +66,7 @@ pub struct BaseConfig { pub el_node_ipc_path: PathBuf, ///Name kept singular for backwards compatibility - #[serde_as(deserialize_as = "OneOrMany<_>")] + #[serde(deserialize_with = "deserialize_cl_url")] pub cl_node_url: Vec, pub jsonrpc_server_port: u16, pub jsonrpc_server_ip: Option, @@ -131,6 +131,25 @@ lazy_static! { pub static ref DEFAULT_IP: Ipv4Addr = Ipv4Addr::new(0, 0, 0, 0); } +fn deserialize_cl_url<'de, D>(deserializer: D) -> Result, D::Error> +where + D: serde::Deserializer<'de>, +{ + #[serde_as] + #[derive(Deserialize)] + struct Helper(#[serde_as(deserialize_as = "OneOrMany")] Vec); + + let helper = Helper::deserialize(deserializer)?; + if helper.0.len() == 1 && helper.0[0].starts_with("env:") { + // Parse from environment variable + let env_var = helper.0[0].trim_start_matches("env:"); + if let Ok(urls) = var(env_var) { + return Ok(urls.split(',').map(String::from).collect()); + } + } + Ok(helper.0) +} + fn parse_ip(ip: &Option) -> Ipv4Addr { ip.as_ref().map_or(*DEFAULT_IP, |s| { s.parse::().unwrap_or(*DEFAULT_IP) diff --git a/crates/rbuilder/src/live_builder/config.rs b/crates/rbuilder/src/live_builder/config.rs index 879e63b5..d3c8b915 100644 --- a/crates/rbuilder/src/live_builder/config.rs +++ b/crates/rbuilder/src/live_builder/config.rs @@ -256,6 +256,8 @@ mod test { "0xb785cd753d62bb25c0afaf75fd40dd94bf295051fdadc972ec857ad6b29cfa72", ); + env::set_var("CL_NODE_URL", "http://localhost:3500"); + let config: Config = load_config_toml_and_env(p).expect("Config load"); assert_eq!( @@ -266,6 +268,11 @@ mod test { .address, address!("75618c70B1BBF111F6660B0E3760387fb494102B") ); + + assert!(config + .base_config + .cl_node_url + .contains(&"http://localhost:3500".to_string())); } #[test]