Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions Cargo.lock

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

68 changes: 40 additions & 28 deletions lib/batch_types/src/batch_info.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use alloy::consensus::{BlobTransactionSidecar, SidecarBuilder, SimpleCoder};
use alloy::primitives::{Address, B256, BlockNumber, U256, keccak256};
use alloy::sol_types::SolValue;
use blake2::{Blake2s256, Digest};
use serde::{Deserialize, Serialize};
use std::ops;
Expand Down Expand Up @@ -43,6 +44,7 @@ impl BatchInfo {
) -> Self {
let mut priority_operations_hash = keccak256([]);
let mut number_of_layer1_txs = 0;
let mut number_of_layer2_txs = 0;
let mut total_pubdata = vec![];
let mut encoded_l2_l1_logs = vec![];

Expand All @@ -62,7 +64,9 @@ impl BatchInfo {
keccak256([priority_operations_hash.0, onchain_data_hash.0].concat());
number_of_layer1_txs += 1;
}
ZkEnvelope::L2(_) => {}
ZkEnvelope::L2(_) => {
number_of_layer2_txs += 1;
}
ZkEnvelope::Upgrade(_) => {
assert!(
upgrade_tx_hash.is_none(),
Expand Down Expand Up @@ -130,6 +134,7 @@ impl BatchInfo {
batch_number,
new_state_commitment,
number_of_layer1_txs,
number_of_layer2_txs,
priority_operations_hash,
dependency_roots_rolling_hash: B256::ZERO,
l2_to_l1_logs_root_hash,
Expand All @@ -153,34 +158,41 @@ impl BatchInfo {
/// Calculate keccak256 hash of BatchOutput part of public input
pub fn public_input_hash(&self, protocol_version: &ProtocolSemanticVersion) -> B256 {
let commit_info = &self.commit_info;
let upgrade_tx_hash = self.upgrade_tx_hash.unwrap_or(B256::ZERO);
match protocol_version.minor {
// 31 needed for upgrade integration test
30..=31 => {
use zk_ee::utils::Bytes32;
let system_batch_output =
zk_os_basic_system::system_implementation::system::BatchOutput {
chain_id: U256::from(commit_info.chain_id),
first_block_timestamp: commit_info.first_block_timestamp,
last_block_timestamp: commit_info.last_block_timestamp,
da_commitment_scheme: (commit_info.l2_da_commitment_scheme as u8)
.try_into()
.expect("Failed to convert DA commitment scheme"),
pubdata_commitment: Bytes32::from(commit_info.da_commitment.0),
number_of_layer_1_txs: U256::from(commit_info.number_of_layer1_txs),
priority_operations_hash: Bytes32::from(
commit_info.priority_operations_hash.0,
),
l2_logs_tree_root: Bytes32::from(commit_info.l2_to_l1_logs_root_hash.0),
upgrade_tx_hash: self
.upgrade_tx_hash
.map(|h| Bytes32::from_array(h.0))
.unwrap_or(Bytes32::ZERO),
interop_root_rolling_hash: Bytes32::from(
commit_info.dependency_roots_rolling_hash.0,
),
};
B256::from(system_batch_output.hash())
}
// v30 and v31 use different packed layouts for batch output hash:
// v31 inserts number_of_layer2_txs between L1 tx count and priority_operations_hash.
30 => B256::from(keccak256(
(
U256::from(commit_info.chain_id),
commit_info.first_block_timestamp,
commit_info.last_block_timestamp,
U256::from(commit_info.l2_da_commitment_scheme as u8),
commit_info.da_commitment,
U256::from(commit_info.number_of_layer1_txs),
commit_info.priority_operations_hash,
commit_info.l2_to_l1_logs_root_hash,
upgrade_tx_hash,
commit_info.dependency_roots_rolling_hash,
)
.abi_encode_packed(),
)),
31 => B256::from(keccak256(
(
U256::from(commit_info.chain_id),
commit_info.first_block_timestamp,
commit_info.last_block_timestamp,
U256::from(commit_info.l2_da_commitment_scheme as u8),
commit_info.da_commitment,
U256::from(commit_info.number_of_layer1_txs),
U256::from(commit_info.number_of_layer2_txs),
commit_info.priority_operations_hash,
commit_info.l2_to_l1_logs_root_hash,
upgrade_tx_hash,
commit_info.dependency_roots_rolling_hash,
)
.abi_encode_packed(),
)),
_ => panic!("Unsupported protocol version: {protocol_version}"),
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/batch_verification/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub fn dummy_commit_batch_info(batch_number: u64, from: u64, to: u64) -> CommitB
batch_number,
new_state_commitment: B256::ZERO,
number_of_layer1_txs: 0,
number_of_layer2_txs: 0,
priority_operations_hash: B256::ZERO,
dependency_roots_rolling_hash: B256::ZERO,
l2_to_l1_logs_root_hash: B256::ZERO,
Expand Down
1 change: 1 addition & 0 deletions lib/batch_verification/src/wire_format/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn create_sample_request() -> BatchVerificationRequest {
batch_number: 42,
new_state_commitment: B256::ZERO,
number_of_layer1_txs: 5,
number_of_layer2_txs: 0,
priority_operations_hash: B256::ZERO,
dependency_roots_rolling_hash: B256::ZERO,
l2_to_l1_logs_root_hash: B256::ZERO,
Expand Down
74 changes: 56 additions & 18 deletions lib/contract_interface/src/calldata.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::models::{CommitBatchInfo, StoredBatchInfo};
use crate::{IExecutor, IExecutorV29, IMultisigCommitter};
use crate::{IExecutor, IExecutorV29, IExecutorV30, IMultisigCommitter};
use alloy::primitives::Address;
use alloy::sol_types::{SolCall, SolValue};

const V29_ENCODING_VERSION: u8 = 2;
const V30_ENCODING_VERSION: u8 = 3;
const V31_ENCODING_VERSION: u8 = 4;

pub struct CommitCalldata {
pub chain_address: Address,
Expand Down Expand Up @@ -50,23 +51,47 @@ impl CommitCalldata {
);
};

if commit_data[0] != V30_ENCODING_VERSION {
if commit_data[0] != V30_ENCODING_VERSION && commit_data[0] != V31_ENCODING_VERSION {
anyhow::bail!("unexpected encoding version: {}", commit_data[0]);
}

let (stored_batch_info, mut commit_batch_infos) =
<(
IExecutor::StoredBatchInfo,
Vec<IExecutor::CommitBatchInfoZKsyncOS>,
)>::abi_decode_params(&commit_data[1..])?;
if commit_batch_infos.len() != 1 {
anyhow::bail!(
"unexpected number of committed batch infos: {}",
commit_batch_infos.len()
);
}
let stored_batch_info = StoredBatchInfo::from(stored_batch_info);
let commit_batch_info = CommitBatchInfo::from(commit_batch_infos.remove(0));
let (stored_batch_info, commit_batch_info) = match commit_data[0] {
V30_ENCODING_VERSION => {
let (stored_batch_info, mut commit_batch_infos) =
<(
IExecutor::StoredBatchInfo,
Vec<IExecutorV30::CommitBatchInfoZKsyncOS>,
)>::abi_decode_params(&commit_data[1..])?;
if commit_batch_infos.len() != 1 {
anyhow::bail!(
"unexpected number of committed batch infos: {}",
commit_batch_infos.len()
);
}
(
StoredBatchInfo::from(stored_batch_info),
CommitBatchInfo::from(commit_batch_infos.remove(0)),
)
}
V31_ENCODING_VERSION => {
let (stored_batch_info, mut commit_batch_infos) =
<(
IExecutor::StoredBatchInfo,
Vec<IExecutor::CommitBatchInfoZKsyncOS>,
)>::abi_decode_params(&commit_data[1..])?;
if commit_batch_infos.len() != 1 {
anyhow::bail!(
"unexpected number of committed batch infos: {}",
commit_batch_infos.len()
);
}
(
StoredBatchInfo::from(stored_batch_info),
CommitBatchInfo::from(commit_batch_infos.remove(0)),
)
}
_ => unreachable!("encoding version pre-validated"),
};
Ok(Self {
chain_address,
process_from,
Expand Down Expand Up @@ -99,9 +124,9 @@ pub fn encode_commit_batch_data(
// Prefixed by current encoding version as expected by protocol
[[V29_ENCODING_VERSION].to_vec(), encoded_data].concat()
}
// 31 needed for upgrade integration test
30..=31 => {
let commit_batch_info = IExecutor::CommitBatchInfoZKsyncOS::from(commit_info.clone());
30 => {
let commit_batch_info =
IExecutorV30::CommitBatchInfoZKsyncOS::from(commit_info.clone());
tracing::debug!(
last_batch_hash = ?prev_batch_info.hash(),
last_batch_number = ?prev_batch_info.batch_number,
Expand All @@ -113,6 +138,19 @@ pub fn encode_commit_batch_data(
// Prefixed by current encoding version as expected by protocol
[[V30_ENCODING_VERSION].to_vec(), encoded_data].concat()
}
31 => {
let commit_batch_info = IExecutor::CommitBatchInfoZKsyncOS::from(commit_info.clone());
tracing::debug!(
last_batch_hash = ?prev_batch_info.hash(),
last_batch_number = ?prev_batch_info.batch_number,
new_batch_number = ?commit_batch_info.batchNumber,
"preparing commit calldata"
);
let encoded_data = (stored_batch_info, vec![commit_batch_info]).abi_encode_params();

// Prefixed by current encoding version as expected by protocol
[[V31_ENCODING_VERSION].to_vec(), encoded_data].concat()
}
_ => panic!("Unsupported protocol version: {protocol_version_minor}"),
}
}
22 changes: 22 additions & 0 deletions lib/contract_interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ alloy::sol! {
uint64 batchNumber;
bytes32 newStateCommitment;
uint256 numberOfLayer1Txs;
uint256 numberOfLayer2Txs;
bytes32 priorityOperationsHash;
bytes32 dependencyRootsRollingHash;
bytes32 l2LogsTreeRoot;
Expand Down Expand Up @@ -313,6 +314,27 @@ alloy::sol! {
}
}

// taken from v30 version of `IExecutor.sol`
// This format is still required to submit v30 batches before the upgrade to v31.
interface IExecutorV30 {
struct CommitBatchInfoZKsyncOS {
uint64 batchNumber;
bytes32 newStateCommitment;
uint256 numberOfLayer1Txs;
bytes32 priorityOperationsHash;
bytes32 dependencyRootsRollingHash;
bytes32 l2LogsTreeRoot;
L2DACommitmentScheme daCommitmentScheme;
bytes32 daCommitment;
uint64 firstBlockTimestamp;
uint64 firstBlockNumber;
uint64 lastBlockTimestamp;
uint64 lastBlockNumber;
uint256 chainId;
bytes operatorDAInput;
}
}

// `IL1GenesisUpgrade.sol`
interface IL1GenesisUpgrade {
event GenesisUpgrade(
Expand Down
52 changes: 51 additions & 1 deletion lib/contract_interface/src/models.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{IExecutor, IExecutorV29};
use crate::{IExecutor, IExecutorV29, IExecutorV30};
use alloy::primitives::{B256, Bytes, U256, keccak256};
use alloy::sol_types::SolValue;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -147,6 +147,8 @@ pub struct CommitBatchInfo {
pub batch_number: u64,
pub new_state_commitment: B256,
pub number_of_layer1_txs: u64,
#[serde(default)]
pub number_of_layer2_txs: u64,
pub priority_operations_hash: B256,
pub dependency_roots_rolling_hash: B256,
pub l2_to_l1_logs_root_hash: B256,
Expand Down Expand Up @@ -175,6 +177,7 @@ impl From<CommitBatchInfo> for IExecutor::CommitBatchInfoZKsyncOS {
value.batch_number,
value.new_state_commitment,
U256::from(value.number_of_layer1_txs),
U256::from(value.number_of_layer2_txs),
value.priority_operations_hash,
value.dependency_roots_rolling_hash,
value.l2_to_l1_logs_root_hash,
Expand Down Expand Up @@ -212,12 +215,58 @@ impl From<CommitBatchInfo> for IExecutorV29::CommitBatchInfoZKsyncOS {
}
}

impl From<CommitBatchInfo> for IExecutorV30::CommitBatchInfoZKsyncOS {
fn from(value: CommitBatchInfo) -> Self {
IExecutorV30::CommitBatchInfoZKsyncOS::from((
value.batch_number,
value.new_state_commitment,
U256::from(value.number_of_layer1_txs),
value.priority_operations_hash,
value.dependency_roots_rolling_hash,
value.l2_to_l1_logs_root_hash,
value.l2_da_commitment_scheme.into(),
value.da_commitment,
value.first_block_timestamp,
// It is expected that for all the newly sent batches this field is always present.
value.first_block_number.unwrap(),
value.last_block_timestamp,
// It is expected that for all the newly sent batches this field is always present.
value.last_block_number.unwrap(),
U256::from(value.chain_id),
Bytes::from(value.operator_da_input),
))
}
}

impl From<IExecutor::CommitBatchInfoZKsyncOS> for CommitBatchInfo {
fn from(value: IExecutor::CommitBatchInfoZKsyncOS) -> Self {
Self {
batch_number: value.batchNumber,
new_state_commitment: value.newStateCommitment,
number_of_layer1_txs: value.numberOfLayer1Txs.to::<u64>(),
number_of_layer2_txs: value.numberOfLayer2Txs.to::<u64>(),
priority_operations_hash: value.priorityOperationsHash,
dependency_roots_rolling_hash: value.dependencyRootsRollingHash,
l2_to_l1_logs_root_hash: value.l2LogsTreeRoot,
l2_da_commitment_scheme: value.daCommitmentScheme.into(),
da_commitment: value.daCommitment,
first_block_timestamp: value.firstBlockTimestamp,
first_block_number: Some(value.firstBlockNumber),
last_block_timestamp: value.lastBlockTimestamp,
last_block_number: Some(value.lastBlockNumber),
chain_id: value.chainId.to::<u64>(),
operator_da_input: value.operatorDAInput.as_ref().to_vec(),
}
}
}

impl From<IExecutorV30::CommitBatchInfoZKsyncOS> for CommitBatchInfo {
fn from(value: IExecutorV30::CommitBatchInfoZKsyncOS) -> Self {
Self {
batch_number: value.batchNumber,
new_state_commitment: value.newStateCommitment,
number_of_layer1_txs: value.numberOfLayer1Txs.to::<u64>(),
number_of_layer2_txs: 0,
priority_operations_hash: value.priorityOperationsHash,
dependency_roots_rolling_hash: value.dependencyRootsRollingHash,
l2_to_l1_logs_root_hash: value.l2LogsTreeRoot,
Expand All @@ -239,6 +288,7 @@ impl fmt::Debug for CommitBatchInfo {
.field("batch_number", &self.batch_number)
.field("new_state_commitment", &self.new_state_commitment)
.field("number_of_layer1_txs", &self.number_of_layer1_txs)
.field("number_of_layer2_txs", &self.number_of_layer2_txs)
.field("priority_operations_hash", &self.priority_operations_hash)
.field(
"dependency_roots_rolling_hash",
Expand Down
1 change: 1 addition & 0 deletions lib/l1_watcher/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ serde.workspace = true
futures.workspace = true
backon.workspace = true
rangemap.workspace = true
blake2.workspace = true
12 changes: 0 additions & 12 deletions lib/l1_watcher/src/factory_deps/README.md

This file was deleted.

Loading