Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(blockifier): share execute_inner_call code #2263

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
Loading