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

Commit

Permalink
chore(executor): make allow_missing_host_functions could be configura…
Browse files Browse the repository at this point in the history
…ble (#12015)

* make `with_instance` pub

* make `allow_missing_host_functions` could be configurable

* add method `allow_missing_host_functions`

* update usage

* update usage

* fix comments

* fix `with_instance`
  • Loading branch information
yjhmelody committed Aug 19, 2022
1 parent a357c29 commit 90d0247
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
4 changes: 2 additions & 2 deletions client/consensus/manual-seal/src/consensus/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ use std::{
/// This works by either fetching the `slot_number` from the most recent header and dividing
/// that value by `slot_duration` in order to fork chains that expect this inherent.
///
/// It produces timestamp inherents that are increaed by `slot_duraation` whenever
/// It produces timestamp inherents that are increased by `slot_duration` whenever
/// `provide_inherent_data` is called.
pub struct SlotTimestampProvider {
// holds the unix millisecnd timestamp for the most recent block
// holds the unix millisecond timestamp for the most recent block
unix_millis: atomic::AtomicU64,
// configured slot_duration in the runtime
slot_duration: SlotDuration,
Expand Down
37 changes: 23 additions & 14 deletions client/executor/src/native_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ pub struct WasmExecutor<H> {
/// The path to a directory which the executor can leverage for a file cache, e.g. put there
/// compiled artifacts.
cache_path: Option<PathBuf>,

/// Ignore missing function imports.
allow_missing_host_functions: bool,
phantom: PhantomData<H>,
}

Expand All @@ -112,6 +113,7 @@ impl<H> Clone for WasmExecutor<H> {
default_heap_pages: self.default_heap_pages,
cache: self.cache.clone(),
cache_path: self.cache_path.clone(),
allow_missing_host_functions: self.allow_missing_host_functions,
phantom: self.phantom,
}
}
Expand Down Expand Up @@ -153,10 +155,16 @@ where
runtime_cache_size,
)),
cache_path,
allow_missing_host_functions: false,
phantom: PhantomData,
}
}

/// Ignore missing function imports if set true.
pub fn allow_missing_host_functions(&mut self, allow_missing_host_functions: bool) {
self.allow_missing_host_functions = allow_missing_host_functions
}

/// Execute the given closure `f` with the latest runtime (based on `runtime_code`).
///
/// The closure `f` is expected to return `Err(_)` when there happened a `panic!` in native code
Expand All @@ -170,11 +178,10 @@ where
/// runtime is invalidated on any `panic!` to prevent a poisoned state. `ext` is already
/// implicitly handled as unwind safe, as we store it in a global variable while executing the
/// native runtime.
fn with_instance<R, F>(
pub fn with_instance<R, F>(
&self,
runtime_code: &RuntimeCode,
ext: &mut dyn Externalities,
allow_missing_host_functions: bool,
f: F,
) -> Result<R>
where
Expand All @@ -190,7 +197,7 @@ where
ext,
self.method,
self.default_heap_pages,
allow_missing_host_functions,
self.allow_missing_host_functions,
|module, instance, version, ext| {
let module = AssertUnwindSafe(module);
let instance = AssertUnwindSafe(instance);
Expand Down Expand Up @@ -353,7 +360,6 @@ where
let result = self.with_instance(
runtime_code,
ext,
false,
|module, mut instance, _onchain_version, mut ext| {
with_externalities_safe(&mut **ext, move || {
preregister_builtin_ext(module.clone());
Expand All @@ -374,7 +380,7 @@ where
ext: &mut dyn Externalities,
runtime_code: &RuntimeCode,
) -> Result<RuntimeVersion> {
self.with_instance(runtime_code, ext, false, |_module, _instance, version, _ext| {
self.with_instance(runtime_code, ext, |_module, _instance, version, _ext| {
Ok(version.cloned().ok_or_else(|| Error::ApiError("Unknown version".into())))
})
}
Expand All @@ -387,7 +393,7 @@ where
D: NativeExecutionDispatch,
{
/// Dummy field to avoid the compiler complaining about us not using `D`.
_dummy: std::marker::PhantomData<D>,
_dummy: PhantomData<D>,
/// Native runtime version info.
native_version: NativeVersion,
/// Fallback wasm executor.
Expand All @@ -414,7 +420,7 @@ impl<D: NativeExecutionDispatch> NativeElseWasmExecutor<D> {
max_runtime_instances: usize,
runtime_cache_size: u8,
) -> Self {
let wasm_executor = WasmExecutor::new(
let wasm = WasmExecutor::new(
fallback_method,
default_heap_pages,
max_runtime_instances,
Expand All @@ -425,9 +431,14 @@ impl<D: NativeExecutionDispatch> NativeElseWasmExecutor<D> {
NativeElseWasmExecutor {
_dummy: Default::default(),
native_version: D::native_version(),
wasm: wasm_executor,
wasm,
}
}

/// Ignore missing function imports if set true.
pub fn allow_missing_host_functions(&mut self, allow_missing_host_functions: bool) {
self.wasm.allow_missing_host_functions = allow_missing_host_functions
}
}

impl<D: NativeExecutionDispatch> RuntimeVersionOf for NativeElseWasmExecutor<D> {
Expand All @@ -436,10 +447,9 @@ impl<D: NativeExecutionDispatch> RuntimeVersionOf for NativeElseWasmExecutor<D>
ext: &mut dyn Externalities,
runtime_code: &RuntimeCode,
) -> Result<RuntimeVersion> {
self.wasm
.with_instance(runtime_code, ext, false, |_module, _instance, version, _ext| {
Ok(version.cloned().ok_or_else(|| Error::ApiError("Unknown version".into())))
})
self.wasm.with_instance(runtime_code, ext, |_module, _instance, version, _ext| {
Ok(version.cloned().ok_or_else(|| Error::ApiError("Unknown version".into())))
})
}
}

Expand Down Expand Up @@ -606,7 +616,6 @@ impl<D: NativeExecutionDispatch + 'static> CodeExecutor for NativeElseWasmExecut
let result = self.wasm.with_instance(
runtime_code,
ext,
false,
|module, mut instance, onchain_version, mut ext| {
let onchain_version =
onchain_version.ok_or_else(|| Error::ApiError("Unknown version".into()))?;
Expand Down

0 comments on commit 90d0247

Please sign in to comment.