Skip to content

Commit 192867e

Browse files
starknet_os_flow_tests: migrate test_deprecated_tx_info
1 parent 3802aa6 commit 192867e

File tree

1 file changed

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

1 file changed

+154
-0
lines changed

crates/starknet_os_flow_tests/src/tests.rs

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ use starknet_api::core::{
2424
PatriciaKey,
2525
};
2626
use starknet_api::executable_transaction::{
27+
AccountTransaction,
2728
DeclareTransaction,
2829
DeployAccountTransaction,
2930
InvokeTransaction,
3031
L1HandlerTransaction as ExecutableL1HandlerTransaction,
32+
Transaction,
33+
TransactionType,
3134
};
3235
use starknet_api::execution_resources::GasAmount;
3336
use starknet_api::state::StorageKey;
@@ -1545,3 +1548,154 @@ async fn test_new_class_flow(#[case] use_kzg_da: bool, #[case] n_blocks_in_multi
15451548
.assert_debug_eq(poseidons);
15461549
}
15471550
}
1551+
1552+
#[rstest]
1553+
#[tokio::test]
1554+
async fn test_deprecated_tx_info() {
1555+
let tx_info_writer = FeatureContract::TxInfoWriter;
1556+
let class_hash = get_class_hash_of_feature_contract(tx_info_writer);
1557+
// Initialize the test manager with the tx info writer already declared.
1558+
// We can ignore the address of the dpeloyed instance.
1559+
let (mut test_manager, _) = TestManager::<DictStateReader>::new_with_default_initial_state([(
1560+
tx_info_writer,
1561+
calldata![],
1562+
)])
1563+
.await;
1564+
1565+
// Prepare to deploy: precompute the address.
1566+
let salt = Felt::ZERO;
1567+
let tx_info_account_address = calculate_contract_address(
1568+
ContractAddressSalt(salt),
1569+
class_hash,
1570+
&calldata![],
1571+
ContractAddress::default(),
1572+
)
1573+
.unwrap();
1574+
1575+
// Fund the address.
1576+
test_manager.add_fund_address_tx_with_default_amount(tx_info_account_address);
1577+
1578+
// Deploy the account.
1579+
let deploy_tx_args = deploy_account_tx_args! {
1580+
class_hash,
1581+
resource_bounds: *NON_TRIVIAL_RESOURCE_BOUNDS,
1582+
contract_address_salt: ContractAddressSalt(salt),
1583+
};
1584+
let deploy_account_tx = DeployAccountTransaction::create(
1585+
deploy_account_tx(deploy_tx_args, test_manager.next_nonce(tx_info_account_address)),
1586+
&CHAIN_ID_FOR_TESTS,
1587+
)
1588+
.unwrap();
1589+
test_manager.add_deploy_account_tx(deploy_account_tx.clone());
1590+
1591+
// Invoke (call write).
1592+
let invoke_args = invoke_tx_args! {
1593+
sender_address: tx_info_account_address,
1594+
nonce: test_manager.next_nonce(tx_info_account_address),
1595+
calldata: calldata![Felt::ZERO],
1596+
resource_bounds: *NON_TRIVIAL_RESOURCE_BOUNDS,
1597+
};
1598+
let invoke_tx = InvokeTransaction::create(invoke_tx(invoke_args), &CHAIN_ID_FOR_TESTS).unwrap();
1599+
test_manager.add_invoke_tx(invoke_tx.clone(), None);
1600+
1601+
// Declare.
1602+
let empty_contract = FeatureContract::Empty(CairoVersion::Cairo1(RunnableCairo1::Casm));
1603+
let empty_contract_sierra = empty_contract.get_sierra();
1604+
let empty_contract_class_hash = empty_contract_sierra.calculate_class_hash();
1605+
let empty_contract_compiled_class_hash =
1606+
empty_contract.get_compiled_class_hash(&HashVersion::V2);
1607+
let declare_tx_args = declare_tx_args! {
1608+
sender_address: tx_info_account_address,
1609+
class_hash: empty_contract_class_hash,
1610+
compiled_class_hash: empty_contract_compiled_class_hash,
1611+
resource_bounds: *NON_TRIVIAL_RESOURCE_BOUNDS,
1612+
nonce: test_manager.next_nonce(tx_info_account_address),
1613+
};
1614+
let account_declare_tx = declare_tx(declare_tx_args);
1615+
let class_info = get_class_info_of_feature_contract(empty_contract);
1616+
let declare_tx =
1617+
DeclareTransaction::create(account_declare_tx, class_info, &CHAIN_ID_FOR_TESTS).unwrap();
1618+
test_manager.add_cairo1_declare_tx(declare_tx.clone(), &empty_contract_sierra);
1619+
1620+
// L1 handler (call `l1_write`).
1621+
let from_address = Felt::from(85);
1622+
let selector = selector_from_name("l1_write");
1623+
let l1_handler_tx = ExecutableL1HandlerTransaction::create(
1624+
L1HandlerTransaction {
1625+
version: L1HandlerTransaction::VERSION,
1626+
nonce: Nonce::default(),
1627+
contract_address: tx_info_account_address,
1628+
entry_point_selector: selector,
1629+
// from_address (L1 address), key, value.
1630+
calldata: calldata![from_address],
1631+
},
1632+
&CHAIN_ID_FOR_TESTS,
1633+
Fee(1_000_000),
1634+
)
1635+
.unwrap();
1636+
test_manager.add_l1_handler_tx(l1_handler_tx.clone(), None);
1637+
1638+
// Run the test.
1639+
let messages_to_l2 = vec![MessageToL2 {
1640+
from_address: from_address.try_into().unwrap(),
1641+
to_address: tx_info_account_address,
1642+
selector,
1643+
payload: L1ToL2Payload::default(),
1644+
nonce: Nonce::default(),
1645+
}];
1646+
let test_output = test_manager
1647+
.execute_test_with_default_block_contexts(&TestParameters {
1648+
messages_to_l2,
1649+
..Default::default()
1650+
})
1651+
.await;
1652+
1653+
// Perform general validations and storage update validations.
1654+
let mut contract_storage_updates = HashMap::new();
1655+
for tx in [
1656+
Transaction::Account(AccountTransaction::DeployAccount(deploy_account_tx)),
1657+
Transaction::Account(AccountTransaction::Invoke(invoke_tx)),
1658+
Transaction::Account(AccountTransaction::Declare(declare_tx)),
1659+
Transaction::L1Handler(l1_handler_tx),
1660+
] {
1661+
let tx_type = &[tx.tx_type().tx_type_as_felt()];
1662+
contract_storage_updates
1663+
.insert(get_storage_var_address("transaction_hash", tx_type), tx.tx_hash().0);
1664+
contract_storage_updates.insert(get_storage_var_address("max_fee", tx_type), Felt::ZERO);
1665+
contract_storage_updates.insert(get_storage_var_address("nonce", tx_type), tx.nonce().0);
1666+
contract_storage_updates.insert(
1667+
get_storage_var_address("account_contract_address", tx_type),
1668+
**tx_info_account_address,
1669+
);
1670+
contract_storage_updates
1671+
.insert(get_storage_var_address("signature_len", tx_type), Felt::ZERO);
1672+
contract_storage_updates.insert(
1673+
get_storage_var_address("chain_id", tx_type),
1674+
Felt::try_from(&*CHAIN_ID_FOR_TESTS).unwrap(),
1675+
);
1676+
if !matches!(tx.tx_type(), TransactionType::L1Handler) {
1677+
contract_storage_updates
1678+
.insert(get_storage_var_address("version", tx_type), tx.version().0);
1679+
} else {
1680+
contract_storage_updates
1681+
.insert(get_storage_var_address("version", tx_type), Felt::ZERO);
1682+
}
1683+
}
1684+
// Add the offset to all storage update values and convert types.
1685+
let offset = Felt::from_hex_unchecked("0x1234");
1686+
let contract_storage_updates = contract_storage_updates
1687+
.into_iter()
1688+
.map(|(key, value)| (StarknetStorageKey(key), StarknetStorageValue(value + offset)))
1689+
.collect();
1690+
1691+
let expected_storage_updates =
1692+
HashMap::from([(tx_info_account_address, contract_storage_updates)]);
1693+
1694+
let perform_global_validations = true;
1695+
test_output.perform_validations(
1696+
perform_global_validations,
1697+
Some(&StateDiff { storage_updates: expected_storage_updates, ..Default::default() }),
1698+
);
1699+
test_output.assert_account_balance_change(tx_info_account_address);
1700+
test_output.assert_account_balance_change(contract_address!(TEST_SEQUENCER_ADDRESS));
1701+
}

0 commit comments

Comments
 (0)