Skip to content

Commit

Permalink
Merge pull request #85 from lambdaclass/concurrent-test
Browse files Browse the repository at this point in the history
Add concurrency test
  • Loading branch information
JulianGCalderon authored Oct 29, 2024
2 parents 38d05a8 + 854d30c commit 3b1b912
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions rpc-state-reader/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ pub fn fetch_block_context(reader: &RpcStateReader) -> BlockContext {

#[cfg(test)]
mod tests {
use std::thread;

use blockifier::{
execution::call_info::CallInfo,
state::cached_state::StateChangesCount,
Expand Down Expand Up @@ -977,6 +979,56 @@ mod tests {
assert_eq!(starknet_rsc, starknet_resources);
}

#[test_case(
"0x0310c46edc795c82c71f600159fa9e6c6540cb294df9d156f685bfe62b31a5f4",
662249,
RpcChain::MainNet
)]
#[test_case(
"0x066e1f01420d8e433f6ef64309adb1a830e5af0ea67e3d935de273ca57b3ae5e",
662252,
RpcChain::MainNet
)]
/// Ideally contract executions should be independent from one another.
/// In practice we use the same loaded dynamic shared library for each
/// execution of the same contract, for performance reasons. This means that
/// if a contract relies on global variables, those will be shared between
/// different executions of the same contract. This test executes a single
/// transaction (therefore, the same contracts) multiple times at the same
/// time, helping to uncover any possible concurrency bug that we may have
fn test_concurrency(tx_hash: &str, block_number: u64, chain: RpcChain) {
let reader = RpcStateReader::new(RpcChain::MainNet, BlockNumber(block_number - 1));

let context = fetch_block_context(&reader);
let tx_hash = TransactionHash(felt!(tx_hash));
let tx = reader.get_transaction(&tx_hash).unwrap();

let mut handles = Vec::new();

for _ in 0..20 {
let context = context.clone();
let tx = tx.clone();

handles.push(thread::spawn(move || {
let reader = RpcStateReader::new(chain, BlockNumber(block_number - 1));
let mut cache = CachedState::new(reader);

let execution_info =
execute_tx_with_blockifier(&mut cache, context, tx, tx_hash).unwrap();

assert!(
!execution_info.is_reverted(),
"{:?}",
execution_info.revert_error.unwrap_or_default()
)
}));
}

for h in handles {
h.join().unwrap()
}
}

// Impl conversion for easier checking against RPC data
impl From<&CallInfo> for RpcCallInfo {
fn from(value: &CallInfo) -> Self {
Expand Down

0 comments on commit 3b1b912

Please sign in to comment.