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

Adapt era_vm to new world struct #232

Merged
merged 2 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/lib/multivm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ pub use crate::{
tracers::{MultiVMTracer, MultiVmTracerPointer},
},
versions::{
vm_1_3_2, vm_1_4_1, vm_1_4_2, vm_boojum_integration, vm_fast, vm_latest, vm_m5, vm_m6,
vm_refunds_enhancement, vm_virtual_blocks, era_vm,
era_vm, vm_1_3_2, vm_1_4_1, vm_1_4_2, vm_boojum_integration, vm_fast, vm_latest, vm_m5,
vm_m6, vm_refunds_enhancement, vm_virtual_blocks,
},
vm_instance::VmInstance,
};
Expand Down
2 changes: 1 addition & 1 deletion core/lib/multivm/src/versions/era_vm/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub(crate) struct L2BlockSnapshot {
}

pub struct VmSnapshot {
state: era_vm::VMState,
execution: era_vm::execution::Execution,

// TODO: Implement snapshots in era vm
// world_snapshot: vm2::ExternalSnapshot,
Expand Down
77 changes: 37 additions & 40 deletions core/lib/multivm/src/versions/era_vm/vm.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::{cell::RefCell, collections::HashMap, fmt::Write, io::Read, rc::Rc, str::FromStr};
use std::{cell::RefCell, collections::HashMap, rc::Rc};

use era_vm::{
store::{L2ToL1Log, StorageError, StorageKey as EraStorageKey},
state::{L2ToL1Log, VMState},
store::{StorageError, StorageKey as EraStorageKey},
vm::ExecutionOutput,
EraVM, VMState,
EraVM, Execution,
};
use once_cell::sync::Lazy;
use zksync_state::{InMemoryStorage, ReadStorage, StoragePtr, StorageView, WriteStorage};
use zksync_state::{ReadStorage, StoragePtr};
use zksync_types::{
l1::is_l1_tx_type, AccountTreeId, StorageKey, Transaction, BOOTLOADER_ADDRESS, H160,
KNOWN_CODES_STORAGE_ADDRESS, U256,
Expand All @@ -25,13 +25,15 @@ use super::{
};
use crate::{
era_vm::{bytecode::compress_bytecodes, transaction_data::TransactionData},
interface::{Halt, TxRevertReason, VmFactory, VmInterface, VmInterfaceHistoryEnabled, VmRevertReason},
interface::{
Halt, TxRevertReason, VmFactory, VmInterface, VmInterfaceHistoryEnabled, VmRevertReason,
},
vm_latest::{
constants::{
get_vm_hook_position, get_vm_hook_start_position_latest, VM_HOOK_PARAMS_COUNT,
},
BootloaderMemory, CurrentExecutionState, ExecutionResult, HistoryEnabled, L1BatchEnv,
L2BlockEnv, SystemEnv, VmExecutionLogs, VmExecutionMode, VmExecutionResultAndLogs,
BootloaderMemory, CurrentExecutionState, ExecutionResult, L1BatchEnv, L2BlockEnv,
SystemEnv, VmExecutionLogs, VmExecutionMode, VmExecutionResultAndLogs,
},
};

Expand Down Expand Up @@ -66,7 +68,7 @@ impl<S: ReadStorage + 'static> VmFactory<S> for Vm<S> {
let vm_hook_position =
get_vm_hook_position(crate::vm_latest::MultiVMSubversion::IncreasedBootloaderMemory)
* 32;
let vm_state = VMState::new(
let vm_execution = Execution::new(
bootloader_code.to_owned(),
Vec::new(),
BOOTLOADER_ADDRESS,
Expand All @@ -87,12 +89,7 @@ impl<S: ReadStorage + 'static> VmFactory<S> for Vm<S> {
);
let pre_contract_storage = Rc::new(RefCell::new(HashMap::new()));
pre_contract_storage.borrow_mut().insert(
h256_to_u256(
system_env
.base_system_smart_contracts
.default_aa
.hash,
),
h256_to_u256(system_env.base_system_smart_contracts.default_aa.hash),
system_env
.base_system_smart_contracts
.default_aa
Expand All @@ -102,7 +99,7 @@ impl<S: ReadStorage + 'static> VmFactory<S> for Vm<S> {
let world_storage1 = World::new(storage.clone(), pre_contract_storage.clone());
let world_storage2 = World::new(storage.clone(), pre_contract_storage.clone());
let mut vm = EraVM::new(
vm_state,
vm_execution,
Rc::new(RefCell::new(world_storage1)),
Rc::new(RefCell::new(world_storage2)),
);
Expand All @@ -111,22 +108,21 @@ impl<S: ReadStorage + 'static> VmFactory<S> for Vm<S> {
// The bootloader shouldn't pay for growing memory and it writes results
// to the end of its heap, so it makes sense to preallocate it in its entirety.
const BOOTLOADER_MAX_MEMORY_SIZE: u32 = 59000000;
vm.state
vm.execution
.heaps
.get_mut(era_vm::state::FIRST_HEAP)
.get_mut(era_vm::execution::FIRST_HEAP)
.unwrap()
.expand_memory(BOOTLOADER_MAX_MEMORY_SIZE);
vm.state
vm.execution
.heaps
.get_mut(era_vm::state::FIRST_HEAP + 1)
.get_mut(era_vm::execution::FIRST_HEAP + 1)
.unwrap()
.expand_memory(BOOTLOADER_MAX_MEMORY_SIZE);

let mut mv = Self {
inner: vm,
suspended_at: 0,
gas_for_account_validation: system_env
.default_validation_computational_gas_limit,
gas_for_account_validation: system_env.default_validation_computational_gas_limit,
last_tx_result: None,
bootloader_state: BootloaderState::new(
system_env.execution_mode.clone(),
Expand All @@ -143,7 +139,6 @@ impl<S: ReadStorage + 'static> VmFactory<S> for Vm<S> {
mv.write_to_bootloader_heap(bootloader_memory);
mv
}

}

impl<S: ReadStorage + 'static> Vm<S> {
Expand All @@ -160,7 +155,7 @@ impl<S: ReadStorage + 'static> Vm<S> {
},
ExecutionOutput::Panic => {
return ExecutionResult::Halt {
reason: if self.inner.state.gas_left().unwrap() == 0 {
reason: if self.inner.execution.gas_left().unwrap() == 0 {
Halt::BootloaderOutOfGas
} else {
Halt::VMPanic
Expand Down Expand Up @@ -226,9 +221,9 @@ impl<S: ReadStorage + 'static> Vm<S> {
Hook::DebugLog => {
let heap = self
.inner
.state
.execution
.heaps
.get(self.inner.state.current_context().unwrap().heap_id)
.get(self.inner.execution.current_context().unwrap().heap_id)
.unwrap();
let vm_hook_params: Vec<_> = self
.get_vm_hook_params(heap)
Expand All @@ -254,16 +249,18 @@ impl<S: ReadStorage + 'static> Vm<S> {
}
Hook::TxHasEnded => {
// println!("TX HAS ENDED");
if let (VmExecutionMode::OneTx, Some(result)) = (execution_mode, self.last_tx_result.take()) {
if let (VmExecutionMode::OneTx, Some(result)) =
(execution_mode, self.last_tx_result.take())
{
return result;
}
}
}
self.inner.state.current_frame_mut().unwrap().pc = self.suspended_at as u64;
self.inner.execution.current_frame_mut().unwrap().pc = self.suspended_at as u64;
}
}

fn get_vm_hook_params(&self, heap: &era_vm::state::Heap) -> Vec<U256> {
fn get_vm_hook_params(&self, heap: &era_vm::execution::Heap) -> Vec<U256> {
(get_vm_hook_start_position_latest()..get_vm_hook_start_position_latest() + 2)
.map(|word| {
let res = heap.read((word * 32) as u32);
Expand Down Expand Up @@ -310,26 +307,26 @@ impl<S: ReadStorage + 'static> Vm<S> {
fn read_heap_word(&self, word: usize) -> U256 {
let heap = self
.inner
.state
.execution
.heaps
.get(self.inner.state.current_context().unwrap().heap_id)
.get(self.inner.execution.current_context().unwrap().heap_id)
.unwrap();
heap.read((word * 32) as u32)
}

#[cfg(test)]
/// Returns the current state of the VM in a format that can be compared for equality.
pub(crate) fn dump_state(&self) -> VMState {
self.inner.state.clone()
pub(crate) fn dump_state(&self) -> Execution {
self.inner.execution.clone()
}

fn write_to_bootloader_heap(&mut self, memory: impl IntoIterator<Item = (usize, U256)>) {
assert!(self.inner.state.running_contexts.len() == 1); // No on-going far calls
assert!(self.inner.execution.running_contexts.len() == 1); // No on-going far calls
if let Some(heap) = &mut self
.inner
.state
.execution
.heaps
.get_mut(self.inner.state.current_context().unwrap().heap_id)
.get_mut(self.inner.execution.current_context().unwrap().heap_id)
{
for (slot, value) in memory {
let end = (slot + 1) * 32;
Expand All @@ -355,7 +352,7 @@ impl<S: ReadStorage + 'static> VmInterface for Vm<S> {
} else {
compress_bytecodes(&tx.factory_deps, |hash| {
self.inner
.state_storage
.state
.storage_read(EraStorageKey::new(
KNOWN_CODES_STORAGE_ADDRESS,
h256_to_u256(hash),
Expand Down Expand Up @@ -400,7 +397,7 @@ impl<S: ReadStorage + 'static> VmInterface for Vm<S> {
result,
logs: VmExecutionLogs {
storage_logs: Default::default(),
events: merge_events(&self.inner.state.events, self.batch_env.number),
events: merge_events(self.inner.state.events(), self.batch_env.number),
user_l2_to_l1_logs: Default::default(),
system_l2_to_l1_logs: Default::default(),
total_log_queries_count: 0, // This field is unused
Expand Down Expand Up @@ -443,7 +440,7 @@ impl<S: ReadStorage + 'static> VmInterface for Vm<S> {
}

fn gas_remaining(&self) -> u32 {
self.inner.state.current_frame().unwrap().gas_left.0
self.inner.execution.current_frame().unwrap().gas_left.0
}
}

Expand Down Expand Up @@ -535,7 +532,6 @@ impl<S: ReadStorage> era_vm::store::ContractStorage for World<S> {

#[cfg(test)]
mod tests {
use super::*;
use std::{cell::RefCell, path::PathBuf, rc::Rc};

use once_cell::sync::Lazy;
Expand All @@ -554,6 +550,7 @@ mod tests {
};
use zksync_utils::bytecode::hash_bytecode;

use super::*;
use crate::{
era_vm::vm::Vm,
interface::{
Expand Down
2 changes: 1 addition & 1 deletion core/lib/multivm/src/versions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod shadow;
pub mod era_vm;
pub mod shadow;
pub mod vm_1_3_2;
pub mod vm_1_4_1;
pub mod vm_1_4_2;
Expand Down
Loading