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

fix: Read before writing when executing the deprecated version of storate_write syscall #1006

Merged
merged 11 commits into from
Sep 5, 2023
Merged
36 changes: 31 additions & 5 deletions src/syscalls/deprecated_business_logic_syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -819,8 +819,9 @@ impl<'a, S: StateReader> DeprecatedBLSyscallHandler<'a, S> {
address: Address,
value: Felt252,
) -> Result<(), SyscallHandlerError> {
self.starknet_storage_state
.write(&felt_to_hash(&address.0), value);
let address = felt_to_hash(&address.0);
self.starknet_storage_state.read(&address)?;
self.starknet_storage_state.write(&address, value);

Ok(())
}
Expand Down Expand Up @@ -920,7 +921,7 @@ mod tests {
state::cached_state::CachedState,
state::in_memory_state_reader::InMemoryStateReader,
syscalls::syscall_handler_errors::SyscallHandlerError,
utils::{test_utils::*, Address},
utils::{felt_to_hash, test_utils::*, Address},
};
use cairo_vm::felt::Felt252;
use cairo_vm::hint_processor::hint_processor_definition::HintProcessorLogic;
Expand All @@ -935,8 +936,8 @@ mod tests {
},
vm::{errors::memory_errors::MemoryError, vm_core::VirtualMachine},
};
use num_traits::Zero;
use std::{any::Any, borrow::Cow, collections::HashMap};
use num_traits::{One, Zero};
use std::{any::Any, borrow::Cow, collections::HashMap, sync::Arc};

type DeprecatedBLSyscallHandler<'a> =
super::DeprecatedBLSyscallHandler<'a, InMemoryStateReader>;
Expand Down Expand Up @@ -1047,4 +1048,29 @@ mod tests {
Ok(value) if value == Felt252::zero()
);
}

#[test]
fn test_storage_write_update_initial_values() {
// Initialize state reader with value
let mut state_reader = InMemoryStateReader::default();
state_reader.address_to_storage.insert(
(Address(Felt252::one()), felt_to_hash(&Felt252::one())),
Felt252::zero(),
);
// Create empty-cached state
let mut state = CachedState::new(Arc::new(state_reader), HashMap::new());
let mut syscall_handler = DeprecatedBLSyscallHandler::default_with(&mut state);
// Perform write
assert!(syscall_handler
.syscall_storage_write(Address(Felt252::one()), Felt252::one())
.is_ok());
// Check that initial values have beed updated in the cache
assert_eq!(
state.cache().storage_initial_values,
HashMap::from([(
(Address(Felt252::one()), felt_to_hash(&Felt252::one())),
Felt252::zero()
)])
)
}
}
Loading