ic-backup 0.1.6

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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//! Fresh local evidence joins; fixtures attest no IC effects or current authority.

use super::*;
use crate::model::{
    attempt_journal::{
        AttemptJournalRecordError, MutationOutcomeRecord, MutationReceiptRequest,
        ObservationOutcomeRecord, ObservationReceiptRequest,
    },
    effect_graph::{EffectGraphRecord, EffectNodeRecord, EffectNodeRequest},
    operation_plan::{OperationPlanRequest, PlanBudgetRecord, tests::request},
};

fn plan() -> OperationPlanRecord {
    OperationPlanRecord::new(request()).expect("original plan")
}
fn journals(plan: &OperationPlanRecord) -> Vec<AttemptJournalRecord> {
    plan.operations()
        .iter()
        .map(|operation| {
            AttemptJournalRecord::new(
                plan.attempt_authority(operation.operation_sequence())
                    .expect("original authority"),
            )
        })
        .collect()
}
fn project(
    plan: &OperationPlanRecord,
    journals: &[AttemptJournalRecord],
) -> Result<ExecutionProgressView, ExecutionProgressError> {
    let references: Vec<_> = journals.iter().collect();
    progress(&ExecutionProgressRequest {
        plan,
        journals: &references,
    })
}
fn resolve(journal: &mut AttemptJournalRecord, outcome: MutationOutcomeRecord) {
    let attempt = journal.reserve_mutation().expect("local reservation");
    journal
        .record_mutation(MutationReceiptRequest {
            attempt,
            request: journal.authority().binding().request().into(),
            outcome,
            evidence: "12".repeat(32),
        })
        .expect("local caller-qualified fixture receipt");
}

#[test]
fn admits_complete_exact_journals_in_graph_order_without_editing_evidence() {
    let plan = plan();
    let mut journals = journals(&plan);
    journals.reverse();
    let original = serde_json::to_vec(&journals).expect("retained fixture bytes");
    let view = project(&plan, &journals).expect("complete original local evidence");
    assert_eq!(view.intent, plan.digest());
    assert_eq!(view.graph, plan.graph().digest());
    assert_eq!(view.applied_operations, 0);
    assert_eq!(view.operations[0].operation_sequence, 0);
    assert_eq!(view.operations[1].operation_sequence, 7);
    assert_eq!(
        view.operations[0].state,
        OperationProgressState::MutationAvailable
    );
    assert_eq!(
        view.operations[1].state,
        OperationProgressState::AwaitingDependencies
    );
    assert_eq!(view.operations[0].attempts, journals[1].view());
    assert_eq!(view.operations[1].attempts, journals[0].view());
    assert_eq!(
        view.attempts,
        ExecutionAttemptTotalsView {
            mutations_used: 0,
            observations_used: 0,
            mutations_remaining: 3,
            observations_remaining: 2,
        }
    );
    assert_eq!(serde_json::to_vec(&journals).expect("unchanged"), original);
    assert_eq!(project(&plan, &journals).expect("repeat"), view);

    let mut request = request();
    request.budget = PlanBudgetRecord::new(100, 100).expect("unassigned headroom");
    let bigger = OperationPlanRecord::new(request).expect("same assignments");
    let bigger_view = project(&bigger, &self::journals(&bigger)).expect("new exact declarations");
    assert_ne!(bigger_view.intent, view.intent);
    assert_eq!(
        bigger_view.attempts, view.attempts,
        "unused headroom grants no allowance"
    );
}

