ic-backup 0.3.5

Host-side snapshot backup and same-release recovery for Internet Computer canisters
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//! Exact local artifact identity and model-owned download lifecycle transitions.

mod view;
pub use view::{DownloadArtifactView, DownloadJournalView, ResumeAction};

use super::artifacts::{ArtifactChecksumRecord, ChecksumError};
use serde::{Deserialize, Deserializer, Serialize, de};
use std::{collections::BTreeSet, fmt};
use thiserror::Error;

/// Maximum artifact entries in a local download journal.
pub const MAX_DOWNLOAD_ARTIFACTS: usize = 1024;
/// Maximum encoded journal bytes admitted by persistence operations.
pub const MAX_DOWNLOAD_JOURNAL_BYTES: u64 = 1024 * 1024;
/// Maximum ASCII bytes in an opaque backend snapshot identifier.
pub const MAX_SNAPSHOT_ID_BYTES: usize = 256;

/// Snapshot identity admitted by the caller after authoritative capture observation.
///
/// This passive request grants no remote authority and contains no signing material.
#[derive(Clone, Debug)]
pub struct DownloadArtifactRequest {
    /// Exact source canister principal, normalized on admission.
    pub canister_id: String,
    /// Exact backend snapshot token; no case folding or inferred identity.
    pub snapshot_id: String,
    /// Observed snapshot timestamp in nanoseconds.
    pub snapshot_taken_at_timestamp: u64,
    /// Observed snapshot size, not the encoded artifact directory's byte length.
    pub snapshot_total_size_bytes: u64,
}

/// Ordered local artifact lifecycle adapted from Canic's download journal.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub enum ArtifactStateRecord {
    /// Exact snapshot identity retained before local download completion.
    Created,
    /// Caller attested a complete staged backend artifact.
    Downloaded,
    /// Local bytes verified and their canonical checksum retained.
    ChecksumVerified,
    /// Verified bytes durably published at the canonical location.
    Durable,
}

impl ArtifactStateRecord {
    const fn can_advance_to(self, next: Self) -> bool {
        matches!(
            (self, next),
            (Self::Created, Self::Downloaded)
                | (Self::Downloaded, Self::ChecksumVerified)
                | (Self::ChecksumVerified, Self::Durable)
        )
    }
}

/// Immutable exact snapshot identity with validated local progress evidence.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(try_from = "ArtifactFields")]
pub struct DownloadArtifactRecord {
    canister_id: String,
    snapshot_id: String,
    snapshot_taken_at_timestamp: u64,
    snapshot_total_size_bytes: u64,
    staging_path: String,
    artifact_path: String,
    state: ArtifactStateRecord,
    checksum: Option<ArtifactChecksumRecord>,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct ArtifactFields {
    canister_id: String,
    snapshot_id: String,
    snapshot_taken_at_timestamp: u64,
    snapshot_total_size_bytes: u64,
    staging_path: String,
    artifact_path: String,
    state: ArtifactStateRecord,
    #[serde(deserialize_with = "required_checksum")]
    checksum: Option<ArtifactChecksumRecord>,
}

fn required_checksum<'de, D: Deserializer<'de>>(
    deserializer: D,
) -> Result<Option<ArtifactChecksumRecord>, D::Error> {
    Option::deserialize(deserializer)
}

impl TryFrom<ArtifactFields> for DownloadArtifactRecord {
    type Error = DownloadJournalRecordError;
    fn try_from(fields: ArtifactFields) -> Result<Self, Self::Error> {
        let mut record = Self::new(DownloadArtifactRequest {
            canister_id: fields.canister_id,
            snapshot_id: fields.snapshot_id,
            snapshot_taken_at_timestamp: fields.snapshot_taken_at_timestamp,
            snapshot_total_size_bytes: fields.snapshot_total_size_bytes,
        })?;
        // Persisted paths must agree with normalized identity, never with a label.
        if fields.staging_path != record.staging_path
            || fields.artifact_path != record.artifact_path
        {
            return Err(DownloadJournalRecordError::ArtifactPathMismatch);
        }
        let requires_checksum = matches!(
            fields.state,
            ArtifactStateRecord::ChecksumVerified | ArtifactStateRecord::Durable
        );
        if requires_checksum != fields.checksum.is_some() {
            return Err(DownloadJournalRecordError::InvalidChecksumState(
                fields.state,
            ));
        }
        record.state = fields.state;
        record.checksum = fields.checksum;
        Ok(record)
    }
}

impl DownloadArtifactRecord {
    fn new(request: DownloadArtifactRequest) -> Result<Self, DownloadJournalRecordError> {
        let canister_id = normalize_principal(&request.canister_id)?;
        if request.snapshot_id.is_empty()
            || request.snapshot_id.len() > MAX_SNAPSHOT_ID_BYTES
            || !request
                .snapshot_id
                .bytes()
                .all(|byte| byte.is_ascii_graphic())
        {
            return Err(DownloadJournalRecordError::InvalidSnapshotId);
        }
        Ok(Self {
            staging_path: format!("artifacts/{canister_id}.tmp"),
            artifact_path: format!("artifacts/{canister_id}"),
            canister_id,
            snapshot_id: request.snapshot_id,
            snapshot_taken_at_timestamp: request.snapshot_taken_at_timestamp,
            snapshot_total_size_bytes: request.snapshot_total_size_bytes,
            state: ArtifactStateRecord::Created,
            checksum: None,
        })
    }

