request.rs (7732B)
1 //! Initial request claims only. Concrete adapters must authenticate complete 2 //! coverage, current public/quote source permission and actual execution rights. 3 use super::*; 4 use crate::{CommercePayload, Terms, TermsRequest, UnixSeconds}; 5 6 /// Borrowed complete request data after self-contained local causal/time checks. 7 /// This value supplies no signature, current-source or execution capability. 8 pub struct InitialRequestClaim<'a> { 9 request: RecordId, 10 terms: &'a Terms, 11 expires: UnixSeconds, 12 quote: Option<RecordId>, 13 } 14 impl InitialRequestClaim<'_> { 15 /// Exact selected request claim; original carrier authentication is separate. 16 pub const fn request(&self) -> RecordId { 17 self.request 18 } 19 /// Complete private terms claim; no stock is acquired by borrowing it. 20 pub const fn terms(&self) -> &Terms { 21 self.terms 22 } 23 /// Earliest current request/explicit-quote deadline, excluding execution scope. 24 pub const fn expires_at(&self) -> UnixSeconds { 25 self.expires 26 } 27 /// Exact optional seller quote whose complete terms equal this request. 28 pub const fn quote(&self) -> Option<RecordId> { 29 self.quote 30 } 31 } 32 impl fmt::Debug for InitialRequestClaim<'_> { 33 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 34 f.write_str("InitialRequestClaim(<private data>)") 35 } 36 } 37 /// Bounded initial-request claim failures, without private source values. 38 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 39 #[non_exhaustive] 40 pub enum RequestClaimError { 41 /// Source count or identity/scope declarations are invalid. 42 InvalidGraph, 43 /// Required original parents or the selected request are absent. 44 MissingHistory, 45 /// The known scoped graph contains an incompatible author fork or cycle. 46 Conflicted, 47 /// Authenticated uninterpreted evidence prevents a complete local reduction. 48 Uninterpreted, 49 /// The selected record is not a buyer request in this exact scope. 50 WrongRequest, 51 /// The request or exact quote no longer admits a fresh decision. 52 Expired, 53 /// Known scoped author time is ahead of the supplied usable observation. 54 ClockAhead, 55 /// An observed buyer withdrawal names the selected authorization. 56 Withdrawn, 57 /// An observed seller decline names the selected request. 58 Declined, 59 /// The exact quoted record has another family or different complete terms. 60 QuoteMismatch, 61 /// Another unexpired unretracted buyer request authorizes incompatible terms. 62 ConflictingAuthorizations, 63 /// Advanced agreement/negotiation state needs its corresponding operation. 64 RequiresReconciliation, 65 } 66 impl fmt::Display for RequestClaimError { 67 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 68 f.write_str(match self { 69 Self::InvalidGraph => "invalid initial request graph", 70 Self::MissingHistory => "initial request history missing", 71 Self::Conflicted => "initial request history conflicted", 72 Self::Uninterpreted => "initial request history uninterpreted", 73 Self::WrongRequest => "wrong initial request", 74 Self::Expired => "initial request deadline expired", 75 Self::ClockAhead => "initial request clock observation insufficient", 76 Self::Withdrawn => "initial request withdrawn", 77 Self::Declined => "initial request declined", 78 Self::QuoteMismatch => "initial request quote differs", 79 Self::ConflictingAuthorizations => "initial request authorizations conflict", 80 Self::RequiresReconciliation => "initial request state needs reconciliation", 81 }) 82 } 83 } 84 impl core::error::Error for RequestClaimError {} 85 fn deadline( 86 request: &TermsRequest, 87 records: &[CausalRecord<'_>], 88 ) -> Result<UnixSeconds, RequestClaimError> { 89 let mut expires = request.expires_at(); 90 if let Some(id) = request.quote() { 91 let quoted = records 92 .iter() 93 .find(|record| record.id == id) 94 .ok_or(RequestClaimError::MissingHistory)?; 95 let CommercePayload::Quote(quote) = quoted.content.payload() else { 96 return Err(RequestClaimError::QuoteMismatch); 97 }; 98 if quote.terms() != request.terms() { 99 return Err(RequestClaimError::QuoteMismatch); 100 } 101 expires = expires.min(quote.expires_at()); 102 } 103 Ok(expires) 104 } 105 106 /// Prepares only an initial quote/request/decline/withdrawal claim. Broader 107 /// negotiation and installed agreement records require their own stateful 108 /// operation; they are preserved, never treated as an empty initial order. 109 /// An original retained operation must be recovered before calling this fresh 110 /// planner. Neither expiry nor withdrawal erases an already admitted operation. 111 pub fn prepare_initial_request<'a>( 112 scope: CausalScope, 113 selected: RecordId, 114 records: &[CausalRecord<'a>], 115 external: &[SourceId], 116 uninterpreted: &[SourceId], 117 observed_at: UnixSeconds, 118 ) -> Result<InitialRequestClaim<'a>, RequestClaimError> { 119 let analysis = analyze(scope, records, external, uninterpreted) 120 .map_err(|_| RequestClaimError::InvalidGraph)?; 121 match analysis.readiness() { 122 CausalReadiness::Complete => {} 123 CausalReadiness::MissingHistory => return Err(RequestClaimError::MissingHistory), 124 CausalReadiness::Conflicted => return Err(RequestClaimError::Conflicted), 125 CausalReadiness::Uninterpreted => return Err(RequestClaimError::Uninterpreted), 126 } 127 let selected_content = records 128 .iter() 129 .find(|record| record.id == selected) 130 .ok_or(RequestClaimError::MissingHistory)? 131 .content; 132 let CommercePayload::Request(request) = selected_content.payload() else { 133 return Err(RequestClaimError::WrongRequest); 134 }; 135 if selected_content.header().order_id() != Some(scope.order) 136 || selected_content.header().author() != scope.buyer 137 { 138 return Err(RequestClaimError::WrongRequest); 139 } 140 if records 141 .iter() 142 .any(|record| record.content.header().created_at() > observed_at) 143 { 144 return Err(RequestClaimError::ClockAhead); 145 } 146 let expires = deadline(request, records)?; 147 if expires <= observed_at { 148 return Err(RequestClaimError::Expired); 149 } 150 let withdrawn = |id| { 151 records.iter().any(|record|matches!(record.content.payload(),CommercePayload::WithdrawalRequested(value) if value.target()==id)) 152 }; 153 let declined = |id| { 154 records.iter().any(|record|matches!(record.content.payload(),CommercePayload::Decline(value) if value.target()==id)) 155 }; 156 if withdrawn(selected) { 157 return Err(RequestClaimError::Withdrawn); 158 } 159 if declined(selected) { 160 return Err(RequestClaimError::Declined); 161 } 162 for record in records { 163 match record.content.payload() { 164 CommercePayload::Request(other) => { 165 let other_expires = deadline(other, records)?; 166 if record.id != selected 167 && other_expires > observed_at 168 && !withdrawn(record.id) 169 && !declined(record.id) 170 && other.terms() != request.terms() 171 { 172 return Err(RequestClaimError::ConflictingAuthorizations); 173 } 174 } 175 CommercePayload::Quote(_) 176 | CommercePayload::Decline(_) 177 | CommercePayload::WithdrawalRequested(_) => {} 178 _ => return Err(RequestClaimError::RequiresReconciliation), 179 } 180 } 181 Ok(InitialRequestClaim { 182 request: selected, 183 terms: request.terms(), 184 expires, 185 quote: request.quote(), 186 }) 187 }