spacedb-query
SpaceDB Layer 4 — compute-to-data.
The environment forbids hauling data to a central brain, so SpaceDB inverts it: the query travels to the data; only the answer travels back. A query is a deterministic, fuel- and memory-bounded WASM function that runs on the node holding the data — and because the runtime is deterministic, the result is corroboratable: a host that returns a different answer than its peers is caught.
Part of SpaceDB. Dual-licensed MIT OR Apache-2.0.
[]
= "0.7"
Run a function next to the data
use ;
let module_wasm: = Vecnew; // a real deterministic WASM module
let input_bytes: = Vecnew;
let runtime = new.unwrap;
let limits = RunLimits ;
let exec = runtime.run.unwrap;
println!;
The runtime is wasmtime with no WASI: no clock, no filesystem, no network,
no randomness. A function that cannot observe anything nondeterministic cannot
produce a nondeterministic answer — which is what makes the next section work.
Fuel exhaustion and the memory ceiling are hard stops, so an untrusted function
cannot pin a home's CPU or RAM.
Corroboration
Every run yields a FunctionRun attestation: workload_hash, input_digest,
output_digest, fuel_used, mem_peak_mb.
use ;
// Two independent hosts ran the same (workload, input) — so every field must match.
let run_a = FunctionRun ;
let run_b = run_a.clone;
assert_eq!;
Honest runs of the same (workload, input) agree on every field. A wrong
output, or a padded fuel count billed for work not done, is Disagree. Fanning
that comparison across independent marketplace hosts is the MATA seam; the
runtime, the attestation, and the comparison are here.
Pinned snapshots and rights
Snapshot::pin(bytes, frontier) freezes exactly what a query reads, and its
hash() goes into the attestation — so two hosts corroborating a result are
provably comparing the same input state, not two different moments in time.
FunctionCtx carries that snapshot plus CtxRights { read, write } and an
explicit denied set: collections whose ciphertext the host holds but
provably cannot compute on. Serving those to server-side code would break the
zero-knowledge promise, so the context refuses them. Accumulated writes are
exposed as a writes_digest rather than applied behind the caller's back.
Partition-aware map-reduce
use ;
let runtime = new.unwrap;
let plan = QueryPlan ;
let shards = vec!;
let outcome = run_query.unwrap;
if !outcome.coverage.is_complete
The planner maps over reachable shards and reduces the partials, and reports
Coverage honestly. A partial answer is labeled partial; it is never rounded up
to complete.
Open-core boundary
Built on wasmtime directly — the same engine MATA's maestro-fn-runtime
wraps — with no MATA dependency.
Performance (0.7.2)
Measured on exact instruction counts (callgrind) with deterministic drivers
whose outputs did not change; release build, 0.7.1 -> 0.7.2:
ir_query 33.4M -> 27.4M (-18.0%).
PinnedRecordsshares a snapshot and its hash across runs (FunctionCtx::with_pinned).- A query hashes each module once and reuses the snapshot's digest (
FunctionRuntime::run_prehashed). - The Database-Function linker is built once per runtime.
Performance (0.7.0)
Measured on exact instruction counts (callgrind) with this crate's
deterministic driver, examples/ir_query.rs; outputs unchanged by every change.
Release build, 0.6.0 -> 0.7.0: 280.9M -> 33.4M instructions (-88.1%).
- Compiled modules are cached by workload hash (a map-reduce used to compile its two modules once per shard and per reduce step).
- Host calls borrow op and payload from guest memory, resolve
memoryandalloconce per instance, and reuse one lookup key and one response buffer per run; thequeryop reads its collection as a key range. - The reduce fold moves partials and frames them in one reused buffer.
Testing
The workspace defaults to wasm32; this crate is native. Test on your host
triple:
Suites: runtime.rs (determinism, fuel/memory limits, module validation),
functions.rs (context, rights, denied collections), planner.rs (map-reduce,
coverage under partition).
License
MIT OR Apache-2.0, at your option. See LICENSE-MIT and LICENSE-APACHE.