Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,3 @@ pub fn deploy_at(
}
}
}

pub fn deploy(
syscall_handler: &mut SyscallHintProcessor,
cheatnet_state: &mut CheatnetState,
class_hash: &ClassHash,
calldata: &[Felt],
) -> Result<(ContractAddress, Vec<Felt>), CheatcodeError> {
let contract_address = cheatnet_state.precalculate_address(class_hash, calldata);

deploy_at(
syscall_handler,
cheatnet_state,
class_hash,
calldata,
contract_address,
)
}
106 changes: 1 addition & 105 deletions crates/cheatnet/tests/cheatcodes/deploy.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::common::assertions::{ClassHashAssert, assert_success};
use crate::common::state::create_cached_state;
use crate::common::{
call_contract, deploy_at_wrapper, deploy_contract, deploy_wrapper, get_contracts,
};
use crate::common::{call_contract, deploy_at_wrapper, deploy_contract, get_contracts};
use cairo_vm::vm::errors::hint_errors::HintError;
use cheatnet::runtime_extensions::call_to_blockifier_runtime_extension::rpc::{
CallFailure, CallResult,
Expand All @@ -12,7 +10,6 @@ use cheatnet::runtime_extensions::forge_runtime_extension::cheatcodes::declare::
use cheatnet::runtime_extensions::forge_runtime_extension::cheatcodes::storage::selector_from_name;
use cheatnet::state::CheatnetState;
use conversions::IntoConv;
use conversions::felt::FromShortString;
use runtime::EnhancedHintError;
use starknet_api::core::ContractAddress;
use starknet_types_core::felt::Felt;
Expand Down Expand Up @@ -210,107 +207,6 @@ fn try_to_deploy_at_0() {
});
}

#[test]
fn deploy_calldata_no_constructor() {
let mut cached_state = create_cached_state();
let mut cheatnet_state = CheatnetState::default();

let contracts_data = get_contracts();

let class_hash = declare(&mut cached_state, "HelloStarknet", &contracts_data)
.unwrap()
.unwrap_success();

let output = deploy_wrapper(
&mut cached_state,
&mut cheatnet_state,
&class_hash,
&[Felt::from(123_321)],
);

assert!(match output {
Err(CheatcodeError::Unrecoverable(EnhancedHintError::Hint(HintError::CustomHint(msg)))) =>
msg.as_ref()
.contains("Cannot pass calldata to a contract with no constructor"),
_ => false,
});
}

#[test]
fn deploy_missing_arguments_in_constructor() {
let mut cached_state = create_cached_state();
let mut cheatnet_state = CheatnetState::default();

let contracts_data = get_contracts();

let class_hash = declare(&mut cached_state, "ConstructorSimple2", &contracts_data)
.unwrap()
.unwrap_success();

let output = deploy_wrapper(
&mut cached_state,
&mut cheatnet_state,
&class_hash,
&[Felt::from(123_321)],
);

assert!(match output {
Err(CheatcodeError::Unrecoverable(EnhancedHintError::Hint(HintError::CustomHint(msg)))) =>
msg.as_ref()
== "\n 0x4661696c656420746f20646573657269616c697a6520706172616d202332 ('Failed to deserialize param #2')\n",
_ => false,
});
}

#[test]
fn deploy_too_many_arguments_in_constructor() {
let mut cached_state = create_cached_state();
let mut cheatnet_state = CheatnetState::default();

let contracts_data = get_contracts();

let class_hash = declare(&mut cached_state, "ConstructorSimple", &contracts_data)
.unwrap()
.unwrap_success();

let output = deploy_wrapper(
&mut cached_state,
&mut cheatnet_state,
&class_hash,
&[Felt::from(123_321), Felt::from(523_325)],
);

assert!(match output {
Err(CheatcodeError::Unrecoverable(EnhancedHintError::Hint(HintError::CustomHint(msg)))) =>
msg.as_ref()
== "\n 0x496e70757420746f6f206c6f6e6720666f7220617267756d656e7473 ('Input too long for arguments')\n",
_ => false,
});
}

#[test]
fn deploy_invalid_class_hash() {
let mut cached_state = create_cached_state();
let mut cheatnet_state = CheatnetState::default();

let class_hash = Felt::from_short_string("Invalid ClassHash")
.unwrap()
.into_();

let output = deploy_wrapper(
&mut cached_state,
&mut cheatnet_state,
&class_hash,
&[Felt::from(123_321), Felt::from(523_325)],
);

assert!(matches!(
output,
Err(CheatcodeError::Unrecoverable(EnhancedHintError::Hint(HintError::CustomHint(msg))))
if msg.as_ref().contains(class_hash.to_hex_string().trim_start_matches("0x")),
));
}

