canic-core 0.62.5

Canic — a canister orchestration and management toolkit for the Internet Computer
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
#![allow(dead_code)]
// Slice B extracts the shared receipt API before root and domain commands are
// migrated onto it.

use crate::{
    ops::{
        replay::model::{
            ExternalEffectDescriptor, OperationId, REPLAY_PAYLOAD_HASH_SCHEMA_VERSION,
            REPLAY_RECEIPT_SCHEMA_VERSION, ReplayActor, ReplayReceipt, ReplayReceiptStatus,
            ReplayTerminalErrorCode, bounded_terminal_error_bytes,
        },
        storage::replay::ReplayReceiptOps,
    },
    storage::stable::replay::{ReplayReceiptRecord, ReplayReceiptSlotKey},
};

use super::model::{CommandKind, RecoveryReason};

pub const MAX_PENDING_REPLAY_RECEIPTS_PER_ACTOR: usize = 64;
pub const MAX_PENDING_REPLAY_RECEIPTS_PER_COMMAND_KIND: usize = 512;

///
/// ReplayReceiptReserveInput
///
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ReplayReceiptReserveInput {
    pub command_kind: CommandKind,
    pub operation_id: OperationId,
    pub actor: ReplayActor,
    pub payload_hash_schema_version: u32,
    pub payload_hash: [u8; 32],
    pub now_ns: u64,
    pub expires_at_ns: Option<u64>,
}

impl ReplayReceiptReserveInput {
    #[must_use]
    pub const fn new(
        command_kind: CommandKind,
        operation_id: OperationId,
        actor: ReplayActor,
        payload_hash: [u8; 32],
        now_ns: u64,
    ) -> Self {
        Self {
            command_kind,
            operation_id,
            actor,
            payload_hash_schema_version: REPLAY_PAYLOAD_HASH_SCHEMA_VERSION,
            payload_hash,
            now_ns,
            expires_at_ns: None,
        }
    }

    #[must_use]
    pub const fn with_expires_at_ns(mut self, expires_at_ns: u64) -> Self {
        self.expires_at_ns = Some(expires_at_ns);
        self
    }
}

///
/// ReplayReceiptToken
///
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ReplayReceiptToken {
    key: ReplayReceiptSlotKey,
    receipt: ReplayReceipt,
}

impl ReplayReceiptToken {
    #[must_use]
    pub const fn key(&self) -> ReplayReceiptSlotKey {
        self.key
    }

    #[must_use]
    pub const fn receipt(&self) -> &ReplayReceipt {
        &self.receipt
    }
}

///
/// ReplayReceiptDecision
///
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ReplayReceiptDecision {
    Fresh(ReplayReceiptToken),
    ReturnCommitted(ReplayReceipt),
    OperationInProgress,
    ActorMismatch,
    PayloadMismatch,
    Expired,
    RecoveryRequired(RecoveryReason),
    TerminalFailed {
        error_code: ReplayTerminalErrorCode,
        error_bytes: Vec<u8>,
        error_bytes_truncated: bool,
    },
    PendingActorQuotaExceeded {
        actor: ReplayActor,
        max_pending: usize,
    },
    PendingCommandQuotaExceeded {
        command_kind: CommandKind,
        max_pending: usize,
    },
}

///
/// ReplayReceiptStoreError
///
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ReplayReceiptStoreError {
    ReceiptDecodeFailed(String),
}

pub fn reserve_or_replay_receipt(
    input: ReplayReceiptReserveInput,
) -> Result<ReplayReceiptDecision, ReplayReceiptStoreError> {
    reserve_or_replay_receipt_with_limits(
        input,
        MAX_PENDING_REPLAY_RECEIPTS_PER_ACTOR,
        MAX_PENDING_REPLAY_RECEIPTS_PER_COMMAND_KIND,
    )
}

fn reserve_or_replay_receipt_with_limits(
    input: ReplayReceiptReserveInput,
    max_pending_per_actor: usize,
    max_pending_per_command_kind: usize,
) -> Result<ReplayReceiptDecision, ReplayReceiptStoreError> {
    let decision = prepare_replay_receipt(input)?;
    if let ReplayReceiptDecision::Fresh(token) = &decision {
        if let Some(quota_decision) = pending_receipt_quota_decision(
            token.receipt(),
            max_pending_per_actor,
            max_pending_per_command_kind,
        ) {
            return Ok(quota_decision);
        }
        reserve_receipt_token(token);
    }
    Ok(decision)
}

