canic-core 0.25.7

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
// Category A - Internal runtime-configured tests (ConfigTestBuilder when needed).

use crate::{
    InternalError,
    cdk::types::Cycles,
    config::schema::{
        CanisterConfig, CanisterKind, DelegatedAuthCanisterConfig, RandomnessConfig, ScalePool,
        ScalePoolPolicy, ScalingConfig, ShardingConfig, StandardsCanisterConfig,
    },
    domain::policy::topology::registry::{
        RegistryPolicy, RegistryPolicyError, RegistryRegistrationObservation,
    },
    domain::policy::topology::{RegistryPolicyInput, TopologyPolicyError, TopologyPolicyInput},
    dto::error::{Error, ErrorCode},
    ids::CanisterRole,
    ops::storage::registry::subnet::SubnetRegistryOps,
    test::seams::{lock, p},
};

fn root_canister_config() -> CanisterConfig {
    CanisterConfig {
        kind: CanisterKind::Root,
        initial_cycles: Cycles::new(0),
        topup_policy: None,
        randomness: RandomnessConfig::default(),
        scaling: None,
        sharding: None,
        delegated_auth: DelegatedAuthCanisterConfig::default(),
        standards: StandardsCanisterConfig::default(),
    }
}

fn singleton_canister_config() -> CanisterConfig {
    CanisterConfig {
        kind: CanisterKind::Singleton,
        initial_cycles: Cycles::new(0),
        topup_policy: None,
        randomness: RandomnessConfig::default(),
        scaling: None,
        sharding: None,
        delegated_auth: DelegatedAuthCanisterConfig::default(),
        standards: StandardsCanisterConfig::default(),
    }
}

fn singleton_scaling_parent_config() -> CanisterConfig {
    let mut scaling = ScalingConfig::default();
    scaling.pools.insert(
        "replicas".to_string(),
        ScalePool {
            canister_role: CanisterRole::new("replica_role"),
            policy: ScalePoolPolicy::default(),
        },
    );

    CanisterConfig {
        kind: CanisterKind::Singleton,
        initial_cycles: Cycles::new(0),
        topup_policy: None,
        randomness: RandomnessConfig::default(),
        scaling: Some(scaling),
        sharding: None,
        delegated_auth: DelegatedAuthCanisterConfig::default(),
        standards: StandardsCanisterConfig::default(),
    }
}

fn singleton_sharding_parent_config() -> CanisterConfig {
    CanisterConfig {
        kind: CanisterKind::Singleton,
        initial_cycles: Cycles::new(0),
        topup_policy: None,
        randomness: RandomnessConfig::default(),
        scaling: None,
        sharding: Some(ShardingConfig::default()),
        delegated_auth: DelegatedAuthCanisterConfig::default(),
        standards: StandardsCanisterConfig::default(),
    }
}

fn replica_canister_config() -> CanisterConfig {
    CanisterConfig {
        kind: CanisterKind::Replica,
        initial_cycles: Cycles::new(0),
        topup_policy: None,
        randomness: RandomnessConfig::default(),
        scaling: None,
        sharding: None,
        delegated_auth: DelegatedAuthCanisterConfig::default(),
        standards: StandardsCanisterConfig::default(),
    }
}

fn shard_canister_config() -> CanisterConfig {
    CanisterConfig {
        kind: CanisterKind::Shard,
        initial_cycles: Cycles::new(0),
        topup_policy: None,
        randomness: RandomnessConfig::default(),
        scaling: None,
        sharding: None,
        delegated_auth: DelegatedAuthCanisterConfig::default(),
        standards: StandardsCanisterConfig::default(),
    }
}

fn tenant_canister_config() -> CanisterConfig {
    CanisterConfig {
        kind: CanisterKind::Tenant,
        initial_cycles: Cycles::new(0),
        topup_policy: None,
        randomness: RandomnessConfig::default(),
        scaling: None,
        sharding: None,
        delegated_auth: DelegatedAuthCanisterConfig::default(),
        standards: StandardsCanisterConfig::default(),
    }
}

