Migrate pallet-sudo to pallet! (#8448)

* WIP convert sudo pallet to attribute macros

* Fix up tests and migrate mock

* Fix up genesis build

* Migrate doc comment example

* Update frame/sudo/src/lib.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Update frame/sudo/src/lib.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Update frame/sudo/src/lib.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Update frame/sudo/src/lib.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Allow unused metadata call_functions

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
This commit is contained in:
Andrew Jones
2021-03-26 14:54:08 +00:00
committed by GitHub
parent 6adf24ca0c
commit e7cd48767a
4 changed files with 179 additions and 102 deletions
+61 -33
View File
@@ -18,7 +18,7 @@
//! Test utilities
use super::*;
use frame_support::{parameter_types, weights::Weight};
use frame_support::{parameter_types, traits::GenesisBuild};
use sp_core::H256;
use sp_runtime::{traits::{BlakeTwo256, IdentityLookup}, testing::Header};
use sp_io;
@@ -27,52 +27,80 @@ use frame_support::traits::Filter;
use frame_system::limits;
// Logger module to track execution.
#[frame_support::pallet]
pub mod logger {
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use super::*;
use frame_system::ensure_root;
#[pallet::config]
pub trait Config: frame_system::Config {
type Event: From<Event<Self>> + Into<<Self as frame_system::Config>::Event>;
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
}
decl_storage! {
trait Store for Module<T: Config> as Logger {
AccountLog get(fn account_log): Vec<T::AccountId>;
I32Log get(fn i32_log): Vec<i32>;
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(PhantomData<T>);
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(*weight)]
pub(crate) fn privileged_i32_log(
origin: OriginFor<T>,
i: i32,
weight: Weight
) -> DispatchResultWithPostInfo {
// Ensure that the `origin` is `Root`.
ensure_root(origin)?;
<I32Log<T>>::append(i);
Self::deposit_event(Event::AppendI32(i, weight));
Ok(().into())
}
#[pallet::weight(*weight)]
pub(crate) fn non_privileged_log(
origin: OriginFor<T>,
i: i32,
weight: Weight
) -> DispatchResultWithPostInfo {
// Ensure that the `origin` is some signed account.
let sender = ensure_signed(origin)?;
<I32Log<T>>::append(i);
<AccountLog<T>>::append(sender.clone());
Self::deposit_event(Event::AppendI32AndAccount(sender, i, weight));
Ok(().into())
}
}
decl_event! {
pub enum Event<T> where AccountId = <T as frame_system::Config>::AccountId {
AppendI32(i32, Weight),
AppendI32AndAccount(AccountId, i32, Weight),
}
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
#[pallet::metadata(T::AccountId = "AccountId")]
pub enum Event<T: Config> {
AppendI32(i32, Weight),
AppendI32AndAccount(T::AccountId, i32, Weight),
}
decl_module! {
pub struct Module<T: Config> for enum Call where origin: <T as frame_system::Config>::Origin {
fn deposit_event() = default;
#[pallet::storage]
#[pallet::getter(fn account_log)]
pub(super) type AccountLog<T: Config> = StorageValue<
_,
Vec<T::AccountId>,
ValueQuery
>;
#[weight = *weight]
fn privileged_i32_log(origin, i: i32, weight: Weight){
// Ensure that the `origin` is `Root`.
ensure_root(origin)?;
<I32Log>::append(i);
Self::deposit_event(RawEvent::AppendI32(i, weight));
}
#[weight = *weight]
fn non_privileged_log(origin, i: i32, weight: Weight){
// Ensure that the `origin` is some signed account.
let sender = ensure_signed(origin)?;
<I32Log>::append(i);
<AccountLog<T>>::append(sender.clone());
Self::deposit_event(RawEvent::AppendI32AndAccount(sender, i, weight));
}
}
}
#[pallet::storage]
#[pallet::getter(fn i32_log)]
pub(super) type I32Log<T> = StorageValue<
_,
Vec<i32>,
ValueQuery
>;
}
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;