From bf681545ac13be9c8bc7d3ced5d7e3e3dea6fe76 Mon Sep 17 00:00:00 2001 From: Zhang Zhuo Date: Tue, 20 Aug 2024 14:53:46 +0800 Subject: [PATCH] finish make_batch_task_detail --- prover/src/zk_circuits_handler/edison.rs | 87 ++++++++++++++++++++---- 1 file changed, 73 insertions(+), 14 deletions(-) diff --git a/prover/src/zk_circuits_handler/edison.rs b/prover/src/zk_circuits_handler/edison.rs index 90921fa55..6f3428540 100644 --- a/prover/src/zk_circuits_handler/edison.rs +++ b/prover/src/zk_circuits_handler/edison.rs @@ -242,7 +242,12 @@ impl CircuitsHandler for EdisonHandler { mod tests { use super::*; use crate::zk_circuits_handler::utils::encode_vk; - use prover_edison::utils::chunk_trace_to_witness_block; + use ethers_core::types::H256; + use prover_darwin::zkevm_circuits::witness::Block; + use prover_edison::{ + aggregator::eip4844, utils::chunk_trace_to_witness_block, BatchData, BatchHeader, + MAX_AGG_SNARKS, + }; use std::{path::PathBuf, sync::LazyLock}; #[ctor::ctor] @@ -285,13 +290,14 @@ mod tests { check_vk(TaskType::Chunk, chunk_vk, "chunk vk must be available"); let chunk_dir_paths = get_chunk_dir_paths()?; log::info!("chunk_dir_paths, {:?}", chunk_dir_paths); + let mut chunk_traces = vec![]; let mut chunk_infos = vec![]; let mut chunk_proofs = vec![]; for (id, chunk_path) in chunk_dir_paths.into_iter().enumerate() { let chunk_id = format!("chunk_proof{}", id + 1); log::info!("start to process {chunk_id}"); let chunk_trace = read_chunk_trace(chunk_path)?; - + chunk_traces.push(chunk_trace.clone()); let chunk_info = traces_to_chunk_info(chunk_trace.clone())?; chunk_infos.push(chunk_info); @@ -306,7 +312,7 @@ mod tests { EdisonHandler::new(ProverType::Batch, &PARAMS_PATH, &ASSETS_PATH, None)?; let batch_vk = batch_handler.get_vk(TaskType::Batch).unwrap(); check_vk(TaskType::Batch, batch_vk, "batch vk must be available"); - let batch_task_detail = make_batch_task_detail(chunk_infos, chunk_proofs); + let batch_task_detail = make_batch_task_detail(chunk_traces, chunk_proofs, None); log::info!("start to prove batch"); let batch_proof = batch_handler.gen_batch_proof_raw(batch_task_detail)?; let proof_data = serde_json::to_string(&batch_proof)?; @@ -315,17 +321,70 @@ mod tests { Ok(()) } - fn make_batch_task_detail(_: Vec, _: Vec) -> BatchTaskDetail { - todo!(); - // BatchTaskDetail { - // chunk_infos, - // batch_proving_task: BatchProvingTask { - // parent_batch_hash: todo!(), - // parent_state_root: todo!(), - // batch_header: todo!(), - // chunk_proofs, - // }, - // } + // copied from https://github.com/scroll-tech/scroll-prover/blob/main/integration/src/prove.rs + fn get_blob_from_chunks(chunks: &[ChunkInfo]) -> Vec { + let num_chunks = chunks.len(); + + let padded_chunk = + ChunkInfo::mock_padded_chunk_info_for_testing(chunks.last().as_ref().unwrap()); + let chunks_with_padding = [ + chunks.to_vec(), + vec![padded_chunk; MAX_AGG_SNARKS - num_chunks], + ] + .concat(); + let batch_data = BatchData::<{ MAX_AGG_SNARKS }>::new(chunks.len(), &chunks_with_padding); + let batch_bytes = batch_data.get_batch_data_bytes(); + let blob_bytes = eip4844::get_blob_bytes(&batch_bytes); + log::info!("blob_bytes len {}", blob_bytes.len()); + blob_bytes + } + + // TODO: chunk_infos can be extracted from chunk_proofs. + // Still needed? + fn make_batch_task_detail( + chunk_traces: Vec>, + chunk_proofs: Vec, + last_batcher_header: Option>, + ) -> BatchTaskDetail { + // dummy parent batch hash + let dummy_parent_batch_hash = H256([ + 0xab, 0xac, 0xad, 0xae, 0xaf, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + ]); + let chunk_infos: Vec<_> = chunk_proofs.iter().map(|p| p.chunk_info.clone()).collect(); + + let l1_message_popped = chunk_traces + .iter() + .flatten() + .map(|chunk| chunk.num_l1_txs()) + .sum(); + let last_block_timestamp = chunk_traces.last().map_or(0, |block_traces| { + block_traces + .last() + .map_or(0, |block_trace| block_trace.header.timestamp.as_u64()) + }); + + let blob_bytes = get_blob_from_chunks(&chunk_infos); + let batch_header = BatchHeader::construct_from_chunks( + last_batcher_header.map_or(4, |header| header.version), + last_batcher_header.map_or(123, |header| header.batch_index + 1), + l1_message_popped, + last_batcher_header.map_or(l1_message_popped, |header| { + header.total_l1_message_popped + l1_message_popped + }), + last_batcher_header.map_or(dummy_parent_batch_hash, |header| header.batch_hash()), + last_block_timestamp, + &chunk_infos, + &blob_bytes, + ); + BatchTaskDetail { + chunk_infos, + batch_proving_task: BatchProvingTask { + chunk_proofs, + batch_header, + blob_bytes, + }, + } } fn check_vk(proof_type: TaskType, vk: Vec, info: &str) {