stock_obtained.rs (3375B)
1 //! Register obtained bytes before any fallible storage-descriptor conversion. 2 use super::*; 3 use crate::session::commerce::{AcceptanceClaim, AcceptanceSelection}; 4 use radroots_domain::PublicKey; 5 use radroots_signing::issuance::SignedArtifact; 6 7 #[cfg(all(test, not(target_arch = "wasm32")))] 8 #[path = "stock_obtained_tests.rs"] 9 mod tests; 10 11 pub(in crate::session) struct ObtainedContext { 12 pub selection: AcceptanceSelection, 13 pub claim: AcceptanceClaim, 14 pub recipient: PublicKey, 15 pub store: Shared<dyn InventoryStore>, 16 pub proof: AttachmentProof, 17 pub lease: Shared<dyn HostBound>, 18 } 19 pub(in crate::session) struct RetainedObtained { 20 context: ObtainedContext, 21 artifact: SignedArtifact, 22 mapped: Mutex<Option<Shared<Pending>>>, 23 } 24 impl Owner { 25 pub(in crate::session) fn retain_obtained( 26 &self, 27 context: ObtainedContext, 28 artifact: SignedArtifact, 29 ) -> Shared<RetainedObtained> { 30 let entry = Shared::new(RetainedObtained { 31 context, 32 artifact, 33 mapped: Mutex::new(None), 34 }); 35 self.unmapped 36 .lock() 37 .unwrap_or_else(std::sync::PoisonError::into_inner) 38 .push(entry.clone()); 39 entry 40 } 41 pub(super) fn map_obtained( 42 &self, 43 entry: &Shared<RetainedObtained>, 44 ) -> crate::Result<Shared<Pending>> { 45 let mut mapped = entry 46 .mapped 47 .lock() 48 .unwrap_or_else(std::sync::PoisonError::into_inner); 49 if let Some(pending) = mapped.as_ref() { 50 return Ok(pending.clone()); 51 } 52 let context = &entry.context; 53 let error = |reason| { 54 Error::capture_pending( 55 reason, 56 OperationContext::retained(context.selection.operation), 57 ) 58 }; 59 let request = entry 60 .artifact 61 .with_json(|raw| { 62 StockEvidenceRequest::from_obtained_wire( 63 context.selection.binding.clone(), 64 context.selection.operation, 65 context.selection.digest, 66 context.claim.id, 67 context.recipient, 68 *context.claim.binding.digest(), 69 raw, 70 entry.artifact.observed_at_unix_ms(), 71 ) 72 }) 73 .map_err(error)?; 74 let proof = context 75 .proof 76 .with_secret(|bytes| AttachmentProof::from_protected_input(bytes)) 77 .map_err(|e| error(e.into()))?; 78 let pending = self.retain(context.store.clone(), request, proof, context.lease.clone()); 79 *mapped = Some(pending.clone()); 80 let removed = { 81 let mut raw = self 82 .unmapped 83 .lock() 84 .unwrap_or_else(std::sync::PoisonError::into_inner); 85 raw.iter() 86 .position(|v| Shared::ptr_eq(v, entry)) 87 .map(|index| raw.remove(index)) 88 }; 89 drop(removed); 90 Ok(pending) 91 } 92 pub(in crate::session) async fn capture_obtained( 93 &self, 94 entry: &Shared<RetainedObtained>, 95 ) -> crate::Result<StockEvidenceOutcome> { 96 let _serial = self.serial.lock().await; 97 let pending = self.map_obtained(entry)?; 98 self.append(&pending, false).await 99 } 100 }