Skip to content

Commit f83207c

Browse files
committed
Remove deploy cheatcode tests
Towards #3659 commit-id:a6d72598
1 parent 3586a93 commit f83207c

File tree

8 files changed

+67
-181
lines changed

8 files changed

+67
-181
lines changed

crates/cheatnet/src/runtime_extensions/forge_runtime_extension/cheatcodes/deploy.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,3 @@ pub fn deploy_at(
7171
}
7272
}
7373
}
74-
75-
pub fn deploy(
76-
syscall_handler: &mut SyscallHintProcessor,
77-
cheatnet_state: &mut CheatnetState,
78-
class_hash: &ClassHash,
79-
calldata: &[Felt],
80-
) -> Result<(ContractAddress, Vec<Felt>), CheatcodeError> {
81-
let contract_address = cheatnet_state.precalculate_address(class_hash, calldata);
82-
83-
deploy_at(
84-
syscall_handler,
85-
cheatnet_state,
86-
class_hash,
87-
calldata,
88-
contract_address,
89-
)
90-
}

crates/cheatnet/tests/cheatcodes/deploy.rs

Lines changed: 1 addition & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use crate::common::assertions::{ClassHashAssert, assert_success};
22
use crate::common::state::create_cached_state;
3-
use crate::common::{
4-
call_contract, deploy_at_wrapper, deploy_contract, deploy_wrapper, get_contracts,
5-
};
3+
use crate::common::{call_contract, deploy_at_wrapper, deploy_contract, get_contracts};
64
use cairo_vm::vm::errors::hint_errors::HintError;
75
use cheatnet::runtime_extensions::call_to_blockifier_runtime_extension::rpc::{
86
CallFailure, CallResult,
@@ -12,7 +10,6 @@ use cheatnet::runtime_extensions::forge_runtime_extension::cheatcodes::declare::
1210
use cheatnet::runtime_extensions::forge_runtime_extension::cheatcodes::storage::selector_from_name;
1311
use cheatnet::state::CheatnetState;
1412
use conversions::IntoConv;
15-
use conversions::felt::FromShortString;
1613
use runtime::EnhancedHintError;
1714
use starknet_api::core::ContractAddress;
1815
use starknet_types_core::felt::Felt;
@@ -210,107 +207,6 @@ fn try_to_deploy_at_0() {
210207
});
211208
}
212209

