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

Commit faa1196

Browse files
committed
executor: Integrate and test wasmtime execution method.
1 parent fca6848 commit faa1196

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

core/executor/src/tests.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::{WasmExecutionMethod, call_in_wasm};
1515
type TestExternalities = CoreTestExternalities<Blake2Hasher, u64>;
1616

1717
#[test_case(WasmExecutionMethod::Interpreted)]
18+
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
1819
fn returning_should_work(wasm_method: WasmExecutionMethod) {
1920
let mut ext = TestExternalities::default();
2021
let mut ext = ext.ext();
@@ -32,6 +33,7 @@ fn returning_should_work(wasm_method: WasmExecutionMethod) {
3233
}
3334

3435
#[test_case(WasmExecutionMethod::Interpreted)]
36+
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
3537
fn panicking_should_work(wasm_method: WasmExecutionMethod) {
3638
let mut ext = TestExternalities::default();
3739
let mut ext = ext.ext();
@@ -69,6 +71,7 @@ fn panicking_should_work(wasm_method: WasmExecutionMethod) {
6971
}
7072

7173
#[test_case(WasmExecutionMethod::Interpreted)]
74+
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
7275
fn storage_should_work(wasm_method: WasmExecutionMethod) {
7376
let mut ext = TestExternalities::default();
7477

@@ -98,6 +101,7 @@ fn storage_should_work(wasm_method: WasmExecutionMethod) {
98101
}
99102

100103
#[test_case(WasmExecutionMethod::Interpreted)]
104+
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
101105
fn clear_prefix_should_work(wasm_method: WasmExecutionMethod) {
102106
let mut ext = TestExternalities::default();
103107
{
@@ -131,6 +135,7 @@ fn clear_prefix_should_work(wasm_method: WasmExecutionMethod) {
131135
}
132136

133137
#[test_case(WasmExecutionMethod::Interpreted)]
138+
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
134139
fn blake2_256_should_work(wasm_method: WasmExecutionMethod) {
135140
let mut ext = TestExternalities::default();
136141
let mut ext = ext.ext();
@@ -160,6 +165,7 @@ fn blake2_256_should_work(wasm_method: WasmExecutionMethod) {
160165
}
161166

162167
#[test_case(WasmExecutionMethod::Interpreted)]
168+
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
163169
fn blake2_128_should_work(wasm_method: WasmExecutionMethod) {
164170
let mut ext = TestExternalities::default();
165171
let mut ext = ext.ext();
@@ -189,6 +195,7 @@ fn blake2_128_should_work(wasm_method: WasmExecutionMethod) {
189195
}
190196

191197
#[test_case(WasmExecutionMethod::Interpreted)]
198+
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
192199
fn twox_256_should_work(wasm_method: WasmExecutionMethod) {
193200
let mut ext = TestExternalities::default();
194201
let mut ext = ext.ext();
@@ -222,6 +229,7 @@ fn twox_256_should_work(wasm_method: WasmExecutionMethod) {
222229
}
223230

224231
#[test_case(WasmExecutionMethod::Interpreted)]
232+
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
225233
fn twox_128_should_work(wasm_method: WasmExecutionMethod) {
226234
let mut ext = TestExternalities::default();
227235
let mut ext = ext.ext();
@@ -251,6 +259,7 @@ fn twox_128_should_work(wasm_method: WasmExecutionMethod) {
251259
}
252260

253261
#[test_case(WasmExecutionMethod::Interpreted)]
262+
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
254263
fn ed25519_verify_should_work(wasm_method: WasmExecutionMethod) {
255264
let mut ext = TestExternalities::default();
256265
let mut ext = ext.ext();
@@ -292,6 +301,7 @@ fn ed25519_verify_should_work(wasm_method: WasmExecutionMethod) {
292301
}
293302

294303
#[test_case(WasmExecutionMethod::Interpreted)]
304+
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
295305
fn sr25519_verify_should_work(wasm_method: WasmExecutionMethod) {
296306
let mut ext = TestExternalities::default();
297307
let mut ext = ext.ext();
@@ -333,6 +343,7 @@ fn sr25519_verify_should_work(wasm_method: WasmExecutionMethod) {
333343
}
334344

335345
#[test_case(WasmExecutionMethod::Interpreted)]
346+
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
336347
fn ordered_trie_root_should_work(wasm_method: WasmExecutionMethod) {
337348
let mut ext = TestExternalities::default();
338349
let mut ext = ext.ext();
@@ -352,6 +363,7 @@ fn ordered_trie_root_should_work(wasm_method: WasmExecutionMethod) {
352363
}
353364

354365
#[test_case(WasmExecutionMethod::Interpreted)]
366+
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
355367
fn offchain_local_storage_should_work(wasm_method: WasmExecutionMethod) {
356368
use substrate_client::backend::OffchainStorage;
357369

@@ -375,6 +387,7 @@ fn offchain_local_storage_should_work(wasm_method: WasmExecutionMethod) {
375387
}
376388

377389
#[test_case(WasmExecutionMethod::Interpreted)]
390+
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
378391
fn offchain_http_should_work(wasm_method: WasmExecutionMethod) {
379392
let mut ext = TestExternalities::default();
380393
let (offchain, state) = testing::TestOffchainExt::new();
@@ -414,6 +427,7 @@ mod sandbox {
414427
use wabt;
415428

416429
#[test_case(WasmExecutionMethod::Interpreted)]
430+
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
417431
fn sandbox_should_work(wasm_method: WasmExecutionMethod) {
418432
let mut ext = TestExternalities::default();
419433
let mut ext = ext.ext();
@@ -454,6 +468,7 @@ mod sandbox {
454468
}
455469

456470
#[test_case(WasmExecutionMethod::Interpreted)]
471+
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
457472
fn sandbox_trap(wasm_method: WasmExecutionMethod) {
458473
let mut ext = TestExternalities::default();
459474
let mut ext = ext.ext();
@@ -483,6 +498,7 @@ mod sandbox {
483498
}
484499

485500
#[test_case(WasmExecutionMethod::Interpreted)]
501+
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
486502
fn sandbox_should_trap_when_heap_exhausted(wasm_method: WasmExecutionMethod) {
487503
let mut ext = TestExternalities::default();
488504
let mut ext = ext.ext();
@@ -513,6 +529,7 @@ mod sandbox {
513529
}
514530

515531
#[test_case(WasmExecutionMethod::Interpreted)]
532+
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
516533
fn start_called(wasm_method: WasmExecutionMethod) {
517534
let mut ext = TestExternalities::default();
518535
let mut ext = ext.ext();
@@ -559,6 +576,7 @@ mod sandbox {
559576
}
560577

561578
#[test_case(WasmExecutionMethod::Interpreted)]
579+
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
562580
fn invoke_args(wasm_method: WasmExecutionMethod) {
563581
let mut ext = TestExternalities::default();
564582
let mut ext = ext.ext();
@@ -601,6 +619,7 @@ mod sandbox {
601619
}
602620

603621
#[test_case(WasmExecutionMethod::Interpreted)]
622+
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
604623
fn return_val(wasm_method: WasmExecutionMethod) {
605624
let mut ext = TestExternalities::default();
606625
let mut ext = ext.ext();
@@ -631,6 +650,7 @@ mod sandbox {
631650
}
632651

633652
#[test_case(WasmExecutionMethod::Interpreted)]
653+
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
634654
fn unlinkable_module(wasm_method: WasmExecutionMethod) {
635655
let mut ext = TestExternalities::default();
636656
let mut ext = ext.ext();
@@ -659,6 +679,7 @@ mod sandbox {
659679
}
660680

661681
#[test_case(WasmExecutionMethod::Interpreted)]
682+
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
662683
fn corrupted_module(wasm_method: WasmExecutionMethod) {
663684
let mut ext = TestExternalities::default();
664685
let mut ext = ext.ext();
@@ -681,6 +702,7 @@ mod sandbox {
681702
}
682703

683704
#[test_case(WasmExecutionMethod::Interpreted)]
705+
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
684706
fn start_fn_ok(wasm_method: WasmExecutionMethod) {
685707
let mut ext = TestExternalities::default();
686708
let mut ext = ext.ext();
@@ -712,6 +734,7 @@ mod sandbox {
712734
}
713735

714736
#[test_case(WasmExecutionMethod::Interpreted)]
737+
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
715738
fn start_fn_traps(wasm_method: WasmExecutionMethod) {
716739
let mut ext = TestExternalities::default();
717740
let mut ext = ext.ext();

core/executor/src/wasm_runtime.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
2222
use crate::error::{Error, WasmError};
2323
use crate::wasmi_execution;
24+
#[cfg(feature = "wasmtime")]
25+
use crate::wasmtime;
2426
use log::{trace, warn};
2527
use codec::Decode;
2628
use primitives::{storage::well_known_keys, traits::Externalities};
@@ -51,6 +53,9 @@ pub trait WasmRuntime {
5153
pub enum WasmExecutionMethod {
5254
/// Uses the Wasmi interpreter.
5355
Interpreted,
56+
/// Uses the Wasmtime compiled runtime.
57+
#[cfg(feature = "wasmtime")]
58+
Compiled,
5459
}
5560

5661
/// Cache for the runtimes.
@@ -168,6 +173,10 @@ pub fn create_wasm_runtime_with_code<E: Externalities>(
168173
WasmExecutionMethod::Interpreted =>
169174
wasmi_execution::create_instance(ext, code, heap_pages)
170175
.map(|runtime| -> Box<dyn WasmRuntime> { Box::new(runtime) }),
176+
#[cfg(feature = "wasmtime")]
177+
WasmExecutionMethod::Compiled =>
178+
wasmtime::create_instance(ext, code, heap_pages)
179+
.map(|runtime| -> Box<dyn WasmRuntime> { Box::new(runtime) }),
171180
}
172181
}
173182

node/executor/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ wabt = "0.9.2"
3434
criterion = "0.3.0"
3535

3636
[features]
37+
wasmtime = [
38+
"substrate-executor/wasmtime",
39+
]
3740
stress-test = []
3841

3942
[[bench]]

node/executor/benches/bench.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ fn bench_execute_block(c: &mut Criterion) {
185185
vec![
186186
ExecutionMethod::Native,
187187
ExecutionMethod::Wasm(WasmExecutionMethod::Interpreted),
188+
#[cfg(feature = "wasmtime")]
189+
ExecutionMethod::Wasm(WasmExecutionMethod::Compiled),
188190
],
189191
);
190192
}

0 commit comments

Comments
 (0)