Skip to content

Commit

Permalink
feat: implement from/to [u8;LEN] for address type, update tests
Browse files Browse the repository at this point in the history
feat:clean deps and adapt word typing to byte sequence

test:change concurrent test signatures
  • Loading branch information
HatemMn committed Nov 27, 2024
1 parent 06be981 commit 33506ee
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 27 deletions.
7 changes: 2 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"

[features]
bench = []
test-utils = ["tokio"]
test-utils = []

[dependencies]
aes = "0.8.4"
Expand All @@ -15,10 +15,7 @@ rand_core = "0.6.4"
tiny-keccak = { version = "2.0.2", features = ["sha3"] }
xts-mode = "0.5.1"
zeroize = { version = "1.8.1", features = ["derive"] }
tokio = { version = "1.38.0", features = [
"macros",
"rt-multi-thread",
], optional = true }


[dev-dependencies]
criterion = "0.5.1"
Expand Down
12 changes: 12 additions & 0 deletions src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ impl<const LENGTH: usize> Deref for Address<LENGTH> {
}
}

impl<const LENGTH: usize> From<[u8; LENGTH]> for Address<LENGTH> {
fn from(bytes: [u8; LENGTH]) -> Self {
Self(bytes)
}
}

impl<const LENGTH: usize> From<Address<LENGTH>> for [u8; LENGTH] {
fn from(address: Address<LENGTH>) -> Self {
address.0
}
}

impl<const LENGTH: usize> DerefMut for Address<LENGTH> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
Expand Down
6 changes: 3 additions & 3 deletions src/in_memory_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,19 @@ mod tests {

#[tokio::test]
async fn test_sequential_read_write() {
let memory = in_memory_store::InMemory::<u128, u128>::default();
let memory = in_memory_store::InMemory::<[u8; 16], [u8; 16]>::default();
test_single_write_and_read(&memory, rand::random()).await;
}

#[tokio::test]
async fn test_sequential_wrong_guard() {
let memory = in_memory_store::InMemory::<u128, u128>::default();
let memory = in_memory_store::InMemory::<[u8; 16], [u8; 16]>::default();
test_wrong_guard(&memory, rand::random()).await;
}

#[tokio::test]
async fn test_concurrent_read_write() {
let memory = in_memory_store::InMemory::<u128, u128>::default();
let memory = in_memory_store::InMemory::<[u8; 16], [u8; 16]>::default();
test_guarded_write_concurrent(memory, rand::random()).await;
}
}
51 changes: 32 additions & 19 deletions src/test/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ use crate::MemoryADT;
pub async fn test_single_write_and_read<T>(memory: &T, seed: [u8; 32])
where
T: MemoryADT,
T::Address: std::fmt::Debug + PartialEq + From<u128>,
T::Word: std::fmt::Debug + PartialEq + From<u128>,
T::Address: std::fmt::Debug + PartialEq + From<[u8; 16]>,
T::Word: std::fmt::Debug + PartialEq + From<[u8; 16]>,
T::Error: std::error::Error,
{
let mut rng = StdRng::from_seed(seed);

// Test batch_read of random addresses, expected to be all empty at this point
let empty_read_result = memory
.batch_read(vec![
T::Address::from(rng.gen::<u128>()),
T::Address::from(rng.gen::<u128>()),
T::Address::from(rng.gen::<u128>()),
T::Address::from(rng.gen::<u128>().to_be_bytes()),
T::Address::from(rng.gen::<u128>().to_be_bytes()),
T::Address::from(rng.gen::<u128>().to_be_bytes()),
])
.await
.unwrap();
Expand All @@ -29,8 +29,8 @@ where
);

// Generate a random address and a random word that we save
let random_address = rng.gen::<u128>();
let random_word = rng.gen::<u128>();
let random_address = rng.gen::<u128>().to_be_bytes();
let random_word = rng.gen::<u128>().to_be_bytes();

// Write the word to the address
let write_result = memory
Expand Down Expand Up @@ -61,13 +61,13 @@ where
pub async fn test_wrong_guard<T>(memory: &T, seed: [u8; 32])
where
T: MemoryADT,
T::Address: std::fmt::Debug + PartialEq + From<u128>,
T::Word: std::fmt::Debug + PartialEq + From<u128>,
T::Address: std::fmt::Debug + PartialEq + From<[u8; 16]>,
T::Word: std::fmt::Debug + PartialEq + From<[u8; 16]>,
T::Error: std::error::Error,
{
let mut rng = StdRng::from_seed(seed);
let random_address = rng.gen::<u128>();
let word_to_write = rng.gen::<u128>();
let random_address = rng.gen::<u128>().to_be_bytes();
let word_to_write = rng.gen::<u128>().to_be_bytes();

// Write something to a random address
memory
Expand All @@ -87,7 +87,7 @@ where
(T::Address::from(random_address), None),
vec![(
T::Address::from(random_address),
T::Word::from(rng.gen::<u128>()),
T::Word::from(rng.gen::<u128>().to_be_bytes()),
)],
)
.await
Expand Down Expand Up @@ -120,13 +120,15 @@ where

pub async fn test_guarded_write_concurrent<T>(memory: T, seed: [u8; 32])
where
T: MemoryADT<Address = u128, Word = u128> + Send + 'static + Clone,
T: MemoryADT + Send + 'static + Clone,
T::Address: std::fmt::Debug + PartialEq + From<[u8; 16]> + Send,
T::Word: std::fmt::Debug + PartialEq + From<[u8; 16]> + Into<[u8; 16]> + Send + Clone + Default,
T::Error: std::error::Error,
{
{
const N: usize = 1000; // number of threads
let mut rng = StdRng::from_seed(seed);
let a = rng.gen::<u128>(); // Random address for a counter
let a = rng.gen::<u128>().to_be_bytes(); // Random address for a counter

let handles: Vec<_> = (0..N)
.map(|_| {
Expand All @@ -136,7 +138,17 @@ where
loop {
// Try to increment
let cur_cnt = mem
.guarded_write((a, old_cnt), vec![(a, old_cnt.unwrap_or_default() + 1)])
.guarded_write(
(a.into(), old_cnt.clone()),
vec![(
a.into(),
(u128::from_be_bytes(
old_cnt.clone().unwrap_or_default().into(),
) + 1)
.to_be_bytes()
.into(),
)],
)
.await
.unwrap();
if cur_cnt == old_cnt {
Expand All @@ -154,13 +166,14 @@ where
handle.join().unwrap().await;
}

let final_count =
memory.batch_read(vec![a]).await.unwrap()[0].expect("Counter should exist");
let final_count = memory.batch_read(vec![a.into()]).await.unwrap()[0]
.clone()
.expect("Counter should exist");

assert_eq!(
final_count, N as u128,
u128::from_be_bytes(final_count.clone().into()), N as u128,
"test_guarded_write_concurrent failed. Expected the counter to be at {:?}, found {:?}.\nDebug seed : {:?}.",
N as u128, final_count, seed
N as u128, u128::from_be_bytes(final_count.into()), seed
);
}
}

0 comments on commit 33506ee

Please sign in to comment.