Skip to content

Commit 54a7e82

Browse files
starknet_os_flow_tests: split test_new_class_flow new account flow
1 parent 295f603 commit 54a7e82

File tree

1 file changed

+151
-105
lines changed
  • crates/starknet_os_flow_tests/src

1 file changed

+151
-105
lines changed

crates/starknet_os_flow_tests/src/tests.rs

Lines changed: 151 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,155 @@ async fn test_experimental_libfuncs_contract(#[values(true, false)] use_kzg_da:
12171217
}
12181218
}
12191219

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

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

15911638
// Verify that the funded account, the new account and the sequencer all have changed balances.
15921639
test_output.assert_account_balance_change(*FUNDED_ACCOUNT_ADDRESS);
1593-
test_output.assert_account_balance_change(faulty_account_address);
15941640
test_output.assert_account_balance_change(contract_address!(TEST_SEQUENCER_ADDRESS));
15951641

15961642
// Validate poseidon usage.
15971643
// TODO(Meshi): Add blake opcode validations.
15981644
let poseidons = test_output.get_builtin_usage(&BuiltinName::poseidon);
15991645
if use_kzg_da {
16001646
expect![[r#"
1601-
560
1647+
471
16021648
"#]]
16031649
.assert_debug_eq(&poseidons);
16041650
} else {
16051651
expect![[r#"
1606-
456
1652+
367
16071653
"#]]
16081654
.assert_debug_eq(&poseidons);
16091655
}

0 commit comments

Comments
 (0)