Skip to content

Commit

Permalink
Fix build after merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Oppen committed Aug 12, 2024
1 parent a7615ab commit 6bde7e7
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 101 deletions.
51 changes: 47 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 core/lib/multivm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ zk_evm_1_3_3.workspace = true
zk_evm_1_3_1.workspace = true
vm2.workspace = true
# era_vm = {path = "../../../../era_vm"}
era_vm = {git = "https://github.com/lambdaclass/era_vm.git", branch = "era_integration"}
era_vm = {git = "https://github.com/lambdaclass/era_vm.git", branch = "main"}

circuit_sequencer_api_1_3_3.workspace = true
circuit_sequencer_api_1_4_0.workspace = true
Expand Down
189 changes: 97 additions & 92 deletions core/lib/multivm/src/versions/era_vm/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use super::{
};
use crate::{
era_vm::{bytecode::compress_bytecodes, transaction_data::TransactionData},
interface::{Halt, TxRevertReason, 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,
Expand Down Expand Up @@ -54,6 +54,100 @@ pub struct Vm<S: WriteStorage> {
snapshots: Vec<VmSnapshot>, // TODO: Implement snapshots logic
}

/// Encapsulates creating VM instance based on the provided environment.
impl<S: WriteStorage + 'static> VmFactory<S> for Vm<S> {
/// Creates a new VM instance.
fn new(batch_env: L1BatchEnv, system_env: SystemEnv, storage: StoragePtr<S>) -> Self {
let bootloader_code = system_env
.clone()
.base_system_smart_contracts
.bootloader
.code;
let vm_hook_position =
get_vm_hook_position(crate::vm_latest::MultiVMSubversion::IncreasedBootloaderMemory)
* 32;
let vm_state = VMState::new(
bootloader_code.to_owned(),
Vec::new(),
BOOTLOADER_ADDRESS,
H160::zero(),
0_u128,
system_env
.base_system_smart_contracts
.default_aa
.hash
.to_fixed_bytes(),
system_env
.base_system_smart_contracts
.default_aa //TODO: Add real evm interpreter
.hash
.to_fixed_bytes(),
vm_hook_position,
true,
);
let pre_contract_storage = Rc::new(RefCell::new(HashMap::new()));
pre_contract_storage.borrow_mut().insert(
h256_to_u256(
system_env
.clone()
.base_system_smart_contracts
.default_aa
.hash,
),
system_env
.clone()
.base_system_smart_contracts
.default_aa
.code,
);
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,
Rc::new(RefCell::new(world_storage1)),
Rc::new(RefCell::new(world_storage2)),
);
let bootloader_memory = bootloader_initial_memory(&batch_env);

// 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
.heaps
.get_mut(era_vm::state::FIRST_HEAP)
.unwrap()
.expand_memory(BOOTLOADER_MAX_MEMORY_SIZE);
vm.state
.heaps
.get_mut(era_vm::state::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
.clone()
.default_validation_computational_gas_limit,
last_tx_result: None,
bootloader_state: BootloaderState::new(
system_env.execution_mode.clone(),
bootloader_initial_memory(&batch_env),
batch_env.first_l2_block,
),
program_cache: pre_contract_storage,
storage,
batch_env,
system_env,
snapshots: Vec::new(),
};

mv.write_to_bootloader_heap(bootloader_memory);
mv
}

}

impl<S: WriteStorage + 'static> Vm<S> {
pub fn run(&mut self, execution_mode: VmExecutionMode) -> ExecutionResult {
loop {
Expand Down Expand Up @@ -248,98 +342,9 @@ impl<S: WriteStorage + 'static> Vm<S> {
}
}

impl<S: WriteStorage + 'static> VmInterface<S, HistoryEnabled> for Vm<S> {
impl<S: WriteStorage + 'static> VmInterface for Vm<S> {
type TracerDispatcher = ();

fn new(batch_env: L1BatchEnv, system_env: SystemEnv, storage: StoragePtr<S>) -> Self {
let bootloader_code = system_env
.clone()
.base_system_smart_contracts
.bootloader
.code;
let vm_hook_position =
get_vm_hook_position(crate::vm_latest::MultiVMSubversion::IncreasedBootloaderMemory)
* 32;
let vm_state = VMState::new(
bootloader_code.to_owned(),
Vec::new(),
BOOTLOADER_ADDRESS,
H160::zero(),
0_u128,
system_env
.base_system_smart_contracts
.default_aa
.hash
.to_fixed_bytes(),
system_env
.base_system_smart_contracts
.default_aa //TODO: Add real evm interpreter
.hash
.to_fixed_bytes(),
vm_hook_position,
true,
);
let pre_contract_storage = Rc::new(RefCell::new(HashMap::new()));
pre_contract_storage.borrow_mut().insert(
h256_to_u256(
system_env
.clone()
.base_system_smart_contracts
.default_aa
.hash,
),
system_env
.clone()
.base_system_smart_contracts
.default_aa
.code,
);
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,
Rc::new(RefCell::new(world_storage1)),
Rc::new(RefCell::new(world_storage2)),
);
let bootloader_memory = bootloader_initial_memory(&batch_env);

// 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
.heaps
.get_mut(era_vm::state::FIRST_HEAP)
.unwrap()
.expand_memory(BOOTLOADER_MAX_MEMORY_SIZE);
vm.state
.heaps
.get_mut(era_vm::state::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
.clone()
.default_validation_computational_gas_limit,
last_tx_result: None,
bootloader_state: BootloaderState::new(
system_env.execution_mode.clone(),
bootloader_initial_memory(&batch_env),
batch_env.first_l2_block,
),
program_cache: pre_contract_storage,
storage,
batch_env,
system_env,
snapshots: Vec::new(),
};

mv.write_to_bootloader_heap(bootloader_memory);
mv
}

fn push_transaction(&mut self, tx: Transaction) {
let tx: TransactionData = tx.into();
let overhead = tx.overhead_gas();
Expand Down Expand Up @@ -444,7 +449,7 @@ impl<S: WriteStorage + 'static> VmInterface<S, HistoryEnabled> for Vm<S> {
}
}

impl<S: WriteStorage + 'static> VmInterfaceHistoryEnabled<S> for Vm<S> {
impl<S: WriteStorage + 'static> VmInterfaceHistoryEnabled for Vm<S> {
fn make_snapshot(&mut self) {
todo!()
}
Expand Down
Loading

0 comments on commit 6bde7e7

Please sign in to comment.