Commit 6b6e523d authored by Hero Bird's avatar Hero Bird
Browse files

[pdsl_core] Rejigged cells, chunks and value with respect to their new_unchecked constructor

parent fe3ac781
...@@ -50,7 +50,7 @@ impl RawCell { ...@@ -50,7 +50,7 @@ impl RawCell {
/// ///
/// This is unsafe since it does not check if the associated /// This is unsafe since it does not check if the associated
/// contract storage does not alias with other accesses. /// contract storage does not alias with other accesses.
pub unsafe fn new_unchecked(key: Key) -> Self { unsafe fn new_unchecked(key: Key) -> Self {
Self{ Self{
key: key, key: key,
non_clone: NonCloneMarker::default() non_clone: NonCloneMarker::default()
...@@ -67,10 +67,7 @@ impl RawCell { ...@@ -67,10 +67,7 @@ impl RawCell {
where where
A: Allocator A: Allocator
{ {
Self{ Self::new_unchecked(alloc.alloc(1))
key: alloc.alloc(1),
non_clone: Default::default(),
}
} }
} }
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
use crate::{ use crate::{
storage::{ storage::{
Key,
cell::TypedCell, cell::TypedCell,
Allocator, Allocator,
}, },
...@@ -151,19 +150,6 @@ impl<T> parity_codec::Decode for SyncCell<T> { ...@@ -151,19 +150,6 @@ impl<T> parity_codec::Decode for SyncCell<T> {
} }
impl<T> SyncCell<T> { impl<T> SyncCell<T> {
/// Creates a new copy cell for the given key.
///
/// # Safety
///
/// This is unsafe since it does not check if the associated
/// contract storage does not alias with other accesses.
pub unsafe fn new_unchecked(key: Key) -> Self {
Self{
cell: TypedCell::new_unchecked(key),
cache: Default::default(),
}
}
/// Allocates a new sync cell using the given storage allocator. /// Allocates a new sync cell using the given storage allocator.
/// ///
/// # Safety /// # Safety
...@@ -258,18 +244,26 @@ where ...@@ -258,18 +244,26 @@ where
#[cfg(all(test, feature = "test-env"))] #[cfg(all(test, feature = "test-env"))]
mod tests { mod tests {
use super::*; use super::*;
use crate::storage::Key;
use crate::{ use crate::{
test_utils::run_test, test_utils::run_test,
env::TestEnv, env::TestEnv,
}; };
fn dummy_cell() -> SyncCell<i32> {
unsafe {
let mut alloc = crate::storage::alloc::ForwardAlloc::from_raw_parts(
Key([0x0; 32])
);
SyncCell::new_using_alloc(&mut alloc)
}
}
#[test] #[test]
fn simple() { fn simple() {
run_test(|| { run_test(|| {
let mut cell: SyncCell<i32> = unsafe { let mut cell = dummy_cell();
SyncCell::new_unchecked(Key([0x42; 32]))
};
assert_eq!(cell.get(), None); assert_eq!(cell.get(), None);
cell.set(5); cell.set(5);
assert_eq!(cell.get(), Some(&5)); assert_eq!(cell.get(), Some(&5));
...@@ -283,9 +277,7 @@ mod tests { ...@@ -283,9 +277,7 @@ mod tests {
#[test] #[test]
fn count_reads() { fn count_reads() {
run_test(|| { run_test(|| {
let cell: SyncCell<i32> = unsafe { let mut cell = dummy_cell();
SyncCell::new_unchecked(Key([0x42; 32]))
};
assert_eq!(TestEnv::total_reads(), 0); assert_eq!(TestEnv::total_reads(), 0);
cell.get(); cell.get();
assert_eq!(TestEnv::total_reads(), 1); assert_eq!(TestEnv::total_reads(), 1);
...@@ -298,9 +290,7 @@ mod tests { ...@@ -298,9 +290,7 @@ mod tests {
#[test] #[test]
fn count_writes() { fn count_writes() {
run_test(|| { run_test(|| {
let mut cell: SyncCell<i32> = unsafe { let mut cell = dummy_cell();
SyncCell::new_unchecked(Key([0x42; 32]))
};
assert_eq!(TestEnv::total_writes(), 0); assert_eq!(TestEnv::total_writes(), 0);
cell.set(1); cell.set(1);
assert_eq!(TestEnv::total_writes(), 1); assert_eq!(TestEnv::total_writes(), 1);
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
use crate::{ use crate::{
storage::{ storage::{
Key,
NonCloneMarker, NonCloneMarker,
cell::RawCell, cell::RawCell,
Allocator, Allocator,
...@@ -58,19 +57,6 @@ impl<T> parity_codec::Decode for TypedCell<T> { ...@@ -58,19 +57,6 @@ impl<T> parity_codec::Decode for TypedCell<T> {
} }
impl<T> TypedCell<T> { impl<T> TypedCell<T> {
/// Creates a new typed cell for the given key.
///
/// # Safety
///
/// This is unsafe since it does not check if the associated
/// contract storage does not alias with other accesses.
pub unsafe fn new_unchecked(key: Key) -> Self {
Self{
cell: RawCell::new_unchecked(key),
non_clone: NonCloneMarker::default()
}
}
/// Allocates a new typed cell using the given storage allocator. /// Allocates a new typed cell using the given storage allocator.
/// ///
/// # Safety /// # Safety
...@@ -116,18 +102,26 @@ where ...@@ -116,18 +102,26 @@ where
#[cfg(all(test, feature = "test-env"))] #[cfg(all(test, feature = "test-env"))]
mod tests { mod tests {
use super::*; use super::*;
use crate::storage::Key;
use crate::{ use crate::{
test_utils::run_test, test_utils::run_test,
env::TestEnv, env::TestEnv,
}; };
fn dummy_cell() -> TypedCell<i32> {
unsafe {
let mut alloc = crate::storage::alloc::ForwardAlloc::from_raw_parts(
Key([0x0; 32])
);
TypedCell::new_using_alloc(&mut alloc)
}
}
#[test] #[test]
fn simple() { fn simple() {
run_test(|| { run_test(|| {
let mut cell: TypedCell<i32> = unsafe { let mut cell = dummy_cell();
TypedCell::new_unchecked(Key([0x42; 32]))
};
assert_eq!(cell.load(), None); assert_eq!(cell.load(), None);
cell.store(&5); cell.store(&5);
assert_eq!(cell.load(), Some(5)); assert_eq!(cell.load(), Some(5));
...@@ -139,9 +133,7 @@ mod tests { ...@@ -139,9 +133,7 @@ mod tests {
#[test] #[test]
fn count_reads() { fn count_reads() {
run_test(|| { run_test(|| {
let cell: TypedCell<i32> = unsafe { let mut cell = dummy_cell();
TypedCell::new_unchecked(Key([0x42; 32]))
};
assert_eq!(TestEnv::total_reads(), 0); assert_eq!(TestEnv::total_reads(), 0);
cell.load(); cell.load();
assert_eq!(TestEnv::total_reads(), 1); assert_eq!(TestEnv::total_reads(), 1);
...@@ -154,9 +146,7 @@ mod tests { ...@@ -154,9 +146,7 @@ mod tests {
#[test] #[test]
fn count_writes() { fn count_writes() {
run_test(|| { run_test(|| {
let mut cell: TypedCell<i32> = unsafe { let mut cell = dummy_cell();
TypedCell::new_unchecked(Key([0x42; 32]))
};
assert_eq!(TestEnv::total_writes(), 0); assert_eq!(TestEnv::total_writes(), 0);
cell.store(&1); cell.store(&1);
assert_eq!(TestEnv::total_writes(), 1); assert_eq!(TestEnv::total_writes(), 1);
......
...@@ -104,7 +104,7 @@ impl RawChunk { ...@@ -104,7 +104,7 @@ impl RawChunk {
/// - ... it does not check if the associated /// - ... it does not check if the associated
/// contract storage does not alias with other accesses. /// contract storage does not alias with other accesses.
/// - ... it does not check if given capacity is non zero. /// - ... it does not check if given capacity is non zero.
pub unsafe fn new_unchecked(key: Key) -> Self { unsafe fn new_unchecked(key: Key) -> Self {
Self{ Self{
key, key,
non_clone: Default::default(), non_clone: Default::default(),
...@@ -115,16 +115,13 @@ impl RawChunk { ...@@ -115,16 +115,13 @@ impl RawChunk {
/// ///
/// # Safety /// # Safety
/// ///
/// The is unsafe because it does not check if the associated storage /// This is unsafe because it does not check if the associated storage
/// does not alias with storage allocated by other storage allocators. /// does not alias with storage allocated by other storage allocators.
pub unsafe fn new_using_alloc<A>(alloc: &mut A) -> Self pub unsafe fn new_using_alloc<A>(alloc: &mut A) -> Self
where where
A: Allocator A: Allocator
{ {
Self{ Self::new_unchecked(alloc.alloc(u32::max_value()))
key: alloc.alloc(u32::max_value()),
non_clone: Default::default(),
}
} }
/// Returns the unterlying key to the cells. /// Returns the unterlying key to the cells.
......
...@@ -292,21 +292,6 @@ impl<T> parity_codec::Decode for SyncChunk<T> { ...@@ -292,21 +292,6 @@ impl<T> parity_codec::Decode for SyncChunk<T> {
} }
impl<T> SyncChunk<T> { impl<T> SyncChunk<T> {
/// Creates a new mutable cell chunk for the given key and capacity.
///
/// # Safety
///
/// This is unsafe because ..
/// - .. it does not check if the associated
/// contract storage does not alias with other accesses.
/// - .. it does not check if given capacity is non zero.
pub unsafe fn new_unchecked(key: Key) -> Self {
Self{
chunk: TypedChunk::new_unchecked(key),
elems: Cache::default(),
}
}
/// Allocates a new sync cell chunk using the given storage allocator. /// Allocates a new sync cell chunk using the given storage allocator.
/// ///
/// # Safety /// # Safety
...@@ -460,14 +445,21 @@ mod tests { ...@@ -460,14 +445,21 @@ mod tests {
env::TestEnv, env::TestEnv,
}; };
fn dummy_chunk() -> SyncChunk<u32> {
unsafe {
let mut alloc = crate::storage::alloc::ForwardAlloc::from_raw_parts(
Key([0x0; 32])
);
SyncChunk::new_using_alloc(&mut alloc)
}
}
#[test] #[test]
fn simple() { fn simple() {
run_test(|| { run_test(|| {
const TEST_LEN: u32 = 5; const TEST_LEN: u32 = 5;
let mut chunk = unsafe { let mut chunk = dummy_chunk();
SyncChunk::new_unchecked(Key([0x42; 32]))
};
// Invariants after initialization // Invariants after initialization
for i in 0..TEST_LEN { for i in 0..TEST_LEN {
...@@ -495,9 +487,7 @@ mod tests { ...@@ -495,9 +487,7 @@ mod tests {
run_test(|| { run_test(|| {
const TEST_LEN: u32 = 5; const TEST_LEN: u32 = 5;
let mut chunk = unsafe { let mut chunk = dummy_chunk();
SyncChunk::new_unchecked(Key([0x42; 32]))
};
// Reads and writes after init. // Reads and writes after init.
assert_eq!(TestEnv::total_reads(), 0); assert_eq!(TestEnv::total_reads(), 0);
...@@ -544,9 +534,7 @@ mod tests { ...@@ -544,9 +534,7 @@ mod tests {
#[test] #[test]
fn replace() { fn replace() {
run_test(|| { run_test(|| {
let mut chunk = unsafe { let mut chunk = dummy_chunk();
SyncChunk::new_unchecked(Key([0x42; 32]))
};
// Replace some with none. // Replace some with none.
assert_eq!(chunk.replace(0, 42), None); assert_eq!(chunk.replace(0, 42), None);
...@@ -562,9 +550,7 @@ mod tests { ...@@ -562,9 +550,7 @@ mod tests {
#[test] #[test]
fn remove() { fn remove() {
run_test(|| { run_test(|| {
let mut chunk = unsafe { let mut chunk = dummy_chunk();
SyncChunk::new_unchecked(Key([0x42; 32]))
};
// Remove at none. // Remove at none.
assert_eq!(chunk.remove(0), None); assert_eq!(chunk.remove(0), None);
......
...@@ -123,21 +123,6 @@ impl<T> parity_codec::Decode for TypedChunk<T> { ...@@ -123,21 +123,6 @@ impl<T> parity_codec::Decode for TypedChunk<T> {
} }
impl<T> TypedChunk<T> { impl<T> TypedChunk<T> {
/// Creates a new typed cell chunk for the given key and length.
///
/// # Safety
///
/// This is unsafe because ..
/// - .. it does not check if the associated
/// contract storage does not alias with other accesses.
/// - .. it does not check if given length is non zero.
pub unsafe fn new_unchecked(key: Key) -> Self {
Self{
chunk: RawChunk::new_unchecked(key),
non_clone: Default::default(),
}
}
/// Allocates a new typed cell chunk using the given storage allocator. /// Allocates a new typed cell chunk using the given storage allocator.
/// ///
/// # Safety /// # Safety
...@@ -226,7 +211,10 @@ mod tests { ...@@ -226,7 +211,10 @@ mod tests {
const TEST_LEN: u32 = 5; const TEST_LEN: u32 = 5;
let mut chunk = unsafe { let mut chunk = unsafe {
TypedChunk::new_unchecked(Key([0x42; 32])) let mut alloc = crate::storage::alloc::ForwardAlloc::from_raw_parts(
Key([0x0; 32])
);
TypedChunk::new_using_alloc(&mut alloc)
}; };
// Invariants after initialization // Invariants after initialization
...@@ -254,7 +242,10 @@ mod tests { ...@@ -254,7 +242,10 @@ mod tests {
const TEST_LEN: u32 = 5; const TEST_LEN: u32 = 5;
let mut chunk = unsafe { let mut chunk = unsafe {
TypedChunk::new_unchecked(Key([0x42; 32])) let mut alloc = crate::storage::alloc::ForwardAlloc::from_raw_parts(
Key([0x0; 32])
);
TypedChunk::new_using_alloc(&mut alloc)
}; };
// Reads and writes after init. // Reads and writes after init.
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
use crate::{ use crate::{
storage::{ storage::{
self, self,
Key,
cell::SyncCell, cell::SyncCell,
}, },
}; };
...@@ -48,25 +47,6 @@ pub struct Value<T> { ...@@ -48,25 +47,6 @@ pub struct Value<T> {
cell: SyncCell<T>, cell: SyncCell<T>,
} }
impl<T> Value<T> {
/// Creates a new storage value for the given key.
///
/// # Safety
///
/// This is an inherently unsafe operation since it does not check
/// for the storage values's invariances, such as
///
/// - Is the storage region determined by the given key aliasing?
/// - Is the storage region correctly formatted to be used as storage value?
///
/// Users should not use this routine directly if possible.
pub unsafe fn from_raw_parts(key: Key) -> Self {
Self{
cell: SyncCell::new_unchecked(key)
}
}
}
impl<T> Value<T> impl<T> Value<T>
where where
T: parity_codec::Codec + Default T: parity_codec::Codec + Default
...@@ -341,6 +321,7 @@ where ...@@ -341,6 +321,7 @@ where
#[cfg(all(test, feature = "test-env"))] #[cfg(all(test, feature = "test-env"))]
mod tests { mod tests {
use super::*; use super::*;
use crate::storage::Key;
use crate::{ use crate::{
test_utils::run_test, test_utils::run_test,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment