acceptance.rs (7258B)
1 //! Shared T0 data and reported effects; concrete stores authenticate and commit. 2 use super::{CommitAttempt, InventoryCheckpointRef, OperationError}; 3 use crate::workspace::{ 4 AttachmentInput, WorkspaceBinding, 5 seller_stock::{StockListingCheckpointRef, StockSourceCheckpointRef}, 6 }; 7 use radroots_domain::{ 8 IssuanceWindow, OrderId, RecordId, TermsDigest, UnixSeconds, stock::PoolSnapshot, 9 }; 10 use radroots_protocol::{ 11 command::{CapturedOrderAcceptance, CapturedStockCommitment, CapturedTermsInstallation}, 12 commerce::EncodedCommerceRumor, 13 }; 14 use std::sync::Arc; 15 16 /// Captured inputs and independently retained minima, never authority evidence. 17 pub struct StockAcceptanceRequest { 18 binding: WorkspaceBinding, 19 minimum: InventoryCheckpointRef, 20 sources: StockSourceCheckpointRef, 21 listings: StockListingCheckpointRef, 22 command: CapturedStockCommitment, 23 } 24 impl StockAcceptanceRequest { 25 /// Copies bounded protected command bytes. Current consent, source policy, 26 /// exact allocation, authority and actual T0 effects belong to the store. 27 pub fn capture( 28 binding: WorkspaceBinding, 29 minimum: InventoryCheckpointRef, 30 sources: StockSourceCheckpointRef, 31 listings: StockListingCheckpointRef, 32 command: &CapturedOrderAcceptance, 33 ) -> Result<Self, OperationError> { 34 Self::capture_wire( 35 binding, 36 minimum, 37 sources, 38 listings, 39 command.protected_wire(), 40 ) 41 } 42 /// Copies exact terms-installation data into the same native commitment 43 /// request. This performs no admission, allocation, signing or stock effect. 44 pub fn capture_terms( 45 binding: WorkspaceBinding, 46 minimum: InventoryCheckpointRef, 47 sources: StockSourceCheckpointRef, 48 listings: StockListingCheckpointRef, 49 command: &CapturedTermsInstallation, 50 ) -> Result<Self, OperationError> { 51 Self::capture_wire( 52 binding, 53 minimum, 54 sources, 55 listings, 56 command.protected_wire(), 57 ) 58 } 59 fn capture_wire( 60 binding: WorkspaceBinding, 61 minimum: InventoryCheckpointRef, 62 sources: StockSourceCheckpointRef, 63 listings: StockListingCheckpointRef, 64 raw: &[u8], 65 ) -> Result<Self, OperationError> { 66 let command = 67 CapturedStockCommitment::parse(raw).map_err(|_| OperationError::InvalidCommand)?; 68 Ok(Self { 69 binding, 70 minimum, 71 sources, 72 listings, 73 command, 74 }) 75 } 76 pub const fn binding(&self) -> &WorkspaceBinding { 77 &self.binding 78 } 79 pub const fn minimum_checkpoint(&self) -> InventoryCheckpointRef { 80 self.minimum 81 } 82 pub const fn source_checkpoint(&self) -> StockSourceCheckpointRef { 83 self.sources 84 } 85 pub const fn listing_checkpoint(&self) -> StockListingCheckpointRef { 86 self.listings 87 } 88 pub const fn command(&self) -> &CapturedStockCommitment { 89 &self.command 90 } 91 /// Original identity anchor; not proof of a confirmed commit. 92 pub fn attempt(&self) -> CommitAttempt { 93 CommitAttempt::new( 94 self.binding.input().workspace_id(), 95 self.command.context().operation_id(), 96 self.command.digest(), 97 None, 98 ) 99 } 100 } 101 impl core::fmt::Debug for StockAcceptanceRequest { 102 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 103 f.write_str("StockAcceptanceRequest(<private command>)") 104 } 105 } 106 107 /// A store's original T0 preparation report, distinct from S or installed T2. 108 /// Constructing or copying this metadata supplies no grant or signing permit. 109 #[derive(Clone)] 110 pub struct StockAcceptanceReceipt { 111 attempt: CommitAttempt, 112 order: OrderId, 113 authorization: RecordId, 114 terms: TermsDigest, 115 checkpoint: InventoryCheckpointRef, 116 pools: Vec<PoolSnapshot>, 117 allocations: usize, 118 window: IssuanceWindow, 119 rumor: Arc<EncodedCommerceRumor>, 120 } 121 impl StockAcceptanceReceipt { 122 /// Checks bounded host report consistency. The actual store must separately 123 /// authenticate protected original inputs/effects and confirm the commit. 124 #[allow(clippy::too_many_arguments)] 125 pub fn from_committed_store( 126 namespace: &AttachmentInput, 127 attempt: CommitAttempt, 128 order: OrderId, 129 authorization: RecordId, 130 terms: TermsDigest, 131 checkpoint: InventoryCheckpointRef, 132 pools: Vec<PoolSnapshot>, 133 allocations: usize, 134 window: IssuanceWindow, 135 rumor: EncodedCommerceRumor, 136 ) -> Result<Self, OperationError> { 137 if attempt.workspace_id() != namespace.workspace_id() 138 || attempt.transaction_id().is_none() 139 || checkpoint.authority() != namespace.authority() 140 || checkpoint.revision().get() == 0 141 || checkpoint.authorization().is_none() 142 || rumor.created_at() != window.prepared_at() 143 || allocations > 64 144 || pools.len() > allocations 145 || pools.iter().any(|pool| pool.version().get() < 2) 146 || pools.windows(2).any(|pair| pair[0].id() >= pair[1].id()) 147 { 148 return Err(OperationError::RecoveryRequired); 149 } 150 Ok(Self { 151 attempt, 152 order, 153 authorization, 154 terms, 155 checkpoint, 156 pools, 157 allocations, 158 window, 159 rumor: Arc::new(rumor), 160 }) 161 } 162 pub const fn attempt(&self) -> CommitAttempt { 163 self.attempt 164 } 165 pub const fn order_id(&self) -> OrderId { 166 self.order 167 } 168 /// Original selected buyer request or terms authorization. This metadata 169 /// alone proves neither participant consent nor a confirmed preparation. 170 pub const fn authorization(&self) -> RecordId { 171 self.authorization 172 } 173 pub const fn terms_digest(&self) -> TermsDigest { 174 self.terms 175 } 176 pub const fn checkpoint(&self) -> InventoryCheckpointRef { 177 self.checkpoint 178 } 179 /// Original after-images in pool identity order, not current free stock. 180 pub fn held_pools(&self) -> &[PoolSnapshot] { 181 &self.pools 182 } 183 /// Transfers reported after-images without duplicating private history. 184 pub fn into_held_pools(self) -> Vec<PoolSnapshot> { 185 self.pools 186 } 187 pub const fn allocation_count(&self) -> usize { 188 self.allocations 189 } 190 pub const fn prepared_at(&self) -> UnixSeconds { 191 self.window.prepared_at() 192 } 193 pub const fn first_issue_deadline(&self) -> UnixSeconds { 194 self.window.first_issue_not_after() 195 } 196 /// Exact unsigned original record; no first-dispatch or replay permission. 197 pub fn rumor(&self) -> &EncodedCommerceRumor { 198 &self.rumor 199 } 200 } 201 impl core::fmt::Debug for StockAcceptanceReceipt { 202 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 203 f.write_str("StockAcceptanceReceipt(<private preparation>)") 204 } 205 } 206 #[derive(Debug)] 207 pub enum StockAcceptanceOutcome { 208 Prepared(Box<StockAcceptanceReceipt>), 209 Aborted(Box<super::StockAbortReceipt>), 210 Unknown(CommitAttempt), 211 }