Skip to content

Commit

Permalink
fix block_context encode in prover
Browse files Browse the repository at this point in the history
  • Loading branch information
anylots committed Jan 24, 2025
1 parent 26c98c4 commit c87efc2
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 21 deletions.
2 changes: 1 addition & 1 deletion contracts/src/deploy-config/holesky.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const config = {
l2BaseFee: 0.1, // Gwei

// verify contract config
programVkey: '0x003b9faf839cf88ea568b87f23a64b7e0089d0afafa6520ad7056871b57a854a',
programVkey: '0x0067523b285111b45b465b927d56449351e78d802252ccbf4bcf5eae588b7356',
// rollup contract config
// initialize config
finalizationPeriodSeconds: 600,
Expand Down
2 changes: 1 addition & 1 deletion contracts/src/deploy-config/l1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const config = {
l2BaseFee: 0.1, // Gwei

// verify contract config
programVkey: '0x003b9faf839cf88ea568b87f23a64b7e0089d0afafa6520ad7056871b57a854a',
programVkey: '0x0067523b285111b45b465b927d56449351e78d802252ccbf4bcf5eae588b7356',
// rollup contract config
// initialize config
finalizationPeriodSeconds: 10,
Expand Down
2 changes: 1 addition & 1 deletion contracts/src/deploy-config/qanetl1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const config = {
l2BaseFee: 0.1, // Gwei

// verify contract config
programVkey: '0x003b9faf839cf88ea568b87f23a64b7e0089d0afafa6520ad7056871b57a854a',
programVkey: '0x0067523b285111b45b465b927d56449351e78d802252ccbf4bcf5eae588b7356',
// rollup contract config
// initialize config
finalizationPeriodSeconds: 600,
Expand Down
2 changes: 1 addition & 1 deletion contracts/src/deploy-config/sepolia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const config = {
/**
* ---to---legacy property
*/
programVkey: '0x003b9faf839cf88ea568b87f23a64b7e0089d0afafa6520ad7056871b57a854a',
programVkey: '0x0067523b285111b45b465b927d56449351e78d802252ccbf4bcf5eae588b7356',
rollupMinDeposit: 0.0001,
rollupProofWindow: 86400,
rollupGenesisBlockNumber: 0,
Expand Down
2 changes: 1 addition & 1 deletion contracts/src/deploy-config/testnetl1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const config = {
sequencerWindowSize: 200,
channelTimeout: 120,

programVkey: '0x003b9faf839cf88ea568b87f23a64b7e0089d0afafa6520ad7056871b57a854a',
programVkey: '0x0067523b285111b45b465b927d56449351e78d802252ccbf4bcf5eae588b7356',
rollupMinDeposit: 1,
rollupProofWindow: 100,
rollupGenesisBlockNumber: 0,
Expand Down
1 change: 1 addition & 0 deletions gas-oracle/app/data/blob_with_context.data

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion gas-oracle/app/src/da_scalar/calculate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ pub fn extract_txn_count(origin_batch: &Vec<u8>, last_block_num: u64) -> Option<
log::error!("total_txn_in_batch < l1_txn_in_batch");
return None;
}
log::debug!("total_txn_in_batch: {:#?}, l1_txn_in_batch: {:#?}", num_txn, num_l1_messages);

txn_count_in_batch += (num_txn - num_l1_messages) as u64;
}
Expand Down
41 changes: 40 additions & 1 deletion gas-oracle/app/src/da_scalar/l1_scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,47 @@ impl ScalarUpdater {

#[cfg(test)]
mod tests {
use crate::da_scalar::blob::Blob;

use super::*;
use std::{env::var, str::FromStr, sync::Arc};
use std::{env::var, fs, path::Path, str::FromStr, sync::Arc};

#[test]
fn test_blob_data() {
let blob_data_path = Path::new("data/blob_with_context.data");
let data = fs::read_to_string(blob_data_path).expect("Unable to read file");
let hex_data: Vec<u8> = hex::decode(data.trim()).unwrap();

let mut blob_array = [0u8; 131072];
blob_array.copy_from_slice(&hex_data);

let blob_struct = Blob(blob_array);
let origin_batch = blob_struct
.get_origin_batch()
.map_err(|e| {
ScalarError::CalculateError(anyhow!(format!(
"Failed to decode blob tx payload: {}",
e
)))
})
.unwrap();

let mut tx_payloads: Vec<Vec<u8>> = vec![];
tx_payloads.push(origin_batch);

let data_with_txn_count: Vec<(u64, u64)> = tx_payloads
.iter()
.map(|batch: &Vec<u8>| {
(batch.len() as u64, extract_txn_count(batch, 328208).unwrap_or_default())
})
.collect();

let (total_size, total_count) = data_with_txn_count
.iter()
.fold((0u64, 0u64), |acc, &(size, count)| (acc.0 + size, acc.1 + count));

println!("total_size: {}, total_count: {}", total_size, total_count)
}

#[tokio::test]
#[ignore]
Expand Down
Binary file modified prover/bin/client/elf/riscv32im-succinct-zkvm-elf
Binary file not shown.
2 changes: 1 addition & 1 deletion prover/bin/host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ mod tests {
std::env::set_var("TRUSTED_SETUP_4844", "../../configs/4844_trusted_setup.txt");
let blob_info: BlobInfo = populate_kzg(&blob).unwrap();

let (versioned_hash, batch_data) = BlobVerifier::verify(&blob_info).unwrap();
let (versioned_hash, batch_data) = BlobVerifier::verify(&blob_info, 1).unwrap();
println!(
"versioned_hash: {:?}, batch_data len: {:?}",
hex::encode(versioned_hash.as_slice()),
Expand Down
2 changes: 1 addition & 1 deletion prover/bin/server/src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ fn save_batch_header(blocks: &mut Vec<BlockTrace>, batch_index: u64) {
blocks.iter_mut().for_each(|blobk| blobk.flatten());
let batch_info = EVMVerifier::verify(blocks).unwrap();
let blob_info = morph_executor_host::get_blob_info(blocks).unwrap();
let (versioned_hash, _) = BlobVerifier::verify(&blob_info).unwrap();
let (versioned_hash, _) = BlobVerifier::verify(&blob_info, blocks.len()).unwrap();

// Save batch_header
// | batch_data_hash | versioned_hash | sequencer_root |
Expand Down
11 changes: 5 additions & 6 deletions prover/crates/morph-executor/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@ pub use verifier::{blob_verifier::BlobVerifier, evm_verifier::EVMVerifier};

pub fn verify(input: &ClientInput) -> Result<B256, anyhow::Error> {
// Verify DA
let (versioned_hash, batch_data) = BlobVerifier::verify(&input.blob_info).unwrap();
println!("cycle-tracker-start: traces-to-data");
let num_blocks = input.l2_traces.len();
let mut batch_from_trace: Vec<u8> = Vec::with_capacity(2 + num_blocks * 60);
batch_from_trace.extend_from_slice(&(num_blocks as u16).to_be_bytes());
let (versioned_hash, batch_data) = BlobVerifier::verify(&input.blob_info, num_blocks).unwrap();
println!("cycle-tracker-start: traces-to-data");
let mut batch_from_trace: Vec<u8> = Vec::with_capacity(num_blocks * 60);
let mut tx_bytes: Vec<u8> = vec![];
for trace in &input.l2_traces {
// BlockContext
let mut block_ctx: Vec<u8> = Vec::with_capacity(60);
block_ctx.extend_from_slice(&trace.number().to_be_bytes());
block_ctx.extend_from_slice(&trace.timestamp().to_be_bytes::<32>());
block_ctx.extend_from_slice(&trace.timestamp().to::<u64>().to_be_bytes());
block_ctx
.extend_from_slice(&trace.base_fee_per_gas().unwrap_or_default().to_be_bytes::<32>());
block_ctx.extend_from_slice(&trace.gas_limit().to_be_bytes::<32>());
block_ctx.extend_from_slice(&trace.gas_limit().to::<u64>().to_be_bytes());
block_ctx.extend_from_slice(&(trace.transactions.len() as u16).to_be_bytes());
block_ctx.extend_from_slice(&(trace.num_l1_txs() as u16).to_be_bytes());
batch_from_trace.extend(block_ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ use crate::types::{blob::get_origin_batch, input::BlobInfo};
pub struct BlobVerifier;

impl BlobVerifier {
pub fn verify(blob_info: &BlobInfo) -> Result<(B256, Vec<u8>), anyhow::Error> {
pub fn verify(
blob_info: &BlobInfo,
num_blocks: usize,
) -> Result<(B256, Vec<u8>), anyhow::Error> {
// decode
println!("cycle-tracker-start: decode_blob");
let origin_batch = get_origin_batch(&blob_info.blob_data).unwrap();
cfg_if::cfg_if! {
if #[cfg(not(target_os = "zkvm"))] {
let tx_list = crate::types::blob::decode_transactions(origin_batch.as_slice());
let tx_list = crate::types::blob::decode_transactions(&origin_batch.as_slice()[num_blocks*60..]);
println!("decoded tx_list_len: {:?}", tx_list.len());
}
}
Expand Down
7 changes: 3 additions & 4 deletions prover/crates/morph-executor/host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,17 @@ pub fn get_blob_info(block_trace: &Vec<BlockTrace>) -> Result<BlobInfo, anyhow::

pub fn get_blob_data(block_trace: &Vec<BlockTrace>) -> [u8; BLOB_DATA_SIZE] {
let num_blocks = block_trace.len();
let mut batch_from_trace: Vec<u8> = Vec::with_capacity(2 + num_blocks * 60);
batch_from_trace.extend_from_slice(&(num_blocks as u16).to_be_bytes());
let mut batch_from_trace: Vec<u8> = Vec::with_capacity(num_blocks * 60);
let mut tx_bytes: Vec<u8> = vec![];
for trace in block_trace {
// BlockContext
// https://github.com/morph-l2/morph/blob/main/contracts/contracts/libraries/codec/BatchCodecV0.sol
let mut block_ctx: Vec<u8> = Vec::with_capacity(60);
block_ctx.extend_from_slice(&trace.number().to_be_bytes());
block_ctx.extend_from_slice(&trace.timestamp().to_be_bytes::<32>());
block_ctx.extend_from_slice(&trace.timestamp().to::<u64>().to_be_bytes());
block_ctx
.extend_from_slice(&trace.base_fee_per_gas().unwrap_or_default().to_be_bytes::<32>());
block_ctx.extend_from_slice(&trace.gas_limit().to_be_bytes::<32>());
block_ctx.extend_from_slice(&trace.gas_limit().to::<u64>().to_be_bytes());
block_ctx.extend_from_slice(&(trace.transactions.len() as u16).to_be_bytes());
block_ctx.extend_from_slice(&(trace.num_l1_txs() as u16).to_be_bytes());
batch_from_trace.extend(block_ctx);
Expand Down

0 comments on commit c87efc2

Please sign in to comment.