Skip to content

Commit

Permalink
refactor: small code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigo-pino committed Oct 14, 2024
1 parent e7f4d37 commit 3e26be2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 33 deletions.
7 changes: 2 additions & 5 deletions crates/blockifier/src/execution/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,8 @@ pub enum EntryPointExecutionError {
InvalidExecutionInput { input_descriptor: String, info: String },
#[error("Native execution error: {info}")]
NativeExecutionError { info: String },
#[error("Native unexpected error: {source}")]
NativeUnexpectedError {
#[source]
source: NativeError,
},
#[error(transparent)]
NativeUnexpectedError(#[from] NativeError),
#[error(transparent)]
PostExecutionError(#[from] PostExecutionError),
#[error(transparent)]
Expand Down
48 changes: 20 additions & 28 deletions crates/blockifier/src/execution/native/entry_point_execution.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::collections::HashMap;

use cairo_lang_sierra::ids::FunctionId;
use cairo_native::execution_result::ContractExecutionResult;
use cairo_native::executor::AotNativeExecutor;
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use num_traits::ToPrimitive;

use crate::execution::call_info::{CallExecution, CallInfo, Retdata};
use crate::execution::contract_class::{NativeContractClassV1, TrackedResource};
Expand All @@ -12,7 +11,7 @@ use crate::execution::entry_point::{
EntryPointExecutionContext,
EntryPointExecutionResult,
};
use crate::execution::errors::EntryPointExecutionError;
use crate::execution::errors::{EntryPointExecutionError, PostExecutionError};
use crate::execution::native::syscall_handler::NativeSyscallHandler;
use crate::execution::native::utils::decode_felts_as_str;
use crate::state::state_api::State;
Expand Down Expand Up @@ -51,10 +50,8 @@ fn run_native_executor(
&mut syscall_handler,
);

let run_result = match execution_result {
Err(runner_err) => {
Err(EntryPointExecutionError::NativeUnexpectedError { source: runner_err })
}
let call_result = match execution_result {
Err(runner_err) => Err(EntryPointExecutionError::NativeUnexpectedError(runner_err)),
Ok(res) if res.failure_flag => Err(EntryPointExecutionError::NativeExecutionError {
info: if !res.return_values.is_empty() {
decode_felts_as_str(&res.return_values)
Expand All @@ -65,43 +62,38 @@ fn run_native_executor(
Ok(res) => Ok(res),
}?;

create_callinfo(call.clone(), run_result, syscall_handler)
create_callinfo(call, call_result, syscall_handler)
}

fn create_callinfo(
call: CallEntryPoint,
run_result: ContractExecutionResult,
call_result: ContractExecutionResult,
syscall_handler: NativeSyscallHandler<'_>,
) -> Result<CallInfo, EntryPointExecutionError> {
let gas_consumed = {
// We can use `.unwrap()` directly in both cases because the most significant bit is could
// be only 63 here (128 = 64 + 64).
let low: u64 = (run_result.remaining_gas & ((1u128 << 64) - 1)).try_into().unwrap();
let high: u64 = (run_result.remaining_gas >> 64).try_into().unwrap();
if high != 0 {
return Err(EntryPointExecutionError::NativeExecutionError {
info: "Overflow: gas consumed bigger than 64 bit".into(),
});
}
call.initial_gas - low
};
// todo(rodro): Even if the property is called `remaining_gas` it behaves like gas used.
// Update once gas works on Native side has been completed (or at least this part)
let gas_used =
call_result.remaining_gas.to_u64().ok_or(PostExecutionError::MalformedReturnData {
error_message: format!(
"Unexpected remaining gas (bigger than u64): {}",
call_result.remaining_gas
),
})?;

let gas_consumed = call.initial_gas - gas_used;

Ok(CallInfo {
call,
execution: CallExecution {
retdata: Retdata(run_result.return_values),
retdata: Retdata(call_result.return_values),
events: syscall_handler.events,
l2_to_l1_messages: syscall_handler.l2_to_l1_messages,
failed: run_result.failure_flag,
failed: call_result.failure_flag,
gas_consumed,
},
// todo(rodrigo): execution resources rely heavily on how the VM work, therefore
// the dummy values
resources: ExecutionResources {
n_steps: 0,
n_memory_holes: 0,
builtin_instance_counter: HashMap::default(),
},
resources: ExecutionResources::default(),
inner_calls: syscall_handler.inner_calls,
storage_read_values: syscall_handler.read_values,
accessed_storage_keys: syscall_handler.accessed_keys,
Expand Down

0 comments on commit 3e26be2

Please sign in to comment.