sc_executor/lib.rs
1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! A crate that provides means of executing/dispatching calls into the runtime.
20//!
21//! There are a few responsibilities of this crate at the moment:
22//!
23//! - It provides an implementation of a common entrypoint for calling into the runtime, both
24//! wasm and compiled.
25//! - It defines the environment for the wasm execution, namely the host functions that are to be
26//! provided into the wasm runtime module.
27//! - It also provides the required infrastructure for executing the current wasm runtime (specified
28//! by the current value of `:code` in the provided externalities), i.e. interfacing with
29//! wasm engine used, instance cache.
30
31#![warn(missing_docs)]
32
33#[macro_use]
34mod executor;
35#[cfg(test)]
36mod integration_tests;
37mod wasm_runtime;
38
39pub use codec::Codec;
40#[allow(deprecated)]
41pub use executor::NativeElseWasmExecutor;
42pub use executor::{with_externalities_safe, NativeExecutionDispatch, WasmExecutor};
43#[doc(hidden)]
44pub use sp_core::traits::Externalities;
45pub use sp_version::{NativeVersion, RuntimeVersion};
46#[doc(hidden)]
47pub use sp_wasm_interface;
48pub use sp_wasm_interface::HostFunctions;
49pub use wasm_runtime::{read_embedded_version, WasmExecutionMethod};
50
51pub use sc_executor_common::{
52 error,
53 wasm_runtime::{HeapAllocStrategy, DEFAULT_HEAP_ALLOC_PAGES, DEFAULT_HEAP_ALLOC_STRATEGY},
54};
55pub use sc_executor_wasmtime::InstantiationStrategy as WasmtimeInstantiationStrategy;
56
57/// Extracts the runtime version of a given runtime code.
58pub trait RuntimeVersionOf {
59 /// Extract [`RuntimeVersion`] of the given `runtime_code`.
60 fn runtime_version(
61 &self,
62 ext: &mut dyn Externalities,
63 runtime_code: &sp_core::traits::RuntimeCode,
64 ) -> error::Result<RuntimeVersion>;
65}
66
67#[cfg(test)]
68mod tests {
69 use super::*;
70 use sc_executor_common::runtime_blob::RuntimeBlob;
71 use sc_runtime_test::wasm_binary_unwrap;
72 use sp_io::TestExternalities;
73
74 #[test]
75 fn call_in_interpreted_wasm_works() {
76 let mut ext = TestExternalities::default();
77 let mut ext = ext.ext();
78
79 let executor = WasmExecutor::<sp_io::SubstrateHostFunctions>::builder().build();
80 let res = executor
81 .uncached_call(
82 RuntimeBlob::uncompress_if_needed(wasm_binary_unwrap()).unwrap(),
83 &mut ext,
84 true,
85 "test_empty_return",
86 &[],
87 )
88 .unwrap();
89 assert_eq!(res, vec![0u8; 0]);
90 }
91}