canic-core 0.110.2

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
//! Module: role_contract::model
//!
//! Responsibility: define passive typed role-contract inputs, outputs, and findings.
//! Does not own: Cargo evidence collection, state descriptors, or report rendering.
//! Boundary: host/build consumers provide typed feature evidence to pure core policy.

use crate::{config::schema::ConfigModel, ids::CanisterRole};
use serde::{Deserialize, Serialize};
use std::collections::BTreeSet;

///
/// CanicFeatureKey
///
/// Maintained public feature of the `canic` facade crate.
///

#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum CanicFeatureKey {
    AuthChainKeyEcdsa,
    AuthChainKeyRootSign,
    AuthDelegatedTokenVerify,
    AuthIssuerCanisterSigCreate,
    AuthIssuerCanisterSigVerify,
    AuthRootCanisterSigCreate,
    AuthRootCanisterSigVerify,
    BlobStorage,
    BlobStorageBilling,
    ControlPlane,
    FleetCoordinatorCanister,
    InternalTestFixtures,
    Sharding,
    WasmStoreCanister,
}

///
/// CanicFeatureEffect
///
/// Whether enabling one public Canic feature selects stable state.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum CanicFeatureEffect {
    NoState,
    StateBearing,
}

///
/// RoleCapabilityKey
///
/// Typed behavior derived from validated role configuration or built-in identity.
///

#[derive(Clone, Copy, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
pub enum RoleCapabilityKey {
    AutomaticTopup,
    ChildProvisioning,
    DelegatedTokenIssuer,
    DelegatedTokenVerifier,
    FleetAdmissionProjection,
    FleetCoordinator,
    Icrc21,
    Index,
    LocalApplicationAuthorization,
    RoleAttestationSigner,
    RoleAttestationVerifier,
    Root,
    RootControlPlane,
    Runtime,
    Scaling,
    Sharding,
    WasmStore,
}

impl RoleCapabilityKey {
    /// Stable manifest spelling used by protocol-profile identity encoding.
    #[must_use]
    pub const fn manifest_name(self) -> &'static str {
        match self {
            Self::AutomaticTopup => "AutomaticTopup",
            Self::ChildProvisioning => "ChildProvisioning",
            Self::DelegatedTokenIssuer => "DelegatedTokenIssuer",
            Self::DelegatedTokenVerifier => "DelegatedTokenVerifier",
            Self::FleetAdmissionProjection => "FleetAdmissionProjection",
            Self::FleetCoordinator => "FleetCoordinator",
            Self::Icrc21 => "Icrc21",
            Self::Index => "Index",
            Self::LocalApplicationAuthorization => "LocalApplicationAuthorization",
            Self::RoleAttestationSigner => "RoleAttestationSigner",
            Self::RoleAttestationVerifier => "RoleAttestationVerifier",
            Self::Root => "Root",
            Self::RootControlPlane => "RootControlPlane",
            Self::Runtime => "Runtime",
            Self::Scaling => "Scaling",
            Self::Sharding => "Sharding",
            Self::WasmStore => "WasmStore",
        }
    }
}

///
/// StateAllocationKey
///
/// Typed identity of one active Canic-managed stable-memory allocation group.
///

#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum StateAllocationKey {
    TemplateManifests,
    TemplateChunkSets,
    TemplateChunkRefs,
    TemplateChunkPayloads,
    WasmStoreGcState,
    FleetCoordinatorAdmission,
    FleetCoordinatorFunding,
    FleetCoordinatorRegistry,
    RootAdmission,
    RootFunding,
    RootWasmStoreState,
    RootFleetRegistryMirror,
    RootComponentRegistry,
    RootCanisterPool,
    RootComponentProvisioning,
    CoreRuntimeChildren,
    CoreRuntimeBindings,
    CoreFleetState,
    CoreFleetActivation,
    CoreAuthState,
    CoreReplayReceipts,
    CoreCycles,
    CoreCyclesIcpRefillRecords,
    CoreRuntimeLog,
    CoreIntent,
    CoreApplicationReceipts,
    CorePlacementAcknowledgement,
    PlacementScalingRegistry,
    PlacementIndexRegistry,
    ShardingRegistry,
    ShardingAssignments,
    ShardingActiveSet,
    BlobStorageRoots,
    BlobStoragePendingDeletions,
    BlobStorageGatewayPrincipals,
    BlobStorageBilling,
    CoreAuthorityRestoreFence,
    CoreAsyncJobRecovery,
    CoreFleetAdmissionProjection,
}

///
/// AllocationOwner
///
/// Crate that owns the records and storage implementation for an allocation.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum AllocationOwner {
    CanicControlPlane,
    CanicCore,
}

impl AllocationOwner {
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::CanicControlPlane => "canic-control-plane",
            Self::CanicCore => "canic-core",
        }
    }
}

///
/// MemoryId
///
/// Typed stable-memory manager ID owned by the allocation registry.
///

#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct MemoryId(u8);

impl MemoryId {
    #[must_use]
    pub const fn new(value: u8) -> Self {
        Self(value)
    }

    #[must_use]
    pub const fn get(self) -> u8 {
        self.0
    }
}

///
/// AllocationDefinition
///
/// Canonical assignment of one or more stable-memory IDs to an active allocation.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct AllocationDefinition {
    pub key: StateAllocationKey,
    pub owner: AllocationOwner,
    pub memory_ids: &'static [MemoryId],
}

///
/// BuiltInRoleKind
///
/// Canic-managed role that is not resolved from an App role declaration.
///

#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum BuiltInRoleKind {
    FleetCoordinator,
    WasmStore,
}

