mirror of
https://github.com/tlsnotary/tlsn.git
synced 2026-01-11 15:47:58 -05:00
Compare commits
2 Commits
dev
...
poc/wasmti
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
317f9a7c9b | ||
|
|
9ecf34a8a4 |
905
Cargo.lock
generated
905
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -27,6 +27,8 @@ members = [
|
||||
"crates/harness/runner",
|
||||
"crates/harness/plot",
|
||||
"crates/tlsn",
|
||||
"crates/wasmtime-plugin",
|
||||
"crates/wasmtime-host",
|
||||
]
|
||||
resolver = "2"
|
||||
|
||||
|
||||
14
crates/wasmtime-host/Cargo.toml
Normal file
14
crates/wasmtime-host/Cargo.toml
Normal file
@@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "wasmtime-host"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
anyhow = { workspace = true }
|
||||
serde = { workspace = true, features = ["derive"] }
|
||||
serde_json = { workspace = true }
|
||||
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
|
||||
wasmtime = { git = "https://github.com/bytecodealliance/wasmtime", branch = "main", features = ["component-model-async"] }
|
||||
73
crates/wasmtime-host/src/main.rs
Normal file
73
crates/wasmtime-host/src/main.rs
Normal file
@@ -0,0 +1,73 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use wasmtime::{component::{bindgen, Component, HasSelf, Linker}, Config, Engine, Store};
|
||||
|
||||
const WASM_PLUGIN_PATH: &str = "../../target/wasm32-unknown-unknown/release/wasmtime_plugin_component.wasm";
|
||||
|
||||
bindgen!({
|
||||
path: "../wasmtime-plugin/wit/world.wit",
|
||||
imports: {
|
||||
"read": async,
|
||||
"write": async,
|
||||
},
|
||||
exports: {
|
||||
"main": async,
|
||||
}
|
||||
});
|
||||
|
||||
#[derive(Serialize, Debug)]
|
||||
struct Input {
|
||||
id: u8
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct Output {
|
||||
result: bool
|
||||
}
|
||||
|
||||
struct HostState {}
|
||||
|
||||
impl PluginImports for HostState {
|
||||
async fn read(&mut self, id: u8) -> Vec<u8> {
|
||||
println!("Id received from plugin: {id}");
|
||||
serde_json::to_vec(&format!("Hello from host {}", id)).unwrap()
|
||||
}
|
||||
|
||||
async fn write(&mut self, payload: Vec<u8>) -> Vec<u8> {
|
||||
let payload: String = serde_json::from_slice(&payload).unwrap();
|
||||
println!("Payload received from plugin: {payload}");
|
||||
let success = true;
|
||||
serde_json::to_vec(&success).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
println!("Starting wasmtime");
|
||||
|
||||
let mut config = Config::new();
|
||||
config
|
||||
.async_support(true)
|
||||
.wasm_component_model_async(true);
|
||||
|
||||
let engine = Engine::new(&config)?;
|
||||
|
||||
let component = Component::from_file(&engine, WASM_PLUGIN_PATH)?;
|
||||
let mut linker = Linker::new(&engine);
|
||||
|
||||
Plugin::add_to_linker::<_, HasSelf<_>>(&mut linker, |state| state)?;
|
||||
|
||||
let mut store = Store::new(&engine, HostState {});
|
||||
|
||||
let plugin = Plugin::instantiate_async(&mut store, &component, &linker).await?;
|
||||
|
||||
let input = Input { id: 0 };
|
||||
println!("Input: {:?}", input);
|
||||
|
||||
let input_bytes = serde_json::to_vec(&input)?;
|
||||
let res_byte = plugin.call_main(&mut store, &input_bytes).await?;
|
||||
let res: Output = serde_json::from_slice(&res_byte)?;
|
||||
|
||||
println!("Output: {:?}", res);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
5
crates/wasmtime-plugin/.cargo/config.toml
Normal file
5
crates/wasmtime-plugin/.cargo/config.toml
Normal file
@@ -0,0 +1,5 @@
|
||||
[build]
|
||||
target = "wasm32-unknown-unknown"
|
||||
|
||||
[unstable]
|
||||
build-std = ["panic_abort", "std"]
|
||||
15
crates/wasmtime-plugin/Cargo.toml
Normal file
15
crates/wasmtime-plugin/Cargo.toml
Normal file
@@ -0,0 +1,15 @@
|
||||
[package]
|
||||
name = "wasmtime-plugin"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
serde = { workspace = true, features = ["derive"] }
|
||||
serde_json = { workspace = true }
|
||||
wit-bindgen = { version = "0.43.0" }
|
||||
13
crates/wasmtime-plugin/build.sh
Executable file
13
crates/wasmtime-plugin/build.sh
Executable file
@@ -0,0 +1,13 @@
|
||||
#!/bin/bash
|
||||
|
||||
PACKAGE_NAME="wasmtime_plugin"
|
||||
|
||||
set -e
|
||||
|
||||
# Build the project
|
||||
cargo build --release
|
||||
|
||||
# Convert the wasm binary to a component
|
||||
wasm-tools component new ../../target/wasm32-unknown-unknown/release/${PACKAGE_NAME}.wasm -o ../../target/wasm32-unknown-unknown/release/${PACKAGE_NAME}_component.wasm
|
||||
|
||||
echo "Component created: ${PACKAGE_NAME}_component.wasm"
|
||||
31
crates/wasmtime-plugin/src/lib.rs
Normal file
31
crates/wasmtime-plugin/src/lib.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
wit_bindgen::generate!({
|
||||
path: "wit/world.wit",
|
||||
async: true
|
||||
});
|
||||
|
||||
struct Component;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Input {
|
||||
id: u8
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct Output {
|
||||
result: bool
|
||||
}
|
||||
|
||||
impl Guest for Component {
|
||||
async fn main(input: Vec<u8>) -> Vec<u8> {
|
||||
let input: Input = serde_json::from_slice(&input).unwrap();
|
||||
let payload_bytes = read(input.id).await;
|
||||
let result_bytes = write(payload_bytes).await;
|
||||
let result: bool = serde_json::from_slice(&result_bytes).unwrap();
|
||||
let output = Output { result };
|
||||
serde_json::to_vec(&output).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
export!(Component);
|
||||
7
crates/wasmtime-plugin/wit/world.wit
Normal file
7
crates/wasmtime-plugin/wit/world.wit
Normal file
@@ -0,0 +1,7 @@
|
||||
package component:wasmtime-plugin;
|
||||
|
||||
world plugin {
|
||||
export main: func(input: list<u8>) -> list<u8>;
|
||||
import read: func(id: u8) -> list<u8>;
|
||||
import write: func(payload: list<u8>) -> list<u8>;
|
||||
}
|
||||
Reference in New Issue
Block a user