Skip to content

Commit

Permalink
feat(api): Start API server after first L1 batch (#1026)
Browse files Browse the repository at this point in the history
## What ❔

- Delays the API server start until there's at least one L1 batch
present in the node DB.
- Refactors the VM sandbox so that it's more modular / maintainable.
- Tests VM-related API server methods.

## Why ❔

Fixes API server operation when working on the DB after snapshot
recovery.

## Checklist

- [x] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [x] Tests for the changes have been added / updated.
- [x] 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`.
- [x] Linkcheck has been run via `zk linkcheck`.
  • Loading branch information
slowli committed Feb 8, 2024
1 parent 636fcfd commit 86e189c
Show file tree
Hide file tree
Showing 20 changed files with 954 additions and 483 deletions.
46 changes: 35 additions & 11 deletions core/lib/state/src/postgres/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{
sync::{Arc, RwLock},
};

use anyhow::Context as _;
use tokio::{runtime::Handle, sync::mpsc};
use zksync_dal::{ConnectionPool, StorageProcessor};
use zksync_types::{L1BatchNumber, MiniblockNumber, StorageKey, StorageValue, H256};
Expand Down Expand Up @@ -342,31 +343,54 @@ pub struct PostgresStorage<'a> {

impl<'a> PostgresStorage<'a> {
/// Creates a new storage using the specified connection.
///
/// # Panics
///
/// Panics on Postgres errors.
pub fn new(
rt_handle: Handle,
mut connection: StorageProcessor<'a>,
connection: StorageProcessor<'a>,
block_number: MiniblockNumber,
consider_new_l1_batch: bool,
) -> PostgresStorage<'a> {
let resolved = rt_handle
.block_on(
connection
.storage_web3_dal()
.resolve_l1_batch_number_of_miniblock(block_number),
)
.expect("Failed resolving L1 batch number for miniblock");
) -> Self {
rt_handle
.clone()
.block_on(Self::new_async(
rt_handle,
connection,
block_number,
consider_new_l1_batch,
))
.unwrap()
}

Self {
/// Asynchronous version of [`Self::new()`] that also propagates errors instead of panicking.
///
/// # Errors
///
/// Propagates Postgres errors.
pub async fn new_async(
rt_handle: Handle,
mut connection: StorageProcessor<'a>,
block_number: MiniblockNumber,
consider_new_l1_batch: bool,
) -> anyhow::Result<PostgresStorage<'a>> {
let resolved = connection
.storage_web3_dal()
.resolve_l1_batch_number_of_miniblock(block_number)
.await
.with_context(|| {
format!("failed resolving L1 batch number for miniblock #{block_number}")
})?;
Ok(Self {
rt_handle,
connection,
miniblock_number: block_number,
l1_batch_number_for_miniblock: resolved.expected_l1_batch(),
pending_l1_batch_number: resolved.pending_l1_batch,
consider_new_l1_batch,
caches: None,
}
})
}

/// Sets the caches to use with the storage.
Expand Down

0 comments on commit 86e189c

Please sign in to comment.