Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
fix(concurrency): in versioned storage - read the writes of the curre…
Browse files Browse the repository at this point in the history
…nt transaction (#1944)
  • Loading branch information
meship-starkware authored Jun 5, 2024
1 parent 63cb2a8 commit 0abafd7
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 24 deletions.
3 changes: 2 additions & 1 deletion crates/blockifier/src/concurrency/versioned_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ impl<S: StateReader> VersionedState<S> {
if tx_index == 0 {
return true;
}

// Ignore values written by the current transaction.
let tx_index = tx_index - 1;
for (&(contract_address, storage_key), expected_value) in &reads.storage {
let value =
self.storage.read(tx_index, (contract_address, storage_key)).expect(READ_ERR);
Expand Down
4 changes: 2 additions & 2 deletions crates/blockifier/src/concurrency/versioned_state_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ fn test_versioned_state_proxy() {
assert!(
versioned_state_proxys[4].state().declared_contracts.read(4, another_class_hash).unwrap()
);
// Ignore the writes in the current transaction.
// Include the writes in the current transaction.
assert_eq!(
versioned_state_proxys[10].get_class_hash_at(contract_address).unwrap(),
class_hash_v7
class_hash_v10
);
assert_eq!(
versioned_state_proxys[2].get_compiled_class_hash(class_hash).unwrap(),
Expand Down
5 changes: 2 additions & 3 deletions crates/blockifier/src/concurrency/versioned_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ where
V: Clone + Debug,
{
pub fn read(&self, tx_index: TxIndex, key: K) -> Option<V> {
// Ignore the writes in the current transaction (may contain an `ESTIMATE` value). Reading
// the value written in this transaction should be handled by the state.
let value = self.writes.get(&key).and_then(|cell| cell.range(..tx_index).next_back());
// TODO: Ignore `ESTIMATE` values (when added).
let value = self.writes.get(&key).and_then(|cell| cell.range(..=tx_index).next_back());
value.map(|(_, value)| value).or_else(|| self.cached_initial_values.get(&key)).cloned()
}

Expand Down
4 changes: 2 additions & 2 deletions crates/blockifier/src/concurrency/versioned_storage_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ fn test_versioned_storage() {
// Read from the past.
storage.write(2, 10, 78);
assert_eq!(storage.read(1, 10).unwrap(), 31);
// Ignore the value written by the current transaction.
assert_eq!(storage.read(2, 10).unwrap(), 31);
// Include the value written by the current transaction.
assert_eq!(storage.read(2, 10).unwrap(), 78);

// Read uninitialized cell.
assert!(storage.read(1, 100).is_none());
Expand Down
4 changes: 1 addition & 3 deletions crates/blockifier/src/concurrency/worker_logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,7 @@ impl<'a, S: StateReader> WorkerExecutor<'a, S> {
return true;
}

let mut next_tx_versioned_state = self.state.pin_version(tx_index + 1);
let (sequencer_balance_value_low, sequencer_balance_value_high) =
next_tx_versioned_state
let (sequencer_balance_value_low, sequencer_balance_value_high) = tx_versioned_state
.get_fee_token_balance(
tx_context.block_context.block_info.sequencer_address,
tx_context.fee_token_address(),
Expand Down
23 changes: 10 additions & 13 deletions crates/blockifier/src/concurrency/worker_logic_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ fn test_worker_execute() {
// Read a write made by the transaction.
assert_eq!(
safe_versioned_state
.pin_version(tx_index + 1)
.pin_version(tx_index)
.get_storage_at(test_contract_address, storage_key)
.unwrap(),
storage_value
Expand Down Expand Up @@ -176,7 +176,7 @@ fn test_worker_execute() {
worker_executor.execute(tx_index);
// No write was made by the transaction.
assert_eq!(
safe_versioned_state.pin_version(tx_index + 1).get_nonce_at(account_address).unwrap(),
safe_versioned_state.pin_version(tx_index).get_nonce_at(account_address).unwrap(),
nonce!(1_u8)
);
let execution_output = worker_executor.execution_outputs[tx_index].lock().unwrap();
Expand All @@ -195,7 +195,7 @@ fn test_worker_execute() {
worker_executor.execute(tx_index);
// Read a write made by the transaction.
assert_eq!(
safe_versioned_state.pin_version(tx_index + 1).get_nonce_at(account_address).unwrap(),
safe_versioned_state.pin_version(tx_index).get_nonce_at(account_address).unwrap(),
nonce!(2_u8)
);
let execution_output = worker_executor.execution_outputs[tx_index].lock().unwrap();
Expand Down Expand Up @@ -289,7 +289,7 @@ fn test_worker_validate() {
// Verify writes exist in state.
assert_eq!(
safe_versioned_state
.pin_version(tx_index + 1)
.pin_version(tx_index)
.get_storage_at(test_contract_address, storage_key)
.unwrap(),
storage_value0
Expand All @@ -304,7 +304,7 @@ fn test_worker_validate() {
// Verify writes were removed.
assert_eq!(
safe_versioned_state
.pin_version(tx_index + 1)
.pin_version(tx_index)
.get_storage_at(test_contract_address, storage_key)
.unwrap(),
storage_value0
Expand Down Expand Up @@ -346,14 +346,11 @@ pub fn test_add_fee_to_sequencer_balance(
sequencer_balance_low,
sequencer_balance_high,
);
let next_tx_versioned_state = safe_versioned_state.pin_version(tx_index + 1);

let new_sequencer_balance_value_low = next_tx_versioned_state
.get_storage_at(fee_token_address, sequencer_balance_key_low)
.unwrap();
let new_sequencer_balance_value_high = next_tx_versioned_state
.get_storage_at(fee_token_address, sequencer_balance_key_high)
.unwrap();

let new_sequencer_balance_value_low =
tx_versioned_state.get_storage_at(fee_token_address, sequencer_balance_key_low).unwrap();
let new_sequencer_balance_value_high =
tx_versioned_state.get_storage_at(fee_token_address, sequencer_balance_key_high).unwrap();
let expected_balance =
(stark_felt_to_felt(sequencer_balance_low) + Felt252::from(actual_fee.0)).to_biguint();

Expand Down

0 comments on commit 0abafd7

Please sign in to comment.