    /// Return the normalized exact source principal.
    #[must_use]
    pub fn canister_id(&self) -> &str {
        &self.canister_id
    }
    /// Return the exact backend snapshot token.
    #[must_use]
    pub fn snapshot_id(&self) -> &str {
        &self.snapshot_id
    }
    /// Return the observed snapshot timestamp in nanoseconds.
    #[must_use]
    pub const fn snapshot_taken_at_timestamp(&self) -> u64 {
        self.snapshot_taken_at_timestamp
    }
    /// Return the observed snapshot size; encoded directory size may differ.
    #[must_use]
    pub const fn snapshot_total_size_bytes(&self) -> u64 {
        self.snapshot_total_size_bytes
    }
    /// Return the fixed relative staging directory for this principal.
    #[must_use]
    pub fn staging_path(&self) -> &str {
        &self.staging_path
    }
    /// Return the fixed relative canonical directory for this principal.
    #[must_use]
    pub fn artifact_path(&self) -> &str {
        &self.artifact_path
    }
    /// Return retained local lifecycle state.
    #[must_use]
    pub const fn state(&self) -> ArtifactStateRecord {
        self.state
    }
    /// Return the retained exact checksum when verification has completed.
    #[must_use]
    pub const fn checksum(&self) -> Option<&ArtifactChecksumRecord> {
        self.checksum.as_ref()
    }
}

/// Maintained v1 local artifact journal bound to a caller-supplied intent digest.
///
/// This record neither authenticates that intent nor proves current remote authority.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(try_from = "JournalFields")]
pub struct DownloadJournalRecord {
    version: u16,
    intent: String,
    artifacts: Vec<DownloadArtifactRecord>,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct JournalFields {
    version: u16,
    intent: String,
    #[serde(deserialize_with = "read_bounded_artifacts")]
    artifacts: Vec<DownloadArtifactRecord>,
}

impl TryFrom<JournalFields> for DownloadJournalRecord {
    type Error = DownloadJournalRecordError;
    fn try_from(mut fields: JournalFields) -> Result<Self, Self::Error> {
        if fields.version != 1 {
            return Err(DownloadJournalRecordError::UnsupportedVersion(
                fields.version,
            ));
        }
        validate_entries(&fields.artifacts)?;
        fields
            .artifacts
            .sort_by(|left, right| left.canister_id.cmp(&right.canister_id));
        Ok(Self {
            version: 1,
            intent: ArtifactChecksumRecord::from_hash(&fields.intent)?
                .hash()
                .to_owned(),
            artifacts: fields.artifacts,
        })
    }
}

impl DownloadJournalRecord {
    /// Admit a nonempty exact physical set before any local transfer completion.
    ///
    /// # Errors
    /// Rejects invalid identities/digests, duplicate principals and exceeded count bounds.
    pub fn new(
        intent: &str,
        requests: Vec<DownloadArtifactRequest>,
    ) -> Result<Self, DownloadJournalRecordError> {
        if requests.len() > MAX_DOWNLOAD_ARTIFACTS {
            return Err(DownloadJournalRecordError::TooManyArtifacts);
        }
        let artifacts = requests
            .into_iter()
            .map(DownloadArtifactRecord::new)
            .collect::<Result<Vec<_>, _>>()?;
        JournalFields {
            version: 1,
            intent: intent.to_owned(),
            artifacts,
        }
        .try_into()
    }