#[test]
fn rejects_missing_duplicate_unknown_and_every_changed_original_binding() {
    let plan = plan();
    let journals = journals(&plan);
    assert_eq!(
        project(&plan, &[]).expect_err("no implied empty journals"),
        ExecutionProgressError::MissingJournal(0)
    );
    assert_eq!(
        project(&plan, &journals[..1]).expect_err("missing dependent"),
        ExecutionProgressError::MissingJournal(7)
    );
    assert_eq!(
        project(&plan, &[journals[0].clone(), journals[0].clone()]).expect_err("duplicate"),
        ExecutionProgressError::DuplicateJournal(0)
    );
    let references = vec![&journals[0]; MAX_EFFECT_OPERATIONS + 1];
    assert_eq!(
        progress(&ExecutionProgressRequest {
            plan: &plan,
            journals: &references
        })
        .expect_err("bounded input"),
        ExecutionProgressError::TooManyJournals
    );
    for (field, changed) in [
        ("intent", "34".repeat(32)),
        ("network", "56".repeat(32)),
        ("caller", "aaaaa-aa".into()),
        ("target", "aaaaa-aa".into()),
        ("release", "78".repeat(32)),
        ("request", "90".repeat(32)),
    ] {
        let mut value = serde_json::to_value(journals[0].authority()).expect("authority");
        value["binding"][field] = changed.into();
        let changed: AttemptAuthorityRecord =
            serde_json::from_value(value).expect("valid different identity");
        let changed = AttemptJournalRecord::new(changed);
        assert_eq!(
            project(&plan, &[changed, journals[1].clone()]).expect_err(field),
            ExecutionProgressError::AuthorityMismatch(0)
        );
    }
    for kind in ["mutations", "observations"] {
        let mut value = serde_json::to_value(journals[0].authority()).expect("authority");
        value["budget"][kind] = 2.into();
        let changed = AttemptJournalRecord::new(
            serde_json::from_value(value).expect("valid altered allowance"),
        );
        assert_eq!(
            project(&plan, &[changed, journals[1].clone()]).expect_err(kind),
            ExecutionProgressError::AuthorityMismatch(0)
        );
    }
    let mut value = serde_json::to_value(journals[0].authority()).expect("authority");
    value["binding"]["operation_sequence"] = 9.into();
    let changed =
        AttemptJournalRecord::new(serde_json::from_value(value).expect("valid unknown sequence"));
    assert_eq!(
        project(&plan, &[changed, journals[1].clone()]).expect_err("unknown"),
        ExecutionProgressError::UnknownOperation(9)
    );
    let mut value = serde_json::to_value(journals[0].authority()).expect("authority");
    value["binding"]["operation_sequence"] = 7.into();
    let changed =
        AttemptJournalRecord::new(serde_json::from_value(value).expect("known wrong operation"));
    assert_eq!(
        project(&plan, &[journals[0].clone(), changed]).expect_err("sequence/request mismatch"),
        ExecutionProgressError::AuthorityMismatch(7)
    );
}

#[test]
fn rejects_any_attempted_dependent_without_applied_prerequisites() {
    let plan = plan();
    let original = journals(&plan);
    for outcome in [
        None,
        Some(MutationOutcomeRecord::NotApplied),
        Some(MutationOutcomeRecord::Applied),
    ] {
        let mut journals = original.clone();
        if let Some(outcome) = outcome {
            resolve(&mut journals[1], outcome);
        } else {
            journals[1]
                .reserve_mutation()
                .expect("pending local reservation");
        }
        let before = serde_json::to_vec(&journals).expect("evidence");
        assert_eq!(
            project(&plan, &journals).expect_err("premature attempt"),
            ExecutionProgressError::PrematureAttempt {
                operation_sequence: 7,
                prerequisite: 0
            }
        );
        assert_eq!(
            serde_json::to_vec(&journals).expect("unchanged evidence"),
            before
        );
    }
    let mut journals = original;
    resolve(&mut journals[0], MutationOutcomeRecord::NotApplied);
    journals[1]
        .reserve_mutation()
        .expect("dependent reservation");
    assert_eq!(
        project(&plan, &journals).expect_err("NotApplied prerequisite"),
        ExecutionProgressError::PrematureAttempt {
            operation_sequence: 7,
            prerequisite: 0
        }
    );
}

#[test]
fn projects_pending_observations_and_exhaustion_without_blind_retry() {
    let plan = plan();
    let mut journals = journals(&plan);
    let mutation = journals[0]
        .reserve_mutation()
        .expect("consume original allowance");
    let view = project(&plan, &journals).expect("unresolved mutation");
    assert_eq!(
        view.operations[0].state,
        OperationProgressState::MutationUnresolved
    );
    assert_eq!(view.operations[0].attempts.pending_mutation, Some(mutation));
    assert_eq!(view.operations[0].attempts.mutations_remaining, 0);
    let observation_request = "23".repeat(32);
    let observation = journals[0]
        .reserve_observation(mutation, &observation_request)
        .expect("separate original observation allowance");
    let view = project(&plan, &journals).expect("lost observation response stays pending");
    assert_eq!(
        view.operations[0].state,
        OperationProgressState::ObservationUnresolved
    );
    assert_eq!(
        view.operations[0].attempts.pending_observation,
        Some(observation)
    );
    assert_eq!(view.operations[0].attempts.observations_remaining, 0);
    journals[0]
        .record_observation(ObservationReceiptRequest {
            attempt: observation,
            request: observation_request,
            outcome: ObservationOutcomeRecord::Uncertain,
            evidence: "45".repeat(32),
        })
        .expect("qualified settled unresolved fixture");
    let view = project(&plan, &journals).expect("unresolved exhausted accounting");
    assert_eq!(
        view.operations[0].state,
        OperationProgressState::ReconciliationExhausted
    );
    assert_eq!(view.operations[0].attempts.pending_mutation, Some(mutation));
    assert_eq!(view.operations[0].attempts.pending_observation, None);
    assert_eq!(view.attempts.mutations_used, 1);
    assert_eq!(view.attempts.observations_used, 1);
    assert_eq!(view.attempts.mutations_remaining, 2);
    assert_eq!(view.attempts.observations_remaining, 1);
    assert!(
        matches!(journals[0].reserve_mutation(), Err(AttemptJournalRecordError::MutationPending { attempt }) if attempt == mutation)
    );
    let restored: Vec<AttemptJournalRecord> =
        serde_json::from_slice(&serde_json::to_vec(&journals).expect("evidence bytes"))
            .expect("local replay");
    assert_eq!(
        project(&plan, &restored).expect("original reopened accounting"),
        view
    );
}

