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;
40pub use executor::{with_externalities_safe, WasmExecutor};
41#[doc(hidden)]
42pub use sp_core::traits::Externalities;
43pub use sp_version::RuntimeVersion;
44#[doc(hidden)]
45pub use sp_wasm_interface;
46pub use sp_wasm_interface::HostFunctions;
47pub use wasm_runtime::{read_embedded_version, WasmExecutionMethod};
48
49pub use sc_executor_common::{
50 error,
51 wasm_runtime::{HeapAllocStrategy, DEFAULT_HEAP_ALLOC_PAGES, DEFAULT_HEAP_ALLOC_STRATEGY},
52};
53pub use sc_executor_wasmtime::InstantiationStrategy as WasmtimeInstantiationStrategy;
54
55/// Extracts the runtime version of a given runtime code.
56pub trait RuntimeVersionOf {
57 /// Extract [`RuntimeVersion`] of the given `runtime_code`.
58 fn runtime_version(
59 &self,
60 ext: &mut dyn Externalities,
61 runtime_code: &sp_core::traits::RuntimeCode,
62 ) -> error::Result<RuntimeVersion>;
63}
64
65#[cfg(test)]
66mod tests {
67 use super::*;
68 use sc_executor_common::runtime_blob::RuntimeBlob;
69 use sc_runtime_test::wasm_binary_unwrap;
70 use sp_io::TestExternalities;
71
72 #[test]
73 fn call_in_interpreted_wasm_works() {
74 let mut ext = TestExternalities::default();
75 let mut ext = ext.ext();
76
77 let executor = WasmExecutor::<sp_io::SubstrateHostFunctions>::builder().build();
78 let res = executor
79 .uncached_call(
80 RuntimeBlob::uncompress_if_needed(wasm_binary_unwrap()).unwrap(),
81 &mut ext,
82 true,
83 "test_empty_return",
84 &[],
85 )
86 .unwrap();
87 assert_eq!(res, vec![0u8; 0]);
88 }
89}