Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(blockifier): add a check on deploy syscall gas cost in the happ… #478

Merged
Merged
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
47 changes: 36 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,11 @@ fn no_constructor(deployer_contract: FeatureContract) {
)
.unwrap();

let deploy_call = &entry_point_call.execute_directly(&mut state).unwrap().inner_calls[0];
let constructor_call = &deploy_call.inner_calls[0];

assert_eq!(deploy_call.call.storage_address, deployed_contract_address);
assert_eq!(constructor_call.call.storage_address, deployed_contract_address);
assert_eq!(
deploy_call.execution,
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 +88,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 +123,27 @@ fn with_constructor(deployer_contract: FeatureContract, expected_gas: u64) {
deployer_contract.get_instance_address(0),
)
.unwrap();
// Note, this is the call info of the constructor call (inner call).
let deploy_call = &entry_point_call.execute_directly(&mut state).unwrap().inner_calls[0];

assert_eq!(deploy_call.call.storage_address, contract_address);
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 constructor_call = &deploy_call.inner_calls[0];

assert_eq!(constructor_call.call.storage_address, contract_address);
assert_eq!(
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
Loading