Runtime worker threads (#7089)

* std variant

* principal work

* format and naming

* format and naming continued

* working nested fork

* add comment

* naming and tabs

* line width

* fix wording

* address review

* refactor dynamic dispatch

* update wasmtime

* some care

* move ext

* more refactor

* doc effort

* simplify

* doc effort

* tests and docs

* address review

* naming

* explain some args

* add example

* unwinding for native and tests

* rename stray

* fix refs

* fix tests

* fix warnings

* stray naming

* fixes and comments

* Update primitives/io/src/tasks.rs

Co-authored-by: cheme <emericchevalier.pro@gmail.com>

* make examples "compile"

* dyn_dispatch -> spawn_call

* fix impl

* address review

* Update primitives/io/src/lib.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Update primitives/io/src/tasks.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Update primitives/io/src/async_externalities.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Update primitives/io/src/tasks.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Update frame/example-parallel/src/lib.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* fix compilation

* Update client/executor/common/src/wasm_runtime.rs

Co-authored-by: Sergei Shulepov <sergei@parity.io>

* address review

* Update client/executor/wasmtime/src/instance_wrapper.rs

Co-authored-by: Sergei Shulepov <sergei@parity.io>

* Update client/executor/src/native_executor.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update primitives/io/src/tasks.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/executor/src/native_executor.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update primitives/io/src/tasks.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/executor/wasmtime/src/instance_wrapper.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* address some issues

* address more issues

* wasm_only interface

* define sp_tasks

* avoid anyhow

* fix example

Co-authored-by: cheme <emericchevalier.pro@gmail.com>
Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
Co-authored-by: Sergei Shulepov <sergei@parity.io>
Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Nikolay Volf
2020-10-20 05:41:51 -07:00
committed by GitHub
parent 203acda659
commit a062bc2f1d
26 changed files with 1498 additions and 112 deletions
+152
View File
@@ -0,0 +1,152 @@
// This file is part of Substrate.
// Copyright (C) 2020 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
//!
//! This example pallet parallelizes validation of the enlisted participants
//! (see `enlist_participants` dispatch).
#![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};
use sp_std::vec::Vec;
#[cfg(test)]
mod tests;
pub trait Trait: frame_system::Trait {
/// The overarching event type.
type Event: From<Event> + Into<<Self as frame_system::Trait>::Event>;
/// The overarching dispatch call type.
type Call: From<Call<Self>>;
}
decl_storage! {
trait Store for Module<T: Trait> 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>>;
/// Current event id to enlist participants to.
CurrentEventId get(fn get_current_event_id): Vec<u8>;
}
}
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 {
pub account: Vec<u8>,
pub signature: Vec<u8>,
}
impl EnlistedParticipant {
fn verify(&self, event_id: &[u8]) -> bool {
use sp_core::Public;
use std::convert::TryFrom;
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
}
}
}
decl_module! {
/// A public part of the pallet.
pub struct Module<T: Trait> 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> {
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,151 @@
// This file is part of Substrate.
// Copyright (C) 2020 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::*;
use codec::{Encode, Decode};
use frame_support::{impl_outer_origin, parameter_types, weights::Weight};
use sp_core::H256;
use sp_runtime::{
Perbill,
testing::{Header},
traits::{BlakeTwo256, IdentityLookup},
};
impl_outer_origin! {
pub enum Origin for Test where system = frame_system {}
}
#[derive(Clone, Eq, PartialEq, Encode, Decode)]
pub struct Test;
parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const MaximumBlockWeight: Weight = 1024;
pub const MaximumBlockLength: u32 = 2 * 1024;
pub const AvailableBlockRatio: Perbill = Perbill::one();
}
impl frame_system::Trait for Test {
type BaseCallFilter = ();
type Origin = Origin;
type Call = ();
type 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 = ();
type BlockHashCount = BlockHashCount;
type MaximumBlockWeight = MaximumBlockWeight;
type DbWeight = ();
type BlockExecutionWeight = ();
type ExtrinsicBaseWeight = ();
type MaximumExtrinsicWeight = MaximumBlockWeight;
type MaximumBlockLength = MaximumBlockLength;
type AvailableBlockRatio = AvailableBlockRatio;
type Version = ();
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
}
parameter_types! {
pub const GracePeriod: u64 = 5;
pub const UnsignedInterval: u64 = 128;
pub const UnsignedPriority: u64 = 1 << 20;
}
impl Trait for Test {
type Event = ();
type Call = Call<Test>;
}
type Example = Module<Test>;
#[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);
});
}