Skip to content

Commit

Permalink
feat(vm): Use the one interface for all vms (#277)
Browse files Browse the repository at this point in the history
What ❔
Implement one common interface for all vms. It's the first attempt to do
it and it's done only for two latest vms.
In the future all vms will be migrated to the one interface.

Why ❔
We need to unify our work with VM now it's always unpredicable what are
we calling and when


p.s. I'm not going to merge it before boojum 

The thing, i don't like: 
Using `vm_latest::HistoryEenable` in interface. 
It suppose to take another Eternity to refactor HistoryMode 

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [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`.

---------

Signed-off-by: Danil <deniallugo@gmail.com>
  • Loading branch information
Deniallugo authored Nov 7, 2023
1 parent 4c82015 commit 91bb99b
Show file tree
Hide file tree
Showing 158 changed files with 2,446 additions and 2,689 deletions.
51 changes: 24 additions & 27 deletions core/bin/system-constants-generator/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
use multivm::interface::{L1BatchEnv, L2BlockEnv, SystemEnv, TxExecutionMode, VmExecutionMode};
use multivm::vm_latest::{
constants::{BLOCK_GAS_LIMIT, BOOTLOADER_HEAP_PAGE},
BootloaderState, BoxedTracer, DynTracer, HistoryEnabled, HistoryMode, Vm,
VmExecutionStopReason, VmTracer, ZkSyncVmState,
};
use once_cell::sync::Lazy;
use std::cell::RefCell;
use std::rc::Rc;

use multivm::interface::{
dyn_tracers::vm_1_4_0::DynTracer, tracer::VmExecutionStopReason, L1BatchEnv, L2BlockEnv,
SystemEnv, TxExecutionMode, VmExecutionMode, VmInterface,
};
use multivm::vm_latest::{
constants::{BLOCK_GAS_LIMIT, BOOTLOADER_HEAP_PAGE},
BootloaderState, HistoryEnabled, HistoryMode, SimpleMemory, ToTracerPointer, Vm, VmTracer,
ZkSyncVmState,
};
use zksync_contracts::{
load_sys_contract, read_bootloader_code, read_sys_contract_bytecode, read_zbin_bytecode,
BaseSystemContracts, ContractLanguage, SystemContractCode,
};
use zksync_state::{InMemoryStorage, StorageView, WriteStorage};
use zksync_types::block::legacy_miniblock_hash;
use zksync_types::{
ethabi::Token, fee::Fee, l1::L1Tx, l2::L2Tx, utils::storage_key_for_eth_balance, AccountTreeId,
Address, Execute, L1BatchNumber, L1TxCommonData, L2ChainId, MiniblockNumber, Nonce,
ProtocolVersionId, StorageKey, Timestamp, Transaction, BOOTLOADER_ADDRESS, H256,
SYSTEM_CONTEXT_ADDRESS, SYSTEM_CONTEXT_GAS_PRICE_POSITION, SYSTEM_CONTEXT_TX_ORIGIN_POSITION,
U256, ZKPORTER_IS_AVAILABLE,
block::legacy_miniblock_hash, ethabi::Token, fee::Fee, l1::L1Tx, l2::L2Tx,
utils::storage_key_for_eth_balance, AccountTreeId, Address, Execute, L1BatchNumber,
L1TxCommonData, L2ChainId, MiniblockNumber, Nonce, ProtocolVersionId, StorageKey, Timestamp,
Transaction, BOOTLOADER_ADDRESS, H256, SYSTEM_CONTEXT_ADDRESS,
SYSTEM_CONTEXT_GAS_PRICE_POSITION, SYSTEM_CONTEXT_TX_ORIGIN_POSITION, U256,
ZKPORTER_IS_AVAILABLE,
};
use zksync_utils::{bytecode::hash_bytecode, bytes_to_be_words, u256_to_h256};

Expand All @@ -31,7 +35,7 @@ struct SpecialBootloaderTracer {
output: Rc<RefCell<u32>>,
}

impl<S: WriteStorage, H: HistoryMode> DynTracer<S, H> for SpecialBootloaderTracer {}
impl<S: WriteStorage, H: HistoryMode> DynTracer<S, SimpleMemory<H>> for SpecialBootloaderTracer {}

impl<S: WriteStorage, H: HistoryMode> VmTracer<S, H> for SpecialBootloaderTracer {
fn initialize_tracer(&mut self, state: &mut ZkSyncVmState<S, H>) {
Expand Down Expand Up @@ -247,14 +251,11 @@ pub(super) fn execute_internal_transfer_test() -> u32 {
let tracer = SpecialBootloaderTracer {
input,
output: tracer_result.clone(),
};
let mut vm = Vm::new(
l1_batch,
system_env,
Rc::new(RefCell::new(storage_view)),
HistoryEnabled,
);
let result = vm.inspect(vec![tracer.into_boxed()], VmExecutionMode::Bootloader);
}
.into_tracer_pointer();
let mut vm: Vm<_, HistoryEnabled> =
Vm::new(l1_batch, system_env, Rc::new(RefCell::new(storage_view)));
let result = vm.inspect(tracer.into(), VmExecutionMode::Bootloader);

assert!(!result.result.is_failed(), "The internal call has reverted");
tracer_result.take()
Expand Down Expand Up @@ -307,12 +308,8 @@ pub(super) fn execute_user_txs_in_test_gas_vm(
chain_id: L2ChainId::default(),
};

let mut vm = Vm::new(
l1_batch,
system_env,
Rc::new(RefCell::new(storage_view)),
HistoryEnabled,
);
let mut vm: Vm<_, HistoryEnabled> =
Vm::new(l1_batch, system_env, Rc::new(RefCell::new(storage_view)));

let mut total_gas_refunded = 0;
for tx in txs {
Expand Down
17 changes: 7 additions & 10 deletions core/lib/multivm/src/glue/init_vm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::GlueInto;
use crate::glue::history_mode::HistoryMode;
use crate::interface::{L1BatchEnv, SystemEnv};
use crate::interface::{L1BatchEnv, SystemEnv, VmInterface};
use crate::vm_instance::VmInstanceVersion;
use crate::VmInstance;
use zksync_state::{ReadStorage, StoragePtr, StorageView};
Expand Down Expand Up @@ -28,7 +28,7 @@ impl<S: ReadStorage, H: HistoryMode> VmInstance<S, H> {
VmVersion::M5WithoutRefunds => {
let oracle_tools = crate::vm_m5::OracleTools::new(
storage_view.clone(),
crate::vm_m5::vm::MultiVMSubversion::V1,
crate::vm_m5::vm_instance::MultiVMSubversion::V1,
);
let block_properties = zk_evm_1_3_1::block_properties::BlockProperties {
default_aa_code_hash: h256_to_u256(
Expand All @@ -37,7 +37,7 @@ impl<S: ReadStorage, H: HistoryMode> VmInstance<S, H> {
zkporter_is_available: false,
};
let inner_vm = crate::vm_m5::vm_with_bootloader::init_vm_with_gas_limit(
crate::vm_m5::vm::MultiVMSubversion::V1,
crate::vm_m5::vm_instance::MultiVMSubversion::V1,
oracle_tools,
l1_batch_env.glue_into(),
block_properties,
Expand All @@ -54,7 +54,7 @@ impl<S: ReadStorage, H: HistoryMode> VmInstance<S, H> {
VmVersion::M5WithRefunds => {
let oracle_tools = crate::vm_m5::OracleTools::new(
storage_view.clone(),
crate::vm_m5::vm::MultiVMSubversion::V2,
crate::vm_m5::vm_instance::MultiVMSubversion::V2,
);
let block_properties = zk_evm_1_3_1::block_properties::BlockProperties {
default_aa_code_hash: h256_to_u256(
Expand All @@ -63,7 +63,7 @@ impl<S: ReadStorage, H: HistoryMode> VmInstance<S, H> {
zkporter_is_available: false,
};
let inner_vm = crate::vm_m5::vm_with_bootloader::init_vm_with_gas_limit(
crate::vm_m5::vm::MultiVMSubversion::V2,
crate::vm_m5::vm_instance::MultiVMSubversion::V2,
oracle_tools,
l1_batch_env.glue_into(),
block_properties,
Expand All @@ -88,7 +88,7 @@ impl<S: ReadStorage, H: HistoryMode> VmInstance<S, H> {
};

let inner_vm = crate::vm_m6::vm_with_bootloader::init_vm_with_gas_limit(
crate::vm_m6::vm::MultiVMSubversion::V1,
crate::vm_m6::vm_instance::MultiVMSubversion::V1,
oracle_tools,
l1_batch_env.glue_into(),
block_properties,
Expand All @@ -113,7 +113,7 @@ impl<S: ReadStorage, H: HistoryMode> VmInstance<S, H> {
};

let inner_vm = crate::vm_m6::vm_with_bootloader::init_vm_with_gas_limit(
crate::vm_m6::vm::MultiVMSubversion::V2,
crate::vm_m6::vm_instance::MultiVMSubversion::V2,
oracle_tools,
l1_batch_env.glue_into(),
block_properties,
Expand Down Expand Up @@ -154,7 +154,6 @@ impl<S: ReadStorage, H: HistoryMode> VmInstance<S, H> {
l1_batch_env.glue_into(),
system_env.clone().glue_into(),
storage_view.clone(),
H::VmVirtualBlocksMode::default(),
);
let vm = VmInstanceVersion::VmVirtualBlocks(Box::new(vm));
Self {
Expand All @@ -168,7 +167,6 @@ impl<S: ReadStorage, H: HistoryMode> VmInstance<S, H> {
l1_batch_env.glue_into(),
system_env.clone(),
storage_view.clone(),
H::VmVirtualBlocksRefundsEnhancement::default(),
);
let vm = VmInstanceVersion::VmVirtualBlocksRefundsEnhancement(Box::new(vm));
Self {
Expand All @@ -182,7 +180,6 @@ impl<S: ReadStorage, H: HistoryMode> VmInstance<S, H> {
l1_batch_env.glue_into(),
system_env.clone(),
storage_view.clone(),
H::VmBoojumIntegration::default(),
);
let vm = VmInstanceVersion::VmBoojumIntegration(Box::new(vm));
Self {
Expand Down
2 changes: 1 addition & 1 deletion core/lib/multivm/src/glue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub(crate) mod block_properties;
pub(crate) mod history_mode;
pub(crate) mod init_vm;
pub(crate) mod oracle_tools;
pub(crate) mod tracer;
pub mod tracers;
mod types;

/// This trait is a workaround on the Rust'c [orphan rule](orphan_rule).
Expand Down
12 changes: 8 additions & 4 deletions core/lib/multivm/src/glue/oracle_tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ where
pub fn new(version: VmVersion, state: StoragePtr<StorageView<S>>, history: H) -> Self {
match version {
VmVersion::M5WithoutRefunds => {
let oracle_tools =
crate::vm_m5::OracleTools::new(state, crate::vm_m5::vm::MultiVMSubversion::V1);
let oracle_tools = crate::vm_m5::OracleTools::new(
state,
crate::vm_m5::vm_instance::MultiVMSubversion::V1,
);
OracleTools::M5(oracle_tools)
}
VmVersion::M5WithRefunds => {
let oracle_tools =
crate::vm_m5::OracleTools::new(state, crate::vm_m5::vm::MultiVMSubversion::V2);
let oracle_tools = crate::vm_m5::OracleTools::new(
state,
crate::vm_m5::vm_instance::MultiVMSubversion::V2,
);
OracleTools::M5(oracle_tools)
}
VmVersion::M6Initial | VmVersion::M6BugWithCompressionFixed => {
Expand Down
112 changes: 0 additions & 112 deletions core/lib/multivm/src/glue/tracer/implementations.rs

This file was deleted.

Loading

0 comments on commit 91bb99b

Please sign in to comment.