chio-store-sqlite 0.1.2

SQLite-backed persistence, query, and report implementations for Chio
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
440
441
442
443
444
//! SQLite-backed `MemoryProvenanceStore`.
//!
//! Durable append-only hash-chain of memory-write provenance entries.
//! Keeps the same contract as `InMemoryMemoryProvenanceStore` in
//! `chio-kernel::memory_provenance`: `append` computes the chain linkage
//! atomically, `verify_entry` checks both the stored `hash` and the
//! `prev_hash` linkage, and `chain_digest` returns the tail hash.
//!
//! Schema:
//!
//! ```sql
//! CREATE TABLE chio_memory_provenance (
//!     seq           INTEGER PRIMARY KEY AUTOINCREMENT,
//!     entry_id      TEXT NOT NULL UNIQUE,
//!     store         TEXT NOT NULL,
//!     entry_key     TEXT NOT NULL,
//!     capability_id TEXT NOT NULL,
//!     receipt_id    TEXT NOT NULL,
//!     written_at    INTEGER NOT NULL,
//!     prev_hash     TEXT NOT NULL,
//!     hash          TEXT NOT NULL
//! );
//! CREATE INDEX idx_chio_memory_provenance_key
//!     ON chio_memory_provenance(store, entry_key, seq);
//! ```
//!
//! The monotonic `seq` column is the chain position; `verify_entry`
//! looks up the preceding row by `seq` to confirm linkage.

use std::fs;
use std::path::Path;

use chio_kernel::{
    recompute_memory_provenance_entry_hash, MemoryProvenanceAppend, MemoryProvenanceEntry,
    MemoryProvenanceError, MemoryProvenanceStore, ProvenanceVerification, UnverifiedReason,
    MEMORY_PROVENANCE_GENESIS_PREV_HASH,
};
use r2d2::Pool;
use r2d2_sqlite::SqliteConnectionManager;
use rusqlite::{params, OptionalExtension};
use uuid::Uuid;

/// Opaque error type for the SQLite-backed memory provenance store.
#[derive(Debug)]
pub struct SqliteMemoryProvenanceStoreError(String);

impl std::fmt::Display for SqliteMemoryProvenanceStoreError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "sqlite memory provenance store error: {}", self.0)
    }
}

impl std::error::Error for SqliteMemoryProvenanceStoreError {}

impl From<rusqlite::Error> for SqliteMemoryProvenanceStoreError {
    fn from(error: rusqlite::Error) -> Self {
        Self(error.to_string())
    }
}

impl From<std::io::Error> for SqliteMemoryProvenanceStoreError {
    fn from(error: std::io::Error) -> Self {
        Self(error.to_string())
    }
}

impl From<r2d2::Error> for SqliteMemoryProvenanceStoreError {
    fn from(error: r2d2::Error) -> Self {
        Self(error.to_string())
    }
}

impl From<SqliteMemoryProvenanceStoreError> for MemoryProvenanceError {
    fn from(error: SqliteMemoryProvenanceStoreError) -> Self {
        MemoryProvenanceError::Backend(error.0)
    }
}

/// SQLite-backed durable memory-provenance chain.
pub struct SqliteMemoryProvenanceStore {
    pool: Pool<SqliteConnectionManager>,
}

/// Memory-provenance-store schema revision. Bump on every schema-affecting change.
const MEMORY_PROVENANCE_STORE_SUPPORTED_SCHEMA_VERSION: i32 = 0;
/// Stable key under which this store records its schema revision in the shared
/// keyed metadata table, distinct from any co-located store's key.
const MEMORY_PROVENANCE_STORE_SCHEMA_KEY: &str = "memory_provenance";
/// Tables shipped before schema stamping existed, used to adopt a pre-stamping
/// memory-provenance database rather than reject it as foreign.
const MEMORY_PROVENANCE_STORE_LEGACY_ANCHOR_TABLES: &[&str] = &["chio_memory_provenance"];

