Skip to content

Commit

Permalink
chore(blockifier): share syscall handler's fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoni-Starkware committed Nov 24, 2024
1 parent 29f5d16 commit 5847aba
Show file tree
Hide file tree
Showing 6 changed files with 319 additions and 339 deletions.
29 changes: 15 additions & 14 deletions crates/blockifier/src/execution/entry_point_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub fn execute_entry_point_call(
} = initialize_execution_context(call, &contract_class, state, context)?;

let args = prepare_call_arguments(
&syscall_handler.call,
&syscall_handler.base.call,
&mut runner,
initial_syscall_ptr,
&mut syscall_handler.read_only_segments,
Expand All @@ -93,7 +93,7 @@ pub fn execute_entry_point_call(
// Collect the set PC values that were visited during the entry point execution.
register_visited_pcs(
&mut runner,
syscall_handler.state,
syscall_handler.base.state,
class_hash,
program_segment_size,
bytecode_length,
Expand Down Expand Up @@ -416,7 +416,7 @@ pub fn finalize_execution(
.get_execution_resources()
.map_err(VirtualMachineError::RunnerError)?
.filter_unused_builtins();
let versioned_constants = syscall_handler.context.versioned_constants();
let versioned_constants = syscall_handler.base.context.versioned_constants();
if versioned_constants.segment_arena_cells {
vm_resources_without_inner_calls
.builtin_instance_counter
Expand All @@ -435,24 +435,25 @@ pub fn finalize_execution(
gas_for_fee: GasAmount(0),
};
let charged_resources = &charged_resources_without_inner_calls
+ &CallInfo::summarize_charged_resources(syscall_handler.inner_calls.iter());
+ &CallInfo::summarize_charged_resources(syscall_handler.base.inner_calls.iter());

let syscall_handler_base = syscall_handler.base;
Ok(CallInfo {
call: syscall_handler.call,
call: syscall_handler_base.call,
execution: CallExecution {
retdata: call_result.retdata,
events: syscall_handler.events,
l2_to_l1_messages: syscall_handler.l2_to_l1_messages,
events: syscall_handler_base.events,
l2_to_l1_messages: syscall_handler_base.l2_to_l1_messages,
failed: call_result.failed,
gas_consumed: call_result.gas_consumed,
},
inner_calls: syscall_handler.inner_calls,
inner_calls: syscall_handler_base.inner_calls,
tracked_resource,
charged_resources,
storage_read_values: syscall_handler.read_values,
accessed_storage_keys: syscall_handler.accessed_keys,
read_class_hash_values: syscall_handler.read_class_hash_values,
accessed_contract_addresses: syscall_handler.accessed_contract_addresses,
storage_read_values: syscall_handler_base.read_values,
accessed_storage_keys: syscall_handler_base.accessed_keys,
read_class_hash_values: syscall_handler_base.read_class_hash_values,
accessed_contract_addresses: syscall_handler_base.accessed_contract_addresses,
})
}

Expand Down Expand Up @@ -489,13 +490,13 @@ fn get_call_result(
error_message: format!("Unexpected remaining gas: {gas}."),
})?;

if gas > syscall_handler.call.initial_gas {
if gas > syscall_handler.base.call.initial_gas {
return Err(PostExecutionError::MalformedReturnData {
error_message: format!("Unexpected remaining gas: {gas}."),
});
}

let gas_consumed = syscall_handler.call.initial_gas - gas;
let gas_consumed = syscall_handler.base.call.initial_gas - gas;
Ok(CallResult {
failed,
retdata: read_execution_retdata(runner, retdata_size, retdata_start)?,
Expand Down
36 changes: 18 additions & 18 deletions crates/blockifier/src/execution/native/entry_point_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn execute_entry_point_call(
let mut syscall_handler: NativeSyscallHandler<'_> =
NativeSyscallHandler::new(call, state, context);

let gas_costs = &syscall_handler.context.versioned_constants().os_constants.gas_costs;
let gas_costs = &syscall_handler.base.context.versioned_constants().os_constants.gas_costs;
let builtin_costs = BuiltinCosts {
// todo(rodrigo): Unsure of what value `const` means, but 1 is the right value
r#const: 1,
Expand All @@ -41,10 +41,10 @@ pub fn execute_entry_point_call(

// Fund the initial budget since the native executor charges it before the run.
// TODO(Yoni): revert once the VM is aligned with this.
let gas = syscall_handler.call.initial_gas + gas_costs.entry_point_initial_budget;
let gas = syscall_handler.base.call.initial_gas + gas_costs.entry_point_initial_budget;
let execution_result = contract_class.executor.run(
entry_point.selector.0,
&syscall_handler.call.calldata.0.clone(),
&syscall_handler.base.call.calldata.0.clone(),
Some(gas),
Some(builtin_costs),
&mut syscall_handler,
Expand All @@ -65,49 +65,49 @@ fn create_callinfo(
) -> Result<CallInfo, EntryPointExecutionError> {
let mut remaining_gas = call_result.remaining_gas;

if remaining_gas > syscall_handler.call.initial_gas {
if remaining_gas - syscall_handler.call.initial_gas
<= syscall_handler.context.gas_costs().entry_point_initial_budget
if remaining_gas > syscall_handler.base.call.initial_gas {
if remaining_gas - syscall_handler.base.call.initial_gas
<= syscall_handler.base.context.gas_costs().entry_point_initial_budget
{
// Revert the refund.
// TODO(Yoni): temporary hack - this is probably a bug. Investigate and fix native.
remaining_gas = syscall_handler.call.initial_gas;
remaining_gas = syscall_handler.base.call.initial_gas;
} else {
return Err(PostExecutionError::MalformedReturnData {
error_message: format!(
"Unexpected remaining gas. Used gas is greater than initial gas: {} > {}",
remaining_gas, syscall_handler.call.initial_gas
remaining_gas, syscall_handler.base.call.initial_gas
),
}
.into());
}
}

let gas_consumed = syscall_handler.call.initial_gas - remaining_gas;
let gas_consumed = syscall_handler.base.call.initial_gas - remaining_gas;

let charged_resources_without_inner_calls = ChargedResources {
vm_resources: ExecutionResources::default(),
// TODO(tzahi): Replace with a computed value.
gas_for_fee: GasAmount(0),
};
let charged_resources = &charged_resources_without_inner_calls
+ &CallInfo::summarize_charged_resources(syscall_handler.inner_calls.iter());
+ &CallInfo::summarize_charged_resources(syscall_handler.base.inner_calls.iter());

Ok(CallInfo {
call: syscall_handler.call,
call: syscall_handler.base.call,
execution: CallExecution {
retdata: Retdata(call_result.return_values),
events: syscall_handler.events,
l2_to_l1_messages: syscall_handler.l2_to_l1_messages,
events: syscall_handler.base.events,
l2_to_l1_messages: syscall_handler.base.l2_to_l1_messages,
failed: call_result.failure_flag,
gas_consumed,
},
charged_resources,
inner_calls: syscall_handler.inner_calls,
storage_read_values: syscall_handler.read_values,
accessed_storage_keys: syscall_handler.accessed_keys,
accessed_contract_addresses: syscall_handler.accessed_contract_addresses,
read_class_hash_values: syscall_handler.read_class_hash_values,
inner_calls: syscall_handler.base.inner_calls,
storage_read_values: syscall_handler.base.read_values,
accessed_storage_keys: syscall_handler.base.accessed_keys,
accessed_contract_addresses: syscall_handler.base.accessed_contract_addresses,
read_class_hash_values: syscall_handler.base.read_class_hash_values,
tracked_resource: TrackedResource::SierraGas,
})
}
Loading

0 comments on commit 5847aba

Please sign in to comment.