use std::collections::{BTreeMap, BTreeSet};
use wasmtime::{
Caller, Instance, Linker, Memory, 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>,
lookup: (String, 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,
lookup: (String::new(), String::new()),
}
}
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], out: &mut Vec<u8>) -> Result<(), String> {
out.clear();
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 (lc, lid) = &mut self.lookup;
lc.clear();
lc.push_str(c);
lid.clear();
lid.push_str(id);
let value = match self.overlay.get(&self.lookup) {
Some(v) => v.as_deref(), None => self.snapshot.get(&self.lookup).map(Vec::as_slice),
};
match value {
Some(v) => {
out.push(1);
out.extend_from_slice(v);
}
None => out.push(0),
}
Ok(())
}
"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 (lc, lid) = &mut self.lookup;
lc.clear();
lc.push_str(c);
lid.clear();
let from = &self.lookup;
let mut live: BTreeMap<&str, &[u8]> = self
.snapshot
.range::<(String, String), _>(from..)
.take_while(|((col, _), _)| col == c)
.map(|((_, id), v)| (id.as_str(), v.as_slice()))
.collect();
for ((col, id), v) in self.overlay.range::<(String, String), _>(from..) {
if col != c {
break;
}
match v {
Some(bytes) => {
live.insert(id.as_str(), bytes.as_slice());
}
None => {
live.remove(id.as_str());
}
}
}
for (id, v) in live {
frame_into(out, id.as_bytes());
frame_into(out, v);
}
Ok(())
}
"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(())
}
"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(())
}
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,
memory: Option<Memory>,
alloc: Option<TypedFunc<i32, i32>>,
resp: Vec<u8>,
}
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 = self.module(&workload_hash, module_wasm)?;
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,
memory: None,
alloc: None,
resp: Vec::new(),
},
);
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 = match caller.data().memory {
Some(memory) => memory,
None => caller
.get_export("memory")
.and_then(|e| e.into_memory())
.ok_or_else(|| wasmtime::Error::msg("host_call: no guest memory"))?,
};
let resp = {
let (data, state) = memory.data_and_store_mut(&mut caller);
let data: &[u8] = data;
let slice = |ptr: i32, len: i32| {
let (ptr, len) = (ptr as usize, len as usize);
data.get(ptr..ptr + len).ok_or_else(|| {
wasmtime::Error::msg("host_call: slice out of bounds")
})
};
let op_bytes = slice(op_ptr, op_len)?;
let payload = slice(pay_ptr, pay_len)?;
let op = std::str::from_utf8(op_bytes)
.map_err(|_| wasmtime::Error::msg("host_call: op must be UTF-8"))?;
let mut resp = std::mem::take(&mut state.resp);
state
.ctx
.dispatch(op, payload, &mut resp)
.map_err(wasmtime::Error::msg)?;
resp
};
let alloc: TypedFunc<i32, i32> = match caller.data().alloc.clone() {
Some(alloc) => alloc,
None => 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)?;
let packed = (((ptr as u32) as i64) << 32) | (resp.len() as u32) as i64;
caller.data_mut().resp = resp; Ok(packed)
},
)
.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"))?;
store.data_mut().memory = Some(memory);
store.data_mut().alloc = Some(alloc.clone());
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 output = read_output(memory.data(&store), out_ptr, out_len)?;
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,
})
}
}
pub(crate) fn read_output(data: &[u8], out_ptr: usize, out_len: usize) -> QueryResult<Vec<u8>> {
out_ptr
.checked_add(out_len)
.and_then(|end| data.get(out_ptr..end))
.map(<[u8]>::to_vec)
.ok_or_else(|| {
QueryError::Abi(format!(
"output read at {out_ptr}+{out_len}: out of bounds memory access"
))
})
}
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()
}