operation.rs (10222B)
1 //! Retained non-stock command preparation. No signing or delivery occurs here. 2 3 use super::{AttachmentInput, crypto::WorkspaceCrypto, listing}; 4 use radroots_domain::{IssuanceWindow, OperationId, TransactionId}; 5 use radroots_storage::command::{AdmissionRequest, OperationError, PreparedOperation}; 6 use sqlx::{Row, SqliteConnection}; 7 8 pub(super) const TABLE_V7: &str = "CREATE TABLE operations (workspace BLOB NOT NULL CHECK(length(workspace)=32), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), actor BLOB NOT NULL CHECK(length(actor)=32), grant_id BLOB NOT NULL CHECK(length(grant_id)=32), command_type TEXT NOT NULL CHECK(command_type IN ('inbox.inquire','inbox.reply')), command_digest BLOB NOT NULL CHECK(length(command_digest)=32), transaction_id BLOB NOT NULL CHECK(length(transaction_id)=32), phase TEXT NOT NULL CHECK(phase='prepared'), revision INTEGER NOT NULL CHECK(revision>0), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), grant_singleton INTEGER NOT NULL CHECK(grant_singleton=1) REFERENCES workspace_grant(singleton), PRIMARY KEY(workspace,operation_id)) STRICT, WITHOUT ROWID"; 9 pub(super) const TABLE: &str = "CREATE TABLE operations (workspace BLOB NOT NULL CHECK(length(workspace)=32), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), actor BLOB NOT NULL CHECK(length(actor)=32), grant_id BLOB NOT NULL CHECK(length(grant_id)=32), command_type TEXT NOT NULL CHECK(command_type IN ('inbox.inquire','inbox.reply','listing.publish')), command_digest BLOB NOT NULL CHECK(length(command_digest)=32), transaction_id BLOB NOT NULL CHECK(length(transaction_id)=32), phase TEXT NOT NULL CHECK(phase='prepared'), revision INTEGER NOT NULL CHECK(revision>0), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), grant_singleton INTEGER NOT NULL CHECK(grant_singleton=1) REFERENCES workspace_grant(singleton), PRIMARY KEY(workspace,operation_id)) STRICT, WITHOUT ROWID"; 10 pub(super) const INDEX: &str = 11 "CREATE UNIQUE INDEX operation_transaction_identity ON operations(workspace,transaction_id)"; 12 13 pub(super) struct ProtectedOperation { 14 pub bytes: Vec<u8>, 15 pub creation: Option<listing::Creation>, 16 } 17 18 pub(super) async fn protect( 19 request: &AdmissionRequest, 20 transaction_id: TransactionId, 21 window: IssuanceWindow, 22 crypto: &WorkspaceCrypto, 23 ) -> Result<Vec<u8>, OperationError> { 24 let plaintext = radroots_storage::command::protected::encode(request, transaction_id, window)?; 25 Ok(crypto 26 .seal_operation( 27 request.binding().input().workspace_id(), 28 request.command().context().operation_id(), 29 &plaintext, 30 ) 31 .await?) 32 } 33 34 pub(super) async fn read( 35 connection: &mut SqliteConnection, 36 selected: &AttachmentInput, 37 operation_id: OperationId, 38 crypto: &WorkspaceCrypto, 39 ) -> Result<Option<(PreparedOperation, ProtectedOperation)>, OperationError> { 40 let row = sqlx::query("SELECT CASE WHEN length(actor)=32 THEN actor END,CASE WHEN length(grant_id)=32 THEN grant_id END,CASE WHEN length(command_type)<=80 THEN command_type END,CASE WHEN length(command_digest)=32 THEN command_digest END,CASE WHEN length(transaction_id)=32 THEN transaction_id END,CASE WHEN length(phase)<=32 THEN phase END,revision,CASE WHEN length(protected)>0 AND length(protected)<=262144 THEN protected END FROM operations WHERE workspace=? AND operation_id=?") 41 .bind(selected.workspace_id().as_bytes().as_slice()).bind(operation_id.as_bytes().as_slice()) 42 .fetch_optional(&mut *connection).await.map_err(|_| OperationError::RecoveryRequired)?; 43 let Some(row) = row else { 44 absent(connection, selected, operation_id).await?; 45 return Ok(None); 46 }; 47 let bytes = |index| -> Result<Vec<u8>, OperationError> { 48 row.try_get::<Option<Vec<u8>>, _>(index) 49 .map_err(|_| OperationError::RecoveryRequired)? 50 .ok_or(OperationError::RecoveryRequired) 51 }; 52 let actor = bytes(0)?; 53 let grant = bytes(1)?; 54 let kind: String = row 55 .try_get::<Option<String>, _>(2) 56 .map_err(|_| OperationError::RecoveryRequired)? 57 .ok_or(OperationError::RecoveryRequired)?; 58 let digest = bytes(3)?; 59 let transaction = bytes(4)?; 60 let phase: String = row 61 .try_get::<Option<String>, _>(5) 62 .map_err(|_| OperationError::RecoveryRequired)? 63 .ok_or(OperationError::RecoveryRequired)?; 64 let revision: i64 = row 65 .try_get(6) 66 .map_err(|_| OperationError::RecoveryRequired)?; 67 let protected = bytes(7)?; 68 let plaintext = crypto 69 .open_operation(selected.workspace_id(), operation_id, &protected) 70 .await?; 71 let snapshot = plaintext.expose_secret(|bytes| { 72 radroots_storage::command::protected::decode(bytes, selected, operation_id) 73 })?; 74 let request = snapshot.request(); 75 if phase != "prepared" 76 || revision != 1 77 || actor != request.command().context().actor().participant().as_bytes() 78 || grant != request.binding().input().grant_id().as_bytes() 79 || digest != request.command().digest().as_bytes() 80 || transaction != snapshot.transaction_id().as_bytes() 81 || kind != request.command().kind().as_str() 82 { 83 return Err(OperationError::RecoveryRequired); 84 } 85 let creation = listing::Creation::read(connection, &snapshot, crypto).await?; 86 Ok(Some(( 87 snapshot, 88 ProtectedOperation { 89 bytes: protected, 90 creation, 91 }, 92 ))) 93 } 94 95 pub(super) async fn insert( 96 connection: &mut SqliteConnection, 97 request: &AdmissionRequest, 98 transaction_id: TransactionId, 99 protected: &[u8], 100 ) -> Result<(), OperationError> { 101 let context = request.command().context(); 102 sqlx::query("INSERT INTO operations(workspace,operation_id,actor,grant_id,command_type,command_digest,transaction_id,phase,revision,protected,grant_singleton) VALUES(?,?,?,?,?,?,?,'prepared',1,?,1)") 103 .bind(context.workspace_id().as_bytes().as_slice()).bind(context.operation_id().as_bytes().as_slice()) 104 .bind(context.actor().participant().as_bytes().as_slice()).bind(request.binding().input().grant_id().as_bytes().as_slice()) 105 .bind(request.command().kind().as_str()).bind(request.command().digest().as_bytes().as_slice()) 106 .bind(transaction_id.as_bytes().as_slice()).bind(protected) 107 .execute(connection).await.map_err(|_| OperationError::StorageFailure)?; 108 Ok(()) 109 } 110 111 /// Rechecks the retained ciphertext and every indexed fact in one transaction, 112 /// without awaiting a provider, signer or network capability under it. 113 pub(super) async fn unchanged( 114 connection: &mut SqliteConnection, 115 snapshot: &PreparedOperation, 116 expected: &ProtectedOperation, 117 ) -> Result<(), OperationError> { 118 let request = snapshot.request(); 119 let context = request.command().context(); 120 let matched: i64 = sqlx::query_scalar("SELECT count(*) FROM operations WHERE workspace=? AND operation_id=? AND actor=? AND grant_id=? AND command_type=? AND command_digest=? AND transaction_id=? AND phase='prepared' AND revision=? AND protected=?") 121 .bind(context.workspace_id().as_bytes().as_slice()).bind(context.operation_id().as_bytes().as_slice()) 122 .bind(context.actor().participant().as_bytes().as_slice()).bind(request.binding().input().grant_id().as_bytes().as_slice()) 123 .bind(request.command().kind().as_str()).bind(request.command().digest().as_bytes().as_slice()) 124 .bind(snapshot.transaction_id().as_bytes().as_slice()) 125 // Sequence construction enforces the signed 64-bit storage bound. 126 .bind(snapshot.revision().get() as i64).bind(&expected.bytes) 127 .fetch_one(&mut *connection).await.map_err(|_| OperationError::RecoveryRequired)?; 128 if matched != 1 { 129 return Err(OperationError::RecoveryRequired); 130 } 131 if let Some(creation) = &expected.creation { 132 creation.unchanged(connection).await?; 133 } else if listing::current(connection).await? { 134 if snapshot.request().command().listing().is_some() { 135 return Err(OperationError::RecoveryRequired); 136 } 137 listing::absent( 138 connection, 139 snapshot.request().binding().input(), 140 context.operation_id(), 141 ) 142 .await?; 143 } 144 Ok(()) 145 } 146 147 pub(super) use radroots_storage::command::protected::check_digest; 148 149 /// Confirms complete original-ID absence before a new preparation or absent 150 /// lookup. Every current operation-owned table has an indexed workspace/ID 151 /// prefix. EXISTS never materializes an unbounded retained record set. 152 pub(super) async fn absent( 153 connection: &mut SqliteConnection, 154 selected: &AttachmentInput, 155 operation: OperationId, 156 ) -> Result<(), OperationError> { 157 let retained: bool = sqlx::query_scalar(super::attempt::sql(connection, "SELECT EXISTS(SELECT 1 FROM operations WHERE workspace=?1 AND operation_id=?2) OR EXISTS(SELECT 1 FROM signing_plans WHERE workspace=?1 AND operation_id=?2) OR EXISTS(SELECT 1 FROM signing_plan_parts WHERE workspace=?1 AND operation_id=?2) OR EXISTS(SELECT 1 FROM signing_attempts WHERE workspace=?1 AND operation_id=?2) OR EXISTS(SELECT 1 FROM seal_evidence WHERE workspace=?1 AND operation_id=?2) OR EXISTS(SELECT 1 FROM seal_evidence_parts WHERE workspace=?1 AND operation_id=?2) OR EXISTS(SELECT 1 FROM chat_messages WHERE workspace=?1 AND operation_id=?2) OR EXISTS(SELECT 1 FROM chat_publication_work WHERE workspace=?1 AND operation_id=?2) OR EXISTS(SELECT 1 FROM chat_delivery_attempts WHERE workspace=?1 AND operation_id=?2) OR EXISTS(SELECT 1 FROM chat_delivery_parts WHERE workspace=?1 AND operation_id=?2) OR EXISTS(SELECT 1 FROM chat_delivery_observations WHERE workspace=?1 AND operation_id=?2)").await?) 158 .bind(selected.workspace_id().as_bytes().as_slice()) 159 .bind(operation.as_bytes().as_slice()) 160 .fetch_one(&mut *connection).await.map_err(|_|OperationError::RecoveryRequired)?; 161 listing::absent(connection, selected, operation).await?; 162 super::listing_installation::absent(connection, selected, operation).await?; 163 if retained { 164 return Err(OperationError::RecoveryRequired); 165 } 166 Ok(()) 167 }