Migrate examples to use pallet macro (#8138)

This commit is contained in:
Guillaume Thiolliere
2021-02-22 12:33:35 +01:00
committed by GitHub
parent 1b2dd6117b
commit ecf4404903
8 changed files with 456 additions and 406 deletions
+60 -60
View File
@@ -22,10 +22,6 @@
#![cfg_attr(not(feature = "std"), no_std)]
use frame_system::ensure_signed;
use frame_support::{
dispatch::DispatchResult, decl_module, decl_storage, decl_event,
};
use sp_runtime::RuntimeDebug;
use codec::{Encode, Decode};
@@ -34,34 +30,72 @@ use sp_std::vec::Vec;
#[cfg(test)]
mod tests;
pub trait Config: frame_system::Config {
/// The overarching event type.
type Event: From<Event> + Into<<Self as frame_system::Config>::Event>;
/// The overarching dispatch call type.
type Call: From<Call<Self>>;
}
pub use pallet::*;
decl_storage! {
trait Store for Module<T: Config> as ExampleOffchainWorker {
/// A vector of current participants
#[frame_support::pallet]
pub mod pallet {
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
/// The overarching dispatch call type.
type Call: From<Call<Self>>;
}
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
/// A public part of the pallet.
#[pallet::call]
impl<T: Config> Pallet<T> {
/// Get the new event running.
#[pallet::weight(0)]
pub fn run_event(origin: OriginFor<T>, id: Vec<u8>) -> DispatchResultWithPostInfo {
let _ = ensure_signed(origin)?;
<Participants<T>>::kill();
<CurrentEventId<T>>::mutate(move |event_id| *event_id = id);
Ok(().into())
}
/// Submit list of participants to the current event.
///
/// To enlist someone to participate, signed payload should be
/// sent to `enlist`.
Participants get(fn participants): Vec<Vec<u8>>;
/// The example utilizes parallel execution by checking half of the
/// signatures in spawned task.
#[pallet::weight(0)]
pub fn enlist_participants(origin: OriginFor<T>, participants: Vec<EnlistedParticipant>)
-> DispatchResultWithPostInfo
{
let _ = ensure_signed(origin)?;
/// Current event id to enlist participants to.
CurrentEventId get(fn get_current_event_id): Vec<u8>;
if validate_participants_parallel(&<CurrentEventId<T>>::get(), &participants[..]) {
for participant in participants {
<Participants<T>>::append(participant.account);
}
}
Ok(().into())
}
}
/// A vector of current participants
///
/// To enlist someone to participate, signed payload should be
/// sent to `enlist`.
#[pallet::storage]
#[pallet::getter(fn participants)]
pub(super) type Participants<T: Config> = StorageValue<_, Vec<Vec<u8>>, ValueQuery>;
/// Current event id to enlist participants to.
#[pallet::storage]
#[pallet::getter(fn get_current_event_id)]
pub(super) type CurrentEventId<T: Config> = StorageValue<_, Vec<u8>, ValueQuery>;
}
decl_event!(
/// Events generated by the module.
pub enum Event {
/// When new event is drafted.
NewEventDrafted(Vec<u8>),
}
);
/// Request to enlist participant.
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)]
pub struct EnlistedParticipant {
@@ -85,40 +119,6 @@ impl EnlistedParticipant {
}
}
decl_module! {
/// A public part of the pallet.
pub struct Module<T: Config> for enum Call where origin: T::Origin {
fn deposit_event() = default;
/// Get the new event running.
#[weight = 0]
pub fn run_event(origin, id: Vec<u8>) -> DispatchResult {
let _ = ensure_signed(origin)?;
Participants::kill();
CurrentEventId::mutate(move |event_id| *event_id = id);
Ok(())
}
/// Submit list of participants to the current event.
///
/// The example utilizes parallel execution by checking half of the
/// signatures in spawned task.
#[weight = 0]
pub fn enlist_participants(origin, participants: Vec<EnlistedParticipant>)
-> DispatchResult
{
let _ = ensure_signed(origin)?;
if validate_participants_parallel(&CurrentEventId::get(), &participants[..]) {
for participant in participants {
Participants::append(participant.account);
}
}
Ok(())
}
}
}
fn validate_participants_parallel(event_id: &[u8], participants: &[EnlistedParticipant]) -> bool {
fn spawn_verify(data: Vec<u8>) -> Vec<u8> {
@@ -34,7 +34,7 @@ frame_support::construct_runtime!(
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Module, Call, Config, Storage, Event<T>},
Example: pallet_example_parallel::{Module, Call, Storage, Event},
Example: pallet_example_parallel::{Module, Call, Storage},
}
);
@@ -75,7 +75,6 @@ parameter_types! {
}
impl Config for Test {
type Event = Event;
type Call = Call;
}