lib

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

address.rs (5034B)


      1 //! Closed v2 private record positions under one opaque operation prefix.
      2 use super::{RecordKey, State};
      3 use crate::{ReadSnapshot, RecordPrefix};
      4 use radroots_domain::OperationId;
      5 use radroots_storage::command::OperationError;
      6 use sha2::{Digest, Sha256};
      7 
      8 pub(super) const PREPARED: u8 = 1;
      9 pub(super) const PLAN: u8 = 2;
     10 pub(super) const CLAIM: u8 = 3;
     11 pub(super) const EVIDENCE: u8 = 4;
     12 pub(super) const INSTALLATION: u8 = 5;
     13 pub(super) const PUBLICATION: u8 = 6;
     14 pub(super) const DELIVERY: u8 = 7;
     15 pub(super) const OBSERVATION: u8 = 8;
     16 pub(super) const REQUEST_INDEX: u8 = 9;
     17 pub(super) const LISTING_INSTALLATION: u8 = 10;
     18 pub(super) const PUBLIC_WORK: u8 = 11;
     19 
     20 fn prefix_bytes(operation: OperationId) -> [u8; 16] {
     21     let digest = Sha256::new()
     22         .chain_update(b"radroots.browser.local_outbox.operation.prefix.v2\0")
     23         .chain_update(operation.as_bytes())
     24         .finalize();
     25     let mut bytes = [0; 16];
     26     bytes.copy_from_slice(&digest[..16]);
     27     bytes
     28 }
     29 pub(super) fn prefix(operation: OperationId) -> RecordPrefix {
     30     RecordPrefix::new(prefix_bytes(operation))
     31 }
     32 pub(super) fn key(operation: OperationId, kind: u8, recipient: u8, part: u8) -> RecordKey {
     33     let mut bytes = [0; 32];
     34     bytes[..16].copy_from_slice(&prefix_bytes(operation));
     35     bytes[16] = kind;
     36     bytes[17] = recipient;
     37     bytes[20] = part;
     38     RecordKey::new(bytes)
     39 }
     40 pub(super) fn fields(key: RecordKey) -> Result<(u8, u8, u8), OperationError> {
     41     let bytes = key.as_bytes();
     42     let kind = bytes[16];
     43     let recipient = bytes[17];
     44     let part = bytes[20];
     45     let delivery = matches!(kind, DELIVERY | OBSERVATION | REQUEST_INDEX);
     46     let ordinal = u16::from_be_bytes([bytes[18], bytes[19]]);
     47     if (if kind == OBSERVATION {
     48         bytes[23..].iter().any(|b| *b != 0) || bytes[21] >= 16 || !(1..=6).contains(&bytes[22])
     49     } else {
     50         bytes[21..].iter().any(|b| *b != 0)
     51     }) || if delivery {
     52         !(1..=192).contains(&ordinal)
     53     } else {
     54         ordinal != 0
     55     } || match kind {
     56         PREPARED => recipient != 0 || part > 4,
     57         PLAN => recipient != 0 || part > 24,
     58         CLAIM => recipient > 2 || part != 0,
     59         EVIDENCE => recipient > 2 || part > 4,
     60         INSTALLATION => recipient != 0 || part != 0,
     61         PUBLICATION => recipient > 1 || part != 0,
     62         LISTING_INSTALLATION | PUBLIC_WORK => recipient != 0 || part != 0,
     63         DELIVERY => recipient > 2 || part > 24,
     64         OBSERVATION => recipient > 2 || part != 0,
     65         REQUEST_INDEX => recipient > 2 || part != 0,
     66         // Later reserved families remain unsupported until their actual
     67         // implementations and proving cohort land. Never ignore them.
     68         _ => true,
     69     } {
     70         return Err(OperationError::RecoveryRequired);
     71     }
     72     Ok((kind, recipient, part))
     73 }
     74 pub(super) fn delivery_key(
     75     operation: OperationId,
     76     kind: u8,
     77     recipient: u8,
     78     ordinal: u16,
     79     part: u8,
     80 ) -> RecordKey {
     81     let mut bytes = *key(operation, kind, recipient, part).as_bytes();
     82     bytes[18..20].copy_from_slice(&ordinal.to_be_bytes());
     83     RecordKey::new(bytes)
     84 }
     85 pub(super) fn delivery_position(key: RecordKey) -> Result<(u8, u8, u16, u8), OperationError> {
     86     let (kind, recipient, part) = fields(key)?;
     87     if !matches!(kind, DELIVERY | OBSERVATION | REQUEST_INDEX) {
     88         return Err(OperationError::RecoveryRequired);
     89     }
     90     let bytes = key.as_bytes();
     91     Ok((
     92         kind,
     93         recipient,
     94         u16::from_be_bytes([bytes[18], bytes[19]]),
     95         part,
     96     ))
     97 }
     98 pub(super) fn observation_key(
     99     operation: OperationId,
    100     recipient: u8,
    101     ordinal: u16,
    102     target: u8,
    103     kind: u8,
    104 ) -> RecordKey {
    105     let mut bytes = *delivery_key(operation, OBSERVATION, recipient, ordinal, 0).as_bytes();
    106     bytes[21] = target;
    107     bytes[22] = kind;
    108     RecordKey::new(bytes)
    109 }
    110 pub(super) fn observation_position(key: RecordKey) -> Result<(u8, u16, u8, u8), OperationError> {
    111     let (family, recipient, ordinal, _) = delivery_position(key)?;
    112     if family != OBSERVATION {
    113         return Err(OperationError::RecoveryRequired);
    114     }
    115     Ok((recipient, ordinal, key.as_bytes()[21], key.as_bytes()[22]))
    116 }
    117 impl State {
    118     pub(super) async fn read_view(
    119         &self,
    120         operation: OperationId,
    121     ) -> Result<ReadSnapshot, OperationError> {
    122         let view = self
    123             .database()
    124             .read_snapshot(prefix(operation))
    125             .await
    126             .map_err(super::journal::storage_error)?;
    127         for key in view.keys() {
    128             fields(key)?;
    129         }
    130         Ok(view)
    131     }
    132     pub(super) async fn read_view_key(
    133         &self,
    134         view: &ReadSnapshot,
    135         key: RecordKey,
    136     ) -> Result<Option<Vec<u8>>, OperationError> {
    137         if !view.contains(key) {
    138             return Ok(None);
    139         }
    140         let value = self
    141             .database()
    142             .get(key)
    143             .await
    144             .map_err(super::journal::storage_error)?;
    145         view.verify(key, value.as_deref())
    146             .map_err(super::journal::storage_error)?;
    147         Ok(value)
    148     }
    149 }