From 2c7e5986c991561d95ad7548c8dd3ca57781e1b3 Mon Sep 17 00:00:00 2001 From: Jonathan LEI Date: Tue, 27 Aug 2024 08:46:09 +0800 Subject: [PATCH] refactor: move code out of main --- bin/host/src/cli.rs | 57 ++++++++++++++++++++++++++ bin/host/src/main.rs | 96 ++++++++++++++++++-------------------------- 2 files changed, 95 insertions(+), 58 deletions(-) create mode 100644 bin/host/src/cli.rs diff --git a/bin/host/src/cli.rs b/bin/host/src/cli.rs new file mode 100644 index 0000000..f25222e --- /dev/null +++ b/bin/host/src/cli.rs @@ -0,0 +1,57 @@ +use alloy_provider::{network::AnyNetwork, Provider as _, ReqwestProvider}; +use clap::Parser; +use url::Url; + +/// The arguments for configuring the chain data provider. +#[derive(Debug, Clone, Parser)] +pub struct ProviderArgs { + /// The rpc url used to fetch data about the block. If not provided, will use the + /// RPC_{chain_id} env var. + #[clap(long)] + rpc_url: Option, + /// The chain ID. If not provided, requires the rpc_url argument to be provided. + #[clap(long)] + chain_id: Option, +} + +pub struct ProviderConfig { + pub rpc_url: Option, + pub chain_id: u64, +} + +impl ProviderArgs { + pub async fn into_provider(self) -> eyre::Result { + // We don't need RPC when using cache with known chain ID, so we leave it as `Option` + // here and decide on whether to panic later. + // + // On the other hand chain ID is always needed. + let (rpc_url, chain_id) = match (self.rpc_url, self.chain_id) { + (Some(rpc_url), Some(chain_id)) => (Some(rpc_url), chain_id), + (None, Some(chain_id)) => { + match std::env::var(format!("RPC_{}", chain_id)) { + Ok(rpc_env_var) => { + // We don't always need it but if the value exists it has to be valid. + (Some(Url::parse(rpc_env_var.as_str()).expect("invalid rpc url")), chain_id) + } + Err(_) => { + // Not having RPC is okay because we know chain ID. + (None, chain_id) + } + } + } + (Some(rpc_url), None) => { + // We can find out about chain ID from RPC. + let provider: ReqwestProvider = + ReqwestProvider::new_http(rpc_url.clone()); + let chain_id = provider.get_chain_id().await?; + + (Some(rpc_url), chain_id) + } + (None, None) => { + eyre::bail!("either --rpc-url or --chain-id must be used") + } + }; + + Ok(ProviderConfig { rpc_url, chain_id }) + } +} diff --git a/bin/host/src/main.rs b/bin/host/src/main.rs index b583d30..cd06661 100644 --- a/bin/host/src/main.rs +++ b/bin/host/src/main.rs @@ -1,4 +1,4 @@ -use alloy_provider::{network::AnyNetwork, Provider, ReqwestProvider}; +use alloy_provider::ReqwestProvider; use clap::Parser; use reth_primitives::B256; use rsp_client_executor::{ @@ -10,24 +10,21 @@ use std::path::PathBuf; use tracing_subscriber::{ filter::EnvFilter, fmt, prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt, }; -use url::Url; mod execute; use execute::process_execution_report; +mod cli; +use cli::ProviderArgs; + /// The arguments for the host executable. #[derive(Debug, Clone, Parser)] struct HostArgs { /// The block number of the block to execute. #[clap(long)] block_number: u64, - /// The rpc url used to fetch data about the block. If not provided, will use the - /// RPC_{chain_id} env var. - #[clap(long)] - rpc_url: Option, - /// The chain ID. If not provided, requires the rpc_url argument to be provided. - #[clap(long)] - chain_id: Option, + #[clap(flatten)] + provider: ProviderArgs, /// Whether to generate a proof or just execute the block. #[clap(long)] prove: bool, @@ -54,62 +51,23 @@ async fn main() -> eyre::Result<()> { // Parse the command line arguments. let args = HostArgs::parse(); + let provider_config = args.provider.into_provider().await?; - // We don't need RPC when using cache with known chain ID, so we leave it as `Option` here - // and decide on whether to panic later. - // - // On the other hand chain ID is always needed. - let (rpc_url, chain_id) = match (args.rpc_url, args.chain_id) { - (Some(rpc_url), Some(chain_id)) => (Some(rpc_url), chain_id), - (None, Some(chain_id)) => { - match std::env::var(format!("RPC_{}", chain_id)) { - Ok(rpc_env_var) => { - // We don't always need it but if the value exists it has to be valid. - (Some(Url::parse(rpc_env_var.as_str()).expect("invalid rpc url")), chain_id) - } - Err(_) => { - // Not having RPC is okay because we know chain ID. - (None, chain_id) - } - } - } - (Some(rpc_url), None) => { - // We can find out about chain ID from RPC. - let provider: ReqwestProvider = ReqwestProvider::new_http(rpc_url.clone()); - let chain_id = provider.get_chain_id().await?; - - (Some(rpc_url), chain_id) - } - (None, None) => { - eyre::bail!("either --rpc-url or --chain-id must be used") - } - }; - - let variant = match chain_id { + let variant = match provider_config.chain_id { CHAIN_ID_ETH_MAINNET => ChainVariant::Ethereum, CHAIN_ID_OP_MAINNET => ChainVariant::Optimism, _ => { - eyre::bail!("unknown chain ID: {}", chain_id); + eyre::bail!("unknown chain ID: {}", provider_config.chain_id); } }; - let client_input_from_cache = if let Some(cache_dir) = args.cache_dir.as_ref() { - let cache_path = cache_dir.join(format!("input/{}/{}.bin", chain_id, args.block_number)); - - if cache_path.exists() { - // TODO: prune the cache if invalid instead - let mut cache_file = std::fs::File::open(cache_path)?; - let client_input: ClientExecutorInput = bincode::deserialize_from(&mut cache_file)?; + let client_input_from_cache = try_load_input_from_cache( + args.cache_dir.as_ref(), + provider_config.chain_id, + args.block_number, + )?; - Some(client_input) - } else { - None - } - } else { - None - }; - - let client_input = match (client_input_from_cache, rpc_url) { + let client_input = match (client_input_from_cache, provider_config.rpc_url) { (Some(client_input_from_cache), _) => client_input_from_cache, (None, Some(rpc_url)) => { // Cache not found but we have RPC @@ -126,7 +84,7 @@ async fn main() -> eyre::Result<()> { .expect("failed to execute host"); if let Some(cache_dir) = args.cache_dir { - let input_folder = cache_dir.join(format!("input/{}", chain_id)); + let input_folder = cache_dir.join(format!("input/{}", provider_config.chain_id)); if !input_folder.exists() { std::fs::create_dir_all(&input_folder)?; } @@ -184,3 +142,25 @@ async fn main() -> eyre::Result<()> { Ok(()) } + +fn try_load_input_from_cache( + cache_dir: Option<&PathBuf>, + chain_id: u64, + block_number: u64, +) -> eyre::Result> { + Ok(if let Some(cache_dir) = cache_dir { + let cache_path = cache_dir.join(format!("input/{}/{}.bin", chain_id, block_number)); + + if cache_path.exists() { + // TODO: prune the cache if invalid instead + let mut cache_file = std::fs::File::open(cache_path)?; + let client_input: ClientExecutorInput = bincode::deserialize_from(&mut cache_file)?; + + Some(client_input) + } else { + None + } + } else { + None + }) +}