Skip to main content

ic_backup/workflow/ic_snapshot_upload/data/
mod.rs

1//! Complete finite original data uploads with explicit integration-qualified receipts.
2
3use super::{IcSnapshotUploadExecutionError, upload_snapshot};
4use crate::{
5    model::{
6        artifacts::ArtifactChecksumRecord,
7        attempt_journal::{MutationOutcomeRecord, MutationReceiptRequest},
8        execution_workflow::ExecutionStagePredecessorRecord,
9        ic_mutation::IcMutationAcknowledgement,
10        ic_snapshot_upload::{
11            IcSnapshotDataUploadPlan, IcSnapshotDataUploadPlanningError, IcSnapshotUploadAttempt,
12            IcSnapshotUploadAttemptError, IcSnapshotUploadRequest,
13        },
14    },
15    ops::persistence::{
16        AttemptJournalError, AttemptJournalGuard, DownloadJournalGuard,
17        ExecutionProgressPersistenceError, ExecutionStageCheckpointError, ExecutionStageGuard,
18        ExecutionWorkflowPersistenceError, IcSnapshotUploadArtifactError, read_execution_progress,
19    },
20    policy::ic_snapshot_upload::{IcSnapshotUploadAssociationError, validate_acknowledgement},
21    ports::ic_snapshot_upload::IcSnapshotUploadProvider,
22};
23use thiserror::Error;
24
25/// Upload every exact original data operation, then checkpoint qualified reply evidence.
26///
27/// Prepare the source-derived data plan, allocation predecessor, retained stage and
28/// complete original journals first; hold no attempt guards. Empty plans and any prior
29/// consumption reject before dispatch: this is a fresh stage, never partial upload
30/// resume. Original source and destination/command custody remain integration-owned.
31/// Fresh guarded preparation buffers one payload and rechecks its immutable binding
32/// before existing single-upload reservation, mandatory fresh `admit` and one call.
33///
34/// `qualify` must independently authenticate exact original data-write attribution and
35/// durably retain original request/reply bytes before returning its explicit Applied
36/// receipt under the selected journal lock. An empty reply never supplies a receipt.
37/// Account remote qualification separately before its calls. Record through the sole
38/// attempt owner before the next dependency can dispatch; failures stop immediately.
39/// Admission, submission and qualification are async under their selected journal
40/// guards. Retain each exact reply before awaiting cancellable qualification work.
41/// Cancellation preserves source bytes/references, earlier Applied receipts and
42/// the current pending reservation, denying whole-stage or single-call reentry.
43///
44/// After all exact receipts, release journal locks and checkpoint the full original plan
45/// and ordered canonical request/raw-reply evidence through the existing stage owner.
46/// This is declared stage settlement, not authenticated whole-backend completeness,
47/// same-release load/start admission, terminal proof or fence/source-reference release.
48/// No hidden observation, reissue, refund, default provider or journal is added.
49/// # Errors
50/// Rejects changed originals, consumed attempts, source/preparation failures, spending,
51/// admission/provider/reply/qualification/receipt or checkpoint failure. Bounded returned
52/// replies survive post-reply rejection; spending, recorded receipts and source remain.
53pub async fn upload_snapshot_data<E: std::error::Error + 'static>(
54    stage: &ExecutionStageGuard<'_>,
55    upload: &IcSnapshotDataUploadPlan<'_, '_, '_>,
56    source: &DownloadJournalGuard<'_>,
57    snapshot: &str,
58    provider: &mut impl IcSnapshotUploadProvider,
59    mut admit: impl AsyncFnMut(&IcSnapshotUploadAttempt<'_, '_>) -> Result<(), E>,
60    mut qualify: impl AsyncFnMut(
61        &IcSnapshotUploadAttempt<'_, '_>,
62        &IcMutationAcknowledgement,
63    ) -> Result<MutationReceiptRequest, E>,
64) -> Result<ExecutionStagePredecessorRecord, IcSnapshotDataUploadExecutionError<E>> {
65    upload.validate_binding(stage.binding())?;
66    let plan = upload
67        .plan()
68        .ok_or(IcSnapshotDataUploadPlanningError::NoDataWrites)?;
69    if stage.plan() != plan {
70        return Err(IcSnapshotDataUploadPlanningError::OriginalMismatch.into());
71    }
72    let progress = read_execution_progress(stage.layout()?, &plan.digest())?;
73    if progress.attempts.mutations_used != 0 || progress.attempts.observations_used != 0 {
74        return Err(IcSnapshotDataUploadExecutionError::AlreadyAttempted);
75    }
76    let mut evidence = b"ic-backup/ic-snapshot-data-upload/v1\0".to_vec();
77    evidence.extend_from_slice(plan.digest().hash().as_bytes());
78    for (index, kind) in upload.kinds().iter().enumerate() {
79        let sequence =
80            u64::try_from(index).map_err(|_| IcSnapshotDataUploadPlanningError::CountOverflow)?;
81        let metadata = upload.metadata();
82        let payload = source.prepare_ic_snapshot_upload_data(
83            metadata.source_plan(),
84            snapshot,
85            metadata,
86            upload.destination(),
87            kind.clone(),
88        )?;
89        // The single-call owner rechecks the exact full source/context/request before spending.
90        let acknowledgement =
91            upload_snapshot(stage, sequence, &payload, provider, &mut admit).await?;
92        let digest = record_qualified(stage, sequence, &payload, &acknowledgement, &mut qualify)
93            .await
94            .map_err(|source| IcSnapshotDataUploadExecutionError::AfterReply {
95                operation_sequence: sequence,
96                source,
97                acknowledgement: Box::new(acknowledgement),
98            })?;
99        evidence.extend_from_slice(digest.hash().as_bytes());
100    }
101    let progress = read_execution_progress(stage.layout()?, &plan.digest())?;
102    if progress.applied_operations != upload.kinds().len() {
103        return Err(IcSnapshotDataUploadExecutionError::AlreadyAttempted);
104    }
105    Ok(stage.checkpoint(ArtifactChecksumRecord::from_bytes(&evidence))?)
106}
107
108async fn record_qualified<E: std::error::Error + 'static>(
109    stage: &ExecutionStageGuard<'_>,
110    sequence: u64,
111    payload: &IcSnapshotUploadRequest<'_>,
112    acknowledgement: &IcMutationAcknowledgement,
113    qualify: &mut impl AsyncFnMut(
114        &IcSnapshotUploadAttempt<'_, '_>,
115        &IcMutationAcknowledgement,
116    ) -> Result<MutationReceiptRequest, E>,
117) -> Result<ArtifactChecksumRecord, IcSnapshotDataUploadReplyError<E>> {
118    let plan = stage.plan();
119    let authority = plan
120        .attempt_authority(sequence)
121        .map_err(IcSnapshotUploadAttemptError::from)?;
122    let mut journal = AttemptJournalGuard::open(stage.layout()?, &authority)?;
123    let request = IcSnapshotUploadAttempt::new(plan, sequence, journal.record()?, payload)?;
124    let admitted = validate_acknowledgement(&request, journal.record()?, acknowledgement)?;
125    let receipt = qualify(&request, acknowledgement)
126        .await
127        .map_err(IcSnapshotDataUploadReplyError::Qualification)?;
128    if receipt.outcome != MutationOutcomeRecord::Applied
129        || receipt.attempt != request.mutation_attempt()
130        || receipt.request != payload.binding_digest().hash()
131    {
132        return Err(IcSnapshotDataUploadReplyError::ReceiptRequired);
133    }
134    stage.layout()?;
135    journal.record_mutation(receipt)?;
136    stage.layout()?;
137    Ok(admitted.reply().digest())
138}
139
140/// Data-stage failure preserves source, original spending and returned acknowledgement bytes.
141#[derive(Debug, Error)]
142pub enum IcSnapshotDataUploadExecutionError<E: std::error::Error + 'static> {
143    /// Fresh-only entrypoint rejects any original consumed mutation or observation.
144    #[error("snapshot data upload original stage was already attempted")]
145    AlreadyAttempted,
146    /// Existing exact original data-plan/binding admission.
147    #[error(transparent)]
148    Planning(#[from] IcSnapshotDataUploadPlanningError),
149    /// Existing retained workflow/stage/ancestor admission.
150    #[error(transparent)]
151    Stage(#[from] ExecutionWorkflowPersistenceError),
152    /// Complete original progress admission.
153    #[error(transparent)]
154    Progress(#[from] ExecutionProgressPersistenceError),
155    /// Fresh exact source preparation failed before the selected operation reserves.
156    #[error(transparent)]
157    Source(#[from] IcSnapshotUploadArtifactError),
158    /// One exact original upload failed; its owner retains bounded returned evidence.
159    #[error(transparent)]
160    Upload(#[from] IcSnapshotUploadExecutionError<E>),
161    /// Receipt settlement failed after a bounded returned acknowledgement.
162    #[error("snapshot data upload reply rejected for operation {operation_sequence}: {source}")]
163    AfterReply {
164        /// Exact original failed operation sequence.
165        operation_sequence: u64,
166        /// Original qualification or persistence rejection.
167        source: IcSnapshotDataUploadReplyError<E>,
168        /// Exact bounded reply, without an inferred outcome.
169        acknowledgement: Box<IcMutationAcknowledgement>,
170    },
171    /// Existing all-Applied stage checkpoint failed; receipts and occupied evidence remain.
172    #[error(transparent)]
173    Checkpoint(#[from] ExecutionStageCheckpointError),
174}
175
176/// Post-reply data-write rejection, without a second outcome/accounting owner.
177#[derive(Debug, Error)]
178pub enum IcSnapshotDataUploadReplyError<E: std::error::Error + 'static> {
179    /// Independent attribution/authentication or durable original-byte retention failed.
180    #[error("snapshot data upload qualification failed: {0}")]
181    Qualification(#[source] E),
182    /// Require the explicit exact original Applied receipt.
183    #[error("snapshot data upload requires an explicit original Applied receipt")]
184    ReceiptRequired,
185    /// Original workflow/stage/ancestor admission failed.
186    #[error(transparent)]
187    Stage(#[from] ExecutionWorkflowPersistenceError),
188    /// Existing selected journal/receipt persistence failed.
189    #[error(transparent)]
190    Journal(#[from] AttemptJournalError),
191    /// Existing original payload/current reservation binding failed.
192    #[error(transparent)]
193    Request(#[from] IcSnapshotUploadAttemptError),
194    /// Existing bounded acknowledgement association failed.
195    #[error(transparent)]
196    Association(#[from] IcSnapshotUploadAssociationError),
197}