pub fn prepare_replay_receipt(
    input: ReplayReceiptReserveInput,
) -> Result<ReplayReceiptDecision, ReplayReceiptStoreError> {
    let key = ReplayReceiptOps::slot_key(&input.command_kind, input.operation_id);
    let Some(existing) = ReplayReceiptOps::get(key) else {
        let receipt = ReplayReceipt {
            schema_version: REPLAY_RECEIPT_SCHEMA_VERSION,
            command_kind: input.command_kind,
            operation_id: input.operation_id,
            actor: input.actor,
            payload_hash_schema_version: input.payload_hash_schema_version,
            payload_hash: input.payload_hash,
            status: ReplayReceiptStatus::Reserved,
            created_at_ns: input.now_ns,
            updated_at_ns: input.now_ns,
            expires_at_ns: input.expires_at_ns,
            response_schema_version: None,
            response_bytes: None,
            effect: None,
        };
        return Ok(ReplayReceiptDecision::Fresh(ReplayReceiptToken {
            key,
            receipt,
        }));
    };

    let existing = existing
        .into_receipt()
        .map_err(ReplayReceiptStoreError::ReceiptDecodeFailed)?;
    Ok(classify_existing_receipt(&input, existing))
}

pub fn reserve_receipt_token(token: &ReplayReceiptToken) {
    ReplayReceiptOps::upsert(
        token.key,
        ReplayReceiptRecord::from_receipt(token.receipt.clone()),
    );
}

fn pending_receipt_quota_decision(
    receipt: &ReplayReceipt,
    max_pending_per_actor: usize,
    max_pending_per_command_kind: usize,
) -> Option<ReplayReceiptDecision> {
    if ReplayReceiptOps::pending_len_for_actor(receipt.actor, receipt.created_at_ns)
        >= max_pending_per_actor
    {
        return Some(ReplayReceiptDecision::PendingActorQuotaExceeded {
            actor: receipt.actor,
            max_pending: max_pending_per_actor,
        });
    }

    if ReplayReceiptOps::pending_len_for_command_kind(&receipt.command_kind, receipt.created_at_ns)
        >= max_pending_per_command_kind
    {
        return Some(ReplayReceiptDecision::PendingCommandQuotaExceeded {
            command_kind: receipt.command_kind.clone(),
            max_pending: max_pending_per_command_kind,
        });
    }

    None
}

pub fn mark_external_effect_in_flight(
    token: &ReplayReceiptToken,
    effect: ExternalEffectDescriptor,
    now_ns: u64,
) {
    let mut receipt = token.receipt.clone();
    receipt.status = ReplayReceiptStatus::ExternalEffectInFlight;
    receipt.effect = Some(effect);
    receipt.updated_at_ns = now_ns;
    ReplayReceiptOps::upsert(token.key, ReplayReceiptRecord::from_receipt(receipt));
}

pub fn commit_receipt_response(
    token: &ReplayReceiptToken,
    response_schema_version: u32,
    response_bytes: Vec<u8>,
    now_ns: u64,
) {
    let mut receipt = latest_receipt_for_token(token);
    receipt.status = ReplayReceiptStatus::Committed;
    receipt.response_schema_version = Some(response_schema_version);
    receipt.response_bytes = Some(response_bytes);
    receipt.updated_at_ns = now_ns;
    ReplayReceiptOps::upsert(token.key, ReplayReceiptRecord::from_receipt(receipt));
}

pub fn commit_terminal_failure(
    token: &ReplayReceiptToken,
    error_code: ReplayTerminalErrorCode,
    error_bytes: &[u8],
    now_ns: u64,
) {
    let bounded = bounded_terminal_error_bytes(error_bytes);
    let mut receipt = latest_receipt_for_token(token);
    receipt.status = ReplayReceiptStatus::TerminalFailed {
        error_code,
        error_bytes: bounded.bytes,
        error_bytes_truncated: bounded.truncated,
    };
    receipt.updated_at_ns = now_ns;
    ReplayReceiptOps::upsert(token.key, ReplayReceiptRecord::from_receipt(receipt));
}