impl SqliteMemoryProvenanceStore {
    /// Open the store at the given path, creating the parent directory
    /// if needed.
    pub fn open(path: impl AsRef<Path>) -> Result<Self, SqliteMemoryProvenanceStoreError> {
        let path = path.as_ref();
        // Resolve any `file:` URI to its on-disk parent before creating it, so a
        // URI-configured store creates the real backing directory rather than a
        // bogus scheme-prefixed one.
        if let Some(parent) = crate::sqlite_parent_dir_to_create(path) {
            fs::create_dir_all(&parent)?;
        }
        let manager = SqliteConnectionManager::file(path);
        let pool = Pool::builder().max_size(8).build(manager)?;
        let store = Self { pool };
        store.run_migrations()?;
        Ok(store)
    }

    /// Open an in-memory store for tests.
    pub fn open_in_memory() -> Result<Self, SqliteMemoryProvenanceStoreError> {
        let manager = SqliteConnectionManager::memory();
        let pool = Pool::builder().max_size(1).build(manager)?;
        let store = Self { pool };
        store.run_migrations()?;
        Ok(store)
    }

    fn run_migrations(&self) -> Result<(), SqliteMemoryProvenanceStoreError> {
        let conn = self
            .pool
            .get()
            .map_err(|error| SqliteMemoryProvenanceStoreError(format!("pool acquire: {error}")))?;
        crate::check_schema_version(
            &conn,
            MEMORY_PROVENANCE_STORE_SCHEMA_KEY,
            MEMORY_PROVENANCE_STORE_SUPPORTED_SCHEMA_VERSION,
            MEMORY_PROVENANCE_STORE_LEGACY_ANCHOR_TABLES,
        )
        .map_err(|error| SqliteMemoryProvenanceStoreError(error.to_string()))?;
        conn.execute_batch(
            r#"
            PRAGMA journal_mode = WAL;
            PRAGMA synchronous = FULL;
            PRAGMA busy_timeout = 5000;

            CREATE TABLE IF NOT EXISTS chio_memory_provenance (
                seq           INTEGER PRIMARY KEY AUTOINCREMENT,
                entry_id      TEXT NOT NULL UNIQUE,
                store         TEXT NOT NULL,
                entry_key     TEXT NOT NULL,
                capability_id TEXT NOT NULL,
                receipt_id    TEXT NOT NULL,
                written_at    INTEGER NOT NULL,
                prev_hash     TEXT NOT NULL,
                hash          TEXT NOT NULL
            );

            CREATE INDEX IF NOT EXISTS idx_chio_memory_provenance_key
                ON chio_memory_provenance(store, entry_key, seq);
            "#,
        )?;
        crate::stamp_schema_version(
            &conn,
            MEMORY_PROVENANCE_STORE_SCHEMA_KEY,
            MEMORY_PROVENANCE_STORE_SUPPORTED_SCHEMA_VERSION,
        )
        .map_err(|error| SqliteMemoryProvenanceStoreError(error.to_string()))?;
        Ok(())
    }

    /// Test helper: overwrite an existing entry's `hash` column to
    /// simulate tamper. Returns `false` when the row was not found.
    pub fn tamper_entry_hash(
        &self,
        entry_id: &str,
        forged_hash: &str,
    ) -> Result<bool, SqliteMemoryProvenanceStoreError> {
        let conn = self
            .pool
            .get()
            .map_err(|error| SqliteMemoryProvenanceStoreError(format!("pool acquire: {error}")))?;
        let updated = conn.execute(
            "UPDATE chio_memory_provenance SET hash = ?1 WHERE entry_id = ?2",
            params![forged_hash, entry_id],
        )?;
        Ok(updated > 0)
    }
}

fn map_row(row: &rusqlite::Row<'_>) -> rusqlite::Result<MemoryProvenanceEntry> {
    Ok(MemoryProvenanceEntry {
        entry_id: row.get("entry_id")?,
        store: row.get("store")?,
        key: row.get("entry_key")?,
        capability_id: row.get("capability_id")?,
        receipt_id: row.get("receipt_id")?,
        written_at: row.get::<_, i64>("written_at")? as u64,
        prev_hash: row.get("prev_hash")?,
        hash: row.get("hash")?,
    })
}

