diff --git a/Cargo.lock b/Cargo.lock index 59908c8..dd7ebb2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -836,6 +836,7 @@ dependencies = [ "eyre", "governor", "hex", + "mesc", "polars", "rand", "serde", @@ -859,6 +860,7 @@ dependencies = [ "indexmap 2.1.0", "indicatif", "lazy_static", + "mesc", "polars", "prefix-hex", "regex", @@ -2383,6 +2385,16 @@ dependencies = [ "autocfg", ] +[[package]] +name = "mesc" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc216c0f54636dcc09d9f6ec664f24927f423976f1c9a2f28b34df8bc71d14be" +dependencies = [ + "serde", + "serde_json", +] + [[package]] name = "mime" version = "0.3.17" diff --git a/Cargo.toml b/Cargo.toml index 2f5baf4..8949a9f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,7 @@ heck = "0.4.1" indexmap = "2.1.0" indicatif = "0.17.7" lazy_static = "1.4.0" +mesc = "0.1.0" polars = { version = "0.35.4", features = [ "parquet", "string_encoding", diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index 3421947..6506d48 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -28,6 +28,7 @@ ethers = { workspace = true } eyre = { workspace = true } governor = { workspace = true } hex = { workspace = true } +mesc = { workspace = true } polars = { workspace = true } rand = { workspace = true } serde = { workspace = true } diff --git a/crates/cli/src/args.rs b/crates/cli/src/args.rs index a147845..49eff61 100644 --- a/crates/cli/src/args.rs +++ b/crates/cli/src/args.rs @@ -86,7 +86,7 @@ pub struct Args { #[arg(long, help_heading = "Content Options")] pub exclude_failed: bool, - /// RPC url [default: ETH_RPC_URL env var] + /// RPC url [default: 1. MESC 2. ETH_RPC_URL] #[arg(short, long, help_heading = "Source Options")] pub rpc: Option, @@ -114,7 +114,7 @@ pub struct Args { #[arg(long, value_name = "M", help_heading = "Acquisition Options")] pub max_concurrent_chunks: Option, - /// Chunk collection order (normal, reverse, or random) + /// Chunk collection order (normal, reverse, random) #[arg(long, help_heading = "Acquisition Options")] pub chunk_order: Option, diff --git a/crates/cli/src/parse/source.rs b/crates/cli/src/parse/source.rs index a778037..c8e1a60 100644 --- a/crates/cli/src/parse/source.rs +++ b/crates/cli/src/parse/source.rs @@ -77,18 +77,33 @@ pub(crate) async fn parse_source(args: &Args) -> Result { } pub(crate) fn parse_rpc_url(args: &Args) -> Result { - let mut url = match &args.rpc { - Some(url) => url.clone(), - _ => match env::var("ETH_RPC_URL") { - Ok(url) => url, - Err(_e) => { - println!("must provide --rpc or set ETH_RPC_URL"); - std::process::exit(0); - } - }, + // get MESC url + let mesc_url = if mesc::is_mesc_enabled() { + let endpoint = match &args.rpc { + Some(url) => mesc::get_endpoint_by_query(url, Some("cryo"))?, + None => mesc::get_default_endpoint(Some("cryo"))?, + }; + endpoint.map(|endpoint| endpoint.url) + } else { + None }; + + // use ETH_RPC_URL if no MESC url found + let url = if let Some(url) = mesc_url { + url + } else if let Some(url) = &args.rpc { + url.clone() + } else if let Ok(url) = env::var("ETH_RPC_URL") { + url + } else { + let message = "must provide --rpc or setup MESC or set ETH_RPC_URL"; + return Err(ParseError::ParseError(message.to_string())) + }; + + // prepend http or https if need be if !url.starts_with("http") & !url.starts_with("ws") & !url.ends_with(".ipc") { - url = "http://".to_string() + url.as_str(); - }; - Ok(url) + Ok("http://".to_string() + url.as_str()) + } else { + Ok(url) + } } diff --git a/crates/cli/src/parse/timestamps.rs b/crates/cli/src/parse/timestamps.rs index 926216e..e6e26b4 100644 --- a/crates/cli/src/parse/timestamps.rs +++ b/crates/cli/src/parse/timestamps.rs @@ -330,7 +330,10 @@ mod tests { use ethers::prelude::*; async fn setup_source() -> Source { - let rpc_url = crate::parse::source::parse_rpc_url(&Args::default()).unwrap(); + let rpc_url = match crate::parse::source::parse_rpc_url(&Args::default()) { + Ok(url) => url, + Err(_) => std::process::exit(0), + }; let max_retry = 5; let initial_backoff = 500; let max_concurrent_requests = 100; diff --git a/crates/freeze/Cargo.toml b/crates/freeze/Cargo.toml index b5edde3..ac84529 100644 --- a/crates/freeze/Cargo.toml +++ b/crates/freeze/Cargo.toml @@ -21,6 +21,7 @@ heck = { workspace = true } indexmap = { workspace = true } indicatif = { workspace = true } lazy_static = { workspace = true } +mesc = { workspace = true } polars = { workspace = true } prefix-hex = { workspace = true } regex = { workspace = true } diff --git a/crates/freeze/src/types/errors.rs b/crates/freeze/src/types/errors.rs index f48495c..ebd9071 100644 --- a/crates/freeze/src/types/errors.rs +++ b/crates/freeze/src/types/errors.rs @@ -101,6 +101,16 @@ pub enum ParseError { /// Parse int error #[error("Parsing error")] ParseIntError(#[from] std::num::ParseIntError), + + /// MESC error + #[error("MESC error: {:?}", .0)] + MescError(mesc::MescError), +} + +impl From for ParseError { + fn from(err: mesc::MescError) -> Self { + ParseError::MescError(err) + } } /// Error performing a chunk operation