Skip to content

Commit fa08c78

Browse files
starknet_os_flow_tests: migrate test_deprecated_tx_info
1 parent 41b8174 commit fa08c78

File tree

1 file changed

+166
-0
lines changed
  • crates/starknet_os_flow_tests/src

1 file changed

+166
-0
lines changed

crates/starknet_os_flow_tests/src/tests.rs

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ use starknet_api::core::{
2525
PatriciaKey,
2626
};
2727
use starknet_api::executable_transaction::{
28+
AccountTransaction,
2829
DeclareTransaction,
2930
DeployAccountTransaction,
3031
InvokeTransaction,
3132
L1HandlerTransaction as ExecutableL1HandlerTransaction,
33+
Transaction,
34+
TransactionType,
3235
};
3336
use starknet_api::execution_resources::GasAmount;
3437
use starknet_api::state::StorageKey;
@@ -1759,3 +1762,166 @@ async fn test_new_class_flow(#[case] use_kzg_da: bool, #[case] n_blocks_in_multi
17591762
.assert_debug_eq(poseidons);
17601763
}
17611764
}
1765+
1766+
#[rstest]
1767+
#[tokio::test]
1768+
async fn test_deprecated_tx_info() {
1769+
let tx_info_writer = FeatureContract::TxInfoWriter;
1770+
let class_hash = get_class_hash_of_feature_contract(tx_info_writer);
1771+
// Initialize the test manager with the tx info writer already declared.
1772+
// We can ignore the address of the dpeloyed instance.
1773+
let (mut test_manager, mut nonce_manager, _) =
1774+
TestManager::<DictStateReader>::new_with_default_initial_state([(
1775+
tx_info_writer,
1776+
calldata![],
1777+
)])
1778+
.await;
1779+
1780+
// Prepare to deploy: precompute the address.
1781+
let salt = Felt::ZERO;
1782+
let tx_info_account_address = calculate_contract_address(
1783+
ContractAddressSalt(salt),
1784+
class_hash,
1785+
&calldata![],
1786+
ContractAddress::default(),
1787+
)
1788+
.unwrap();
1789+
1790+
// Fund the address.
1791+
let transfer_amount = 2 * NON_TRIVIAL_RESOURCE_BOUNDS.max_possible_fee(Tip(0)).0;
1792+
let transfer_tx_args = invoke_tx_args! {
1793+
sender_address: *FUNDED_ACCOUNT_ADDRESS,
1794+
nonce: nonce_manager.next(*FUNDED_ACCOUNT_ADDRESS),
1795+
calldata: create_calldata(
1796+
*STRK_FEE_TOKEN_ADDRESS,
1797+
"transfer",
1798+
&[**tx_info_account_address, Felt::from(transfer_amount), Felt::ZERO]
1799+
),
1800+
resource_bounds: *NON_TRIVIAL_RESOURCE_BOUNDS,
1801+
};
1802+
test_manager.add_invoke_tx_from_args(transfer_tx_args, &CHAIN_ID_FOR_TESTS, None);
1803+
1804+
// Deploy the account.
1805+
let deploy_tx_args = deploy_account_tx_args! {
1806+
class_hash,
1807+
resource_bounds: *NON_TRIVIAL_RESOURCE_BOUNDS,
1808+
contract_address_salt: ContractAddressSalt(salt),
1809+
};
1810+
let deploy_account_tx = DeployAccountTransaction::create(
1811+
deploy_account_tx(deploy_tx_args, nonce_manager.next(tx_info_account_address)),
1812+
&CHAIN_ID_FOR_TESTS,
1813+
)
1814+
.unwrap();
1815+
test_manager.add_deploy_account_tx(deploy_account_tx.clone());
1816+
1817+
// Invoke (call write).
1818+
let invoke_args = invoke_tx_args! {
1819+
sender_address: tx_info_account_address,
1820+
nonce: nonce_manager.next(tx_info_account_address),
1821+
calldata: calldata![Felt::ZERO],
1822+
resource_bounds: *NON_TRIVIAL_RESOURCE_BOUNDS,
1823+
};
1824+
let invoke_tx = InvokeTransaction::create(invoke_tx(invoke_args), &CHAIN_ID_FOR_TESTS).unwrap();
1825+
test_manager.add_invoke_tx(invoke_tx.clone(), None);
1826+
1827+
// Declare.
1828+
let empty_contract = FeatureContract::Empty(CairoVersion::Cairo1(RunnableCairo1::Casm));
1829+
let empty_contract_sierra = empty_contract.get_sierra();
1830+
let empty_contract_class_hash = empty_contract_sierra.calculate_class_hash();
1831+
let empty_contract_compiled_class_hash =
1832+
empty_contract.get_compiled_class_hash(&HashVersion::V2);
1833+
let declare_tx_args = declare_tx_args! {
1834+
sender_address: tx_info_account_address,
1835+
class_hash: empty_contract_class_hash,
1836+
compiled_class_hash: empty_contract_compiled_class_hash,
1837+
resource_bounds: *NON_TRIVIAL_RESOURCE_BOUNDS,
1838+
nonce: nonce_manager.next(tx_info_account_address),
1839+
};
1840+
let account_declare_tx = declare_tx(declare_tx_args);
1841+
let class_info = get_class_info_of_feature_contract(empty_contract);
1842+
let declare_tx =
1843+
DeclareTransaction::create(account_declare_tx, class_info, &CHAIN_ID_FOR_TESTS).unwrap();
1844+
test_manager.add_cairo1_declare_tx(declare_tx.clone(), &empty_contract_sierra);
1845+
1846+
// L1 handler (call `l1_write`).
1847+
let from_address = Felt::from(85);
1848+
let selector = selector_from_name("l1_write");
1849+
let l1_handler_tx = ExecutableL1HandlerTransaction::create(
1850+
L1HandlerTransaction {
1851+
version: L1HandlerTransaction::VERSION,
1852+
nonce: Nonce::default(),
1853+
contract_address: tx_info_account_address,
1854+
entry_point_selector: selector,
1855+
// from_address (L1 address), key, value.
1856+
calldata: calldata![from_address],
1857+
},
1858+
&CHAIN_ID_FOR_TESTS,
1859+
Fee(1_000_000),
1860+
)
1861+
.unwrap();
1862+
test_manager.add_l1_handler_tx(l1_handler_tx.clone(), None);
1863+
1864+
// Run the test.
1865+
let messages_to_l2 = vec![MessageToL2 {
1866+
from_address: from_address.try_into().unwrap(),
1867+
to_address: tx_info_account_address,
1868+
selector,
1869+
payload: L1ToL2Payload::default(),
1870+
nonce: Nonce::default(),
1871+
}];
1872+
let test_output = test_manager
1873+
.execute_test_with_default_block_contexts(&TestParameters {
1874+
messages_to_l2,
1875+
..Default::default()
1876+
})
1877+
.await;
1878+
1879+
// Perform general validations and storage update validations.
1880+
let mut contract_storage_updates = HashMap::new();
1881+
for tx in [
1882+
Transaction::Account(AccountTransaction::DeployAccount(deploy_account_tx)),
1883+
Transaction::Account(AccountTransaction::Invoke(invoke_tx)),
1884+
Transaction::Account(AccountTransaction::Declare(declare_tx)),
1885+
Transaction::L1Handler(l1_handler_tx),
1886+
] {
1887+
let tx_type = &[tx.tx_type().tx_type_as_felt()];
1888+
contract_storage_updates
1889+
.insert(get_storage_var_address("transaction_hash", tx_type), tx.tx_hash().0);
1890+
contract_storage_updates.insert(get_storage_var_address("max_fee", tx_type), Felt::ZERO);
1891+
contract_storage_updates.insert(get_storage_var_address("nonce", tx_type), tx.nonce().0);
1892+
contract_storage_updates.insert(
1893+
get_storage_var_address("account_contract_address", tx_type),
1894+
**tx_info_account_address,
1895+
);
1896+
contract_storage_updates
1897+
.insert(get_storage_var_address("signature_len", tx_type), Felt::ZERO);
1898+
contract_storage_updates.insert(
1899+
get_storage_var_address("chain_id", tx_type),
1900+
Felt::try_from(&*CHAIN_ID_FOR_TESTS).unwrap(),
1901+
);
1902+
if !matches!(tx.tx_type(), TransactionType::L1Handler) {
1903+
contract_storage_updates
1904+
.insert(get_storage_var_address("version", tx_type), tx.version().0);
1905+
} else {
1906+
contract_storage_updates
1907+
.insert(get_storage_var_address("version", tx_type), Felt::ZERO);
1908+
}
1909+
}
1910+
// Add the offset to all storage update values and convert types.
1911+
let offset = Felt::from_hex_unchecked("0x1234");
1912+
let contract_storage_updates = contract_storage_updates
1913+
.into_iter()
1914+
.map(|(key, value)| (StarknetStorageKey(key), StarknetStorageValue(value + offset)))
1915+
.collect();
1916+
1917+
let expected_storage_updates =
1918+
HashMap::from([(tx_info_account_address, contract_storage_updates)]);
1919+
1920+
let perform_global_validations = true;
1921+
test_output.perform_validations(
1922+
perform_global_validations,
1923+
Some(&StateDiff { storage_updates: expected_storage_updates, ..Default::default() }),
1924+
);
1925+
test_output.assert_account_balance_change(tx_info_account_address);
1926+
test_output.assert_account_balance_change(contract_address!(TEST_SEQUENCER_ADDRESS));
1927+
}

0 commit comments

Comments
 (0)