Skip to content

Commit

Permalink
chore(types): add support for 6 blobs (matter-labs#1565)
Browse files Browse the repository at this point in the history
## What ❔

<!-- What are the changes this PR brings about? -->
<!-- Example: This PR adds a PR template to the repo. -->
<!-- (For bigger PRs adding more context is appreciated) -->

## Why ❔

<!-- Why are these changes done? What goal do they contribute to? What
are the principles behind them? -->
<!-- Example: PR templates ensure PR reviewers, observers, and future
iterators are in context about the evolution of repos. -->

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [ ] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [ ] Tests for the changes have been added / updated.
- [ ] Documentation comments have been added / updated.
- [ ] Code has been formatted via `zk fmt` and `zk lint`.
- [ ] Spellcheck has been run via `zk spellcheck`.
- [ ] Linkcheck has been run via `zk linkcheck`.

---------

Co-authored-by: Stanislav Breadless <[email protected]>
  • Loading branch information
koloz193 and StanislavBreadless authored Apr 5, 2024
1 parent 030d447 commit 1c13f7f
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 62 deletions.
2 changes: 1 addition & 1 deletion checks-config/links.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@
}
],
"aliveStatusCodes": [0, 200, 206, 304]
}
}
14 changes: 14 additions & 0 deletions core/lib/basic_types/src/protocol_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ impl ProtocolVersionId {
self == &ProtocolVersionId::Version20
}

pub fn is_pre_1_4_1(&self) -> bool {
self < &ProtocolVersionId::Version20
}

pub fn is_post_1_4_1(&self) -> bool {
self >= &ProtocolVersionId::Version20
}
Expand All @@ -122,11 +126,21 @@ impl ProtocolVersionId {
self < &ProtocolVersionId::Version21
}

pub fn is_1_4_2(&self) -> bool {
self == &ProtocolVersionId::Version21 || self == &ProtocolVersionId::Version22
}

pub fn is_pre_1_5_0(&self) -> bool {
// In the current codebase all the protocol versions are pre-1.5.0.
// This method will be updated once the v1.5.0 is added to the server
true
}

pub fn is_post_1_5_0(&self) -> bool {
// In the current codebase all the protocol versions are pre-1.5.0.
// This method will be updated once the v1.5.0 is added to the server
false
}
}

impl Default for ProtocolVersionId {
Expand Down
4 changes: 4 additions & 0 deletions core/lib/dal/src/models/storage_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ impl TryInto<L1BatchMetadata> for StorageL1Batch {
.default_aa_code_hash
.ok_or(StorageL1BatchConvertError::Incomplete)?,
),
protocol_version: self
.protocol_version
.map(|v| (v as u16).try_into().unwrap())
.ok_or(StorageL1BatchConvertError::Incomplete)?,
},
state_diffs_compressed: self.compressed_state_diffs.unwrap_or_default(),
events_queue_commitment: self.events_queue_commitment.map(|v| H256::from_slice(&v)),
Expand Down
18 changes: 8 additions & 10 deletions core/lib/l1_contract_interface/src/i_executor/commit/kzg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,24 +267,22 @@ impl KzgInfo {
}
}