///
/// RoleContractSource
///
/// Validated configuration source used by pure role-contract resolution.
///

pub enum RoleContractSource<'a> {
    BuiltIn(BuiltInRoleKind),
    Declared {
        config: &'a ConfigModel,
        role: &'a CanisterRole,
    },
}

///
/// RoleContractInput
///
/// Typed feature evidence for one pure role-contract resolution.
///

pub struct RoleContractInput<'a> {
    pub source: RoleContractSource<'a>,
    pub declared_features: BTreeSet<CanicFeatureKey>,
    pub default_features_enabled: bool,
}

///
/// RoleFeatureRequirement
///
/// Public Canic feature required by one derived role capability.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct RoleFeatureRequirement {
    pub capability: RoleCapabilityKey,
    pub config_key: &'static str,
    pub feature: CanicFeatureKey,
    pub reason: &'static str,
}

///
/// SelectionProvenance
///
/// Typed reason one active allocation was selected for a role contract.
///

#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum SelectionProvenance {
    BuiltInRole(BuiltInRoleKind),
    Capability(RoleCapabilityKey),
    EffectiveFeature(CanicFeatureKey),
}

///
/// ResolvedStateAllocation
///
/// Active allocation selected for one resolved role contract.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ResolvedStateAllocation {
    pub key: StateAllocationKey,
    pub owner: AllocationOwner,
    pub memory_ids: Vec<MemoryId>,
    pub selected_by: BTreeSet<SelectionProvenance>,
}

///
/// ResolvedRoleContract
///
/// Consumable role contract returned only when all blocking checks pass.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ResolvedRoleContract {
    pub role: CanisterRole,
    pub built_in: Option<BuiltInRoleKind>,
    pub capabilities: BTreeSet<RoleCapabilityKey>,
    pub required_features: BTreeSet<CanicFeatureKey>,
    pub effective_features: BTreeSet<CanicFeatureKey>,
    pub allocations: Vec<ResolvedStateAllocation>,
}

///
/// RoleContractFinding
///
/// Blocking failure produced by pure role-contract validation.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum RoleContractFinding {
    AllocationDescriptorDuplicate {
        key: StateAllocationKey,
    },
    AllocationDescriptorIdMismatch {
        key: StateAllocationKey,
        expected: Vec<MemoryId>,
        actual: Vec<MemoryId>,
    },
    AllocationDescriptorMissing {
        key: StateAllocationKey,
    },
    CatalogInvalid {
        reason: String,
    },
    BuiltInPackageUnavailable {
        role: BuiltInRoleKind,
    },
    CanicVersionMismatch {
        expected: String,
        actual: String,
    },
    CargoCatalogDrift {
        reason: String,
    },
    CargoEvidenceUnavailable {
        phase: String,
        cause: String,
    },
    DependencyShapeUnsupported {
        reason: String,
    },
    MemoryIdCollision {
        memory_id: MemoryId,
        first: StateAllocationKey,
        second: StateAllocationKey,
    },
    MultipleCanicPackages {
        packages: Vec<String>,
    },
    PackageAmbiguous {
        role: CanisterRole,
    },
    PackageMetadataMismatch {
        expected_app: String,
        expected_role: CanisterRole,
        actual_app: Option<String>,
        actual_role: Option<String>,
    },
    PackageMissing {
        role: CanisterRole,
    },
    RequiredFeatureMissing {
        capability: RoleCapabilityKey,
        feature: CanicFeatureKey,
    },
    RoleUnknown {
        role: CanisterRole,
    },
    RuntimeCanicDependencyMissing {
        role: CanisterRole,
    },
}

impl RoleContractFinding {
    #[must_use]
    pub const fn code(&self) -> &'static str {
        match self {
            Self::AllocationDescriptorDuplicate { .. } => {
                "role_contract_allocation_descriptor_duplicate"
            }
            Self::AllocationDescriptorIdMismatch { .. } => "role_contract_allocation_id_mismatch",
            Self::AllocationDescriptorMissing { .. } => {
                "role_contract_allocation_descriptor_missing"
            }
            Self::BuiltInPackageUnavailable { .. } => "role_contract_builtin_package_unavailable",
            Self::CanicVersionMismatch { .. } => "role_contract_canic_version_mismatch",
            Self::CargoCatalogDrift { .. } => "role_contract_cargo_catalog_drift",
            Self::CargoEvidenceUnavailable { .. } => "role_contract_cargo_evidence_unavailable",
            Self::CatalogInvalid { .. } => "role_contract_catalog_invalid",
            Self::DependencyShapeUnsupported { .. } => "role_contract_dependency_shape_unsupported",
            Self::MemoryIdCollision { .. } => "role_contract_memory_id_collision",
            Self::MultipleCanicPackages { .. } => "role_contract_multiple_canic_packages",
            Self::PackageAmbiguous { .. } => "role_contract_package_ambiguous",
            Self::PackageMetadataMismatch { .. } => "role_contract_package_metadata_mismatch",
            Self::PackageMissing { .. } => "role_contract_package_missing",
            Self::RequiredFeatureMissing { .. } => "role_contract_required_feature_missing",
            Self::RoleUnknown { .. } => "role_contract_role_unknown",
            Self::RuntimeCanicDependencyMissing { .. } => {
                "role_contract_runtime_canic_dependency_missing"
            }
        }
    }
}

///
/// RoleContractResolution
///
/// Resolved contract or blocking findings; rejected results never carry a contract.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum RoleContractResolution {
    Rejected { errors: Vec<RoleContractFinding> },
    Resolved { contract: ResolvedRoleContract },
}