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

Hash & gather factory deps to snapshot #42

Merged
merged 5 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
99 changes: 71 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ members = ["state-reconstruct-fetcher"]
[dependencies]
async-trait = "0.1.74"
blake2 = "0.10.6"
chrono = "0.4.31"
clap = { version = "4.4.7", features = ["derive", "env"] }
ethers = "1.0.2"
eyre = "0.6.8"
Expand All @@ -22,3 +23,4 @@ tokio = { version = "1.33.0", features = ["macros"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.17"
zksync_merkle_tree = { git = "https://github.com/matter-labs/zksync-era.git" }
zkevm_opcode_defs = { git = "https://github.com/matter-labs/era-zkevm_opcode_defs.git" }
8 changes: 8 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ pub enum Command {
#[arg(short, long, env = "ZK_SYNC_DB_PATH")]
db_path: Option<String>,
},

/// Testing.
ExportSnapshot {
#[command(flatten)]
l1_fetcher_options: L1FetcherOptions,
/// The path of the file to export the snapshot to.
file: Option<String>,
},
}

#[derive(Parser)]
Expand Down
25 changes: 25 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::{
use clap::Parser;
use cli::{Cli, Command, ReconstructSource};
use eyre::Result;
use processor::snapshot::SnapshotExporter;
use state_reconstruct_fetcher::{
constants::storage,
l1_fetcher::{L1Fetcher, L1FetcherOptions},
Expand Down Expand Up @@ -52,6 +53,7 @@ fn start_logger(default_level: LevelFilter) {
}

#[tokio::main]
#[allow(clippy::too_many_lines)]
async fn main() -> Result<()> {
start_logger(LevelFilter::INFO);

Expand Down Expand Up @@ -150,6 +152,29 @@ async fn main() -> Result<()> {
println!("{result}");
}
}
Command::ExportSnapshot {
l1_fetcher_options,
file,
} => {
let fetcher_options = L1FetcherOptions {
http_url: l1_fetcher_options.http_url,
start_block: l1_fetcher_options.start_block,
block_step: l1_fetcher_options.block_step,
block_count: l1_fetcher_options.block_count,
disable_polling: l1_fetcher_options.disable_polling,
};

let fetcher = L1Fetcher::new(fetcher_options, None)?;
let processor = SnapshotExporter::new(file);

let (tx, rx) = mpsc::channel::<CommitBlockInfoV1>(5);
let processor_handle = tokio::spawn(async move {
processor.run(rx).await;
});

fetcher.run(tx).await?;
processor_handle.await?;
}
}

Ok(())
Expand Down
3 changes: 2 additions & 1 deletion src/processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use state_reconstruct_fetcher::types::CommitBlockInfoV1;
use tokio::sync::mpsc;

pub mod json;
pub mod snapshot;
pub mod tree;

#[async_trait]
pub trait Processor {
async fn run(self, rx: mpsc::Receiver<CommitBlockInfoV1>);
async fn run(self, mut rx: mpsc::Receiver<CommitBlockInfoV1>);
}
26 changes: 26 additions & 0 deletions src/processor/snapshot/bytecode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use ethers::types::H256;

// These are copied from zkSync-era project to avoid pulling in full dependency.
pub fn bytes_to_chunks(bytes: &[u8]) -> Vec<[u8; 32]> {
assert!(
bytes.len() % 32 == 0,
"Bytes must be divisible by 32 to split into chunks"
);

bytes
.chunks(32)
.map(|el| {
let mut chunk = [0u8; 32];
chunk.copy_from_slice(el);
chunk
})
.collect()
}

pub fn hash_bytecode(code: &[u8]) -> H256 {
let chunked_code = bytes_to_chunks(code);
let hash =
zkevm_opcode_defs::utils::bytecode_to_code_hash(&chunked_code).expect("Invalid bytecode");

H256(hash)
}
Loading