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 2 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
7 changes: 7 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,11 @@ pub mod ethereum {

/// zkSync smart contract address.
pub const ZK_SYNC_ADDR: &str = "0x32400084C286CF3E17e7B677ea9583e60a000324";

/// 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";
zeapoz marked this conversation as resolved.
Show resolved Hide resolved
}
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.

30 changes: 20 additions & 10 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::{BLOCK_STEP, GENESIS_BLOCK};
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 All @@ -26,13 +35,13 @@ fn cli() -> Command {
.arg(
arg!(--"start-block" <START_BLOCK>)
.help("Ethereum block number to start state import from")
.default_value(ethereum::GENESIS_BLOCK.to_string())
.default_value(GENESIS_BLOCK.to_string())
zeapoz marked this conversation as resolved.
Show resolved Hide resolved
.value_parser(value_parser!(u64)),
)
.arg(
arg!(--"block-step" <BLOCK_STEP>)
.help("Number of blocks to filter & process in one step")
.default_value(ethereum::BLOCK_STEP.to_string())
.default_value(BLOCK_STEP.to_string())
zeapoz marked this conversation as resolved.
Show resolved Hide resolved
.value_parser(value_parser!(u64)),
),
)
Expand All @@ -52,18 +61,19 @@ 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);
tokio::spawn(async move {
while let Some(blks) = rx.recv().await {
blks.iter().for_each(|x| println!("{:?}", x));
}
});
let processor = TreeProcessor::new(&db_dir)?;
let (tx, rx) = mpsc::channel::<Vec<CommitBlockInfoV1>>(5);
processor.run(rx);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The returned JoinHandle is not captured nor "awaited" here - how does this work?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same behavior that we had before, we're just spawning the task inside this function instead. The thing with tokio futures is that they will start executing as soon as they're spawned, await will just wait until that task is done.


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

use crate::types::CommitBlockInfoV1;

pub mod tree;

pub trait Processor {
fn run(self, rx: mpsc::Receiver<Vec<CommitBlockInfoV1>>) -> JoinHandle<()>;
}
Loading