lib

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

backing.rs (2356B)


      1 //! Fresh issuance checks current backing without freezing unrelated pool changes.
      2 use super::*;
      3 pub(in crate::workspace::stock) struct BackingProof {
      4     id: PoolId,
      5     version: Sequence,
      6     protected_digest: [u8; 32],
      7     ancestry: MinimumProof,
      8 }
      9 pub(in crate::workspace::stock) async fn read(
     10     turn: &mut Turn<'_>,
     11     access: &Access,
     12     pools: &[PoolSnapshot],
     13 ) -> Result<Vec<BackingProof>, OperationError> {
     14     if pools.len() > 96 {
     15         return Err(WorkspaceError::Capacity.into());
     16     }
     17     let mut proofs = Vec::with_capacity(pools.len());
     18     for held in pools {
     19         let current = read_pool(turn.connection, turn.selected, turn.crypto, held.id())
     20             .await?
     21             .ok_or(OperationError::RecoveryRequired)?;
     22         let ancestry = authenticate_projection(
     23             turn.connection,
     24             turn.selected,
     25             turn.crypto,
     26             &access.current,
     27             &access.authority,
     28             held.id(),
     29             Some(current.pool),
     30         )
     31         .await?;
     32         proofs.push(BackingProof {
     33             id: held.id(),
     34             version: current.pool.version(),
     35             protected_digest: Sha256::digest(&current.protected).into(),
     36             ancestry,
     37         });
     38     }
     39     Ok(proofs)
     40 }
     41 impl BackingProof {
     42     pub(in crate::workspace::stock) async fn confirm(
     43         &self,
     44         connection: &mut SqliteConnection,
     45     ) -> Result<(), OperationError> {
     46         self.ancestry.confirm(connection).await?;
     47         let row=sqlx::query("SELECT revision,CASE WHEN length(protected) BETWEEN 1 AND 262144 THEN protected END FROM stock_pools WHERE pool_id=?").bind(self.id.as_bytes().as_slice()).fetch_optional(connection).await.map_err(|_|OperationError::RecoveryRequired)?.ok_or(OperationError::RecoveryRequired)?;
     48         let version: i64 = row
     49             .try_get(0)
     50             .map_err(|_| OperationError::RecoveryRequired)?;
     51         let protected: Vec<u8> = row
     52             .try_get::<Option<Vec<u8>>, _>(1)
     53             .map_err(|_| OperationError::RecoveryRequired)?
     54             .ok_or(OperationError::RecoveryRequired)?;
     55         if version <= 0
     56             || version as u64 != self.version.get()
     57             || <[u8; 32]>::from(Sha256::digest(protected)) != self.protected_digest
     58         {
     59             return Err(OperationError::RecoveryRequired);
     60         }
     61         Ok(())
     62     }
     63 }