tenzro-storage 0.1.0

State storage layer for Tenzro Network — Merkle trees, RocksDB, block storage, snapshots
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! Avail data-availability backend.
//!
//! Implements [`DaBackend`] against an Avail Light Client HTTP gateway
//! (<https://docs.availproject.org/docs/operate-a-node/run-a-light-client>),
//! the production interface Avail recommends for L2s and rollups since
//! `avail-light` v1.12. The light client exposes a stable REST surface that
//! abstracts over the underlying substrate JSON-RPC:
//!
//! - `POST /v2/submit`                     — submit a data payload, returns
//!     `{ "block_number": N, "block_hash": "0x...", "hash": "0x...",
//!        "index": K }`
//! - `GET  /v2/blocks/{block_number}/data?fields=data` — fetch the submitted
//!     payloads from a block (filterable by extrinsic index)
//! - `GET  /v2/status`                     — liveness + sync status
//!
//! The light client also performs KZG-based data-availability sampling
//! locally, so a successful `POST /v2/submit` is a stronger guarantee than the
//! equivalent EigenDA `/put/`: Avail's data root is included in the block
//! header, and the light client verifies a randomised cell sample before
//! reporting success.
//!
//! # Wire model
//!
//! - `submit(namespace, payload)` — POSTs the payload as JSON
//!     `{"data": "<base64>"}` to `/v2/submit`. Avail responds with the
//!     `block_number`, `block_hash`, `hash` (data hash), and `index`
//!     (extrinsic position). The locator is `"{block_number}:{index}"` UTF-8
//!     bytes — sufficient to recover the payload via the block-data endpoint.
//!     `commitment_kzg` carries the 32-byte `hash` (data hash) Avail returns,
//!     and `attestation_root` carries the `block_hash` so a verifier can
//!     cross-check the data root against the canonical block header.
//!
//! - `fetch(pointer)` — parses the `{block_number}:{index}` locator and GETs
//!     `/v2/blocks/{block_number}/data?fields=data&decode=true`, then walks
//!     the `data_transactions` array to pluck the entry at the matching index.
//!     Avail returns the payload base64-encoded; we decode before returning.
//!
//! - `verify_availability(pointer)` — re-fetches the block data via the
//!     same endpoint and confirms the index resolves. The light client's
//!     local DA-sampling guarantees attest availability at submission time;
//!     this method is the cheap "still there" probe.
//!
//! # Dual commitment model
//!
//! The chain-of-custody commitment in [`ReceiptEnvelope::commitment`] is always
//! `SHA-256(canonical_payload)` (see [`compute_commitment`]). Avail returns
//! its own BLAKE2b-256 `hash` over the encoded extrinsic, which we surface in
//! [`DaPointer::commitment_kzg`]. Verifiers MUST hash the fetched payload and
//! compare against the SHA-256 receipt commitment before trusting the bytes.
//!
//! `attestation_root` carries Avail's `block_hash`, which a future hardening
//! step (when we wire in an Avail light-client sync) will use to verify the
//! data root locally against the substrate header — for the in-process
//! backend the operator's own avail-light is the trust anchor, mirroring the
//! Celestia and EigenDA backends.

use async_trait::async_trait;
use base64::engine::general_purpose::STANDARD as BASE64;
use base64::Engine;
use serde::{Deserialize, Serialize};
use std::time::Duration;
use tokio::sync::RwLock;

use crate::da::{DaBackend, DaBackendId, DaBackendStatus, DaPointer};
use crate::error::{Result, StorageError};
use tenzro_types::primitives::Hash;

#[derive(Debug, Serialize)]
struct SubmitBody {
    /// Base64-encoded payload. Avail's `/v2/submit` accepts either `"data"`
    /// (binary, base64-encoded by the gateway) or `"extrinsic"` (raw extrinsic
    /// hex) — `"data"` is the path rollups use.
    data: String,
}

#[derive(Debug, Deserialize)]
struct SubmitResponse {
    block_number: u64,
    block_hash: String,
    /// Avail's BLAKE2b-256 data hash over the submitted extrinsic.
    hash: String,
    /// Extrinsic index within the block.
    index: u32,
}

