Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[storage] Implement Drop for Pack #600

Merged
merged 4 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/primitives/src/key_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ impl KeyPtr {
self.key += old_shift;
&self.key
}

/// Returns the underlying offset key.
pub fn key(&self) -> Key {
self.key
cmichi marked this conversation as resolved.
Show resolved Hide resolved
}
}
10 changes: 10 additions & 0 deletions crates/storage/src/collections/binary_heap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use crate::{
};

pub use children_vec::Iter;
use ink_primitives::Key;
pub use reverse::Reverse;

/// A priority queue implemented with a binary heap.
Expand All @@ -50,6 +51,14 @@ where
{
/// The individual elements of the heap.
elements: ChildrenVec<T>,
/// The offset key for the N cells.
///
/// If the lazy chunk has been initialized during contract initialization
/// the key will be `None` since there won't be a storage region associated
/// to the lazy chunk which prevents it from lazily loading elements. This,
/// however, is only checked at contract runtime. We might incorporate
/// compile-time checks for this particular use case later on.
key: Option<Key>,
cmichi marked this conversation as resolved.
Show resolved Hide resolved
}

impl<T> BinaryHeap<T>
Expand All @@ -60,6 +69,7 @@ where
pub fn new() -> Self {
Self {
elements: ChildrenVec::new(),
key: None,
}
}

Expand Down
2 changes: 2 additions & 0 deletions crates/storage/src/collections/binary_heap/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ where
const FOOTPRINT: u64 = <ChildrenVec<T> as SpreadLayout>::FOOTPRINT;

fn pull_spread(ptr: &mut KeyPtr) -> Self {
let key = ptr.key();
Self {
elements: SpreadLayout::pull_spread(ptr),
key: Some(key),
}
}

Expand Down
15 changes: 14 additions & 1 deletion crates/storage/src/collections/binary_heap/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,10 @@ fn spread_layout_push_pull_works() -> ink_env::Result<()> {
// both instances are equal:
let heap2 =
<BinaryHeap<u8> as SpreadLayout>::pull_spread(&mut KeyPtr::from(root_key));
assert_eq!(heap1, heap2);
// we compare only the `elements` of the heap, since for `heap1` the `heap1.key`
// is `None`, since no pull was executed on this object yet. `heap2.key` will be
// `Some(root_key)` though, since we just pulled it from storage.
assert_eq!(heap1.elements, heap2.elements);
Ok(())
})
}
Expand Down Expand Up @@ -255,6 +258,16 @@ fn drop_works() {

assert!(setup_result.is_ok(), "setup should not panic");

let contract_id = ink_env::test::get_current_contract_account_id::<
ink_env::DefaultEnvironment,
>()
.expect("Cannot get contract id");
let used_cells = ink_env::test::count_used_storage_cells::<
ink_env::DefaultEnvironment,
>(&contract_id)
.expect("Used cells must be returned");
assert_eq!(used_cells, 0);

let _ =
<BinaryHeap<u8> as SpreadLayout>::pull_spread(&mut KeyPtr::from(root_key));
Ok(())
Expand Down
35 changes: 35 additions & 0 deletions crates/storage/src/collections/hashmap/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,38 @@ fn storage_is_cleared_completely_after_pull_lazy() {
})
.unwrap()
}

#[test]
#[should_panic(expected = "storage entry was empty")]
fn drop_works() {
ink_env::test::run_test::<ink_env::DefaultEnvironment, _>(|_| {
let root_key = Key::from([0x42; 32]);

// if the setup panics it should not cause the test to pass
let setup_result = std::panic::catch_unwind(|| {
let hmap = filled_hmap();
SpreadLayout::push_spread(&hmap, &mut KeyPtr::from(root_key));
let _ = <StorageHashMap<u8, i32> as SpreadLayout>::pull_spread(
&mut KeyPtr::from(root_key),
);
// hmap is dropped which should clear the cells
});
assert!(setup_result.is_ok(), "setup should not panic");

let contract_id = ink_env::test::get_current_contract_account_id::<
ink_env::DefaultEnvironment,
>()
.expect("Cannot get contract id");
let used_cells = ink_env::test::count_used_storage_cells::<
ink_env::DefaultEnvironment,
>(&contract_id)
.expect("used cells must be returned");
assert_eq!(used_cells, 0);

let _ = <StorageHashMap<u8, i32> as SpreadLayout>::pull_spread(
&mut KeyPtr::from(root_key),
);
Ok(())
})
.unwrap()
}
34 changes: 34 additions & 0 deletions crates/storage/src/collections/stash/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -781,3 +781,37 @@ fn storage_is_cleared_completely_after_pull_lazy() {
})
.unwrap()
}

#[test]
#[should_panic(expected = "storage entry was empty")]
fn drop_works() {
ink_env::test::run_test::<ink_env::DefaultEnvironment, _>(|_| {
let root_key = Key::from([0x42; 32]);

// if the setup panics it should not cause the test to pass
let setup_result = std::panic::catch_unwind(|| {
let stash = create_holey_stash();
SpreadLayout::push_spread(&stash, &mut KeyPtr::from(root_key));
let _ = <StorageStash<u8> as SpreadLayout>::pull_spread(&mut KeyPtr::from(
root_key,
));
// stash is dropped which should clear the cells
});
assert!(setup_result.is_ok(), "setup should not panic");

let contract_id = ink_env::test::get_current_contract_account_id::<
ink_env::DefaultEnvironment,
>()
.expect("Cannot get contract id");
let used_cells = ink_env::test::count_used_storage_cells::<
ink_env::DefaultEnvironment,
>(&contract_id)
.expect("used cells must be returned");
assert_eq!(used_cells, 0);

let _ =
<StorageStash<u8> as SpreadLayout>::pull_spread(&mut KeyPtr::from(root_key));
Ok(())
})
.unwrap()
}
Loading