From cc0ea832d29607aae6fc8dba46facae248ba040c Mon Sep 17 00:00:00 2001 From: koloz193 Date: Tue, 2 Jul 2024 07:35:34 -0400 Subject: [PATCH] feat(pubdata): send call encoded pubdata (#2360) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## What ❔ Update server to encode pubdata with parameters for DA validator call. ## Why ❔ ## Checklist - [ ] 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`. --- Cargo.lock | 1 + contracts | 2 +- core/lib/contracts/src/lib.rs | 7 ++ core/lib/multivm/Cargo.toml | 2 + .../src/versions/vm_latest/tests/block_tip.rs | 9 +- .../versions/vm_latest/tests/l1_messenger.rs | 4 +- .../vm_latest/types/internals/pubdata.rs | 87 ++++++++++++++++++ etc/env/base/chain.toml | 4 +- etc/env/base/contracts.toml | 4 +- .../fee_estimate.yul/fee_estimate.yul.zbin | Bin 76448 -> 76448 bytes .../gas_test.yul/gas_test.yul.zbin | Bin 72544 -> 72544 bytes .../playground_batch.yul.zbin | Bin 76640 -> 76640 bytes .../proved_batch.yul/proved_batch.yul.zbin | Bin 73056 -> 73056 bytes prover/Cargo.lock | 1 + 14 files changed, 108 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index aee185e2dca1..6ebfcd63b9b5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3727,6 +3727,7 @@ dependencies = [ "zk_evm 1.5.0", "zksync_contracts", "zksync_eth_signer", + "zksync_mini_merkle_tree", "zksync_state", "zksync_system_constants", "zksync_test_account", diff --git a/contracts b/contracts index 8c0b80c27cb6..7cb54b53901f 160000 --- a/contracts +++ b/contracts @@ -1 +1 @@ -Subproject commit 8c0b80c27cb67a6cc409f4d40ebb42a37c97b3ea +Subproject commit 7cb54b53901fb6f8dfbe615689c53950461cbe9a diff --git a/core/lib/contracts/src/lib.rs b/core/lib/contracts/src/lib.rs index 8313cf8dc93e..01f941d1d077 100644 --- a/core/lib/contracts/src/lib.rs +++ b/core/lib/contracts/src/lib.rs @@ -113,6 +113,13 @@ pub fn load_sys_contract(contract_name: &str) -> Contract { )) } +pub fn load_sys_contract_interface(contract_name: &str) -> Contract { + load_contract(format!( + "contracts/system-contracts/artifacts-zk/contracts-preprocessed/interfaces/{0}.sol/{0}.json", + contract_name + )) +} + pub fn read_contract_abi(path: impl AsRef + std::fmt::Debug) -> String { read_file_to_json_value(path)["abi"] .as_str() diff --git a/core/lib/multivm/Cargo.toml b/core/lib/multivm/Cargo.toml index d76257d342d9..575c3c84bba5 100644 --- a/core/lib/multivm/Cargo.toml +++ b/core/lib/multivm/Cargo.toml @@ -28,6 +28,8 @@ zksync_contracts.workspace = true zksync_utils.workspace = true zksync_system_constants.workspace = true +zksync_mini_merkle_tree.workspace = true + anyhow.workspace = true hex.workspace = true diff --git a/core/lib/multivm/src/versions/vm_latest/tests/block_tip.rs b/core/lib/multivm/src/versions/vm_latest/tests/block_tip.rs index a667a52a4526..78136602dae2 100644 --- a/core/lib/multivm/src/versions/vm_latest/tests/block_tip.rs +++ b/core/lib/multivm/src/versions/vm_latest/tests/block_tip.rs @@ -393,8 +393,7 @@ fn test_dry_run_upper_bound() { .max() .unwrap(); assert!( - // FIXME: use the 1.5 factor after optimizations - max_used_gas.0 <= BOOTLOADER_BATCH_TIP_OVERHEAD, + max_used_gas.0 * 3 / 2 <= BOOTLOADER_BATCH_TIP_OVERHEAD, "BOOTLOADER_BATCH_TIP_OVERHEAD is too low for {} with result {}, BOOTLOADER_BATCH_TIP_OVERHEAD = {}", max_used_gas.1, max_used_gas.0, @@ -407,8 +406,7 @@ fn test_dry_run_upper_bound() { .max() .unwrap(); assert!( - // FIXME: use the 1.5 factor after optimizations - circuit_statistics.0 <= BOOTLOADER_BATCH_TIP_CIRCUIT_STATISTICS_OVERHEAD as u64, + circuit_statistics.0 * 3 / 2 <= BOOTLOADER_BATCH_TIP_CIRCUIT_STATISTICS_OVERHEAD as u64, "BOOTLOADER_BATCH_TIP_CIRCUIT_STATISTICS_OVERHEAD is too low for {} with result {}, BOOTLOADER_BATCH_TIP_CIRCUIT_STATISTICS_OVERHEAD = {}", circuit_statistics.1, circuit_statistics.0, @@ -421,8 +419,7 @@ fn test_dry_run_upper_bound() { .max() .unwrap(); assert!( - // FIXME: use the 1.5 factor after optimizations - execution_metrics_size.0 <= BOOTLOADER_BATCH_TIP_METRICS_SIZE_OVERHEAD as u64, + execution_metrics_size.0 * 3 / 2 <= BOOTLOADER_BATCH_TIP_METRICS_SIZE_OVERHEAD as u64, "BOOTLOADER_BATCH_TIP_METRICS_SIZE_OVERHEAD is too low for {} with result {}, BOOTLOADER_BATCH_TIP_METRICS_SIZE_OVERHEAD = {}", execution_metrics_size.1, execution_metrics_size.0, diff --git a/core/lib/multivm/src/versions/vm_latest/tests/l1_messenger.rs b/core/lib/multivm/src/versions/vm_latest/tests/l1_messenger.rs index 544414cf520f..293d24dd5c01 100644 --- a/core/lib/multivm/src/versions/vm_latest/tests/l1_messenger.rs +++ b/core/lib/multivm/src/versions/vm_latest/tests/l1_messenger.rs @@ -19,8 +19,8 @@ use crate::{ }, }; -pub(crate) const L2_DA_VALIDATOR_OUTPUT_HASH_KEY: usize = 13; -pub(crate) const USED_L2_DA_VALIDATOR_ADDRESS_KEY: usize = 14; +pub(crate) const L2_DA_VALIDATOR_OUTPUT_HASH_KEY: usize = 7; +pub(crate) const USED_L2_DA_VALIDATOR_ADDRESS_KEY: usize = 8; pub(crate) fn encoded_uncompressed_state_diffs(input: &PubdataInput) -> Vec { let mut result = vec![]; diff --git a/core/lib/multivm/src/versions/vm_latest/types/internals/pubdata.rs b/core/lib/multivm/src/versions/vm_latest/types/internals/pubdata.rs index 6ee57f8f1ec5..1cde9a4f3019 100644 --- a/core/lib/multivm/src/versions/vm_latest/types/internals/pubdata.rs +++ b/core/lib/multivm/src/versions/vm_latest/types/internals/pubdata.rs @@ -1,7 +1,15 @@ +use ethabi::Token; +use zksync_contracts::load_sys_contract_interface; +use zksync_mini_merkle_tree::MiniMerkleTree; use zksync_types::{ + ethabi, event::L1MessengerL2ToL1Log, + l2_to_l1_log::l2_to_l1_logs_tree_size, + web3::keccak256, writes::{compress_state_diffs, StateDiffRecord}, + ProtocolVersionId, }; +use zksync_utils::bytecode::hash_bytecode; /// Struct based on which the pubdata blob is formed #[derive(Debug, Clone, Default)] @@ -44,6 +52,7 @@ fn encode_user_logs(user_logs: Vec) -> Vec { impl PubdataBuilder for RollupPubdataBuilder { fn build_pubdata(&self, input: PubdataInput, l2_version: bool) -> Vec { let mut l1_messenger_pubdata = vec![]; + let mut l2_da_header = vec![]; let PubdataInput { user_logs, @@ -52,6 +61,18 @@ impl PubdataBuilder for RollupPubdataBuilder { state_diffs, } = input; + if l2_version { + let chained_log_hash = build_chained_log_hash(user_logs.clone()); + let log_root_hash = build_logs_root(user_logs.clone()); + let chained_msg_hash = build_chained_message_hash(l2_to_l1_messages.clone()); + let chained_bytecodes_hash = build_chained_bytecode_hash(published_bytecodes.clone()); + + l2_da_header.push(Token::FixedBytes(chained_log_hash)); + l2_da_header.push(Token::FixedBytes(log_root_hash)); + l2_da_header.push(Token::FixedBytes(chained_msg_hash)); + l2_da_header.push(Token::FixedBytes(chained_bytecodes_hash)); + } + l1_messenger_pubdata.extend(encode_user_logs(user_logs)); // Encoding L2->L1 messages @@ -80,6 +101,18 @@ impl PubdataBuilder for RollupPubdataBuilder { for state_diff in state_diffs { l1_messenger_pubdata.extend(state_diff.encode_padded()); } + + let func_selector = load_sys_contract_interface("IL2DAValidator") + .function("validatePubdata") + .expect("validatePubdata Function does not exist on IL2DAValidator") + .short_signature() + .to_vec(); + + l2_da_header.push(ethabi::Token::Bytes(l1_messenger_pubdata)); + + l1_messenger_pubdata = [func_selector, ethabi::encode(&l2_da_header)] + .concat() + .to_vec(); } l1_messenger_pubdata @@ -100,6 +133,60 @@ impl PubdataBuilder for ValidiumPubdataBuilder { } } +fn build_chained_log_hash(user_logs: Vec) -> Vec { + let mut chained_log_hash = vec![0u8; 32]; + + for log in user_logs { + let log_bytes = log.packed_encoding(); + let hash = keccak256(&log_bytes); + + chained_log_hash = keccak256(&[chained_log_hash, hash.to_vec()].concat()).to_vec(); + } + + chained_log_hash +} + +fn build_logs_root(user_logs: Vec) -> Vec { + let logs = user_logs.iter().map(|log| { + let encoded = log.packed_encoding(); + let mut slice = [0u8; 88]; + slice.copy_from_slice(&encoded); + slice + }); + MiniMerkleTree::new( + logs, + Some(l2_to_l1_logs_tree_size(ProtocolVersionId::latest())), + ) + .merkle_root() + .as_bytes() + .to_vec() +} + +fn build_chained_message_hash(l2_to_l1_messages: Vec>) -> Vec { + let mut chained_msg_hash = vec![0u8; 32]; + + for msg in l2_to_l1_messages { + let hash = keccak256(&msg); + + chained_msg_hash = keccak256(&[chained_msg_hash, hash.to_vec()].concat()).to_vec(); + } + + chained_msg_hash +} + +fn build_chained_bytecode_hash(published_bytecodes: Vec>) -> Vec { + let mut chained_bytecode_hash = vec![0u8; 32]; + + for bytecode in published_bytecodes { + let hash = hash_bytecode(&bytecode).to_fixed_bytes(); + + chained_bytecode_hash = + keccak256(&[chained_bytecode_hash, hash.to_vec()].concat()).to_vec(); + } + + chained_bytecode_hash +} + #[cfg(test)] mod tests { diff --git a/etc/env/base/chain.toml b/etc/env/base/chain.toml index 834c69e48f64..44b55462713c 100644 --- a/etc/env/base/chain.toml +++ b/etc/env/base/chain.toml @@ -90,8 +90,8 @@ fee_model_version = "V1" validation_computational_gas_limit = 300000 save_call_traces = true -bootloader_hash = "0x010008ebe92cafb31d44d1855fe97af98550f0f58bced18f29a7d74efd863b3f" -default_aa_hash = "0x0100055df3908fa6efcfa0bdfce813d4224f4657b696bcfdea0c1dbac659ecf4" +bootloader_hash = "0x010008eba3fb775aa81ef7cc2dbfe6cb84559f4a4f594522357ddd843cb87841" +default_aa_hash = "0x0100055d16f15378f8338471eef7af52def45925e83f98affc0ee7883e34c494" [chain.operations_manager] # Sleep time when there is no new input data diff --git a/etc/env/base/contracts.toml b/etc/env/base/contracts.toml index 81bf8506d1a2..8a6b90b92406 100644 --- a/etc/env/base/contracts.toml +++ b/etc/env/base/contracts.toml @@ -28,8 +28,8 @@ RECURSION_NODE_LEVEL_VK_HASH = "0x1186ec268d49f1905f8d9c1e9d39fc33e98c74f91d91a2 RECURSION_LEAF_LEVEL_VK_HASH = "0x101e08b00193e529145ee09823378ef51a3bc8966504064f1f6ba3f1ba863210" RECURSION_CIRCUITS_SET_VKS_HASH = "0x18c1639094f58177409186e8c48d9f577c9410901d2f1d486b3e7d6cf553ae4c" GENESIS_TX_HASH = "0xb99ebfea46cbe05a21cd80fe5597d97b204befc52a16303f579c607dc1ac2e2e" -GENESIS_ROOT = "0x5e597aa72d5f475debf6d334e591a8fe17e06117b538cf1e42e524540f1826f9" -GENESIS_BATCH_COMMITMENT = "0x327d772f5b3d06e56da8dcc9eb34dfa35ecbc82b231a50c02b626815dbce2e2b" +GENESIS_ROOT = "0x38231d1956c379d89392ef85dd92256c76e48f32ba9fb6a0a96a7c97512f6321" +GENESIS_BATCH_COMMITMENT = "0xde5b29f5278bcd72ca559852409b99fd9cbf2f801a92444f1a7dec2fb638097e" PRIORITY_TX_MAX_GAS_LIMIT = 72000000 DEPLOY_L2_BRIDGE_COUNTERPART_GAS_LIMIT = 10000000 GENESIS_ROLLUP_LEAF_INDEX = "64" diff --git a/etc/multivm_bootloaders/vm_sync_layer/fee_estimate.yul/fee_estimate.yul.zbin b/etc/multivm_bootloaders/vm_sync_layer/fee_estimate.yul/fee_estimate.yul.zbin index 16702f1519f67e90ed0d0d32145c09e2126db6e6..e7b2f9201f571257cfe4335e72eeadd2150755b4 100644 GIT binary patch delta 85 zcmV-b0IL6>)dZl`1h7*Z0SU8I8_(+sp^>nDd-P)T++A;N5?zQ=LqFERf7^GrHW r*tb9j0YD=l?!tkL-iG_!b2|y;_JAPHZ^IfaEI-rG9t*UZWlr)?sqZLw delta 85 zcmV-b0IL6>)dZl`1h7*Z0S~iO8_(+>z=-VS r!M8vM0YD=l<^SHD$XrXq2wP+eYM$^J9me~Cc>_X37Z=4&Dz=|`4{Rvr diff --git a/etc/multivm_bootloaders/vm_sync_layer/gas_test.yul/gas_test.yul.zbin b/etc/multivm_bootloaders/vm_sync_layer/gas_test.yul/gas_test.yul.zbin index 6e70b3ffb2acac9c28ab04e9de4fb12f5dc65ee8..98cd6d5cc5afcb17442586ce551fff6ddeb3a5ac 100644 GIT binary patch delta 83 zcmV-Z0IdJuwglj|1h9i10SU8&9$(rWr>sp^>nDd-P)T++A;N5?zQ=LqFERf7^GrHW p*s}RRA|OITIM+nC#>4pDEF12JHus3ChlJCsOoUq{XV+=0J1C8$DEI&X delta 83 zcmV-Z0IdJuwglj|1h9i10S~i;9$(rWrNg(>z=-VS p!Ls>4A|P7GrQ|R409^K~D{cY~`j`HWBB{4JK>ULowbjg?uO9~bE0X{K diff --git a/etc/multivm_bootloaders/vm_sync_layer/playground_batch.yul/playground_batch.yul.zbin b/etc/multivm_bootloaders/vm_sync_layer/playground_batch.yul/playground_batch.yul.zbin index 7f5d18586640da73353595407499095c613e079d..285c51b6a2495975e3869af00849a881245b2f41 100644 GIT binary patch delta 85 zcmV-b0IL7s)&$_z1h7*Z0SU8I8!zk~r>sp^>nDd-P)T++A;N5?zQ=LqFERf7^GrHW r*tehu0iYuw9#XkjT44&SlDTu^ZCKf)ah2@=fT^9Pmq>BDxd9r*kSr(& delta 85 zcmV-b0IL7s)&$_z1h7*Z0S~iO8!zk~rNg(>z=-VS r!MC6X0iYuw{CW>1@IZ9=Pwbhpr|wushYgK|W@thq1NuyXjo=CjCQ&J$ diff --git a/etc/multivm_bootloaders/vm_sync_layer/proved_batch.yul/proved_batch.yul.zbin b/etc/multivm_bootloaders/vm_sync_layer/proved_batch.yul/proved_batch.yul.zbin index 9f8c964e315bdaab5a74c1020b0cfc2d5f078840..9b78961644188d50d8f0714300784b3cbb091c50 100644 GIT binary patch delta 84 zcmV-a0IUDtx&+|51h9i10SU8&9^cy?r>sp^>nDd-P)T++A;N5?zQ=LqFERf7^GrHW q*t3B8z#<^x`-g`-&>z=-VS q!Lxw+z#<^h`6gE1)64eKA|ZeDC(0@R)Ky*ouurT)k3QR&2e${4x-JF) diff --git a/prover/Cargo.lock b/prover/Cargo.lock index e58e97f11af8..f83fe16a1df3 100644 --- a/prover/Cargo.lock +++ b/prover/Cargo.lock @@ -3881,6 +3881,7 @@ dependencies = [ "zk_evm 1.4.1", "zk_evm 1.5.0", "zksync_contracts", + "zksync_mini_merkle_tree", "zksync_state", "zksync_system_constants", "zksync_types",