#[test]
fn derives_applied_and_available_progress_from_actual_local_journal_receipts() {
    let plan = plan();
    let mut journals = journals(&plan);
    resolve(&mut journals[0], MutationOutcomeRecord::Applied);
    let view = project(&plan, &journals).expect("applied prerequisite");
    assert_eq!(view.applied_operations, 1);
    assert_eq!(view.operations[0].state, OperationProgressState::Applied);
    assert_eq!(
        view.operations[1].state,
        OperationProgressState::MutationAvailable
    );
    resolve(&mut journals[1], MutationOutcomeRecord::NotApplied);
    let view = project(&plan, &journals).expect("spent not-applied attempt");
    assert_eq!(
        view.operations[1].state,
        OperationProgressState::MutationAvailable
    );
    assert_eq!(view.operations[1].attempts.mutations_remaining, 1);
    resolve(&mut journals[1], MutationOutcomeRecord::Applied);
    let view = project(&plan, &journals).expect("all operation receipts applied");
    assert_eq!(view.applied_operations, 2);
    assert!(
        view.operations
            .iter()
            .all(|op| op.state == OperationProgressState::Applied)
    );
    assert_eq!(view.attempts.mutations_used, 3);
    assert_eq!(view.attempts.mutations_remaining, 0);
    assert_eq!(
        view.attempts.observations_remaining, 2,
        "unused original allowance is not a dispatch permit"
    );
    assert!(matches!(
        journals[1].reserve_mutation(),
        Err(AttemptJournalRecordError::AlreadyApplied)
    ));
}

#[test]
fn distinguishes_settled_not_applied_from_unresolved_without_replenishing_limits() {
    let plan = plan();
    for outcome in [
        ObservationOutcomeRecord::Applied,
        ObservationOutcomeRecord::NotApplied,
    ] {
        let mut journals = journals(&plan);
        let mutation = journals[0].reserve_mutation().expect("mutation");
        let request = "67".repeat(32);
        let observation = journals[0]
            .reserve_observation(mutation, &request)
            .expect("observation");
        journals[0]
            .record_observation(ObservationReceiptRequest {
                attempt: observation,
                request,
                outcome,
                evidence: "89".repeat(32),
            })
            .expect("qualified fixture settlement");
        let view = project(&plan, &journals).expect("settled exact observation");
        assert_eq!(view.operations[0].attempts.pending_mutation, None);
        assert_eq!(view.operations[0].attempts.pending_observation, None);
        assert_eq!(view.operations[0].attempts.mutations_remaining, 0);
        assert_eq!(view.operations[0].attempts.observations_remaining, 0);
        let expected = if outcome == ObservationOutcomeRecord::Applied {
            OperationProgressState::Applied
        } else {
            OperationProgressState::MutationExhausted
        };
        assert_eq!(view.operations[0].state, expected);
    }
}

