Skip to content

Commit 2ff59a1

Browse files
starknet_os_flow_tests: migrate test_initial_sierra_gas
1 parent a20b5be commit 2ff59a1

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

0 commit comments

Comments
 (0)