Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support stable compiler for VM (and some other crates) #3248

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
238 changes: 48 additions & 190 deletions Cargo.lock

Large diffs are not rendered by default.

13 changes: 5 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -227,19 +227,16 @@ tokio-stream = "0.1.16"
# We *always* pin the latest version of protocol to disallow accidental changes in the execution logic.
# However, for the historical version of protocol crates, we have lax requirements. Otherwise,
# Bumping a crypto dependency like `boojum` would require us to republish all the historical packages.
circuit_sequencer_api_1_3_3 = { package = "circuit_sequencer_api", version = "0.133" }
circuit_sequencer_api_1_4_0 = { package = "circuit_sequencer_api", version = "0.140" }
circuit_sequencer_api_1_4_1 = { package = "circuit_sequencer_api", version = "0.141" }
circuit_sequencer_api_1_4_2 = { package = "circuit_sequencer_api", version = "0.142" }
circuit_sequencer_api_1_5_0 = { package = "circuit_sequencer_api", version = "=0.150.7" }
crypto_codegen = { package = "zksync_solidity_vk_codegen", version = "=0.30.1" }
kzg = { package = "zksync_kzg", version = "=0.150.7" }
circuit_encodings = { package = "circuit_encodings", version = "=0.150.16" }
circuit_sequencer_api = { package = "circuit_sequencer_api", version = "=0.150.16" }
crypto_codegen = { package = "zksync_solidity_vk_codegen", version = "=0.30.9" }
kzg = { package = "zksync_kzg", version = "=0.150.16" }
zk_evm = { version = "=0.133.0" }
zk_evm_1_3_1 = { package = "zk_evm", version = "0.131.0-rc.2" }
zk_evm_1_3_3 = { package = "zk_evm", version = "0.133" }
zk_evm_1_4_0 = { package = "zk_evm", version = "0.140" }
zk_evm_1_4_1 = { package = "zk_evm", version = "0.141" }
zk_evm_1_5_0 = { package = "zk_evm", version = "=0.150.7" }
zk_evm_1_5_0 = { package = "zk_evm", version = "=0.150.16" }

# New VM; pinned to a specific commit because of instability
zksync_vm2 = { git = "https://github.com/matter-labs/vm2.git", rev = "457d8a7eea9093af9440662e33e598c13ba41633" }
Expand Down
6 changes: 1 addition & 5 deletions core/lib/multivm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ zk_evm_1_3_3.workspace = true
zk_evm_1_3_1.workspace = true
zksync_vm2.workspace = true

circuit_sequencer_api_1_3_3.workspace = true
circuit_sequencer_api_1_4_0.workspace = true
circuit_sequencer_api_1_4_1.workspace = true
circuit_sequencer_api_1_4_2.workspace = true
circuit_sequencer_api_1_5_0.workspace = true
circuit_sequencer_api.workspace = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!


zksync_types.workspace = true
zksync_contracts.workspace = true
Expand Down
44 changes: 16 additions & 28 deletions core/lib/multivm/src/glue/types/vm/vm_block_result.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::collections::HashMap;

use circuit_sequencer_api_1_3_3::sort_storage_access::sort_storage_access_queries as sort_storage_access_queries_1_3_3;
use itertools::Itertools;
use zk_evm_1_3_1::aux_structures::LogQuery as LogQuery_1_3_1;
use circuit_sequencer_api::sort_storage_access::sort_storage_access_queries;
use zksync_types::l2_to_l1_log::UserL2ToL1Log;

use crate::{
Expand All @@ -11,6 +9,7 @@ use crate::{
CurrentExecutionState, ExecutionResult, Refunds, VmExecutionLogs, VmExecutionResultAndLogs,
VmExecutionStatistics,
},
utils::glue_log_query,
};

