diff --git a/substrate/frame/sudo/src/lib.rs b/substrate/frame/sudo/src/lib.rs index 3a80c2e946..b7486edf31 100644 --- a/substrate/frame/sudo/src/lib.rs +++ b/substrate/frame/sudo/src/lib.rs @@ -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 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) { // 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! { /// # fn set_key(origin, new: ::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: ::Source, proposal: Box) { // 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, + } +}