Skip to content

Commit

Permalink
chore(vm): Bump vm2 repo revision (#2883)
Browse files Browse the repository at this point in the history
## What ❔

Bumps the `vm2` crate revision and updates new VM usage correspondingly.

## Why ❔

To keep the `vm2` dependency up to date and track possible regressions.

## 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] Code has been formatted via `zk fmt` and `zk lint`.
  • Loading branch information
slowli authored Sep 19, 2024
1 parent 6ec528e commit 209ac10
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 88 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ zk_evm_1_4_1 = { package = "zk_evm", version = "0.141" }
zk_evm_1_5_0 = { package = "zk_evm", version = "=0.150.5" }

# New VM; pinned to a specific commit because of instability
zksync_vm2 = { git = "https://github.com/matter-labs/vm2.git", rev = "cd6136c42ec56856e0abcf2a98d1a9e120161482" }
zksync_vm2 = { git = "https://github.com/matter-labs/vm2.git", rev = "74577d9be13b1bff9d1a712389731f669b179e47" }

# Consensus dependencies.
zksync_concurrency = "=0.1.1"
Expand Down
8 changes: 4 additions & 4 deletions core/lib/multivm/src/versions/vm_fast/circuits_tracer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use circuit_sequencer_api_1_5_0::{geometry_config::get_geometry_config, toolset::GeometryConfig};
use zksync_vm2::{CycleStats, Opcode, OpcodeType, StateInterface, Tracer};
use zksync_vm2::interface::{CycleStats, Opcode, OpcodeType, StateInterface, Tracer};
use zksync_vm_interface::CircuitStatistic;

use crate::vm_latest::tracers::circuits_capacity::*;
Expand All @@ -17,7 +17,7 @@ pub(crate) struct CircuitsTracer {
keccak256_cycles: u32,
ecrecover_cycles: u32,
sha256_cycles: u32,
secp256k1_verify_cycles: u32,
secp256r1_verify_cycles: u32,
transient_storage_checker_cycles: u32,
}

Expand Down Expand Up @@ -115,7 +115,7 @@ impl Tracer for CircuitsTracer {
CycleStats::Keccak256(cycles) => self.keccak256_cycles += cycles,
CycleStats::Sha256(cycles) => self.sha256_cycles += cycles,
CycleStats::EcRecover(cycles) => self.ecrecover_cycles += cycles,
CycleStats::Secp256k1Verify(cycles) => self.secp256k1_verify_cycles += cycles,
CycleStats::Secp256r1Verify(cycles) => self.secp256r1_verify_cycles += cycles,
CycleStats::Decommit(cycles) => self.code_decommitter_cycles += cycles,
CycleStats::StorageRead => self.storage_application_cycles += 1,
CycleStats::StorageWrite => self.storage_application_cycles += 2,
Expand Down Expand Up @@ -146,7 +146,7 @@ impl CircuitsTracer {
ecrecover: self.ecrecover_cycles as f32
/ GEOMETRY_CONFIG.cycles_per_ecrecover_circuit as f32,
sha256: self.sha256_cycles as f32 / GEOMETRY_CONFIG.cycles_per_sha256_circuit as f32,
secp256k1_verify: self.secp256k1_verify_cycles as f32
secp256k1_verify: self.secp256r1_verify_cycles as f32
/ GEOMETRY_CONFIG.cycles_per_secp256r1_verify_circuit as f32,
transient_storage_checker: self.transient_storage_checker_cycles as f32
/ GEOMETRY_CONFIG.cycles_per_transient_storage_sorter as f32,
Expand Down
11 changes: 7 additions & 4 deletions core/lib/multivm/src/versions/vm_fast/events.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use zksync_types::{L1BatchNumber, H256};
use zksync_utils::h256_to_account_address;
use zksync_vm2::Event;
use zksync_vm2::interface::Event;

use crate::interface::VmEvent;

Expand All @@ -23,18 +23,21 @@ impl EventAccumulator {
}
}

pub(crate) fn merge_events(events: &[Event], block_number: L1BatchNumber) -> Vec<VmEvent> {
pub(crate) fn merge_events(
events: impl Iterator<Item = Event>,
block_number: L1BatchNumber,
) -> Vec<VmEvent> {
let mut result = vec![];
let mut current: Option<(usize, u32, EventAccumulator)> = None;

for message in events.iter() {
for event in events {
let Event {
shard_id,
is_first,
tx_number,
key,
value,
} = message.clone();
} = event;

if !is_first {
if let Some((mut remaining_data_length, mut remaining_topics, mut event)) =
Expand Down
9 changes: 5 additions & 4 deletions core/lib/multivm/src/versions/vm_fast/glue.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use zksync_types::l2_to_l1_log::{L2ToL1Log, SystemL2ToL1Log};
use zksync_utils::u256_to_h256;
use zksync_vm2::interface;

use crate::glue::GlueFrom;

impl GlueFrom<&zksync_vm2::L2ToL1Log> for SystemL2ToL1Log {
fn glue_from(value: &zksync_vm2::L2ToL1Log) -> Self {
let zksync_vm2::L2ToL1Log {
impl GlueFrom<interface::L2ToL1Log> for SystemL2ToL1Log {
fn glue_from(value: interface::L2ToL1Log) -> Self {
let interface::L2ToL1Log {
key,
value,
is_service,
address,
shard_id,
tx_number,
} = *value;
} = value;

Self(L2ToL1Log {
shard_id,
Expand Down
2 changes: 1 addition & 1 deletion core/lib/multivm/src/versions/vm_fast/tests/bootloader.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use assert_matches::assert_matches;
use zksync_types::U256;
use zksync_vm2::HeapId;
use zksync_vm2::interface::HeapId;

use crate::{
interface::{ExecutionResult, Halt, TxExecutionMode, VmExecutionMode, VmInterfaceExt},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt;

use zksync_types::{ExecuteTransactionCommon, Transaction, H160, U256};
use zksync_vm2::interface::{Event, StateInterface};

use super::VmTester;
use crate::{
Expand Down Expand Up @@ -190,7 +191,7 @@ impl TransactionTestInfo {
struct VmStateDump<S> {
state: S,
storage_writes: Vec<((H160, U256), U256)>,
events: Box<[zksync_vm2::Event]>,
events: Box<[Event]>,
}

impl<S: PartialEq> PartialEq for VmStateDump<S> {
Expand All @@ -205,14 +206,8 @@ impl<S: ReadStorage> Vm<S> {
fn dump_state(&self) -> VmStateDump<impl PartialEq + fmt::Debug> {
VmStateDump {
state: self.inner.dump_state(),
storage_writes: self
.inner
.world_diff()
.get_storage_state()
.iter()
.map(|(k, v)| (*k, *v))
.collect(),
events: self.inner.world_diff().events().into(),
storage_writes: self.inner.get_storage_state().collect(),
events: self.inner.events().collect(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/lib/multivm/src/versions/vm_fast/tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use zksync_types::{
U256,
};
use zksync_utils::{bytecode::hash_bytecode, bytes_to_be_words, h256_to_u256, u256_to_h256};
use zksync_vm2::{HeapId, StateInterface};
use zksync_vm2::interface::{HeapId, StateInterface};

use crate::interface::storage::ReadStorage;

Expand Down
80 changes: 20 additions & 60 deletions core/lib/multivm/src/versions/vm_fast/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use zksync_types::{
};
use zksync_utils::{bytecode::hash_bytecode, h256_to_u256, u256_to_h256};
use zksync_vm2::{
decode::decode_program, CallframeInterface, ExecutionEnd, FatPointer, HeapId, Program,
Settings, StateInterface, Tracer, VirtualMachine,
interface::{CallframeInterface, HeapId, StateInterface, Tracer},
ExecutionEnd, FatPointer, Program, Settings, VirtualMachine,
};

use super::{
Expand Down Expand Up @@ -79,7 +79,7 @@ impl<S: ReadStorage> Vm<S> {
operator_suggested_refund: 0,
};
let mut last_tx_result = None;
let mut pubdata_before = self.inner.world_diff().pubdata() as u32;
let mut pubdata_before = self.inner.pubdata() as u32;

let result = loop {
let hook = match self.inner.run(&mut self.world, tracer) {
Expand Down Expand Up @@ -125,7 +125,7 @@ impl<S: ReadStorage> Vm<S> {
)
.as_u64();

let pubdata_published = self.inner.world_diff().pubdata() as u32;
let pubdata_published = self.inner.pubdata() as u32;

refunds.operator_suggested_refund = compute_refund(
&self.batch_env,
Expand Down Expand Up @@ -186,8 +186,7 @@ impl<S: ReadStorage> Vm<S> {
unreachable!("We do not provide the pubdata when executing the block tip or a single transaction");
}

let events =
merge_events(self.inner.world_diff().events(), self.batch_env.number);
let events = merge_events(self.inner.events(), self.batch_env.number);

let published_bytecodes = events
.iter()
Expand Down Expand Up @@ -421,7 +420,7 @@ impl<S: ReadStorage> Vm<S> {
BOOTLOADER_ADDRESS,
bootloader,
H160::zero(),
vec![],
&[],
system_env.bootloader_gas_limit,
Settings {
default_aa_code_hash,
Expand Down Expand Up @@ -461,7 +460,8 @@ impl<S: ReadStorage> Vm<S> {
// visible for testing
pub(super) fn get_current_execution_state(&self) -> CurrentExecutionState {
let world_diff = self.inner.world_diff();
let events = merge_events(world_diff.events(), self.batch_env.number);
let vm = &self.inner;
let events = merge_events(vm.events(), self.batch_env.number);

let user_l2_to_l1_logs = extract_l2tol1logs_from_l1_messenger(&events)
.into_iter()
Expand All @@ -480,22 +480,12 @@ impl<S: ReadStorage> Vm<S> {
})
.collect(),
used_contract_hashes: self.decommitted_hashes().collect(),
system_logs: world_diff
.l2_to_l1_logs()
.iter()
.map(|x| x.glue_into())
.collect(),
system_logs: vm.l2_to_l1_logs().map(GlueInto::glue_into).collect(),
user_l2_to_l1_logs,
storage_refunds: world_diff.storage_refunds().to_vec(),
pubdata_costs: world_diff.pubdata_costs().to_vec(),
}
}

fn delete_history_if_appropriate(&mut self) {
if self.snapshot.is_none() && !self.has_previous_far_calls() {
self.inner.delete_history();
}
}
}

impl<S: ReadStorage> VmInterface for Vm<S> {
Expand All @@ -519,7 +509,7 @@ impl<S: ReadStorage> VmInterface for Vm<S> {

let mut tracer = CircuitsTracer::default();
let start = self.inner.world_diff().snapshot();
let pubdata_before = self.inner.world_diff().pubdata();
let pubdata_before = self.inner.pubdata();
let gas_before = self.gas_remaining();

let (result, refunds) = self.run(execution_mode, &mut tracer, track_refunds);
Expand Down Expand Up @@ -549,7 +539,7 @@ impl<S: ReadStorage> VmInterface for Vm<S> {
})
.collect();
let events = merge_events(
self.inner.world_diff().events_after(&start),
self.inner.world_diff().events_after(&start).iter().copied(),
self.batch_env.number,
);
let user_l2_to_l1_logs = extract_l2tol1logs_from_l1_messenger(&events)
Expand All @@ -562,7 +552,7 @@ impl<S: ReadStorage> VmInterface for Vm<S> {
.world_diff()
.l2_to_l1_logs_after(&start)
.iter()
.map(|x| x.glue_into())
.map(|&log| log.glue_into())
.collect();
VmExecutionLogs {
storage_logs,
Expand All @@ -573,7 +563,7 @@ impl<S: ReadStorage> VmInterface for Vm<S> {
}
};

let pubdata_after = self.inner.world_diff().pubdata();
let pubdata_after = self.inner.pubdata();
let circuit_statistic = tracer.circuit_statistic();
let gas_remaining = self.gas_remaining();
VmExecutionResultAndLogs {
Expand Down Expand Up @@ -648,7 +638,6 @@ impl<S: ReadStorage> VmInterface for Vm<S> {

#[derive(Debug)]
struct VmSnapshot {
vm_snapshot: zksync_vm2::Snapshot,
bootloader_snapshot: BootloaderStateSnapshot,
gas_for_account_validation: u32,
}
Expand All @@ -660,31 +649,27 @@ impl<S: ReadStorage> VmInterfaceHistoryEnabled for Vm<S> {
"cannot create a VM snapshot until a previous snapshot is rolled back to or popped"
);

self.delete_history_if_appropriate();
self.inner.make_snapshot();
self.snapshot = Some(VmSnapshot {
vm_snapshot: self.inner.snapshot(),
bootloader_snapshot: self.bootloader_state.get_snapshot(),
gas_for_account_validation: self.gas_for_account_validation,
});
}

fn rollback_to_the_latest_snapshot(&mut self) {
let VmSnapshot {
vm_snapshot,
bootloader_snapshot,
gas_for_account_validation,
} = self.snapshot.take().expect("no snapshots to rollback to");

self.inner.rollback(vm_snapshot);
self.inner.rollback();
self.bootloader_state.apply_snapshot(bootloader_snapshot);
self.gas_for_account_validation = gas_for_account_validation;

self.delete_history_if_appropriate();
}

fn pop_snapshot_no_rollback(&mut self) {
self.inner.pop_snapshot();
self.snapshot = None;
self.delete_history_if_appropriate();
}
}

Expand Down Expand Up @@ -721,39 +706,13 @@ impl<S: ReadStorage, T: Tracer> World<S, T> {
}
}

fn bytecode_to_program(bytecode: &[u8]) -> Program<T, Self> {
Program::new(
decode_program(
&bytecode
.chunks_exact(8)
.map(|chunk| u64::from_be_bytes(chunk.try_into().unwrap()))
.collect::<Vec<_>>(),
false,
),
bytecode
.chunks_exact(32)
.map(U256::from_big_endian)
.collect::<Vec<_>>(),
)
}

fn convert_system_contract_code(
code: &SystemContractCode,
is_bootloader: bool,
) -> (U256, Program<T, Self>) {
(
h256_to_u256(code.hash),
Program::new(
decode_program(
&code
.code
.iter()
.flat_map(|x| x.0.into_iter().rev())
.collect::<Vec<_>>(),
is_bootloader,
),
code.code.clone(),
),
Program::from_words(code.code.clone(), is_bootloader),
)
}
}
Expand Down Expand Up @@ -808,11 +767,12 @@ impl<S: ReadStorage, T: Tracer> zksync_vm2::World<T> for World<S, T> {
self.program_cache
.entry(hash)
.or_insert_with(|| {
Self::bytecode_to_program(self.bytecode_cache.entry(hash).or_insert_with(|| {
let bytecode = self.bytecode_cache.entry(hash).or_insert_with(|| {
self.storage
.load_factory_dep(u256_to_h256(hash))
.expect("vm tried to decommit nonexistent bytecode")
}))
});
Program::new(bytecode, false)
})
.clone()
}
Expand Down
Loading

0 comments on commit 209ac10

Please sign in to comment.