Skip to content

Commit

Permalink
refactor(blockifier): share deploy syscall code
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoni-Starkware committed Nov 26, 2024
1 parent 42db010 commit 787a25e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 79 deletions.
58 changes: 12 additions & 46 deletions crates/blockifier/src/execution/native/syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,16 @@ use cairo_native::starknet::{
};
use num_bigint::BigUint;
use starknet_api::contract_class::EntryPointType;
use starknet_api::core::{
calculate_contract_address,
ClassHash,
ContractAddress,
EntryPointSelector,
EthAddress,
};
use starknet_api::core::{ClassHash, ContractAddress, EntryPointSelector, EthAddress};
use starknet_api::state::StorageKey;
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, OrderedL2ToL1Message, Retdata};
use crate::execution::common_hints::ExecutionMode;
use crate::execution::contract_class::RunnableContractClass;
use crate::execution::entry_point::{
CallEntryPoint,
CallType,
ConstructorContext,
EntryPointExecutionContext,
};
use crate::execution::entry_point::{CallEntryPoint, CallType, EntryPointExecutionContext};
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::hint_processor::{
Expand Down Expand Up @@ -289,39 +276,18 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> {
) -> SyscallResult<(Felt, Vec<Felt>)> {
self.pre_execute_syscall(remaining_gas, self.gas_costs().deploy_gas_cost)?;

let deployer_address = self.base.call.storage_address;
let deployer_address_for_calculation =
if deploy_from_zero { ContractAddress::default() } else { deployer_address };

let class_hash = ClassHash(class_hash);
let calldata = Calldata(Arc::new(calldata.to_vec()));

let deployed_contract_address = calculate_contract_address(
ContractAddressSalt(contract_address_salt),
class_hash,
&calldata,
deployer_address_for_calculation,
)
.map_err(|err| self.handle_error(remaining_gas, err.into()))?;

let ctor_context = ConstructorContext {
class_hash,
code_address: Some(deployed_contract_address),
storage_address: deployed_contract_address,
caller_address: deployer_address,
};

let call_info = execute_deployment(
self.base.state,
self.base.context,
ctor_context,
calldata,
remaining_gas,
)
.map_err(|err| self.handle_error(remaining_gas, err.into()))?;
let (deployed_contract_address, call_info) = self
.base
.deploy(
ClassHash(class_hash),
ContractAddressSalt(contract_address_salt),
Calldata(Arc::new(calldata.to_vec())),
deploy_from_zero,
remaining_gas,
)
.map_err(|err| self.handle_error(remaining_gas, err))?;

let constructor_retdata = call_info.execution.retdata.0[..].to_vec();

self.base.inner_calls.push(call_info);

Ok((Felt::from(deployed_contract_address), constructor_retdata))
Expand Down
36 changes: 5 additions & 31 deletions crates/blockifier/src/execution/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@ use cairo_vm::vm::vm_core::VirtualMachine;
use num_traits::ToPrimitive;
use starknet_api::block::{BlockHash, BlockNumber};
use starknet_api::contract_class::EntryPointType;
use starknet_api::core::{
calculate_contract_address,
ClassHash,
ContractAddress,
EntryPointSelector,
EthAddress,
};
use starknet_api::core::{ClassHash, ContractAddress, EntryPointSelector, EthAddress};
use starknet_api::state::StorageKey;
use starknet_api::transaction::fields::{Calldata, ContractAddressSalt};
use starknet_api::transaction::{EventContent, EventData, EventKey, L2ToL1Payload};
Expand All @@ -29,9 +23,8 @@ use self::hint_processor::{
};
use crate::execution::call_info::{MessageToL1, OrderedL2ToL1Message};
use crate::execution::deprecated_syscalls::DeprecatedSyscallSelector;
use crate::execution::entry_point::{CallEntryPoint, CallType, ConstructorContext};
use crate::execution::entry_point::{CallEntryPoint, CallType};
use crate::execution::execution_utils::{
execute_deployment,
felt_from_ptr,
write_felt,
write_maybe_relocatable,
Expand Down Expand Up @@ -247,32 +240,13 @@ pub fn deploy(
syscall_handler: &mut SyscallHintProcessor<'_>,
remaining_gas: &mut u64,
) -> SyscallResult<DeployResponse> {
let deployer_address = syscall_handler.storage_address();
let deployer_address_for_calculation = match request.deploy_from_zero {
true => ContractAddress::default(),
false => deployer_address,
};
let deployed_contract_address = calculate_contract_address(
request.contract_address_salt,
let (deployed_contract_address, call_info) = syscall_handler.base.deploy(
request.class_hash,
&request.constructor_calldata,
deployer_address_for_calculation,
)?;

let ctor_context = ConstructorContext {
class_hash: request.class_hash,
code_address: Some(deployed_contract_address),
storage_address: deployed_contract_address,
caller_address: deployer_address,
};
let call_info = execute_deployment(
syscall_handler.base.state,
syscall_handler.base.context,
ctor_context,
request.contract_address_salt,
request.constructor_calldata,
request.deploy_from_zero,
remaining_gas,
)?;

let constructor_retdata =
create_retdata_segment(vm, syscall_handler, &call_info.execution.retdata.0)?;
syscall_handler.base.inner_calls.push(call_info);
Expand Down
46 changes: 44 additions & 2 deletions crates/blockifier/src/execution/syscalls/syscall_base.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
use std::collections::{hash_map, HashMap, HashSet};
use std::convert::From;

use starknet_api::core::{ClassHash, ContractAddress};
use starknet_api::core::{calculate_contract_address, ClassHash, ContractAddress};
use starknet_api::state::StorageKey;
use starknet_api::transaction::fields::{Calldata, ContractAddressSalt};
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;
use crate::execution::entry_point::{CallEntryPoint, EntryPointExecutionContext};
use crate::execution::entry_point::{
CallEntryPoint,
ConstructorContext,
EntryPointExecutionContext,
};
use crate::execution::execution_utils::execute_deployment;
use crate::execution::syscalls::hint_processor::{
SyscallExecutionError,
BLOCK_NUMBER_OUT_OF_RANGE_ERROR,
Expand Down Expand Up @@ -159,6 +165,42 @@ impl<'state> SyscallHandlerBase<'state> {
Ok(())
}

pub fn deploy(
&mut self,
class_hash: ClassHash,
contract_address_salt: ContractAddressSalt,
constructor_calldata: Calldata,
deploy_from_zero: bool,
remaining_gas: &mut u64,
) -> SyscallResult<(ContractAddress, CallInfo)> {
let deployer_address = self.call.storage_address;
let deployer_address_for_calculation = match deploy_from_zero {
true => ContractAddress::default(),
false => deployer_address,
};
let deployed_contract_address = calculate_contract_address(
contract_address_salt,
class_hash,
&constructor_calldata,
deployer_address_for_calculation,
)?;

let ctor_context = ConstructorContext {
class_hash,
code_address: Some(deployed_contract_address),
storage_address: deployed_contract_address,
caller_address: deployer_address,
};
let call_info = execute_deployment(
self.state,
self.context,
ctor_context,
constructor_calldata,
remaining_gas,
)?;
Ok((deployed_contract_address, call_info))
}

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

0 comments on commit 787a25e

Please sign in to comment.