observation.rs (4226B)
1 //! Original per-target classes, tied to one actual protected potential invocation. 2 use super::*; 3 use serde::{Deserialize, Serialize}; 4 use wire::Reference; 5 6 #[derive(Serialize, Deserialize)] 7 #[serde(deny_unknown_fields)] 8 pub(super) struct Wire { 9 version: u8, 10 attempt: Reference, 11 target: DeliveryTargetReceipt, 12 kind: u8, 13 #[serde(deserialize_with = "super::super::required_nullable")] 14 observed: Option<NonZeroU64>, 15 } 16 impl Wire { 17 pub(super) fn new(attempt: &attempt::Retained, observation: &StockDeliveryObservation) -> Self { 18 Self { 19 version: 1, 20 attempt: Reference::new(&attempt.frame.header), 21 target: observation.target_receipt().clone(), 22 kind: observation.kind().code(), 23 observed: observation.observed_at_unix_ms(), 24 } 25 } 26 } 27 pub(in crate::workspace::stock) struct Retained { 28 pub frame: ledger::Retained, 29 pub receipt: StockDeliveryObservationReceipt, 30 pub projection: Projection, 31 } 32 impl Retained { 33 pub(super) async fn confirm( 34 &self, 35 connection: &mut SqliteConnection, 36 ) -> Result<(), OperationError> { 37 confirm_record(connection, &self.frame, self.projection).await 38 } 39 pub(in crate::workspace::stock) fn into_history(self) -> ledger::HistoryEntry { 40 history(self.frame, self.projection) 41 } 42 } 43 pub(in crate::workspace::stock) async fn read_entry( 44 connection: &mut SqliteConnection, 45 input: &AttachmentInput, 46 crypto: &WorkspaceCrypto, 47 binding: &WorkspaceBinding, 48 administrator: GrantId, 49 entry: OperationId, 50 ) -> Result<Option<Retained>, OperationError> { 51 let Some(frame) = Box::pin(ledger::read(connection, input, crypto, entry)).await? else { 52 return Ok(None); 53 }; 54 if frame.header.family != ledger::Family::DeliveryObservation { 55 return Err(OperationError::RecoveryRequired); 56 } 57 Ok(Some( 58 Box::pin(decode_entry( 59 connection, 60 input, 61 crypto, 62 binding, 63 administrator, 64 frame, 65 )) 66 .await?, 67 )) 68 } 69 async fn decode_entry( 70 connection: &mut SqliteConnection, 71 input: &AttachmentInput, 72 crypto: &WorkspaceCrypto, 73 binding: &WorkspaceBinding, 74 administrator: GrantId, 75 frame: ledger::Retained, 76 ) -> Result<Retained, OperationError> { 77 let value: Wire = wire::decode(&frame.body, OBSERVATION_MAX)?; 78 if value.version != 1 || wire::head(&frame.header, &frame.body) != frame.header.head { 79 return Err(OperationError::RecoveryRequired); 80 } 81 let attempt = Box::pin(attempt::read_entry( 82 connection, 83 input, 84 crypto, 85 binding, 86 administrator, 87 value.attempt.entry, 88 )) 89 .await? 90 .ok_or(OperationError::RecoveryRequired)?; 91 value.attempt.confirm(&attempt.frame.header)?; 92 let h = &frame.header; 93 let original = &attempt.frame.header; 94 if h.actor != original.actor 95 || h.digest != original.digest 96 || h.order != original.order 97 || h.related_operation != original.related_operation 98 || h.revision <= original.revision 99 { 100 return Err(OperationError::RecoveryRequired); 101 } 102 let observation = 103 StockDeliveryObservation::from_store_report(&attempt.report, &value.target, value.observed) 104 .map_err(|_| OperationError::RecoveryRequired)?; 105 if observation.kind().code() != value.kind { 106 return Err(OperationError::RecoveryRequired); 107 } 108 let source = observation.source(); 109 let projection = Projection::Observation { 110 operation: source.attempt().operation_id(), 111 recipient: source.recipient(), 112 request: request_key(observation.request_id()), 113 target: target_key(observation.target_receipt().target().fingerprint()), 114 kind: observation.kind().code(), 115 }; 116 let receipt = StockDeliveryObservationReceipt::from_store_report( 117 observation, 118 h.transaction, 119 InventoryCheckpointRef::from_host_metadata(input.authority(), h.revision, Some(h.head))?, 120 )?; 121 let retained = Retained { 122 frame, 123 receipt, 124 projection, 125 }; 126 retained.confirm(connection).await?; 127 Ok(retained) 128 }