From b23af9423f29ba6a6c4e28c8747c698e405b0174 Mon Sep 17 00:00:00 2001 From: Jim Posen Date: Mon, 21 Oct 2019 16:57:46 +0200 Subject: [PATCH] executor: Integrate and test wasmtime execution method. --- core/executor/src/tests.rs | 23 +++++++++++++++++++++++ core/executor/src/wasm_runtime.rs | 9 +++++++++ node/executor/Cargo.toml | 3 +++ node/executor/benches/bench.rs | 2 ++ 4 files changed, 37 insertions(+) diff --git a/core/executor/src/tests.rs b/core/executor/src/tests.rs index 2671e0a5a3f79..97ac49d799a80 100644 --- a/core/executor/src/tests.rs +++ b/core/executor/src/tests.rs @@ -15,6 +15,7 @@ use crate::{WasmExecutionMethod, call_in_wasm}; type TestExternalities = CoreTestExternalities; #[test_case(WasmExecutionMethod::Interpreted)] +#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))] fn returning_should_work(wasm_method: WasmExecutionMethod) { let mut ext = TestExternalities::default(); let mut ext = ext.ext(); @@ -32,6 +33,7 @@ fn returning_should_work(wasm_method: WasmExecutionMethod) { } #[test_case(WasmExecutionMethod::Interpreted)] +#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))] fn panicking_should_work(wasm_method: WasmExecutionMethod) { let mut ext = TestExternalities::default(); let mut ext = ext.ext(); @@ -69,6 +71,7 @@ fn panicking_should_work(wasm_method: WasmExecutionMethod) { } #[test_case(WasmExecutionMethod::Interpreted)] +#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))] fn storage_should_work(wasm_method: WasmExecutionMethod) { let mut ext = TestExternalities::default(); @@ -98,6 +101,7 @@ fn storage_should_work(wasm_method: WasmExecutionMethod) { } #[test_case(WasmExecutionMethod::Interpreted)] +#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))] fn clear_prefix_should_work(wasm_method: WasmExecutionMethod) { let mut ext = TestExternalities::default(); { @@ -131,6 +135,7 @@ fn clear_prefix_should_work(wasm_method: WasmExecutionMethod) { } #[test_case(WasmExecutionMethod::Interpreted)] +#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))] fn blake2_256_should_work(wasm_method: WasmExecutionMethod) { let mut ext = TestExternalities::default(); let mut ext = ext.ext(); @@ -160,6 +165,7 @@ fn blake2_256_should_work(wasm_method: WasmExecutionMethod) { } #[test_case(WasmExecutionMethod::Interpreted)] +#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))] fn blake2_128_should_work(wasm_method: WasmExecutionMethod) { let mut ext = TestExternalities::default(); let mut ext = ext.ext(); @@ -189,6 +195,7 @@ fn blake2_128_should_work(wasm_method: WasmExecutionMethod) { } #[test_case(WasmExecutionMethod::Interpreted)] +#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))] fn twox_256_should_work(wasm_method: WasmExecutionMethod) { let mut ext = TestExternalities::default(); let mut ext = ext.ext(); @@ -222,6 +229,7 @@ fn twox_256_should_work(wasm_method: WasmExecutionMethod) { } #[test_case(WasmExecutionMethod::Interpreted)] +#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))] fn twox_128_should_work(wasm_method: WasmExecutionMethod) { let mut ext = TestExternalities::default(); let mut ext = ext.ext(); @@ -251,6 +259,7 @@ fn twox_128_should_work(wasm_method: WasmExecutionMethod) { } #[test_case(WasmExecutionMethod::Interpreted)] +#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))] fn ed25519_verify_should_work(wasm_method: WasmExecutionMethod) { let mut ext = TestExternalities::default(); let mut ext = ext.ext(); @@ -292,6 +301,7 @@ fn ed25519_verify_should_work(wasm_method: WasmExecutionMethod) { } #[test_case(WasmExecutionMethod::Interpreted)] +#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))] fn sr25519_verify_should_work(wasm_method: WasmExecutionMethod) { let mut ext = TestExternalities::default(); let mut ext = ext.ext(); @@ -333,6 +343,7 @@ fn sr25519_verify_should_work(wasm_method: WasmExecutionMethod) { } #[test_case(WasmExecutionMethod::Interpreted)] +#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))] fn ordered_trie_root_should_work(wasm_method: WasmExecutionMethod) { let mut ext = TestExternalities::default(); let mut ext = ext.ext(); @@ -352,6 +363,7 @@ fn ordered_trie_root_should_work(wasm_method: WasmExecutionMethod) { } #[test_case(WasmExecutionMethod::Interpreted)] +#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))] fn offchain_local_storage_should_work(wasm_method: WasmExecutionMethod) { use substrate_client::backend::OffchainStorage; @@ -375,6 +387,7 @@ fn offchain_local_storage_should_work(wasm_method: WasmExecutionMethod) { } #[test_case(WasmExecutionMethod::Interpreted)] +#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))] fn offchain_http_should_work(wasm_method: WasmExecutionMethod) { let mut ext = TestExternalities::default(); let (offchain, state) = testing::TestOffchainExt::new(); @@ -414,6 +427,7 @@ mod sandbox { use wabt; #[test_case(WasmExecutionMethod::Interpreted)] + #[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))] fn sandbox_should_work(wasm_method: WasmExecutionMethod) { let mut ext = TestExternalities::default(); let mut ext = ext.ext(); @@ -454,6 +468,7 @@ mod sandbox { } #[test_case(WasmExecutionMethod::Interpreted)] + #[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))] fn sandbox_trap(wasm_method: WasmExecutionMethod) { let mut ext = TestExternalities::default(); let mut ext = ext.ext(); @@ -483,6 +498,7 @@ mod sandbox { } #[test_case(WasmExecutionMethod::Interpreted)] + #[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))] fn sandbox_should_trap_when_heap_exhausted(wasm_method: WasmExecutionMethod) { let mut ext = TestExternalities::default(); let mut ext = ext.ext(); @@ -513,6 +529,7 @@ mod sandbox { } #[test_case(WasmExecutionMethod::Interpreted)] + #[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))] fn start_called(wasm_method: WasmExecutionMethod) { let mut ext = TestExternalities::default(); let mut ext = ext.ext(); @@ -559,6 +576,7 @@ mod sandbox { } #[test_case(WasmExecutionMethod::Interpreted)] + #[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))] fn invoke_args(wasm_method: WasmExecutionMethod) { let mut ext = TestExternalities::default(); let mut ext = ext.ext(); @@ -601,6 +619,7 @@ mod sandbox { } #[test_case(WasmExecutionMethod::Interpreted)] + #[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))] fn return_val(wasm_method: WasmExecutionMethod) { let mut ext = TestExternalities::default(); let mut ext = ext.ext(); @@ -631,6 +650,7 @@ mod sandbox { } #[test_case(WasmExecutionMethod::Interpreted)] + #[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))] fn unlinkable_module(wasm_method: WasmExecutionMethod) { let mut ext = TestExternalities::default(); let mut ext = ext.ext(); @@ -659,6 +679,7 @@ mod sandbox { } #[test_case(WasmExecutionMethod::Interpreted)] + #[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))] fn corrupted_module(wasm_method: WasmExecutionMethod) { let mut ext = TestExternalities::default(); let mut ext = ext.ext(); @@ -681,6 +702,7 @@ mod sandbox { } #[test_case(WasmExecutionMethod::Interpreted)] + #[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))] fn start_fn_ok(wasm_method: WasmExecutionMethod) { let mut ext = TestExternalities::default(); let mut ext = ext.ext(); @@ -712,6 +734,7 @@ mod sandbox { } #[test_case(WasmExecutionMethod::Interpreted)] + #[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))] fn start_fn_traps(wasm_method: WasmExecutionMethod) { let mut ext = TestExternalities::default(); let mut ext = ext.ext(); diff --git a/core/executor/src/wasm_runtime.rs b/core/executor/src/wasm_runtime.rs index 8d2291fe04893..56afd8f45ccce 100644 --- a/core/executor/src/wasm_runtime.rs +++ b/core/executor/src/wasm_runtime.rs @@ -21,6 +21,8 @@ use crate::error::{Error, WasmError}; use crate::wasmi_execution; +#[cfg(feature = "wasmtime")] +use crate::wasmtime; use log::{trace, warn}; use codec::Decode; use primitives::{storage::well_known_keys, traits::Externalities}; @@ -51,6 +53,9 @@ pub trait WasmRuntime { pub enum WasmExecutionMethod { /// Uses the Wasmi interpreter. Interpreted, + /// Uses the Wasmtime compiled runtime. + #[cfg(feature = "wasmtime")] + Compiled, } /// Cache for the runtimes. @@ -168,6 +173,10 @@ pub fn create_wasm_runtime_with_code( WasmExecutionMethod::Interpreted => wasmi_execution::create_instance(ext, code, heap_pages) .map(|runtime| -> Box { Box::new(runtime) }), + #[cfg(feature = "wasmtime")] + WasmExecutionMethod::Compiled => + wasmtime::create_instance(ext, code, heap_pages) + .map(|runtime| -> Box { Box::new(runtime) }), } } diff --git a/node/executor/Cargo.toml b/node/executor/Cargo.toml index ebde7f434bffb..93f29910edbe0 100644 --- a/node/executor/Cargo.toml +++ b/node/executor/Cargo.toml @@ -34,6 +34,9 @@ wabt = "0.9.2" criterion = "0.3.0" [features] +wasmtime = [ + "substrate-executor/wasmtime", +] stress-test = [] [[bench]] diff --git a/node/executor/benches/bench.rs b/node/executor/benches/bench.rs index fafd20fb5b41f..5d00ce0ba81c5 100644 --- a/node/executor/benches/bench.rs +++ b/node/executor/benches/bench.rs @@ -185,6 +185,8 @@ fn bench_execute_block(c: &mut Criterion) { vec![ ExecutionMethod::Native, ExecutionMethod::Wasm(WasmExecutionMethod::Interpreted), + #[cfg(feature = "wasmtime")] + ExecutionMethod::Wasm(WasmExecutionMethod::Compiled), ], ); }