lib

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

allocations.rs (5145B)


      1 //! Bounded traversal of actual original T0/T2 allocations, shared by loss
      2 //! planning and current inspection. SQL pointers never establish the claim.
      3 use super::*;
      4 use radroots_domain::stock::{ObligationBacking, StockAllocation};
      5 
      6 pub(super) struct InstalledAllocation {
      7     pub acceptance: OperationId,
      8     pub installation: OperationId,
      9     pub allocation: StockAllocation,
     10 }
     11 impl InstalledAllocation {
     12     pub async fn backing(
     13         &self,
     14         connection: &mut SqliteConnection,
     15         input: &AttachmentInput,
     16         crypto: &WorkspaceCrypto,
     17         before_revision: u64,
     18     ) -> Result<ObligationBacking, OperationError> {
     19         let catalog: i64 = sqlx::query_scalar("PRAGMA user_version")
     20             .fetch_one(&mut *connection)
     21             .await
     22             .map_err(|_| OperationError::RecoveryRequired)?;
     23         if catalog >= 20 {
     24             return super::changes::current(connection, input, crypto, self, before_revision).await;
     25         }
     26         let prior = super::deficit::prior_quantity(
     27             connection,
     28             input,
     29             crypto,
     30             self.acceptance,
     31             self.installation,
     32             self.allocation.line_id(),
     33             self.allocation.pool_id(),
     34             before_revision,
     35         )
     36         .await?;
     37         let backed = self
     38             .allocation
     39             .ticks()
     40             .checked_sub(prior)
     41             .map_err(|_| OperationError::RecoveryRequired)?;
     42         ObligationBacking::new(self.allocation.ticks(), backed, prior)
     43             .map_err(|_| OperationError::RecoveryRequired)
     44     }
     45 }
     46 
     47 pub(super) struct InstalledCursor {
     48     pool: PoolId,
     49     before_revision: u64,
     50     revision: u64,
     51     acceptance: Option<OperationId>,
     52     installation: Option<OperationId>,
     53     pending: std::vec::IntoIter<StockAllocation>,
     54 }
     55 impl InstalledCursor {
     56     pub fn new(pool: PoolId, before_revision: u64) -> Result<Self, OperationError> {
     57         if before_revision == 0 || before_revision > i64::MAX as u64 {
     58             return Err(WorkspaceError::Capacity.into());
     59         }
     60         Ok(Self {
     61             pool,
     62             before_revision,
     63             revision: 0,
     64             acceptance: None,
     65             installation: None,
     66             pending: Vec::new().into_iter(),
     67         })
     68     }
     69     pub async fn next(
     70         &mut self,
     71         connection: &mut SqliteConnection,
     72         input: &AttachmentInput,
     73         crypto: &WorkspaceCrypto,
     74     ) -> Result<Option<InstalledAllocation>, OperationError> {
     75         loop {
     76             if let Some(allocation) = self.pending.next() {
     77                 if allocation.pool_id() != self.pool {
     78                     continue;
     79                 }
     80                 return Ok(Some(InstalledAllocation {
     81                     acceptance: self.acceptance.ok_or(OperationError::RecoveryRequired)?,
     82                     installation: self.installation.ok_or(OperationError::RecoveryRequired)?,
     83                     allocation,
     84                 }));
     85             }
     86             let row = sqlx::query("SELECT revision,CASE WHEN length(operation_id)=32 THEN operation_id END FROM stock_operations WHERE family=6 AND revision>? AND revision<? ORDER BY revision LIMIT 1")
     87                 .bind(self.revision as i64).bind(self.before_revision as i64)
     88                 .fetch_optional(&mut *connection).await.map_err(|_| OperationError::RecoveryRequired)?;
     89             let Some(row) = row else {
     90                 return Ok(None);
     91             };
     92             let revision: i64 = row
     93                 .try_get(0)
     94                 .map_err(|_| OperationError::RecoveryRequired)?;
     95             let entry: Vec<u8> = row
     96                 .try_get::<Option<Vec<u8>>, _>(1)
     97                 .map_err(|_| OperationError::RecoveryRequired)?
     98                 .ok_or(OperationError::RecoveryRequired)?;
     99             if revision <= 0 {
    100                 return Err(OperationError::RecoveryRequired);
    101             }
    102             self.revision = revision as u64;
    103             let entry = OperationId::from_bytes(
    104                 entry
    105                     .try_into()
    106                     .map_err(|_| OperationError::RecoveryRequired)?,
    107             );
    108             let installed = Box::pin(super::installation::read_entry(
    109                 connection, input, crypto, entry,
    110             ))
    111             .await?
    112             .ok_or(OperationError::RecoveryRequired)?;
    113             if installed.frame.header.revision.get() != self.revision
    114                 || installed.frame.header.actor.participant() != input.participant()
    115             {
    116                 return Err(OperationError::RecoveryRequired);
    117             }
    118             let acceptance = installed
    119                 .frame
    120                 .header
    121                 .related_operation
    122                 .ok_or(OperationError::RecoveryRequired)?;
    123             let original = Box::pin(super::acceptance::codec::read(
    124                 connection, input, crypto, acceptance,
    125             ))
    126             .await?
    127             .ok_or(OperationError::RecoveryRequired)?;
    128             self.pending = original.original_allocations()?.into_iter();
    129             self.acceptance = Some(acceptance);
    130             self.installation = Some(entry);
    131         }
    132     }
    133 }