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

Commit 7ad29c8

Browse files
committed
Merge branch 'master' into dp-jsonrpsee-integration-2
2 parents e2fdd1a + 60b51e3 commit 7ad29c8

File tree

15 files changed

+363
-251
lines changed

15 files changed

+363
-251
lines changed

.github/workflows/polkadot-companion-labels.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
with:
1515
authToken: ${{ secrets.GITHUB_TOKEN }}
1616
ref: ${{ github.event.pull_request.head.sha }}
17-
contexts: 'continuous-integration/gitlab-check-polkadot-companion-build'
17+
contexts: 'continuous-integration/gitlab-check-dependent-polkadot'
1818
timeout: 1800
1919
notPresentTimeout: 3600 # It can take quite a while before the job starts on Gitlab when the CI queue is large
2020
failureStates: failure

Cargo.lock

Lines changed: 1 addition & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ members = [
1111
"bin/node/executor",
1212
"bin/node/primitives",
1313
"bin/node/rpc",
14-
"bin/node/rpc-client",
1514
"bin/node/runtime",
1615
"bin/node/testing",
1716
"bin/utils/chain-spec-builder",

client/executor/runtime-test/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ sp-runtime = { version = "4.0.0-dev", default-features = false, path = "../../..
1919
sp-sandbox = { version = "0.10.0-dev", default-features = false, path = "../../../primitives/sandbox" }
2020
sp-std = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/std" }
2121
sp-tasks = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/tasks" }
22+
paste = "1.0.4"
2223

2324
[build-dependencies]
2425
substrate-wasm-builder = { version = "5.0.0-dev", path = "../../../utils/wasm-builder" }

client/executor/runtime-test/src/lib.rs

Lines changed: 107 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use sp_runtime::{
3030
traits::{BlakeTwo256, Hash},
3131
};
3232
#[cfg(not(feature = "std"))]
33-
use sp_sandbox::Value;
33+
use sp_sandbox::{SandboxEnvironmentBuilder, SandboxInstance, SandboxMemory, Value};
3434

3535
extern "C" {
3636
#[allow(dead_code)]
@@ -183,61 +183,6 @@ sp_core::wasm_export_functions! {
183183
).as_ref().to_vec()
184184
}
185185

186-
fn test_sandbox(code: Vec<u8>) -> bool {
187-
execute_sandboxed(&code, &[]).is_ok()
188-
}
189-
190-
fn test_sandbox_args(code: Vec<u8>) -> bool {
191-
execute_sandboxed(
192-
&code,
193-
&[
194-
Value::I32(0x12345678),
195-
Value::I64(0x1234567887654321),
196-
],
197-
).is_ok()
198-
}
199-
200-
fn test_sandbox_return_val(code: Vec<u8>) -> bool {
201-
let ok = match execute_sandboxed(
202-
&code,
203-
&[
204-
Value::I32(0x1336),
205-
]
206-
) {
207-
Ok(sp_sandbox::ReturnValue::Value(Value::I32(0x1337))) => true,
208-
_ => false,
209-
};
210-
211-
ok
212-
}
213-
214-
fn test_sandbox_instantiate(code: Vec<u8>) -> u8 {
215-
let env_builder = sp_sandbox::EnvironmentDefinitionBuilder::new();
216-
let code = match sp_sandbox::Instance::new(&code, &env_builder, &mut ()) {
217-
Ok(_) => 0,
218-
Err(sp_sandbox::Error::Module) => 1,
219-
Err(sp_sandbox::Error::Execution) => 2,
220-
Err(sp_sandbox::Error::OutOfBounds) => 3,
221-
};
222-
223-
code
224-
}
225-
226-
fn test_sandbox_get_global_val(code: Vec<u8>) -> i64 {
227-
let env_builder = sp_sandbox::EnvironmentDefinitionBuilder::new();
228-
let instance = if let Ok(i) = sp_sandbox::Instance::new(&code, &env_builder, &mut ()) {
229-
i
230-
} else {
231-
return 20;
232-
};
233-
234-
match instance.get_global_val("test_global") {
235-
Some(sp_sandbox::Value::I64(val)) => val,
236-
None => 30,
237-
_ => 40,
238-
}
239-
}
240-
241186
fn test_offchain_index_set() {
242187
sp_io::offchain_index::set(b"k", b"v");
243188
}
@@ -408,15 +353,112 @@ mod tasks {
408353
}
409354
}
410355

356+
/// A macro to define a test entrypoint for each available sandbox executor.
357+
macro_rules! wasm_export_sandbox_test_functions {
358+
(
359+
$(
360+
fn $name:ident<T>(
361+
$( $arg_name:ident: $arg_ty:ty ),* $(,)?
362+
) $( -> $ret_ty:ty )? where T: SandboxInstance<$state:ty> $(,)?
363+
{ $( $fn_impl:tt )* }
364+
)*
365+
) => {
366+
$(
367+
#[cfg(not(feature = "std"))]
368+
fn $name<T>( $($arg_name: $arg_ty),* ) $( -> $ret_ty )? where T: SandboxInstance<$state> {
369+
$( $fn_impl )*
370+
}
371+
372+
paste::paste! {
373+
sp_core::wasm_export_functions! {
374+
fn [<$name _host>]( $($arg_name: $arg_ty),* ) $( -> $ret_ty )? {
375+
$name::<sp_sandbox::host_executor::Instance<$state>>( $( $arg_name ),* )
376+
}
377+
378+
fn [<$name _embedded>]( $($arg_name: $arg_ty),* ) $( -> $ret_ty )? {
379+
$name::<sp_sandbox::embedded_executor::Instance<$state>>( $( $arg_name ),* )
380+
}
381+
}
382+
}
383+
)*
384+
};
385+
}
386+
387+
wasm_export_sandbox_test_functions! {
388+
fn test_sandbox<T>(code: Vec<u8>) -> bool
389+
where
390+
T: SandboxInstance<State>,
391+
{
392+
execute_sandboxed::<T>(&code, &[]).is_ok()
393+
}
394+
395+
fn test_sandbox_args<T>(code: Vec<u8>) -> bool
396+
where
397+
T: SandboxInstance<State>,
398+
{
399+
execute_sandboxed::<T>(&code, &[Value::I32(0x12345678), Value::I64(0x1234567887654321)])
400+
.is_ok()
401+
}
402+
403+
fn test_sandbox_return_val<T>(code: Vec<u8>) -> bool
404+
where
405+
T: SandboxInstance<State>,
406+
{
407+
let ok = match execute_sandboxed::<T>(&code, &[Value::I32(0x1336)]) {
408+
Ok(sp_sandbox::ReturnValue::Value(Value::I32(0x1337))) => true,
409+
_ => false,
410+
};
411+
412+
ok
413+
}
414+
415+
fn test_sandbox_instantiate<T>(code: Vec<u8>) -> u8
416+
where
417+
T: SandboxInstance<()>,
418+
{
419+
let env_builder = T::EnvironmentBuilder::new();
420+
let code = match T::new(&code, &env_builder, &mut ()) {
421+
Ok(_) => 0,
422+
Err(sp_sandbox::Error::Module) => 1,
423+
Err(sp_sandbox::Error::Execution) => 2,
424+
Err(sp_sandbox::Error::OutOfBounds) => 3,
425+
};
426+
427+
code
428+
}
429+
430+
fn test_sandbox_get_global_val<T>(code: Vec<u8>) -> i64
431+
where
432+
T: SandboxInstance<()>,
433+
{
434+
let env_builder = T::EnvironmentBuilder::new();
435+
let instance = if let Ok(i) = T::new(&code, &env_builder, &mut ()) {
436+
i
437+
} else {
438+
return 20
439+
};
440+
441+
match instance.get_global_val("test_global") {
442+
Some(sp_sandbox::Value::I64(val)) => val,
443+
None => 30,
444+
_ => 40,
445+
}
446+
}
447+
}
448+
411449
#[cfg(not(feature = "std"))]
412-
fn execute_sandboxed(
450+
struct State {
451+
counter: u32,
452+
}
453+
454+
#[cfg(not(feature = "std"))]
455+
fn execute_sandboxed<T>(
413456
code: &[u8],
414457
args: &[Value],
415-
) -> Result<sp_sandbox::ReturnValue, sp_sandbox::HostError> {
416-
struct State {
417-
counter: u32,
418-
}
419-
458+
) -> Result<sp_sandbox::ReturnValue, sp_sandbox::HostError>
459+
where
460+
T: sp_sandbox::SandboxInstance<State>,
461+
{
420462
fn env_assert(
421463
_e: &mut State,
422464
args: &[Value],
@@ -446,10 +488,10 @@ fn execute_sandboxed(
446488
let mut state = State { counter: 0 };
447489

448490
let env_builder = {
449-
let mut env_builder = sp_sandbox::EnvironmentDefinitionBuilder::new();
491+
let mut env_builder = T::EnvironmentBuilder::new();
450492
env_builder.add_host_func("env", "assert", env_assert);
451493
env_builder.add_host_func("env", "inc_counter", env_inc_counter);
452-
let memory = match sp_sandbox::Memory::new(1, Some(16)) {
494+
let memory = match T::Memory::new(1, Some(16)) {
453495
Ok(m) => m,
454496
Err(_) => unreachable!(
455497
"
@@ -462,7 +504,7 @@ fn execute_sandboxed(
462504
env_builder
463505
};
464506

465-
let mut instance = sp_sandbox::Instance::new(code, &env_builder, &mut state)?;
507+
let mut instance = T::new(code, &env_builder, &mut state)?;
466508
let result = instance.invoke("call", args, &mut state);
467509

468510
result.map_err(|_| sp_sandbox::HostError)

client/executor/src/integration_tests/mod.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,53 @@ macro_rules! test_wasm_execution {
7171
};
7272
}
7373

74+
/// A macro to run a given test for each available WASM execution method *and* for each
75+
/// sandbox execution method.
76+
#[macro_export]
77+
macro_rules! test_wasm_execution_sandbox {
78+
($method_name:ident) => {
79+
paste::item! {
80+
#[test]
81+
fn [<$method_name _interpreted_host_executor>]() {
82+
$method_name(WasmExecutionMethod::Interpreted, "_host");
83+
}
84+
85+
#[test]
86+
fn [<$method_name _interpreted_embedded_executor>]() {
87+
$method_name(WasmExecutionMethod::Interpreted, "_embedded");
88+
}
89+
90+
#[test]
91+
#[cfg(feature = "wasmtime")]
92+
fn [<$method_name _compiled_host_executor>]() {
93+
$method_name(WasmExecutionMethod::Compiled, "_host");
94+
}
95+
96+
#[test]
97+
#[cfg(feature = "wasmtime")]
98+
fn [<$method_name _compiled_embedded_executor>]() {
99+
$method_name(WasmExecutionMethod::Compiled, "_embedded");
100+
}
101+
}
102+
};
103+
104+
(interpreted_only $method_name:ident) => {
105+
paste::item! {
106+
#[test]
107+
fn [<$method_name _interpreted_host_executor>]() {
108+
$method_name(WasmExecutionMethod::Interpreted, "_host");
109+
}
110+
}
111+
112+
paste::item! {
113+
#[test]
114+
fn [<$method_name _interpreted_embedded_executor>]() {
115+
$method_name(WasmExecutionMethod::Interpreted, "_embedded");
116+
}
117+
}
118+
};
119+
}
120+
74121
fn call_in_wasm<E: Externalities>(
75122
function: &str,
76123
call_data: &[u8],

0 commit comments

Comments
 (0)