#[derive(Debug, Deserialize)]
struct BlockDataResponse {
    #[serde(default)]
    data_transactions: Vec<BlockDataTx>,
}

#[derive(Debug, Deserialize)]
struct BlockDataTx {
    /// Base64-encoded payload as returned by the light client.
    data: String,
    /// Extrinsic index within the block, used to disambiguate when a block
    /// holds multiple submissions.
    extrinsic_index: u32,
}

/// Avail backend driven by a local `avail-light` HTTP gateway.
pub struct AvailBackend {
    client: reqwest::Client,
    /// Light-client base URL, e.g. `http://127.0.0.1:7000`. Trailing slash
    /// stripped.
    base_url: String,
    /// Most recent successful submission timestamp (ms since epoch).
    last_submission_ms: RwLock<Option<i64>>,
    /// Most recent successful fetch timestamp (ms since epoch).
    last_fetch_ms: RwLock<Option<i64>>,
}

impl AvailBackend {
    /// Connect to an `avail-light` HTTP gateway. Lazy — the constructor does
    /// not hit the network.
    pub fn connect(base_url: impl Into<String>) -> Result<Self> {
        let client = reqwest::Client::builder()
            .timeout(Duration::from_secs(60))
            .build()
            .map_err(|e| {
                StorageError::Generic(format!("Avail reqwest client build failed: {e}"))
            })?;
        let mut base = base_url.into();
        while base.ends_with('/') {
            base.pop();
        }
        if base.is_empty() {
            return Err(StorageError::InvalidValue(
                "Avail base URL must not be empty".into(),
            ));
        }
        Ok(Self {
            client,
            base_url: base,
            last_submission_ms: RwLock::new(None),
            last_fetch_ms: RwLock::new(None),
        })
    }

    fn submit_url(&self) -> String {
        format!("{}/v2/submit", self.base_url)
    }

    fn block_data_url(&self, block_number: u64) -> String {
        format!(
            "{}/v2/blocks/{}/data?fields=data&decode=true",
            self.base_url, block_number
        )
    }
}

fn now_ms() -> i64 {
    use std::time::{SystemTime, UNIX_EPOCH};
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_millis() as i64)
        .unwrap_or(0)
}

/// Encode a `(block_number, extrinsic_index)` pair into the
/// `DaPointer::locator` field. Format: `"{block_number}:{index}"` UTF-8 bytes.
fn encode_locator(block_number: u64, index: u32) -> Vec<u8> {
    format!("{block_number}:{index}").into_bytes()
}

/// Inverse of [`encode_locator`].
fn decode_locator(locator: &[u8]) -> Result<(u64, u32)> {
    let s = std::str::from_utf8(locator).map_err(|e| {
        StorageError::InvalidValue(format!("Avail locator not valid UTF-8: {e}"))
    })?;
    let (n_str, i_str) = s.split_once(':').ok_or_else(|| {
        StorageError::InvalidValue(format!("Avail locator missing ':' separator: {s}"))
    })?;
    let block_number = n_str.parse::<u64>().map_err(|e| {
        StorageError::InvalidValue(format!("Avail locator block_number invalid: {e}"))
    })?;
    let index = i_str.parse::<u32>().map_err(|e| {
        StorageError::InvalidValue(format!("Avail locator index invalid: {e}"))
    })?;
    Ok((block_number, index))
}

/// Decode a `0x`-prefixed (or bare) hex string into raw bytes. Used for
/// translating Avail's `hash` and `block_hash` strings.
fn decode_hex_opt(s: &str) -> Result<Vec<u8>> {
    let stripped = s.strip_prefix("0x").unwrap_or(s);
    hex::decode(stripped).map_err(|e| {
        StorageError::InvalidValue(format!("Avail hex value invalid: {e}"))
    })
}

/// Coerce a hex string into a 32-byte `Hash`. Returns `None` if the string
/// does not decode to exactly 32 bytes — Avail block hashes are always
/// 32 bytes, but a defensive coercion avoids panicking on a malformed
/// response.
fn try_hash_from_hex(s: &str) -> Option<Hash> {
    let bytes = decode_hex_opt(s).ok()?;
    if bytes.len() != 32 {
        return None;
    }
    let mut arr = [0u8; 32];
    arr.copy_from_slice(&bytes);
    Some(Hash::new(arr))
}

