Skip to content

Commit

Permalink
Add StorageNoopGuard (paritytech#12163)
Browse files Browse the repository at this point in the history
* Add StorageNoopGuard

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fix import

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fix feature gate

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fix feature gate

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Use sp-std

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
  • Loading branch information
ggwpez authored and ark0f committed Feb 27, 2023
1 parent 295f864 commit 29b10fe
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
2 changes: 2 additions & 0 deletions frame/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ pub mod unsigned {
};
}

#[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
pub use self::storage::storage_noop_guard::StorageNoopGuard;
pub use self::{
dispatch::{Callable, Parameter},
hash::{
Expand Down
1 change: 1 addition & 0 deletions frame/support/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub mod child;
pub mod generator;
pub mod hashed;
pub mod migration;
pub mod storage_noop_guard;
pub mod transactional;
pub mod types;
pub mod unhashed;
Expand Down
114 changes: 114 additions & 0 deletions frame/support/src/storage/storage_noop_guard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// This file is part of Substrate.

// Copyright (C) 2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Feature gated since it can panic.
#![cfg(any(feature = "std", feature = "runtime-benchmarks", test))]

//! Contains the [`crate::StorageNoopGuard`] for conveniently asserting
//! that no storage mutation has been made by a whole code block.

/// Asserts that no storage changes took place between con- and destruction of [`Self`].
///
/// This is easier than wrapping the whole code-block inside a `assert_storage_noop!`.
///
/// # Example
///
/// ```should_panic
/// use frame_support::{StorageNoopGuard, storage::unhashed::put};
///
/// sp_io::TestExternalities::default().execute_with(|| {
/// let _guard = frame_support::StorageNoopGuard::default();
/// put(b"key", b"value");
/// // Panics since there are storage changes.
/// });
/// ```
#[must_use]
pub struct StorageNoopGuard(sp_std::vec::Vec<u8>);

impl Default for StorageNoopGuard {
fn default() -> Self {
Self(frame_support::storage_root(frame_support::StateVersion::V1))
}
}

impl Drop for StorageNoopGuard {
fn drop(&mut self) {
// No need to double panic, eg. inside a test assertion failure.
if sp_std::thread::panicking() {
return
}
assert_eq!(
frame_support::storage_root(frame_support::StateVersion::V1),
self.0,
"StorageNoopGuard detected wrongful storage changes.",
);
}
}

#[cfg(test)]
mod tests {
use super::*;
use sp_io::TestExternalities;

#[test]
#[should_panic(expected = "StorageNoopGuard detected wrongful storage changes.")]
fn storage_noop_guard_panics_on_changed() {
TestExternalities::default().execute_with(|| {
let _guard = StorageNoopGuard::default();
frame_support::storage::unhashed::put(b"key", b"value");
});
}

#[test]
fn storage_noop_guard_works_on_unchanged() {
TestExternalities::default().execute_with(|| {
let _guard = StorageNoopGuard::default();
frame_support::storage::unhashed::put(b"key", b"value");
frame_support::storage::unhashed::kill(b"key");
});
}

#[test]
#[should_panic(expected = "StorageNoopGuard detected wrongful storage changes.")]
fn storage_noop_guard_panics_on_early_drop() {
TestExternalities::default().execute_with(|| {
let guard = StorageNoopGuard::default();
frame_support::storage::unhashed::put(b"key", b"value");
sp_std::mem::drop(guard);
frame_support::storage::unhashed::kill(b"key");
});
}

#[test]
fn storage_noop_guard_works_on_changed_forget() {
TestExternalities::default().execute_with(|| {
let guard = StorageNoopGuard::default();
frame_support::storage::unhashed::put(b"key", b"value");
sp_std::mem::forget(guard);
});
}

#[test]
#[should_panic(expected = "Something else")]
fn storage_noop_guard_does_not_double_panic() {
TestExternalities::default().execute_with(|| {
let _guard = StorageNoopGuard::default();
frame_support::storage::unhashed::put(b"key", b"value");
panic!("Something else");
});
}
}

0 comments on commit 29b10fe

Please sign in to comment.