use std::collections::{BTreeMap, BTreeSet};
use wasmtime::{Caller, Instance, Linker, Module, Store, StoreLimits, StoreLimitsBuilder, TypedFunc};
use crate::corroborate::FunctionRun;
use crate::error::{QueryError, QueryResult};
use crate::runtime::{Execution, FunctionRuntime, RunLimits};
pub type RecordSnapshot = BTreeMap<(String, String), Vec<u8>>;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct CtxRights {
pub read: bool,
pub write: bool,
}
#[derive(Debug)]
pub struct FunctionCtx {
snapshot: RecordSnapshot,
snapshot_hash: [u8; 32],
overlay: BTreeMap<(String, String), Option<Vec<u8>>>,
rights: CtxRights,
denied: BTreeSet<String>,
}
impl FunctionCtx {
pub fn new(snapshot: RecordSnapshot, rights: CtxRights, denied: BTreeSet<String>) -> Self {
let snapshot_hash = hash_records(&snapshot);
Self { snapshot, snapshot_hash, overlay: BTreeMap::new(), rights, denied }
}
pub fn snapshot_hash(&self) -> [u8; 32] {
self.snapshot_hash
}
pub fn writes(&self) -> &BTreeMap<(String, String), Option<Vec<u8>>> {
&self.overlay
}
pub fn writes_digest(&self) -> [u8; 32] {
let mut h = blake3::Hasher::new();
for ((c, id), v) in &self.overlay {
frame(&mut h, c.as_bytes());
frame(&mut h, id.as_bytes());
match v {
Some(bytes) => {
h.update(&[1]);
frame(&mut h, bytes);
}
None => {
h.update(&[0]);
}
}
}
*h.finalize().as_bytes()
}
pub fn write_bytes(&self) -> u64 {
self.overlay
.iter()
.map(|((c, id), v)| {
(c.len() + id.len() + v.as_ref().map(Vec::len).unwrap_or(0)) as u64
})
.sum()
}
fn dispatch(&mut self, op: &str, payload: &[u8]) -> Result<Vec<u8>, String> {
match op {
"get" => {
if !self.rights.read {
return Err("rights: get requires database:read".into());
}
let (c, id) = split2(payload).ok_or("get: payload must be collection\\0id")?;
self.check_open(c)?;
let key = (c.to_string(), id.to_string());
let value = match self.overlay.get(&key) {
Some(v) => v.as_deref(), None => self.snapshot.get(&key).map(Vec::as_slice),
};
Ok(match value {
Some(v) => {
let mut out = Vec::with_capacity(1 + v.len());
out.push(1);
out.extend_from_slice(v);
out
}
None => vec![0],
})
}
"query" => {
if !self.rights.read {
return Err("rights: query requires database:read".into());
}
let c = std::str::from_utf8(payload).map_err(|_| "query: collection must be UTF-8")?;
self.check_open(c)?;
let mut live: BTreeMap<&str, &[u8]> = self
.snapshot
.iter()
.filter(|((col, _), _)| col == c)
.map(|((_, id), v)| (id.as_str(), v.as_slice()))
.collect();
for ((col, id), v) in &self.overlay {
if col == c {
match v {
Some(bytes) => {
live.insert(id.as_str(), bytes.as_slice());
}
None => {
live.remove(id.as_str());
}
}
}
}
let mut out = Vec::new();
for (id, v) in live {
frame_into(&mut out, id.as_bytes());
frame_into(&mut out, v);
}
Ok(out)
}
"put" => {
if !self.rights.write {
return Err("rights: put requires database:write".into());
}
let (c, id, value) =
split3(payload).ok_or("put: payload must be collection\\0id\\0value")?;
self.check_open(c)?;
self.overlay.insert((c.to_string(), id.to_string()), Some(value.to_vec()));
Ok(Vec::new())
}
"delete" => {
if !self.rights.write {
return Err("rights: delete requires database:write".into());
}
let (c, id) = split2(payload).ok_or("delete: payload must be collection\\0id")?;
self.check_open(c)?;
self.overlay.insert((c.to_string(), id.to_string()), None);
Ok(Vec::new())
}
other => Err(format!("unknown host op `{other}`")),
}
}
fn check_open(&self, collection: &str) -> Result<(), String> {
if self.denied.contains(collection) {
return Err(format!(
"collection `{collection}` is zero-knowledge: the host cannot compute on it"
));
}
Ok(())
}
}
#[derive(Debug)]
pub struct FunctionOutcome {
pub execution: Execution,
pub ctx: FunctionCtx,
}
struct CtxHostState {
limits: StoreLimits,
ctx: FunctionCtx,
}
impl FunctionRuntime {
pub fn validate_function(&self, module_wasm: &[u8]) -> QueryResult<()> {
let module = Module::new(self.engine(), module_wasm)
.map_err(|e| QueryError::Compile(e.to_string()))?;
for import in module.imports() {
if !(import.module() == "spacedb" && import.name() == "host_call") {
return Err(QueryError::Abi(format!(
"forbidden import `{}::{}` — functions may import only spacedb::host_call",
import.module(),
import.name()
)));
}
}
for export in ["memory", "alloc", "run"] {
if module.get_export(export).is_none() {
return Err(QueryError::Abi(format!("missing required export `{export}`")));
}
}
Ok(())
}
pub fn run_with_ctx(
&self,
module_wasm: &[u8],
input: &[u8],
limits: &RunLimits,
ctx: FunctionCtx,
) -> QueryResult<FunctionOutcome> {
let workload_hash = hash(module_wasm);
let input_digest = {
let mut h = blake3::Hasher::new();
h.update(input);
h.update(&ctx.snapshot_hash());
*h.finalize().as_bytes()
};
let module = Module::new(self.engine(), module_wasm)
.map_err(|e| QueryError::Compile(e.to_string()))?;
let max_bytes = limits.max_mem_mb as usize * 1024 * 1024;
let mut store = Store::new(
self.engine(),
CtxHostState {
limits: StoreLimitsBuilder::new().memory_size(max_bytes).build(),
ctx,
},
);
store.limiter(|h| &mut h.limits);
store.set_fuel(limits.max_fuel).map_err(|e| QueryError::Fuel(e.to_string()))?;
let mut linker: Linker<CtxHostState> = Linker::new(self.engine());
linker
.func_wrap(
"spacedb",
"host_call",
|mut caller: Caller<'_, CtxHostState>,
op_ptr: i32,
op_len: i32,
pay_ptr: i32,
pay_len: i32|
-> wasmtime::Result<i64> {
let memory = caller
.get_export("memory")
.and_then(|e| e.into_memory())
.ok_or_else(|| wasmtime::Error::msg("host_call: no guest memory"))?;
let read = |caller: &Caller<'_, CtxHostState>, ptr: i32, len: i32| {
let (ptr, len) = (ptr as usize, len as usize);
memory
.data(caller)
.get(ptr..ptr + len)
.map(<[u8]>::to_vec)
.ok_or_else(|| wasmtime::Error::msg("host_call: slice out of bounds"))
};
let op_bytes = read(&caller, op_ptr, op_len)?;
let payload = read(&caller, pay_ptr, pay_len)?;
let op = std::str::from_utf8(&op_bytes)
.map_err(|_| wasmtime::Error::msg("host_call: op must be UTF-8"))?
.to_string();
let resp = caller
.data_mut()
.ctx
.dispatch(&op, &payload)
.map_err(wasmtime::Error::msg)?;
let alloc: TypedFunc<i32, i32> = caller
.get_export("alloc")
.and_then(|e| e.into_func())
.ok_or_else(|| wasmtime::Error::msg("host_call: no guest alloc"))?
.typed(&caller)
.map_err(|e| wasmtime::Error::msg(format!("host_call: alloc: {e}")))?;
let len = i32::try_from(resp.len())
.map_err(|_| wasmtime::Error::msg("host_call: response too large"))?;
let ptr = alloc.call(&mut caller, len)?;
memory.write(&mut caller, ptr as usize, &resp)?;
Ok((((ptr as u32) as i64) << 32) | (resp.len() as u32) as i64)
},
)
.map_err(|e| QueryError::Instantiate(e.to_string()))?;
let instance: Instance = linker
.instantiate(&mut store, &module)
.map_err(|e| QueryError::Instantiate(e.to_string()))?;
let memory = instance
.get_memory(&mut store, "memory")
.ok_or(QueryError::MissingExport("memory"))?;
let alloc: TypedFunc<i32, i32> = instance
.get_typed_func(&mut store, "alloc")
.map_err(|_| QueryError::MissingExport("alloc"))?;
let run: TypedFunc<(i32, i32), i64> = instance
.get_typed_func(&mut store, "run")
.map_err(|_| QueryError::MissingExport("run"))?;
let in_len =
i32::try_from(input.len()).map_err(|_| QueryError::Abi("input too large".into()))?;
let in_ptr = alloc.call(&mut store, in_len).map_err(|e| QueryError::Trap(e.to_string()))?;
memory
.write(&mut store, in_ptr as usize, input)
.map_err(|e| QueryError::Abi(format!("input write: {e}")))?;
let packed =
run.call(&mut store, (in_ptr, in_len)).map_err(|e| QueryError::Trap(e.to_string()))?;
let packed = packed as u64;
let (out_ptr, out_len) = ((packed >> 32) as usize, (packed & 0xFFFF_FFFF) as usize);
let mut output = vec![0u8; out_len];
memory
.read(&store, out_ptr, &mut output)
.map_err(|e| QueryError::Abi(format!("output read at {out_ptr}+{out_len}: {e}")))?;
let output_digest = hash(&output);
let remaining = store.get_fuel().map_err(|e| QueryError::Fuel(e.to_string()))?;
let fuel_used = limits.max_fuel.saturating_sub(remaining);
let mem_peak_mb = (memory.data_size(&store) / (1024 * 1024)) as u32;
Ok(FunctionOutcome {
execution: Execution {
output,
run: FunctionRun {
workload_hash,
input_digest,
output_digest,
fuel_used,
mem_peak_mb,
},
},
ctx: store.into_data().ctx,
})
}
}
fn split2(payload: &[u8]) -> Option<(&str, &str)> {
let nul = payload.iter().position(|b| *b == 0)?;
let c = std::str::from_utf8(&payload[..nul]).ok()?;
let id = std::str::from_utf8(&payload[nul + 1..]).ok()?;
(!id.as_bytes().contains(&0)).then_some((c, id))
}
fn split3(payload: &[u8]) -> Option<(&str, &str, &[u8])> {
let first = payload.iter().position(|b| *b == 0)?;
let rest = &payload[first + 1..];
let second = rest.iter().position(|b| *b == 0)?;
let c = std::str::from_utf8(&payload[..first]).ok()?;
let id = std::str::from_utf8(&rest[..second]).ok()?;
Some((c, id, &rest[second + 1..]))
}
fn frame(h: &mut blake3::Hasher, bytes: &[u8]) {
h.update(&(bytes.len() as u32).to_le_bytes());
h.update(bytes);
}
fn frame_into(out: &mut Vec<u8>, bytes: &[u8]) {
out.extend_from_slice(&(bytes.len() as u32).to_le_bytes());
out.extend_from_slice(bytes);
}
fn hash_records(records: &RecordSnapshot) -> [u8; 32] {
let mut h = blake3::Hasher::new();
for ((c, id), v) in records {
frame(&mut h, c.as_bytes());
frame(&mut h, id.as_bytes());
frame(&mut h, v);
}
*h.finalize().as_bytes()
}
fn hash(bytes: &[u8]) -> [u8; 32] {
*blake3::hash(bytes).as_bytes()
}