213-
#[test]
214-
fn deploy_calldata_no_constructor() {
215-
let mut cached_state = create_cached_state();
216-
let mut cheatnet_state = CheatnetState::default();
217-
218-
let contracts_data = get_contracts();
219-
220-
let class_hash = declare(&mut cached_state, "HelloStarknet", &contracts_data)
221-
.unwrap()
222-
.unwrap_success();
223-
224-
let output = deploy_wrapper(
225-
&mut cached_state,
226-
&mut cheatnet_state,
227-
&class_hash,
228-
&[Felt::from(123_321)],
229-
);
230-
231-
assert!(match output {
232-
Err(CheatcodeError::Unrecoverable(EnhancedHintError::Hint(HintError::CustomHint(msg)))) =>
233-
msg.as_ref()
234-
.contains("Cannot pass calldata to a contract with no constructor"),
235-
_ => false,
236-
});
237-
}
238-
239-
#[test]
240-
fn deploy_missing_arguments_in_constructor() {
241-
let mut cached_state = create_cached_state();
242-
let mut cheatnet_state = CheatnetState::default();
243-
244-
let contracts_data = get_contracts();
245-
246-
let class_hash = declare(&mut cached_state, "ConstructorSimple2", &contracts_data)
247-
.unwrap()
248-
.unwrap_success();
249-
250-
let output = deploy_wrapper(
251-
&mut cached_state,
252-
&mut cheatnet_state,
253-
&class_hash,
254-
&[Felt::from(123_321)],
255-
);
256-
257-
assert!(match output {
258-
Err(CheatcodeError::Unrecoverable(EnhancedHintError::Hint(HintError::CustomHint(msg)))) =>
259-
msg.as_ref()
260-
== "\n 0x4661696c656420746f20646573657269616c697a6520706172616d202332 ('Failed to deserialize param #2')\n",
261-
_ => false,
262-
});
263-
}
264-
265-
#[test]
266-
fn deploy_too_many_arguments_in_constructor() {
267-
let mut cached_state = create_cached_state();
268-
let mut cheatnet_state = CheatnetState::default();
269-
270-
let contracts_data = get_contracts();
271-
272-
let class_hash = declare(&mut cached_state, "ConstructorSimple", &contracts_data)
273-
.unwrap()
274-
.unwrap_success();
275-
276-
let output = deploy_wrapper(
277-
&mut cached_state,
278-
&mut cheatnet_state,
279-
&class_hash,
280-
&[Felt::from(123_321), Felt::from(523_325)],
281-
);
282-
283-
assert!(match output {
284-
Err(CheatcodeError::Unrecoverable(EnhancedHintError::Hint(HintError::CustomHint(msg)))) =>
285-
msg.as_ref()
286-
== "\n 0x496e70757420746f6f206c6f6e6720666f7220617267756d656e7473 ('Input too long for arguments')\n",
287-
_ => false,
288-
});
289-
}
290-
291-
#[test]
292-
fn deploy_invalid_class_hash() {
293-
let mut cached_state = create_cached_state();
294-
let mut cheatnet_state = CheatnetState::default();
295-
296-
let class_hash = Felt::from_short_string("Invalid ClassHash")
297-
.unwrap()
298-
.into_();
299-
300-
let output = deploy_wrapper(
301-
&mut cached_state,
302-
&mut cheatnet_state,
303-
&class_hash,
304-
&[Felt::from(123_321), Felt::from(523_325)],
305-
);
306-
307-
assert!(matches!(
308-
output,
309-
Err(CheatcodeError::Unrecoverable(EnhancedHintError::Hint(HintError::CustomHint(msg))))
310-
if msg.as_ref().contains(class_hash.to_hex_string().trim_start_matches("0x")),
311-
));
312-
}
313-
314210
#[test]
315211
fn deploy_invokes_constructor() {
316212
let mut cached_state = create_cached_state();

crates/cheatnet/tests/cheatcodes/get_class_hash.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::common::assertions::ClassHashAssert;
22
use crate::common::get_contracts;
33
use crate::common::state::create_cached_state;
4-
use crate::common::{call_contract, deploy_wrapper};
4+
use crate::common::{call_contract, deploy};
55
use cheatnet::runtime_extensions::forge_runtime_extension::cheatcodes::declare::declare;
66
use cheatnet::runtime_extensions::forge_runtime_extension::cheatcodes::get_class_hash::get_class_hash;
77
use cheatnet::runtime_extensions::forge_runtime_extension::cheatcodes::storage::selector_from_name;
@@ -17,8 +17,7 @@ fn get_class_hash_simple() {
1717
let class_hash = declare(&mut cached_state, "HelloStarknet", &contracts_data)
1818
.unwrap()
1919
.unwrap_success();
20-
let contract_address =
21-
deploy_wrapper(&mut cached_state, &mut cheatnet_state, &class_hash, &[]).unwrap();
20+
let contract_address = deploy(&mut cached_state, &mut cheatnet_state, &class_hash, &[]);
2221

2322
assert_eq!(
2423
class_hash,
@@ -35,8 +34,7 @@ fn get_class_hash_upgrade() {
3534
let class_hash = declare(&mut cached_state, "GetClassHashCheckerUpg", &contracts_data)
3635
.unwrap()
3736
.unwrap_success();
38-
let contract_address =
39-
deploy_wrapper(&mut cached_state, &mut cheatnet_state, &class_hash, &[]).unwrap();
37+
let contract_address = deploy(&mut cached_state, &mut cheatnet_state, &class_hash, &[]);
4038

4139
assert_eq!(
4240
class_hash,

crates/cheatnet/tests/cheatcodes/mock_call.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::test_environment::TestEnvironment;
22
use crate::common::assertions::ClassHashAssert;
33
use crate::common::recover_data;
44
use crate::common::state::create_cached_state;
5-
use crate::common::{call_contract, deploy_wrapper};
5+
use crate::common::{call_contract, deploy};
66
use crate::{
77
common::assertions::assert_success,
88
common::{deploy_contract, get_contracts},
@@ -366,13 +366,12 @@ fn mock_call_library_call_no_effect() {
366366
.unwrap()
367367
.unwrap_success();
368368

369-
let contract_address = deploy_wrapper(
369+
let contract_address = deploy(
370370
&mut cached_state,
371371
&mut cheatnet_state,
372372
&class_hash,
373373
&[Felt::from(420)],
374-
)
375-
.unwrap();
374+
);
376375

377376
let lib_call_address = deploy_contract(
378377
&mut cached_state,
@@ -421,13 +420,12 @@ fn mock_call_before_deployment() {
421420
&ret_data,
422421
);
423422

424-
let contract_address = deploy_wrapper(
423+
let contract_address = deploy(
425424
&mut cached_state,
426425
&mut cheatnet_state,
427426
&class_hash,
428427
&[Felt::from(420)],
429-
)
430-
.unwrap();
428+
);
431429

432430
assert_eq!(precalculated_address, contract_address);
433431

@@ -484,8 +482,7 @@ fn mock_call_in_constructor() {
484482
let class_hash = declare(&mut cached_state, "HelloStarknet", &contracts_data)
485483
.unwrap()
486484
.unwrap_success();
487-
let balance_contract_address =
488-
deploy_wrapper(&mut cached_state, &mut cheatnet_state, &class_hash, &[]).unwrap();
485+
let balance_contract_address = deploy(&mut cached_state, &mut cheatnet_state, &class_hash, &[]);
489486
let ret_data = [Felt::from(223)];
490487
cheatnet_state.start_mock_call(
491488
balance_contract_address,
@@ -496,13 +493,12 @@ fn mock_call_in_constructor() {
496493
let class_hash = declare(&mut cached_state, "ConstructorMockChecker", &contracts_data)
497494
.unwrap()
498495
.unwrap_success();
499-
let contract_address = deploy_wrapper(
496+
let contract_address = deploy(
500497
&mut cached_state,
501498
&mut cheatnet_state,
502499
&class_hash,
503500
&[balance_contract_address.into_()],
504-
)
505-
.unwrap();
501+
);
506502

507503
let selector = selector_from_name("get_constructor_balance");
508504

crates/cheatnet/tests/cheatcodes/test_environment.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::common::assertions::ClassHashAssert;
2-
use crate::common::{call_contract, deploy_wrapper};
2+
use crate::common::{call_contract, deploy};
33
use crate::common::{deploy_contract, state::create_cached_state};
44
use blockifier::state::cached_state::CachedState;
55
use cheatnet::runtime_extensions::call_to_blockifier_runtime_extension::rpc::CallResult;
@@ -42,13 +42,12 @@ impl TestEnvironment {
4242
}
4343

4444
pub fn deploy_wrapper(&mut self, class_hash: &ClassHash, calldata: &[Felt]) -> ContractAddress {
45-
deploy_wrapper(
45+
deploy(
4646
&mut self.cached_state,
4747
&mut self.cheatnet_state,
4848
class_hash,
4949
calldata,
5050
)
51-
.unwrap()
5251
}
5352

5453
pub fn call_contract(

crates/cheatnet/tests/common/mod.rs

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ use assertions::ClassHashAssert;
22
use blockifier::execution::call_info::CallInfo;
33
use blockifier::execution::contract_class::TrackedResource;
44
use blockifier::execution::entry_point::{
5-
CallEntryPoint, CallType, EntryPointExecutionContext, EntryPointExecutionResult,
5+
CallEntryPoint, CallType, ConstructorContext, EntryPointExecutionContext,
6+
EntryPointExecutionResult,
67
};
78
use blockifier::execution::execution_utils::ReadOnlySegments;
89
use blockifier::execution::syscalls::hint_processor::SyscallHintProcessor;
910
use blockifier::state::state_api::State;
1011
use cairo_lang_casm::hints::Hint;
1112
use cairo_vm::types::relocatable::Relocatable;
13+
use cheatnet::runtime_extensions::call_to_blockifier_runtime_extension::execution::cheated_syscalls;
1214
use cheatnet::runtime_extensions::call_to_blockifier_runtime_extension::execution::entry_point::execute_call_entry_point;
1315
use cheatnet::runtime_extensions::call_to_blockifier_runtime_extension::rpc::{
1416
AddressOrClassHash, call_entry_point,
@@ -19,9 +21,7 @@ use cheatnet::runtime_extensions::call_to_blockifier_runtime_extension::rpc::{
1921
use cheatnet::runtime_extensions::common::create_execute_calldata;
2022
use cheatnet::runtime_extensions::forge_runtime_extension::cheatcodes::CheatcodeError;
2123
use cheatnet::runtime_extensions::forge_runtime_extension::cheatcodes::declare::declare;
22-
use cheatnet::runtime_extensions::forge_runtime_extension::cheatcodes::deploy::{
23-
deploy, deploy_at,
24-
};
24+
use cheatnet::runtime_extensions::forge_runtime_extension::cheatcodes::deploy::deploy_at;
2525
use cheatnet::runtime_extensions::forge_runtime_extension::contracts_data::ContractsData;
2626
use cheatnet::state::CheatnetState;
2727
use conversions::IntoConv;
@@ -36,8 +36,10 @@ use scarb_api::{
3636
use starknet::core::utils::get_selector_from_name;
3737
use starknet_api::contract_class::EntryPointType;
3838
use starknet_api::core::{ClassHash, ContractAddress, EntryPointSelector};
39+
use starknet_api::transaction::fields::Calldata;
3940
use starknet_types_core::felt::Felt;
4041
use std::collections::HashMap;
42+
use std::sync::Arc;
4143

4244
pub mod assertions;
4345
pub mod cache;
@@ -116,23 +118,23 @@ pub fn deploy_contract(
116118
&hints,
117119
);
118120

119-
let (contract_address, _retdata) = deploy(
121+
let (contract_address, _) = deploy_helper(
120122
&mut syscall_hint_processor,
121123
cheatnet_state,
122124
&class_hash,
125+
None,
123126
calldata,
124-
)
125-
.unwrap();
127+
);
126128

127129
contract_address
128130
}
129131

130-
pub fn deploy_wrapper(
132+
pub fn deploy(
131133
state: &mut dyn State,
132134
cheatnet_state: &mut CheatnetState,
133135
class_hash: &ClassHash,
134136
calldata: &[Felt],
135-
) -> Result<ContractAddress, CheatcodeError> {
137+
) -> ContractAddress {
136138
let mut entry_point_execution_context = build_context(
137139
&cheatnet_state.block_info,
138140
None,
@@ -147,14 +149,15 @@ pub fn deploy_wrapper(
147149
&hints,
148150
);
149151

150-
let (contract_address, _retdata) = deploy(
152+
let (contract_address, _) = deploy_helper(
151153
&mut syscall_hint_processor,
152154
cheatnet_state,
153155
class_hash,
156+
None,
154157
calldata,
155-
)?;
158+
);
156159

157-
Ok(contract_address)
160+
contract_address
158161
}
159162

160163
pub fn deploy_at_wrapper(
@@ -189,6 +192,41 @@ pub fn deploy_at_wrapper(
189192
Ok(contract_address)
190193
}
191194

195+
fn deploy_helper(
196+
syscall_handler: &mut SyscallHintProcessor,
197+
cheatnet_state: &mut CheatnetState,
198+
class_hash: &ClassHash,
199+
contract_address: Option<ContractAddress>,
200+
calldata: &[Felt],
201+
) -> (ContractAddress, Vec<cairo_vm::Felt252>) {
202+
let contract_address = contract_address
203+
.unwrap_or_else(|| cheatnet_state.precalculate_address(class_hash, calldata));
204+
let calldata = Calldata(Arc::new(calldata.to_vec()));
205+
206+
let ctor_context = ConstructorContext {
207+
class_hash: *class_hash,
208+
code_address: Some(contract_address),
209+
storage_address: contract_address,
210+
caller_address: TryFromHexStr::try_from_hex_str(TEST_ADDRESS).unwrap(),
211+
};
212+
213+
let call_info = cheated_syscalls::execute_deployment(
214+
syscall_handler.base.state,
215+
cheatnet_state,
216+
syscall_handler.base.context,
217+
&ctor_context,
218+
calldata,
219+
i64::MAX as u64,
220+
)
221+
.unwrap();
222+
cheatnet_state.increment_deploy_salt_base();
223+
224+
let retdata = call_info.execution.retdata.0.clone();
225+
syscall_handler.base.inner_calls.push(call_info);
226+
227+
(contract_address, retdata)
228+
}
229+
192230
// This does contract call without the transaction layer. This way `call_contract` can return data and modify state.
193231
// `call` and `invoke` on the transactional layer use such method under the hood.
194232
pub fn call_contract(

0 commit comments

Comments
 (0)