Skip to content

Commit 4cc533d

Browse files
starknet_os_flow_tests: migrate test_initial_sierra_gas
1 parent 2913251 commit 4cc533d

File tree

1 file changed

+104
-1
lines changed
  • crates/starknet_os_flow_tests/src

1 file changed

+104
-1
lines changed

crates/starknet_os_flow_tests/src/tests.rs

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use cairo_vm::types::builtin_name::BuiltinName;
1313
use expect_test::expect;
1414
use rstest::rstest;
1515
use starknet_api::abi::abi_utils::{get_storage_var_address, selector_from_name};
16-
use starknet_api::block::{BlockInfo, BlockNumber, BlockTimestamp};
16+
use starknet_api::block::{BlockInfo, BlockNumber, BlockTimestamp, GasPrice};
1717
use starknet_api::contract_class::compiled_class_hash::{HashVersion, HashableCompiledClass};
1818
use starknet_api::contract_class::{ClassInfo, ContractClass};
1919
use starknet_api::core::{
@@ -1980,3 +1980,106 @@ async fn test_block_info(#[values(true, false)] is_cairo0: bool) {
19801980
test_manager.execute_test_with_default_block_contexts(&TestParameters::default()).await;
19811981
test_output.perform_default_validations();
19821982
}
1983+
1984+
#[rstest]
1985+
#[tokio::test]
1986+
async fn test_initial_sierra_gas() {
1987+
let account_contract = FeatureContract::Experimental;
1988+
let (mut test_manager, [account_address]) =
1989+
TestManager::<DictStateReader>::new_with_default_initial_state([(
1990+
account_contract,
1991+
calldata![],
1992+
)])
1993+
.await;
1994+
1995+
// Fund the account.
1996+
test_manager.add_fund_address_tx_with_default_amount(account_address);
1997+
1998+
// Test invoke gas limits.
1999+
let os_constants = &VersionedConstants::latest_constants().os_constants;
2000+
let validate_max_sierra_gas = os_constants.validate_max_sierra_gas.0;
2001+
let execute_max_sierra_gas = os_constants.execute_max_sierra_gas.0;
2002+
for l2_gas_amount in [
2003+
validate_max_sierra_gas - 123456,
2004+
validate_max_sierra_gas + 123456,
2005+
10 * execute_max_sierra_gas,
2006+
] {
2007+
// Set up gas parameters.
2008+
let resource_bounds = match *NON_TRIVIAL_RESOURCE_BOUNDS {
2009+
ValidResourceBounds::AllResources(all_resource_bounds) => {
2010+
ValidResourceBounds::AllResources(AllResourceBounds {
2011+
l2_gas: ResourceBounds {
2012+
max_amount: GasAmount(l2_gas_amount),
2013+
max_price_per_unit: GasPrice(1 << 40),
2014+
},
2015+
..all_resource_bounds
2016+
})
2017+
}
2018+
_ => panic!("Resource bounds are not all resources"),
2019+
};
2020+
let expected_validate_gas_upper_limit = u64::min(l2_gas_amount, validate_max_sierra_gas);
2021+
let expected_validate_gas_lower_limit = expected_validate_gas_upper_limit - 50000;
2022+
let expected_execute_gas_upper_limit =
2023+
u64::min(l2_gas_amount - 50000, execute_max_sierra_gas);
2024+
let expected_execute_gas_lower_limit = expected_execute_gas_upper_limit - 150000;
2025+
2026+
// Invoke verify_gas_limits.
2027+
let invoke_args = invoke_tx_args! {
2028+
sender_address: account_address,
2029+
nonce: test_manager.next_nonce(account_address),
2030+
calldata: create_calldata(
2031+
account_address,
2032+
"verify_gas_limits",
2033+
&[
2034+
Felt::from(expected_validate_gas_lower_limit),
2035+
Felt::from(expected_validate_gas_upper_limit),
2036+
Felt::from(expected_execute_gas_lower_limit),
2037+
Felt::from(expected_execute_gas_upper_limit)
2038+
]
2039+
),
2040+
resource_bounds: resource_bounds,
2041+
};
2042+
test_manager.add_invoke_tx_from_args(invoke_args, &CHAIN_ID_FOR_TESTS, None);
2043+
}
2044+
2045+
// L1 handler bounds test.
2046+
let expected_l1_handler_gas_upper_bound = os_constants.l1_handler_max_amount_bounds.l2_gas.0;
2047+
let expected_l1_handler_gas_lower_bound = expected_l1_handler_gas_upper_bound - 10001;
2048+
let from_address = Felt::from_hex_unchecked("0xDEADACC");
2049+
let selector = selector_from_name("verify_gas_limits_l1_handler");
2050+
let calldata = vec![
2051+
Felt::from(expected_l1_handler_gas_lower_bound),
2052+
Felt::from(expected_l1_handler_gas_upper_bound),
2053+
];
2054+
let l1_handler_tx = ExecutableL1HandlerTransaction::create(
2055+
L1HandlerTransaction {
2056+
version: L1HandlerTransaction::VERSION,
2057+
nonce: Nonce::default(),
2058+
contract_address: account_address,
2059+
entry_point_selector: selector,
2060+
calldata: Calldata(Arc::new([vec![from_address], calldata.clone()].concat())),
2061+
},
2062+
&CHAIN_ID_FOR_TESTS,
2063+
Fee(1_000_000),
2064+
)
2065+
.unwrap();
2066+
test_manager.add_l1_handler_tx(l1_handler_tx.clone(), None);
2067+
let messages_to_l2 = vec![MessageToL2 {
2068+
from_address: EthAddress::try_from(from_address).unwrap(),
2069+
to_address: account_address,
2070+
nonce: Nonce::default(),
2071+
selector,
2072+
payload: L1ToL2Payload(calldata),
2073+
}];
2074+
2075+
// Run test.
2076+
let test_output = test_manager
2077+
.execute_test_with_default_block_contexts(&TestParameters {
2078+
use_kzg_da: true,
2079+
messages_to_l2,
2080+
..Default::default()
2081+
})
2082+
.await;
2083+
2084+
test_output.perform_default_validations();
2085+
}

0 commit comments

Comments
 (0)