lib

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

codec.rs (10364B)


      1 use super::*;
      2 
      3 #[derive(Clone, Copy, Eq, PartialEq, Serialize, Deserialize)]
      4 #[serde(rename_all = "snake_case")]
      5 pub(super) enum MovementKind {
      6     Introduced,
      7     Removed,
      8     Wasted,
      9 }
     10 #[derive(Serialize, Deserialize)]
     11 #[serde(deny_unknown_fields)]
     12 pub(super) struct Movement {
     13     pub(super) version: u8,
     14     pub(super) operation: OperationId,
     15     pub(super) digest: CommandDigest,
     16     pub(super) pool: PoolId,
     17     pub(super) quantity: radroots_domain::stock::StockTicks,
     18     pub(super) kind: MovementKind,
     19 }
     20 #[derive(Serialize)]
     21 pub(super) struct OperationWire<'a> {
     22     pub(super) version: u8,
     23     #[serde(skip_serializing_if = "Option::is_none")]
     24     pub(super) before: Option<Option<PoolSnapshot>>,
     25     pub(super) selection: &'a AttachmentInput,
     26     pub(super) scope: WorkspaceScope,
     27     pub(super) principal: &'a str,
     28     pub(super) grant_revision: Sequence,
     29     pub(super) grant_expires_at: UnixSeconds,
     30     pub(super) observed_at: UnixSeconds,
     31     pub(super) minimum_revision: Sequence,
     32     pub(super) minimum_head: Option<CheckpointDigest>,
     33     pub(super) transaction: TransactionId,
     34     pub(super) revision: Sequence,
     35     pub(super) previous_head: Option<CheckpointDigest>,
     36     pub(super) head: CheckpointDigest,
     37     pub(super) command: &'a RawValue,
     38     pub(super) pool: PoolSnapshot,
     39     pub(super) movement: Option<[u8; 32]>,
     40 }
     41 #[derive(Deserialize)]
     42 #[serde(deny_unknown_fields)]
     43 pub(super) struct OperationDecode<'a> {
     44     pub(super) version: u8,
     45     pub(super) selection: AttachmentInput,
     46     pub(super) scope: WorkspaceScope,
     47     pub(super) principal: String,
     48     pub(super) grant_revision: Sequence,
     49     pub(super) grant_expires_at: UnixSeconds,
     50     pub(super) observed_at: UnixSeconds,
     51     pub(super) minimum_revision: Sequence,
     52     #[serde(deserialize_with = "super::required_nullable")]
     53     pub(super) minimum_head: Option<CheckpointDigest>,
     54     pub(super) transaction: TransactionId,
     55     pub(super) revision: Sequence,
     56     #[serde(deserialize_with = "super::required_nullable")]
     57     pub(super) previous_head: Option<CheckpointDigest>,
     58     pub(super) head: CheckpointDigest,
     59     #[serde(borrow)]
     60     pub(super) command: &'a RawValue,
     61     pub(super) pool: PoolSnapshot,
     62     #[serde(deserialize_with = "super::required_nullable")]
     63     pub(super) movement: Option<[u8; 32]>,
     64 }
     65 #[derive(Deserialize)]
     66 #[serde(deny_unknown_fields)]
     67 pub(super) struct OperationDecodeV2<'a> {
     68     pub(super) version: u8,
     69     #[serde(deserialize_with = "super::required_nullable")]
     70     pub(super) before: Option<PoolSnapshot>,
     71     pub(super) selection: AttachmentInput,
     72     pub(super) scope: WorkspaceScope,
     73     pub(super) principal: String,
     74     pub(super) grant_revision: Sequence,
     75     pub(super) grant_expires_at: UnixSeconds,
     76     pub(super) observed_at: UnixSeconds,
     77     pub(super) minimum_revision: Sequence,
     78     #[serde(deserialize_with = "super::required_nullable")]
     79     pub(super) minimum_head: Option<CheckpointDigest>,
     80     pub(super) transaction: TransactionId,
     81     pub(super) revision: Sequence,
     82     #[serde(deserialize_with = "super::required_nullable")]
     83     pub(super) previous_head: Option<CheckpointDigest>,
     84     pub(super) head: CheckpointDigest,
     85     #[serde(borrow)]
     86     pub(super) command: &'a RawValue,
     87     pub(super) pool: PoolSnapshot,
     88     #[serde(deserialize_with = "super::required_nullable")]
     89     pub(super) movement: Option<[u8; 32]>,
     90 }
     91 #[derive(Serialize, Deserialize)]
     92 #[serde(deny_unknown_fields)]
     93 struct OperationV3<'a> {
     94     version: u8,
     95     authorization: StockAuthorizationRef,
     96     #[serde(borrow)]
     97     operation: &'a RawValue,
     98 }
     99 #[derive(Serialize, Deserialize)]
    100 #[serde(deny_unknown_fields)]
    101 struct OperationV4<'a> {
    102     version: u8,
    103     authorization: StockAuthorizationRef,
    104     #[serde(borrow)]
    105     operation: &'a RawValue,
    106     #[serde(deserialize_with = "super::required_nullable")]
    107     mapping: Option<super::mapping::MappingEffect>,
    108 }
    109 #[derive(Serialize, Deserialize)]
    110 #[serde(deny_unknown_fields)]
    111 struct OperationV5<'a> {
    112     version: u8,
    113     authorization: StockAuthorizationRef,
    114     #[serde(borrow)]
    115     operation: &'a RawValue,
    116     #[serde(deserialize_with = "super::required_nullable")]
    117     mapping: Option<super::mapping::MappingEffect>,
    118     losses: Vec<super::deficit::Loss>,
    119 }
    120 pub(super) fn encode_operation(
    121     value: &OperationWire<'_>,
    122     catalog: i64,
    123     authorization: Option<StockAuthorizationRef>,
    124     mapping: Option<super::mapping::MappingEffect>,
    125     losses: &[super::deficit::Loss],
    126 ) -> Result<zeroize::Zeroizing<Vec<u8>>, OperationError> {
    127     let clear = super::encode(value)?;
    128     if catalog < 4 {
    129         return Ok(clear);
    130     }
    131     let raw = RawValue::from_string(
    132         String::from_utf8(clear.to_vec()).map_err(|_| OperationError::StorageFailure)?,
    133     )
    134     .map_err(|_| OperationError::StorageFailure)?;
    135     if catalog >= 19 {
    136         return super::encode(&OperationV5 {
    137             version: 5,
    138             authorization: authorization.ok_or(OperationError::RecoveryRequired)?,
    139             operation: &raw,
    140             mapping,
    141             losses: losses.to_vec(),
    142         });
    143     }
    144     if !losses.is_empty() {
    145         return Err(OperationError::UnsupportedOperation);
    146     }
    147     if matches!(catalog, 5..=18) {
    148         return super::encode(&OperationV4 {
    149             version: 4,
    150             authorization: authorization.ok_or(OperationError::RecoveryRequired)?,
    151             operation: &raw,
    152             mapping,
    153         });
    154     }
    155     super::encode(&OperationV3 {
    156         version: 3,
    157         authorization: authorization.ok_or(OperationError::RecoveryRequired)?,
    158         operation: &raw,
    159     })
    160 }
    161 pub(super) struct OperationData {
    162     pub(super) losses: Vec<super::deficit::Loss>,
    163     pub(super) mapping: Option<super::mapping::MappingEffect>,
    164     pub(super) authorization: Option<StockAuthorizationRef>,
    165     pub(super) version: u8,
    166     pub(super) before: Option<PoolSnapshot>,
    167     pub(super) transaction: TransactionId,
    168     pub(super) revision: Sequence,
    169     pub(super) previous_head: Option<CheckpointDigest>,
    170     pub(super) head: CheckpointDigest,
    171     pub(super) pool: PoolSnapshot,
    172     pub(super) movement: Option<[u8; 32]>,
    173 }
    174 
    175 pub(super) fn decode_operation(
    176     bytes: &[u8],
    177     catalog: i64,
    178 ) -> Result<(InventoryRequest, OperationData), OperationError> {
    179     decode_inner(bytes, catalog, false)
    180 }
    181 fn decode_inner(
    182     bytes: &[u8],
    183     catalog: i64,
    184     allow_mapping: bool,
    185 ) -> Result<(InventoryRequest, OperationData), OperationError> {
    186     if catalog >= 19 {
    187         let decoded: OperationV5<'_> =
    188             super::closed_record(bytes).map_err(|_| OperationError::RecoveryRequired)?;
    189         if decoded.version != 5 || decoded.losses.len() > super::deficit::MAX_LOSSES {
    190             return Err(OperationError::RecoveryRequired);
    191         }
    192         let (request, mut data) = decode_inner(decoded.operation.get().as_bytes(), 3, true)?;
    193         data.authorization = Some(decoded.authorization);
    194         data.mapping = decoded.mapping;
    195         data.losses = decoded.losses;
    196         return Ok((request, data));
    197     }
    198     if matches!(catalog, 5..=18) {
    199         let decoded: OperationV4<'_> =
    200             super::closed_record(bytes).map_err(|_| OperationError::RecoveryRequired)?;
    201         if decoded.version != 4 {
    202             return Err(OperationError::RecoveryRequired);
    203         }
    204         let (request, mut data) = decode_inner(decoded.operation.get().as_bytes(), 3, true)?;
    205         data.authorization = Some(decoded.authorization);
    206         data.mapping = decoded.mapping;
    207         return Ok((request, data));
    208     }
    209     if catalog == 4 {
    210         let decoded: OperationV3<'_> =
    211             super::closed_record(bytes).map_err(|_| OperationError::RecoveryRequired)?;
    212         if decoded.version != 3 {
    213             return Err(OperationError::RecoveryRequired);
    214         }
    215         let (request, mut data) = decode_inner(decoded.operation.get().as_bytes(), 3, false)?;
    216         data.authorization = Some(decoded.authorization);
    217         return Ok((request, data));
    218     }
    219     macro_rules! captured {
    220         ($decoded:expr,$prior:expr) => {{
    221             let decoded = $decoded;
    222             let prior = $prior;
    223             let captured = WorkspaceBinding::from_authenticated_host(
    224                 decoded.selection,
    225                 decoded.scope,
    226                 radroots_storage::inventory::CapturedInventoryCommand::parse(
    227                     decoded.command.get().as_bytes(),
    228                 )
    229                 .map_err(|_| OperationError::RecoveryRequired)?
    230                 .context()
    231                 .actor(),
    232                 decoded.principal,
    233                 decoded.grant_revision,
    234                 decoded.grant_expires_at,
    235                 decoded.observed_at,
    236             )?;
    237             let minimum = radroots_storage::inventory::InventoryCheckpointRef::from_host_metadata(
    238                 captured.input().authority(),
    239                 decoded.minimum_revision,
    240                 decoded.minimum_head,
    241             )?;
    242             let request = InventoryRequest::from_protected_wire(
    243                 captured,
    244                 minimum,
    245                 decoded.command.get().as_bytes(),
    246             )?;
    247             if !allow_mapping && request.command().mapping_input().is_some() {
    248                 return Err(OperationError::RecoveryRequired);
    249             }
    250             Ok((
    251                 request,
    252                 OperationData {
    253                     losses: Vec::new(),
    254                     mapping: None,
    255                     authorization: None,
    256                     version: decoded.version,
    257                     before: prior,
    258                     transaction: decoded.transaction,
    259                     revision: decoded.revision,
    260                     previous_head: decoded.previous_head,
    261                     head: decoded.head,
    262                     pool: decoded.pool,
    263                     movement: decoded.movement,
    264                 },
    265             ))
    266         }};
    267     }
    268     match catalog {
    269         2 => {
    270             let decoded: OperationDecode<'_> =
    271                 super::closed_record(bytes).map_err(|_| OperationError::RecoveryRequired)?;
    272             captured!(decoded, None)
    273         }
    274         3 => {
    275             let decoded: OperationDecodeV2<'_> =
    276                 super::closed_record(bytes).map_err(|_| OperationError::RecoveryRequired)?;
    277             let prior = decoded.before;
    278             captured!(decoded, prior)
    279         }
    280         _ => Err(OperationError::RecoveryRequired),
    281     }
    282 }