Skip to content

Commit

Permalink
fix(vm): Prepare new VM for use in API server and fix divergences (#2994
Browse files Browse the repository at this point in the history
)

## What ❔

Prepares the new VM for use in the API server (without tracers):

- Fills in more statistics
- Covers more statistic fields in divergence checks and fixes the
corresponding divergence in `pubdata_published`
- Fixes a divergence if revert reaches the bootloader call frame
(happens in the call execution mode)
- Fixes a panic in `ShadowVm` if the transaction hash is not set and
makes `impl Debug for Transaction` non-panicking. (A hash is not set if
a transaction is converted from a call.)

## Why ❔

Part of preparations for integrating the new VM into the API server.

## 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] Documentation comments have been added / updated.
- [x] Code has been formatted via `zk fmt` and `zk lint`.

---------

Co-authored-by: Joonatan Saarhelo <[email protected]>
  • Loading branch information
slowli and joonazan authored Oct 7, 2024
1 parent b51c530 commit 741b77e
Show file tree
Hide file tree
Showing 34 changed files with 301 additions and 210 deletions.
8 changes: 4 additions & 4 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 @@ -228,7 +228,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 = "74577d9be13b1bff9d1a712389731f669b179e47" }
zksync_vm2 = { git = "https://github.com/matter-labs/vm2.git", rev = "a233d44bbe61dc6a758a754c3b78fe4f83e56699" }

# Consensus dependencies.
zksync_concurrency = "=0.3.0"
Expand Down
36 changes: 19 additions & 17 deletions core/lib/multivm/src/versions/vm_1_3_2/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ pub struct Vm<S: WriteStorage, H: HistoryMode> {
pub(crate) system_env: SystemEnv,
}

impl<S: WriteStorage, H: HistoryMode> Vm<S, H> {
pub(crate) fn record_vm_memory_metrics(&self) -> VmMemoryMetrics {
VmMemoryMetrics {
event_sink_inner: self.vm.state.event_sink.get_size(),
event_sink_history: self.vm.state.event_sink.get_history_size(),
memory_inner: self.vm.state.memory.get_size(),
memory_history: self.vm.state.memory.get_history_size(),
decommittment_processor_inner: self.vm.state.decommittment_processor.get_size(),
decommittment_processor_history: self
.vm
.state
.decommittment_processor
.get_history_size(),
storage_inner: self.vm.state.storage.get_size(),
storage_history: self.vm.state.storage.get_history_size(),
}
}
}

impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
type TracerDispatcher = TracerDispatcher;

Expand Down Expand Up @@ -160,23 +179,6 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
}
}

fn record_vm_memory_metrics(&self) -> VmMemoryMetrics {
VmMemoryMetrics {
event_sink_inner: self.vm.state.event_sink.get_size(),
event_sink_history: self.vm.state.event_sink.get_history_size(),
memory_inner: self.vm.state.memory.get_size(),
memory_history: self.vm.state.memory.get_history_size(),
decommittment_processor_inner: self.vm.state.decommittment_processor.get_size(),
decommittment_processor_history: self
.vm
.state
.decommittment_processor
.get_history_size(),
storage_inner: self.vm.state.storage.get_size(),
storage_history: self.vm.state.storage.get_history_size(),
}
}