#[test]
fn preserves_explicit_reverse_dependencies_and_zero_allowance_states() {
    let mut request = request();
    request.graph = EffectGraphRecord::new(vec![
        EffectNodeRecord::new(EffectNodeRequest {
            operation_sequence: 0,
            depends_on: vec![7],
        })
        .expect("explicit later identity dependency"),
        EffectNodeRecord::new(EffectNodeRequest {
            operation_sequence: 7,
            depends_on: vec![],
        })
        .expect("first prerequisite"),
    ])
    .expect("acyclic reverse numeric order");
    let plan = OperationPlanRecord::new(request).expect("exact reverse declared plan");
    let mut journals = journals(&plan);
    let view = project(&plan, &journals).expect("graph order");
    assert_eq!(view.operations[0].operation_sequence, 7);
    assert_eq!(view.operations[1].operation_sequence, 0);
    assert_eq!(
        view.operations[1].state,
        OperationProgressState::AwaitingDependencies
    );
    resolve(&mut journals[1], MutationOutcomeRecord::Applied);
    assert_eq!(
        project(&plan, &journals)
            .expect("reverse prerequisite applied")
            .operations[1]
            .state,
        OperationProgressState::MutationAvailable
    );

    let mut request: OperationPlanRequest = self::request();
    request.operations = request
        .operations
        .into_iter()
        .map(|op| {
            let mut value = serde_json::to_value(op).expect("fixture operation");
            value["budget"]["mutations"] = 0.into();
            value["budget"]["observations"] = 0.into();
            serde_json::from_value(value).expect("zero original allowance")
        })
        .collect();
    request.budget = PlanBudgetRecord::new(0, 0).expect("zero aggregate");
    let plan = OperationPlanRecord::new(request).expect("zero declaration");
    let view = project(&plan, &self::journals(&plan)).expect("no fake completion for zero budget");
    assert_eq!(
        view.operations[0].state,
        OperationProgressState::MutationExhausted
    );
    assert_eq!(
        view.operations[1].state,
        OperationProgressState::AwaitingDependencies
    );
    assert_eq!(view.applied_operations, 0);
}

#[test]
fn admits_maximum_journal_count_and_exact_original_aggregate_allowance() {
    use crate::model::attempt_journal::{AttemptBudgetRecord, OperationBindingRequest};
    for (count, mutations, ceiling) in [(MAX_EFFECT_OPERATIONS, 0, 0), (64, 1024, 65536)] {
        let mut request = request();
        let template = serde_json::to_value(&request.operations[0]).expect("operation fixture");
        let mut nodes = Vec::with_capacity(count);
        let mut operations = Vec::with_capacity(count);
        for index in 0..count {
            let sequence = u64::try_from(index).expect("bounded operation count");
            nodes.push(
                EffectNodeRecord::new(EffectNodeRequest {
                    operation_sequence: sequence,
                    depends_on: vec![],
                })
                .expect("independent node"),
            );
            let mut operation = template.clone();
            operation["operation_sequence"] = sequence.into();
            operation["budget"]["mutations"] = mutations.into();
            operation["budget"]["observations"] = 0.into();
            operations.push(serde_json::from_value(operation).expect("exact operation"));
        }
        request.graph = EffectGraphRecord::new(nodes).expect("bounded graph");
        request.operations = operations;
        request.budget = PlanBudgetRecord::new(ceiling, 0).expect("aggregate limit");
        let plan = OperationPlanRecord::new(request).expect("original declaration");
        let intent = plan.digest();
        // Build exact validated fixture authorities using one original intent hash,
        // avoiding repeated full-plan hashing in the maximum-count test setup.
        let journals: Vec<_> = plan
            .operations()
            .iter()
            .rev()
            .map(|operation| {
                let binding = OperationBindingRecord::new(&OperationBindingRequest {
                    intent: intent.hash().into(),
                    operation_sequence: operation.operation_sequence(),
                    network: plan.context().network().into(),
                    caller: plan.context().caller().into(),
                    target: operation.target().into(),
                    release: plan.context().release().into(),
                    request: operation.request().into(),
                })
                .expect("exact original binding");
                AttemptJournalRecord::new(AttemptAuthorityRecord::new(
                    binding,
                    AttemptBudgetRecord::new(mutations, 0).expect("original limits"),
                ))
            })
            .collect();
        let view = project(&plan, &journals).expect("maximum exact coverage");
        assert_eq!(view.operations.len(), count);
        assert_eq!(view.operations[0].operation_sequence, 0);
        assert_eq!(
            view.operations
                .last()
                .expect("last operation")
                .operation_sequence,
            u64::try_from(count - 1).expect("sequence")
        );
        assert_eq!(view.attempts.mutations_remaining, ceiling);
        assert_eq!(view.attempts.mutations_used, 0);
        assert_eq!(view.attempts.observations_used, 0);
        assert_eq!(view.attempts.observations_remaining, 0);
        assert_eq!(view.applied_operations, 0);
    }
}