admission.rs (5006B)
1 //! Original public snapshots and protected current-observed source checks. 2 //! The caller independently proves request/quote and current execution rights. 3 use super::*; 4 use radroots_domain::{AgreementLine, UnixSeconds, listing::ListingState}; 5 use radroots_storage::command::OperationError; 6 7 pub(in crate::workspace::stock) struct ListingView { 8 source: ListingRevision, 9 original: checkpoint::Proof, 10 projection: checkpoint::Proof, 11 head: Option<record::RetainedHead>, 12 parts: u8, 13 parts_witness: [u8; 32], 14 listing: Box<radroots_protocol::listing::DecodedListing>, 15 } 16 pub(in crate::workspace::stock) async fn prepare( 17 connection: &mut SqliteConnection, 18 input: &AttachmentInput, 19 crypto: &WorkspaceCrypto, 20 authority: &Authority, 21 source: ListingRevision, 22 observed_at: UnixSeconds, 23 ) -> Result<ListingView, OperationError> { 24 let retained = record::read( 25 connection, 26 input, 27 crypto, 28 authority.input.grant_id(), 29 source.event_id(), 30 ) 31 .await?; 32 let original = checkpoint::authenticate( 33 connection, 34 input, 35 crypto, 36 authority, 37 checkpoint::Target::Event( 38 source.event_id(), 39 retained 40 .as_ref() 41 .map(|value| value.identity()) 42 .transpose()?, 43 ), 44 ) 45 .await?; 46 let retained = retained.ok_or(OperationError::MissingHistory)?; 47 if retained.header.manifest.listing != source { 48 return Err(OperationError::InvalidEvidence); 49 } 50 let parts = record::read_parts(connection, input, crypto, &retained.header).await?; 51 let head = record::read_head(connection, input, crypto, source.coordinate()).await?; 52 let projection = checkpoint::authenticate( 53 connection, 54 input, 55 crypto, 56 authority, 57 checkpoint::Target::Coordinate( 58 source.coordinate(), 59 head.as_ref().map(|value| value.head.clone()), 60 ), 61 ) 62 .await?; 63 let raw = std::str::from_utf8(&parts.raw).map_err(|_| OperationError::RecoveryRequired)?; 64 let mut observed = ObservedHead::listing( 65 source.coordinate().author(), 66 &source.coordinate().d().to_string(), 67 ) 68 .map_err(|_| OperationError::RecoveryRequired)?; 69 observed 70 .observe(raw) 71 .map_err(|_| OperationError::RecoveryRequired)?; 72 let meaning = observed 73 .interpret( 74 radroots_protocol::advertisement::UnixSeconds::new(observed_at.get()) 75 .map_err(|_| WorkspaceError::Unavailable)?, 76 ) 77 .ok_or(OperationError::RecoveryRequired)?; 78 let listing = match meaning { 79 radroots_nostr::public_ingress::Interpretation::Listing(listing) => listing, 80 radroots_nostr::public_ingress::Interpretation::ClockAhead => { 81 return Err(WorkspaceError::Unavailable.into()); 82 } 83 radroots_nostr::public_ingress::Interpretation::Unsupported => { 84 return Err(OperationError::UnsupportedOperation); 85 } 86 _ => return Err(OperationError::InvalidEvidence), 87 }; 88 Ok(ListingView { 89 source, 90 original, 91 projection, 92 head, 93 parts: retained.header.manifest.parts, 94 parts_witness: parts.witness, 95 listing, 96 }) 97 } 98 impl ListingView { 99 pub(in crate::workspace::stock) fn validate( 100 &self, 101 line: &AgreementLine, 102 explicit_quote: bool, 103 observed_at: UnixSeconds, 104 ) -> Result<(), OperationError> { 105 let listing = self.listing.listing(); 106 let terms = listing.terms(); 107 if listing.created_at() > observed_at { 108 return Err(WorkspaceError::Unavailable.into()); 109 } 110 if line.source() != self.source 111 || listing.source() != self.source 112 || line.product() != terms.product() 113 || terms 114 .options() 115 .iter() 116 .find(|option| option.id() == line.option_id()) 117 != Some(line.option()) 118 { 119 return Err(OperationError::InvalidEvidence); 120 } 121 if !explicit_quote 122 && (terms.state() != ListingState::Active 123 || !self 124 .head 125 .as_ref() 126 .is_some_and(|head| head.head.listing == self.source)) 127 { 128 return Err(OperationError::StaleState); 129 } 130 Ok(()) 131 } 132 pub(in crate::workspace::stock) async fn confirm( 133 &self, 134 connection: &mut SqliteConnection, 135 ) -> Result<(), OperationError> { 136 self.original.confirm(connection).await?; 137 self.projection.confirm(connection).await?; 138 record::confirm_head(connection, self.source.coordinate(), self.head.as_ref()).await?; 139 if record::parts_witness(connection, self.source.event_id()).await? 140 != (self.parts, self.parts_witness) 141 { 142 return Err(OperationError::RecoveryRequired); 143 } 144 Ok(()) 145 } 146 }