[][src]Crate srml_sudo

Sudo Module

Overview

The sudo module allows for a single account (called the "sudo key") to execute dispatchable functions that require a Root call or designate a new account to replace them as the sudo key. Only one account can be the sudo key at a time.

You can start using the sudo module by implementing the sudo Trait.

Supported dispatchable functions are documented in the Call enum.

Interface

Dispatchable Functions

Only the sudo key can call the dispatchable functions from the sudo module.

Please refer to the Call enum and its associated variants for documentation on each function.

Usage

Prerequisites

To use the sudo module in your runtime, you must implement the following trait in your runtime:

This example is not tested
impl sudo::Trait for Runtime {
	type Event = Event;
	type Proposal = Call;
}

You can then import the Sudo module in your construct_runtime! macro with:

This example is not tested
Sudo: sudo,

Executing Privileged Functions

The sudo module itself is not intended to be used within other modules. Instead, you can build "privileged functions" in other modules that require Root origin. You can execute these privileged functions by calling sudo with the sudo key account. Privileged functions cannot be directly executed via an extrinsic.

Learn more about privileged functions and Root origin in the Origin type documentation.

Simple Code Snippet

This is an example of a module that exposes a privileged function:

This example is not tested
use support::{decl_module, dispatch::Result};
use system::ensure_root;

pub trait Trait: system::Trait {}

decl_module! {
    pub struct Module<T: Trait> for enum Call where origin: T::Origin {
        pub fn privileged_function(origin) -> Result {
            ensure_root(origin)?;

            // do something...

            Ok(())
        }
    }
}

Example from SRML

The consensus module exposes a set_code privileged function that allows you to set the on-chain Wasm runtime code:

This example is not tested
/// Set the new code.
pub fn set_code(new: Vec<u8>) {
    storage::unhashed::put_raw(well_known_keys::CODE, &new);
}

Genesis Config

To use the sudo module, you need to set an initial superuser account as the sudo key.

This example is not tested
GenesisConfig {
    sudo: Some(SudoConfig {
        key: AccountId,
    })
}

Structs

GenesisConfig
Module

Enums

Call
RawEvent

Events for this module.

Traits

Trait

Type Definitions

Event

RawEvent specialized for the configuration Trait