From b3b636e77500f475050ffb4cebd1632650c2a198 Mon Sep 17 00:00:00 2001 From: Lucas Fiegl Date: Tue, 4 Nov 2025 09:54:42 -0300 Subject: [PATCH] reuse stack pool --- crates/vm/backends/levm/mod.rs | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/crates/vm/backends/levm/mod.rs b/crates/vm/backends/levm/mod.rs index 08aedca3601..2792b10b5e3 100644 --- a/crates/vm/backends/levm/mod.rs +++ b/crates/vm/backends/levm/mod.rs @@ -18,6 +18,7 @@ use ethrex_common::{ }, }; use ethrex_levm::EVMConfig; +use ethrex_levm::call_frame::Stack; use ethrex_levm::constants::{SYS_CALL_GAS_LIMIT, TX_BASE_COST}; use ethrex_levm::db::gen_db::GeneralizedDatabase; use ethrex_levm::errors::{InternalError, TxValidationError}; @@ -91,13 +92,22 @@ impl LEVM { ) -> Result { Self::prepare_block(block, db, vm_type)?; + let mut shared_stack_pool = Vec::with_capacity(1024); + let mut receipts = Vec::new(); let mut cumulative_gas_used = 0; for (tx, tx_sender) in block.body.get_transactions_with_sender().map_err(|error| { EvmError::Transaction(format!("Couldn't recover addresses with error: {error}")) })? { - let report = Self::execute_tx(tx, tx_sender, &block.header, db, vm_type)?; + let report = Self::execute_tx_in_block( + tx, + tx_sender, + &block.header, + db, + vm_type, + &mut shared_stack_pool, + )?; LEVM::send_state_transitions_tx(&merkleizer, db, queue_length)?; cumulative_gas_used += report.gas_used; @@ -215,6 +225,26 @@ impl LEVM { vm.execute().map_err(VMError::into) } + pub fn execute_tx_in_block( + // The transaction to execute. + tx: &Transaction, + // The transactions recovered address + tx_sender: Address, + // The block header for the current block. + block_header: &BlockHeader, + db: &mut GeneralizedDatabase, + vm_type: VMType, + stack_pool: &mut Vec, + ) -> Result { + let env = Self::setup_env(tx, tx_sender, block_header, db, vm_type)?; + let mut vm = VM::new(env, db, tx, LevmCallTracer::disabled(), vm_type)?; + + std::mem::swap(&mut vm.stack_pool, stack_pool); + let result = vm.execute().map_err(VMError::into); + std::mem::swap(&mut vm.stack_pool, stack_pool); + result + } + pub fn undo_last_tx(db: &mut GeneralizedDatabase) -> Result<(), EvmError> { db.undo_last_transaction()?; Ok(())