Skip to content

Commit

Permalink
chore(blockifier): add a check on deploy syscall gas cost in the happ…
Browse files Browse the repository at this point in the history
…y flows of deploy tests
  • Loading branch information
meship-starkware committed Aug 19, 2024
1 parent 3294ee4 commit 2cb57a7
Showing 1 changed file with 37 additions and 11 deletions.
48 changes: 37 additions & 11 deletions crates/blockifier/src/execution/syscalls/syscall_tests/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::initial_test_state::test_state;
use crate::test_utils::{calldata_for_deploy_test, trivial_external_entry_point_new, CairoVersion};

#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1);"VM")]
fn no_constructor(deployer_contract: FeatureContract) {
#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1), 204260;"VM")]
fn no_constructor(deployer_contract: FeatureContract, expected_gas: u64) {
// TODO(Yoni): share the init code of the tests in this file.

let empty_contract = FeatureContract::Empty(CairoVersion::Cairo1);
Expand All @@ -33,6 +33,17 @@ fn no_constructor(deployer_contract: FeatureContract) {
calldata,
..trivial_external_entry_point_new(deployer_contract)
};

let deploy_call = &entry_point_call.execute_directly(&mut state).unwrap();
assert_eq!(
deploy_call.execution,
CallExecution {
retdata: retdata![],
gas_consumed: expected_gas,
..CallExecution::default()
}
);

let deployed_contract_address = calculate_contract_address(
ContractAddressSalt::default(),
class_hash,
Expand All @@ -41,11 +52,13 @@ fn no_constructor(deployer_contract: FeatureContract) {
)
.unwrap();

let deploy_call = &entry_point_call.execute_directly(&mut state).unwrap().inner_calls[0];
// Note, this is the CallInfo of the constructor call. Meaning the CallExecution is the
// execution of the constructor and not the deploy syscall.
let deploy_constructor_call = &deploy_call.inner_calls[0];

assert_eq!(deploy_call.call.storage_address, deployed_contract_address);
assert_eq!(deploy_constructor_call.call.storage_address, deployed_contract_address);
assert_eq!(
deploy_call.execution,
deploy_constructor_call.execution,
CallExecution { retdata: retdata![], gas_consumed: 0, ..CallExecution::default() }
);
assert_eq!(state.get_class_hash_at(deployed_contract_address).unwrap(), class_hash);
Expand Down Expand Up @@ -77,8 +90,12 @@ fn no_constructor_nonempty_calldata(deployer_contract: FeatureContract) {
));
}

#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1), 5210;"VM")]
fn with_constructor(deployer_contract: FeatureContract, expected_gas: u64) {
#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1),214210, 5210;"VM")]
fn with_constructor(
deployer_contract: FeatureContract,
expected_gas: u64,
expected_constructor_gas: u64,
) {
let empty_contract = FeatureContract::Empty(CairoVersion::Cairo1);
let mut state = test_state(
&ChainInfo::create_for_testing(),
Expand Down Expand Up @@ -108,17 +125,26 @@ fn with_constructor(deployer_contract: FeatureContract, expected_gas: u64) {
deployer_contract.get_instance_address(0),
)
.unwrap();
let deploy_call = &entry_point_call.execute_directly(&mut state).unwrap();
assert_eq!(
deploy_call.execution,
CallExecution {
retdata: retdata![],
gas_consumed: expected_gas,
..CallExecution::default()
}
);
// Note, this it the call info of the constructor call (inner call).
let deploy_call = &entry_point_call.execute_directly(&mut state).unwrap().inner_calls[0];
let deploy_constructor_call = &deploy_call.inner_calls[0];

assert_eq!(deploy_call.call.storage_address, contract_address);
assert_eq!(deploy_constructor_call.call.storage_address, contract_address);
assert_eq!(
deploy_call.execution,
deploy_constructor_call.execution,
CallExecution {
// The test contract constructor returns its first argument.
retdata: retdata![constructor_calldata[0]],
// This reflects the gas cost of storage write syscall.
gas_consumed: expected_gas,
gas_consumed: expected_constructor_gas,
..CallExecution::default()
}
);
Expand Down

0 comments on commit 2cb57a7

Please sign in to comment.