From 42db010430164331f781f270b1be3fbcc86bec52 Mon Sep 17 00:00:00 2001 From: Yoni <78365039+Yoni-Starkware@users.noreply.github.com> Date: Tue, 26 Nov 2024 16:52:35 +0200 Subject: [PATCH] refactor(blockifier): share class hash syscalls code (#2282) --- .../src/execution/native/syscall_handler.rs | 31 +++---------------- .../blockifier/src/execution/syscalls/mod.rs | 15 ++------- .../src/execution/syscalls/syscall_base.rs | 22 +++++++++++++ 3 files changed, 29 insertions(+), 39 deletions(-) diff --git a/crates/blockifier/src/execution/native/syscall_handler.rs b/crates/blockifier/src/execution/native/syscall_handler.rs index 1be977b86e..0c0869d535 100644 --- a/crates/blockifier/src/execution/native/syscall_handler.rs +++ b/crates/blockifier/src/execution/native/syscall_handler.rs @@ -259,15 +259,11 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> { self.pre_execute_syscall(remaining_gas, self.gas_costs().get_class_hash_at_gas_cost)?; let request = ContractAddress::try_from(contract_address) .map_err(|err| self.handle_error(remaining_gas, err.into()))?; - self.base.accessed_contract_addresses.insert(request); let class_hash = self .base - .state .get_class_hash_at(request) - .map_err(|err| self.handle_error(remaining_gas, err.into()))?; - self.base.read_class_hash_values.push(class_hash); - + .map_err(|err| self.handle_error(remaining_gas, err))?; Ok(class_hash.0) } @@ -333,27 +329,10 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> { fn replace_class(&mut self, class_hash: Felt, remaining_gas: &mut u64) -> SyscallResult<()> { self.pre_execute_syscall(remaining_gas, self.gas_costs().replace_class_gas_cost)?; - let class_hash = ClassHash(class_hash); - let contract_class = self - .base - .state - .get_compiled_contract_class(class_hash) - .map_err(|e| self.handle_error(remaining_gas, e.into()))?; - - match contract_class { - RunnableContractClass::V0(_) => Err(self.handle_error( - remaining_gas, - SyscallExecutionError::ForbiddenClassReplacement { class_hash }, - )), - RunnableContractClass::V1(_) | RunnableContractClass::V1Native(_) => { - self.base - .state - .set_class_hash_at(self.base.call.storage_address, class_hash) - .map_err(|e| self.handle_error(remaining_gas, e.into()))?; - - Ok(()) - } - } + self.base + .replace_class(ClassHash(class_hash)) + .map_err(|err| self.handle_error(remaining_gas, err))?; + Ok(()) } fn library_call( diff --git a/crates/blockifier/src/execution/syscalls/mod.rs b/crates/blockifier/src/execution/syscalls/mod.rs index 648e3316df..13d4dd117a 100644 --- a/crates/blockifier/src/execution/syscalls/mod.rs +++ b/crates/blockifier/src/execution/syscalls/mod.rs @@ -39,7 +39,6 @@ use crate::execution::execution_utils::{ }; use crate::execution::syscalls::hint_processor::{INVALID_INPUT_LENGTH_ERROR, OUT_OF_GAS_ERROR}; use crate::execution::syscalls::syscall_base::SyscallResult; -use crate::transaction::account_transaction::is_cairo1; use crate::versioned_constants::{EventLimits, VersionedConstants}; pub mod hint_processor; @@ -485,14 +484,7 @@ pub fn replace_class( syscall_handler: &mut SyscallHintProcessor<'_>, _remaining_gas: &mut u64, ) -> SyscallResult { - // Ensure the class is declared (by reading it), and of type V1. - let class_hash = request.class_hash; - let class = syscall_handler.base.state.get_compiled_contract_class(class_hash)?; - - if !is_cairo1(&class) { - return Err(SyscallExecutionError::ForbiddenClassReplacement { class_hash }); - } - syscall_handler.base.state.set_class_hash_at(syscall_handler.storage_address(), class_hash)?; + syscall_handler.base.replace_class(request.class_hash)?; Ok(ReplaceClassResponse {}) } @@ -791,8 +783,5 @@ pub(crate) fn get_class_hash_at( syscall_handler: &mut SyscallHintProcessor<'_>, _remaining_gas: &mut u64, ) -> SyscallResult { - syscall_handler.base.accessed_contract_addresses.insert(request); - let class_hash = syscall_handler.base.state.get_class_hash_at(request)?; - syscall_handler.base.read_class_hash_values.push(class_hash); - Ok(class_hash) + syscall_handler.base.get_class_hash_at(request) } diff --git a/crates/blockifier/src/execution/syscalls/syscall_base.rs b/crates/blockifier/src/execution/syscalls/syscall_base.rs index ccdc1ecf0f..54bc82d1ce 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_base.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_base.rs @@ -17,6 +17,7 @@ use crate::execution::syscalls::hint_processor::{ ENTRYPOINT_FAILED_ERROR, }; use crate::state::state_api::State; +use crate::transaction::account_transaction::is_cairo1; pub type SyscallResult = Result; @@ -124,6 +125,16 @@ impl<'state> SyscallHandlerBase<'state> { Ok(()) } + pub fn get_class_hash_at( + &mut self, + contract_address: ContractAddress, + ) -> SyscallResult { + self.accessed_contract_addresses.insert(contract_address); + let class_hash = self.state.get_class_hash_at(contract_address)?; + self.read_class_hash_values.push(class_hash); + Ok(class_hash) + } + pub fn emit_event(&mut self, event: EventContent) -> SyscallResult<()> { exceeds_event_size_limit( self.context.versioned_constants(), @@ -137,6 +148,17 @@ impl<'state> SyscallHandlerBase<'state> { Ok(()) } + pub fn replace_class(&mut self, class_hash: ClassHash) -> SyscallResult<()> { + // Ensure the class is declared (by reading it), and of type V1. + let class = self.state.get_compiled_contract_class(class_hash)?; + + if !is_cairo1(&class) { + return Err(SyscallExecutionError::ForbiddenClassReplacement { class_hash }); + } + self.state.set_class_hash_at(self.call.storage_address, class_hash)?; + Ok(()) + } + pub fn execute_inner_call( &mut self, call: CallEntryPoint,