Skip to content

Commit 7c9dec5

Browse files
cptarturksew1
authored andcommitted
Move code to dedicated modules
1 parent c3e6519 commit 7c9dec5

File tree

4 files changed

+235
-220
lines changed

4 files changed

+235
-220
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use blockifier::execution::call_info::CallInfo;
2+
use blockifier::execution::entry_point::CallEntryPoint;
3+
use blockifier::execution::execution_utils::update_remaining_gas;
4+
use blockifier::execution::syscalls::hint_processor::{SyscallExecutionError, ENTRYPOINT_FAILED_ERROR};
5+
use blockifier::execution::syscalls::syscall_base::SyscallHandlerBase;
6+
use starknet_types_core::felt::Felt;
7+
use crate::runtime_extensions::call_to_blockifier_runtime_extension::execution::entry_point::execute_call_entry_point;
8+
use crate::runtime_extensions::native::native_syscall_handler::BaseSyscallResult;
9+
use crate::state::CheatnetState;
10+
11+
#[allow(clippy::result_large_err)]
12+
pub fn execute_inner_call(
13+
syscall_handler_base: &mut SyscallHandlerBase,
14+
cheatnet_state: &mut CheatnetState,
15+
call: &mut CallEntryPoint,
16+
remaining_gas: &mut u64,
17+
) -> BaseSyscallResult<Vec<Felt>> {
18+
let revert_idx = syscall_handler_base.context.revert_infos.0.len();
19+
20+
// region: Modified blockifier code
21+
let call_info = execute_call_entry_point(
22+
call,
23+
syscall_handler_base.state,
24+
cheatnet_state,
25+
syscall_handler_base.context,
26+
true,
27+
)?;
28+
// TODO not sure if to keep it
29+
update_remaining_gas(remaining_gas, &call_info);
30+
// endregion
31+
32+
let mut raw_retdata = call_info.execution.retdata.0.clone();
33+
let failed = call_info.execution.failed;
34+
syscall_handler_base.inner_calls.push(call_info);
35+
if failed {
36+
syscall_handler_base
37+
.context
38+
.revert(revert_idx, syscall_handler_base.state)?;
39+
40+
// Delete events and l2_to_l1_messages from the reverted call.
41+
let reverted_call = &mut syscall_handler_base.inner_calls.last_mut().unwrap();
42+
let mut stack: Vec<&mut CallInfo> = vec![reverted_call];
43+
while let Some(call_info) = stack.pop() {
44+
call_info.execution.events.clear();
45+
call_info.execution.l2_to_l1_messages.clear();
46+
// Add inner calls that did not fail to the stack.
47+
// The events and l2_to_l1_messages of the failed calls were already cleared.
48+
stack.extend(
49+
call_info
50+
.inner_calls
51+
.iter_mut()
52+
.filter(|call_info| !call_info.execution.failed),
53+
);
54+
}
55+
56+
raw_retdata
57+
.push(Felt::from_hex(ENTRYPOINT_FAILED_ERROR).map_err(SyscallExecutionError::from)?);
58+
return Err(SyscallExecutionError::Revert {
59+
error_data: raw_retdata,
60+
});
61+
}
62+
63+
Ok(raw_retdata)
64+
}
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
pub mod execution;
22
pub mod native_syscall_handler;
3+
mod deploy;
4+
mod call;

0 commit comments

Comments
 (0)