From 2bb912a1d5e17b7d25afae6e6c05bc9aa8c72670 Mon Sep 17 00:00:00 2001 From: Marcos Nicolau Date: Mon, 2 Sep 2024 18:46:57 -0300 Subject: [PATCH] Fix parallel execution logs --- .../era_vm/bootloader_state/l2_block.rs | 1 - .../era_vm/tests/parallel_execution.rs | 4 +- .../versions/era_vm/tests/tester/vm_tester.rs | 30 ++++----- core/lib/multivm/src/versions/era_vm/vm.rs | 61 +++++++++++++++++-- 4 files changed, 74 insertions(+), 22 deletions(-) diff --git a/core/lib/multivm/src/versions/era_vm/bootloader_state/l2_block.rs b/core/lib/multivm/src/versions/era_vm/bootloader_state/l2_block.rs index 48dcb1d3cae..23704ed2a08 100644 --- a/core/lib/multivm/src/versions/era_vm/bootloader_state/l2_block.rs +++ b/core/lib/multivm/src/versions/era_vm/bootloader_state/l2_block.rs @@ -57,7 +57,6 @@ impl BootloaderL2Block { pub(crate) fn interim_version(&self) -> BootloaderL2Block { let mut interim = self.clone(); interim.max_virtual_blocks_to_create = 0; - println!("IN INTERIM VERSION"); interim } diff --git a/core/lib/multivm/src/versions/era_vm/tests/parallel_execution.rs b/core/lib/multivm/src/versions/era_vm/tests/parallel_execution.rs index 7f1f75cdabc..7748f786c25 100644 --- a/core/lib/multivm/src/versions/era_vm/tests/parallel_execution.rs +++ b/core/lib/multivm/src/versions/era_vm/tests/parallel_execution.rs @@ -10,7 +10,7 @@ use crate::{ fn prepare_test(is_parallel: bool) -> (VmTester, [Transaction; 3]) { let bytes = [1; 32]; let account = Account::new(K256PrivateKey::from_bytes(bytes.into()).unwrap()); - dbg!(&account); + let mut vm_tester = VmTesterBuilder::new() .with_empty_in_memory_storage() .with_deployer() @@ -18,7 +18,7 @@ fn prepare_test(is_parallel: bool) -> (VmTester, [Transaction; 3]) { .build(); if is_parallel { - vm_tester.deploy_test_contract(); + vm_tester.deploy_test_contract_parallel(); } else { vm_tester.deploy_test_contract(); } diff --git a/core/lib/multivm/src/versions/era_vm/tests/tester/vm_tester.rs b/core/lib/multivm/src/versions/era_vm/tests/tester/vm_tester.rs index 51c2ec9b180..a9be55109f1 100644 --- a/core/lib/multivm/src/versions/era_vm/tests/tester/vm_tester.rs +++ b/core/lib/multivm/src/versions/era_vm/tests/tester/vm_tester.rs @@ -52,21 +52,21 @@ impl VmTester { self.test_contract = Some(deployed_address); } - // pub(crate) fn deploy_test_contract_parallel(&mut self) { - // let contract = read_test_contract(); - // let tx = self - // .deployer - // .as_mut() - // .expect("You have to initialize builder with deployer") - // .get_deploy_tx(&contract, None, TxType::L2) - // .tx; - // let nonce = tx.nonce().unwrap().0.into(); - // self.vm.push_parallel_transaction(tx, 0, true); - // self.vm.execute_parallel(VmExecutionMode::OneTx); - // let deployed_address = - // deployed_address_create(self.deployer.as_ref().unwrap().address, nonce); - // self.test_contract = Some(deployed_address); - // } + pub(crate) fn deploy_test_contract_parallel(&mut self) { + let contract = read_test_contract(); + let tx = self + .deployer + .as_mut() + .expect("You have to initialize builder with deployer") + .get_deploy_tx(&contract, None, TxType::L2) + .tx; + let nonce = tx.nonce().unwrap().0.into(); + self.vm.push_parallel_transaction(tx, 0, true); + self.vm.execute_parallel(VmExecutionMode::OneTx); + let deployed_address = + deployed_address_create(self.deployer.as_ref().unwrap().address, nonce); + self.test_contract = Some(deployed_address); + } pub(crate) fn reset_with_empty_storage(&mut self) { self.storage = Rc::new(RefCell::new(get_empty_storage())); diff --git a/core/lib/multivm/src/versions/era_vm/vm.rs b/core/lib/multivm/src/versions/era_vm/vm.rs index 5a769674b97..deb2880a6b8 100644 --- a/core/lib/multivm/src/versions/era_vm/vm.rs +++ b/core/lib/multivm/src/versions/era_vm/vm.rs @@ -577,6 +577,58 @@ impl Vm { } } + pub fn inspect_inner_with_custom_snapshot( + &mut self, + tracer: TracerDispatcher, + custom_pubdata_tracer: Option, + execution_mode: VmExecutionMode, + snapshot: era_vm::state::StateSnapshot, + ) -> VmExecutionResultAndLogs { + let mut track_refunds = false; + if let VmExecutionMode::OneTx = execution_mode { + // Move the pointer to the next transaction + self.bootloader_state.move_tx_to_execute_pointer(); + track_refunds = true; + } + + let refund_tracer = if track_refunds { + Some(RefundsTracer::new()) + } else { + None + }; + let mut tracer = + VmTracerManager::new(execution_mode, tracer, refund_tracer, custom_pubdata_tracer); + let ergs_before = self.inner.execution.gas_left().unwrap(); + let monotonic_counter_before = self.inner.statistics.monotonic_counter; + + let result = self.run(execution_mode, &mut tracer); + let ergs_after = self.inner.execution.gas_left().unwrap(); + + let ignore_world_diff = matches!(execution_mode, VmExecutionMode::OneTx) + && matches!(result, ExecutionResult::Halt { .. }); + + let logs = if ignore_world_diff { + VmExecutionLogs::default() + } else { + self.get_execution_logs(snapshot) + }; + + VmExecutionResultAndLogs { + result, + logs, + statistics: self.get_execution_statistics( + monotonic_counter_before, + tracer.pubdata_tracer.pubdata_published, + ergs_before, + ergs_after, + tracer + .circuits_tracer + .circuit_statistics(&self.inner.statistics), + ), + refunds: tracer.refund_tracer.unwrap_or_default().into(), + } + } + fn get_execution_logs(&self, snapshot: era_vm::state::StateSnapshot) -> VmExecutionLogs { let events = merge_events( self.inner.state.get_events_after_snapshot(snapshot.events), @@ -660,7 +712,7 @@ impl Vm { // and sealing the batch from the final merged state. pub fn execute_parallel_inner(&mut self, one_tx: bool) -> VmExecutionResultAndLogs { let txs_to_process = if one_tx { - 1 + self.transaction_to_execute.len() } else { self.transaction_to_execute.len() }; @@ -671,6 +723,7 @@ impl Vm { // we only care about the final VMState, since that is where the pubdata and L2 changes reside // we will merge this results later + let snapshot = self.inner.state.snapshot(); let mut state: era_vm::state::VMState = self.inner.state.clone(); // to run in parallel, we spin up new vms to process and run the transaction in their own bootloader @@ -681,7 +734,7 @@ impl Vm { self.storage.clone(), ); // set the last block to what the main vm has - vm.bootloader_state.l2_blocks.push(BootloaderL2Block { + vm.bootloader_state.l2_blocks[0] = BootloaderL2Block { number: self.bootloader_state.last_l2_block().number, timestamp: self.bootloader_state.last_l2_block().timestamp, txs_rolling_hash: self.bootloader_state.last_l2_block().txs_rolling_hash, @@ -692,7 +745,7 @@ impl Vm { .last_l2_block() .max_virtual_blocks_to_create, txs: vec![], - }); + }; vm.inner.state = state; vm.is_parallel = true; vm.current_tx = self.current_tx; @@ -705,6 +758,7 @@ impl Vm { if vm.current_tx == 0 { // since the first transaction starts the batch, we want to push it normally so that // it create the virtual block at the beginning + vm.is_parallel = false; vm.push_transaction_inner_no_bytecode( tx.tx.clone(), tx.refund, @@ -740,7 +794,6 @@ impl Vm { let ergs_before = self.inner.execution.gas_left().unwrap(); let monotonic_counter_before = self.inner.statistics.monotonic_counter; - let snapshot = self.inner.state.snapshot(); self.inner.state = state; self.is_parallel = true;