Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port all lib.rs code into binary #11

Merged
merged 5 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-trait = "0.1.73"
clap = { version = "4.4.0", features = ["string"] }
ethers = "1"
eyre = "0.6.8"
Expand Down
9 changes: 9 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,12 @@ pub mod ethereum {
/// zkSync smart contract address.
pub const ZK_SYNC_ADDR: &str = "0x32400084C286CF3E17e7B677ea9583e60a000324";
}

pub mod storage {
/// The path to the initial state file.
pub const INITAL_STATE_PATH: &str = "InitialState.csv";

// TODO: Configure via env / CLI.
/// The path of the state file.
pub const STATE_FILE_PATH: &str = "StateSnapshot.json";
}
75 changes: 72 additions & 3 deletions src/l1_fetcher.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use ethers::abi::Function;
use ethers::{abi::Contract, prelude::*, providers::Provider};
use eyre::Result;
use rand::random;
use state_reconstruct::constants::ethereum::{BLOCK_STEP, GENESIS_BLOCK, ZK_SYNC_ADDR};
use state_reconstruct::parse_calldata;
use state_reconstruct::CommitBlockInfoV1;
use tokio::sync::mpsc;
use tokio::time::{sleep, Duration};

use crate::constants::ethereum::{BLOCK_STEP, GENESIS_BLOCK, ZK_SYNC_ADDR};
use crate::types::{CommitBlockInfoV1, ParseError};

pub struct L1Fetcher {
provider: Provider<Http>,
contract: Contract,
Expand Down Expand Up @@ -131,3 +132,71 @@ impl L1Fetcher {
Ok(())
}
}

pub fn parse_calldata(
commit_blocks_fn: &Function,
calldata: &[u8],
) -> Result<Vec<CommitBlockInfoV1>> {
let mut parsed_input = commit_blocks_fn
.decode_input(&calldata[4..])
.map_err(|e| ParseError::InvalidCalldata(e.to_string()))?;

if parsed_input.len() != 2 {
return Err(ParseError::InvalidCalldata(format!(
"invalid number of parameters (got {}, expected 2) for commitBlocks function",
parsed_input.len()
))
.into());
}

let new_blocks_data = parsed_input
.pop()
.ok_or_else(|| ParseError::InvalidCalldata("new blocks data".to_string()))?;
let stored_block_info = parsed_input
.pop()
.ok_or_else(|| ParseError::InvalidCalldata("stored block info".to_string()))?;

let abi::Token::Tuple(stored_block_info) = stored_block_info else {
return Err(ParseError::InvalidCalldata("invalid StoredBlockInfo".to_string()).into());
};

let abi::Token::Uint(_previous_l2_block_number) = stored_block_info[0].clone() else {
return Err(ParseError::InvalidStoredBlockInfo(
"cannot parse previous L2 block number".to_string(),
)
.into());
};

let abi::Token::Uint(_previous_enumeration_index) = stored_block_info[2].clone() else {
return Err(ParseError::InvalidStoredBlockInfo(
"cannot parse previous enumeration index".to_string(),
)
.into());
};

//let previous_enumeration_index = previous_enumeration_index.0[0];
// TODO: What to do here?
// assert_eq!(previous_enumeration_index, tree.next_enumeration_index());

parse_commit_block_info(&new_blocks_data)
}

fn parse_commit_block_info(data: &abi::Token) -> Result<Vec<CommitBlockInfoV1>> {
let mut res = vec![];

let abi::Token::Array(data) = data else {
return Err(ParseError::InvalidCommitBlockInfo(
"cannot convert newBlocksData to array".to_string(),
)
.into());
};

for data in data.iter() {
match CommitBlockInfoV1::try_from(data) {
Ok(blk) => res.push(blk),
Err(e) => println!("failed to parse commit block info: {}", e),
}
}

Ok(res)
}
205 changes: 0 additions & 205 deletions src/lib.rs

This file was deleted.

25 changes: 19 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
#![feature(array_chunks)]

mod constants;
mod l1_fetcher;
mod processor;
mod types;

use std::env;

use clap::{arg, value_parser, Command};
use constants::ethereum;
use ethers::types::U64;
use eyre::Result;
use l1_fetcher::L1Fetcher;
use state_reconstruct::CommitBlockInfoV1;
use tokio::sync::mpsc;

use constants::ethereum;
use crate::{
processor::{tree::TreeProcessor, Processor},
types::CommitBlockInfoV1,
};

fn cli() -> Command {
Command::new("state-reconstruct")
Expand Down Expand Up @@ -52,17 +61,21 @@ async fn main() -> Result<()> {
match matches.subcommand() {
Some(("reconstruct", sub_matches)) => match sub_matches.subcommand() {
Some(("l1", args)) => {
// TODO: Use start_block from snapshot.
let start_block = args.get_one::<u64>("start-block").expect("required");
let block_step = args.get_one::<u64>("block-step").expect("required");
let http_url = args.get_one::<String>("http-url").expect("required");
println!("reconstruct from L1, starting from block number {}, processing {} blocks at a time", start_block, block_step);

// TODO: This should be an env variable / CLI argument.
let db_dir = env::current_dir()?.join("db");

let fetcher = L1Fetcher::new(http_url)?;
let (tx, mut rx) = mpsc::channel::<Vec<CommitBlockInfoV1>>(5);
let processor = TreeProcessor::new(&db_dir)?;
let (tx, rx) = mpsc::channel::<Vec<CommitBlockInfoV1>>(5);

tokio::spawn(async move {
while let Some(blks) = rx.recv().await {
blks.iter().for_each(|x| println!("{:?}", x));
}
processor.run(rx).await;
});

fetcher.fetch(tx, Some(U64([*start_block])), None).await?;
Expand Down
11 changes: 11 additions & 0 deletions src/processor/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use async_trait::async_trait;
use tokio::sync::mpsc;

use crate::types::CommitBlockInfoV1;

pub mod tree;

#[async_trait]
pub trait Processor {
async fn run(self, rx: mpsc::Receiver<Vec<CommitBlockInfoV1>>);
}
Loading