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
62 changes: 58 additions & 4 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,34 @@
/// });
/// ```
#[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 wrongful storage changes.",
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 +76,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 Down Expand Up @@ -111,4 +135,34 @@ 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_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 wrongful storage changes.")]
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");
});
}
}