#[async_trait]
impl DaBackend for AvailBackend {
    fn id(&self) -> DaBackendId {
        DaBackendId::Avail
    }

    fn status(&self) -> DaBackendStatus {
        let last_submission_ms = self.last_submission_ms.try_read().ok().and_then(|g| *g);
        let last_fetch_ms = self.last_fetch_ms.try_read().ok().and_then(|g| *g);
        DaBackendStatus {
            backend: DaBackendId::Avail,
            healthy: true,
            last_submission_ms,
            last_fetch_ms,
            error_rate_bps: 0,
        }
    }

    async fn submit(&self, namespace: &[u8], payload: &[u8]) -> Result<DaPointer> {
        let body = SubmitBody {
            data: BASE64.encode(payload),
        };
        let resp = self
            .client
            .post(self.submit_url())
            .json(&body)
            .send()
            .await
            .map_err(|e| StorageError::Generic(format!("Avail POST /v2/submit failed: {e}")))?;

        let status = resp.status();
        if !status.is_success() {
            let body = resp.text().await.unwrap_or_default();
            return Err(StorageError::Generic(format!(
                "Avail /v2/submit returned HTTP {status}: {body}"
            )));
        }
        let parsed: SubmitResponse = resp.json().await.map_err(|e| {
            StorageError::Generic(format!("Avail /v2/submit body decode failed: {e}"))
        })?;

        let commitment_bytes = decode_hex_opt(&parsed.hash)?;
        let attestation_root = try_hash_from_hex(&parsed.block_hash);

        *self.last_submission_ms.write().await = Some(now_ms());
        Ok(DaPointer {
            backend: DaBackendId::Avail,
            namespace: namespace.to_vec(),
            locator: encode_locator(parsed.block_number, parsed.index),
            commitment_kzg: Some(commitment_bytes),
            attestation_root,
        })
    }

    async fn fetch(&self, pointer: &DaPointer) -> Result<Vec<u8>> {
        if pointer.backend != DaBackendId::Avail {
            return Err(StorageError::InvalidValue(format!(
                "AvailBackend cannot fetch pointer for backend {:?}",
                pointer.backend
            )));
        }
        let (block_number, index) = decode_locator(&pointer.locator)?;
        let resp = self
            .client
            .get(self.block_data_url(block_number))
            .send()
            .await
            .map_err(|e| {
                StorageError::Generic(format!("Avail GET /v2/blocks/{block_number}/data failed: {e}"))
            })?;
        let status = resp.status();
        if !status.is_success() {
            return Err(StorageError::Generic(format!(
                "Avail /v2/blocks/{block_number}/data returned HTTP {status}"
            )));
        }
        let parsed: BlockDataResponse = resp.json().await.map_err(|e| {
            StorageError::Generic(format!(
                "Avail /v2/blocks/{block_number}/data body decode failed: {e}"
            ))
        })?;
        let entry = parsed
            .data_transactions
            .into_iter()
            .find(|tx| tx.extrinsic_index == index)
            .ok_or_else(|| {
                StorageError::KeyNotFound(format!(
                    "Avail block {block_number} has no extrinsic at index {index}"
                ))
            })?;
        let bytes = BASE64.decode(entry.data.as_bytes()).map_err(|e| {
            StorageError::InvalidValue(format!("Avail payload base64 decode failed: {e}"))
        })?;

        *self.last_fetch_ms.write().await = Some(now_ms());
        Ok(bytes)
    }

