Skip to content

Commit c43afc6

Browse files
starknet_os_flow_tests: split test_new_class_flow new account flow
1 parent 9a33d1b commit c43afc6

File tree

1 file changed

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

1 file changed

+135
-104
lines changed

crates/starknet_os_flow_tests/src/tests.rs

Lines changed: 135 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,140 @@ async fn test_experimental_libfuncs_contract(#[values(true, false)] use_kzg_da:
12041204
}
12051205
}
12061206

1207+
#[rstest]
1208+
#[tokio::test]
1209+
async fn test_new_account_flow(#[values(true, false)] use_kzg_da: bool) {
1210+
let (mut test_manager, []) =
1211+
TestManager::<DictStateReader>::new_with_default_initial_state([]).await;
1212+
let current_block_number = test_manager.initial_state.next_block_number;
1213+
1214+
assert!(
1215+
current_block_number.0 > STORED_BLOCK_HASH_BUFFER,
1216+
"Current block number must be greater than STORED_BLOCK_HASH_BUFFER for the test to work."
1217+
);
1218+
1219+
let mut expected_messages_to_l1 = Vec::new();
1220+
1221+
// Declare a Cairo 1.0 account contract.
1222+
// TODO(Noa): Replace the main account of the test with this Cairo 1 account.
1223+
let faulty_account = FeatureContract::FaultyAccount(CairoVersion::Cairo1(RunnableCairo1::Casm));
1224+
let faulty_account_sierra = faulty_account.get_sierra();
1225+
let faulty_account_class_hash = faulty_account_sierra.calculate_class_hash();
1226+
let faulty_account_compiled_class_hash =
1227+
faulty_account.get_compiled_class_hash(&HashVersion::V2);
1228+
let declare_tx_args = declare_tx_args! {
1229+
sender_address: *FUNDED_ACCOUNT_ADDRESS,
1230+
class_hash: faulty_account_class_hash,
1231+
compiled_class_hash: faulty_account_compiled_class_hash,
1232+
resource_bounds: *NON_TRIVIAL_RESOURCE_BOUNDS,
1233+
nonce: test_manager.next_nonce(*FUNDED_ACCOUNT_ADDRESS),
1234+
};
1235+
let account_declare_tx = declare_tx(declare_tx_args);
1236+
let class_info = get_class_info_of_feature_contract(faulty_account);
1237+
let tx =
1238+
DeclareTransaction::create(account_declare_tx, class_info, &CHAIN_ID_FOR_TESTS).unwrap();
1239+
test_manager.add_cairo1_declare_tx(tx, &faulty_account_sierra);
1240+
1241+
// Deploy it.
1242+
let salt = ContractAddressSalt(Felt::ZERO);
1243+
let validate_constructor = Felt::ZERO; // false.
1244+
let ctor_calldata = calldata![validate_constructor];
1245+
let (deploy_tx, _) = get_deploy_contract_tx_and_address_with_salt(
1246+
faulty_account_class_hash,
1247+
ctor_calldata.clone(),
1248+
test_manager.next_nonce(*FUNDED_ACCOUNT_ADDRESS),
1249+
*NON_TRIVIAL_RESOURCE_BOUNDS,
1250+
salt,
1251+
);
1252+
test_manager.add_invoke_tx(deploy_tx, None);
1253+
1254+
// Prepare deploying an instance of the account by precomputing the address and funding it.
1255+
let valid = Felt::ZERO;
1256+
let salt = ContractAddressSalt(Felt::from(1993));
1257+
let faulty_account_address = calculate_contract_address(
1258+
salt,
1259+
faulty_account_class_hash,
1260+
&ctor_calldata,
1261+
ContractAddress::default(),
1262+
)
1263+
.unwrap();
1264+
// Fund the address.
1265+
test_manager.add_fund_address_tx_with_default_amount(faulty_account_address);
1266+
1267+
// Create a DeployAccount transaction.
1268+
let deploy_tx_args = deploy_account_tx_args! {
1269+
class_hash: faulty_account_class_hash,
1270+
resource_bounds: *NON_TRIVIAL_RESOURCE_BOUNDS,
1271+
contract_address_salt: salt,
1272+
signature: TransactionSignature(Arc::new(vec![valid])),
1273+
constructor_calldata: ctor_calldata,
1274+
};
1275+
let deploy_account_tx =
1276+
deploy_account_tx(deploy_tx_args, test_manager.next_nonce(faulty_account_address));
1277+
test_manager.add_deploy_account_tx(
1278+
DeployAccountTransaction::create(deploy_account_tx, &CHAIN_ID_FOR_TESTS).unwrap(),
1279+
);
1280+
1281+
// Declare a contract using the newly deployed account.
1282+
let empty_contract = FeatureContract::Empty(CairoVersion::Cairo1(RunnableCairo1::Casm));
1283+
let empty_contract_sierra = empty_contract.get_sierra();
1284+
let empty_contract_class_hash = empty_contract_sierra.calculate_class_hash();
1285+
let empty_contract_compiled_class_hash =
1286+
empty_contract.get_compiled_class_hash(&HashVersion::V2);
1287+
let declare_tx_args = declare_tx_args! {
1288+
sender_address: faulty_account_address,
1289+
class_hash: empty_contract_class_hash,
1290+
compiled_class_hash: empty_contract_compiled_class_hash,
1291+
resource_bounds: *NON_TRIVIAL_RESOURCE_BOUNDS,
1292+
nonce: test_manager.next_nonce(faulty_account_address),
1293+
signature: TransactionSignature(Arc::new(vec![valid])),
1294+
};
1295+
let account_declare_tx = declare_tx(declare_tx_args);
1296+
let class_info = get_class_info_of_feature_contract(empty_contract);
1297+
let tx =
1298+
DeclareTransaction::create(account_declare_tx, class_info, &CHAIN_ID_FOR_TESTS).unwrap();
1299+
test_manager.add_cairo1_declare_tx(tx, &empty_contract_sierra);
1300+
// The faulty account's __execute__ sends a message to L1.
1301+
expected_messages_to_l1.push(MessageToL1 {
1302+
from_address: faulty_account_address,
1303+
to_address: EthAddress::default(),
1304+
payload: L2ToL1Payload::default(),
1305+
});
1306+
1307+
// Invoke a function on the new account.
1308+
let invoke_tx_args = invoke_tx_args! {
1309+
sender_address: faulty_account_address,
1310+
nonce: test_manager.next_nonce(faulty_account_address),
1311+
calldata: create_calldata(faulty_account_address, "foo", &[]),
1312+
resource_bounds: *NON_TRIVIAL_RESOURCE_BOUNDS,
1313+
signature: TransactionSignature(Arc::new(vec![valid])),
1314+
};
1315+
test_manager.add_invoke_tx_from_args(invoke_tx_args, &CHAIN_ID_FOR_TESTS, None);
1316+
// The faulty account's __execute__ sends a message to L1.
1317+
expected_messages_to_l1.push(MessageToL1 {
1318+
from_address: faulty_account_address,
1319+
to_address: EthAddress::default(),
1320+
payload: L2ToL1Payload::default(),
1321+
});
1322+
1323+
// Run the test.
1324+
let test_output = test_manager
1325+
.execute_test_with_default_block_contexts(&TestParameters {
1326+
use_kzg_da,
1327+
messages_to_l1: expected_messages_to_l1,
1328+
..Default::default()
1329+
})
1330+
.await;
1331+
1332+
// Perform general validations and storage update validations.
1333+
test_output.perform_default_validations();
1334+
1335+
// Verify that the funded account, the new account and the sequencer all have changed balances.
1336+
test_output.assert_account_balance_change(*FUNDED_ACCOUNT_ADDRESS);
1337+
test_output.assert_account_balance_change(faulty_account_address);
1338+
test_output.assert_account_balance_change(contract_address!(TEST_SEQUENCER_ADDRESS));
1339+
}
1340+
12071341
#[rstest]
12081342
#[case::use_kzg(true, 5)]
12091343
#[case::not_use_kzg(false, 1)]
@@ -1452,108 +1586,6 @@ async fn test_new_class_flow(#[case] use_kzg_da: bool, #[case] n_blocks_in_multi
14521586
test_manager.add_funded_account_invoke(invoke_tx_args! { calldata });
14531587
}
14541588