impl MemoryProvenanceStore for SqliteMemoryProvenanceStore {
    fn append(
        &self,
        input: MemoryProvenanceAppend,
    ) -> Result<MemoryProvenanceEntry, MemoryProvenanceError> {
        let mut conn = self
            .pool
            .get()
            .map_err(|error| MemoryProvenanceError::Backend(format!("pool acquire: {error}")))?;
        // `IMMEDIATE` guarantees a write lock is taken before any
        // subsequent read inside the transaction -- two concurrent
        // appenders cannot both observe the same tail and fork the
        // chain.
        let tx = conn
            .transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)
            .map_err(|error| MemoryProvenanceError::Backend(error.to_string()))?;

        let existing = tx
            .query_row(
                r#"
                SELECT entry_id, store, entry_key, capability_id, receipt_id,
                       written_at, prev_hash, hash
                FROM chio_memory_provenance
                WHERE receipt_id = ?1
                LIMIT 1
                "#,
                params![input.receipt_id.as_str()],
                map_row,
            )
            .optional()
            .map_err(|error| MemoryProvenanceError::Backend(error.to_string()))?;
        if let Some(existing) = existing {
            if existing.store != input.store
                || existing.key != input.key
                || existing.capability_id != input.capability_id
                || existing.written_at != input.written_at
            {
                return Err(MemoryProvenanceError::Backend(
                    "memory provenance receipt id was reused with different fields".to_string(),
                ));
            }
            tx.commit()
                .map_err(|error| MemoryProvenanceError::Backend(error.to_string()))?;
            return Ok(existing);
        }

        let prev_hash: String = tx
            .query_row(
                "SELECT hash FROM chio_memory_provenance ORDER BY seq DESC LIMIT 1",
                [],
                |row| row.get(0),
            )
            .optional()
            .map_err(|error| MemoryProvenanceError::Backend(error.to_string()))?
            .unwrap_or_else(|| MEMORY_PROVENANCE_GENESIS_PREV_HASH.to_string());

        let entry_id = format!("mem-prov-{}", Uuid::now_v7());
        let hash = recompute_memory_provenance_entry_hash(
            &entry_id,
            &input.store,
            &input.key,
            &input.capability_id,
            &input.receipt_id,
            input.written_at,
            &prev_hash,
        )?;

        let written_at_i64 = i64::try_from(input.written_at).unwrap_or(i64::MAX);
        tx.execute(
            r#"
            INSERT INTO chio_memory_provenance
                (entry_id, store, entry_key, capability_id, receipt_id, written_at, prev_hash, hash)
            VALUES
                (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)
            "#,
            params![
                entry_id,
                input.store,
                input.key,
                input.capability_id,
                input.receipt_id,
                written_at_i64,
                prev_hash,
                hash,
            ],
        )
        .map_err(|error| MemoryProvenanceError::Backend(error.to_string()))?;

        tx.commit()
            .map_err(|error| MemoryProvenanceError::Backend(error.to_string()))?;

