-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vm-runner): shadow protective reads using VM runner (#2017)
## What ❔ Adds a new component `vm_runner_protective_reads` that computes protective reads independently and asynchronously from state keeper. For now, the component does not actually save anything; instead, it computes protective reads and compares them against what state keeper has already written to the DB. So, in short, this is just the first stepping stone aka a sanity check that the VM runner mechanism works as intended. ## Why ❔ In the future, we want to be able to save protective reads asynchronously thus saving time on L1 batch sealing. ## 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. - [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`.
- Loading branch information
Showing
38 changed files
with
622 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use serde::Deserialize; | ||
|
||
#[derive(Debug, Deserialize, Clone, PartialEq, Default)] | ||
pub struct ProtectiveReadsWriterConfig { | ||
/// Path to the RocksDB data directory that serves state cache. | ||
#[serde(default = "ProtectiveReadsWriterConfig::default_protective_reads_db_path")] | ||
pub protective_reads_db_path: String, | ||
/// How many max batches should be processed at the same time. | ||
pub protective_reads_window_size: u32, | ||
} | ||
|
||
impl ProtectiveReadsWriterConfig { | ||
fn default_protective_reads_db_path() -> String { | ||
"./db/protective_reads_writer".to_owned() | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...lib/dal/.sqlx/query-1f38966f65ce0ed8365b969d0a1f125cf30578457040c14fd6882c73a87fb3d6.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
...lib/dal/.sqlx/query-c31632143b459ea6684908ce7a15d03a811221d4ddf26e2e0ddc34147a0d8e23.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
...lib/dal/.sqlx/query-f2f1b6c4f4686b423a4c449c56e1687e91d8367347b3830830a4c76407d60bc5.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
core/lib/dal/migrations/20240522215934_add_vm_runner_protective_reads_table.down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE IF EXISTS vm_runner_protective_reads; |
7 changes: 7 additions & 0 deletions
7
core/lib/dal/migrations/20240522215934_add_vm_runner_protective_reads_table.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CREATE TABLE IF NOT EXISTS vm_runner_protective_reads | ||
( | ||
l1_batch_number BIGINT NOT NULL PRIMARY KEY, | ||
created_at TIMESTAMP NOT NULL, | ||
updated_at TIMESTAMP NOT NULL, | ||
time_taken TIME | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use zksync_db_connection::{connection::Connection, error::DalResult, instrument::InstrumentExt}; | ||
use zksync_types::L1BatchNumber; | ||
|
||
use crate::Core; | ||
|
||
#[derive(Debug)] | ||
pub struct VmRunnerDal<'c, 'a> { | ||
pub(crate) storage: &'c mut Connection<'a, Core>, | ||
} | ||
|
||
impl VmRunnerDal<'_, '_> { | ||
pub async fn get_protective_reads_latest_processed_batch( | ||
&mut self, | ||
) -> DalResult<L1BatchNumber> { | ||
let row = sqlx::query!( | ||
r#" | ||
SELECT | ||
COALESCE(MAX(l1_batch_number), 0) AS "last_processed_l1_batch!" | ||
FROM | ||
vm_runner_protective_reads | ||
"# | ||
) | ||
.instrument("get_protective_reads_latest_processed_batch") | ||
.report_latency() | ||
.fetch_one(self.storage) | ||
.await?; | ||
Ok(L1BatchNumber(row.last_processed_l1_batch as u32)) | ||
} | ||
|
||
pub async fn get_protective_reads_last_ready_batch( | ||
&mut self, | ||
window_size: u32, | ||
) -> DalResult<L1BatchNumber> { | ||
let row = sqlx::query!( | ||
r#" | ||
WITH | ||
available_batches AS ( | ||
SELECT | ||
MAX(number) AS "last_batch" | ||
FROM | ||
l1_batches | ||
), | ||
processed_batches AS ( | ||
SELECT | ||
COALESCE(MAX(l1_batch_number), 0) + $1 AS "last_ready_batch" | ||
FROM | ||
vm_runner_protective_reads | ||
) | ||
SELECT | ||
LEAST(last_batch, last_ready_batch) AS "last_ready_batch!" | ||
FROM | ||
available_batches | ||
FULL JOIN processed_batches ON TRUE | ||
"#, | ||
window_size as i32 | ||
) | ||
.instrument("get_protective_reads_last_ready_batch") | ||
.report_latency() | ||
.fetch_one(self.storage) | ||
.await?; | ||
Ok(L1BatchNumber(row.last_ready_batch as u32)) | ||
} | ||
|
||
pub async fn mark_protective_reads_batch_as_completed( | ||
&mut self, | ||
l1_batch_number: L1BatchNumber, | ||
) -> DalResult<()> { | ||
sqlx::query!( | ||
r#" | ||
INSERT INTO | ||
vm_runner_protective_reads (l1_batch_number, created_at, updated_at) | ||
VALUES | ||
($1, NOW(), NOW()) | ||
"#, | ||
i64::from(l1_batch_number.0), | ||
) | ||
.instrument("mark_protective_reads_batch_as_completed") | ||
.report_latency() | ||
.execute(self.storage) | ||
.await?; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use zksync_config::configs::ProtectiveReadsWriterConfig; | ||
|
||
use crate::{envy_load, FromEnv}; | ||
|
||
impl FromEnv for ProtectiveReadsWriterConfig { | ||
fn from_env() -> anyhow::Result<Self> { | ||
envy_load("vm_runner.protective_reads", "VM_RUNNER_PROTECTIVE_READS_") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
syntax = "proto3"; | ||
|
||
package zksync.config.vm_runner; | ||
|
||
message ProtectiveReadsWriter { | ||
optional string protective_reads_db_path = 1; // required; fs path | ||
optional uint64 protective_reads_window_size = 2; // required | ||
} |
Oops, something went wrong.