stock_rewrap.rs (4155B)
1 //! Original admission credentials survive uncertainty; repair grants no invocation. 2 use super::delivery::DeliveryContext; 3 use super::*; 4 use radroots_domain::OperationId; 5 use radroots_storage::inventory::{StockEnvelopeReceipt, StockRewrapOutcome, StockRewrapRequest}; 6 7 pub(in crate::session) struct RetainedAdmission { 8 context: DeliveryContext, 9 original: StockEnvelopeReceipt, 10 id: OperationId, 11 } 12 impl Owner { 13 pub(in crate::session) fn retain_rewrap_admission( 14 &self, 15 context: DeliveryContext, 16 original: StockEnvelopeReceipt, 17 id: OperationId, 18 ) -> Shared<RetainedAdmission> { 19 let entry = Shared::new(RetainedAdmission { 20 context, 21 original, 22 id, 23 }); 24 self.rewraps 25 .lock() 26 .unwrap_or_else(std::sync::PoisonError::into_inner) 27 .push(entry.clone()); 28 entry 29 } 30 pub(in crate::session) async fn prepare_rewrap( 31 &self, 32 entry: &Shared<RetainedAdmission>, 33 ) -> crate::Result<StockRewrapOutcome> { 34 let _serial = self.serial.lock().await; 35 let c = &entry.context; 36 let request = StockRewrapRequest::new(c.request(None).map_err(|e| c.error(e))?, entry.id); 37 let result = c 38 .store 39 .prepare_stock_rewrap(request, c.proof().map_err(|e| c.error(e))?) 40 .await 41 .map_err(|e| c.error(e))?; 42 match &result { 43 StockRewrapOutcome::Claimed(admitted) | StockRewrapOutcome::Retained(admitted) => { 44 if admitted.rewrap_id() != entry.id 45 || admitted.original() != entry.original 46 || admitted.checkpoint().authority() != c.selection.minimum.authority() 47 || admitted.checkpoint().revision() <= entry.original.checkpoint().revision() 48 || (matches!(&result, StockRewrapOutcome::Claimed(_)) 49 && admitted.checkpoint().revision() <= c.selection.minimum.revision()) 50 { 51 return Err(c.error(OperationError::RecoveryRequired)); 52 } 53 self.finish_rewrap_admission(entry); 54 } 55 StockRewrapOutcome::Unknown(attempt) => { 56 if attempt.workspace_id() != c.selection.binding.input().workspace_id() 57 || attempt.operation_id() != c.selection.operation 58 || attempt.command_digest() != c.selection.digest 59 { 60 return Err(c.error(OperationError::RecoveryRequired)); 61 } 62 // Neither an unknown write nor a lost waiter may create keys. 63 // Keep original proof, store and task/byte lease until repair. 64 } 65 } 66 Ok(result) 67 } 68 fn finish_rewrap_admission(&self, entry: &Shared<RetainedAdmission>) { 69 let removed = { 70 let mut entries = self 71 .rewraps 72 .lock() 73 .unwrap_or_else(std::sync::PoisonError::into_inner); 74 entries 75 .iter() 76 .position(|v| Shared::ptr_eq(v, entry)) 77 .map(|index| entries.remove(index)) 78 }; 79 drop(removed); 80 } 81 /// Called under the existing capture serial lock. This repairs the writer 82 /// fence only; status reads still require current access and consent. 83 pub(super) async fn drain_rewrap_admissions(&self) -> crate::Result<()> { 84 loop { 85 let entry = self 86 .rewraps 87 .lock() 88 .unwrap_or_else(std::sync::PoisonError::into_inner) 89 .first() 90 .cloned(); 91 let Some(entry) = entry else { return Ok(()) }; 92 let c = &entry.context; 93 c.store 94 .reconcile_stock_rewrap_admission( 95 StockRewrapRequest::new(c.request(None).map_err(|e| c.error(e))?, entry.id), 96 c.proof().map_err(|e| c.error(e))?, 97 ) 98 .await 99 .map_err(|e| c.error(e))?; 100 self.finish_rewrap_admission(&entry); 101 } 102 } 103 }