Skip to content

Commit

Permalink
feat: Add the ability to read the cl_node_urls from env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
MoeMahhouk committed Jul 13, 2024
1 parent db634fc commit 1d80972
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
4 changes: 3 additions & 1 deletion config-live-example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
23 changes: 21 additions & 2 deletions crates/rbuilder/src/live_builder/base_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<String>,
pub jsonrpc_server_port: u16,
pub jsonrpc_server_ip: Option<String>,
Expand Down Expand Up @@ -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<Vec<String>, D::Error>
where
D: serde::Deserializer<'de>,
{
#[serde_as]
#[derive(Deserialize)]
struct Helper(#[serde_as(deserialize_as = "OneOrMany<DisplayFromStr>")] Vec<String>);

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<String>) -> Ipv4Addr {
ip.as_ref().map_or(*DEFAULT_IP, |s| {
s.parse::<Ipv4Addr>().unwrap_or(*DEFAULT_IP)
Expand Down
7 changes: 7 additions & 0 deletions crates/rbuilder/src/live_builder/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand All @@ -266,6 +268,11 @@ mod test {
.address,
address!("75618c70B1BBF111F6660B0E3760387fb494102B")
);

assert!(config
.base_config
.cl_node_url
.contains(&"http://localhost:3500".to_string()));
}

#[test]
Expand Down

0 comments on commit 1d80972

Please sign in to comment.