    /// Return the canonical caller-supplied immutable intent digest.
    #[must_use]
    pub fn intent(&self) -> &str {
        &self.intent
    }
    /// Read entries in canonical principal-text order.
    #[must_use]
    pub fn artifacts(&self) -> &[DownloadArtifactRecord] {
        &self.artifacts
    }

    /// Hash canonical original intent, exact snapshot metadata, paths and local evidence.
    ///
    /// The NUL-terminated v1 domain precedes 64 ASCII intent bytes and a big-endian
    /// u64 entry count. Each canonical entry has four u32-length-prefixed UTF-8
    /// strings (principal, snapshot token, staging path, artifact path), two u64
    /// metadata values, a state byte (Created=0 through Durable=3), then a checksum
    /// presence byte and its 64 ASCII hash when present. Integers are big-endian.
    /// This grants no transfer, snapshot authenticity or terminal proof.
    #[must_use]
    #[expect(
        clippy::cast_possible_truncation,
        reason = "record admission bounds principals, snapshot tokens and identity-derived paths below u32"
    )]
    pub fn digest(&self) -> ArtifactChecksumRecord {
        let mut bytes = b"ic-backup/download-journal/v1\0".to_vec();
        bytes.extend_from_slice(self.intent.as_bytes());
        bytes.extend_from_slice(&(self.artifacts.len() as u64).to_be_bytes());
        for entry in &self.artifacts {
            for value in [
                entry.canister_id(),
                entry.snapshot_id(),
                entry.staging_path(),
                entry.artifact_path(),
            ] {
                bytes.extend_from_slice(&(value.len() as u32).to_be_bytes());
                bytes.extend_from_slice(value.as_bytes());
            }
            bytes.extend_from_slice(&entry.snapshot_taken_at_timestamp.to_be_bytes());
            bytes.extend_from_slice(&entry.snapshot_total_size_bytes.to_be_bytes());
            bytes.push(match entry.state {
                ArtifactStateRecord::Created => 0,
                ArtifactStateRecord::Downloaded => 1,
                ArtifactStateRecord::ChecksumVerified => 2,
                ArtifactStateRecord::Durable => 3,
            });
            bytes.push(u8::from(entry.checksum.is_some()));
            if let Some(checksum) = &entry.checksum {
                bytes.extend_from_slice(checksum.hash().as_bytes());
            }
        }
        ArtifactChecksumRecord::from_bytes(&bytes)
    }

    pub(crate) fn artifact(
        &self,
        canister: &str,
        snapshot: &str,
    ) -> Result<&DownloadArtifactRecord, DownloadJournalRecordError> {
        let canister = normalize_principal(canister)?;
        let entry = self
            .artifacts
            .iter()
            .find(|entry| entry.canister_id == canister)
            .ok_or(DownloadJournalRecordError::UnknownArtifact)?;
        if entry.snapshot_id != snapshot {
            return Err(DownloadJournalRecordError::SnapshotMismatch);
        }
        Ok(entry)
    }

    pub(crate) fn advance(
        &mut self,
        canister: &str,
        snapshot: &str,
        next: ArtifactStateRecord,
        checksum: Option<ArtifactChecksumRecord>,
    ) -> Result<(), DownloadJournalRecordError> {
        let entry = self.artifact(canister, snapshot)?;
        let from = entry.state;
        if !from.can_advance_to(next) {
            return Err(DownloadJournalRecordError::InvalidStateTransition { from, to: next });
        }
        let checksum = match next {
            ArtifactStateRecord::Downloaded if checksum.is_none() => None,
            ArtifactStateRecord::ChecksumVerified if checksum.is_some() => checksum,
            ArtifactStateRecord::Durable if checksum.is_none() => entry.checksum.clone(),
            _ => return Err(DownloadJournalRecordError::InvalidChecksumState(next)),
        };
        let canister = entry.canister_id.clone();
        let entry = self
            .artifacts
            .iter_mut()
            .find(|entry| entry.canister_id == canister)
            .ok_or(DownloadJournalRecordError::UnknownArtifact)?;
        entry.state = next;
        entry.checksum = checksum;
        Ok(())
    }
}

