mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 08:51:09 +00:00
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:
@@ -186,6 +186,25 @@ impl TaskExecutorExt {
|
||||
}
|
||||
}
|
||||
|
||||
/// Runtime spawn extension.
|
||||
pub trait RuntimeSpawn: Send {
|
||||
/// Create new runtime instance and use dynamic dispatch to invoke with specified payload.
|
||||
///
|
||||
/// Returns handle of the spawned task.
|
||||
///
|
||||
/// Function pointers (`dispatcher_ref`, `func`) are WASM pointer types.
|
||||
fn spawn_call(&self, dispatcher_ref: u32, func: u32, payload: Vec<u8>) -> u64;
|
||||
|
||||
/// Join the result of previously created runtime instance invocation.
|
||||
fn join(&self, handle: u64) -> Vec<u8>;
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
sp_externalities::decl_extension! {
|
||||
/// Extension that supports spawning extra runtime instances in externalities.
|
||||
pub struct RuntimeSpawnExt(Box<dyn RuntimeSpawn>);
|
||||
}
|
||||
|
||||
/// Something that can spawn futures (blocking and non-blocking) with an assigned name.
|
||||
#[dyn_clonable::clonable]
|
||||
pub trait SpawnNamed: Clone + Send + Sync {
|
||||
|
||||
@@ -79,6 +79,12 @@ macro_rules! decl_extension {
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl From<$inner> for $ext_name {
|
||||
fn from(inner: $inner) -> Self {
|
||||
Self(inner)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ use tracing;
|
||||
#[cfg(feature = "std")]
|
||||
use sp_core::{
|
||||
crypto::Pair,
|
||||
traits::{CallInWasmExt, TaskExecutorExt},
|
||||
traits::{CallInWasmExt, TaskExecutorExt, RuntimeSpawnExt},
|
||||
offchain::{OffchainExt, TransactionPoolExt},
|
||||
hexdisplay::HexDisplay,
|
||||
storage::ChildInfo,
|
||||
@@ -520,7 +520,6 @@ pub trait Crypto {
|
||||
fn start_batch_verify(&mut self) {
|
||||
let scheduler = self.extension::<TaskExecutorExt>()
|
||||
.expect("No task executor associated with the current context!")
|
||||
.0
|
||||
.clone();
|
||||
|
||||
self.register_extension(VerificationExt(BatchVerifier::new(scheduler)))
|
||||
@@ -755,7 +754,7 @@ pub trait OffchainIndex {
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
sp_externalities::decl_extension! {
|
||||
/// The keystore extension to register/retrieve from the externalities.
|
||||
/// Batch verification extension to register/retrieve from the externalities.
|
||||
pub struct VerificationExt(BatchVerifier);
|
||||
}
|
||||
|
||||
@@ -1239,6 +1238,34 @@ pub trait Sandbox {
|
||||
}
|
||||
}
|
||||
|
||||
/// Wasm host functions for managing tasks.
|
||||
///
|
||||
/// This should not be used directly. Use `sp_tasks` for running parallel tasks instead.
|
||||
#[runtime_interface(wasm_only)]
|
||||
pub trait RuntimeTasks {
|
||||
/// Wasm host function for spawning task.
|
||||
///
|
||||
/// This should not be used directly. Use `sp_tasks::spawn` instead.
|
||||
fn spawn(dispatcher_ref: u32, entry: u32, payload: Vec<u8>) -> u64 {
|
||||
sp_externalities::with_externalities(|mut ext|{
|
||||
let runtime_spawn = ext.extension::<RuntimeSpawnExt>()
|
||||
.expect("Cannot spawn without dynamic runtime dispatcher (RuntimeSpawnExt)");
|
||||
runtime_spawn.spawn_call(dispatcher_ref, entry, payload)
|
||||
}).expect("`RuntimeTasks::spawn`: called outside of externalities context")
|
||||
}
|
||||
|
||||
/// Wasm host function for joining a task.
|
||||
///
|
||||
/// This should not be used directly. Use `join` of `sp_tasks::spawn` result instead.
|
||||
fn join(handle: u64) -> Vec<u8> {
|
||||
sp_externalities::with_externalities(|mut ext| {
|
||||
let runtime_spawn = ext.extension::<RuntimeSpawnExt>()
|
||||
.expect("Cannot join without dynamic runtime dispatcher (RuntimeSpawnExt)");
|
||||
runtime_spawn.join(handle)
|
||||
}).expect("`RuntimeTasks::join`: called outside of externalities context")
|
||||
}
|
||||
}
|
||||
|
||||
/// Allocator used by Substrate when executing the Wasm runtime.
|
||||
#[cfg(not(feature = "std"))]
|
||||
struct WasmAllocator;
|
||||
@@ -1306,6 +1333,7 @@ pub type SubstrateHostFunctions = (
|
||||
sandbox::HostFunctions,
|
||||
crate::trie::HostFunctions,
|
||||
offchain_index::HostFunctions,
|
||||
runtime_tasks::HostFunctions,
|
||||
);
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -312,7 +312,7 @@ pub mod pass_by;
|
||||
|
||||
mod util;
|
||||
|
||||
pub use util::unpack_ptr_and_len;
|
||||
pub use util::{unpack_ptr_and_len, pack_ptr_and_len};
|
||||
|
||||
/// Something that can be used by the runtime interface as type to communicate between wasm and the
|
||||
/// host.
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
//! Basic implementation for Externalities.
|
||||
|
||||
use std::{
|
||||
collections::BTreeMap, any::{TypeId, Any}, iter::FromIterator, ops::Bound
|
||||
collections::BTreeMap, any::{TypeId, Any}, iter::FromIterator, ops::Bound,
|
||||
};
|
||||
use crate::{Backend, StorageKey, StorageValue};
|
||||
use hash_db::Hasher;
|
||||
|
||||
@@ -17,9 +17,8 @@
|
||||
|
||||
//! Test implementation for Externalities.
|
||||
|
||||
use std::any::{Any, TypeId};
|
||||
use codec::Decode;
|
||||
use hash_db::Hasher;
|
||||
use std::{any::{Any, TypeId}, panic::{AssertUnwindSafe, UnwindSafe}};
|
||||
|
||||
use crate::{
|
||||
backend::Backend, OverlayedChanges, StorageTransactionCache, ext::Ext, InMemoryBackend,
|
||||
StorageKey, StorageValue,
|
||||
@@ -30,6 +29,9 @@ use crate::{
|
||||
State as ChangesTrieState,
|
||||
},
|
||||
};
|
||||
|
||||
use codec::{Decode, Encode};
|
||||
use hash_db::Hasher;
|
||||
use sp_core::{
|
||||
offchain::{
|
||||
testing::TestPersistentOffchainDB,
|
||||
@@ -42,7 +44,6 @@ use sp_core::{
|
||||
traits::TaskExecutorExt,
|
||||
testing::TaskExecutor,
|
||||
};
|
||||
use codec::Encode;
|
||||
use sp_externalities::{Extensions, Extension};
|
||||
|
||||
/// Simple HashMap-based Externalities impl.
|
||||
@@ -178,6 +179,19 @@ impl<H: Hasher, N: ChangesTrieBlockNumber> TestExternalities<H, N>
|
||||
let mut ext = self.ext();
|
||||
sp_externalities::set_and_run_with_externalities(&mut ext, execute)
|
||||
}
|
||||
|
||||
/// Execute the given closure while `self` is set as externalities.
|
||||
///
|
||||
/// Returns the result of the given closure, if no panics occured.
|
||||
/// Otherwise, returns `Err`.
|
||||
pub fn execute_with_safe<R>(&mut self, f: impl FnOnce() -> R + UnwindSafe) -> Result<R, String> {
|
||||
let mut ext = AssertUnwindSafe(self.ext());
|
||||
std::panic::catch_unwind(move ||
|
||||
sp_externalities::set_and_run_with_externalities(&mut *ext, f)
|
||||
).map_err(|e| {
|
||||
format!("Closure panicked: {:?}", e)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<H: Hasher, N: ChangesTrieBlockNumber> std::fmt::Debug for TestExternalities<H, N>
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
[package]
|
||||
name = "sp-tasks"
|
||||
version = "2.0.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
license = "Apache-2.0"
|
||||
homepage = "https://substrate.dev"
|
||||
repository = "https://github.com/paritytech/substrate/"
|
||||
description = "Runtime asynchronous, pure computational tasks"
|
||||
documentation = "https://docs.rs/sp-tasks"
|
||||
readme = "README.md"
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
||||
|
||||
[dependencies]
|
||||
log = { version = "0.4.8", optional = true }
|
||||
sp-core = { version = "2.0.0", default-features = false, path = "../core" }
|
||||
sp-externalities = { version = "0.8.0", optional = true, path = "../externalities" }
|
||||
sp-io = { version = "2.0.0", default-features = false, path = "../io" }
|
||||
sp-runtime-interface = { version = "2.0.0", default-features = false, path = "../runtime-interface" }
|
||||
sp-std = { version = "2.0.0", default-features = false, path = "../std" }
|
||||
|
||||
[dev-dependencies]
|
||||
codec = { package = "parity-scale-codec", default-features = false, version = "1.3.1" }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = [
|
||||
"log",
|
||||
"sp-core/std",
|
||||
"sp-externalities",
|
||||
"sp-io/std",
|
||||
"sp-runtime-interface/std",
|
||||
"sp-std/std",
|
||||
]
|
||||
@@ -0,0 +1,3 @@
|
||||
Runtime asynchronous, pure computational tasks.
|
||||
|
||||
License: Apache-2.0
|
||||
@@ -0,0 +1,215 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 2020 Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
|
||||
|
||||
// This program 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.
|
||||
|
||||
// This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
//! Async externalities.
|
||||
|
||||
use std::any::{TypeId, Any};
|
||||
use sp_core::{
|
||||
storage::{ChildInfo, TrackedStorageKey},
|
||||
traits::{Externalities, SpawnNamed, TaskExecutorExt, RuntimeSpawnExt, RuntimeSpawn},
|
||||
};
|
||||
use sp_externalities::{Extensions, ExternalitiesExt as _};
|
||||
|
||||
/// Simple state-less externalities for use in async context.
|
||||
///
|
||||
/// Will panic if anything is accessing the storage.
|
||||
#[derive(Debug)]
|
||||
pub struct AsyncExternalities {
|
||||
extensions: Extensions,
|
||||
}
|
||||
|
||||
/// New Async externalities.
|
||||
pub fn new_async_externalities(scheduler: Box<dyn SpawnNamed>) -> Result<AsyncExternalities, &'static str> {
|
||||
let mut res = AsyncExternalities { extensions: Default::default() };
|
||||
let mut ext = &mut res as &mut dyn Externalities;
|
||||
ext.register_extension::<TaskExecutorExt>(TaskExecutorExt(scheduler.clone()))
|
||||
.map_err(|_| "Failed to register task executor extension.")?;
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
impl AsyncExternalities {
|
||||
/// Extend async externalities with the ability to spawn wasm instances.
|
||||
pub fn with_runtime_spawn(
|
||||
mut self,
|
||||
runtime_ext: Box<dyn RuntimeSpawn>,
|
||||
) -> Result<Self, &'static str> {
|
||||
let mut ext = &mut self as &mut dyn Externalities;
|
||||
ext.register_extension::<RuntimeSpawnExt>(RuntimeSpawnExt(runtime_ext))
|
||||
.map_err(|_| "Failed to register task executor extension.")?;
|
||||
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
|
||||
type StorageKey = Vec<u8>;
|
||||
|
||||
type StorageValue = Vec<u8>;
|
||||
|
||||
impl Externalities for AsyncExternalities {
|
||||
fn set_offchain_storage(&mut self, _key: &[u8], _value: Option<&[u8]>) {
|
||||
panic!("`set_offchain_storage`: should not be used in async externalities!")
|
||||
}
|
||||
|
||||
fn storage(&self, _key: &[u8]) -> Option<StorageValue> {
|
||||
panic!("`storage`: should not be used in async externalities!")
|
||||
}
|
||||
|
||||
fn storage_hash(&self, _key: &[u8]) -> Option<Vec<u8>> {
|
||||
panic!("`storage_hash`: should not be used in async externalities!")
|
||||
}
|
||||
|
||||
fn child_storage(
|
||||
&self,
|
||||
_child_info: &ChildInfo,
|
||||
_key: &[u8],
|
||||
) -> Option<StorageValue> {
|
||||
panic!("`child_storage`: should not be used in async externalities!")
|
||||
}
|
||||
|
||||
fn child_storage_hash(
|
||||
&self,
|
||||
_child_info: &ChildInfo,
|
||||
_key: &[u8],
|
||||
) -> Option<Vec<u8>> {
|
||||
panic!("`child_storage_hash`: should not be used in async externalities!")
|
||||
}
|
||||
|
||||
fn next_storage_key(&self, _key: &[u8]) -> Option<StorageKey> {
|
||||
panic!("`next_storage_key`: should not be used in async externalities!")
|
||||
}
|
||||
|
||||
fn next_child_storage_key(
|
||||
&self,
|
||||
_child_info: &ChildInfo,
|
||||
_key: &[u8],
|
||||
) -> Option<StorageKey> {
|
||||
panic!("`next_child_storage_key`: should not be used in async externalities!")
|
||||
}
|
||||
|
||||
fn place_storage(&mut self, _key: StorageKey, _maybe_value: Option<StorageValue>) {
|
||||
panic!("`place_storage`: should not be used in async externalities!")
|
||||
}
|
||||
|
||||
fn place_child_storage(
|
||||
&mut self,
|
||||
_child_info: &ChildInfo,
|
||||
_key: StorageKey,
|
||||
_value: Option<StorageValue>,
|
||||
) {
|
||||
panic!("`place_child_storage`: should not be used in async externalities!")
|
||||
}
|
||||
|
||||
fn kill_child_storage(
|
||||
&mut self,
|
||||
_child_info: &ChildInfo,
|
||||
) {
|
||||
panic!("`kill_child_storage`: should not be used in async externalities!")
|
||||
}
|
||||
|
||||
fn clear_prefix(&mut self, _prefix: &[u8]) {
|
||||
panic!("`clear_prefix`: should not be used in async externalities!")
|
||||
}
|
||||
|
||||
fn clear_child_prefix(
|
||||
&mut self,
|
||||
_child_info: &ChildInfo,
|
||||
_prefix: &[u8],
|
||||
) {
|
||||
panic!("`clear_child_prefix`: should not be used in async externalities!")
|
||||
}
|
||||
|
||||
fn storage_append(
|
||||
&mut self,
|
||||
_key: Vec<u8>,
|
||||
_value: Vec<u8>,
|
||||
) {
|
||||
panic!("`storage_append`: should not be used in async externalities!")
|
||||
}
|
||||
|
||||
fn chain_id(&self) -> u64 { 42 }
|
||||
|
||||
fn storage_root(&mut self) -> Vec<u8> {
|
||||
panic!("`storage_root`: should not be used in async externalities!")
|
||||
}
|
||||
|
||||
fn child_storage_root(
|
||||
&mut self,
|
||||
_child_info: &ChildInfo,
|
||||
) -> Vec<u8> {
|
||||
panic!("`child_storage_root`: should not be used in async externalities!")
|
||||
}
|
||||
|
||||
fn storage_changes_root(&mut self, _parent: &[u8]) -> Result<Option<Vec<u8>>, ()> {
|
||||
panic!("`storage_changes_root`: should not be used in async externalities!")
|
||||
}
|
||||
|
||||
fn storage_start_transaction(&mut self) {
|
||||
unimplemented!("Transactions are not supported by AsyncExternalities");
|
||||
}
|
||||
|
||||
fn storage_rollback_transaction(&mut self) -> Result<(), ()> {
|
||||
unimplemented!("Transactions are not supported by AsyncExternalities");
|
||||
}
|
||||
|
||||
fn storage_commit_transaction(&mut self) -> Result<(), ()> {
|
||||
unimplemented!("Transactions are not supported by AsyncExternalities");
|
||||
}
|
||||
|
||||
fn wipe(&mut self) {}
|
||||
|
||||
fn commit(&mut self) {}
|
||||
|
||||
fn read_write_count(&self) -> (u32, u32, u32, u32) {
|
||||
unimplemented!("read_write_count is not supported in AsyncExternalities")
|
||||
}
|
||||
|
||||
fn reset_read_write_count(&mut self) {
|
||||
unimplemented!("reset_read_write_count is not supported in AsyncExternalities")
|
||||
}
|
||||
|
||||
fn get_whitelist(&self) -> Vec<TrackedStorageKey> {
|
||||
unimplemented!("get_whitelist is not supported in AsyncExternalities")
|
||||
}
|
||||
|
||||
fn set_whitelist(&mut self, _: Vec<TrackedStorageKey>) {
|
||||
unimplemented!("set_whitelist is not supported in AsyncExternalities")
|
||||
}
|
||||
}
|
||||
|
||||
impl sp_externalities::ExtensionStore for AsyncExternalities {
|
||||
fn extension_by_type_id(&mut self, type_id: TypeId) -> Option<&mut dyn Any> {
|
||||
self.extensions.get_mut(type_id)
|
||||
}
|
||||
|
||||
fn register_extension_with_type_id(
|
||||
&mut self,
|
||||
type_id: TypeId,
|
||||
extension: Box<dyn sp_externalities::Extension>,
|
||||
) -> Result<(), sp_externalities::Error> {
|
||||
self.extensions.register_with_type_id(type_id, extension)
|
||||
}
|
||||
|
||||
fn deregister_extension_by_type_id(&mut self, type_id: TypeId) -> Result<(), sp_externalities::Error> {
|
||||
if self.extensions.deregister(type_id) {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(sp_externalities::Error::ExtensionIsNotRegistered(type_id))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,247 @@
|
||||
// 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.
|
||||
|
||||
//! Runtime tasks.
|
||||
//!
|
||||
//! Contains runtime-usable functions for spawning parallel purely computational tasks.
|
||||
//!
|
||||
//! NOTE: This is experimental API.
|
||||
//! NOTE: When using in actual runtime, make sure you don't produce unbounded parallelism.
|
||||
//! So this is bad example to use it:
|
||||
//! ```rust
|
||||
//! fn my_parallel_computator(data: Vec<u8>) -> Vec<u8> {
|
||||
//! unimplemented!()
|
||||
//! }
|
||||
//! fn test(dynamic_variable: i32) {
|
||||
//! for _ in 0..dynamic_variable { sp_tasks::spawn(my_parallel_computator, vec![]); }
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//! While this is a good example:
|
||||
//! ```rust
|
||||
//! use codec::Encode;
|
||||
//! static STATIC_VARIABLE: i32 = 4;
|
||||
//!
|
||||
//! fn my_parallel_computator(data: Vec<u8>) -> Vec<u8> {
|
||||
//! unimplemented!()
|
||||
//! }
|
||||
//!
|
||||
//! fn test(computation_payload: Vec<u8>) {
|
||||
//! let parallel_tasks = (0..STATIC_VARIABLE).map(|idx|
|
||||
//! sp_tasks::spawn(my_parallel_computator, computation_payload.chunks(10).nth(idx as _).encode())
|
||||
//! );
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//! When allowing unbounded parallelism, malicious transactions can exploit it and partition
|
||||
//! network consensus based on how much resources nodes have.
|
||||
//!
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
mod async_externalities;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
pub use async_externalities::{new_async_externalities, AsyncExternalities};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
mod inner {
|
||||
use std::{panic::AssertUnwindSafe, sync::mpsc};
|
||||
use sp_externalities::ExternalitiesExt as _;
|
||||
use sp_core::traits::TaskExecutorExt;
|
||||
|
||||
/// Task handle (wasm).
|
||||
///
|
||||
/// This can be `join`-ed to get (blocking) the result of
|
||||
/// the spawned task execution.
|
||||
#[must_use]
|
||||
pub struct DataJoinHandle {
|
||||
receiver: mpsc::Receiver<Vec<u8>>,
|
||||
}
|
||||
|
||||
impl DataJoinHandle {
|
||||
/// Join handle returned by `spawn` function
|
||||
pub fn join(self) -> Vec<u8> {
|
||||
self.receiver.recv().expect("Spawned runtime task terminated before sending result.")
|
||||
}
|
||||
}
|
||||
|
||||
/// Spawn new runtime task (native).
|
||||
pub fn spawn(entry_point: fn(Vec<u8>) -> Vec<u8>, data: Vec<u8>) -> DataJoinHandle {
|
||||
let scheduler = sp_externalities::with_externalities(|mut ext| ext.extension::<TaskExecutorExt>()
|
||||
.expect("No task executor associated with the current context!")
|
||||
.clone()
|
||||
).expect("Spawn called outside of externalities context!");
|
||||
|
||||
let (sender, receiver) = mpsc::channel();
|
||||
let extra_scheduler = scheduler.clone();
|
||||
scheduler.spawn("parallel-runtime-spawn", Box::pin(async move {
|
||||
let result = match crate::new_async_externalities(extra_scheduler) {
|
||||
Ok(mut ext) => {
|
||||
let mut ext = AssertUnwindSafe(&mut ext);
|
||||
match std::panic::catch_unwind(move || {
|
||||
sp_externalities::set_and_run_with_externalities(
|
||||
&mut **ext,
|
||||
move || entry_point(data),
|
||||
)
|
||||
}) {
|
||||
Ok(result) => result,
|
||||
Err(panic) => {
|
||||
log::error!(
|
||||
target: "runtime",
|
||||
"Spawned task panicked: {:?}",
|
||||
panic,
|
||||
);
|
||||
|
||||
// This will drop sender without sending anything.
|
||||
return;
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
log::error!(
|
||||
target: "runtime",
|
||||
"Unable to run async task: {}",
|
||||
e,
|
||||
);
|
||||
|
||||
return;
|
||||
},
|
||||
};
|
||||
|
||||
let _ = sender.send(result);
|
||||
}));
|
||||
|
||||
DataJoinHandle { receiver }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
mod inner {
|
||||
use core::mem;
|
||||
use sp_std::prelude::*;
|
||||
|
||||
/// Dispatch wrapper for wasm blob.
|
||||
///
|
||||
/// Serves as trampoline to call any rust function with (Vec<u8>) -> Vec<u8> compiled
|
||||
/// into the runtime.
|
||||
///
|
||||
/// Function item should be provided with `func_ref`. Argument for the call
|
||||
/// will be generated from bytes at `payload_ptr` with `payload_len`.
|
||||
///
|
||||
/// NOTE: Since this dynamic dispatch function and the invoked function are compiled with
|
||||
/// the same compiler, there should be no problem with ABI incompatibility.
|
||||
extern "C" fn dispatch_wrapper(func_ref: *const u8, payload_ptr: *mut u8, payload_len: u32) -> u64 {
|
||||
let payload_len = payload_len as usize;
|
||||
let output = unsafe {
|
||||
let payload = Vec::from_raw_parts(payload_ptr, payload_len, payload_len);
|
||||
let ptr: fn(Vec<u8>) -> Vec<u8> = mem::transmute(func_ref);
|
||||
(ptr)(payload)
|
||||
};
|
||||
sp_runtime_interface::pack_ptr_and_len(output.as_ptr() as usize as _, output.len() as _)
|
||||
}
|
||||
|
||||
/// Spawn new runtime task (wasm).
|
||||
pub fn spawn(entry_point: fn(Vec<u8>) -> Vec<u8>, payload: Vec<u8>) -> DataJoinHandle {
|
||||
let func_ptr: usize = unsafe { mem::transmute(entry_point) };
|
||||
|
||||
let handle = sp_io::runtime_tasks::spawn(
|
||||
dispatch_wrapper as usize as _,
|
||||
func_ptr as u32,
|
||||
payload,
|
||||
);
|
||||
DataJoinHandle { handle }
|
||||
}
|
||||
|
||||
/// Task handle (wasm).
|
||||
///
|
||||
/// This can be `join`-ed to get (blocking) the result of
|
||||
/// the spawned task execution.
|
||||
#[must_use]
|
||||
pub struct DataJoinHandle {
|
||||
handle: u64,
|
||||
}
|
||||
|
||||
impl DataJoinHandle {
|
||||
/// Join handle returned by `spawn` function
|
||||
pub fn join(self) -> Vec<u8> {
|
||||
sp_io::runtime_tasks::join(self.handle)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub use inner::{DataJoinHandle, spawn};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
use super::*;
|
||||
|
||||
fn async_runner(mut data: Vec<u8>) -> Vec<u8> {
|
||||
data.sort();
|
||||
data
|
||||
}
|
||||
|
||||
fn async_panicker(_data: Vec<u8>) -> Vec<u8> {
|
||||
panic!("panic in async panicker!")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn basic() {
|
||||
sp_io::TestExternalities::default().execute_with(|| {
|
||||
let a1 = spawn(async_runner, vec![5, 2, 1]).join();
|
||||
assert_eq!(a1, vec![1, 2, 5]);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn panicking() {
|
||||
let res = sp_io::TestExternalities::default().execute_with_safe(||{
|
||||
spawn(async_panicker, vec![5, 2, 1]).join();
|
||||
});
|
||||
|
||||
assert!(res.unwrap_err().contains("Closure panicked"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn many_joins() {
|
||||
sp_io::TestExternalities::default().execute_with_safe(|| {
|
||||
// converges to 1 only after 1000+ steps
|
||||
let mut running_val = 9780657630u64;
|
||||
let mut data = vec![];
|
||||
let handles = (0..1024).map(
|
||||
|_| {
|
||||
running_val = if running_val % 2 == 0 {
|
||||
running_val / 2
|
||||
} else {
|
||||
3 * running_val + 1
|
||||
};
|
||||
data.push(running_val as u8);
|
||||
(spawn(async_runner, data.clone()), data.clone())
|
||||
}
|
||||
).collect::<Vec<_>>();
|
||||
|
||||
for (handle, mut data) in handles {
|
||||
let result = handle.join();
|
||||
data.sort();
|
||||
|
||||
assert_eq!(result, data);
|
||||
}
|
||||
}).expect("Failed to run with externalities");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user