pub fn mark_recovery_required(token: &ReplayReceiptToken, reason: RecoveryReason, now_ns: u64) {
    let mut receipt = latest_receipt_for_token(token);
    receipt.status = ReplayReceiptStatus::RecoveryRequired { reason };
    receipt.updated_at_ns = now_ns;
    ReplayReceiptOps::upsert(token.key, ReplayReceiptRecord::from_receipt(receipt));
}

pub fn abort_reserved_receipt(token: &ReplayReceiptToken) {
    let Some(receipt) =
        ReplayReceiptOps::get(token.key).and_then(|record| record.into_receipt().ok())
    else {
        return;
    };
    if receipt.status == ReplayReceiptStatus::Reserved {
        let _ = ReplayReceiptOps::remove(token.key);
    }
}

pub fn abort_uncommitted_receipt(token: &ReplayReceiptToken) {
    let Some(receipt) =
        ReplayReceiptOps::get(token.key).and_then(|record| record.into_receipt().ok())
    else {
        return;
    };
    if matches!(
        receipt.status,
        ReplayReceiptStatus::Reserved | ReplayReceiptStatus::ExternalEffectInFlight
    ) {
        let _ = ReplayReceiptOps::remove(token.key);
    }
}

fn latest_receipt_for_token(token: &ReplayReceiptToken) -> ReplayReceipt {
    ReplayReceiptOps::get(token.key)
        .and_then(|record| record.into_receipt().ok())
        .unwrap_or_else(|| token.receipt.clone())
}

