Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Migrate examples to use pallet macro (#8138)
Browse files Browse the repository at this point in the history
  • Loading branch information
gui1117 authored Feb 22, 2021
1 parent 11e879f commit fd88fbf
Show file tree
Hide file tree
Showing 8 changed files with 433 additions and 383 deletions.
2 changes: 1 addition & 1 deletion frame/balances/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//! Autogenerated weights for pallet_balances
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.0
//! DATE: 2021-01-06, STEPS: [50, ], REPEAT: 20, LOW RANGE: [], HIGH RANGE: []
//! DATE: 2021-01-06, STEPS: \[50, \], REPEAT: 20, LOW RANGE: [], HIGH RANGE: []
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128
// Executed Command:
Expand Down
2 changes: 1 addition & 1 deletion frame/benchmarking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ macro_rules! impl_benchmark_test {

/// This creates a test suite which runs the module's benchmarks.
///
/// When called in [`pallet_example`] as
/// When called in `pallet_example` as
///
/// ```rust,ignore
/// impl_benchmark_test_suite!(Module, crate::tests::new_test_ext(), crate::tests::Test);
Expand Down
355 changes: 181 additions & 174 deletions frame/example-offchain-worker/src/lib.rs

Large diffs are not rendered by default.

120 changes: 60 additions & 60 deletions frame/example-parallel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -34,33 +30,71 @@ 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
///
/// To enlist someone to participate, signed payload should be
/// sent to `enlist`.
Participants get(fn participants): Vec<Vec<u8>>;
#[frame_support::pallet]
pub mod pallet {
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use super::*;

/// Current event id to enlist participants to.
CurrentEventId get(fn get_current_event_id): Vec<u8>;
#[pallet::config]
pub trait Config: frame_system::Config {
/// The overarching dispatch call type.
type Call: From<Call<Self>>;
}
}

decl_event!(
/// Events generated by the module.
pub enum Event {
/// When new event is drafted.
NewEventDrafted(Vec<u8>),
#[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.
///
/// 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)?;

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>;
}

/// Request to enlist participant.
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)]
Expand All @@ -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> {
Expand Down
3 changes: 1 addition & 2 deletions frame/example-parallel/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}
);

Expand Down Expand Up @@ -75,7 +75,6 @@ parameter_types! {
}

impl Config for Test {
type Event = Event;
type Call = Call;
}

Expand Down
Loading

0 comments on commit fd88fbf

Please sign in to comment.