Skip to main content

ic_backup/policy/ic_snapshot_upload/
mod.rs

1//! Pure current-reservation and passive upload-reply association; no IO or settlement.
2
3#[cfg(test)]
4mod tests;
5
6use super::response_identity::{ResponseIdentity, ResponseIdentityMismatch};
7use crate::model::{
8    attempt_journal::AttemptJournalRecord,
9    ic_mutation::IcMutationAcknowledgement,
10    ic_snapshot_upload::{
11        IcSnapshotUploadAttempt, IcSnapshotUploadAttemptError, IcSnapshotUploadError,
12        IcSnapshotUploadReply,
13    },
14};
15use thiserror::Error;
16
17/// Read-only retained original acknowledgement and bounded source-bound upload reply.
18#[derive(Debug)]
19pub struct IcSnapshotUploadAcknowledgementView<'request, 'source> {
20    acknowledgement: &'request IcMutationAcknowledgement,
21    reply: IcSnapshotUploadReply<'request, 'source>,
22}
23impl<'request, 'source> IcSnapshotUploadAcknowledgementView<'request, 'source> {
24    /// Read exact claimed context/target/authority/attempt/raw bytes/evidence.
25    #[must_use]
26    pub const fn acknowledgement(&self) -> &'request IcMutationAcknowledgement {
27        self.acknowledgement
28    }
29    /// Read passive bounded metadata ID or data acknowledgement; no Applied receipt.
30    #[must_use]
31    pub const fn reply(&self) -> &IcSnapshotUploadReply<'request, 'source> {
32        &self.reply
33    }
34}
35
36/// Recheck exact original pending upload and passive actual association fields.
37///
38/// Reuses the existing bounded acknowledgement owner, with tighter upload decoding.
39/// Actual authentication, exclusive original allocation/write attribution, freshness,
40/// controller/byte custody and independent per-call accounting remain integration-owned.
41/// Lost replies stay pending. Zero/one/multiple inventory entries or absent data never
42/// establish an outcome. Only separately qualified settled observations can reconcile;
43/// no observation, receipt, retry, refund, completion or release is produced here.
44/// # Errors
45/// Rejects original reservation drift, mismatching actual claims and invalid reply shape.
46pub fn validate_acknowledgement<'request, 'source>(
47    request: &IcSnapshotUploadAttempt<'request, 'source>,
48    journal: &AttemptJournalRecord,
49    acknowledgement: &'request IcMutationAcknowledgement,
50) -> Result<IcSnapshotUploadAcknowledgementView<'request, 'source>, IcSnapshotUploadAssociationError>
51{
52    request.validate_journal(journal)?;
53    let input = acknowledgement.input();
54    ResponseIdentity::from(input)
55        .compare(&ResponseIdentity {
56            authority: &request.authority().digest(),
57            mutation_attempt: request.mutation_attempt(),
58            context: request.plan().context(),
59            target: request.payload().target(),
60        })
61        .map_err(|mismatch| match mismatch {
62            ResponseIdentityMismatch::Authority => {
63                IcSnapshotUploadAssociationError::AuthorityMismatch
64            }
65            ResponseIdentityMismatch::Attempt => IcSnapshotUploadAssociationError::AttemptMismatch,
66            ResponseIdentityMismatch::Context => IcSnapshotUploadAssociationError::ContextMismatch,
67            ResponseIdentityMismatch::Target => IcSnapshotUploadAssociationError::TargetMismatch,
68        })?;
69    let reply = IcSnapshotUploadReply::decode(request.payload(), &input.reply)?;
70    Ok(IcSnapshotUploadAcknowledgementView {
71        acknowledgement,
72        reply,
73    })
74}
75
76/// Typed passive association denial; all original obligations and counters remain retained.
77#[derive(Debug, Error)]
78pub enum IcSnapshotUploadAssociationError {
79    /// Exact current original mutation/reservation no longer matches.
80    #[error(transparent)]
81    Reservation(#[from] IcSnapshotUploadAttemptError),
82    /// Claimed full original authority differs.
83    #[error("snapshot upload acknowledgement authority mismatch")]
84    AuthorityMismatch,
85    /// Claimed already allocated mutation differs.
86    #[error("snapshot upload acknowledgement attempt mismatch")]
87    AttemptMismatch,
88    /// Claimed actual network/caller/release differs.
89    #[error("snapshot upload acknowledgement context mismatch")]
90    ContextMismatch,
91    /// Claimed actual target differs.
92    #[error("snapshot upload acknowledgement target mismatch")]
93    TargetMismatch,
94    /// Canonical bounded upload decoder rejected the raw reply.
95    #[error(transparent)]
96    Reply(#[from] IcSnapshotUploadError),
97}