Skip to content

Commit 8b30e4f

Browse files
committed
Add WASM tests
1 parent b739734 commit 8b30e4f

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ futures = "0.3.31"
1919
reth-exex-test-utils = { git = "https://github.com/paradigmxyz/reth", rev = "bbbc1c143432c22878e0428b90201ba625ef6187" }
2020
tokio = { version = "1.0", features = ["full"] }
2121
alloy-sol-types = { version = "0.8.23", features = ["json"] }
22+
hex = "0.4.3"
2223

wasm-runner/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ wasmtime = "30.0.2"
2323
[dev-dependencies]
2424
reth-exex-test-utils.workspace = true
2525
tokio.workspace = true
26+
hex.workspace = true

wasm-runner/src/main.rs

+34-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl ExecutionRequestEvent {
2929
fn on_req(req: &ExecutionRequestEvent) -> eyre::Result<()> {
3030
let engine = Engine::default();
3131
let mut store = Store::new(&engine, ());
32-
let module = Module::new(&engine, &req.code)
32+
let module = Module::from_binary(&engine, &req.code)
3333
.map_err(|e| eyre!("WASM error: {e:?}"))?;
3434
let linker = Linker::new(&engine);
3535
let instance = linker
@@ -107,9 +107,12 @@ fn main() -> eyre::Result<()> {
107107

108108
#[cfg(test)]
109109
mod tests {
110+
use super::*;
111+
110112
use reth_execution_types::{Chain, ExecutionOutcome};
111113
use reth_exex_test_utils::{PollOnce, test_exex_context};
112114
use std::pin::pin;
115+
use wasmtime::CodeBuilder;
113116

114117
#[tokio::test]
115118
async fn test_exex() -> eyre::Result<()> {
@@ -129,4 +132,34 @@ mod tests {
129132

130133
Ok(())
131134
}
135+
136+
#[test]
137+
fn test_wasm_code_execution_success() {
138+
let source = r#"
139+
(module
140+
(import "wasi_snapshot_preview1" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32)))
141+
(memory (export "memory") 1)
142+
(data (i32.const 8) "Hello, borker!\n")
143+
144+
(func $main (export "_start")
145+
(i32.store (i32.const 0) (i32.const 8))
146+
(i32.store (i32.const 4) (i32.const 14))
147+
(call $fd_write (i32.const 1) (i32.const 0) (i32.const 1) (i32.const 0))
148+
drop
149+
)
150+
)
151+
"#;
152+
let code = wat2wasm(&source).unwrap();
153+
let actual_res = on_req(&ExecutionRequestEvent { code });
154+
dbg!(&actual_res);
155+
assert!(actual_res.is_ok());
156+
}
157+
158+
fn wat2wasm(source: &str) -> eyre::Result<Vec<u8>> {
159+
Ok(CodeBuilder::new(&Engine::default())
160+
.wasm_binary_or_text(&source.as_bytes(), None)
161+
.map_err(|e| eyre!("WASM Compilation Error: {e:?}"))?
162+
.compile_module_serialized()
163+
.map_err(|e| eyre!("WASM Compilation Error: {e:?}"))?)
164+
}
132165
}

0 commit comments

Comments
 (0)