|
|
|
@@ -26,7 +26,6 @@ use sp_core::{
|
|
|
|
|
};
|
|
|
|
|
use sc_runtime_test::wasm_binary_unwrap;
|
|
|
|
|
use sp_state_machine::TestExternalities as CoreTestExternalities;
|
|
|
|
|
use test_case::test_case;
|
|
|
|
|
use sp_trie::{TrieConfiguration, trie_types::Layout};
|
|
|
|
|
use sp_wasm_interface::HostFunctions as _;
|
|
|
|
|
use sp_runtime::traits::BlakeTwo256;
|
|
|
|
@@ -37,6 +36,34 @@ use crate::WasmExecutionMethod;
|
|
|
|
|
pub type TestExternalities = CoreTestExternalities<BlakeTwo256, u64>;
|
|
|
|
|
type HostFunctions = sp_io::SubstrateHostFunctions;
|
|
|
|
|
|
|
|
|
|
/// Simple macro that runs a given method as test with the available wasm execution methods.
|
|
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! test_wasm_execution {
|
|
|
|
|
($method_name:ident) => {
|
|
|
|
|
paste::item! {
|
|
|
|
|
#[test]
|
|
|
|
|
fn [<$method_name _interpreted>]() {
|
|
|
|
|
$method_name(WasmExecutionMethod::Interpreted);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
#[cfg(feature = "wasmtime")]
|
|
|
|
|
fn [<$method_name _compiled>]() {
|
|
|
|
|
$method_name(WasmExecutionMethod::Compiled);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
(interpreted_only $method_name:ident) => {
|
|
|
|
|
paste::item! {
|
|
|
|
|
#[test]
|
|
|
|
|
fn [<$method_name _interpreted>]() {
|
|
|
|
|
$method_name(WasmExecutionMethod::Interpreted);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn call_in_wasm<E: Externalities>(
|
|
|
|
|
function: &str,
|
|
|
|
|
call_data: &[u8],
|
|
|
|
@@ -59,8 +86,7 @@ fn call_in_wasm<E: Externalities>(
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test_case(WasmExecutionMethod::Interpreted)]
|
|
|
|
|
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
|
|
|
|
|
test_wasm_execution!(returning_should_work);
|
|
|
|
|
fn returning_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
let mut ext = TestExternalities::default();
|
|
|
|
|
let mut ext = ext.ext();
|
|
|
|
@@ -74,8 +100,7 @@ fn returning_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
assert_eq!(output, vec![0u8; 0]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test_case(WasmExecutionMethod::Interpreted)]
|
|
|
|
|
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
|
|
|
|
|
test_wasm_execution!(call_not_existing_function);
|
|
|
|
|
fn call_not_existing_function(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
let mut ext = TestExternalities::default();
|
|
|
|
|
let mut ext = ext.ext();
|
|
|
|
@@ -102,8 +127,7 @@ fn call_not_existing_function(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test_case(WasmExecutionMethod::Interpreted)]
|
|
|
|
|
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
|
|
|
|
|
test_wasm_execution!(call_yet_another_not_existing_function);
|
|
|
|
|
fn call_yet_another_not_existing_function(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
let mut ext = TestExternalities::default();
|
|
|
|
|
let mut ext = ext.ext();
|
|
|
|
@@ -130,8 +154,7 @@ fn call_yet_another_not_existing_function(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test_case(WasmExecutionMethod::Interpreted)]
|
|
|
|
|
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
|
|
|
|
|
test_wasm_execution!(panicking_should_work);
|
|
|
|
|
fn panicking_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
let mut ext = TestExternalities::default();
|
|
|
|
|
let mut ext = ext.ext();
|
|
|
|
@@ -161,8 +184,7 @@ fn panicking_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
assert!(output.is_err());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test_case(WasmExecutionMethod::Interpreted)]
|
|
|
|
|
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
|
|
|
|
|
test_wasm_execution!(storage_should_work);
|
|
|
|
|
fn storage_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
let mut ext = TestExternalities::default();
|
|
|
|
|
|
|
|
|
@@ -191,8 +213,7 @@ fn storage_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
assert_eq!(ext, expected);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test_case(WasmExecutionMethod::Interpreted)]
|
|
|
|
|
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
|
|
|
|
|
test_wasm_execution!(clear_prefix_should_work);
|
|
|
|
|
fn clear_prefix_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
let mut ext = TestExternalities::default();
|
|
|
|
|
{
|
|
|
|
@@ -225,8 +246,7 @@ fn clear_prefix_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
assert_eq!(expected, ext);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test_case(WasmExecutionMethod::Interpreted)]
|
|
|
|
|
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
|
|
|
|
|
test_wasm_execution!(blake2_256_should_work);
|
|
|
|
|
fn blake2_256_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
let mut ext = TestExternalities::default();
|
|
|
|
|
let mut ext = ext.ext();
|
|
|
|
@@ -250,8 +270,7 @@ fn blake2_256_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test_case(WasmExecutionMethod::Interpreted)]
|
|
|
|
|
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
|
|
|
|
|
test_wasm_execution!(blake2_128_should_work);
|
|
|
|
|
fn blake2_128_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
let mut ext = TestExternalities::default();
|
|
|
|
|
let mut ext = ext.ext();
|
|
|
|
@@ -275,8 +294,7 @@ fn blake2_128_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test_case(WasmExecutionMethod::Interpreted)]
|
|
|
|
|
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
|
|
|
|
|
test_wasm_execution!(sha2_256_should_work);
|
|
|
|
|
fn sha2_256_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
let mut ext = TestExternalities::default();
|
|
|
|
|
let mut ext = ext.ext();
|
|
|
|
@@ -306,8 +324,7 @@ fn sha2_256_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test_case(WasmExecutionMethod::Interpreted)]
|
|
|
|
|
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
|
|
|
|
|
test_wasm_execution!(twox_256_should_work);
|
|
|
|
|
fn twox_256_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
let mut ext = TestExternalities::default();
|
|
|
|
|
let mut ext = ext.ext();
|
|
|
|
@@ -335,8 +352,7 @@ fn twox_256_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test_case(WasmExecutionMethod::Interpreted)]
|
|
|
|
|
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
|
|
|
|
|
test_wasm_execution!(twox_128_should_work);
|
|
|
|
|
fn twox_128_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
let mut ext = TestExternalities::default();
|
|
|
|
|
let mut ext = ext.ext();
|
|
|
|
@@ -360,8 +376,7 @@ fn twox_128_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test_case(WasmExecutionMethod::Interpreted)]
|
|
|
|
|
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
|
|
|
|
|
test_wasm_execution!(ed25519_verify_should_work);
|
|
|
|
|
fn ed25519_verify_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
let mut ext = TestExternalities::default();
|
|
|
|
|
let mut ext = ext.ext();
|
|
|
|
@@ -397,8 +412,7 @@ fn ed25519_verify_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test_case(WasmExecutionMethod::Interpreted)]
|
|
|
|
|
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
|
|
|
|
|
test_wasm_execution!(sr25519_verify_should_work);
|
|
|
|
|
fn sr25519_verify_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
let mut ext = TestExternalities::default();
|
|
|
|
|
let mut ext = ext.ext();
|
|
|
|
@@ -434,8 +448,7 @@ fn sr25519_verify_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test_case(WasmExecutionMethod::Interpreted)]
|
|
|
|
|
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
|
|
|
|
|
test_wasm_execution!(ordered_trie_root_should_work);
|
|
|
|
|
fn ordered_trie_root_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
let mut ext = TestExternalities::default();
|
|
|
|
|
let trie_input = vec![b"zero".to_vec(), b"one".to_vec(), b"two".to_vec()];
|
|
|
|
@@ -450,8 +463,7 @@ fn ordered_trie_root_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test_case(WasmExecutionMethod::Interpreted)]
|
|
|
|
|
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
|
|
|
|
|
test_wasm_execution!(offchain_index);
|
|
|
|
|
fn offchain_index(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
let mut ext = TestExternalities::default();
|
|
|
|
|
let (offchain, _state) = testing::TestOffchainExt::new();
|
|
|
|
@@ -472,8 +484,7 @@ fn offchain_index(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test_case(WasmExecutionMethod::Interpreted)]
|
|
|
|
|
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
|
|
|
|
|
test_wasm_execution!(offchain_local_storage_should_work);
|
|
|
|
|
fn offchain_local_storage_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
let mut ext = TestExternalities::default();
|
|
|
|
|
let (offchain, state) = testing::TestOffchainExt::new();
|
|
|
|
@@ -490,8 +501,7 @@ fn offchain_local_storage_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
assert_eq!(state.read().persistent_storage.get(b"test"), Some(vec![]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test_case(WasmExecutionMethod::Interpreted)]
|
|
|
|
|
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
|
|
|
|
|
test_wasm_execution!(offchain_http_should_work);
|
|
|
|
|
fn offchain_http_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
let mut ext = TestExternalities::default();
|
|
|
|
|
let (offchain, state) = testing::TestOffchainExt::new();
|
|
|
|
@@ -519,9 +529,7 @@ fn offchain_http_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test_case(WasmExecutionMethod::Interpreted)]
|
|
|
|
|
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
|
|
|
|
|
#[should_panic(expected = "Allocator ran out of space")]
|
|
|
|
|
test_wasm_execution!(should_trap_when_heap_exhausted);
|
|
|
|
|
fn should_trap_when_heap_exhausted(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
let mut ext = TestExternalities::default();
|
|
|
|
|
|
|
|
|
@@ -531,18 +539,20 @@ fn should_trap_when_heap_exhausted(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
HostFunctions::host_functions(),
|
|
|
|
|
8,
|
|
|
|
|
);
|
|
|
|
|
executor.call_in_wasm(
|
|
|
|
|
|
|
|
|
|
let err = executor.call_in_wasm(
|
|
|
|
|
&wasm_binary_unwrap()[..],
|
|
|
|
|
None,
|
|
|
|
|
"test_exhaust_heap",
|
|
|
|
|
&[0],
|
|
|
|
|
&mut ext.ext(),
|
|
|
|
|
sp_core::traits::MissingHostFunctions::Allow,
|
|
|
|
|
).unwrap();
|
|
|
|
|
).unwrap_err();
|
|
|
|
|
|
|
|
|
|
assert!(err.contains("Allocator ran out of space"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test_case(WasmExecutionMethod::Interpreted)]
|
|
|
|
|
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
|
|
|
|
|
test_wasm_execution!(returns_mutable_static);
|
|
|
|
|
fn returns_mutable_static(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
let runtime = crate::wasm_runtime::create_wasm_runtime_with_code(
|
|
|
|
|
wasm_method,
|
|
|
|
@@ -567,8 +577,7 @@ fn returns_mutable_static(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
// returned to its initial value and thus the stack space is going to be leaked.
|
|
|
|
|
//
|
|
|
|
|
// See https://github.com/paritytech/substrate/issues/2967 for details
|
|
|
|
|
#[test_case(WasmExecutionMethod::Interpreted)]
|
|
|
|
|
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
|
|
|
|
|
test_wasm_execution!(restoration_of_globals);
|
|
|
|
|
fn restoration_of_globals(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
// Allocate 32 pages (of 65536 bytes) which gives the runtime 2048KB of heap to operate on
|
|
|
|
|
// (plus some additional space unused from the initial pages requested by the wasm runtime
|
|
|
|
@@ -596,7 +605,7 @@ fn restoration_of_globals(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
assert!(res.is_ok());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test_case(WasmExecutionMethod::Interpreted)]
|
|
|
|
|
test_wasm_execution!(interpreted_only heap_is_reset_between_calls);
|
|
|
|
|
fn heap_is_reset_between_calls(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
let runtime = crate::wasm_runtime::create_wasm_runtime_with_code(
|
|
|
|
|
wasm_method,
|
|
|
|
@@ -620,8 +629,7 @@ fn heap_is_reset_between_calls(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
instance.call_export("check_and_set_in_heap", ¶ms).unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test_case(WasmExecutionMethod::Interpreted)]
|
|
|
|
|
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
|
|
|
|
|
test_wasm_execution!(parallel_execution);
|
|
|
|
|
fn parallel_execution(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
let executor = std::sync::Arc::new(crate::WasmExecutor::new(
|
|
|
|
|
wasm_method,
|
|
|
|
@@ -656,7 +664,7 @@ fn parallel_execution(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test_case(WasmExecutionMethod::Interpreted)]
|
|
|
|
|
test_wasm_execution!(wasm_tracing_should_work);
|
|
|
|
|
fn wasm_tracing_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
|
|
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
@@ -728,10 +736,8 @@ fn wasm_tracing_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
assert_eq!(len, 2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test_case(WasmExecutionMethod::Interpreted)]
|
|
|
|
|
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
|
|
|
|
|
test_wasm_execution!(spawning_runtime_instance_should_work);
|
|
|
|
|
fn spawning_runtime_instance_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
|
|
|
|
|
let mut ext = TestExternalities::default();
|
|
|
|
|
let mut ext = ext.ext();
|
|
|
|
|
|
|
|
|
@@ -743,10 +749,8 @@ fn spawning_runtime_instance_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
).unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test_case(WasmExecutionMethod::Interpreted)]
|
|
|
|
|
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
|
|
|
|
|
test_wasm_execution!(spawning_runtime_instance_nested_should_work);
|
|
|
|
|
fn spawning_runtime_instance_nested_should_work(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
|
|
|
|
|
let mut ext = TestExternalities::default();
|
|
|
|
|
let mut ext = ext.ext();
|
|
|
|
|
|
|
|
|
@@ -758,10 +762,8 @@ fn spawning_runtime_instance_nested_should_work(wasm_method: WasmExecutionMethod
|
|
|
|
|
).unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test_case(WasmExecutionMethod::Interpreted)]
|
|
|
|
|
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
|
|
|
|
|
test_wasm_execution!(panic_in_spawned_instance_panics_on_joining_its_result);
|
|
|
|
|
fn panic_in_spawned_instance_panics_on_joining_its_result(wasm_method: WasmExecutionMethod) {
|
|
|
|
|
|
|
|
|
|
let mut ext = TestExternalities::default();
|
|
|
|
|
let mut ext = ext.ext();
|
|
|
|
|
|
|
|
|
|