lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

mod.rs (4911B)


      1 //! Append-only protected transport attempts on the existing workspace owner.
      2 
      3 mod codec;
      4 pub(super) mod containment;
      5 pub(super) mod graph;
      6 pub(in crate::workspace) mod json;
      7 pub(super) mod observation;
      8 mod observation_operations;
      9 mod operations;
     10 
     11 pub(super) const TABLE_V10: &str = "CREATE TABLE chat_delivery_attempts (workspace BLOB NOT NULL, operation_id BLOB NOT NULL, recipient BLOB NOT NULL CHECK(length(recipient)=32), request_key BLOB NOT NULL CHECK(length(request_key)=32), event_id BLOB NOT NULL CHECK(length(event_id)=32), transaction_id BLOB NOT NULL CHECK(length(transaction_id)=32), part_count INTEGER NOT NULL CHECK(part_count BETWEEN 1 AND 24), byte_count INTEGER NOT NULL CHECK(byte_count BETWEEN 1 AND 393216), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), PRIMARY KEY(workspace,operation_id,recipient,request_key), FOREIGN KEY(workspace,operation_id,recipient) REFERENCES chat_publication_work(workspace,operation_id,recipient)) STRICT, WITHOUT ROWID";
     12 pub(super) const TABLE: &str = "CREATE TABLE artifact_delivery_attempts (workspace BLOB NOT NULL, operation_id BLOB NOT NULL, artifact_key BLOB NOT NULL CHECK(length(artifact_key)=32 OR (length(artifact_key)=1 AND hex(artifact_key)='00')), request_key BLOB NOT NULL CHECK(length(request_key)=32), event_id BLOB NOT NULL CHECK(length(event_id)=32), transaction_id BLOB NOT NULL CHECK(length(transaction_id)=32), part_count INTEGER NOT NULL CHECK(part_count BETWEEN 1 AND 24), byte_count INTEGER NOT NULL CHECK(byte_count BETWEEN 1 AND 393216), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), PRIMARY KEY(workspace,operation_id,artifact_key,request_key), FOREIGN KEY(workspace,operation_id,artifact_key) REFERENCES artifact_evidence(workspace,operation_id,artifact_key)) STRICT, WITHOUT ROWID";
     13 pub(super) const PARTS_V10: &str = "CREATE TABLE chat_delivery_parts (workspace BLOB NOT NULL, operation_id BLOB NOT NULL, recipient BLOB NOT NULL, request_key BLOB NOT NULL, part INTEGER NOT NULL CHECK(part BETWEEN 1 AND 24), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), PRIMARY KEY(workspace,operation_id,recipient,request_key,part), FOREIGN KEY(workspace,operation_id,recipient,request_key) REFERENCES chat_delivery_attempts(workspace,operation_id,recipient,request_key)) STRICT, WITHOUT ROWID";
     14 pub(super) const PARTS: &str = "CREATE TABLE artifact_delivery_parts (workspace BLOB NOT NULL, operation_id BLOB NOT NULL, artifact_key BLOB NOT NULL, request_key BLOB NOT NULL, part INTEGER NOT NULL CHECK(part BETWEEN 1 AND 24), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), PRIMARY KEY(workspace,operation_id,artifact_key,request_key,part), FOREIGN KEY(workspace,operation_id,artifact_key,request_key) REFERENCES artifact_delivery_attempts(workspace,operation_id,artifact_key,request_key)) STRICT, WITHOUT ROWID";
     15 pub(super) const TX_INDEX_V10: &str = "CREATE UNIQUE INDEX chat_delivery_transaction_identity ON chat_delivery_attempts(workspace,transaction_id)";
     16 pub(super) const TX_INDEX: &str = "CREATE UNIQUE INDEX artifact_delivery_transaction_identity ON artifact_delivery_attempts(workspace,transaction_id)";
     17 
     18 const MAX_BYTES: usize = 393216;
     19 const MAX_PROTECTED_BYTES: usize = 2 * 1024 * 1024;
     20 const MAX_ATTEMPTS: usize = 2 * 16 * 12;
     21 pub(super) const OBSERVATIONS: &str = observation::TABLE;
     22 pub(super) const OBSERVATIONS_V10: &str = observation::TABLE_V10;
     23 
     24 /// Fixed identifier adaptation for exact retained catalogs, never user SQL.
     25 pub(super) async fn sql(
     26     connection: &mut sqlx::SqliteConnection,
     27     legacy: &'static str,
     28 ) -> Result<sqlx::AssertSqlSafe<String>, radroots_storage::command::OperationError> {
     29     let version: i64 = sqlx::query_scalar("PRAGMA user_version")
     30         .fetch_one(connection)
     31         .await
     32         .map_err(|_| radroots_storage::command::OperationError::RecoveryRequired)?;
     33     match version {
     34         7..=10 => Ok(sqlx::AssertSqlSafe(legacy.to_owned())),
     35         11 => Ok(sqlx::AssertSqlSafe(
     36             legacy
     37                 .replace("chat_delivery", "artifact_delivery")
     38                 .replace("recipient", "artifact_key"),
     39         )),
     40         _ => Err(radroots_storage::command::OperationError::RecoveryRequired),
     41     }
     42 }
     43 pub(super) async fn require_target(
     44     connection: &mut sqlx::SqliteConnection,
     45     target: radroots_storage::command::SigningTarget,
     46 ) -> Result<(), radroots_storage::command::OperationError> {
     47     if target == radroots_storage::command::SigningTarget::PublicEvent {
     48         let version: i64 = sqlx::query_scalar("PRAGMA user_version")
     49             .fetch_one(connection)
     50             .await
     51             .map_err(|_| radroots_storage::command::OperationError::RecoveryRequired)?;
     52         if version != 11 {
     53             return Err(radroots_storage::command::OperationError::UnsupportedOperation);
     54         }
     55     }
     56     Ok(())
     57 }