|
18 | 18 |
|
19 | 19 | //! Async externalities. |
20 | 20 |
|
21 | | -use std::{ |
22 | | - any::{TypeId, Any}, |
23 | | - sync::Arc, |
24 | | -}; |
| 21 | +use std::any::{TypeId, Any}; |
25 | 22 | use sp_core::{ |
26 | 23 | storage::{ChildInfo, TrackedStorageKey}, |
27 | 24 | traits::{Externalities, SpawnNamed, TaskExecutorExt}, |
28 | 25 | }; |
29 | | -use sp_io::RuntimeSpawnExt; |
| 26 | +use crate::{RuntimeSpawnExt, RuntimeSpawn}; |
30 | 27 | use sp_externalities::{Extensions, ExternalitiesExt as _}; |
31 | | -use crate::native_executor::RuntimeInstanceSpawn; |
32 | 28 |
|
33 | 29 | /// Simple state-less externalities for use in async context. |
34 | 30 | /// |
35 | 31 | /// Will panic if anything is accessing the storage. |
36 | 32 | #[derive(Debug)] |
37 | 33 | pub struct AsyncExternalities { |
38 | | - extensions: Extensions, |
| 34 | + extensions: Extensions, |
39 | 35 | } |
40 | 36 |
|
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> { |
45 | 39 | let extensions = Extensions::default(); |
46 | 40 | let mut res = AsyncExternalities { extensions }; |
47 | 41 | let mut ext = &mut res as &mut dyn Externalities; |
48 | 42 | ext.register_extension::<TaskExecutorExt>(TaskExecutorExt(scheduler.clone())) |
49 | 43 | .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.")?; |
56 | 44 |
|
57 | 45 | Ok(res) |
58 | 46 | } |
59 | 47 |
|
| 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 | + |
60 | 62 | type StorageKey = Vec<u8>; |
61 | 63 |
|
62 | 64 | type StorageValue = Vec<u8>; |
|
0 commit comments