|
| 1 | +use crate::runtime_extensions::call_to_blockifier_runtime_extension::execution::entry_point::execute_call_entry_point; |
| 2 | +use crate::runtime_extensions::native::native_syscall_handler::BaseSyscallResult; |
| 3 | +use crate::state::CheatnetState; |
| 4 | +use blockifier::execution::call_info::CallInfo; |
| 5 | +use blockifier::execution::entry_point::{ |
| 6 | + CallEntryPoint, CallType, ConstructorContext, ConstructorEntryPointExecutionResult, |
| 7 | + EntryPointExecutionContext, handle_empty_constructor, |
| 8 | +}; |
| 9 | +use blockifier::execution::errors::ConstructorEntryPointExecutionError; |
| 10 | +use blockifier::execution::syscalls::syscall_base::SyscallHandlerBase; |
| 11 | +use blockifier::execution::syscalls::vm_syscall_utils::SyscallSelector; |
| 12 | +use blockifier::state::errors::StateError; |
| 13 | +use blockifier::state::state_api::State; |
| 14 | +use starknet_api::contract_class::EntryPointType; |
| 15 | +use starknet_api::core::{ClassHash, ContractAddress, calculate_contract_address}; |
| 16 | +use starknet_api::transaction::fields::{Calldata, ContractAddressSalt}; |
| 17 | + |
| 18 | +#[allow(clippy::result_large_err)] |
| 19 | +pub fn execute_constructor_entry_point( |
| 20 | + state: &mut dyn State, |
| 21 | + cheatnet_state: &mut CheatnetState, |
| 22 | + context: &mut EntryPointExecutionContext, |
| 23 | + ctor_context: ConstructorContext, |
| 24 | + calldata: Calldata, |
| 25 | + remaining_gas: &mut u64, |
| 26 | +) -> ConstructorEntryPointExecutionResult<CallInfo> { |
| 27 | + // Ensure the class is declared (by reading it). |
| 28 | + let compiled_class = state |
| 29 | + .get_compiled_class(ctor_context.class_hash) |
| 30 | + .map_err(|error| { |
| 31 | + ConstructorEntryPointExecutionError::new(error.into(), &ctor_context, None) |
| 32 | + })?; |
| 33 | + let Some(constructor_selector) = compiled_class.constructor_selector() else { |
| 34 | + // Contract has no constructor. |
| 35 | + return handle_empty_constructor( |
| 36 | + compiled_class, |
| 37 | + context, |
| 38 | + &ctor_context, |
| 39 | + calldata, |
| 40 | + *remaining_gas, |
| 41 | + ) |
| 42 | + .map_err(|error| ConstructorEntryPointExecutionError::new(error, &ctor_context, None)); |
| 43 | + }; |
| 44 | + |
| 45 | + let mut constructor_call = CallEntryPoint { |
| 46 | + class_hash: None, |
| 47 | + code_address: ctor_context.code_address, |
| 48 | + entry_point_type: EntryPointType::Constructor, |
| 49 | + entry_point_selector: constructor_selector, |
| 50 | + calldata, |
| 51 | + storage_address: ctor_context.storage_address, |
| 52 | + caller_address: ctor_context.caller_address, |
| 53 | + call_type: CallType::Call, |
| 54 | + initial_gas: *remaining_gas, |
| 55 | + }; |
| 56 | + |
| 57 | + let call_info = |
| 58 | + execute_call_entry_point(&mut constructor_call, state, cheatnet_state, context, false) |
| 59 | + .map_err(|error| { |
| 60 | + ConstructorEntryPointExecutionError::new( |
| 61 | + error, |
| 62 | + &ctor_context, |
| 63 | + Some(constructor_selector), |
| 64 | + ) |
| 65 | + })?; |
| 66 | + |
| 67 | + Ok(call_info) |
| 68 | +} |
| 69 | + |
| 70 | +fn execute_deployment( |
| 71 | + state: &mut dyn State, |
| 72 | + cheatnet_state: &mut CheatnetState, |
| 73 | + context: &mut EntryPointExecutionContext, |
| 74 | + ctor_context: ConstructorContext, |
| 75 | + constructor_calldata: Calldata, |
| 76 | + remaining_gas: &mut u64, |
| 77 | +) -> ConstructorEntryPointExecutionResult<CallInfo> { |
| 78 | + // Address allocation in the state is done before calling the constructor, so that it is |
| 79 | + // visible from it. |
| 80 | + let deployed_contract_address = ctor_context.storage_address; |
| 81 | + let current_class_hash = |
| 82 | + state |
| 83 | + .get_class_hash_at(deployed_contract_address) |
| 84 | + .map_err(|error| { |
| 85 | + ConstructorEntryPointExecutionError::new(error.into(), &ctor_context, None) |
| 86 | + })?; |
| 87 | + if current_class_hash != ClassHash::default() { |
| 88 | + return Err(ConstructorEntryPointExecutionError::new( |
| 89 | + StateError::UnavailableContractAddress(deployed_contract_address).into(), |
| 90 | + &ctor_context, |
| 91 | + None, |
| 92 | + )); |
| 93 | + } |
| 94 | + |
| 95 | + state |
| 96 | + .set_class_hash_at(deployed_contract_address, ctor_context.class_hash) |
| 97 | + .map_err(|error| { |
| 98 | + ConstructorEntryPointExecutionError::new(error.into(), &ctor_context, None) |
| 99 | + })?; |
| 100 | + |
| 101 | + execute_constructor_entry_point( |
| 102 | + state, |
| 103 | + cheatnet_state, |
| 104 | + context, |
| 105 | + ctor_context, |
| 106 | + constructor_calldata, |
| 107 | + remaining_gas, |
| 108 | + ) |
| 109 | +} |
| 110 | + |
| 111 | +pub fn deploy( |
| 112 | + syscall_handler_base: &mut SyscallHandlerBase, |
| 113 | + cheatnet_state: &mut CheatnetState, |
| 114 | + class_hash: ClassHash, |
| 115 | + contract_address_salt: ContractAddressSalt, |
| 116 | + constructor_calldata: Calldata, |
| 117 | + deploy_from_zero: bool, |
| 118 | + remaining_gas: &mut u64, |
| 119 | +) -> BaseSyscallResult<(ContractAddress, CallInfo)> { |
| 120 | + syscall_handler_base |
| 121 | + .increment_syscall_linear_factor_by(&SyscallSelector::Deploy, constructor_calldata.0.len()); |
| 122 | + // let versioned_constants = &syscall_handler_base |
| 123 | + // .context |
| 124 | + // .tx_context |
| 125 | + // .block_context |
| 126 | + // .versioned_constants; |
| 127 | + // TODO support for reject |
| 128 | + // if should_reject_deploy( |
| 129 | + // versioned_constants.disable_deploy_in_validation_mode, |
| 130 | + // syscall_handler_base.context.execution_mode, |
| 131 | + // ) { |
| 132 | + // syscall_handler_base.reject_syscall_in_validate_mode("deploy")?; |
| 133 | + // } |
| 134 | + |
| 135 | + let deployer_address = syscall_handler_base.call.storage_address; |
| 136 | + let deployer_address_for_calculation = match deploy_from_zero { |
| 137 | + true => ContractAddress::default(), |
| 138 | + false => deployer_address, |
| 139 | + }; |
| 140 | + let deployed_contract_address = calculate_contract_address( |
| 141 | + contract_address_salt, |
| 142 | + class_hash, |
| 143 | + &constructor_calldata, |
| 144 | + deployer_address_for_calculation, |
| 145 | + )?; |
| 146 | + |
| 147 | + let ctor_context = ConstructorContext { |
| 148 | + class_hash, |
| 149 | + code_address: Some(deployed_contract_address), |
| 150 | + storage_address: deployed_contract_address, |
| 151 | + caller_address: deployer_address, |
| 152 | + }; |
| 153 | + let call_info = execute_deployment( |
| 154 | + syscall_handler_base.state, |
| 155 | + cheatnet_state, |
| 156 | + syscall_handler_base.context, |
| 157 | + ctor_context, |
| 158 | + constructor_calldata, |
| 159 | + remaining_gas, |
| 160 | + )?; |
| 161 | + Ok((deployed_contract_address, call_info)) |
| 162 | +} |
0 commit comments