Move all example pallets under examples folder. (#10215)

* Put all examples under one folder

Signed-off-by: Jimmy Chu <jimmychu0807@gmail.com>

* Updated Cargo.toml

Signed-off-by: Jimmy Chu <jimmychu0807@gmail.com>

* updated for ci script

Signed-off-by: Jimmy Chu <jimmychu0807@gmail.com>

* update

Signed-off-by: Jimmy Chu <jimmychu0807@gmail.com>

* Added notes that example pallets are not meant to be used in production.

Signed-off-by: Jimmy Chu <jimmychu0807@gmail.com>

* updated

Signed-off-by: Jimmy Chu <jimmychu0807@gmail.com>
This commit is contained in:
Jimmy Chu
2021-11-10 16:11:28 +08:00
committed by GitHub
parent 617e2cc75e
commit db59cfcf14
18 changed files with 81 additions and 60 deletions
@@ -0,0 +1,39 @@
[package]
name = "pallet-example-parallel"
version = "3.0.0-dev"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2021"
license = "Unlicense"
homepage = "https://substrate.io"
repository = "https://github.com/paritytech/substrate/"
description = "FRAME example pallet using runtime worker threads"
[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false }
scale-info = { version = "1.0", default-features = false, features = ["derive"] }
frame-support = { version = "4.0.0-dev", default-features = false, path = "../../support" }
frame-system = { version = "4.0.0-dev", default-features = false, path = "../../system" }
sp-core = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/core" }
sp-io = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/io" }
sp-runtime = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/runtime" }
sp-std = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/std" }
sp-tasks = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/tasks" }
[features]
default = ["std"]
std = [
"codec/std",
"scale-info/std",
"frame-support/std",
"frame-system/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
"sp-tasks/std",
]
try-runtime = ["frame-support/try-runtime"]
@@ -0,0 +1,7 @@
<!-- markdown-link-check-disable -->
# Parallel Tasks Example Pallet
This example pallet demonstrates parallelizing validation of the enlisted participants (see
`enlist_participants` dispatch).
**This pallet serves as an example and is not meant to be used in production.**
@@ -0,0 +1,150 @@
// This file is part of Substrate.
// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! # Parallel Tasks Example Pallet
//!
//! This example pallet demonstrates parallelizing validation of the enlisted participants
//! (see `enlist_participants` dispatch).
//!
//! **This pallet serves as an example and is not meant to be used in production.**
#![cfg_attr(not(feature = "std"), no_std)]
use sp_runtime::RuntimeDebug;
use codec::{Decode, Encode};
use sp_std::vec::Vec;
#[cfg(test)]
mod tests;
pub use pallet::*;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
#[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>(_);
/// 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, scale_info::TypeInfo)]
pub struct EnlistedParticipant {
pub account: Vec<u8>,
pub signature: Vec<u8>,
}
impl EnlistedParticipant {
fn verify(&self, event_id: &[u8]) -> bool {
use sp_core::Public;
use sp_runtime::traits::Verify;
match sp_core::sr25519::Signature::try_from(&self.signature[..]) {
Ok(signature) => {
let public = sp_core::sr25519::Public::from_slice(self.account.as_ref());
signature.verify(event_id, &public)
},
_ => false,
}
}
}
fn validate_participants_parallel(event_id: &[u8], participants: &[EnlistedParticipant]) -> bool {
fn spawn_verify(data: Vec<u8>) -> Vec<u8> {
let stream = &mut &data[..];
let event_id = Vec::<u8>::decode(stream).expect("Failed to decode");
let participants = Vec::<EnlistedParticipant>::decode(stream).expect("Failed to decode");
for participant in participants {
if !participant.verify(&event_id) {
return false.encode()
}
}
true.encode()
}
let mut async_payload = Vec::new();
event_id.encode_to(&mut async_payload);
participants[..participants.len() / 2].encode_to(&mut async_payload);
let handle = sp_tasks::spawn(spawn_verify, async_payload);
let mut result = true;
for participant in &participants[participants.len() / 2 + 1..] {
if !participant.verify(event_id) {
result = false;
break
}
}
bool::decode(&mut &handle.join()[..]).expect("Failed to decode result") && result
}
@@ -0,0 +1,149 @@
// This file is part of Substrate.
// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::{self as pallet_example_parallel, *};
use frame_support::parameter_types;
use sp_core::H256;
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
Perbill,
};
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;
frame_support::construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
Example: pallet_example_parallel::{Pallet, Call, Storage},
}
);
parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const AvailableBlockRatio: Perbill = Perbill::one();
}
impl frame_system::Config for Test {
type BaseCallFilter = frame_support::traits::Everything;
type Origin = Origin;
type Call = Call;
type PalletInfo = PalletInfo;
type Index = u64;
type BlockNumber = u64;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = sp_core::sr25519::Public;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = Event;
type BlockHashCount = BlockHashCount;
type DbWeight = ();
type BlockWeights = ();
type BlockLength = ();
type Version = ();
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = ();
}
parameter_types! {
pub const GracePeriod: u64 = 5;
pub const UnsignedInterval: u64 = 128;
pub const UnsignedPriority: u64 = 1 << 20;
}
impl Config for Test {
type Call = Call;
}
#[test]
fn it_can_enlist() {
use sp_core::Pair;
sp_io::TestExternalities::default().execute_with(|| {
let (pair1, _) = sp_core::sr25519::Pair::generate();
let (pair2, _) = sp_core::sr25519::Pair::generate();
let event_name = b"test";
Example::run_event(Origin::signed(Default::default()), event_name.to_vec())
.expect("Failed to enlist");
let participants = vec![
EnlistedParticipant {
account: pair1.public().to_vec(),
signature: AsRef::<[u8]>::as_ref(&pair1.sign(event_name)).to_vec(),
},
EnlistedParticipant {
account: pair2.public().to_vec(),
signature: AsRef::<[u8]>::as_ref(&pair2.sign(event_name)).to_vec(),
},
];
Example::enlist_participants(Origin::signed(Default::default()), participants)
.expect("Failed to enlist");
assert_eq!(Example::participants().len(), 2);
});
}
#[test]
fn one_wrong_will_not_enlist_anyone() {
use sp_core::Pair;
sp_io::TestExternalities::default().execute_with(|| {
let (pair1, _) = sp_core::sr25519::Pair::generate();
let (pair2, _) = sp_core::sr25519::Pair::generate();
let (pair3, _) = sp_core::sr25519::Pair::generate();
let event_name = b"test";
Example::run_event(Origin::signed(Default::default()), event_name.to_vec())
.expect("Failed to enlist");
let participants = vec![
EnlistedParticipant {
account: pair1.public().to_vec(),
signature: AsRef::<[u8]>::as_ref(&pair1.sign(event_name)).to_vec(),
},
EnlistedParticipant {
account: pair2.public().to_vec(),
signature: AsRef::<[u8]>::as_ref(&pair2.sign(event_name)).to_vec(),
},
// signing wrong event
EnlistedParticipant {
account: pair3.public().to_vec(),
signature: AsRef::<[u8]>::as_ref(&pair3.sign(&[])).to_vec(),
},
];
Example::enlist_participants(Origin::signed(Default::default()), participants)
.expect("Failed to enlist");
assert_eq!(Example::participants().len(), 0);
});
}