#[test]
fn registry_kind_policy_blocks_but_ops_allows() {
    let _guard = lock();

    for (pid, _) in SubnetRegistryOps::data().entries {
        let _ = SubnetRegistryOps::remove(&pid);
    }

    let role = CanisterRole::new("seam_registry_singleton");
    let parent_role = CanisterRole::ROOT;
    let existing_pid = p(1);
    let root_pid = p(2);

    let data = RegistryPolicyInput {
        entries: vec![TopologyPolicyInput {
            pid: existing_pid,
            role: role.clone(),
            parent_pid: Some(root_pid),
            module_hash: None,
        }],
    };

    let err = RegistryPolicy::can_register_role(
        &role,
        root_pid,
        &data,
        &root_canister_config(),
        &parent_role,
        &root_canister_config(),
    )
    .expect_err("policy should reject duplicate singleton role");
    match &err {
        RegistryPolicyError::RoleAlreadyRegistered {
            role: err_role,
            pid,
        } => {
            assert_eq!(err_role, &role);
            assert_eq!(*pid, existing_pid);
        }
        RegistryPolicyError::SingletonAlreadyRegisteredUnderParent { .. }
        | RegistryPolicyError::ReplicaRequiresSingletonWithScaling { .. }
        | RegistryPolicyError::ShardRequiresSingletonWithSharding { .. }
        | RegistryPolicyError::TenantRequiresSingletonParent { .. } => {
            panic!("expected root duplicate role error")
        }
    }

    let public = Error::from(InternalError::from(TopologyPolicyError::from(err)));
    assert_eq!(public.code, ErrorCode::PolicyRoleAlreadyRegistered);

    let created_at = 1;
    SubnetRegistryOps::register_root(root_pid, created_at);
    SubnetRegistryOps::register_unchecked(existing_pid, &role, root_pid, vec![], created_at)
        .expect("register first canister");
    let duplicate_pid = p(3);
    SubnetRegistryOps::register_unchecked(duplicate_pid, &role, root_pid, vec![], created_at)
        .expect("ops should allow duplicate role when policy is bypassed");

    let duplicates = SubnetRegistryOps::data()
        .entries
        .into_iter()
        .filter(|(_, entry)| entry.role == role)
        .count();

    assert_eq!(duplicates, 2);
}

#[test]
fn registry_singleton_policy_blocks_under_parent() {
    let _guard = lock();

    for (pid, _) in SubnetRegistryOps::data().entries {
        let _ = SubnetRegistryOps::remove(&pid);
    }

    let role = CanisterRole::new("seam_registry_singleton_child");
    let parent_role = CanisterRole::new("singleton_parent");
    let parent_pid = p(4);
    let existing_pid = p(5);

    let data = RegistryPolicyInput {
        entries: vec![TopologyPolicyInput {
            pid: existing_pid,
            role: role.clone(),
            parent_pid: Some(parent_pid),
            module_hash: None,
        }],
    };

    let err = RegistryPolicy::can_register_role(
        &role,
        parent_pid,
        &data,
        &singleton_canister_config(),
        &parent_role,
        &singleton_canister_config(),
    )
    .expect_err("policy should reject duplicate singleton role under parent");

    match &err {
        RegistryPolicyError::SingletonAlreadyRegisteredUnderParent {
            role: err_role,
            parent_pid: err_parent,
            pid,
        } => {
            assert_eq!(err_role, &role);
            assert_eq!(*err_parent, parent_pid);
            assert_eq!(*pid, existing_pid);
        }
        RegistryPolicyError::RoleAlreadyRegistered { .. }
        | RegistryPolicyError::ReplicaRequiresSingletonWithScaling { .. }
        | RegistryPolicyError::ShardRequiresSingletonWithSharding { .. }
        | RegistryPolicyError::TenantRequiresSingletonParent { .. } => {
            panic!("expected duplicate singleton under parent error");
        }
    }

    let public = Error::from(InternalError::from(TopologyPolicyError::from(err)));
    assert_eq!(
        public.code,
        ErrorCode::PolicySingletonAlreadyRegisteredUnderParent
    );
    assert!(public.message.contains("singleton role"));
}

#[test]
fn registry_wasm_store_policy_allows_multiple_under_same_parent() {
    let role = CanisterRole::WASM_STORE;
    let parent_role = CanisterRole::ROOT;
    let parent_pid = p(6);
    let existing_pid = p(7);

    let data = RegistryPolicyInput {
        entries: vec![TopologyPolicyInput {
            pid: existing_pid,
            role: role.clone(),
            parent_pid: Some(parent_pid),
            module_hash: None,
        }],
    };

    RegistryPolicy::can_register_role(
        &role,
        parent_pid,
        &data,
        &singleton_canister_config(),
        &parent_role,
        &root_canister_config(),
    )
    .expect("wasm_store fleet role should allow multiple stores under the same root");
}

