spacedb-meter 0.7.3

SpaceDB Layer 6 — metering & settlement: deterministic measurement of storage (bytes x time x replica_count), compute (fuel + invocations) and transit (bilaterally-corroborated served bytes), priced by a rate card and bounded by an agent budget. SpaceDB measures; the Settlement seam lets a host plug pricing + payout (e.g. MATA's maestro -> Iron Bank loop) underneath. Phase 1 SpaceDB Mission, M8.
Documentation

spacedb-meter

crates.io docs.rs License: MIT OR Apache-2.0 Remade With Rust By Mata Network

SpaceDB Layer 6 — metering & settlement.

SpaceDB measures; it does not price or pay. This crate computes the three resource amounts deterministically, accumulates them, and drains them into amounts-only claims. What a claim is worth, and who actually gets paid, sits behind a seam.

Part of SpaceDB. Dual-licensed MIT OR Apache-2.0.

[dependencies]

spacedb-meter = "0.7"

What gets measured

use spacedb_meter::Usage;
let (bytes, seconds, replica_count) = (1u64 << 30, 86_400u64, 3u32);
let (fuel, invocations) = (2_500_000u64, 40u64);
let (server_claimed, consumer_acked) = (1_000_000u64, 999_000u64);

Usage::storage(bytes, seconds, replica_count);  // byte-seconds × replicas
Usage::compute(fuel, invocations);              // deterministic fuel + per-call
Usage::transit(server_claimed, consumer_acked); // the MINIMUM of the two

Transit takes the minimum of what the server claims it served and what the consumer acknowledges receiving. Bilateral corroboration means neither side can inflate a bill unilaterally — over-claiming simply doesn't pay.

Ledger → claims

use spacedb_meter::{MeterLedger, Usage};
let (period_start, period_end) = (1_700_000_000u64, 1_700_086_400u64);

let mut ledger = MeterLedger::new();
ledger.record("did:mata:customer", Usage::storage(1 << 30, 86_400, 3));
ledger.record("did:mata:customer", Usage::compute(2_500_000, 40));

// Drain a billing period into per-customer, per-class claims.
let claims = ledger.drain_claims("did:mata:node", period_start, period_end);

A UsageClaim carries a deterministic claim_id (node:settles_to:class: period_end) — so a retried or duplicated claim dedups instead of double-billing — the amounts, the period bounds, and an optional ProofRef (StorageProbe / ComputeAttestation / TransitReceipt) content-addressing the artifact that backs it, e.g. a FunctionRun digest from spacedb-query.

Pricing and budgets

RateCard prices storage per GiB-month, compute per megafuel plus per invocation, and transit per GiB — multiplying before dividing so sub-unit usage is never silently rounded to zero. estimate(&usages) gives a pre-deploy cost before anything runs.

Budget is the agent spend cap: can_afford, charge (fails rather than overdrawing), credit, remaining. An AI agent with a capability gets a hard ceiling, not a monthly surprise.

The settlement seam

use spacedb_meter::{MeterError, Settled, UsageClaim};
pub trait Settlement {
    fn settle(&mut self, claim: &UsageClaim) -> Result<Settled, MeterError>;
}
  • LocalSettlement ships here: price against a rate card, tally per customer. Enough for a self-hoster; no tokens minted.
  • A host implements Settlement over its own money plane. For MATA that is the existing UsageClaim → Maestro counter-sign → EarningRecord → Iron Bank loop — so SpaceDB usage settles through it without ever depending on it.

Open-core boundary

Depends on serde / postcard / thiserror only — no spacedb-* and no mata-* crates.

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: exercised by the console driver (ir_console -33.1% overall).

  • UsageClaim ids are joined into one sized String instead of format!.

Performance (0.7.0)

Measured on exact instruction counts (callgrind) with this crate's deterministic driver, examples/ir_console.rs; outputs unchanged by every change. Exercised by the console driver (spacedb-console).

  • LocalSettlement copies a customer id into its tally only for a new customer.

Testing

The workspace defaults to wasm32; this crate is native. Test on your host triple:

cargo test -p spacedb-meter --target aarch64-apple-darwin   # or your host triple

Suite: meter.rs — measurement determinism, transit corroboration, claim dedup keys, rate-card rounding, budget exhaustion.

License

MIT OR Apache-2.0, at your option. See LICENSE-MIT and LICENSE-APACHE.