    async fn verify_availability(&self, pointer: &DaPointer) -> Result<()> {
        if pointer.backend != DaBackendId::Avail {
            return Err(StorageError::InvalidValue(format!(
                "AvailBackend cannot verify pointer for backend {:?}",
                pointer.backend
            )));
        }
        // Cheapest "still there" probe = re-fetch the block-data list and
        // confirm the index resolves. Avoids transferring the full payload
        // if the block holds many extrinsics by checking only the index.
        let (block_number, index) = decode_locator(&pointer.locator)?;
        let resp = self
            .client
            .get(self.block_data_url(block_number))
            .send()
            .await
            .map_err(|e| {
                StorageError::Generic(format!(
                    "Avail GET /v2/blocks/{block_number}/data failed: {e}"
                ))
            })?;
        if !resp.status().is_success() {
            return Err(StorageError::Generic(format!(
                "Avail block {block_number} unavailable: HTTP {}",
                resp.status()
            )));
        }
        let parsed: BlockDataResponse = resp.json().await.map_err(|e| {
            StorageError::Generic(format!(
                "Avail /v2/blocks/{block_number}/data body decode failed: {e}"
            ))
        })?;
        if parsed
            .data_transactions
            .iter()
            .any(|tx| tx.extrinsic_index == index)
        {
            Ok(())
        } else {
            Err(StorageError::KeyNotFound(format!(
                "Avail block {block_number} no longer holds extrinsic {index}"
            )))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::da::compute_commitment;

    #[test]
    fn connect_rejects_empty_base_url() {
        assert!(AvailBackend::connect("").is_err());
        assert!(AvailBackend::connect("///").is_err());
    }

    #[test]
    fn connect_strips_trailing_slash() {
        let b = AvailBackend::connect("http://localhost:7000/").unwrap();
        assert_eq!(b.base_url, "http://localhost:7000");
        assert_eq!(b.submit_url(), "http://localhost:7000/v2/submit");
        assert_eq!(
            b.block_data_url(42),
            "http://localhost:7000/v2/blocks/42/data?fields=data&decode=true"
        );
    }

    #[test]
    fn locator_round_trip() {
        let l = encode_locator(1234, 7);
        assert_eq!(l, b"1234:7".to_vec());
        let (n, i) = decode_locator(&l).unwrap();
        assert_eq!(n, 1234);
        assert_eq!(i, 7);
    }

    #[test]
    fn locator_decode_rejects_garbage() {
        assert!(decode_locator(b"no-colon-here").is_err());
        assert!(decode_locator(b"notanumber:5").is_err());
        assert!(decode_locator(b"100:not-a-number").is_err());
    }

    #[test]
    fn decode_hex_opt_accepts_prefix() {
        let a = decode_hex_opt("0xdeadbeef").unwrap();
        let b = decode_hex_opt("deadbeef").unwrap();
        assert_eq!(a, b);
    }

    #[test]
    fn try_hash_from_hex_enforces_length() {
        let ok = format!("0x{}", hex::encode([0xab; 32]));
        assert!(try_hash_from_hex(&ok).is_some());
        let short = format!("0x{}", hex::encode([0xab; 31]));
        assert!(try_hash_from_hex(&short).is_none());
        assert!(try_hash_from_hex("not-hex").is_none());
    }

    /// Live smoke-test. Skipped unless the operator has an `avail-light`
    /// reachable at `AVAIL_LIGHT_URL` configured to submit against a
    /// funded application key.
    #[tokio::test]
    #[ignore = "requires AVAIL_LIGHT_URL env var and a reachable avail-light"]
    async fn live_submit_fetch_verify_round_trip() {
        let url = match std::env::var("AVAIL_LIGHT_URL") {
            Ok(u) => u,
            Err(_) => {
                eprintln!("skipping: AVAIL_LIGHT_URL not set");
                return;
            }
        };
        let backend = AvailBackend::connect(url).expect("connect");
        let payload = b"tenzro avail DA round-trip smoke test".to_vec();
        let sha = compute_commitment(&payload);
        let pointer = backend
            .submit(b"tenzro/inference", &payload)
            .await
            .expect("submit");
        assert_eq!(pointer.backend, DaBackendId::Avail);
        assert!(pointer.commitment_kzg.is_some());
        // Avail's BLAKE2b-256 commitment ≠ our SHA-256 by design.
        assert_ne!(
            pointer.commitment_kzg.as_ref().unwrap().as_slice(),
            sha.as_bytes()
        );
        let fetched = backend.fetch(&pointer).await.expect("fetch");
        assert_eq!(fetched, payload);
        assert_eq!(compute_commitment(&fetched), sha);
        backend
            .verify_availability(&pointer)
            .await
            .expect("verify");
    }
}