lib

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

recovery.rs (4081B)


      1 //! Same-owner connection recovery confers only original obtained-fact retention.
      2 use super::{Database, OpenMode, State, WorkspaceError, namespace, stored};
      3 use radroots_domain::OperationId;
      4 use radroots_storage::command::{
      5     ArtifactEvidence, DeliveryObservation, DeliveryObservationKind, PublicationArtifact,
      6 };
      7 use radroots_storage::workspace::{AttachmentInput, AttachmentProof, grant::WorkspaceGrant};
      8 use radroots_transport::{sink::DeliveryRequestId, target::TargetFingerprint};
      9 
     10 #[derive(Clone, Eq, PartialEq)]
     11 pub(super) enum CaptureIdentity {
     12     Seal {
     13         operation: OperationId,
     14         digest: [u8; 32],
     15     },
     16     Observation {
     17         work: Box<PublicationArtifact>,
     18         request: DeliveryRequestId,
     19         target: TargetFingerprint,
     20         kind: DeliveryObservationKind,
     21     },
     22 }
     23 impl CaptureIdentity {
     24     pub fn artifact(value: &ArtifactEvidence) -> Self {
     25         Self::Seal {
     26             operation: value.operation_id(),
     27             digest: *value.digest(),
     28         }
     29     }
     30     pub fn observation(value: &DeliveryObservation) -> Self {
     31         Self::Observation {
     32             work: Box::new(value.work()),
     33             request: value.request_id().clone(),
     34             target: value.target_receipt().target().fingerprint().clone(),
     35             kind: value.kind(),
     36         }
     37     }
     38 }
     39 
     40 pub(super) struct Authorization {
     41     grant: WorkspaceGrant,
     42     close_confirmed: bool,
     43 }
     44 impl State {
     45     pub(super) fn capture_confirmed(&self, identity: &CaptureIdentity) {
     46         let matches = self.capture_scope.borrow().as_ref() == Some(identity);
     47         if matches {
     48             self.uncertain.set(false);
     49             self.capture_scope.borrow_mut().take();
     50             self.recovery.borrow_mut().take();
     51         }
     52     }
     53     pub(super) async fn capture_retained(
     54         &self,
     55         input: &AttachmentInput,
     56         proof: &AttachmentProof,
     57     ) -> Result<(WorkspaceGrant, Vec<u8>), WorkspaceError> {
     58         self.active()?;
     59         if input != &self.selected {
     60             return Err(WorkspaceError::Unauthorized);
     61         }
     62         if self.uncertain.get() {
     63             self.reopen_retained(input, proof).await?;
     64         }
     65         self.capture_grant(input, proof).await
     66     }
     67     async fn reopen_retained(
     68         &self,
     69         input: &AttachmentInput,
     70         proof: &AttachmentProof,
     71     ) -> Result<(), WorkspaceError> {
     72         // A live connection must authenticate its actual grant before disposal.
     73         // After confirmed closure, cached original authorization survives a
     74         // failed existing-only reopen. No raw proof is retained in this cache.
     75         if self
     76             .recovery
     77             .borrow()
     78             .as_ref()
     79             .is_none_or(|value| !value.close_confirmed)
     80         {
     81             let (grant, _) = self.capture_grant(input, proof).await?;
     82             *self.recovery.borrow_mut() = Some(Authorization {
     83                 grant,
     84                 close_confirmed: false,
     85             });
     86         }
     87         {
     88             let cached = self.recovery.borrow();
     89             cached
     90                 .as_ref()
     91                 .ok_or(WorkspaceError::RecoveryRequired)?
     92                 .grant
     93                 .authenticate_retained_capture(input, proof)?;
     94         }
     95         let closed = self
     96             .recovery
     97             .borrow()
     98             .as_ref()
     99             .ok_or(WorkspaceError::RecoveryRequired)?
    100             .close_confirmed;
    101         if !closed {
    102             self.database().close().await.map_err(stored)?;
    103             self.recovery
    104                 .borrow_mut()
    105                 .as_mut()
    106                 .ok_or(WorkspaceError::RecoveryRequired)?
    107                 .close_confirmed = true;
    108         }
    109         self.active()?;
    110         let replacement = Database::open(namespace(&self.selected)?, OpenMode::Existing)
    111             .await
    112             .map_err(stored)?;
    113         self.active()?;
    114         *self.db.borrow_mut() = replacement;
    115         self.recovery
    116             .borrow_mut()
    117             .as_mut()
    118             .ok_or(WorkspaceError::RecoveryRequired)?
    119             .close_confirmed = false;
    120         Ok(())
    121     }
    122 }