fn normalize_principal(value: &str) -> Result<String, DownloadJournalRecordError> {
    super::principal::canonical_text(value).ok_or(DownloadJournalRecordError::InvalidPrincipal)
}

fn validate_entries(entries: &[DownloadArtifactRecord]) -> Result<(), DownloadJournalRecordError> {
    if entries.is_empty() {
        return Err(DownloadJournalRecordError::EmptyArtifacts);
    }
    if entries.len() > MAX_DOWNLOAD_ARTIFACTS {
        return Err(DownloadJournalRecordError::TooManyArtifacts);
    }
    let mut identities = BTreeSet::new();
    for entry in entries {
        if !identities.insert(entry.canister_id()) {
            return Err(DownloadJournalRecordError::DuplicateCanister);
        }
    }
    Ok(())
}

fn read_bounded_artifacts<'de, D: Deserializer<'de>>(
    deserializer: D,
) -> Result<Vec<DownloadArtifactRecord>, D::Error> {
    struct ArtifactsVisitor;
    impl<'de> de::Visitor<'de> for ArtifactsVisitor {
        type Value = Vec<DownloadArtifactRecord>;
        fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
            formatter.write_str("a bounded artifact list")
        }
        fn visit_seq<A: de::SeqAccess<'de>>(
            self,
            mut sequence: A,
        ) -> Result<Self::Value, A::Error> {
            let mut entries = Vec::new();
            while entries.len() < MAX_DOWNLOAD_ARTIFACTS {
                match sequence.next_element()? {
                    Some(entry) => entries.push(entry),
                    None => return Ok(entries),
                }
            }
            if sequence.next_element::<de::IgnoredAny>()?.is_some() {
                return Err(de::Error::custom(
                    DownloadJournalRecordError::TooManyArtifacts,
                ));
            }
            Ok(entries)
        }
    }
    deserializer.deserialize_seq(ArtifactsVisitor)
}

/// Typed local journal identity, schema or transition rejection.
#[derive(Debug, Error)]
pub enum DownloadJournalRecordError {
    /// Only v1 records are maintained.
    #[error("unsupported download journal version {0}")]
    UnsupportedVersion(u16),
    /// An empty physical set cannot claim completion.
    #[error("download journal artifacts must not be empty")]
    EmptyArtifacts,
    /// The bounded artifact count was exceeded.
    #[error("download journal artifact count exceeds {MAX_DOWNLOAD_ARTIFACTS}")]
    TooManyArtifacts,
    /// More than one snapshot was selected for the same physical canister.
    #[error("duplicate download canister identity")]
    DuplicateCanister,
    /// The supplied principal failed canonical admission.
    #[error("invalid download canister principal")]
    InvalidPrincipal,
    /// The exact snapshot token is empty, excessive or contains nongraphic bytes.
    #[error("invalid download snapshot token")]
    InvalidSnapshotId,
    /// A persisted path differs from its immutable identity-derived location.
    #[error("download artifact path does not match source identity")]
    ArtifactPathMismatch,
    /// The requested physical target is not retained.
    #[error("unknown download artifact")]
    UnknownArtifact,
    /// The supplied snapshot differs from the retained exact token.
    #[error("download snapshot identity mismatch")]
    SnapshotMismatch,
    /// Progress cannot be reset, repeated or advanced over an intermediate state.
    #[error("invalid download state transition from {from:?} to {to:?}")]
    InvalidStateTransition {
        /// Retained state.
        from: ArtifactStateRecord,
        /// Rejected next state.
        to: ArtifactStateRecord,
    },
    /// Checksum evidence disagrees with lifecycle state.
    #[error("invalid checksum evidence for download state {0:?}")]
    InvalidChecksumState(ArtifactStateRecord),
    /// The immutable intent or checksum is malformed.
    #[error(transparent)]
    Checksum(#[from] ChecksumError),
}

#[cfg(test)]
mod tests;