Skip to content

Commit

Permalink
Move construct_pubdata method from utils to L1BatchWithMetadata (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
toni-calvin authored Feb 19, 2024
1 parent 9a26760 commit 3028625
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 36 deletions.
32 changes: 32 additions & 0 deletions core/lib/types/src/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,38 @@ impl L1BatchWithMetadata {
}
})
}

/// Packs all pubdata needed for batch commitment in boojum into one bytes array. The packing contains the
/// following: logs, messages, bytecodes, and compressed state diffs.
/// This data is currently part of calldata but will be submitted as part of the blob section post EIP-4844.
pub fn construct_pubdata(l1_batch_with_metadata: &L1BatchWithMetadata) -> Vec<u8> {
let mut res: Vec<u8> = vec![];

// Process and Pack Logs
res.extend((l1_batch_with_metadata.header.l2_to_l1_logs.len() as u32).to_be_bytes());
for l2_to_l1_log in &l1_batch_with_metadata.header.l2_to_l1_logs {
res.extend(l2_to_l1_log.0.to_bytes());
}

// Process and Pack Messages
res.extend((l1_batch_with_metadata.header.l2_to_l1_messages.len() as u32).to_be_bytes());
for msg in &l1_batch_with_metadata.header.l2_to_l1_messages {
res.extend((msg.len() as u32).to_be_bytes());
res.extend(msg);
}

// Process and Pack Bytecodes
res.extend((l1_batch_with_metadata.factory_deps.len() as u32).to_be_bytes());
for bytecode in &l1_batch_with_metadata.factory_deps {
res.extend((bytecode.len() as u32).to_be_bytes());
res.extend(bytecode);
}

// Extend with Compressed StateDiffs
res.extend(&l1_batch_with_metadata.metadata.state_diffs_compressed);

res
}
}

impl SerializeCommitment for L2ToL1Log {
Expand Down
4 changes: 2 additions & 2 deletions core/lib/types/src/l1_batch_commit_data_generator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use zksync_basic_types::{ethabi::Token, U256};

use crate::{commitment::L1BatchWithMetadata, utils};
use crate::commitment::L1BatchWithMetadata;

pub trait L1BatchCommitDataGenerator
where
Expand Down Expand Up @@ -69,7 +69,7 @@ fn validium_mode_l1_commit_data(l1_batch_with_metadata: &L1BatchWithMetadata) ->

fn rollup_mode_l1_commit_data(l1_batch_with_metadata: &L1BatchWithMetadata) -> Vec<Token> {
let mut commit_data = validium_mode_l1_commit_data(l1_batch_with_metadata);
commit_data.push(Token::Bytes(utils::construct_pubdata(
commit_data.push(Token::Bytes(L1BatchWithMetadata::construct_pubdata(
l1_batch_with_metadata,
)));
commit_data
Expand Down
36 changes: 2 additions & 34 deletions core/lib/types/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use zksync_basic_types::{Address, H256};
use zksync_utils::{address_to_h256, u256_to_h256};

use crate::{
commitment::L1BatchWithMetadata, system_contracts::DEPLOYMENT_NONCE_INCREMENT,
web3::signing::keccak256, AccountTreeId, StorageKey, L2_ETH_TOKEN_ADDRESS, U256,
system_contracts::DEPLOYMENT_NONCE_INCREMENT, web3::signing::keccak256, AccountTreeId,
StorageKey, L2_ETH_TOKEN_ADDRESS, U256,
};

/// Transforms the *full* account nonce into an *account* nonce.
Expand Down Expand Up @@ -77,38 +77,6 @@ pub fn deployed_address_create(sender: Address, deploy_nonce: U256) -> Address {
Address::from_slice(&keccak256(&bytes)[12..])
}

/// Packs all pubdata needed for batch commitment in boojum into one bytes array. The packing contains the
/// following: logs, messages, bytecodes, and compressed state diffs.
/// This data is currently part of calldata but will be submitted as part of the blob section post EIP-4844.
pub fn construct_pubdata(l1_batch_with_metadata: &L1BatchWithMetadata) -> Vec<u8> {
let mut res: Vec<u8> = vec![];

// Process and Pack Logs
res.extend((l1_batch_with_metadata.header.l2_to_l1_logs.len() as u32).to_be_bytes());
for l2_to_l1_log in &l1_batch_with_metadata.header.l2_to_l1_logs {
res.extend(l2_to_l1_log.0.to_bytes());
}

// Process and Pack Messages
res.extend((l1_batch_with_metadata.header.l2_to_l1_messages.len() as u32).to_be_bytes());
for msg in &l1_batch_with_metadata.header.l2_to_l1_messages {
res.extend((msg.len() as u32).to_be_bytes());
res.extend(msg);
}

// Process and Pack Bytecodes
res.extend((l1_batch_with_metadata.factory_deps.len() as u32).to_be_bytes());
for bytecode in &l1_batch_with_metadata.factory_deps {
res.extend((bytecode.len() as u32).to_be_bytes());
res.extend(bytecode);
}

// Extend with Compressed StateDiffs
res.extend(&l1_batch_with_metadata.metadata.state_diffs_compressed);

res
}

#[cfg(test)]
mod tests {
use std::str::FromStr;
Expand Down

0 comments on commit 3028625

Please sign in to comment.