Skip to content

Commit

Permalink
feat(tee_verifier_input_producer): use `FactoryDepsDal::get_factory_d…
Browse files Browse the repository at this point in the history
…eps() (#2271)

## 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

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

- [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 <harald@matterlabs.dev>
  • Loading branch information
haraldh committed Jun 19, 2024
1 parent 15bb71e commit 2c0a00a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 50 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

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

2 changes: 0 additions & 2 deletions core/node/tee_verifier_input_producer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
70 changes: 24 additions & 46 deletions core/node/tee_verifier_input_producer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,7 +47,6 @@ impl TeeVerifierInputProducer {
}

async fn process_job_impl(
rt_handle: Handle,
l1_batch_number: L1BatchNumber,
started_at: Instant,
connection_pool: ConnectionPool<Core>,
Expand All @@ -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)
Expand Down Expand Up @@ -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<u8>)` in `InMemoryStorage` wants flat byte vecs.
pub fn into_flattened<T: Clone, const N: usize>(data: Vec<[T; N]>) -> Vec<T> {
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:?}");

Expand All @@ -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<Core>,
) -> anyhow::Result<Vec<(H256, Vec<u8>)>> {
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]
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 2c0a00a

Please sign in to comment.