        Ok(MemoryProvenanceEntry {
            entry_id,
            store: input.store,
            key: input.key,
            capability_id: input.capability_id,
            receipt_id: input.receipt_id,
            written_at: input.written_at,
            prev_hash,
            hash,
        })
    }

    fn get_entry(
        &self,
        entry_id: &str,
    ) -> Result<Option<MemoryProvenanceEntry>, MemoryProvenanceError> {
        let conn = self
            .pool
            .get()
            .map_err(|error| MemoryProvenanceError::Backend(format!("pool acquire: {error}")))?;
        let row = conn
            .query_row(
                r#"
                SELECT entry_id, store, entry_key, capability_id, receipt_id,
                       written_at, prev_hash, hash
                FROM chio_memory_provenance
                WHERE entry_id = ?1
                "#,
                params![entry_id],
                map_row,
            )
            .optional()
            .map_err(|error| MemoryProvenanceError::Backend(error.to_string()))?;
        Ok(row)
    }

    fn latest_for_key(
        &self,
        store: &str,
        key: &str,
    ) -> Result<Option<MemoryProvenanceEntry>, MemoryProvenanceError> {
        let conn = self
            .pool
            .get()
            .map_err(|error| MemoryProvenanceError::Backend(format!("pool acquire: {error}")))?;
        let row = conn
            .query_row(
                r#"
                SELECT entry_id, store, entry_key, capability_id, receipt_id,
                       written_at, prev_hash, hash
                FROM chio_memory_provenance
                WHERE store = ?1 AND entry_key = ?2
                ORDER BY seq DESC
                LIMIT 1
                "#,
                params![store, key],
                map_row,
            )
            .optional()
            .map_err(|error| MemoryProvenanceError::Backend(error.to_string()))?;
        Ok(row)
    }

    fn verify_entry(
        &self,
        entry_id: &str,
    ) -> Result<ProvenanceVerification, MemoryProvenanceError> {
        let conn = self
            .pool
            .get()
            .map_err(|error| MemoryProvenanceError::Backend(format!("pool acquire: {error}")))?;

        // Fetch the candidate row plus its seq and the prev row's hash
        // in the same query so verification is a single round-trip.
        let row = conn
            .query_row(
                r#"
                SELECT seq, entry_id, store, entry_key, capability_id, receipt_id,
                       written_at, prev_hash, hash
                FROM chio_memory_provenance
                WHERE entry_id = ?1
                "#,
                params![entry_id],
                |row| {
                    let seq: i64 = row.get("seq")?;
                    let entry = map_row(row)?;
                    Ok((seq, entry))
                },
            )
            .optional()
            .map_err(|error| MemoryProvenanceError::Backend(error.to_string()))?;

        let Some((seq, entry)) = row else {
            return Ok(ProvenanceVerification::Unverified {
                reason: UnverifiedReason::NoProvenance,
            });
        };

        let expected = entry.expected_hash()?;
        if expected != entry.hash {
            return Ok(ProvenanceVerification::Unverified {
                reason: UnverifiedReason::ChainTampered,
            });
        }

        let expected_prev: String = conn
            .query_row(
                r#"
                SELECT hash
                FROM chio_memory_provenance
                WHERE seq < ?1
                ORDER BY seq DESC
                LIMIT 1
                "#,
                params![seq],
                |row| row.get(0),
            )
            .optional()
            .map_err(|error| MemoryProvenanceError::Backend(error.to_string()))?
            .unwrap_or_else(|| MEMORY_PROVENANCE_GENESIS_PREV_HASH.to_string());

        if expected_prev != entry.prev_hash {
            return Ok(ProvenanceVerification::Unverified {
                reason: UnverifiedReason::ChainLinkBroken,
            });
        }

        let chain_digest: String = conn
            .query_row(
                "SELECT hash FROM chio_memory_provenance ORDER BY seq DESC LIMIT 1",
                [],
                |row| row.get(0),
            )
            .optional()
            .map_err(|error| MemoryProvenanceError::Backend(error.to_string()))?
            .unwrap_or_else(|| MEMORY_PROVENANCE_GENESIS_PREV_HASH.to_string());

        Ok(ProvenanceVerification::Verified {
            entry,
            chain_digest,
        })
    }

    fn chain_digest(&self) -> Result<String, MemoryProvenanceError> {
        let conn = self
            .pool
            .get()
            .map_err(|error| MemoryProvenanceError::Backend(format!("pool acquire: {error}")))?;
        let digest: Option<String> = conn
            .query_row(
                "SELECT hash FROM chio_memory_provenance ORDER BY seq DESC LIMIT 1",
                [],
                |row| row.get(0),
            )
            .optional()
            .map_err(|error| MemoryProvenanceError::Backend(error.to_string()))?;
        Ok(digest.unwrap_or_else(|| MEMORY_PROVENANCE_GENESIS_PREV_HASH.to_string()))
    }
}