1455-
// Declare a Cairo 1.0 account contract.
1456-
// TODO(Noa): Replace the main account of the test with this Cairo 1 account.
1457-
let faulty_account = FeatureContract::FaultyAccount(CairoVersion::Cairo1(RunnableCairo1::Casm));
1458-
let faulty_account_sierra = faulty_account.get_sierra();
1459-
let faulty_account_class_hash = faulty_account_sierra.calculate_class_hash();
1460-
let faulty_account_compiled_class_hash =
1461-
faulty_account.get_compiled_class_hash(&HashVersion::V2);
1462-
let declare_tx_args = declare_tx_args! {
1463-
sender_address: *FUNDED_ACCOUNT_ADDRESS,
1464-
class_hash: faulty_account_class_hash,
1465-
compiled_class_hash: faulty_account_compiled_class_hash,
1466-
resource_bounds: *NON_TRIVIAL_RESOURCE_BOUNDS,
1467-
nonce: test_manager.next_nonce(*FUNDED_ACCOUNT_ADDRESS),
1468-
};
1469-
let account_declare_tx = declare_tx(declare_tx_args);
1470-
let class_info = get_class_info_of_feature_contract(faulty_account);
1471-
let tx =
1472-
DeclareTransaction::create(account_declare_tx, class_info, &CHAIN_ID_FOR_TESTS).unwrap();
1473-
test_manager.add_cairo1_declare_tx(tx, &faulty_account_sierra);
1474-
1475-
// Deploy it.
1476-
let salt = ContractAddressSalt(Felt::ZERO);
1477-
let validate_constructor = Felt::ZERO; // false.
1478-
let ctor_calldata = calldata![validate_constructor];
1479-
let (deploy_tx, _) = get_deploy_contract_tx_and_address_with_salt(
1480-
faulty_account_class_hash,
1481-
ctor_calldata.clone(),
1482-
test_manager.next_nonce(*FUNDED_ACCOUNT_ADDRESS),
1483-
*NON_TRIVIAL_RESOURCE_BOUNDS,
1484-
salt,
1485-
);
1486-
test_manager.add_invoke_tx(deploy_tx, None);
1487-
1488-
// Prepare deploying an instance of the account by precomputing the address and funding it.
1489-
let valid = Felt::ZERO;
1490-
let salt = ContractAddressSalt(Felt::from(1993));
1491-
let faulty_account_address = calculate_contract_address(
1492-
salt,
1493-
faulty_account_class_hash,
1494-
&ctor_calldata,
1495-
ContractAddress::default(),
1496-
)
1497-
.unwrap();
1498-
// Fund the address.
1499-
test_manager.add_fund_address_tx_with_default_amount(faulty_account_address);
1500-
1501-
// Create a DeployAccount transaction.
1502-
let deploy_tx_args = deploy_account_tx_args! {
1503-
class_hash: faulty_account_class_hash,
1504-
resource_bounds: *NON_TRIVIAL_RESOURCE_BOUNDS,
1505-
contract_address_salt: salt,
1506-
signature: TransactionSignature(Arc::new(vec![valid])),
1507-
constructor_calldata: ctor_calldata,
1508-
};
1509-
let deploy_account_tx =
1510-
deploy_account_tx(deploy_tx_args, test_manager.next_nonce(faulty_account_address));
1511-
test_manager.add_deploy_account_tx(
1512-
DeployAccountTransaction::create(deploy_account_tx, &CHAIN_ID_FOR_TESTS).unwrap(),
1513-
);
1514-
1515-
// Declare a contract using the newly deployed account.
1516-
let empty_contract = FeatureContract::Empty(CairoVersion::Cairo1(RunnableCairo1::Casm));
1517-
let empty_contract_sierra = empty_contract.get_sierra();
1518-
let empty_contract_class_hash = empty_contract_sierra.calculate_class_hash();
1519-
let empty_contract_compiled_class_hash =
1520-
empty_contract.get_compiled_class_hash(&HashVersion::V2);
1521-
let declare_tx_args = declare_tx_args! {
1522-
sender_address: faulty_account_address,
1523-
class_hash: empty_contract_class_hash,
1524-
compiled_class_hash: empty_contract_compiled_class_hash,
1525-
resource_bounds: *NON_TRIVIAL_RESOURCE_BOUNDS,
1526-
nonce: test_manager.next_nonce(faulty_account_address),
1527-
signature: TransactionSignature(Arc::new(vec![valid])),
1528-
};
1529-
let account_declare_tx = declare_tx(declare_tx_args);
1530-
let class_info = get_class_info_of_feature_contract(empty_contract);
1531-
let tx =
1532-
DeclareTransaction::create(account_declare_tx, class_info, &CHAIN_ID_FOR_TESTS).unwrap();
1533-
test_manager.add_cairo1_declare_tx(tx, &empty_contract_sierra);
1534-
// The faulty account's __execute__ sends a message to L1.
1535-
expected_messages_to_l1.push(MessageToL1 {
1536-
from_address: faulty_account_address,
1537-
to_address: EthAddress::default(),
1538-
payload: L2ToL1Payload::default(),
1539-
});
1540-
1541-
// Invoke a function on the new account.
1542-
let invoke_tx_args = invoke_tx_args! {
1543-
sender_address: faulty_account_address,
1544-
nonce: test_manager.next_nonce(faulty_account_address),
1545-
calldata: create_calldata(faulty_account_address, "foo", &[]),
1546-
resource_bounds: *NON_TRIVIAL_RESOURCE_BOUNDS,
1547-
signature: TransactionSignature(Arc::new(vec![valid])),
1548-
};
1549-
test_manager.add_invoke_tx_from_args(invoke_tx_args, &CHAIN_ID_FOR_TESTS, None);
1550-
// The faulty account's __execute__ sends a message to L1.
1551-
expected_messages_to_l1.push(MessageToL1 {
1552-
from_address: faulty_account_address,
1553-
to_address: EthAddress::default(),
1554-
payload: L2ToL1Payload::default(),
1555-
});
1556-
15571589
// Run the test.
15581590
update_expected_storage_updates_for_block_hash_contract(
15591591
&mut expected_storage_updates,
@@ -1576,8 +1608,7 @@ async fn test_new_class_flow(#[case] use_kzg_da: bool, #[case] n_blocks_in_multi
15761608
Some(&StateDiff { storage_updates: expected_storage_updates, ..Default::default() }),
15771609
);
15781610

1579-
// Verify that the funded account, the new account and the sequencer all have changed balances.
1611+
// Verify that the funded account and the sequencer have both updated their balances.
15801612
test_output.assert_account_balance_change(*FUNDED_ACCOUNT_ADDRESS);
1581-
test_output.assert_account_balance_change(faulty_account_address);
15821613
test_output.assert_account_balance_change(contract_address!(TEST_SEQUENCER_ADDRESS));
15831614
}

0 commit comments

Comments
 (0)