#[test]
fn tenant_creation_requires_singleton_parent() {
    let role = CanisterRole::new("tenant_child");
    let parent_role = CanisterRole::new("plain_parent");
    let parent_pid = p(7);
    let data = RegistryPolicyInput { entries: vec![] };

    let err = RegistryPolicy::can_register_role(
        &role,
        parent_pid,
        &data,
        &tenant_canister_config(),
        &parent_role,
        &root_canister_config(),
    )
    .expect_err("policy should reject tenant creation under non-singleton parent");

    match &err {
        RegistryPolicyError::TenantRequiresSingletonParent {
            role: err_role,
            parent_role: err_parent_role,
        } => {
            assert_eq!(err_role, &role);
            assert_eq!(err_parent_role, &parent_role);
        }
        _ => panic!("expected tenant singleton-parent policy error"),
    }

    let public = Error::from(InternalError::from(TopologyPolicyError::from(err)));
    assert_eq!(public.code, ErrorCode::PolicyTenantRequiresSingletonParent);
    assert!(
        public
            .message
            .contains("must be created by a singleton parent")
    );
}

#[test]
fn tenant_creation_succeeds_under_singleton_parent() {
    let role = CanisterRole::new("tenant_child");
    let parent_role = CanisterRole::new("project_hub");
    let parent_pid = p(10);
    let data = RegistryPolicyInput { entries: vec![] };

    RegistryPolicy::can_register_role(
        &role,
        parent_pid,
        &data,
        &tenant_canister_config(),
        &parent_role,
        &singleton_canister_config(),
    )
    .expect("tenant should be allowed under singleton parent");
}

#[test]
fn replica_creation_succeeds_under_singleton_scaling_parent() {
    let role = CanisterRole::new("replica_child");
    let parent_role = CanisterRole::new("scale_hub");
    let parent_pid = p(8);
    let data = RegistryPolicyInput { entries: vec![] };

    RegistryPolicy::can_register_role(
        &role,
        parent_pid,
        &data,
        &replica_canister_config(),
        &parent_role,
        &singleton_scaling_parent_config(),
    )
    .expect("replica should be allowed under singleton scaling parent");
}

#[test]
fn shard_creation_succeeds_under_singleton_sharding_parent() {
    let role = CanisterRole::new("shard_child");
    let parent_role = CanisterRole::new("shard_hub");
    let parent_pid = p(9);
    let data = RegistryPolicyInput { entries: vec![] };

    RegistryPolicy::can_register_role(
        &role,
        parent_pid,
        &data,
        &shard_canister_config(),
        &parent_role,
        &singleton_sharding_parent_config(),
    )
    .expect("shard should be allowed under singleton sharding parent");
}

#[test]
fn observed_registration_policy_matches_duplicate_singleton_decision() {
    let role = CanisterRole::new("observed_singleton_child");
    let parent_role = CanisterRole::new("singleton_parent");
    let parent_pid = p(11);
    let existing_pid = p(12);

    let err = RegistryPolicy::can_register_role_observed(
        &role,
        parent_pid,
        RegistryRegistrationObservation {
            existing_role_pid: Some(existing_pid),
            existing_singleton_under_parent_pid: Some(existing_pid),
        },
        &singleton_canister_config(),
        &parent_role,
        &singleton_canister_config(),
    )
    .expect_err("observed policy should reject duplicate singleton role under parent");

    match err {
        RegistryPolicyError::SingletonAlreadyRegisteredUnderParent {
            role: err_role,
            parent_pid: err_parent,
            pid,
        } => {
            assert_eq!(err_role, role);
            assert_eq!(err_parent, parent_pid);
            assert_eq!(pid, existing_pid);
        }
        other => panic!("unexpected observed policy error: {other}"),
    }
}

#[test]
fn observed_registration_policy_accepts_replica_without_registry_snapshot() {
    let role = CanisterRole::new("replica_child_observed");
    let parent_role = CanisterRole::new("scale_hub");

    RegistryPolicy::can_register_role_observed(
        &role,
        p(13),
        RegistryRegistrationObservation::default(),
        &replica_canister_config(),
        &parent_role,
        &singleton_scaling_parent_config(),
    )
    .expect("replica should be allowed from observed parent config without full registry input");
}