Skip to content

Commit

Permalink
refactor(blockifier): share execute_inner_call code
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoni-Starkware committed Nov 25, 2024
1 parent 19260eb commit 0aa3e21
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 30 deletions.
31 changes: 1 addition & 30 deletions crates/blockifier/src/execution/syscalls/hint_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use starknet_types_core::felt::{Felt, FromStrError};
use thiserror::Error;

use crate::abi::sierra_types::SierraTypeError;
use crate::execution::call_info::CallInfo;
use crate::execution::common_hints::{ExecutionMode, HintExecutionResult};
use crate::execution::entry_point::{CallEntryPoint, EntryPointExecutionContext};
use crate::execution::errors::{ConstructorEntryPointExecutionError, EntryPointExecutionError};
Expand Down Expand Up @@ -750,35 +749,7 @@ pub fn execute_inner_call(
syscall_handler: &mut SyscallHintProcessor<'_>,
remaining_gas: &mut u64,
) -> SyscallResult<ReadOnlySegment> {
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)
}

Expand Down
41 changes: 41 additions & 0 deletions crates/blockifier/src/execution/syscalls/syscall_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,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;

Expand Down Expand Up @@ -97,4 +98,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<Vec<Felt>> {
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)
}
}

0 comments on commit 0aa3e21

Please sign in to comment.