diff --git a/src/l1_fetcher.rs b/src/l1_fetcher.rs index e5f6492..a39d069 100644 --- a/src/l1_fetcher.rs +++ b/src/l1_fetcher.rs @@ -14,6 +14,7 @@ use tokio::{ }; use crate::{ + cli::L1FetcherOptions, constants::ethereum::{BLOCK_STEP, GENESIS_BLOCK, ZK_SYNC_ADDR}, snapshot::StateSnapshot, types::{CommitBlockInfoV1, ParseError}, @@ -80,13 +81,17 @@ impl L1Metrics { pub struct L1Fetcher { provider: Provider, contract: Contract, + config: L1FetcherOptions, snapshot: Option>>, } impl L1Fetcher { - pub fn new(http_url: &str, snapshot: Option>>) -> Result { - let provider = - Provider::::try_from(http_url).expect("could not instantiate HTTP Provider"); + pub fn new( + config: L1FetcherOptions, + snapshot: Option>>, + ) -> Result { + let provider = Provider::::try_from(&config.http_url) + .expect("could not instantiate HTTP Provider"); let abi_file = std::fs::File::open("./IZkSync.json")?; let contract = Contract::load(abi_file)?; @@ -94,22 +99,17 @@ impl L1Fetcher { Ok(L1Fetcher { provider, contract, + config, snapshot, }) } #[allow(clippy::too_many_lines)] - pub async fn fetch( - &self, - sink: mpsc::Sender, - start_block: Option, - end_block: Option, - mut disable_polling: bool, - ) -> Result<()> { + pub async fn run(&self, sink: mpsc::Sender) -> Result<()> { // Start fetching from the `GENESIS_BLOCK` unless the `start_block` argument is supplied, // in which case, start from that instead. If no argument was supplied and a state snapshot // exists, start from the block number specified in that snapshot. - let mut current_l1_block_number = start_block.unwrap_or(GENESIS_BLOCK.into()); + let mut current_l1_block_number = U64::from(self.config.start_block); // User might have supplied their own start block, in that case we shouldn't enforce the // use of the snapshot value. if current_l1_block_number == GENESIS_BLOCK.into() { @@ -206,6 +206,12 @@ impl L1Fetcher { let provider_clone = self.provider.clone(); let snapshot_clone = self.snapshot.clone(); let metrics = metrics.clone(); + let mut disable_polling = self.config.disable_polling; + let end_block = self + .config + .block_count + .map(|count| U64::from(self.config.start_block + count)); + async move { let mut latest_l2_block_number = U256::zero(); diff --git a/src/main.rs b/src/main.rs index f146d6c..180df80 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,9 +20,8 @@ use std::{ }; use clap::Parser; -use cli::{Cli, Command, L1FetcherOptions, Query, ReconstructSource}; +use cli::{Cli, Command, Query, ReconstructSource}; use constants::storage; -use ethers::types::U64; use eyre::Result; use snapshot::StateSnapshot; use tokio::sync::{mpsc, Mutex}; @@ -70,19 +69,10 @@ async fn main() -> Result<()> { }; match source { - ReconstructSource::L1 { - l1_fetcher_options: - L1FetcherOptions { - http_url, - start_block, - block_step: _, - block_count, - disable_polling, - }, - } => { + ReconstructSource::L1 { l1_fetcher_options } => { let snapshot = Arc::new(Mutex::new(StateSnapshot::default())); - let fetcher = L1Fetcher::new(&http_url, Some(snapshot.clone()))?; + let fetcher = L1Fetcher::new(l1_fetcher_options, Some(snapshot.clone()))?; let processor = TreeProcessor::new(db_path, snapshot.clone()).await?; let (tx, rx) = mpsc::channel::(5); @@ -90,11 +80,7 @@ async fn main() -> Result<()> { processor.run(rx).await; }); - let end_block = block_count.map(|n| U64([start_block + n])); - - fetcher - .fetch(tx, Some(U64([start_block])), end_block, disable_polling) - .await?; + fetcher.run(tx).await?; processor_handle.await?; } ReconstructSource::File { file } => { @@ -120,17 +106,10 @@ async fn main() -> Result<()> { } } Command::Download { - l1_fetcher_options: - L1FetcherOptions { - http_url, - start_block, - block_step: _, - block_count, - disable_polling, - }, + l1_fetcher_options, file, } => { - let fetcher = L1Fetcher::new(&http_url, None)?; + let fetcher = L1Fetcher::new(l1_fetcher_options, None)?; let processor = JsonSerializationProcessor::new(Path::new(&file))?; let (tx, rx) = mpsc::channel::(5); @@ -138,11 +117,7 @@ async fn main() -> Result<()> { processor.run(rx).await; }); - let end_block = block_count.map(|n| U64([start_block + n])); - - fetcher - .fetch(tx, Some(U64([start_block])), end_block, disable_polling) - .await?; + fetcher.run(tx).await?; processor_handle.await?; } Command::Query {