use super::ShelfError;
use rusqlite::{params, Connection, OptionalExtension, Transaction};
pub(crate) const SCHEMA: &str = "
CREATE TABLE IF NOT EXISTS book_records (
book_id TEXT NOT NULL,
seq INTEGER NOT NULL,
record_id TEXT NOT NULL,
header BLOB NOT NULL,
capsule BLOB NOT NULL,
envelope BLOB NOT NULL,
PRIMARY KEY (book_id, record_id)
);
CREATE INDEX IF NOT EXISTS book_records_seq ON book_records (book_id, seq);
CREATE TABLE IF NOT EXISTS cadence_logs (log_id TEXT PRIMARY KEY);
CREATE TABLE IF NOT EXISTS cadence_live (log_id TEXT PRIMARY KEY);
CREATE TABLE IF NOT EXISTS cadence_witnessed (
log_id TEXT NOT NULL,
checkpoint_sha256 TEXT NOT NULL,
mmr_size INTEGER NOT NULL,
tick INTEGER NOT NULL,
PRIMARY KEY (log_id, checkpoint_sha256)
);
CREATE INDEX IF NOT EXISTS cadence_witnessed_size ON cadence_witnessed (log_id, mmr_size);
CREATE TABLE IF NOT EXISTS cadence_ticks (
tick INTEGER PRIMARY KEY,
at TEXT NOT NULL,
due_next TEXT NOT NULL,
entry_seq INTEGER NOT NULL,
checkpoint_size INTEGER NOT NULL,
checkpoint BLOB NOT NULL,
leaves TEXT NOT NULL,
witnessed INTEGER NOT NULL DEFAULT 0,
receipt TEXT
);
CREATE TABLE IF NOT EXISTS book_checkpoints (
book_id TEXT NOT NULL,
n INTEGER NOT NULL,
checkpoint_id TEXT NOT NULL,
checkpoint BLOB NOT NULL,
PRIMARY KEY (book_id, n),
UNIQUE (book_id, checkpoint_id)
);
CREATE TABLE IF NOT EXISTS shelf_meta (id INTEGER PRIMARY KEY CHECK (id = 1), version INTEGER NOT NULL);
CREATE TABLE IF NOT EXISTS shelf_logs (log_id TEXT PRIMARY KEY, kind TEXT NOT NULL CHECK (kind IN ('book', 'host', 'journal')));
CREATE TABLE IF NOT EXISTS shelf_entries (
entry_seq INTEGER PRIMARY KEY,
kind TEXT NOT NULL,
book_id TEXT NOT NULL,
log_id TEXT NOT NULL,
doc BLOB NOT NULL,
link_opening BLOB,
status_opening BLOB,
tick INTEGER NOT NULL
);
CREATE INDEX IF NOT EXISTS shelf_entries_log ON shelf_entries (log_id, entry_seq);
-- Every witness receipt that verified for a shelf checkpoint, one per
-- witness (its receipt's URL) and directory key id it verified under, with
-- the registration time it gives in Unix seconds or null
-- (record_time.rs). A shelf written before evidencebook#492 keyed it by URL
-- alone; migrate_shelf_receipts rebuilds it.
CREATE TABLE IF NOT EXISTS shelf_receipts (
checkpoint_sha256 TEXT NOT NULL,
witness_id TEXT NOT NULL,
key_id TEXT NOT NULL,
receipt TEXT NOT NULL,
registered_at INTEGER,
PRIMARY KEY (checkpoint_sha256, witness_id, key_id)
);
CREATE TABLE IF NOT EXISTS shelf_migration (id INTEGER PRIMARY KEY CHECK (id = 1), state TEXT NOT NULL, backup_sha256 TEXT NOT NULL);
-- The store of what the other side pushes (evidencebook#461 §3,
-- shelf_receive.go): its records, by digest, with the predecessor each names
-- and whether it is an unanswered unsolicited offer; every stamp proof
-- accepted, and whether the records held recomputed its root (0 not yet, 1
-- recomputed, 2 contradicted); the tombstone a deleted record leaves; the
-- evidence of a fork, a contradicting checkpoint or a checkpoint the records
-- do not recompute; each conflict found, once per position (a fork) or
-- size (a checkpoint), which no deletion undoes; and the position and digest
-- of every deleted record, so another record arriving there is a fork. A
-- row with offer = 1 (a dropped offer's position, a conflict in a log of
-- offers alone) counts under the offer bounds and is not kept past them.
-- Nothing here is appended to a book.
CREATE TABLE IF NOT EXISTS shelf_received (
digest TEXT PRIMARY KEY,
sender_key TEXT NOT NULL,
log_id TEXT NOT NULL,
position INTEGER NOT NULL,
prev TEXT NOT NULL,
deal_id TEXT NOT NULL,
offer INTEGER NOT NULL DEFAULT 0,
record BLOB NOT NULL,
received_at TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS shelf_received_position ON shelf_received (sender_key, log_id, position);
CREATE INDEX IF NOT EXISTS shelf_received_deal ON shelf_received (deal_id);
CREATE TABLE IF NOT EXISTS shelf_received_proofs (
sender_key TEXT NOT NULL,
log_id TEXT NOT NULL,
size INTEGER NOT NULL,
shelf_checkpoint_sha256 TEXT NOT NULL,
book_checkpoint BLOB NOT NULL,
fact BLOB NOT NULL,
status_opening BLOB,
inclusion_proof BLOB NOT NULL,
shelf_checkpoint BLOB NOT NULL,
received_at TEXT NOT NULL,
verified INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY (sender_key, log_id, size, shelf_checkpoint_sha256)
);
CREATE TABLE IF NOT EXISTS shelf_received_tombstones (
digest TEXT PRIMARY KEY,
sender_key TEXT NOT NULL,
received_at TEXT NOT NULL,
deleted_at TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS shelf_evidence (
id INTEGER PRIMARY KEY,
kind TEXT NOT NULL,
a BLOB NOT NULL,
b BLOB NOT NULL,
noted_at TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS shelf_received_conflicts (
sender_key TEXT NOT NULL,
log_id TEXT NOT NULL,
kind TEXT NOT NULL,
at INTEGER NOT NULL,
evidence_id INTEGER NOT NULL,
offer INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY (sender_key, log_id, kind, at)
);
CREATE TABLE IF NOT EXISTS shelf_received_vacated (
sender_key TEXT NOT NULL,
log_id TEXT NOT NULL,
position INTEGER NOT NULL,
digest TEXT NOT NULL,
deal_id TEXT NOT NULL,
offer INTEGER NOT NULL,
PRIMARY KEY (sender_key, log_id, position, digest)
);
";
pub(crate) fn write_tx(conn: &mut Connection) -> Result<Transaction<'_>, ShelfError> {
Ok(conn.transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?)
}
pub const SHELF_VERSION: i64 = 1;
pub const SHELF_VERSION_FACTS: i64 = 2;
pub(crate) const KIND_BOOK: &str = "book";
pub(crate) const KIND_HOST: &str = "host";
pub(crate) const KIND_JOURNAL: &str = "journal";
pub(crate) fn allow_journal_kind(tx: &Transaction<'_>) -> Result<(), ShelfError> {
let create: String = tx.query_row(
"SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'shelf_logs'",
[],
|r| r.get(0),
)?;
if create.contains("'journal'") {
return Ok(());
}
tx.execute_batch(
"CREATE TABLE shelf_logs_journal (log_id TEXT PRIMARY KEY, kind TEXT NOT NULL CHECK (kind IN ('book', 'host', 'journal')));
INSERT INTO shelf_logs_journal (log_id, kind) SELECT log_id, kind FROM shelf_logs;
DROP TABLE shelf_logs;
ALTER TABLE shelf_logs_journal RENAME TO shelf_logs;",
)?;
Ok(())
}
pub(crate) fn init(conn: &mut Connection) -> Result<(), ShelfError> {
let tx = write_tx(conn)?;
let has_table = |name: &str| -> Result<bool, ShelfError> {
let n: i64 = tx.query_row(
"SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = ?",
[name],
|r| r.get(0),
)?;
Ok(n > 0)
};
let mut versioned = false;
if has_table("shelf_meta")? {
let version: Option<i64> = tx
.query_row("SELECT version FROM shelf_meta WHERE id = 1", [], |r| {
r.get(0)
})
.optional()?;
match version {
None => {}
Some(v) if v != SHELF_VERSION && v != SHELF_VERSION_FACTS => {
return Err(ShelfError::Unsupported(format!(
"shelf file version {v} (this build opens versions {SHELF_VERSION} and {SHELF_VERSION_FACTS})"
)))
}
Some(_) => versioned = true,
}
}
let has_logs = has_table("shelf_logs")?;
tx.execute_batch(SCHEMA)?;
migrate_shelf_receipts(&tx)?;
if !versioned {
tx.execute(
"INSERT INTO shelf_meta (id, version) VALUES (1, ?)",
[SHELF_VERSION],
)?;
}
if !has_logs {
tx.execute_batch(
"INSERT INTO shelf_logs (log_id, kind) SELECT DISTINCT book_id, 'book' FROM book_records;
INSERT INTO shelf_logs (log_id, kind) SELECT log_id, 'host' FROM cadence_logs WHERE log_id NOT IN (SELECT book_id FROM book_records);",
)?;
}
tx.commit()?;
Ok(())
}
pub(crate) const RECEIPTS_KEYED_BY_URL: &str = "PRIMARY KEY (checkpoint_sha256, witness_id)";
fn migrate_shelf_receipts(tx: &Transaction<'_>) -> Result<(), ShelfError> {
let ddl: String = tx.query_row(
"SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'shelf_receipts'",
[],
|r| r.get(0),
)?;
if !ddl.contains(RECEIPTS_KEYED_BY_URL) {
return Ok(());
}
tx.execute_batch(
"CREATE TABLE shelf_receipts_492 (
checkpoint_sha256 TEXT NOT NULL,
witness_id TEXT NOT NULL,
key_id TEXT NOT NULL,
receipt TEXT NOT NULL,
registered_at INTEGER,
PRIMARY KEY (checkpoint_sha256, witness_id, key_id)
);
INSERT INTO shelf_receipts_492 (checkpoint_sha256, witness_id, key_id, receipt, registered_at) SELECT checkpoint_sha256, witness_id, key_id, receipt, registered_at FROM shelf_receipts;
DROP TABLE shelf_receipts;
ALTER TABLE shelf_receipts_492 RENAME TO shelf_receipts;",
)?;
Ok(())
}
pub(crate) fn log_kind(conn: &Connection, log_id: &str) -> Result<Option<String>, ShelfError> {
Ok(conn
.query_row(
"SELECT kind FROM shelf_logs WHERE log_id = ?",
[log_id],
|r| r.get(0),
)
.optional()?)
}
pub(crate) fn claim_kind(tx: &Transaction<'_>, log_id: &str, kind: &str) -> Result<(), ShelfError> {
tx.execute(
"INSERT INTO shelf_logs (log_id, kind) VALUES (?, ?) ON CONFLICT (log_id) DO NOTHING",
params![log_id, kind],
)?;
let held = log_kind(tx, log_id)?.unwrap_or_default();
if held != kind {
return Err(ShelfError::Invalid(format!(
"{log_id} is a {held} log, not a {kind} log"
)));
}
Ok(())
}
pub(crate) fn mark_live(tx: &Transaction<'_>, log_id: &str) -> Result<(), ShelfError> {
tx.execute(
"INSERT OR IGNORE INTO cadence_logs (log_id) VALUES (?)",
[log_id],
)?;
tx.execute(
"INSERT OR IGNORE INTO cadence_live (log_id) VALUES (?)",
[log_id],
)?;
Ok(())
}
pub(crate) fn record_count(conn: &Connection, log_id: &str) -> Result<u64, ShelfError> {
let n: i64 = conn.query_row(
"SELECT COUNT(*) FROM book_records WHERE book_id = ?",
[log_id],
|r| r.get(0),
)?;
Ok(n as u64)
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StoredRecord {
pub seq: u64,
pub record_id: String,
pub header: Vec<u8>,
pub capsule: Vec<u8>,
pub envelope: Vec<u8>,
}
pub(crate) fn put_record(
conn: &mut Connection,
book_id: &str,
kind: &str,
record: &StoredRecord,
) -> Result<(), ShelfError> {
if let Some(existing) = get_record(conn, book_id, &record.record_id)? {
if existing.seq != record.seq || existing.capsule != record.capsule {
return Err(ShelfError::Corrupt(format!(
"record {} already stored with different content",
record.record_id
)));
}
return Ok(());
}
let tx = write_tx(conn)?;
tx.execute(
"INSERT INTO book_records (book_id, seq, record_id, header, capsule, envelope) VALUES (?, ?, ?, ?, ?, ?)",
params![book_id, record.seq as i64, record.record_id, record.header, record.capsule, record.envelope],
)?;
claim_kind(&tx, book_id, kind)?;
mark_live(&tx, book_id)?;
tx.commit()?;
Ok(())
}
pub(crate) fn get_record(
conn: &Connection,
book_id: &str,
record_id: &str,
) -> Result<Option<StoredRecord>, ShelfError> {
Ok(conn
.query_row(
"SELECT seq, header, capsule, envelope FROM book_records WHERE book_id = ? AND record_id = ?",
params![book_id, record_id],
|r| {
Ok(StoredRecord {
seq: r.get::<_, i64>(0)? as u64,
record_id: record_id.to_string(),
header: r.get(1)?,
capsule: r.get(2)?,
envelope: r.get(3)?,
})
},
)
.optional()?)
}
pub(crate) fn records(conn: &Connection, book_id: &str) -> Result<Vec<StoredRecord>, ShelfError> {
let mut stmt = conn.prepare(
"SELECT seq, record_id, header, capsule, envelope FROM book_records WHERE book_id = ? ORDER BY seq",
)?;
let rows = stmt.query_map([book_id], |r| {
Ok(StoredRecord {
seq: r.get::<_, i64>(0)? as u64,
record_id: r.get(1)?,
header: r.get(2)?,
capsule: r.get(3)?,
envelope: r.get(4)?,
})
})?;
Ok(rows.collect::<Result<_, _>>()?)
}
pub(crate) fn put_checkpoint(
conn: &mut Connection,
book_id: &str,
checkpoint_id: &str,
encoded: &[u8],
) -> Result<(), ShelfError> {
let tx = write_tx(conn)?;
let held: i64 = tx.query_row(
"SELECT COUNT(*) FROM book_checkpoints WHERE book_id = ? AND checkpoint_id = ?",
params![book_id, checkpoint_id],
|r| r.get(0),
)?;
if held > 0 {
return Ok(());
}
let n: i64 = tx.query_row(
"SELECT COALESCE(MAX(n), 0) + 1 FROM book_checkpoints WHERE book_id = ?",
[book_id],
|r| r.get(0),
)?;
tx.execute(
"INSERT INTO book_checkpoints (book_id, n, checkpoint_id, checkpoint) VALUES (?, ?, ?, ?)",
params![book_id, n, checkpoint_id, encoded],
)?;
tx.commit()?;
Ok(())
}
pub(crate) fn checkpoints(conn: &Connection, book_id: &str) -> Result<Vec<Vec<u8>>, ShelfError> {
let mut stmt =
conn.prepare("SELECT checkpoint FROM book_checkpoints WHERE book_id = ? ORDER BY n")?;
let rows = stmt.query_map([book_id], |r| r.get(0))?;
Ok(rows.collect::<Result<_, _>>()?)
}