1use std::{
21 borrow::Cow,
22 fmt::{Debug, Display},
23 panic::UnwindSafe,
24};
25
26pub use externalities::{Externalities, ExternalitiesExt};
27
28pub trait CodeExecutor: Sized + Send + Sync + CallInWasm + Clone + 'static {
30 type Error: Display + Debug + Send + Sync + 'static;
32
33 fn call<
36 R: codec::Codec + PartialEq,
37 NC: FnOnce() -> Result<R, String> + UnwindSafe,
38 >(
39 &self,
40 ext: &mut dyn Externalities,
41 runtime_code: &RuntimeCode,
42 method: &str,
43 data: &[u8],
44 use_native: bool,
45 native_call: Option<NC>,
46 ) -> (Result<crate::NativeOrEncoded<R>, Self::Error>, bool);
47}
48
49pub trait FetchRuntimeCode {
51 fn fetch_runtime_code<'a>(&'a self) -> Option<Cow<'a, [u8]>>;
55}
56
57pub struct WrappedRuntimeCode<'a>(pub std::borrow::Cow<'a, [u8]>);
59
60impl<'a> FetchRuntimeCode for WrappedRuntimeCode<'a> {
61 fn fetch_runtime_code<'b>(&'b self) -> Option<Cow<'b, [u8]>> {
62 Some(self.0.as_ref().into())
63 }
64}
65
66pub struct NoneFetchRuntimeCode;
68
69impl FetchRuntimeCode for NoneFetchRuntimeCode {
70 fn fetch_runtime_code<'a>(&'a self) -> Option<Cow<'a, [u8]>> {
71 None
72 }
73}
74
75#[derive(Clone)]
77pub struct RuntimeCode<'a> {
78 pub code_fetcher: &'a dyn FetchRuntimeCode,
80 pub heap_pages: Option<u64>,
84 pub hash: Vec<u8>,
89}
90
91impl<'a> PartialEq for RuntimeCode<'a> {
92 fn eq(&self, other: &Self) -> bool {
93 self.hash == other.hash
94 }
95}
96
97impl<'a> RuntimeCode<'a> {
98 pub fn empty() -> Self {
102 Self {
103 code_fetcher: &NoneFetchRuntimeCode,
104 hash: Vec::new(),
105 heap_pages: None,
106 }
107 }
108}
109
110impl<'a> FetchRuntimeCode for RuntimeCode<'a> {
111 fn fetch_runtime_code<'b>(&'b self) -> Option<Cow<'b, [u8]>> {
112 self.code_fetcher.fetch_runtime_code()
113 }
114}
115
116#[derive(Debug)]
118pub struct CodeNotFound;
119
120impl std::fmt::Display for CodeNotFound {
121 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
122 write!(f, "the storage entry `:code` doesn't have any code")
123 }
124}
125
126#[derive(Clone, Copy, Debug)]
128pub enum MissingHostFunctions {
129 Allow,
132 Disallow,
134}
135
136impl MissingHostFunctions {
137 pub fn allowed(self) -> bool {
139 matches!(self, Self::Allow)
140 }
141}
142
143pub trait CallInWasm: Send + Sync {
145 fn call_in_wasm(
155 &self,
156 wasm_code: &[u8],
157 code_hash: Option<Vec<u8>>,
158 method: &str,
159 call_data: &[u8],
160 ext: &mut dyn Externalities,
161 missing_host_functions: MissingHostFunctions,
162 ) -> Result<Vec<u8>, String>;
163}
164
165externalities::decl_extension! {
166 pub struct CallInWasmExt(Box<dyn CallInWasm>);
168}
169
170impl CallInWasmExt {
171 pub fn new<T: CallInWasm + 'static>(inner: T) -> Self {
173 Self(Box::new(inner))
174 }
175}
176
177externalities::decl_extension! {
178 pub struct TaskExecutorExt(Box<dyn SpawnNamed>);
180}
181
182impl TaskExecutorExt {
183 pub fn new(spawn_handle: impl SpawnNamed + Send + 'static) -> Self {
185 Self(Box::new(spawn_handle))
186 }
187}
188
189pub trait RuntimeSpawn: Send {
191 fn spawn_call(&self, dispatcher_ref: u32, func: u32, payload: Vec<u8>) -> u64;
197
198 fn join(&self, handle: u64) -> Vec<u8>;
200}
201
202#[cfg(feature = "std")]
203externalities::decl_extension! {
204 pub struct RuntimeSpawnExt(Box<dyn RuntimeSpawn>);
206}
207
208#[dyn_clonable::clonable]
210pub trait SpawnNamed: Clone + Send + Sync {
211 fn spawn_blocking(&self, name: &'static str, future: futures::future::BoxFuture<'static, ()>);
215 fn spawn(&self, name: &'static str, future: futures::future::BoxFuture<'static, ()>);
219}
220
221impl SpawnNamed for Box<dyn SpawnNamed> {
222 fn spawn_blocking(&self, name: &'static str, future: futures::future::BoxFuture<'static, ()>) {
223 (**self).spawn_blocking(name, future)
224 }
225
226 fn spawn(&self, name: &'static str, future: futures::future::BoxFuture<'static, ()>) {
227 (**self).spawn(name, future)
228 }
229}