From 6ae3f6893b401cc358ac4f1c6a9d714473a1d76c Mon Sep 17 00:00:00 2001 From: Yonatan Iluz Date: Mon, 25 Nov 2024 11:02:14 +0200 Subject: [PATCH] refactor(blockifier): share execute_inner_call code --- .../src/execution/syscalls/hint_processor.rs | 30 +------------- .../src/execution/syscalls/syscall_base.rs | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+), 29 deletions(-) diff --git a/crates/blockifier/src/execution/syscalls/hint_processor.rs b/crates/blockifier/src/execution/syscalls/hint_processor.rs index 3e3af676bb1..27d6671a61d 100644 --- a/crates/blockifier/src/execution/syscalls/hint_processor.rs +++ b/crates/blockifier/src/execution/syscalls/hint_processor.rs @@ -751,35 +751,7 @@ pub fn execute_inner_call( syscall_handler: &mut SyscallHintProcessor<'_>, remaining_gas: &mut u64, ) -> SyscallResult { - let revert_idx = syscall_handler.base.context.revert_infos.0.len(); - - let call_info = - call.execute(syscall_handler.base.state, syscall_handler.base.context, remaining_gas)?; - - let mut raw_retdata = call_info.execution.retdata.0.clone(); - let failed = call_info.execution.failed; - syscall_handler.base.inner_calls.push(call_info); - if failed { - syscall_handler.base.context.revert(revert_idx, syscall_handler.base.state)?; - - // Delete events and l2_to_l1_messages from the reverted call. - let reverted_call = &mut syscall_handler.base.inner_calls.last_mut().unwrap(); - let mut stack: Vec<&mut CallInfo> = vec![reverted_call]; - while let Some(call_info) = stack.pop() { - call_info.execution.events.clear(); - call_info.execution.l2_to_l1_messages.clear(); - // Add inner calls that did not fail to the stack. - // The events and l2_to_l1_messages of the failed calls were already cleared. - stack.extend( - call_info.inner_calls.iter_mut().filter(|call_info| !call_info.execution.failed), - ); - } - - raw_retdata - .push(Felt::from_hex(ENTRYPOINT_FAILED_ERROR).map_err(SyscallExecutionError::from)?); - return Err(SyscallExecutionError::SyscallError { error_data: raw_retdata }); - } - + let raw_retdata = syscall_handler.base.execute_inner_call(call, remaining_gas)?; create_retdata_segment(vm, syscall_handler, &raw_retdata) } diff --git a/crates/blockifier/src/execution/syscalls/syscall_base.rs b/crates/blockifier/src/execution/syscalls/syscall_base.rs index e06badbd134..df29b93c624 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_base.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_base.rs @@ -13,6 +13,7 @@ use crate::execution::entry_point::{CallEntryPoint, EntryPointExecutionContext}; use crate::execution::syscalls::hint_processor::{ SyscallExecutionError, BLOCK_NUMBER_OUT_OF_RANGE_ERROR, + ENTRYPOINT_FAILED_ERROR, }; use crate::state::state_api::State; @@ -98,4 +99,44 @@ impl<'state> SyscallHandlerBase<'state> { ContractAddress::try_from(Felt::from(constants::BLOCK_HASH_CONTRACT_ADDRESS))?; Ok(self.state.get_storage_at(block_hash_contract_address, key)?) } + + pub fn execute_inner_call( + &mut self, + call: CallEntryPoint, + remaining_gas: &mut u64, + ) -> SyscallResult> { + let revert_idx = self.context.revert_infos.0.len(); + + let call_info = call.execute(self.state, self.context, remaining_gas)?; + + let mut raw_retdata = call_info.execution.retdata.0.clone(); + let failed = call_info.execution.failed; + self.inner_calls.push(call_info); + if failed { + self.context.revert(revert_idx, self.state)?; + + // Delete events and l2_to_l1_messages from the reverted call. + let reverted_call = &mut self.inner_calls.last_mut().unwrap(); + let mut stack: Vec<&mut CallInfo> = vec![reverted_call]; + while let Some(call_info) = stack.pop() { + call_info.execution.events.clear(); + call_info.execution.l2_to_l1_messages.clear(); + // Add inner calls that did not fail to the stack. + // The events and l2_to_l1_messages of the failed calls were already cleared. + stack.extend( + call_info + .inner_calls + .iter_mut() + .filter(|call_info| !call_info.execution.failed), + ); + } + + raw_retdata.push( + Felt::from_hex(ENTRYPOINT_FAILED_ERROR).map_err(SyscallExecutionError::from)?, + ); + return Err(SyscallExecutionError::SyscallError { error_data: raw_retdata }); + } + + Ok(raw_retdata) + } }