snapshot.rs (15354B)
1 //! One authenticated observation of original preparation and per-recipient S. 2 use super::*; 3 use inventory::{Turn, checkpoint}; 4 use issuance::{claim, evidence::selection}; 5 use radroots_domain::{CommandDigest, OperationId, PublicKey, SigningAttemptId, UnixSeconds}; 6 use radroots_signing::issuance::IntentBinding; 7 use radroots_storage::{command::OperationError, inventory::InventoryCheckpointRef}; 8 9 /// Original-operation selector data; it grants no read or execution permission. 10 pub struct StockSnapshotRequest { 11 binding: WorkspaceBinding, 12 minimum: InventoryCheckpointRef, 13 operation: OperationId, 14 digest: CommandDigest, 15 } 16 impl StockSnapshotRequest { 17 pub const fn new( 18 binding: WorkspaceBinding, 19 minimum: InventoryCheckpointRef, 20 operation: OperationId, 21 digest: CommandDigest, 22 ) -> Self { 23 Self { 24 binding, 25 minimum, 26 operation, 27 digest, 28 } 29 } 30 } 31 impl std::fmt::Debug for StockSnapshotRequest { 32 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 33 f.write_str("StockSnapshotRequest(<redacted>)") 34 } 35 } 36 37 /// Recorded signing facts, independent of installation or delivery success. 38 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 39 pub enum StockSigningProgress { 40 Unbound, 41 Bound, 42 Unknown, 43 Obtained, 44 OriginalUnissued, 45 } 46 /// Immutable claim identity. This reference cannot construct a dispatch permit. 47 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 48 pub struct StockClaimReference { 49 id: SigningAttemptId, 50 transaction: TransactionId, 51 at: UnixSeconds, 52 binding: IntentBinding, 53 } 54 impl StockClaimReference { 55 pub const fn attempt_id(&self) -> SigningAttemptId { 56 self.id 57 } 58 pub const fn transaction_id(&self) -> TransactionId { 59 self.transaction 60 } 61 pub const fn claimed_at(&self) -> UnixSeconds { 62 self.at 63 } 64 pub const fn binding(&self) -> IntentBinding { 65 self.binding 66 } 67 } 68 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 69 pub struct StockRecipientSnapshot { 70 recipient: PublicKey, 71 expected: Option<[u8; 32]>, 72 claim: Option<StockClaimReference>, 73 count: Sequence, 74 first: Option<StockEvidenceReceipt>, 75 unissued: Option<super::StockUnissuedReceipt>, 76 replay: Option<super::StockReplayAdmissionReceipt>, 77 } 78 impl StockRecipientSnapshot { 79 pub const fn recipient(&self) -> PublicKey { 80 self.recipient 81 } 82 pub const fn expected_event_id(&self) -> Option<[u8; 32]> { 83 self.expected 84 } 85 pub const fn claim(&self) -> Option<StockClaimReference> { 86 self.claim 87 } 88 /// Number of distinct retained original encodings, not new commitments. 89 pub const fn evidence_count(&self) -> Sequence { 90 self.count 91 } 92 pub const fn first_evidence(&self) -> Option<StockEvidenceReceipt> { 93 self.first 94 } 95 /// Authenticated original-invocation fact, independent of later S. 96 pub const fn original_unissued(&self) -> Option<super::StockUnissuedReceipt> { 97 self.unissued 98 } 99 /// First durable current-catalog replay admission, not invocation or S. 100 /// Retained catalogs without this family return None. 101 pub const fn replay_admission(&self) -> Option<super::StockReplayAdmissionReceipt> { 102 self.replay 103 } 104 pub const fn progress(&self) -> StockSigningProgress { 105 if self.first.is_some() { 106 StockSigningProgress::Obtained 107 } else if self.unissued.is_some() { 108 StockSigningProgress::OriginalUnissued 109 } else if self.claim.is_some() { 110 StockSigningProgress::Unknown 111 } else if self.expected.is_some() { 112 StockSigningProgress::Bound 113 } else { 114 StockSigningProgress::Unbound 115 } 116 } 117 } 118 /// Coherent original T0/T1/claim/obtained view. No installation, release, 119 /// reissuance, delivery or global history-completeness claim follows from it. 120 pub struct StockOperationSnapshot { 121 preparation: StockAcceptanceReceipt, 122 plan: Option<StockBoundSigningPlan>, 123 checkpoint: InventoryCheckpointRef, 124 recipients: [StockRecipientSnapshot; 2], 125 installation: Option<super::StockInstallationReceipt>, 126 aborted: Option<super::StockAbortReceipt>, 127 delivery: Option<Box<[radroots_storage::inventory::StockDeliverySummary; 2]>>, 128 } 129 impl StockOperationSnapshot { 130 /// Coherent original per-recipient delivery progress; retained catalogs 131 /// without stock D expose no supported delivery view. 132 pub fn delivery(&self) -> Option<&[radroots_storage::inventory::StockDeliverySummary; 2]> { 133 self.delivery.as_deref() 134 } 135 pub fn aborted(&self) -> Option<&super::StockAbortReceipt> { 136 self.aborted.as_ref() 137 } 138 pub fn installation(&self) -> Option<&super::StockInstallationReceipt> { 139 self.installation.as_ref() 140 } 141 pub const fn preparation(&self) -> &StockAcceptanceReceipt { 142 &self.preparation 143 } 144 pub const fn signing_plan(&self) -> Option<&StockBoundSigningPlan> { 145 self.plan.as_ref() 146 } 147 pub const fn checkpoint(&self) -> InventoryCheckpointRef { 148 self.checkpoint 149 } 150 pub const fn recipients(&self) -> &[StockRecipientSnapshot; 2] { 151 &self.recipients 152 } 153 } 154 impl std::fmt::Debug for StockOperationSnapshot { 155 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 156 f.write_str("StockOperationSnapshot(<redacted>)") 157 } 158 } 159 160 pub(in crate::workspace) async fn read( 161 turn: &mut Turn<'_>, 162 request: StockSnapshotRequest, 163 proof: AttachmentProof, 164 ) -> Result<StockOperationSnapshot, OperationError> { 165 let auth = Box::pin(signing_plan::load( 166 turn, 167 &request.binding, 168 request.minimum, 169 request.operation, 170 request.digest, 171 &proof, 172 )) 173 .await?; 174 Box::pin(read_authorized(turn, request, proof, auth)).await 175 } 176 177 // Keep authorization loading outside the projection's larger polling frame. 178 async fn read_authorized( 179 turn: &mut Turn<'_>, 180 request: StockSnapshotRequest, 181 proof: AttachmentProof, 182 auth: signing_plan::Authorized, 183 ) -> Result<StockOperationSnapshot, OperationError> { 184 if auth.access.authority.catalog < 11 { 185 return Err(OperationError::UnsupportedOperation); 186 } 187 let mut reports = Vec::with_capacity(2); 188 let mut claims = Vec::with_capacity(2); 189 let mut refusals = Vec::with_capacity(2); 190 let mut replays = Vec::with_capacity(2); 191 let mut summaries = Vec::with_capacity(2); 192 for recipient in auth.original.receipt.rumor().recipients() { 193 let (retained, claim_proof) = 194 Box::pin(claim::existing(turn, &auth, request.operation, *recipient)).await?; 195 let reference = retained.as_ref().map(|value| StockClaimReference { 196 id: value.report.attempt_id(), 197 transaction: value.report.transaction_id(), 198 at: value.report.claimed_at(), 199 binding: value.report.binding(), 200 }); 201 let summary = selection::Summary::read( 202 turn.connection, 203 turn.selected.participant(), 204 request.operation, 205 *recipient, 206 ) 207 .await?; 208 let first = if let Some(selected) = summary.first { 209 Some( 210 Box::pin(selection::open( 211 turn.connection, 212 turn.selected, 213 turn.crypto, 214 request.operation, 215 *recipient, 216 selected, 217 )) 218 .await? 219 .value 220 .receipt(), 221 ) 222 } else { 223 None 224 }; 225 let expected = auth.plan.as_ref().and_then(|plan| { 226 plan.bound 227 .plan() 228 .seals() 229 .iter() 230 .find(|seal| seal.recipient() == *recipient) 231 .map(|seal| seal.event_id()) 232 }); 233 if reference.is_some() && expected.is_none() 234 || first.is_some_and(|value| { 235 reference.is_none_or(|claim| claim.id != value.signing_attempt_id()) 236 }) 237 { 238 return Err(OperationError::RecoveryRequired); 239 } 240 let unissued = if auth.access.authority.catalog >= 14 { 241 Box::pin(issuance::unissued::lookup( 242 turn.connection, 243 turn.selected, 244 turn.crypto, 245 request.operation, 246 *recipient, 247 )) 248 .await? 249 } else { 250 None 251 }; 252 if unissued 253 .as_ref() 254 .is_some_and(|v| reference.is_none_or(|c| c.id != v.receipt().signing_attempt_id())) 255 { 256 return Err(OperationError::RecoveryRequired); 257 } 258 let replay = if auth.access.authority.catalog >= 15 { 259 Box::pin(issuance::replay_admission::lookup( 260 turn.connection, 261 turn.selected, 262 turn.crypto, 263 request.operation, 264 *recipient, 265 )) 266 .await? 267 } else { 268 None 269 }; 270 if replay 271 .as_ref() 272 .is_some_and(|v| reference.is_none_or(|c| c.id != v.receipt().signing_attempt_id())) 273 { 274 return Err(OperationError::RecoveryRequired); 275 } 276 reports.push(StockRecipientSnapshot { 277 recipient: *recipient, 278 expected, 279 claim: reference, 280 count: summary.count, 281 first, 282 unissued: unissued.as_ref().map(|v| v.receipt()), 283 replay: replay.as_ref().map(|v| v.receipt()), 284 }); 285 refusals.push(unissued); 286 replays.push(replay); 287 claims.push(claim_proof); 288 summaries.push(summary); 289 } 290 let installation = if auth.access.authority.catalog >= 12 { 291 Some( 292 Box::pin(super::inventory::installation::existing( 293 turn, 294 &auth, 295 request.operation, 296 )) 297 .await?, 298 ) 299 } else { 300 None 301 }; 302 let aborted = if auth.access.authority.catalog >= 13 { 303 Some( 304 Box::pin(super::inventory::abort::existing( 305 turn, 306 &auth.access, 307 request.operation, 308 )) 309 .await?, 310 ) 311 } else { 312 None 313 }; 314 if aborted.as_ref().is_some_and(|(value, _)| value.is_some()) 315 && installation 316 .as_ref() 317 .is_some_and(|(value, _)| value.is_some()) 318 { 319 return Err(OperationError::RecoveryRequired); 320 } 321 let current_order = if auth.access.authority.catalog >= 13 { 322 Some( 323 Box::pin(super::inventory::checkpoint::authenticate_order_state( 324 turn.connection, 325 turn.selected, 326 turn.crypto, 327 &auth.access.current, 328 &auth.access.authority, 329 auth.original.receipt.order_id(), 330 )) 331 .await?, 332 ) 333 } else { 334 None 335 }; 336 let before = auth 337 .access 338 .authority 339 .inventory 340 .ok_or(OperationError::RecoveryRequired)?; 341 let delivery = if matches!(auth.access.authority.catalog, 17..=20) { 342 Some(Box::new( 343 Box::pin(super::delivery::snapshot( 344 turn, 345 radroots_storage::inventory::StockEnvelopeRequest::new( 346 request.binding.clone(), 347 request.minimum, 348 request.operation, 349 request.digest, 350 auth.original.receipt.rumor().recipients()[0], 351 None, 352 )?, 353 &auth.access.authority, 354 )) 355 .await?, 356 )) 357 } else { 358 None 359 }; 360 let minimum = InventoryCheckpointRef::from_host_metadata( 361 turn.selected.authority(), 362 Sequence::new(0).map_err(|_| WorkspaceError::InvalidInput)?, 363 None, 364 )?; 365 let witness = if current_order.is_none() { 366 Some( 367 Box::pin(checkpoint::authenticate_minimum( 368 turn.connection, 369 turn.selected, 370 turn.crypto, 371 &auth.access.current, 372 &auth.access.authority, 373 minimum, 374 )) 375 .await?, 376 ) 377 } else { 378 None 379 }; 380 let mut tx = turn 381 .connection 382 .begin_with("BEGIN IMMEDIATE") 383 .await 384 .map_err(|_| OperationError::StorageFailure)?; 385 let checked: Result<(), OperationError> = async { 386 if let Some(witness) = &witness { 387 witness.confirm(&mut tx).await?; 388 } 389 if let Some(current_order) = ¤t_order { 390 current_order.confirm(&mut tx).await?; 391 } 392 if let Some((retained, identity)) = &aborted { 393 identity.confirm(&mut tx).await?; 394 if let Some(retained) = retained { 395 retained.confirm(&mut tx).await?; 396 } 397 } 398 if let Some((retained, identity)) = &installation { 399 identity.confirm(&mut tx).await?; 400 if let Some(retained) = retained { 401 retained.confirm(&mut tx).await?; 402 } 403 } 404 for (index, report) in reports.iter().enumerate() { 405 claims[index].confirm(&mut tx).await?; 406 if let Some(value) = &replays[index] { 407 value.confirm(&mut tx).await?; 408 } 409 if let Some(value) = &refusals[index] { 410 value.confirm(&mut tx).await?; 411 } 412 summaries[index] 413 .confirm( 414 &mut tx, 415 turn.selected.participant(), 416 request.operation, 417 report.recipient, 418 ) 419 .await?; 420 } 421 Box::pin(auth.confirm(&mut tx, turn.namespace, turn.clock, &proof, false)).await?; 422 Ok(()) 423 } 424 .await; 425 if tx.rollback().await.is_err() { 426 *turn.uncertain = true; 427 return Err(OperationError::RecoveryRequired); 428 } 429 checked?; 430 let checkpoint = InventoryCheckpointRef::from_host_metadata( 431 turn.selected.authority(), 432 before.revision, 433 before.head, 434 )? 435 .with_authorization( 436 auth.access 437 .authority 438 .authorization 439 .ok_or(OperationError::RecoveryRequired)?, 440 ); 441 Ok(StockOperationSnapshot { 442 delivery, 443 aborted: aborted.and_then(|(retained, _)| retained.map(|value| value.receipt)), 444 installation: installation.and_then(|(retained, _)| retained.map(|value| value.receipt)), 445 preparation: auth.original.receipt, 446 plan: auth.plan.map(|value| value.bound), 447 checkpoint, 448 recipients: reports 449 .try_into() 450 .map_err(|_| OperationError::RecoveryRequired)?, 451 }) 452 } 453 impl NativeWorkspace { 454 pub async fn stock_operation_snapshot( 455 &self, 456 request: StockSnapshotRequest, 457 proof: &AttachmentProof, 458 ) -> Result<StockOperationSnapshot, OperationError> { 459 self.worker 460 .stock_operation_snapshot( 461 request, 462 proof.with_secret(|bytes| AttachmentProof::from_protected_input(bytes))?, 463 ) 464 .await 465 } 466 }