From 184d7ef0ca35458ef88ab6c0dc430bd21d7acac8 Mon Sep 17 00:00:00 2001 From: Ori Friedlender Date: Tue, 4 Jun 2024 14:47:18 +0300 Subject: [PATCH] test(concurrency): test scenarios of validate_reads with unvalid reads --- .../src/concurrency/versioned_state_test.rs | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/crates/blockifier/src/concurrency/versioned_state_test.rs b/crates/blockifier/src/concurrency/versioned_state_test.rs index 7482e45f93..3525657244 100644 --- a/crates/blockifier/src/concurrency/versioned_state_test.rs +++ b/crates/blockifier/src/concurrency/versioned_state_test.rs @@ -324,6 +324,80 @@ fn test_validate_reads( ); } +#[rstest] +#[case::storage( + StateMaps { + storage: HashMap::from( + [((contract_address!("0x1"), storage_key!("0x1")), stark_felt!(1_u8))] + ), + ..Default::default() + }, + StateMaps { + storage: HashMap::from( + [((contract_address!("0x1"), storage_key!("0x1")), stark_felt!(2_u8))] + ), + ..Default::default() + } +)] +#[case::nonces( + StateMaps { + nonces: HashMap::from([(contract_address!("0x1"), nonce!(1_u8))]), + ..Default::default() + }, + StateMaps { + nonces: HashMap::from([(contract_address!("0x1"), nonce!(2_u8))]), + ..Default::default() + } +)] +#[case::class_hashes( + StateMaps { + class_hashes: HashMap::from([(contract_address!("0x1"), class_hash!(1_u8))]), + ..Default::default() + }, + StateMaps { + class_hashes: HashMap::from([(contract_address!("0x1"), class_hash!(2_u8))]), + ..Default::default() + } +)] +#[case::compiled_class_hashes( + StateMaps { + compiled_class_hashes: HashMap::from([(class_hash!(1_u8), compiled_class_hash!(1_u8))]), + ..Default::default() + }, + StateMaps { + compiled_class_hashes: HashMap::from([(class_hash!(1_u8), compiled_class_hash!(2_u8))]), + ..Default::default() + } +)] +fn test_false_validate_reads( + #[case] tx_1_reads: StateMaps, + #[case] tx_0_writes: StateMaps, + safe_versioned_state: ThreadSafeVersionedState>, +) { + let version_state_proxy = safe_versioned_state.pin_version(0); + version_state_proxy.state().apply_writes(0, &tx_0_writes, &HashMap::default()); + assert!(!safe_versioned_state.pin_version(1).validate_reads(&tx_1_reads)); +} + +#[rstest] +fn test_false_validate_reads_declared_contracts( + safe_versioned_state: ThreadSafeVersionedState>, +) { + let tx_1_reads = StateMaps { + declared_contracts: HashMap::from([(class_hash!(1_u8), false)]), + ..Default::default() + }; + let tx_0_writes = StateMaps { + declared_contracts: HashMap::from([(class_hash!(1_u8), true)]), + ..Default::default() + }; + let version_state_proxy = safe_versioned_state.pin_version(0); + let compiled_contract_calss = FeatureContract::TestContract(CairoVersion::Cairo1).get_class(); + let class_hash_to_class = HashMap::from([(class_hash!(1_u8), compiled_contract_calss)]); + version_state_proxy.state().apply_writes(0, &tx_0_writes, &class_hash_to_class); + assert!(!safe_versioned_state.pin_version(1).validate_reads(&tx_1_reads)); +} + #[rstest] fn test_apply_writes( contract_address: ContractAddress,