fn finish_batch(&mut self) -> FinishedL1Batch {
self.vm
.execute_till_block_end(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ pub(super) fn assemble_tx_meta(execution_mode: TxExecutionMode, execute_tx: bool
// Set 0 byte (execution mode)
output[0] = match execution_mode {
TxExecutionMode::VerifyExecute => 0x00,
TxExecutionMode::EstimateFee { .. } => 0x00,
TxExecutionMode::EthCall { .. } => 0x02,
TxExecutionMode::EstimateFee => 0x00,
TxExecutionMode::EthCall => 0x02,
};

// Set 31 byte (marker for tx execution)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<S: WriteStorage, H: HistoryMode> Vm<S, H> {
}

/// Returns the info about all oracles' sizes.
pub(crate) fn record_vm_memory_metrics_inner(&self) -> VmMemoryMetrics {
pub(crate) fn record_vm_memory_metrics(&self) -> VmMemoryMetrics {
VmMemoryMetrics {
event_sink_inner: self.state.event_sink.get_size(),
event_sink_history: self.state.event_sink.get_history_size(),
Expand Down
5 changes: 0 additions & 5 deletions core/lib/multivm/src/versions/vm_1_4_1/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::{
BytecodeCompressionError, BytecodeCompressionResult, CurrentExecutionState,
FinishedL1Batch, L1BatchEnv, L2BlockEnv, SystemEnv, VmExecutionMode,
VmExecutionResultAndLogs, VmFactory, VmInterface, VmInterfaceHistoryEnabled,
VmMemoryMetrics,
},
utils::events::extract_l2tol1logs_from_l1_messenger,
vm_1_4_1::{
Expand Down Expand Up @@ -124,10 +123,6 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
}
}

fn record_vm_memory_metrics(&self) -> VmMemoryMetrics {
self.record_vm_memory_metrics_inner()
}

fn finish_batch(&mut self) -> FinishedL1Batch {
let result = self.inspect(&mut TracerDispatcher::default(), VmExecutionMode::Batch);
let execution_state = self.get_current_execution_state();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ pub(super) fn assemble_tx_meta(execution_mode: TxExecutionMode, execute_tx: bool
// Set 0 byte (execution mode)
output[0] = match execution_mode {
TxExecutionMode::VerifyExecute => 0x00,
TxExecutionMode::EstimateFee { .. } => 0x00,
TxExecutionMode::EthCall { .. } => 0x02,
TxExecutionMode::EstimateFee => 0x00,
TxExecutionMode::EthCall => 0x02,
};

// Set 31 byte (marker for tx execution)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<S: WriteStorage, H: HistoryMode> Vm<S, H> {
}

/// Returns the info about all oracles' sizes.
pub(crate) fn record_vm_memory_metrics_inner(&self) -> VmMemoryMetrics {
pub(crate) fn record_vm_memory_metrics(&self) -> VmMemoryMetrics {
VmMemoryMetrics {
event_sink_inner: self.state.event_sink.get_size(),
event_sink_history: self.state.event_sink.get_history_size(),
Expand Down
5 changes: 0 additions & 5 deletions core/lib/multivm/src/versions/vm_1_4_2/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::{
BytecodeCompressionError, BytecodeCompressionResult, CurrentExecutionState,
FinishedL1Batch, L1BatchEnv, L2BlockEnv, SystemEnv, VmExecutionMode,
VmExecutionResultAndLogs, VmFactory, VmInterface, VmInterfaceHistoryEnabled,
VmMemoryMetrics,
},
utils::events::extract_l2tol1logs_from_l1_messenger,
vm_1_4_2::{
Expand Down Expand Up @@ -126,10 +125,6 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
}
}

fn record_vm_memory_metrics(&self) -> VmMemoryMetrics {
self.record_vm_memory_metrics_inner()
}

fn finish_batch(&mut self) -> FinishedL1Batch {
let result = self.inspect(&mut TracerDispatcher::default(), VmExecutionMode::Batch);
let execution_state = self.get_current_execution_state();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ pub(super) fn assemble_tx_meta(execution_mode: TxExecutionMode, execute_tx: bool
// Set 0 byte (execution mode)
output[0] = match execution_mode {
TxExecutionMode::VerifyExecute => 0x00,
TxExecutionMode::EstimateFee { .. } => 0x00,
TxExecutionMode::EthCall { .. } => 0x02,
TxExecutionMode::EstimateFee => 0x00,
TxExecutionMode::EthCall => 0x02,
};

// Set 31 byte (marker for tx execution)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<S: WriteStorage, H: HistoryMode> Vm<S, H> {
}

/// Returns the info about all oracles' sizes.
pub(crate) fn record_vm_memory_metrics_inner(&self) -> VmMemoryMetrics {
pub(crate) fn record_vm_memory_metrics(&self) -> VmMemoryMetrics {
VmMemoryMetrics {
event_sink_inner: self.state.event_sink.get_size(),
event_sink_history: self.state.event_sink.get_history_size(),
Expand Down
5 changes: 0 additions & 5 deletions core/lib/multivm/src/versions/vm_boojum_integration/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::{
BytecodeCompressionError, BytecodeCompressionResult, CurrentExecutionState,
FinishedL1Batch, L1BatchEnv, L2BlockEnv, SystemEnv, VmExecutionMode,
VmExecutionResultAndLogs, VmFactory, VmInterface, VmInterfaceHistoryEnabled,
VmMemoryMetrics,
},
utils::events::extract_l2tol1logs_from_l1_messenger,
vm_boojum_integration::{
Expand Down Expand Up @@ -125,10 +124,6 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
}
}

fn record_vm_memory_metrics(&self) -> VmMemoryMetrics {
self.record_vm_memory_metrics_inner()
}

fn finish_batch(&mut self) -> FinishedL1Batch {
let result = self.inspect(&mut TracerDispatcher::default(), VmExecutionMode::Batch);
let execution_state = self.get_current_execution_state();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ pub(super) fn assemble_tx_meta(execution_mode: TxExecutionMode, execute_tx: bool
// Set 0 byte (execution mode)
output[0] = match execution_mode {
TxExecutionMode::VerifyExecute => 0x00,
TxExecutionMode::EstimateFee { .. } => 0x00,
TxExecutionMode::EthCall { .. } => 0x02,
TxExecutionMode::EstimateFee => 0x00,
TxExecutionMode::EthCall => 0x02,
};

// Set 31 byte (marker for tx execution)
Expand Down
36 changes: 34 additions & 2 deletions core/lib/multivm/src/versions/vm_fast/tests/rollbacks.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use assert_matches::assert_matches;
use ethabi::Token;
use zksync_contracts::{get_loadnext_contract, test_contracts::LoadnextContractExecutionParams};
use zksync_types::{Execute, U256};
use zksync_types::{Address, Execute, U256};
use zksync_vm_interface::VmInterfaceExt;

use crate::{
interface::TxExecutionMode,
interface::{ExecutionResult, TxExecutionMode},
versions::testonly::ContractToDeploy,
vm_fast::tests::{
tester::{DeployContractsTx, TransactionTestInfo, TxModifier, TxType, VmTesterBuilder},
utils::read_test_contract,
Expand Down Expand Up @@ -142,3 +145,32 @@ fn test_vm_loadnext_rollbacks() {

assert_eq!(result_without_rollbacks, result_with_rollbacks);
}

#[test]
fn rollback_in_call_mode() {
let counter_bytecode = read_test_contract();
let counter_address = Address::repeat_byte(1);

let mut vm = VmTesterBuilder::new()
.with_empty_in_memory_storage()
.with_execution_mode(TxExecutionMode::EthCall)
.with_custom_contracts(vec![ContractToDeploy::new(
counter_bytecode,
counter_address,
)])
.with_random_rich_accounts(1)
.build();
let account = &mut vm.rich_accounts[0];
let tx = account.get_test_contract_transaction(counter_address, true, None, false, TxType::L2);

let (compression_result, vm_result) = vm
.vm
.execute_transaction_with_bytecode_compression(tx, true);
compression_result.unwrap();
assert_matches!(
vm_result.result,
ExecutionResult::Revert { output }
if output.to_string().contains("This method always reverts")
);
assert_eq!(vm_result.logs.storage_logs, []);
}
Loading

0 comments on commit 741b77e

Please sign in to comment.