Make sudo use decl_error! (#4369)

* Make sudo use `decl_error`

* copy pasta error

* Update to use `as_str`

* Add doc

* Add back `decl_error`
This commit is contained in:
Shawn Tabrizi
2019-12-17 00:32:40 +01:00
committed by GitHub
parent 8778ca7dc8
commit 7e3872c064
+19 -8
View File
@@ -88,10 +88,11 @@
use sp_std::prelude::*;
use sp_runtime::{
traits::{StaticLookup, Dispatchable}, DispatchError,
traits::{StaticLookup, Dispatchable, ModuleDispatchError}, DispatchError,
};
use frame_support::{
Parameter, decl_module, decl_event, decl_storage, ensure,
Parameter, decl_module, decl_event, decl_storage, decl_error, ensure,
weights::SimpleDispatchInfo,
};
use frame_system::{self as system, ensure_signed};
@@ -107,6 +108,8 @@ pub trait Trait: frame_system::Trait {
decl_module! {
// Simple declaration of the `Module` type. Lets the macro know what it's working on.
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
type Error = Error;
fn deposit_event() = default;
/// Authenticates the sudo key and dispatches a function call with `Root` origin.
@@ -122,8 +125,8 @@ decl_module! {
#[weight = SimpleDispatchInfo::FreeOperational]
fn sudo(origin, proposal: Box<T::Proposal>) {
// This is a public call, so we ensure that the origin is some signed account.
let sender = ensure_signed(origin)?;
ensure!(sender == Self::key(), "only the current sudo key can sudo");
let sender = ensure_signed(origin).map_err(|e| e.as_str())?;
ensure!(sender == Self::key(), Error::RequireSudo);
let res = match proposal.dispatch(frame_system::RawOrigin::Root.into()) {
Ok(_) => true,
@@ -148,8 +151,8 @@ decl_module! {
/// # </weight>
fn set_key(origin, new: <T::Lookup as StaticLookup>::Source) {
// This is a public call, so we ensure that the origin is some signed account.
let sender = ensure_signed(origin)?;
ensure!(sender == Self::key(), "only the current sudo key can change the sudo key");
let sender = ensure_signed(origin).map_err(|e| e.as_str())?;
ensure!(sender == Self::key(), Error::RequireSudo);
let new = T::Lookup::lookup(new)?;
Self::deposit_event(RawEvent::KeyChanged(Self::key()));
@@ -170,8 +173,8 @@ decl_module! {
#[weight = SimpleDispatchInfo::FixedOperational(0)]
fn sudo_as(origin, who: <T::Lookup as StaticLookup>::Source, proposal: Box<T::Proposal>) {
// This is a public call, so we ensure that the origin is some signed account.
let sender = ensure_signed(origin)?;
ensure!(sender == Self::key(), "only the current sudo key can sudo");
let sender = ensure_signed(origin).map_err(|e| e.as_str())?;
ensure!(sender == Self::key(), Error::RequireSudo);
let who = T::Lookup::lookup(who)?;
@@ -206,3 +209,11 @@ decl_storage! {
Key get(fn key) config(): T::AccountId;
}
}
decl_error! {
/// Error for the Sudo module
pub enum Error {
/// Sender must be the Sudo account
RequireSudo,
}
}