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

Commit

Permalink
executor: Integrate and test wasmtime execution method.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimpo committed Oct 21, 2019
1 parent fca6848 commit faa1196
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
23 changes: 23 additions & 0 deletions core/executor/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{WasmExecutionMethod, call_in_wasm};
type TestExternalities = CoreTestExternalities<Blake2Hasher, u64>;

#[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();
Expand All @@ -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();
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();
{
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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;

Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
9 changes: 9 additions & 0 deletions core/executor/src/wasm_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -168,6 +173,10 @@ pub fn create_wasm_runtime_with_code<E: Externalities>(
WasmExecutionMethod::Interpreted =>
wasmi_execution::create_instance(ext, code, heap_pages)
.map(|runtime| -> Box<dyn WasmRuntime> { Box::new(runtime) }),
#[cfg(feature = "wasmtime")]
WasmExecutionMethod::Compiled =>
wasmtime::create_instance(ext, code, heap_pages)
.map(|runtime| -> Box<dyn WasmRuntime> { Box::new(runtime) }),
}
}

Expand Down
3 changes: 3 additions & 0 deletions node/executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ wabt = "0.9.2"
criterion = "0.3.0"

[features]
wasmtime = [
"substrate-executor/wasmtime",
]
stress-test = []

[[bench]]
Expand Down
2 changes: 2 additions & 0 deletions node/executor/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
],
);
}

0 comments on commit faa1196

Please sign in to comment.