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

Commit b001b81

Browse files
committed
move ext
1 parent 2752db3 commit b001b81

5 files changed

Lines changed: 49 additions & 23 deletions

File tree

client/executor/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#![warn(missing_docs)]
3232
#![recursion_limit="128"]
3333

34-
mod async_externalities;
3534
#[macro_use]
3635
mod native_executor;
3736
mod wasm_runtime;

client/executor/src/native_executor.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use crate::{
2020
RuntimeInfo, error::{Error, Result},
2121
wasm_runtime::{RuntimeCache, WasmExecutionMethod},
22-
async_externalities::new_async_externalities,
2322
};
2423
use sp_version::{NativeVersion, RuntimeVersion};
2524
use codec::{Decode, Encode};
@@ -31,7 +30,7 @@ use std::{result, panic::{UnwindSafe, AssertUnwindSafe}, sync::Arc, collections:
3130
use sp_wasm_interface::{HostFunctions, Function};
3231
use sc_executor_common::wasm_runtime::{WasmInstance, WasmModule, CallSite};
3332
use sp_externalities::ExternalitiesExt as _;
34-
use sp_io::RuntimeSpawnExt;
33+
use sp_io::{RuntimeSpawnExt, new_async_externalities};
3534

3635
/// Default num of pages for the heap
3736
const DEFAULT_HEAP_PAGES: u64 = 1024;
@@ -306,8 +305,10 @@ impl sp_io::RuntimeSpawn for RuntimeInstanceSpawn {
306305
self.scheduler.spawn("forked runtime invoke", Box::pin(async move {
307306
let module = AssertUnwindSafe(module);
308307
let result = with_externalities_safe(
309-
&mut new_async_externalities(scheduler, module.clone())
310-
.expect("Failed to setup externalities for async context"),
308+
&mut new_async_externalities(scheduler.clone())
309+
.expect("Failed to setup externalities for async context")
310+
.with_runtime_ext(Box::new(RuntimeInstanceSpawn::new(module.clone(), scheduler)))
311+
.expect("Failed to setup runtime extension for async externalities"),
311312
move || {
312313

313314
// FIXME: Should be refactored to shared "instance factory".

client/executor/src/async_externalities.rs renamed to primitives/io/src/async_externalities.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,47 @@
1818

1919
//! Async externalities.
2020
21-
use std::{
22-
any::{TypeId, Any},
23-
sync::Arc,
24-
};
21+
use std::any::{TypeId, Any};
2522
use sp_core::{
2623
storage::{ChildInfo, TrackedStorageKey},
2724
traits::{Externalities, SpawnNamed, TaskExecutorExt},
2825
};
29-
use sp_io::RuntimeSpawnExt;
26+
use crate::{RuntimeSpawnExt, RuntimeSpawn};
3027
use sp_externalities::{Extensions, ExternalitiesExt as _};
31-
use crate::native_executor::RuntimeInstanceSpawn;
3228

3329
/// Simple state-less externalities for use in async context.
3430
///
3531
/// Will panic if anything is accessing the storage.
3632
#[derive(Debug)]
3733
pub struct AsyncExternalities {
38-
extensions: Extensions,
34+
extensions: Extensions,
3935
}
4036

41-
pub fn new_async_externalities(
42-
scheduler: Box<dyn SpawnNamed>,
43-
module: Arc<dyn sc_executor_common::wasm_runtime::WasmModule>,
44-
) -> Result<AsyncExternalities, &'static str> {
37+
/// New Async externalities.
38+
pub fn new_async_externalities(scheduler: Box<dyn SpawnNamed>) -> Result<AsyncExternalities, &'static str> {
4539
let extensions = Extensions::default();
4640
let mut res = AsyncExternalities { extensions };
4741
let mut ext = &mut res as &mut dyn Externalities;
4842
ext.register_extension::<TaskExecutorExt>(TaskExecutorExt(scheduler.clone()))
4943
.map_err(|_| "Failed to register task executor extension.")?;
50-
ext.register_extension::<RuntimeSpawnExt>(
51-
RuntimeSpawnExt(Box::new(
52-
RuntimeInstanceSpawn::new(module, scheduler)
53-
))
54-
)
55-
.map_err(|_| "Failed to register task executor extension.")?;
5644

5745
Ok(res)
5846
}
5947

48+
impl AsyncExternalities {
49+
/// Extend async externalities with the ability to spawn wasm instances.
50+
pub fn with_runtime_ext(
51+
mut self,
52+
runtime_ext: Box<dyn RuntimeSpawn>,
53+
) -> Result<Self, &'static str> {
54+
let mut ext = &mut self as &mut dyn Externalities;
55+
ext.register_extension::<RuntimeSpawnExt>(RuntimeSpawnExt(runtime_ext))
56+
.map_err(|_| "Failed to register task executor extension.")?;
57+
58+
Ok(self)
59+
}
60+
}
61+
6062
type StorageKey = Vec<u8>;
6163

6264
type StorageValue = Vec<u8>;

primitives/io/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ use sp_externalities::{ExternalitiesExt, Externalities};
6161
#[cfg(feature = "std")]
6262
mod batch_verifier;
6363

64+
#[cfg(feature = "std")]
65+
mod async_externalities;
66+
67+
#[cfg(feature = "std")]
68+
pub use async_externalities::{new_async_externalities, AsyncExternalities};
69+
6470
pub mod tasks;
6571

6672
#[cfg(feature = "std")]

primitives/io/src/tasks.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,26 @@ mod inner {
4949
).ok_or("Spawn called outside of externalities context!")?;
5050

5151
let (sender, receiver) = mpsc::channel();
52+
let extra_scheduler = scheduler.clone();
5253
scheduler.spawn("parallel-runtime-spawn", Box::pin(async move {
53-
let result = entry_point(data);
54+
let result = match crate::new_async_externalities(extra_scheduler) {
55+
Ok(mut ext) => {
56+
sp_externalities::set_and_run_with_externalities(
57+
&mut ext,
58+
move || entry_point(data),
59+
)
60+
},
61+
Err(e) => {
62+
log::warn!(
63+
target: "runtime",
64+
"Unable to run async task: {}",
65+
e,
66+
);
67+
68+
return;
69+
},
70+
};
71+
5472
let _ = sender.send(result);
5573
}));
5674

0 commit comments

Comments
 (0)