lib

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

projection.rs (5643B)


      1 //! Exact body-derived indexes; projections cannot supply dispatch permission.
      2 use super::*;
      3 
      4 #[derive(Clone, Copy)]
      5 pub(in crate::workspace::stock) enum Projection {
      6     Attempt {
      7         operation: OperationId,
      8         recipient: PublicKey,
      9         request: [u8; 32],
     10     },
     11     Observation {
     12         operation: OperationId,
     13         recipient: PublicKey,
     14         request: [u8; 32],
     15         target: [u8; 32],
     16         kind: u8,
     17     },
     18 }
     19 impl Projection {
     20     pub(in crate::workspace::stock) fn digest(
     21         self,
     22         actor: PublicKey,
     23         entry: OperationId,
     24     ) -> [u8; 32] {
     25         let mut hash = Sha256::new();
     26         hash.update(b"radroots.seller_stock.delivery_projection.v1\0");
     27         hash.update(actor.as_bytes());
     28         hash.update(entry.as_bytes());
     29         let (operation, recipient, request) = match self {
     30             Self::Attempt {
     31                 operation,
     32                 recipient,
     33                 request,
     34             } => {
     35                 hash.update([11]);
     36                 (operation, recipient, request)
     37             }
     38             Self::Observation {
     39                 operation,
     40                 recipient,
     41                 request,
     42                 target,
     43                 kind,
     44             } => {
     45                 hash.update([12]);
     46                 hash.update(target);
     47                 hash.update([kind]);
     48                 (operation, recipient, request)
     49             }
     50         };
     51         hash.update(operation.as_bytes());
     52         hash.update(recipient.as_bytes());
     53         hash.update(request);
     54         hash.finalize().into()
     55     }
     56     pub(super) async fn insert(
     57         self,
     58         connection: &mut SqliteConnection,
     59         actor: PublicKey,
     60         entry: OperationId,
     61     ) -> Result<(), OperationError> {
     62         match self {
     63             Self::Attempt {
     64                 operation,
     65                 recipient,
     66                 request,
     67             } => {
     68                 sqlx::query("INSERT INTO stock_delivery_attempts(actor,operation_id,recipient,request_key,entry_id) VALUES(?,?,?,?,?)")
     69                     .bind(actor.as_bytes().as_slice()).bind(operation.as_bytes().as_slice())
     70                     .bind(recipient.as_bytes().as_slice()).bind(request.as_slice())
     71                     .bind(entry.as_bytes().as_slice()).execute(connection).await
     72                     .map_err(|_|OperationError::StorageFailure)?;
     73             }
     74             Self::Observation {
     75                 operation,
     76                 recipient,
     77                 request,
     78                 target,
     79                 kind,
     80             } => {
     81                 sqlx::query("INSERT INTO stock_delivery_observations(actor,operation_id,recipient,request_key,target_key,kind,entry_id) VALUES(?,?,?,?,?,?,?)")
     82                     .bind(actor.as_bytes().as_slice()).bind(operation.as_bytes().as_slice())
     83                     .bind(recipient.as_bytes().as_slice()).bind(request.as_slice()).bind(target.as_slice())
     84                     .bind(i64::from(kind)).bind(entry.as_bytes().as_slice()).execute(connection).await
     85                     .map_err(|_|OperationError::StorageFailure)?;
     86             }
     87         }
     88         Ok(())
     89     }
     90     pub(super) async fn confirm(
     91         self,
     92         connection: &mut SqliteConnection,
     93         actor: PublicKey,
     94         entry: OperationId,
     95     ) -> Result<(), OperationError> {
     96         let family = if matches!(self, Self::Attempt { .. }) {
     97             11
     98         } else {
     99             12
    100         };
    101         if raw_projection(connection, actor.as_bytes(), entry.as_bytes(), family).await?
    102             != self.digest(actor, entry)
    103         {
    104             return Err(OperationError::RecoveryRequired);
    105         }
    106         Ok(())
    107     }
    108 }
    109 pub(in crate::workspace::stock) async fn raw_projection(
    110     connection: &mut SqliteConnection,
    111     actor: &[u8; 32],
    112     entry: &[u8; 32],
    113     family: i64,
    114 ) -> Result<[u8; 32], OperationError> {
    115     let sql = match family {
    116         11 => {
    117             "SELECT operation_id,recipient,request_key,NULL,NULL FROM stock_delivery_attempts WHERE actor=? AND entry_id=?"
    118         }
    119         12 => {
    120             "SELECT operation_id,recipient,request_key,target_key,kind FROM stock_delivery_observations WHERE actor=? AND entry_id=?"
    121         }
    122         _ => return Err(OperationError::RecoveryRequired),
    123     };
    124     let row = sqlx::query(sql)
    125         .bind(actor.as_slice())
    126         .bind(entry.as_slice())
    127         .fetch_optional(connection)
    128         .await
    129         .map_err(|_| OperationError::RecoveryRequired)?
    130         .ok_or(OperationError::RecoveryRequired)?;
    131     let field = |index| {
    132         row.try_get::<Vec<u8>, _>(index)
    133             .map_err(|_| OperationError::RecoveryRequired)?
    134             .try_into()
    135             .map_err(|_| OperationError::RecoveryRequired)
    136     };
    137     let operation = OperationId::from_bytes(field(0)?);
    138     let recipient =
    139         PublicKey::from_bytes(field(1)?).map_err(|_| OperationError::RecoveryRequired)?;
    140     let request = field(2)?;
    141     let projection = if family == 11 {
    142         Projection::Attempt {
    143             operation,
    144             recipient,
    145             request,
    146         }
    147     } else {
    148         let kind: i64 = row
    149             .try_get(4)
    150             .map_err(|_| OperationError::RecoveryRequired)?;
    151         if !(1..=6).contains(&kind) {
    152             return Err(OperationError::RecoveryRequired);
    153         }
    154         Projection::Observation {
    155             operation,
    156             recipient,
    157             request,
    158             target: field(3)?,
    159             kind: kind as u8,
    160         }
    161     };
    162     Ok(projection.digest(
    163         PublicKey::from_bytes(*actor).map_err(|_| OperationError::RecoveryRequired)?,
    164         OperationId::from_bytes(*entry),
    165     ))
    166 }