lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

inspection.rs (3922B)


      1 //! One currently authenticated pool snapshot, with no signer or store effect.
      2 use super::*;
      3 use radroots_domain::stock::{ObligationBacking, StockTicks};
      4 use radroots_storage::inventory::StockSnapshot;
      5 
      6 impl Turn<'_> {
      7     pub async fn inspect(
      8         &mut self,
      9         binding: WorkspaceBinding,
     10         minimum: radroots_storage::inventory::InventoryCheckpointRef,
     11         id: PoolId,
     12         proof: AttachmentProof,
     13     ) -> Result<Option<StockSnapshot>, OperationError> {
     14         let mut access = self.access(&binding, minimum, &proof).await?;
     15         if access.authority.catalog < 19 {
     16             return Err(OperationError::UnsupportedOperation);
     17         }
     18         access.record.require(StockAction::ReadPools, id)?;
     19         access.record.require(StockAction::ReadOperations, id)?;
     20         let retained = read_pool(self.connection, self.selected, self.crypto, id).await?;
     21         access.projection_proof = Some(
     22             authenticate_projection(
     23                 self.connection,
     24                 self.selected,
     25                 self.crypto,
     26                 &access.current,
     27                 &access.authority,
     28                 id,
     29                 retained.as_ref().map(|value| value.pool),
     30             )
     31             .await?,
     32         );
     33         let snapshot = if let Some(pool) = &retained {
     34             let current = access
     35                 .authority
     36                 .inventory
     37                 .ok_or(OperationError::RecoveryRequired)?;
     38             let before_revision = current
     39                 .revision
     40                 .get()
     41                 .checked_add(1)
     42                 .ok_or(WorkspaceError::Capacity)?;
     43             let mut cursor = super::allocations::InstalledCursor::new(id, before_revision)?;
     44             let mut remaining = StockTicks::ZERO;
     45             let mut backed = StockTicks::ZERO;
     46             let mut deficit = StockTicks::ZERO;
     47             while let Some(allocation) =
     48                 Box::pin(cursor.next(self.connection, self.selected, self.crypto)).await?
     49             {
     50                 let value = allocation
     51                     .backing(self.connection, self.selected, self.crypto, before_revision)
     52                     .await?;
     53                 remaining = remaining
     54                     .checked_add(value.remaining())
     55                     .map_err(|_| OperationError::RecoveryRequired)?;
     56                 backed = backed
     57                     .checked_add(value.backed())
     58                     .map_err(|_| OperationError::RecoveryRequired)?;
     59                 deficit = deficit
     60                     .checked_add(value.deficit())
     61                     .map_err(|_| OperationError::RecoveryRequired)?;
     62             }
     63             // Current original-draw transitions must reconcile to actual
     64             // physical committed backing, independently of historical receipts.
     65             if backed != pool.pool.balances().physical().committed_hold
     66                 || pool.pool.balances().physical().physical_quarantine != StockTicks::ZERO
     67             {
     68                 return Err(OperationError::RecoveryRequired);
     69             }
     70             let checkpoint =
     71                 radroots_storage::inventory::InventoryCheckpointRef::from_host_metadata(
     72                     self.selected.authority(),
     73                     current.revision,
     74                     current.head,
     75                 )?
     76                 .with_optional_authorization(access.authority.authorization);
     77             Some(StockSnapshot::from_host_metadata(
     78                 pool.view.clone(),
     79                 ObligationBacking::new(remaining, backed, deficit)
     80                     .map_err(|_| OperationError::RecoveryRequired)?,
     81                 checkpoint,
     82             )?)
     83         } else {
     84             None
     85         };
     86         self.confirm(
     87             &access,
     88             &proof,
     89             None,
     90             retained.as_ref(),
     91             None,
     92             retained.is_none().then_some(id),
     93         )
     94         .await?;
     95         Ok(snapshot)
     96     }
     97 }