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

Add custom error message for StorageNoopGuard #1727

Merged
merged 13 commits into from
Sep 27, 2023
82 changes: 76 additions & 6 deletions substrate/frame/support/src/storage/storage_noop_guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,40 @@
/// });
/// ```
#[must_use]
pub struct StorageNoopGuard(sp_std::vec::Vec<u8>);
pub struct StorageNoopGuard {
storage_root: sp_std::vec::Vec<u8>,
error_message: &'static str,
seadanda marked this conversation as resolved.
Show resolved Hide resolved
}

impl Default for StorageNoopGuard {
fn default() -> Self {
Self(sp_io::storage::root(sp_runtime::StateVersion::V1))
Self {
storage_root: sp_io::storage::root(sp_runtime::StateVersion::V1),
error_message: "`StorageNoopGuard` detected an attempted storage change.",
}
}
}

impl From<&'static str> for StorageNoopGuard {
fn from(error_message: &'static str) -> Self {
seadanda marked this conversation as resolved.
Show resolved Hide resolved
StorageNoopGuard { storage_root: sp_std::vec::Vec::new(), error_message }
seadanda marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl StorageNoopGuard {
/// Alias to `default()`.
pub fn new() -> Self {
Self::default()
}

/// Creates a new [`StorageNoopGuard`] with a custom error message.
pub fn from_error_message(error_message: &'static str) -> Self {
Self { storage_root: sp_io::storage::root(sp_runtime::StateVersion::V1), error_message }
}

/// Sets a custom error message for a [`StorageNoopGuard`].
pub fn set_error_message(&mut self, error_message: &'static str) {
self.error_message = error_message;
}
}

Expand All @@ -53,8 +82,9 @@ impl Drop for StorageNoopGuard {
}
assert_eq!(
sp_io::storage::root(sp_runtime::StateVersion::V1),
self.0,
"StorageNoopGuard detected wrongful storage changes.",
self.storage_root,
"{}",
self.error_message,
);
}
}
Expand All @@ -65,7 +95,7 @@ mod tests {
use sp_io::TestExternalities;

#[test]
#[should_panic(expected = "StorageNoopGuard detected wrongful storage changes.")]
#[should_panic(expected = "`StorageNoopGuard` detected an attempted storage change.")]
fn storage_noop_guard_panics_on_changed() {
TestExternalities::default().execute_with(|| {
let _guard = StorageNoopGuard::default();
Expand All @@ -83,7 +113,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "StorageNoopGuard detected wrongful storage changes.")]
#[should_panic(expected = "`StorageNoopGuard` detected an attempted storage change.")]
fn storage_noop_guard_panics_on_early_drop() {
TestExternalities::default().execute_with(|| {
let guard = StorageNoopGuard::default();
Expand Down Expand Up @@ -111,4 +141,44 @@ mod tests {
panic!("Something else");
});
}

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

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

#[test]
#[should_panic(expected = "`StorageNoopGuard` found unexpected storage changes.")]
fn storage_noop_guard_panics_with_set_error_message() {
TestExternalities::default().execute_with(|| {
let mut guard = StorageNoopGuard::default();
guard.set_error_message("`StorageNoopGuard` found unexpected storage changes.");
frame_support::storage::unhashed::put(b"key", b"value");
});
}

#[test]
#[should_panic(expected = "`StorageNoopGuard` detected an attempted storage change.")]
fn storage_noop_guard_panics_new_alias() {
seadanda marked this conversation as resolved.
Show resolved Hide resolved
TestExternalities::default().execute_with(|| {
let _guard = StorageNoopGuard::new();
frame_support::storage::unhashed::put(b"key", b"value");
});
}
}