// Note: In version after vm `VmVirtualBlocks` the bootloader memory knowledge is encapsulated into the VM.
Expand All @@ -21,18 +20,12 @@ use crate::{
impl GlueFrom<crate::vm_m5::vm_instance::VmBlockResult> for crate::interface::FinishedL1Batch {
fn glue_from(value: crate::vm_m5::vm_instance::VmBlockResult) -> Self {
let storage_log_queries = value.full_result.storage_log_queries.clone();
let deduplicated_storage_logs: Vec<LogQuery_1_3_1> = sort_storage_access_queries_1_3_3(
&storage_log_queries
let deduplicated_storage_logs = sort_storage_access_queries(
storage_log_queries
.iter()
.map(|log| {
GlueInto::<zk_evm_1_3_3::aux_structures::LogQuery>::glue_into(log.log_query)
})
.collect_vec(),
.map(|log| glue_log_query(log.log_query)),
)
.1
.into_iter()
.map(GlueInto::<LogQuery_1_3_1>::glue_into)
.collect();
.1;

crate::interface::FinishedL1Batch {
block_tip_execution_result: VmExecutionResultAndLogs {
Expand Down Expand Up @@ -78,18 +71,12 @@ impl GlueFrom<crate::vm_m5::vm_instance::VmBlockResult> for crate::interface::Fi
impl GlueFrom<crate::vm_m6::vm_instance::VmBlockResult> for crate::interface::FinishedL1Batch {
fn glue_from(value: crate::vm_m6::vm_instance::VmBlockResult) -> Self {
let storage_log_queries = value.full_result.storage_log_queries.clone();
let deduplicated_storage_logs: Vec<LogQuery_1_3_1> = sort_storage_access_queries_1_3_3(
&storage_log_queries
let deduplicated_storage_logs = sort_storage_access_queries(
storage_log_queries
.iter()
.map(|log| {
GlueInto::<zk_evm_1_3_3::aux_structures::LogQuery>::glue_into(log.log_query)
})
.collect_vec(),
.map(|log| glue_log_query(log.log_query)),
)
.1
.into_iter()
.map(GlueInto::<LogQuery_1_3_1>::glue_into)
.collect();
.1;

crate::interface::FinishedL1Batch {
block_tip_execution_result: VmExecutionResultAndLogs {
Expand Down Expand Up @@ -135,11 +122,12 @@ impl GlueFrom<crate::vm_m6::vm_instance::VmBlockResult> for crate::interface::Fi
impl GlueFrom<crate::vm_1_3_2::vm_instance::VmBlockResult> for crate::interface::FinishedL1Batch {
fn glue_from(value: crate::vm_1_3_2::vm_instance::VmBlockResult) -> Self {
let storage_log_queries = value.full_result.storage_log_queries.clone();
let deduplicated_storage_logs =
circuit_sequencer_api_1_3_3::sort_storage_access::sort_storage_access_queries(
storage_log_queries.iter().map(|log| &log.log_query),
)
.1;
let deduplicated_storage_logs = sort_storage_access_queries(
storage_log_queries
.iter()
.map(|log| glue_log_query(log.log_query)),
)
.1;

crate::interface::FinishedL1Batch {
block_tip_execution_result: VmExecutionResultAndLogs {
Expand Down
2 changes: 1 addition & 1 deletion core/lib/multivm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![warn(unused_extern_crates)]
#![warn(unused_imports)]

pub use circuit_sequencer_api_1_5_0 as circuit_sequencer_api_latest;
pub use circuit_sequencer_api as circuit_sequencer_api_latest;
pub use zk_evm_1_5_0 as zk_evm_latest;
pub use zksync_types::vm::VmVersion;
pub use zksync_vm_interface as interface;
Expand Down
18 changes: 17 additions & 1 deletion core/lib/multivm/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,27 @@ use zksync_types::{
};

pub use self::deduplicator::{ModifiedSlot, StorageWritesDeduplicator};
use crate::interface::L1BatchEnv;
use crate::{
glue::{GlueFrom, GlueInto},
interface::L1BatchEnv,
};

pub(crate) mod bytecode;
mod deduplicator;
pub(crate) mod events;
pub(crate) mod sort_storage_access;

/// Allows to convert `LogQuery` between two different versions, even if they don't provide
/// direct conversion between each other.
/// It transforms the input query to the `LogQuery` from `zksync_types` (for which most of the
/// `zk_evm` versions provide conversion) and then converts it to the target version.
pub fn glue_log_query<L, R>(l: L) -> R
where
L: GlueInto<zksync_types::zk_evm_types::LogQuery>,
R: GlueFrom<zksync_types::zk_evm_types::LogQuery>,
{
R::glue_from(l.glue_into())
}

/// Calculates the base fee and gas per pubdata for the given L1 gas price.
pub fn derive_base_fee_and_gas_per_pubdata(
Expand Down
1 change: 1 addition & 0 deletions core/lib/multivm/src/utils/sort_storage_access.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this empty file a mistake?

12 changes: 8 additions & 4 deletions core/lib/multivm/src/versions/testonly/precompiles.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use circuit_sequencer_api_1_5_0::geometry_config::get_geometry_config;
use circuit_sequencer_api::geometry_config::ProtocolGeometry;
use zksync_test_contracts::TestContract;
use zksync_types::{Address, Execute};

Expand Down Expand Up @@ -41,7 +41,9 @@ pub(crate) fn test_keccak<VM: TestedVm>() {
assert!(!exec_result.result.is_failed(), "{exec_result:#?}");

let keccak_count = exec_result.statistics.circuit_statistic.keccak256
* get_geometry_config().cycles_per_keccak256_circuit as f32;
* ProtocolGeometry::V1_5_0
.config()
.cycles_per_keccak256_circuit as f32;
assert!(keccak_count >= 1000.0, "{keccak_count}");
}

Expand Down Expand Up @@ -77,7 +79,7 @@ pub(crate) fn test_sha256<VM: TestedVm>() {
assert!(!exec_result.result.is_failed(), "{exec_result:#?}");

let sha_count = exec_result.statistics.circuit_statistic.sha256
* get_geometry_config().cycles_per_sha256_circuit as f32;
* ProtocolGeometry::V1_5_0.config().cycles_per_sha256_circuit as f32;
assert!(sha_count >= 1000.0, "{sha_count}");
}

Expand Down Expand Up @@ -106,6 +108,8 @@ pub(crate) fn test_ecrecover<VM: TestedVm>() {
assert!(!exec_result.result.is_failed(), "{exec_result:#?}");

let ecrecover_count = exec_result.statistics.circuit_statistic.ecrecover
* get_geometry_config().cycles_per_ecrecover_circuit as f32;
* ProtocolGeometry::V1_5_0
.config()
.cycles_per_ecrecover_circuit as f32;
assert!((ecrecover_count - 1.0).abs() < 1e-4, "{ecrecover_count}");
}
16 changes: 10 additions & 6 deletions core/lib/multivm/src/versions/vm_1_3_2/pubdata_utils.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::collections::HashMap;

use circuit_sequencer_api_1_3_3::sort_storage_access::sort_storage_access_queries;
use circuit_sequencer_api::sort_storage_access::sort_storage_access_queries;
use zk_evm_1_3_3::aux_structures::Timestamp;
use zksync_types::{StorageKey, PUBLISH_BYTECODE_OVERHEAD, SYSTEM_CONTEXT_ADDRESS};

use crate::{
interface::{storage::WriteStorage, VmEvent},
utils::bytecode::bytecode_len_in_bytes,
utils::{bytecode::bytecode_len_in_bytes, glue_log_query},
vm_1_3_2::{history_recorder::HistoryMode, oracles::storage::storage_key_of_log, VmInstance},
};

Expand Down Expand Up @@ -71,16 +71,20 @@ impl<H: HistoryMode, S: WriteStorage> VmInstance<S, H> {
.state
.storage
.storage_log_queries_after_timestamp(from_timestamp);
let (_, deduplicated_logs) =
sort_storage_access_queries(storage_logs.iter().map(|log| &log.log_query));
let (_, deduplicated_logs) = sort_storage_access_queries(
storage_logs.iter().map(|log| glue_log_query(log.log_query)),
);

deduplicated_logs
.into_iter()
.filter_map(|log| {
if log.rw_flag {
let key = storage_key_of_log(&log);
let key = storage_key_of_log(&glue_log_query(log));
let pre_paid = pre_paid_before_tx(&key);
let to_pay_by_user = self.state.storage.base_price_for_write(&log);
let to_pay_by_user = self
.state
.storage
.base_price_for_write(&glue_log_query(log));

if to_pay_by_user > pre_paid {
Some(to_pay_by_user - pre_paid)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use circuit_sequencer_api_1_3_3::INITIAL_MONOTONIC_CYCLE_COUNTER;
use circuit_sequencer_api::INITIAL_MONOTONIC_CYCLE_COUNTER;
use itertools::Itertools;
use zk_evm_1_3_3::{
aux_structures::{MemoryPage, Timestamp},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use circuit_sequencer_api_1_4_1::{geometry_config::get_geometry_config, toolset::GeometryConfig};
use circuit_sequencer_api::geometry_config::{GeometryConfig, ProtocolGeometry};

use crate::{interface::CircuitStatistic, utils::CircuitCycleStatistic};

Expand Down Expand Up @@ -40,7 +40,7 @@ pub(crate) const UMA_READ_RAM_CYCLES: u32 = 3;
pub(crate) const PRECOMPILE_RAM_CYCLES: u32 = 1;
pub(crate) const PRECOMPILE_LOG_DEMUXER_CYCLES: u32 = 1;

const GEOMETRY_CONFIG: GeometryConfig = get_geometry_config();
const GEOMETRY_CONFIG: GeometryConfig = ProtocolGeometry::V1_4_1.config();

pub(crate) fn circuit_statistic_from_cycles(cycles: CircuitCycleStatistic) -> CircuitStatistic {
CircuitStatistic {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::marker::PhantomData;

use circuit_sequencer_api_1_4_1::sort_storage_access::sort_storage_access_queries;
use circuit_sequencer_api::sort_storage_access::sort_storage_access_queries;
use zk_evm_1_4_1::{
aux_structures::Timestamp,
tracing::{BeforeExecutionData, VmLocalStateData},
Expand All @@ -24,6 +24,7 @@ use crate::{
extract_bytecode_publication_requests_from_l1_messenger,
extract_l2tol1logs_from_l1_messenger,
},
glue_log_query,
},
vm_1_4_1::{
bootloader_state::{utils::apply_pubdata_to_memory, BootloaderState},
Expand Down Expand Up @@ -130,7 +131,7 @@ impl<S: WriteStorage> PubdataTracer<S> {
storage
.storage_log_queries_after_timestamp(Timestamp(0))
.iter()
.map(|log| &log.log_query),
.map(|log| glue_log_query(log.log_query)),
)
.1
.into_iter()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use circuit_sequencer_api_1_3_3::INITIAL_MONOTONIC_CYCLE_COUNTER;
use circuit_sequencer_api::INITIAL_MONOTONIC_CYCLE_COUNTER;
use zk_evm_1_4_1::{
aux_structures::{MemoryPage, Timestamp},
block_properties::BlockProperties,
Expand Down
12 changes: 8 additions & 4 deletions core/lib/multivm/src/versions/vm_1_4_1/vm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::rc::Rc;

use circuit_sequencer_api_1_4_1::sort_storage_access::sort_storage_access_queries;
use circuit_sequencer_api::sort_storage_access::sort_storage_access_queries;
use zksync_types::{
l2_to_l1_log::{SystemL2ToL1Log, UserL2ToL1Log},
Transaction,
Expand All @@ -15,7 +15,7 @@ use crate::{
FinishedL1Batch, L1BatchEnv, L2BlockEnv, PushTransactionResult, SystemEnv, VmExecutionMode,
VmExecutionResultAndLogs, VmFactory, VmInterface, VmInterfaceHistoryEnabled,
},
utils::events::extract_l2tol1logs_from_l1_messenger,
utils::{events::extract_l2tol1logs_from_l1_messenger, glue_log_query},
vm_1_4_1::{
bootloader_state::BootloaderState,
old_vm::events::merge_events,
Expand Down Expand Up @@ -60,8 +60,12 @@ impl<S: WriteStorage, H: HistoryMode> Vm<S, H> {

let storage_log_queries = self.state.storage.get_final_log_queries();

let deduped_storage_log_queries =
sort_storage_access_queries(storage_log_queries.iter().map(|log| &log.log_query)).1;
let deduped_storage_log_queries = sort_storage_access_queries(
storage_log_queries
.iter()
.map(|log| glue_log_query(log.log_query)),
)
.1;

CurrentExecutionState {
events,
Expand Down
2 changes: 1 addition & 1 deletion core/lib/multivm/src/versions/vm_1_4_2/constants.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use circuit_sequencer_api_1_4_2::{BLOB_CHUNK_SIZE, ELEMENTS_PER_4844_BLOCK};
use circuit_sequencer_api::{BLOB_CHUNK_SIZE, ELEMENTS_PER_4844_BLOCK};
use zk_evm_1_4_1::aux_structures::MemoryPage;
pub use zk_evm_1_4_1::zkevm_opcode_defs::system_params::{
ERGS_PER_CIRCUIT, INITIAL_STORAGE_WRITE_PUBDATA_BYTES,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use circuit_sequencer_api_1_4_2::{geometry_config::get_geometry_config, toolset::GeometryConfig};
use circuit_sequencer_api::geometry_config::{GeometryConfig, ProtocolGeometry};

use crate::{interface::CircuitStatistic, utils::CircuitCycleStatistic};

Expand Down Expand Up @@ -41,7 +41,7 @@ pub(crate) const UMA_READ_RAM_CYCLES: u32 = 3;
pub(crate) const PRECOMPILE_RAM_CYCLES: u32 = 1;
pub(crate) const PRECOMPILE_LOG_DEMUXER_CYCLES: u32 = 1;

const GEOMETRY_CONFIG: GeometryConfig = get_geometry_config();
const GEOMETRY_CONFIG: GeometryConfig = ProtocolGeometry::V1_4_2.config();

pub(crate) fn circuit_statistic_from_cycles(cycles: CircuitCycleStatistic) -> CircuitStatistic {
CircuitStatistic {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::marker::PhantomData;

use circuit_sequencer_api_1_4_2::sort_storage_access::sort_storage_access_queries;
use circuit_sequencer_api::sort_storage_access::sort_storage_access_queries;
use zk_evm_1_4_1::{
aux_structures::Timestamp,
tracing::{BeforeExecutionData, VmLocalStateData},
Expand All @@ -24,6 +24,7 @@ use crate::{
extract_bytecode_publication_requests_from_l1_messenger,
extract_l2tol1logs_from_l1_messenger,
},
glue_log_query,
},
vm_1_4_2::{
bootloader_state::{utils::apply_pubdata_to_memory, BootloaderState},
Expand Down Expand Up @@ -150,7 +151,7 @@ impl<S: WriteStorage> PubdataTracer<S> {
storage
.storage_log_queries_after_timestamp(Timestamp(0))
.iter()
.map(|log| &log.log_query),
.map(|log| glue_log_query(log.log_query)),
)
.1
.into_iter()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use circuit_sequencer_api_1_3_3::INITIAL_MONOTONIC_CYCLE_COUNTER;
use circuit_sequencer_api::INITIAL_MONOTONIC_CYCLE_COUNTER;
use zk_evm_1_4_1::{
aux_structures::{MemoryPage, Timestamp},
block_properties::BlockProperties,
Expand Down
Loading
Loading