Skip to content

Commit

Permalink
Merge pull request #2003 from matter-labs/fvs-integrade-vm2-step-1
Browse files Browse the repository at this point in the history
feat(vm2): implement inspect_transaction_with_bytecode_compression
  • Loading branch information
joonazan authored May 21, 2024
2 parents ea34526 + 96c44db commit e3b6fe8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 22 deletions.
26 changes: 15 additions & 11 deletions core/lib/multivm/src/versions/vm_fast/bytecode.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
use itertools::Itertools;
use zksync_state::ReadStorage;
use zksync_types::H256;
use zksync_utils::bytecode::{compress_bytecode, hash_bytecode, CompressedBytecodeInfo};

/*impl Vm {
use super::Vm;

impl<S: ReadStorage> Vm<S> {
/// Checks the last transaction has successfully published compressed bytecodes and returns `true` if there is at least one is still unknown.
pub(crate) fn has_unpublished_bytecodes(&mut self) -> bool {
self.get_last_tx_compressed_bytecodes().iter().any(|info| {
!self
.state
.storage
.storage
.get_ptr()
.borrow_mut()
.is_bytecode_known(&hash_bytecode(&info.original))
})
self.bootloader_state
.get_last_tx_compressed_bytecodes()
.iter()
.any(|info| {
!self
.world
.storage
.borrow_mut()
.is_bytecode_known(&hash_bytecode(&info.original))
})
}
}*/
}

pub(crate) fn compress_bytecodes(
bytecodes: &[Vec<u8>],
Expand Down
4 changes: 2 additions & 2 deletions core/lib/multivm/src/versions/vm_fast/tests/refunds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn test_predetermined_refunded_gas() {
.build();

vm.vm
.push_transaction_inner(tx.clone(), result.refunds.gas_refunded);
.push_transaction_inner(tx.clone(), result.refunds.gas_refunded, true);

let result_with_predefined_refunds = vm.vm.execute(VmExecutionMode::Batch);
let mut current_state_with_predefined_refunds = vm.vm.get_current_execution_state();
Expand Down Expand Up @@ -107,7 +107,7 @@ fn test_predetermined_refunded_gas() {

let changed_operator_suggested_refund = result.refunds.gas_refunded + 1000;
vm.vm
.push_transaction_inner(tx, changed_operator_suggested_refund);
.push_transaction_inner(tx, changed_operator_suggested_refund, true);
let result = vm.vm.execute(VmExecutionMode::Batch);
let mut current_state_with_changed_predefined_refunds = vm.vm.get_current_execution_state();

Expand Down
34 changes: 25 additions & 9 deletions core/lib/multivm/src/versions/vm_fast/vm.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};

use vm2::{
decode::decode_program, fat_pointer::FatPointer, ExecutionEnd, Program, Settings,
decode::decode_program, fat_pointer::FatPointer, ExecutionEnd, HeapId, Program, Settings,
VirtualMachine,
};
use zk_evm_1_5_0::zkevm_opcode_defs::system_params::INITIAL_FRAME_FORMAL_EH_LOCATION;
Expand Down Expand Up @@ -34,7 +34,10 @@ use super::{
};
use crate::{
glue::GlueInto,
interface::{Halt, TxRevertReason, VmInterface, VmInterfaceHistoryEnabled, VmRevertReason},
interface::{
BytecodeCompressionError, Halt, TxRevertReason, VmInterface, VmInterfaceHistoryEnabled,
VmRevertReason,
},
vm_fast::{
bootloader_state::utils::{apply_l2_block, apply_pubdata_to_memory},
events::merge_events,
Expand Down Expand Up @@ -340,13 +343,18 @@ impl<S: ReadStorage> Vm<S> {
)
}

pub(crate) fn push_transaction_inner(&mut self, tx: zksync_types::Transaction, refund: u64) {
pub(crate) fn push_transaction_inner(
&mut self,
tx: zksync_types::Transaction,
refund: u64,
with_compression: bool,
) {
let tx: TransactionData = tx.into();
let overhead = tx.overhead_gas();

self.insert_bytecodes(tx.factory_deps.iter().map(|dep| &dep[..]));

let compressed_bytecodes = if is_l1_tx_type(tx.tx_type) {
let compressed_bytecodes = if is_l1_tx_type(tx.tx_type) || !with_compression {
// L1 transactions do not need compression
vec![]
} else {
Expand Down Expand Up @@ -446,10 +454,8 @@ impl<S: ReadStorage> VmInterface<S, HistoryEnabled> for Vm<S> {

// The bootloader writes results to high addresses in its heap, so it makes sense to preallocate it.
inner.state.heaps[vm2::FIRST_HEAP].resize(get_used_bootloader_memory_bytes(VM_VERSION), 0);

inner.state.current_frame.heap_size = u32::MAX;
inner.state.current_frame.aux_heap_size = u32::MAX;

inner.state.current_frame.exception_handler = INITIAL_FRAME_FORMAL_EH_LOCATION;

let mut me = Self {
Expand All @@ -474,7 +480,7 @@ impl<S: ReadStorage> VmInterface<S, HistoryEnabled> for Vm<S> {
}

fn push_transaction(&mut self, tx: zksync_types::Transaction) {
self.push_transaction_inner(tx, 0);
self.push_transaction_inner(tx, 0, true);
}

fn inspect(
Expand Down Expand Up @@ -559,7 +565,17 @@ impl<S: ReadStorage> VmInterface<S, HistoryEnabled> for Vm<S> {
Result<(), crate::interface::BytecodeCompressionError>,
VmExecutionResultAndLogs,
) {
todo!()
self.push_transaction_inner(tx, 0, with_compression);
let result = self.inspect(tracer, VmExecutionMode::OneTx);

if self.has_unpublished_bytecodes() {
(
Err(BytecodeCompressionError::BytecodeCompressionFailed),
result,
)
} else {
(Ok(()), result)
}
}

fn get_bootloader_memory(&self) -> BootloaderMemory {
Expand Down

0 comments on commit e3b6fe8

Please sign in to comment.