Skip to content

Commit

Permalink
refactor(blockifier): share emit_event and storage_read syscalls (#2273)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoni-Starkware authored Nov 26, 2024
1 parent abf5736 commit 00e4ac7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 45 deletions.
22 changes: 3 additions & 19 deletions crates/blockifier/src/execution/native/syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use starknet_api::transaction::fields::{Calldata, ContractAddressSalt};
use starknet_api::transaction::{EventContent, EventData, EventKey, L2ToL1Payload};
use starknet_types_core::felt::Felt;

use crate::execution::call_info::{MessageToL1, OrderedEvent, OrderedL2ToL1Message, Retdata};
use crate::execution::call_info::{MessageToL1, OrderedL2ToL1Message, Retdata};
use crate::execution::common_hints::ExecutionMode;
use crate::execution::contract_class::RunnableContractClass;
use crate::execution::entry_point::{
Expand All @@ -43,7 +43,6 @@ use crate::execution::errors::EntryPointExecutionError;
use crate::execution::execution_utils::execute_deployment;
use crate::execution::native::utils::{calculate_resource_bounds, default_tx_v2_info};
use crate::execution::secp;
use crate::execution::syscalls::exceeds_event_size_limit;
use crate::execution::syscalls::hint_processor::{
SyscallExecutionError,
INVALID_INPUT_LENGTH_ERROR,
Expand Down Expand Up @@ -441,12 +440,7 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> {
let key = StorageKey::try_from(address)
.map_err(|e| self.handle_error(remaining_gas, e.into()))?;

let read_result = self.base.state.get_storage_at(self.base.call.storage_address, key);
let value = read_result.map_err(|e| self.handle_error(remaining_gas, e.into()))?;

self.base.accessed_keys.insert(key);
self.base.read_values.push(value);

let value = self.base.storage_read(key).map_err(|e| self.handle_error(remaining_gas, e))?;
Ok(value)
}

Expand Down Expand Up @@ -480,22 +474,12 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> {
) -> SyscallResult<()> {
self.pre_execute_syscall(remaining_gas, self.gas_costs().emit_event_gas_cost)?;

let order = self.base.context.n_emitted_events;
let event = EventContent {
keys: keys.iter().copied().map(EventKey).collect(),
data: EventData(data.to_vec()),
};

exceeds_event_size_limit(
self.base.context.versioned_constants(),
self.base.context.n_emitted_events + 1,
&event,
)
.map_err(|e| self.handle_error(remaining_gas, e.into()))?;

self.base.events.push(OrderedEvent { order, event });
self.base.context.n_emitted_events += 1;

self.base.emit_event(event).map_err(|e| self.handle_error(remaining_gas, e))?;
Ok(())
}

Expand Down
13 changes: 0 additions & 13 deletions crates/blockifier/src/execution/syscalls/hint_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use cairo_vm::vm::errors::vm_errors::VirtualMachineError;
use cairo_vm::vm::runners::cairo_runner::{ResourceTracker, RunResources};
use cairo_vm::vm::vm_core::VirtualMachine;
use starknet_api::core::{ClassHash, ContractAddress, EntryPointSelector};
use starknet_api::state::StorageKey;
use starknet_api::transaction::fields::{
AllResourceBounds,
Calldata,
Expand Down Expand Up @@ -64,7 +63,6 @@ use crate::execution::syscalls::{
sha_256_process_block,
storage_read,
storage_write,
StorageReadResponse,
SyscallRequest,
SyscallRequestWrapper,
SyscallResponse,
Expand Down Expand Up @@ -631,17 +629,6 @@ impl<'a> SyscallHintProcessor<'a> {
Ok(tx_info_start_ptr)
}

pub fn get_contract_storage_at(
&mut self,
key: StorageKey,
) -> SyscallResult<StorageReadResponse> {
self.base.accessed_keys.insert(key);
let value = self.base.state.get_storage_at(self.storage_address(), key)?;
self.base.read_values.push(value);

Ok(StorageReadResponse { value })
}

pub fn finalize(&mut self) {
self.base.finalize();
}
Expand Down
17 changes: 4 additions & 13 deletions crates/blockifier/src/execution/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use self::hint_processor::{
SyscallExecutionError,
SyscallHintProcessor,
};
use crate::execution::call_info::{MessageToL1, OrderedEvent, OrderedL2ToL1Message};
use crate::execution::call_info::{MessageToL1, OrderedL2ToL1Message};
use crate::execution::deprecated_syscalls::DeprecatedSyscallSelector;
use crate::execution::entry_point::{CallEntryPoint, CallType, ConstructorContext};
use crate::execution::execution_utils::{
Expand Down Expand Up @@ -332,17 +332,7 @@ pub fn emit_event(
syscall_handler: &mut SyscallHintProcessor<'_>,
_remaining_gas: &mut u64,
) -> SyscallResult<EmitEventResponse> {
let execution_context = &mut syscall_handler.base.context;
exceeds_event_size_limit(
execution_context.versioned_constants(),
execution_context.n_emitted_events + 1,
&request.content,
)?;
let ordered_event =
OrderedEvent { order: execution_context.n_emitted_events, event: request.content };
syscall_handler.base.events.push(ordered_event);
execution_context.n_emitted_events += 1;

syscall_handler.base.emit_event(request.content)?;
Ok(EmitEventResponse {})
}

Expand Down Expand Up @@ -580,7 +570,8 @@ pub fn storage_read(
syscall_handler: &mut SyscallHintProcessor<'_>,
_remaining_gas: &mut u64,
) -> SyscallResult<StorageReadResponse> {
syscall_handler.get_contract_storage_at(request.address)
let value = syscall_handler.base.storage_read(request.address)?;
Ok(StorageReadResponse { value })
}

// StorageWrite syscall.
Expand Down
22 changes: 22 additions & 0 deletions crates/blockifier/src/execution/syscalls/syscall_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use std::convert::From;

use starknet_api::core::{ClassHash, ContractAddress};
use starknet_api::state::StorageKey;
use starknet_api::transaction::EventContent;
use starknet_types_core::felt::Felt;

use super::exceeds_event_size_limit;
use crate::abi::constants;
use crate::execution::call_info::{CallInfo, OrderedEvent, OrderedL2ToL1Message};
use crate::execution::common_hints::ExecutionMode;
Expand Down Expand Up @@ -99,6 +101,13 @@ impl<'state> SyscallHandlerBase<'state> {
Ok(self.state.get_storage_at(block_hash_contract_address, key)?)
}

pub fn storage_read(&mut self, key: StorageKey) -> SyscallResult<Felt> {
self.accessed_keys.insert(key);
let value = self.state.get_storage_at(self.call.storage_address, key)?;
self.read_values.push(value);
Ok(value)
}

pub fn storage_write(&mut self, key: StorageKey, value: Felt) -> SyscallResult<()> {
let contract_address = self.call.storage_address;

Expand All @@ -115,6 +124,19 @@ impl<'state> SyscallHandlerBase<'state> {
Ok(())
}

pub fn emit_event(&mut self, event: EventContent) -> SyscallResult<()> {
exceeds_event_size_limit(
self.context.versioned_constants(),
self.context.n_emitted_events + 1,
&event,
)?;
let ordered_event = OrderedEvent { order: self.context.n_emitted_events, event };
self.events.push(ordered_event);
self.context.n_emitted_events += 1;

Ok(())
}

pub fn execute_inner_call(
&mut self,
call: CallEntryPoint,
Expand Down

0 comments on commit 00e4ac7

Please sign in to comment.