pub fn pubdata_to_blob_commitments(pubdata_input: &[u8]) -> [H256; 2] {
pub fn pubdata_to_blob_commitments(num_blobs: usize, pubdata_input: &[u8]) -> Vec<H256> {
assert!(
pubdata_input.len() <= 2 * ZK_SYNC_BYTES_PER_BLOB,
"Pubdata length exceeds size of 2 blobs"
pubdata_input.len() <= num_blobs * ZK_SYNC_BYTES_PER_BLOB,
"Pubdata length exceeds size of blobs"
);

let blob_commitments = pubdata_input
let mut blob_commitments = pubdata_input
.chunks(ZK_SYNC_BYTES_PER_BLOB)
.map(|blob| {
let kzg_info = KzgInfo::new(blob);
H256(kzg_info.to_blob_commitment())
})
.collect::<Vec<_>>();

// If length of `pubdata_input` is less than or equal to `ZK_SYNC_BYTES_PER_BLOB` (126976)
// then only one blob will be used and 32 zero bytes will be used as a commitment for the second blob.
[
blob_commitments.get(0).copied().unwrap_or_default(),
blob_commitments.get(1).copied().unwrap_or_default(),
]
// Depending on the length of `pubdata_input`, we will sending the ceiling of
// `pubdata_input / ZK_SYNC_BYTES_PER_BLOB (126976)` blobs. The rest of the blob commitments will be 32 zero bytes.
blob_commitments.resize(num_blobs, H256::zero());
blob_commitments
}
23 changes: 23 additions & 0 deletions core/lib/types/src/blob.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use zksync_basic_types::protocol_version::ProtocolVersionId;

/// Returns the number of blobs supported by each VM version
pub fn num_blobs_required(protocol_version: &ProtocolVersionId) -> usize {
if protocol_version.is_pre_1_4_1() {
0
} else if protocol_version.is_pre_1_5_0() {
2
} else {
16
}
}

/// Returns the number of blobs created within each VM version
pub fn num_blobs_created(protocol_version: &ProtocolVersionId) -> usize {
if protocol_version.is_pre_1_4_2() {
0
} else if protocol_version.is_pre_1_5_0() {
2
} else {
6
}
}
72 changes: 28 additions & 44 deletions core/lib/types/src/commitment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ use serde::{Deserialize, Serialize};
use zksync_contracts::BaseSystemContractsHashes;
use zksync_mini_merkle_tree::MiniMerkleTree;
use zksync_system_constants::{
BLOB1_LINEAR_HASH_KEY, BLOB2_LINEAR_HASH_KEY, KNOWN_CODES_STORAGE_ADDRESS,
L2_TO_L1_LOGS_TREE_ROOT_KEY, PUBDATA_CHUNK_PUBLISHER_ADDRESS, STATE_DIFF_HASH_KEY,
KNOWN_CODES_STORAGE_ADDRESS, L2_TO_L1_LOGS_TREE_ROOT_KEY, STATE_DIFF_HASH_KEY,
ZKPORTER_IS_AVAILABLE,
};
use zksync_utils::u256_to_h256;

use crate::{
blob::num_blobs_required,
block::{L1BatchHeader, L1BatchTreeData},
l2_to_l1_log::{l2_to_l1_logs_tree_size, L2ToL1Log, SystemL2ToL1Log, UserL2ToL1Log},
l2_to_l1_log::{
l2_to_l1_logs_tree_size, parse_system_logs_for_blob_hashes, L2ToL1Log, SystemL2ToL1Log,
UserL2ToL1Log,
},
web3::signing::keccak256,
writes::{
compress_state_diffs, InitialStorageWrite, RepeatedStorageWrite, StateDiffRecord,
Expand Down Expand Up @@ -280,8 +283,8 @@ enum L1BatchAuxiliaryOutput {
state_diffs_compressed: Vec<u8>,
state_diffs_hash: H256,
aux_commitments: AuxCommitments,
blob_linear_hashes: [H256; 2],
blob_commitments: [H256; 2],
blob_linear_hashes: Vec<H256>,
blob_commitments: Vec<H256>,
},
}

Expand Down Expand Up @@ -354,30 +357,8 @@ impl L1BatchAuxiliaryOutput {
let state_diffs_hash = H256::from(keccak256(&(state_diffs_packed)));
let state_diffs_compressed = compress_state_diffs(state_diffs);

let blob_linear_hashes = if common_input.protocol_version.is_post_1_4_2() {
let blob1_linear_hash = system_logs.iter().find_map(|log| {
(log.0.sender == PUBDATA_CHUNK_PUBLISHER_ADDRESS
&& log.0.key == H256::from_low_u64_be(BLOB1_LINEAR_HASH_KEY as u64))
.then_some(log.0.value)
});
let blob2_linear_hash = system_logs.iter().find_map(|log| {
(log.0.sender == PUBDATA_CHUNK_PUBLISHER_ADDRESS
&& log.0.key == H256::from_low_u64_be(BLOB2_LINEAR_HASH_KEY as u64))
.then_some(log.0.value)
});
match (&blob1_linear_hash, &blob2_linear_hash) {
(Some(_), None) | (None, Some(_)) => {
panic!("Only one blob hash was found in system logs")
}
_ => {}
}
[
blob1_linear_hash.unwrap_or_else(H256::zero),
blob2_linear_hash.unwrap_or_else(H256::zero),
]
} else {
[H256::zero(), H256::zero()]
};
let blob_linear_hashes =
parse_system_logs_for_blob_hashes(&common_input.protocol_version, &system_logs);

// Sanity checks. System logs are empty for the genesis batch, so we can't do checks for it.
if !system_logs.is_empty() {
Expand Down Expand Up @@ -436,7 +417,6 @@ impl L1BatchAuxiliaryOutput {
result.extend(repeated_writes_hash.as_bytes());
}
Self::PostBoojum {
common,
system_logs_linear_hash,
state_diffs_hash,
aux_commitments,
Expand All @@ -453,17 +433,9 @@ impl L1BatchAuxiliaryOutput {
);
result.extend(aux_commitments.events_queue_commitment.as_bytes());

if common.protocol_version.is_1_4_1() {
// We are using zeroes as commitments to the KZG pubdata as per convention.
result.extend(H256::zero().as_bytes());
result.extend(H256::zero().as_bytes());
result.extend(H256::zero().as_bytes());
result.extend(H256::zero().as_bytes());
} else if common.protocol_version.is_post_1_4_2() {
result.extend(blob_linear_hashes[0].as_bytes());
result.extend(blob_commitments[0].as_bytes());
result.extend(blob_linear_hashes[1].as_bytes());
result.extend(blob_commitments[1].as_bytes());
for i in 0..blob_commitments.len() {
result.extend(blob_linear_hashes[i].as_bytes());
result.extend(blob_commitments[i].as_bytes());
}
}
}
Expand All @@ -489,15 +461,22 @@ pub struct L1BatchMetaParameters {
pub zkporter_is_available: bool,
pub bootloader_code_hash: H256,
pub default_aa_code_hash: H256,
pub protocol_version: ProtocolVersionId,
}

impl L1BatchMetaParameters {
pub fn to_bytes(&self) -> Vec<u8> {
const SERIALIZED_SIZE: usize = 4 + 1 + 32 + 32;
const SERIALIZED_SIZE: usize = 1 + 32 + 32 + 32;
let mut result = Vec::with_capacity(SERIALIZED_SIZE);
result.push(self.zkporter_is_available as u8);
result.extend(self.bootloader_code_hash.as_bytes());
result.extend(self.default_aa_code_hash.as_bytes());

if self.protocol_version.is_post_1_5_0() {
// EVM simulator hash for now is the same as the default AA hash.
result.extend(self.default_aa_code_hash.as_bytes());
}

result
}

Expand Down Expand Up @@ -564,6 +543,7 @@ impl L1BatchCommitment {
zkporter_is_available: ZKPORTER_IS_AVAILABLE,
bootloader_code_hash: input.common().bootloader_code_hash,
default_aa_code_hash: input.common().default_aa_code_hash,
protocol_version: input.common().protocol_version,
};

Self {
Expand Down Expand Up @@ -681,7 +661,7 @@ pub enum CommitmentInput {
system_logs: Vec<SystemL2ToL1Log>,
state_diffs: Vec<StateDiffRecord>,
aux_commitments: AuxCommitments,
blob_commitments: [H256; 2],
blob_commitments: Vec<H256>,
},
}

Expand Down Expand Up @@ -722,7 +702,11 @@ impl CommitmentInput {
events_queue_commitment: H256::zero(),
bootloader_initial_content_commitment: H256::zero(),
},
blob_commitments: [H256::zero(), H256::zero()],
blob_commitments: {
let num_blobs = num_blobs_required(&protocol_version);

vec![H256::zero(); num_blobs]
},
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,8 @@
"meta_parameters": {
"zkporter_is_available": false,
"bootloader_code_hash": "0x010007ed0e328b940e241f7666a6303b7ffd4e3fd7e8c154d6e7556befe6cd6d",
"default_aa_code_hash": "0x0100055b7a8be90522251be8be1a186464d056462973502ac8a0437c85e4d2a9"
"default_aa_code_hash": "0x0100055b7a8be90522251be8be1a186464d056462973502ac8a0437c85e4d2a9",
"protocol_version": "Version20"
},
"auxiliary_output": {
"PostBoojum": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,8 @@
"meta_parameters": {
"zkporter_is_available": false,
"bootloader_code_hash": "0x010007ed0e328b940e241f7666a6303b7ffd4e3fd7e8c154d6e7556befe6cd6d",
"default_aa_code_hash": "0x0100055b7a8be90522251be8be1a186464d056462973502ac8a0437c85e4d2a9"
"default_aa_code_hash": "0x0100055b7a8be90522251be8be1a186464d056462973502ac8a0437c85e4d2a9",
"protocol_version": "Version21"
},
"auxiliary_output": {
"PostBoojum": {
Expand Down
3 changes: 2 additions & 1 deletion core/lib/types/src/commitment/tests/pre_boojum_test.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@
"meta_parameters": {
"zkporter_is_available": false,
"bootloader_code_hash": "0x010007ed0e328b940e241f7666a6303b7ffd4e3fd7e8c154d6e7556befe6cd6d",
"default_aa_code_hash": "0x0100055b7a8be90522251be8be1a186464d056462973502ac8a0437c85e4d2a9"
"default_aa_code_hash": "0x0100055b7a8be90522251be8be1a186464d056462973502ac8a0437c85e4d2a9",
"protocol_version": "Version17"
},
"auxiliary_output": {
"PreBoojum": {
Expand Down
41 changes: 39 additions & 2 deletions core/lib/types/src/l2_to_l1_log.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use serde::{Deserialize, Serialize};
use zksync_system_constants::{BLOB1_LINEAR_HASH_KEY, PUBDATA_CHUNK_PUBLISHER_ADDRESS};

use crate::{commitment::SerializeCommitment, Address, ProtocolVersionId, H256};
use crate::{
blob::{num_blobs_created, num_blobs_required},
commitment::SerializeCommitment,
Address, ProtocolVersionId, H256,
};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default, Eq)]
pub struct L2ToL1Log {
Expand Down Expand Up @@ -61,14 +66,46 @@ pub fn l2_to_l1_logs_tree_size(protocol_version: ProtocolVersionId) -> usize {
pub const PRE_BOOJUM_L2_L1_LOGS_TREE_SIZE: usize = 512;
pub const VM_1_4_0_L2_L1_LOGS_TREE_SIZE: usize = 2048;
pub const VM_1_4_2_L2_L1_LOGS_TREE_SIZE: usize = 4096;
pub const VM_1_5_0_L2_L1_LOGS_TREE_SIZE: usize = 16384;

if protocol_version.is_pre_boojum() {
PRE_BOOJUM_L2_L1_LOGS_TREE_SIZE
} else if protocol_version.is_1_4_0() || protocol_version.is_1_4_1() {
VM_1_4_0_L2_L1_LOGS_TREE_SIZE
} else {
} else if protocol_version.is_pre_1_5_0() {
VM_1_4_2_L2_L1_LOGS_TREE_SIZE
} else {
VM_1_5_0_L2_L1_LOGS_TREE_SIZE
}
}

/// Returns the blob hashes parsed out from the system logs
pub fn parse_system_logs_for_blob_hashes(
protocol_version: &ProtocolVersionId,
system_logs: &[SystemL2ToL1Log],
) -> Vec<H256> {
let num_required_blobs = num_blobs_required(protocol_version) as u32;
let num_created_blobs = num_blobs_created(protocol_version) as u32;

if num_created_blobs == 0 {
return vec![H256::zero(); num_required_blobs as usize];
}

let mut blob_hashes = system_logs
.iter()
.filter(|log| {
log.0.sender == PUBDATA_CHUNK_PUBLISHER_ADDRESS
&& log.0.key >= H256::from_low_u64_be(BLOB1_LINEAR_HASH_KEY as u64)
&& log.0.key
< H256::from_low_u64_be((BLOB1_LINEAR_HASH_KEY + num_created_blobs) as u64)
})
.map(|log| (log.0.key, log.0.value))
.collect::<Vec<(H256, H256)>>();

blob_hashes.sort_unstable_by_key(|(k, _)| *k);
let mut blob_hashes = blob_hashes.iter().map(|(_, v)| *v).collect::<Vec<H256>>();
blob_hashes.resize(num_required_blobs as usize, H256::zero());
blob_hashes
}

#[cfg(test)]
Expand Down
1 change: 1 addition & 0 deletions core/lib/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub use crate::{Nonce, H256, U256, U64};
pub type SerialId = u64;

pub mod aggregated_operations;
pub mod blob;
pub mod block;
pub mod circuit;
pub mod commitment;
Expand Down
5 changes: 3 additions & 2 deletions core/lib/zksync_core/src/commitment_generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use zksync_dal::{ConnectionPool, Core, CoreDal};
use zksync_health_check::{Health, HealthStatus, HealthUpdater, ReactiveHealthCheck};
use zksync_l1_contract_interface::i_executor::commit::kzg::pubdata_to_blob_commitments;
use zksync_types::{
blob::num_blobs_required,
commitment::{AuxCommitments, CommitmentCommonInput, CommitmentInput, L1BatchCommitment},
event::convert_vm_events_to_log_queries,
writes::{InitialStorageWrite, RepeatedStorageWrite, StateDiffRecord},
Expand Down Expand Up @@ -230,9 +231,9 @@ impl CommitmentGenerator {
format!("`pubdata_input` is missing for L1 batch #{l1_batch_number}")
})?;

pubdata_to_blob_commitments(&pubdata_input)
pubdata_to_blob_commitments(num_blobs_required(&protocol_version), &pubdata_input)
} else {
[H256::zero(), H256::zero()]
vec![H256::zero(); num_blobs_required(&protocol_version)]
};

CommitmentInput::PostBoojum {
Expand Down
1 change: 1 addition & 0 deletions core/lib/zksync_core/src/eth_sender/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ fn default_l1_batch_metadata() -> L1BatchMetadata {
zkporter_is_available: false,
bootloader_code_hash: Default::default(),
default_aa_code_hash: Default::default(),
protocol_version: Default::default(),
},
aux_data_hash: Default::default(),
meta_parameters_hash: Default::default(),
Expand Down
1 change: 1 addition & 0 deletions core/lib/zksync_core/src/utils/testonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub(crate) fn create_l1_batch_metadata(number: u32) -> L1BatchMetadata {
zkporter_is_available: ZKPORTER_IS_AVAILABLE,
bootloader_code_hash: BaseSystemContractsHashes::default().bootloader,
default_aa_code_hash: BaseSystemContractsHashes::default().default_aa,
protocol_version: ProtocolVersionId::latest(),
},
aux_data_hash: H256::zero(),
meta_parameters_hash: H256::zero(),
Expand Down

0 comments on commit 1c13f7f

Please sign in to comment.