#[test]
fn deploy_invokes_constructor() {
let mut cached_state = create_cached_state();
Expand Down
8 changes: 3 additions & 5 deletions crates/cheatnet/tests/cheatcodes/get_class_hash.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::common::assertions::ClassHashAssert;
use crate::common::get_contracts;
use crate::common::state::create_cached_state;
use crate::common::{call_contract, deploy_wrapper};
use crate::common::{call_contract, deploy};
use cheatnet::runtime_extensions::forge_runtime_extension::cheatcodes::declare::declare;
use cheatnet::runtime_extensions::forge_runtime_extension::cheatcodes::get_class_hash::get_class_hash;
use cheatnet::runtime_extensions::forge_runtime_extension::cheatcodes::storage::selector_from_name;
Expand All @@ -17,8 +17,7 @@ fn get_class_hash_simple() {
let class_hash = declare(&mut cached_state, "HelloStarknet", &contracts_data)
.unwrap()
.unwrap_success();
let contract_address =
deploy_wrapper(&mut cached_state, &mut cheatnet_state, &class_hash, &[]).unwrap();
let contract_address = deploy(&mut cached_state, &mut cheatnet_state, &class_hash, &[]);

assert_eq!(
class_hash,
Expand All @@ -35,8 +34,7 @@ fn get_class_hash_upgrade() {
let class_hash = declare(&mut cached_state, "GetClassHashCheckerUpg", &contracts_data)
.unwrap()
.unwrap_success();
let contract_address =
deploy_wrapper(&mut cached_state, &mut cheatnet_state, &class_hash, &[]).unwrap();
let contract_address = deploy(&mut cached_state, &mut cheatnet_state, &class_hash, &[]);

assert_eq!(
class_hash,
Expand Down
20 changes: 8 additions & 12 deletions crates/cheatnet/tests/cheatcodes/mock_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::test_environment::TestEnvironment;
use crate::common::assertions::ClassHashAssert;
use crate::common::recover_data;
use crate::common::state::create_cached_state;
use crate::common::{call_contract, deploy_wrapper};
use crate::common::{call_contract, deploy};
use crate::{
common::assertions::assert_success,
common::{deploy_contract, get_contracts},
Expand Down Expand Up @@ -366,13 +366,12 @@ fn mock_call_library_call_no_effect() {
.unwrap()
.unwrap_success();

let contract_address = deploy_wrapper(
let contract_address = deploy(
&mut cached_state,
&mut cheatnet_state,
&class_hash,
&[Felt::from(420)],
)
.unwrap();
);

let lib_call_address = deploy_contract(
&mut cached_state,
Expand Down Expand Up @@ -421,13 +420,12 @@ fn mock_call_before_deployment() {
&ret_data,
);

let contract_address = deploy_wrapper(
let contract_address = deploy(
&mut cached_state,
&mut cheatnet_state,
&class_hash,
&[Felt::from(420)],
)
.unwrap();
);

assert_eq!(precalculated_address, contract_address);

Expand Down Expand Up @@ -484,8 +482,7 @@ fn mock_call_in_constructor() {
let class_hash = declare(&mut cached_state, "HelloStarknet", &contracts_data)
.unwrap()
.unwrap_success();
let balance_contract_address =
deploy_wrapper(&mut cached_state, &mut cheatnet_state, &class_hash, &[]).unwrap();
let balance_contract_address = deploy(&mut cached_state, &mut cheatnet_state, &class_hash, &[]);
let ret_data = [Felt::from(223)];
cheatnet_state.start_mock_call(
balance_contract_address,
Expand All @@ -496,13 +493,12 @@ fn mock_call_in_constructor() {
let class_hash = declare(&mut cached_state, "ConstructorMockChecker", &contracts_data)
.unwrap()
.unwrap_success();
let contract_address = deploy_wrapper(
let contract_address = deploy(
&mut cached_state,
&mut cheatnet_state,
&class_hash,
&[balance_contract_address.into_()],
)
.unwrap();
);

let selector = selector_from_name("get_constructor_balance");

Expand Down
5 changes: 2 additions & 3 deletions crates/cheatnet/tests/cheatcodes/test_environment.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::common::assertions::ClassHashAssert;
use crate::common::{call_contract, deploy_wrapper};
use crate::common::{call_contract, deploy};
use crate::common::{deploy_contract, state::create_cached_state};
use blockifier::state::cached_state::CachedState;
use cheatnet::runtime_extensions::call_to_blockifier_runtime_extension::rpc::CallResult;
Expand Down Expand Up @@ -42,13 +42,12 @@ impl TestEnvironment {
}

pub fn deploy_wrapper(&mut self, class_hash: &ClassHash, calldata: &[Felt]) -> ContractAddress {
deploy_wrapper(
deploy(
&mut self.cached_state,
&mut self.cheatnet_state,
class_hash,
calldata,
)
.unwrap()
}

pub fn call_contract(
Expand Down
62 changes: 50 additions & 12 deletions crates/cheatnet/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ use assertions::ClassHashAssert;
use blockifier::execution::call_info::CallInfo;
use blockifier::execution::contract_class::TrackedResource;
use blockifier::execution::entry_point::{
CallEntryPoint, CallType, EntryPointExecutionContext, EntryPointExecutionResult,
CallEntryPoint, CallType, ConstructorContext, EntryPointExecutionContext,
EntryPointExecutionResult,
};
use blockifier::execution::execution_utils::ReadOnlySegments;
use blockifier::execution::syscalls::hint_processor::SyscallHintProcessor;
use blockifier::state::state_api::State;
use cairo_lang_casm::hints::Hint;
use cairo_vm::types::relocatable::Relocatable;
use cheatnet::runtime_extensions::call_to_blockifier_runtime_extension::execution::cheated_syscalls;
use cheatnet::runtime_extensions::call_to_blockifier_runtime_extension::execution::entry_point::execute_call_entry_point;
use cheatnet::runtime_extensions::call_to_blockifier_runtime_extension::rpc::{
AddressOrClassHash, call_entry_point,
Expand All @@ -19,9 +21,7 @@ use cheatnet::runtime_extensions::call_to_blockifier_runtime_extension::rpc::{
use cheatnet::runtime_extensions::common::create_execute_calldata;
use cheatnet::runtime_extensions::forge_runtime_extension::cheatcodes::CheatcodeError;
use cheatnet::runtime_extensions::forge_runtime_extension::cheatcodes::declare::declare;
use cheatnet::runtime_extensions::forge_runtime_extension::cheatcodes::deploy::{
deploy, deploy_at,
};
use cheatnet::runtime_extensions::forge_runtime_extension::cheatcodes::deploy::deploy_at;
use cheatnet::runtime_extensions::forge_runtime_extension::contracts_data::ContractsData;
use cheatnet::state::CheatnetState;
use conversions::IntoConv;
Expand All @@ -36,8 +36,10 @@ use scarb_api::{
use starknet::core::utils::get_selector_from_name;
use starknet_api::contract_class::EntryPointType;
use starknet_api::core::{ClassHash, ContractAddress, EntryPointSelector};
use starknet_api::transaction::fields::Calldata;
use starknet_types_core::felt::Felt;
use std::collections::HashMap;
use std::sync::Arc;

pub mod assertions;
pub mod cache;
Expand Down Expand Up @@ -116,23 +118,23 @@ pub fn deploy_contract(
&hints,
);

let (contract_address, _retdata) = deploy(
let (contract_address, _) = deploy_helper(
&mut syscall_hint_processor,
cheatnet_state,
&class_hash,
None,
calldata,
)
.unwrap();
);

contract_address
}

pub fn deploy_wrapper(
pub fn deploy(
state: &mut dyn State,
cheatnet_state: &mut CheatnetState,
class_hash: &ClassHash,
calldata: &[Felt],
) -> Result<ContractAddress, CheatcodeError> {
) -> ContractAddress {
let mut entry_point_execution_context = build_context(
&cheatnet_state.block_info,
None,
Expand All @@ -147,14 +149,15 @@ pub fn deploy_wrapper(
&hints,
);

let (contract_address, _retdata) = deploy(
let (contract_address, _) = deploy_helper(
&mut syscall_hint_processor,
cheatnet_state,
class_hash,
None,
calldata,
)?;
);

Ok(contract_address)
contract_address
}

pub fn deploy_at_wrapper(
Expand Down Expand Up @@ -189,6 +192,41 @@ pub fn deploy_at_wrapper(
Ok(contract_address)
}

fn deploy_helper(
syscall_handler: &mut SyscallHintProcessor,
cheatnet_state: &mut CheatnetState,
class_hash: &ClassHash,
contract_address: Option<ContractAddress>,
calldata: &[Felt],
) -> (ContractAddress, Vec<cairo_vm::Felt252>) {
let contract_address = contract_address
.unwrap_or_else(|| cheatnet_state.precalculate_address(class_hash, calldata));
let calldata = Calldata(Arc::new(calldata.to_vec()));

let ctor_context = ConstructorContext {
class_hash: *class_hash,
code_address: Some(contract_address),
storage_address: contract_address,
caller_address: TryFromHexStr::try_from_hex_str(TEST_ADDRESS).unwrap(),
};

let call_info = cheated_syscalls::execute_deployment(
syscall_handler.base.state,
cheatnet_state,
syscall_handler.base.context,
&ctor_context,
calldata,
i64::MAX as u64,
)
.unwrap();
cheatnet_state.increment_deploy_salt_base();

let retdata = call_info.execution.retdata.0.clone();
syscall_handler.base.inner_calls.push(call_info);

(contract_address, retdata)
}

// This does contract call without the transaction layer. This way `call_contract` can return data and modify state.
// `call` and `invoke` on the transactional layer use such method under the hood.
pub fn call_contract(
Expand Down
Loading
Loading