fn classify_existing_receipt(
    input: &ReplayReceiptReserveInput,
    existing: ReplayReceipt,
) -> ReplayReceiptDecision {
    if existing
        .expires_at_ns
        .is_some_and(|expires_at_ns| input.now_ns >= expires_at_ns)
    {
        return ReplayReceiptDecision::Expired;
    }

    if existing.actor != input.actor {
        return ReplayReceiptDecision::ActorMismatch;
    }
    if existing.payload_hash_schema_version != input.payload_hash_schema_version
        || existing.payload_hash != input.payload_hash
    {
        return ReplayReceiptDecision::PayloadMismatch;
    }

    match existing.status {
        ReplayReceiptStatus::Reserved | ReplayReceiptStatus::ExternalEffectInFlight => {
            ReplayReceiptDecision::OperationInProgress
        }
        ReplayReceiptStatus::Committed => ReplayReceiptDecision::ReturnCommitted(existing),
        ReplayReceiptStatus::TerminalFailed {
            error_code,
            error_bytes,
            error_bytes_truncated,
        } => ReplayReceiptDecision::TerminalFailed {
            error_code,
            error_bytes,
            error_bytes_truncated,
        },
        ReplayReceiptStatus::RecoveryRequired { reason } => {
            ReplayReceiptDecision::RecoveryRequired(reason)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{cdk::types::Principal, ops::storage::replay::ReplayReceiptOps};

    fn p(id: u8) -> Principal {
        Principal::from_slice(&[id; 29])
    }

    fn input() -> ReplayReceiptReserveInput {
        input_with("test.command.v1", p(1), [7; 32], 100)
    }

    fn input_with(
        command_kind: &str,
        actor: Principal,
        operation_id: [u8; 32],
        now_ns: u64,
    ) -> ReplayReceiptReserveInput {
        ReplayReceiptReserveInput::new(
            CommandKind::new(command_kind).expect("command"),
            OperationId::from_bytes(operation_id),
            ReplayActor::direct_caller(actor),
            [9; 32],
            now_ns,
        )
    }

    #[test]
    fn reserve_or_replay_receipt_reserves_new_receipt() {
        ReplayReceiptOps::reset_for_tests();

        let decision = reserve_or_replay_receipt(input()).expect("decision");
        let ReplayReceiptDecision::Fresh(token) = decision else {
            panic!("expected fresh reservation");
        };

        assert_eq!(ReplayReceiptOps::len(), 1);
        assert_eq!(token.receipt.status, ReplayReceiptStatus::Reserved);
    }

    #[test]
    fn reserve_or_replay_receipt_returns_committed_response_for_duplicate() {
        ReplayReceiptOps::reset_for_tests();

        let token = match reserve_or_replay_receipt(input()).expect("reserve") {
            ReplayReceiptDecision::Fresh(token) => token,
            other => panic!("expected fresh, got {other:?}"),
        };
        commit_receipt_response(&token, 1, vec![1, 2, 3], 200);

        let duplicate = reserve_or_replay_receipt(input()).expect("duplicate");
        let ReplayReceiptDecision::ReturnCommitted(receipt) = duplicate else {
            panic!("expected committed replay");
        };

        assert_eq!(receipt.response_schema_version, Some(1));
        assert_eq!(receipt.response_bytes.as_deref(), Some(&[1, 2, 3][..]));
    }

    #[test]
    fn reserve_or_replay_receipt_rejects_actor_or_payload_mismatch() {
        ReplayReceiptOps::reset_for_tests();

        let _ = reserve_or_replay_receipt(input()).expect("reserve");

        let mut actor_mismatch = input();
        actor_mismatch.actor = ReplayActor::direct_caller(p(2));
        assert_eq!(
            reserve_or_replay_receipt(actor_mismatch).expect("actor mismatch"),
            ReplayReceiptDecision::ActorMismatch
        );

        let mut payload_mismatch = input();
        payload_mismatch.payload_hash = [8; 32];
        assert_eq!(
            reserve_or_replay_receipt(payload_mismatch).expect("payload mismatch"),
            ReplayReceiptDecision::PayloadMismatch
        );
    }

    #[test]
    fn reserve_or_replay_receipt_reports_in_progress_for_reserved_or_effect() {
        ReplayReceiptOps::reset_for_tests();

        let token = match reserve_or_replay_receipt(input()).expect("reserve") {
            ReplayReceiptDecision::Fresh(token) => token,
            other => panic!("expected fresh, got {other:?}"),
        };
        assert_eq!(
            reserve_or_replay_receipt(input()).expect("reserved duplicate"),
            ReplayReceiptDecision::OperationInProgress
        );

        mark_external_effect_in_flight(
            &token,
            ExternalEffectDescriptor::ManagementCreateCanister {
                command_kind: CommandKind::new("test.command.v1").expect("command"),
            },
            150,
        );
        assert_eq!(
            reserve_or_replay_receipt(input()).expect("in-flight duplicate"),
            ReplayReceiptDecision::OperationInProgress
        );
    }

    #[test]
    fn reserve_or_replay_receipt_enforces_pending_actor_quota_for_fresh_receipts() {
        ReplayReceiptOps::reset_for_tests();

        let first = reserve_or_replay_receipt_with_limits(input(), 1, 10).expect("first reserve");
        assert!(matches!(first, ReplayReceiptDecision::Fresh(_)));

        let second = reserve_or_replay_receipt_with_limits(
            input_with("test.command.v1", p(1), [8; 32], 101),
            1,
            10,
        )
        .expect("second decision");

        assert_eq!(
            second,
            ReplayReceiptDecision::PendingActorQuotaExceeded {
                actor: ReplayActor::direct_caller(p(1)),
                max_pending: 1,
            }
        );
        assert_eq!(
            ReplayReceiptOps::len(),
            1,
            "quota rejection must not write a fresh receipt"
        );
    }

    #[test]
    fn reserve_or_replay_receipt_enforces_pending_command_quota_for_fresh_receipts() {
        ReplayReceiptOps::reset_for_tests();

        let first = reserve_or_replay_receipt_with_limits(input(), 10, 1).expect("first reserve");
        assert!(matches!(first, ReplayReceiptDecision::Fresh(_)));

        let second = reserve_or_replay_receipt_with_limits(
            input_with("test.command.v1", p(2), [8; 32], 101),
            10,
            1,
        )
        .expect("second decision");

        assert_eq!(
            second,
            ReplayReceiptDecision::PendingCommandQuotaExceeded {
                command_kind: CommandKind::new("test.command.v1").expect("command"),
                max_pending: 1,
            }
        );
        assert_eq!(
            ReplayReceiptOps::len(),
            1,
            "quota rejection must not write a fresh receipt"
        );
    }

    #[test]
    fn pending_receipt_quota_ignores_expired_pending_receipts() {
        ReplayReceiptOps::reset_for_tests();

        let first = reserve_or_replay_receipt_with_limits(input().with_expires_at_ns(100), 1, 10)
            .expect("first reserve");
        assert!(matches!(first, ReplayReceiptDecision::Fresh(_)));

        let second = reserve_or_replay_receipt_with_limits(
            input_with("test.command.v1", p(1), [8; 32], 101),
            1,
            10,
        )
        .expect("second reserve");

        assert!(
            matches!(second, ReplayReceiptDecision::Fresh(_)),
            "expired pending receipts must not consume pending quota"
        );
        assert_eq!(ReplayReceiptOps::len(), 2);
    }

    #[test]
    fn pending_receipt_quota_ignores_pending_receipts_at_expiry_boundary() {
        ReplayReceiptOps::reset_for_tests();

        let first = reserve_or_replay_receipt_with_limits(
            input_with("test.command.v1", p(1), [7; 32], 90).with_expires_at_ns(100),
            1,
            10,
        )
        .expect("first reserve");
        assert!(matches!(first, ReplayReceiptDecision::Fresh(_)));

        let second = reserve_or_replay_receipt_with_limits(
            input_with("test.command.v1", p(1), [8; 32], 100),
            1,
            10,
        )
        .expect("second reserve");

        assert!(
            matches!(second, ReplayReceiptDecision::Fresh(_)),
            "receipts at their expiry boundary must not consume pending quota"
        );
        assert_eq!(ReplayReceiptOps::len(), 2);
    }

    #[test]
    fn pending_receipt_quota_does_not_block_committed_replay() {
        ReplayReceiptOps::reset_for_tests();

        let committed_token = match reserve_or_replay_receipt_with_limits(input(), 1, 10)
            .expect("reserve committed target")
        {
            ReplayReceiptDecision::Fresh(token) => token,
            other => panic!("expected fresh, got {other:?}"),
        };
        commit_receipt_response(&committed_token, 1, vec![1, 2, 3], 150);

        let pending = reserve_or_replay_receipt_with_limits(
            input_with("test.command.v1", p(1), [8; 32], 160),
            1,
            10,
        )
        .expect("reserve pending filler");
        assert!(matches!(pending, ReplayReceiptDecision::Fresh(_)));

        let duplicate =
            reserve_or_replay_receipt_with_limits(input(), 1, 10).expect("committed duplicate");
        assert!(matches!(
            duplicate,
            ReplayReceiptDecision::ReturnCommitted(_)
        ));
    }

    #[test]
    fn terminal_receipt_transitions_preserve_recorded_external_effect() {
        ReplayReceiptOps::reset_for_tests();

        let token = match reserve_or_replay_receipt(input()).expect("reserve") {
            ReplayReceiptDecision::Fresh(token) => token,
            other => panic!("expected fresh, got {other:?}"),
        };
        let effect = ExternalEffectDescriptor::ThresholdEcdsaSign {
            key_id_hash: [1; 32],
            purpose: super::super::model::EcdsaPurpose::DelegationProof,
            message_hash: [2; 32],
        };
        mark_external_effect_in_flight(&token, effect.clone(), 150);
        commit_receipt_response(&token, 1, vec![1, 2, 3], 200);

        let receipt = ReplayReceiptOps::get(token.key())
            .expect("receipt stored")
            .into_receipt()
            .expect("receipt decodes");
        assert_eq!(receipt.status, ReplayReceiptStatus::Committed);
        assert_eq!(receipt.effect, Some(effect));
    }

    #[test]
    fn terminal_failure_is_bounded_before_storage() {
        ReplayReceiptOps::reset_for_tests();

        let token = match reserve_or_replay_receipt(input()).expect("reserve") {
            ReplayReceiptDecision::Fresh(token) => token,
            other => panic!("expected fresh, got {other:?}"),
        };
        commit_terminal_failure(
            &token,
            ReplayTerminalErrorCode::ExecutionFailed,
            &vec![7; super::super::model::MAX_REPLAY_TERMINAL_ERROR_BYTES + 1],
            300,
        );

        let duplicate = reserve_or_replay_receipt(input()).expect("duplicate");
        let ReplayReceiptDecision::TerminalFailed {
            error_code,
            error_bytes,
            error_bytes_truncated,
        } = duplicate
        else {
            panic!("expected terminal failure replay");
        };

        assert_eq!(error_code, ReplayTerminalErrorCode::ExecutionFailed);
        assert_eq!(
            error_bytes.len(),
            super::super::model::MAX_REPLAY_TERMINAL_ERROR_BYTES
        );
        assert!(error_bytes_truncated);
    }
}