From 622d4b159716fb9e3ecf89a1114c34766ed1ae14 Mon Sep 17 00:00:00 2001 From: Harald Hoyer Date: Wed, 19 Jun 2024 15:09:01 +0200 Subject: [PATCH] feat(tee_verifier_input_producer): use `FactoryDepsDal::get_factory_deps() (#2271) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## What ❔ Use `FactoryDepsDal::get_factory_deps()` in the tee_verifier_input_producer crate. ## Why ❔ This optimizes getting the system contracts and gets rid of a couple of workarounds. ## Checklist - [x] 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. - [x] Code has been formatted via `zk fmt` and `zk lint`. - [x] Spellcheck has been run via `zk spellcheck`. Signed-off-by: Harald Hoyer --- Cargo.lock | 2 - .../tee_verifier_input_producer/Cargo.toml | 2 - .../tee_verifier_input_producer/src/lib.rs | 70 +++++++------------ 3 files changed, 24 insertions(+), 50 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 774246abd125..72d7cb39295d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9415,7 +9415,6 @@ version = "0.1.0" dependencies = [ "anyhow", "async-trait", - "multivm", "tokio", "tracing", "vise", @@ -9424,7 +9423,6 @@ dependencies = [ "zksync_object_store", "zksync_prover_interface", "zksync_queued_job_processor", - "zksync_state", "zksync_tee_verifier", "zksync_types", "zksync_utils", diff --git a/core/node/tee_verifier_input_producer/Cargo.toml b/core/node/tee_verifier_input_producer/Cargo.toml index 49856f5c7022..208e7e35760c 100644 --- a/core/node/tee_verifier_input_producer/Cargo.toml +++ b/core/node/tee_verifier_input_producer/Cargo.toml @@ -14,11 +14,9 @@ zksync_dal.workspace = true zksync_object_store.workspace = true zksync_prover_interface.workspace = true zksync_queued_job_processor.workspace = true -zksync_state.workspace = true zksync_tee_verifier.workspace = true zksync_types.workspace = true zksync_utils.workspace = true -multivm.workspace = true vm_utils.workspace = true vise.workspace = true diff --git a/core/node/tee_verifier_input_producer/src/lib.rs b/core/node/tee_verifier_input_producer/src/lib.rs index efa3c9e00b11..9104b62fa5e5 100644 --- a/core/node/tee_verifier_input_producer/src/lib.rs +++ b/core/node/tee_verifier_input_producer/src/lib.rs @@ -11,16 +11,14 @@ use std::{sync::Arc, time::Instant}; use anyhow::Context; use async_trait::async_trait; -use multivm::zk_evm_latest::ethereum_types::H256; -use tokio::{runtime::Handle, task::JoinHandle}; +use tokio::task::JoinHandle; use vm_utils::storage::L1BatchParamsProvider; use zksync_dal::{tee_verifier_input_producer_dal::JOB_MAX_ATTEMPT, ConnectionPool, Core, CoreDal}; use zksync_object_store::ObjectStore; use zksync_prover_interface::inputs::PrepareBasicCircuitsJob; use zksync_queued_job_processor::JobProcessor; -use zksync_state::{PostgresStorage, ReadStorage}; use zksync_tee_verifier::TeeVerifierInput; -use zksync_types::{block::L1BatchHeader, L1BatchNumber, L2BlockNumber, L2ChainId}; +use zksync_types::{L1BatchNumber, L2ChainId}; use zksync_utils::u256_to_h256; use self::metrics::METRICS; @@ -49,7 +47,6 @@ impl TeeVerifierInputProducer { } async fn process_job_impl( - rt_handle: Handle, l1_batch_number: L1BatchNumber, started_at: Instant, connection_pool: ConnectionPool, @@ -71,8 +68,6 @@ impl TeeVerifierInputProducer { .get_l2_blocks_to_execute_for_l1_batch(l1_batch_number) .await?; - let last_batch_miniblock_number = l2_blocks_execution_data.first().unwrap().number - 1; - let l1_batch_header = connection .blocks_dal() .get_l1_batch_header(l1_batch_number) @@ -107,19 +102,29 @@ impl TeeVerifierInputProducer { .await .context("expected miniblock to be executed and sealed")?; - // need a new connection in the next block - drop(connection); + let used_contract_hashes = l1_batch_header + .used_contract_hashes + .into_iter() + .map(u256_to_h256) + .collect(); + + // `get_factory_deps()` returns the bytecode in chunks of `Vec<[u8; 32]>`, + // but `fn store_factory_dep(&mut self, hash: H256, bytecode: Vec)` in `InMemoryStorage` wants flat byte vecs. + pub fn into_flattened(data: Vec<[T; N]>) -> Vec { + let mut new = Vec::new(); + for slice in data.iter() { + new.extend_from_slice(slice); + } + new + } - // `PostgresStorage` needs a blocking context - let used_contracts = rt_handle - .spawn_blocking(move || { - Self::get_used_contracts( - last_batch_miniblock_number, - l1_batch_header, - connection_pool, - ) - }) - .await??; + let used_contracts = connection + .factory_deps_dal() + .get_factory_deps(&used_contract_hashes) + .await + .into_iter() + .map(|(hash, bytes)| (u256_to_h256(hash), into_flattened(bytes))) + .collect(); tracing::info!("Started execution of l1_batch: {l1_batch_number:?}"); @@ -146,31 +151,6 @@ impl TeeVerifierInputProducer { Ok(tee_verifier_input) } - - fn get_used_contracts( - last_batch_miniblock_number: L2BlockNumber, - l1_batch_header: L1BatchHeader, - connection_pool: ConnectionPool, - ) -> anyhow::Result)>> { - let rt_handle = Handle::current(); - - let connection = rt_handle - .block_on(connection_pool.connection()) - .context("failed to get connection for TeeVerifierInputProducer")?; - - let mut pg_storage = - PostgresStorage::new(rt_handle, connection, last_batch_miniblock_number, true); - - Ok(l1_batch_header - .used_contract_hashes - .into_iter() - .filter_map(|hash| { - pg_storage - .load_factory_dep(u256_to_h256(hash)) - .map(|bytes| (u256_to_h256(hash), bytes)) - }) - .collect()) - } } #[async_trait] @@ -217,9 +197,7 @@ impl JobProcessor for TeeVerifierInputProducer { let connection_pool = self.connection_pool.clone(); let object_store = self.object_store.clone(); tokio::task::spawn(async move { - let rt_handle = Handle::current(); Self::process_job_impl( - rt_handle, job, started_at, connection_pool.clone(),