create parallel tasks extension (#5249)

This commit is contained in:
Nikolay Volf
2020-03-16 08:30:39 -07:00
committed by GitHub
parent 418b7b8bc2
commit 372745705d
25 changed files with 189 additions and 26 deletions
@@ -200,6 +200,7 @@ fn record_proof_works() {
&backend,
&mut overlay,
&executor,
sp_core::tasks::executor(),
"Core_execute_block",
&block.encode(),
&runtime_code,
+4 -1
View File
@@ -34,6 +34,7 @@ sp-debug-derive = { version = "2.0.0-alpha.2", path = "../debug-derive" }
sp-externalities = { version = "0.8.0-alpha.2", optional = true, path = "../externalities" }
sp-storage = { version = "2.0.0-alpha.2", default-features = false, path = "../storage" }
parity-util-mem = { version = "0.5.2", default-features = false, features = ["primitive-types"] }
futures = { version = "0.3.1", optional = true }
# full crypto
ed25519-dalek = { version = "1.0.0-pre.3", default-features = false, features = ["u64_backend", "alloc"], optional = true }
@@ -102,7 +103,9 @@ std = [
"sp-externalities",
"sp-storage/std",
"sp-runtime-interface/std",
"zeroize/alloc"
"zeroize/alloc",
"futures",
"futures/thread-pool",
]
# This feature enables all crypto primitives for `no_std` builds like microcontrollers
+2
View File
@@ -70,6 +70,8 @@ mod changes_trie;
#[cfg(feature = "std")]
pub mod traits;
pub mod testing;
#[cfg(feature = "std")]
pub mod tasks;
pub use self::hash::{H160, H256, H512, convert_hash};
pub use self::uint::U256;
+56
View File
@@ -0,0 +1,56 @@
// Copyright 2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Module for low-level asynchronous processing.
use crate::traits::CloneableSpawn;
use futures::{executor, task};
/// Simple task executor.
///
/// Uses single thread for scheduling tasks. Can be cloned and used in
/// runtime host (implements `CloneableSpawn`).
#[derive(Debug, Clone)]
pub struct Executor {
pool: executor::ThreadPool,
}
impl Executor {
fn new() -> Self {
Self {
pool: executor::ThreadPool::builder().pool_size(1).create()
.expect("Failed to create task executor")
}
}
}
impl task::Spawn for Executor {
fn spawn_obj(&self, future: task::FutureObj<'static, ()>)
-> Result<(), task::SpawnError> {
self.pool.spawn_obj(future)
}
}
impl CloneableSpawn for Executor {
fn clone(&self) -> Box<dyn CloneableSpawn> {
Box::new(Clone::clone(self))
}
}
/// Create tasks executor.
pub fn executor() -> Box<dyn CloneableSpawn> {
Box::new(Executor::new())
}
+18
View File
@@ -212,3 +212,21 @@ impl CallInWasmExt {
Self(Box::new(inner))
}
}
/// Something that can spawn tasks and also can be cloned.
pub trait CloneableSpawn: futures::task::Spawn + Send + Sync {
/// Clone as heap-allocated handle.
fn clone(&self) -> Box<dyn CloneableSpawn>;
}
sp_externalities::decl_extension! {
/// Task executor extension.
pub struct TaskExecutorExt(Box<dyn CloneableSpawn>);
}
impl TaskExecutorExt {
/// New instance of task executor extension.
pub fn new(spawn_handle: Box<dyn CloneableSpawn>) -> Self {
Self(spawn_handle)
}
}
@@ -12,4 +12,4 @@ documentation = "https://docs.rs/sp-externalities"
[dependencies]
sp-storage = { version = "2.0.0-alpha.2", path = "../storage" }
sp-std = { version = "2.0.0-alpha.2", path = "../std" }
environmental = { version = "1.1.1" }
environmental = { version = "1.1.1" }
@@ -573,7 +573,6 @@ mod tests {
const CHILD_UUID_1: &[u8] = b"unique_id_1";
const CHILD_INFO_1: ChildInfo<'static> = ChildInfo::new_default(CHILD_UUID_1);
fn prepare_overlay_with_changes() -> OverlayedChanges {
OverlayedChanges {
prospective: vec![
@@ -74,6 +74,7 @@ pub use trie_backend::TrieBackend;
pub use error::{Error, ExecutionError};
pub use in_memory_backend::InMemory as InMemoryBackend;
pub use stats::{UsageInfo, UsageUnit};
pub use sp_core::traits::CloneableSpawn;
type CallResult<R, E> = Result<NativeOrEncoded<R>, E>;
@@ -210,8 +211,10 @@ impl<'a, B, H, N, Exec> StateMachine<'a, B, H, N, Exec> where
call_data: &'a [u8],
mut extensions: Extensions,
runtime_code: &'a RuntimeCode,
spawn_handle: Box<dyn CloneableSpawn>,
) -> Self {
extensions.register(CallInWasmExt::new(exec.clone()));
extensions.register(sp_core::traits::TaskExecutorExt::new(spawn_handle));
Self {
backend,
@@ -437,6 +440,7 @@ pub fn prove_execution<B, H, N, Exec>(
mut backend: B,
overlay: &mut OverlayedChanges,
exec: &Exec,
spawn_handle: Box<dyn CloneableSpawn>,
method: &str,
call_data: &[u8],
runtime_code: &RuntimeCode,
@@ -454,6 +458,7 @@ where
trie_backend,
overlay,
exec,
spawn_handle,
method,
call_data,
runtime_code,
@@ -473,6 +478,7 @@ pub fn prove_execution_on_trie_backend<S, H, N, Exec>(
trie_backend: &TrieBackend<S, H>,
overlay: &mut OverlayedChanges,
exec: &Exec,
spawn_handle: Box<dyn CloneableSpawn>,
method: &str,
call_data: &[u8],
runtime_code: &RuntimeCode,
@@ -494,6 +500,7 @@ where
call_data,
Extensions::default(),
runtime_code,
spawn_handle,
);
let result = sm.execute_using_consensus_failure_handler::<_, NeverNativeValue, fn() -> _>(
@@ -510,6 +517,7 @@ pub fn execution_proof_check<H, N, Exec>(
proof: StorageProof,
overlay: &mut OverlayedChanges,
exec: &Exec,
spawn_handle: Box<dyn CloneableSpawn>,
method: &str,
call_data: &[u8],
runtime_code: &RuntimeCode,
@@ -525,6 +533,7 @@ where
&trie_backend,
overlay,
exec,
spawn_handle,
method,
call_data,
runtime_code,
@@ -536,6 +545,7 @@ pub fn execution_proof_check_on_trie_backend<H, N, Exec>(
trie_backend: &TrieBackend<MemoryDB<H>, H>,
overlay: &mut OverlayedChanges,
exec: &Exec,
spawn_handle: Box<dyn CloneableSpawn>,
method: &str,
call_data: &[u8],
runtime_code: &RuntimeCode,
@@ -555,6 +565,7 @@ where
call_data,
Extensions::default(),
runtime_code,
spawn_handle,
);
sm.execute_using_consensus_failure_handler::<_, NeverNativeValue, fn() -> _>(
@@ -820,6 +831,7 @@ mod tests {
&[],
Default::default(),
&wasm_code,
sp_core::tasks::executor(),
);
assert_eq!(
@@ -849,6 +861,7 @@ mod tests {
&[],
Default::default(),
&wasm_code,
sp_core::tasks::executor(),
);
assert_eq!(state_machine.execute(ExecutionStrategy::NativeElseWasm).unwrap(), vec![66]);
@@ -875,6 +888,7 @@ mod tests {
&[],
Default::default(),
&wasm_code,
sp_core::tasks::executor(),
);
assert!(
@@ -905,6 +919,7 @@ mod tests {
remote_backend,
&mut Default::default(),
&executor,
sp_core::tasks::executor(),
"test",
&[],
&RuntimeCode::empty(),
@@ -916,6 +931,7 @@ mod tests {
remote_proof,
&mut Default::default(),
&executor,
sp_core::tasks::executor(),
"test",
&[],
&RuntimeCode::empty(),