Skip to content

Commit

Permalink
refactor(blockifier): share class hash syscalls code (#2282)
Browse files Browse the repository at this point in the history
Yoni-Starkware authored Nov 26, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 8533938 commit 42db010
Showing 3 changed files with 29 additions and 39 deletions.
31 changes: 5 additions & 26 deletions crates/blockifier/src/execution/native/syscall_handler.rs
Original file line number Diff line number Diff line change
@@ -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(
15 changes: 2 additions & 13 deletions crates/blockifier/src/execution/syscalls/mod.rs
Original file line number Diff line number Diff line change
@@ -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<ReplaceClassResponse> {
// 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<GetClassHashAtResponse> {
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)
}
22 changes: 22 additions & 0 deletions crates/blockifier/src/execution/syscalls/syscall_base.rs
Original file line number Diff line number Diff line change
@@ -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<T> = Result<T, SyscallExecutionError>;

@@ -124,6 +125,16 @@ impl<'state> SyscallHandlerBase<'state> {
Ok(())
}

pub fn get_class_hash_at(
&mut self,
contract_address: ContractAddress,
) -> SyscallResult<ClassHash> {
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,

0 comments on commit 42db010

Please sign in to comment.