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 all commits
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
}
}
10 changes: 10 additions & 0 deletions crates/storage/src/collections/binary_heap/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,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