Skip to content

Commit

Permalink
pallet-sudo: Accept Root origin as valid sudo (#2783)
Browse files Browse the repository at this point in the history
This changes `pallet-sudo` to also accept `Root` origin for
`ensure_sudo`. This can be useful for parachains who allow the relay
chain to have superuser rights to setup the sudo pallet for example.
  • Loading branch information
bkchr authored Dec 22, 2023
1 parent 46dd4b8 commit 96bec7a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
12 changes: 12 additions & 0 deletions prdoc/pr_2783.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
title: "Accept Root origin as valid sudo"

doc:
- audience: Runtime User
description: |
Dispatchables of `pallet-sudo` will now also accept the `Root` origin
as valid `sudo` origin. This enhancement is useful for parachains that
allow the relay chain as a superuser. It enables the relay chain to send
an XCM message to initialize the sudo key.

crates:
- name: "pallet-sudo"
14 changes: 9 additions & 5 deletions substrate/frame/sudo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,16 @@ pub mod pallet {
impl<T: Config> Pallet<T> {
/// Ensure that the caller is the sudo key.
pub(crate) fn ensure_sudo(origin: OriginFor<T>) -> DispatchResult {
let sender = ensure_signed(origin)?;

if Self::key().map_or(false, |k| k == sender) {
Ok(())
let sender = ensure_signed_or_root(origin)?;

if let Some(sender) = sender {
if Self::key().map_or(false, |k| k == sender) {
Ok(())
} else {
Err(Error::<T>::RequireSudo.into())
}
} else {
Err(Error::<T>::RequireSudo.into())
Ok(())
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions substrate/frame/sudo/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,18 @@ fn remove_key_works() {
});
}

#[test]
fn using_root_origin_works() {
new_test_ext(1).execute_with(|| {
assert_ok!(Sudo::remove_key(RuntimeOrigin::root()));
assert!(Sudo::key().is_none());
System::assert_has_event(TestEvent::Sudo(Event::KeyRemoved {}));

assert_ok!(Sudo::set_key(RuntimeOrigin::root(), 1));
assert_eq!(Some(1), Sudo::key());
});
}

#[test]
fn sudo_as_basics() {
new_test_ext(1).execute_with(|| {
Expand Down

0 comments on commit 96bec7a

Please sign in to comment.