This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(concurrency): multithreaded test for decrease_validation_index
- Loading branch information
1 parent
25b259b
commit 689a2d4
Showing
6 changed files
with
127 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use std::collections::HashMap; | ||
use std::sync::Arc; | ||
|
||
use rstest::rstest; | ||
use starknet_api::core::{ContractAddress, PatriciaKey}; | ||
use starknet_api::hash::{StarkFelt, StarkHash}; | ||
use starknet_api::stark_felt; | ||
|
||
use crate::concurrency::scheduler::{Scheduler, Task}; | ||
use crate::concurrency::test_utils::{ | ||
contract_address, safe_versioned_state_for_testing, DEFAULT_CHUNK_SIZE, | ||
}; | ||
use crate::state::cached_state::{ContractClassMapping, StateMaps}; | ||
use crate::state::state_api::StateReader; | ||
use crate::storage_key; | ||
use crate::test_utils::dict_state_reader::DictStateReader; | ||
|
||
// TODO(barak, 01/07/2024): Check txs with "reads". | ||
// TODO(barak, 01/07/2024): Check cached_initial_values in versioned_state_proxy flow test. | ||
#[rstest] | ||
fn scheduler_flow_test(contract_address: ContractAddress) { | ||
// Simulate DEFAULT_CHUNK_SIZE txs. Each reads (CONTRACT_ADDRESS, STORAGE_KEY) and writes its tx | ||
// index inside the chunk to the same storage cell. | ||
let scheduler = Arc::new(Scheduler::new(DEFAULT_CHUNK_SIZE)); | ||
let storage_key = storage_key!(27_u8); | ||
let storage_initial_value = stark_felt!(128_u8); | ||
let versioned_state = safe_versioned_state_for_testing(DictStateReader { | ||
storage_view: HashMap::from([((contract_address, storage_key), storage_initial_value)]), | ||
..Default::default() | ||
}); | ||
let mut handles = vec![]; | ||
|
||
let num_threads = 2_u8.pow(5); | ||
for _ in 0..num_threads { | ||
let scheduler = Arc::clone(&scheduler); | ||
let versioned_state = versioned_state.clone(); | ||
let handle = std::thread::spawn(move || { | ||
let mut task = Task::NoTask; | ||
while !scheduler.done() { | ||
match task { | ||
Task::ExecutionTask(tx_index) => { | ||
let versioned_state_proxy = versioned_state.pin_version(tx_index); | ||
let storage_new_value: u8 = tx_index.try_into().unwrap(); | ||
let write_set = StateMaps { | ||
storage: HashMap::from([( | ||
(contract_address, storage_key), | ||
stark_felt!(storage_new_value), | ||
)]), | ||
..Default::default() | ||
}; | ||
versioned_state_proxy | ||
.apply_writes(&write_set, &ContractClassMapping::default()); | ||
scheduler.finish_execution(tx_index); | ||
task = Task::NoTask; | ||
} | ||
Task::ValidationTask(tx_index) => { | ||
let versioned_state_proxy = versioned_state.pin_version(tx_index); | ||
let current_cell_value = versioned_state_proxy | ||
.get_storage_at(contract_address, storage_key) | ||
.unwrap(); | ||
let read_set = StateMaps { | ||
storage: HashMap::from([( | ||
(contract_address, storage_key), | ||
stark_felt!(current_cell_value), | ||
)]), | ||
..Default::default() | ||
}; | ||
let read_set_valid = versioned_state_proxy.validate_read_set(&read_set); | ||
let aborted = !read_set_valid && scheduler.try_validation_abort(tx_index); | ||
if aborted { | ||
versioned_state_proxy.delete_writes(tx_index) | ||
} | ||
task = scheduler.finish_validation(tx_index, aborted); | ||
} | ||
Task::NoTask => { | ||
task = scheduler.next_task(); | ||
} | ||
Task::Done => (), | ||
} | ||
} | ||
}); | ||
handles.push(handle); | ||
} | ||
for handle in handles { | ||
handle.join().unwrap(); | ||
} | ||
|
||
let chunk_size_as_u8: u8 = DEFAULT_CHUNK_SIZE.try_into().unwrap(); | ||
let final_storage_value = chunk_size_as_u8 - 1; | ||
let storage_writes = versioned_state.state().get_writes(DEFAULT_CHUNK_SIZE).storage; | ||
assert_eq!( | ||
*storage_writes.get(&(contract_address, storage_key)).unwrap(), | ||
stark_felt!(final_storage_value) | ||
); | ||
dbg!(storage_writes.get(&(contract_address, storage_key)).unwrap()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters