canic-core 0.101.26

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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
//! Module: ops::fleet_registry
//!
//! Responsibility: compile and encode canonical Fleet Registry snapshot evidence.
//! Does not own: stable commits, lifecycle transitions, synchronization, or endpoints.
//! Boundary: validates passive snapshots against one exact compiled Component Topology.

#[cfg(test)]
mod tests;
mod validation;

use crate::{
    InternalError,
    config::{
        ComponentTopology, ComponentTopologyError, FleetServiceMemberPurpose,
        FleetServicePlacementPolicy,
    },
    dto::fleet_registry::{
        FleetComponentSpecEntry, FleetDirectoryProvenance, FleetDirectoryService,
        FleetDirectoryServiceComponent, FleetDirectorySnapshot, FleetRegistry,
        FleetRegistryManifest, FleetRegistryVersion, FleetServiceBinding,
        FleetServiceComponentBinding, FleetServiceMode, FleetSubnetRootDirectoryEntry,
        FleetSubnetRootEntry, FleetSubnetRootStatus,
    },
    ids::{
        AppId, ComponentSpecAdmission, ComponentSpecId, FleetRegistryAuthority,
        FleetSubnetRootLimits, ReleaseBuildId,
    },
    ops::OpsError,
};
use candid::Principal;
use sha2::{Digest, Sha256};
use thiserror::Error as ThisError;

const FLEET_REGISTRY_DOMAIN: &[u8] = b"canic/fleet-registry/v1";
const FLEET_REGISTRY_SCHEMA_VERSION: u32 = 1;

/// Maximum canonical bytes accepted for one Fleet Registry snapshot.
pub const MAX_FLEET_REGISTRY_CANONICAL_BYTES: usize = 2_097_152;

///
/// FleetRegistryOpsError
///
/// Typed operations-layer failure while validating or compiling canonical Registry evidence.
///

#[derive(Debug, ThisError)]
pub enum FleetRegistryOpsError {
    #[error("Fleet Registry Coordinator principal must not be anonymous")]
    AnonymousCoordinator,

    #[error("Fleet Registry Coordinator Subnet must not be anonymous")]
    AnonymousCoordinatorSubnet,

    #[error("Fleet Registry root principal must not be anonymous")]
    AnonymousFleetSubnetRoot,

    #[error("Fleet Registry authority does not match the protected expected authority")]
    AuthorityMismatch,

    #[error("Fleet Registry canonical bytes exceed bound {maximum_bytes}: {actual_bytes}")]
    CanonicalBytesExceeded {
        actual_bytes: usize,
        maximum_bytes: usize,
    },

    #[error("Fleet Registry contains duplicate root principal {fleet_subnet_root}")]
    DuplicateFleetSubnetRoot { fleet_subnet_root: Principal },

    #[error(
        "Fleet Registry admissions for Component Spec '{component_spec}' exceed its Fleet maximum {maximum_fleet_instances}: {admitted}"
    )]
    FleetAdmissionsExceedMaximum {
        component_spec: ComponentSpecId,
        admitted: u32,
        maximum_fleet_instances: u32,
    },

    #[error("Fleet Registry admission total overflowed for Component Spec '{component_spec}'")]
    FleetAdmissionsOverflow { component_spec: ComponentSpecId },

    #[error(
        "Fleet Registry Component Spec '{component_spec}' does not match the compiled topology"
    )]
    FleetComponentSpecMismatch { component_spec: ComponentSpecId },

    #[error("Fleet Registry Component Specs are not the complete compiled topology")]
    FleetComponentSpecSetMismatch,

    #[error("Fleet Registry root join conflicts with an existing Subnet or root principal")]
    FleetSubnetRootJoinIdentityConflict,

    #[error("Fleet Registry initial service publication requires an empty current service set")]
    FleetServicePublicationRequiresEmptyRegistry,

    #[error("Fleet Registry initial service publication requires a non-empty complete service set")]
    FleetServicePublicationRequiresServices,

    #[error("Fleet Registry root join requires status Joining")]
    FleetSubnetRootJoinRequiresJoining,

    #[error("Fleet Registry activation requires a non-empty all-Joining root set")]
    FleetSubnetRootActivationRequiresAllJoining,

    #[error("Fleet Registry root draining requires an Active target")]
    FleetSubnetRootDrainingRequiresActive,

    #[error("Fleet Registry root draining target {fleet_subnet_root} is missing")]
    FleetSubnetRootDrainingTargetMissing { fleet_subnet_root: Principal },

    #[error("Fleet Registry root removal requires a Draining target")]
    FleetSubnetRootRemovalRequiresDraining,

    #[error("Fleet Registry root removal target {fleet_subnet_root} is missing")]
    FleetSubnetRootRemovalTargetMissing { fleet_subnet_root: Principal },

    #[error("Fleet Directory activation requires a non-empty root set with no Joining rows")]
    FleetDirectoryRequiresPublishedRoots,

    #[error("Fleet Directory source does not name one current non-Removed Registry root")]
    FleetDirectorySourceMissing,

    #[error("Fleet Registry genesis App '{received}' does not match configured App '{expected}'")]
    GenesisAppMismatch { expected: AppId, received: AppId },

    #[error("Fleet Registry genesis requires authority epoch 1, got {0}")]
    GenesisAuthorityEpoch(u64),

    #[error("Fleet Registry root order is not strictly ascending by physical Subnet")]
    NonCanonicalFleetSubnetRootOrder,

    #[error("Fleet Registry service order is not strictly ascending by service ID")]
    NonCanonicalFleetServiceOrder,

    #[error("Fleet Registry service '{service}' member order is not canonical")]
    NonCanonicalFleetServiceMemberOrder { service: crate::ids::FleetServiceId },

    #[error("Fleet Registry service '{service}' has no members")]
    EmptyFleetService { service: crate::ids::FleetServiceId },

    #[error("Fleet Registry service '{service}' does not match its Fleet Component Spec")]
    FleetServiceSpecMismatch { service: crate::ids::FleetServiceId },

    #[error("Fleet Registry service '{service}' has an invalid mode-specific member set")]
    FleetServiceModeMismatch { service: crate::ids::FleetServiceId },

    #[error("Fleet Registry service '{service}' has an invalid placement policy or assignment")]
    FleetServicePlacementMismatch { service: crate::ids::FleetServiceId },

    #[error("Fleet Registry service '{service}' member names a non-Active or non-admitting root")]
    FleetServiceRootMismatch { service: crate::ids::FleetServiceId },

    #[error("Fleet Registry service member Component identity is zero")]
    EmptyFleetServiceComponentIdentity,

    #[error("Fleet Registry service member Canister principal must not be anonymous")]
    AnonymousFleetServiceComponent,

    #[error("Fleet Registry services reuse Component identity {component}")]
    DuplicateFleetServiceComponent {
        component: crate::ids::ComponentInstanceId,
    },

    #[error("Fleet Registry services reuse Canister principal {canister_id}")]
    DuplicateFleetServiceCanister { canister_id: Principal },

    #[error("Fleet Registry authority epoch must be positive")]
    NonPositiveAuthorityEpoch,

    #[error("Fleet Registry revision must be positive")]
    NonPositiveRevision,

    #[error("Fleet Registry revision is exhausted")]
    RevisionExhausted,

    #[error("Fleet Registry root principal conflicts with its Coordinator")]
    RootPrincipalConflictsWithCoordinator,

    #[error(
        "Fleet Registry roots carry different active release builds: expected {expected}, got {received}"
    )]
    RootReleaseBuildMismatch {
        expected: ReleaseBuildId,
        received: ReleaseBuildId,
    },

    #[error(transparent)]
    Topology(#[from] ComponentTopologyError),
}

///
/// FleetRegistryOps
///
/// Deterministic canonical Fleet Registry compiler used by Coordinator workflows.
///

pub struct FleetRegistryOps;

impl FleetRegistryOps {
    /// Compile revision-one empty-root authority from one exact Component Topology.
    pub fn compile_genesis(
        configured_app: &AppId,
        authority: FleetRegistryAuthority,
        topology: &ComponentTopology,
    ) -> Result<FleetRegistry, InternalError> {
        validation::compile_genesis(configured_app, authority, topology)
            .map_err(OpsError::from)
            .map_err(InternalError::from)
    }

    /// Construct the next canonical snapshot with one exact root added as `Joining`.
    pub fn compile_joining(
        expected_authority: &FleetRegistryAuthority,
        topology: &ComponentTopology,
        current: &FleetRegistry,
        entry: FleetSubnetRootEntry,
    ) -> Result<FleetRegistry, InternalError> {
        compile_joining(expected_authority, topology, current, entry)
            .map_err(OpsError::from)
            .map_err(InternalError::from)
    }

    /// Construct the next canonical snapshot with every current root atomically `Active`.
    pub fn compile_active(
        expected_authority: &FleetRegistryAuthority,
        topology: &ComponentTopology,
        current: &FleetRegistry,
    ) -> Result<FleetRegistry, InternalError> {
        compile_active(expected_authority, topology, current)
            .map_err(OpsError::from)
            .map_err(InternalError::from)
    }

    /// Construct the next canonical snapshot with the complete initial service set.
    pub fn compile_initial_services(
        expected_authority: &FleetRegistryAuthority,
        topology: &ComponentTopology,
        current: &FleetRegistry,
        services: Vec<FleetServiceBinding>,
    ) -> Result<FleetRegistry, InternalError> {
        compile_initial_services(expected_authority, topology, current, services)
            .map_err(OpsError::from)
            .map_err(InternalError::from)
    }

    /// Construct the next canonical snapshot with one exact active root marked `Draining`.
    pub fn compile_draining(
        expected_authority: &FleetRegistryAuthority,
        topology: &ComponentTopology,
        current: &FleetRegistry,
        fleet_subnet_root: Principal,
    ) -> Result<FleetRegistry, InternalError> {
        compile_draining(expected_authority, topology, current, fleet_subnet_root)
            .map_err(OpsError::from)
            .map_err(InternalError::from)
    }

    /// Construct the next canonical snapshot with one exact draining root marked `Removed`.
    pub fn compile_removed(
        expected_authority: &FleetRegistryAuthority,
        topology: &ComponentTopology,
        current: &FleetRegistry,
        fleet_subnet_root: Principal,
    ) -> Result<FleetRegistry, InternalError> {
        compile_removed(expected_authority, topology, current, fleet_subnet_root)
            .map_err(OpsError::from)
            .map_err(InternalError::from)
    }

    /// Validate the complete Registry snapshot against its compiled topology.
    pub fn validate(
        expected_authority: &FleetRegistryAuthority,
        topology: &ComponentTopology,
        registry: &FleetRegistry,
    ) -> Result<(), InternalError> {
        validation::validate(expected_authority, topology, registry)
            .map_err(OpsError::from)
            .map_err(InternalError::from)
    }

    /// Encode one validated Registry snapshot with the frozen canonical schema.
    pub fn canonical_bytes(
        expected_authority: &FleetRegistryAuthority,
        topology: &ComponentTopology,
        registry: &FleetRegistry,
    ) -> Result<Vec<u8>, InternalError> {
        canonical_bytes(expected_authority, topology, registry)
            .map_err(OpsError::from)
            .map_err(InternalError::from)
    }

    /// Derive the exact manifest for one complete canonical Registry snapshot.
    pub fn manifest(
        expected_authority: &FleetRegistryAuthority,
        topology: &ComponentTopology,
        registry: &FleetRegistry,
    ) -> Result<FleetRegistryManifest, InternalError> {
        let bytes = canonical_bytes(expected_authority, topology, registry)
            .map_err(OpsError::from)
            .map_err(InternalError::from)?;
        Ok(FleetRegistryManifest {
            authority: registry.authority.clone(),
            revision: registry.revision,
            byte_length: bytes.len() as u64,
            content_hash: Sha256::digest(bytes).into(),
        })
    }

    /// Derive the compact version used by mirrors, acknowledgements, and journals.
    pub fn version(
        expected_authority: &FleetRegistryAuthority,
        topology: &ComponentTopology,
        registry: &FleetRegistry,
    ) -> Result<FleetRegistryVersion, InternalError> {
        let manifest = Self::manifest(expected_authority, topology, registry)?;
        Ok(FleetRegistryVersion {
            authority: manifest.authority,
            revision: manifest.revision,
            content_hash: manifest.content_hash,
        })
    }

    /// Derive one current root's exact Fleet Directory from a complete published Registry.
    pub fn directory_for_root(
        expected_authority: &FleetRegistryAuthority,
        topology: &ComponentTopology,
        registry: &FleetRegistry,
        source_fleet_subnet_root: Principal,
    ) -> Result<FleetDirectorySnapshot, InternalError> {
        directory_for_root(
            expected_authority,
            topology,
            registry,
            source_fleet_subnet_root,
        )
        .map_err(OpsError::from)
        .map_err(InternalError::from)
    }
}

fn directory_for_root(
    expected_authority: &FleetRegistryAuthority,
    topology: &ComponentTopology,
    registry: &FleetRegistry,
    source_fleet_subnet_root: Principal,
) -> Result<FleetDirectorySnapshot, FleetRegistryOpsError> {
    validation::validate(expected_authority, topology, registry)?;
    let contains_joining_root = registry
        .fleet_subnet_roots
        .iter()
        .any(|entry| entry.status == FleetSubnetRootStatus::Joining);
    if registry.fleet_subnet_roots.is_empty() || contains_joining_root {
        return Err(FleetRegistryOpsError::FleetDirectoryRequiresPublishedRoots);
    }
    let source_is_current = registry.fleet_subnet_roots.iter().any(|entry| {
        entry.fleet_subnet_root == source_fleet_subnet_root
            && entry.status != FleetSubnetRootStatus::Removed
    });
    if !source_is_current {
        return Err(FleetRegistryOpsError::FleetDirectorySourceMissing);
    }
    let manifest = {
        let bytes = canonical_bytes(expected_authority, topology, registry)?;
        FleetRegistryVersion {
            authority: registry.authority.clone(),
            revision: registry.revision,
            content_hash: Sha256::digest(bytes).into(),
        }
    };
    Ok(FleetDirectorySnapshot {
        provenance: FleetDirectoryProvenance {
            registry: manifest,
            source_fleet_subnet_root,
        },
        fleet_subnet_roots: registry
            .fleet_subnet_roots
            .iter()
            .map(|entry| FleetSubnetRootDirectoryEntry {
                placement_subnet: entry.placement_subnet,
                fleet_subnet_root: entry.fleet_subnet_root,
                status: entry.status,
            })
            .collect(),
        services: registry
            .services
            .iter()
            .map(|service| FleetDirectoryService {
                service: service.service.clone(),
                role: service.role.clone(),
                component_spec: service.component_spec.clone(),
                mode: service.mode,
                placement: service.placement,
                members: service
                    .members
                    .iter()
                    .map(|member| FleetDirectoryServiceComponent {
                        member_purpose: member.member_purpose,
                        component: member.component,
                        fleet_subnet_root: member.fleet_subnet_root,
                        canister_id: member.canister_id,
                        group_placement: member.group_placement.clone(),
                        member_path: member.member_path.clone(),
                    })
                    .collect(),
            })
            .collect(),
    })
}

fn compile_joining(
    expected_authority: &FleetRegistryAuthority,
    topology: &ComponentTopology,
    current: &FleetRegistry,
    entry: FleetSubnetRootEntry,
) -> Result<FleetRegistry, FleetRegistryOpsError> {
    validation::validate(expected_authority, topology, current)?;
    if entry.status != FleetSubnetRootStatus::Joining {
        return Err(FleetRegistryOpsError::FleetSubnetRootJoinRequiresJoining);
    }
    if current
        .fleet_subnet_roots
        .iter()
        .any(|existing| existing == &entry)
    {
        return Ok(current.clone());
    }
    if current.fleet_subnet_roots.iter().any(|existing| {
        existing.placement_subnet == entry.placement_subnet
            || existing.fleet_subnet_root == entry.fleet_subnet_root
    }) {
        return Err(FleetRegistryOpsError::FleetSubnetRootJoinIdentityConflict);
    }

    let mut next = current.clone();
    next.revision = next
        .revision
        .checked_add(1)
        .ok_or(FleetRegistryOpsError::RevisionExhausted)?;
    next.fleet_subnet_roots.push(entry);
    next.fleet_subnet_roots
        .sort_by_key(|root| root.placement_subnet);
    validation::validate(expected_authority, topology, &next)?;
    Ok(next)
}

fn compile_active(
    expected_authority: &FleetRegistryAuthority,
    topology: &ComponentTopology,
    current: &FleetRegistry,
) -> Result<FleetRegistry, FleetRegistryOpsError> {
    validation::validate(expected_authority, topology, current)?;
    if current.fleet_subnet_roots.is_empty()
        || current
            .fleet_subnet_roots
            .iter()
            .any(|entry| entry.status != FleetSubnetRootStatus::Joining)
    {
        return Err(FleetRegistryOpsError::FleetSubnetRootActivationRequiresAllJoining);
    }

    let mut next = current.clone();
    next.revision = next
        .revision
        .checked_add(1)
        .ok_or(FleetRegistryOpsError::RevisionExhausted)?;
    for root in &mut next.fleet_subnet_roots {
        root.status = FleetSubnetRootStatus::Active;
    }
    validation::validate(expected_authority, topology, &next)?;
    Ok(next)
}

fn compile_initial_services(
    expected_authority: &FleetRegistryAuthority,
    topology: &ComponentTopology,
    current: &FleetRegistry,
    services: Vec<FleetServiceBinding>,
) -> Result<FleetRegistry, FleetRegistryOpsError> {
    validation::validate(expected_authority, topology, current)?;
    if !current.services.is_empty() {
        return Err(FleetRegistryOpsError::FleetServicePublicationRequiresEmptyRegistry);
    }
    if services.is_empty() {
        return Err(FleetRegistryOpsError::FleetServicePublicationRequiresServices);
    }

    let mut next = current.clone();
    next.revision = next
        .revision
        .checked_add(1)
        .ok_or(FleetRegistryOpsError::RevisionExhausted)?;
    next.services = services;
    validation::validate(expected_authority, topology, &next)?;
    Ok(next)
}

fn compile_draining(
    expected_authority: &FleetRegistryAuthority,
    topology: &ComponentTopology,
    current: &FleetRegistry,
    fleet_subnet_root: Principal,
) -> Result<FleetRegistry, FleetRegistryOpsError> {
    validation::validate(expected_authority, topology, current)?;
    let target_index = current
        .fleet_subnet_roots
        .iter()
        .position(|entry| entry.fleet_subnet_root == fleet_subnet_root)
        .ok_or(FleetRegistryOpsError::FleetSubnetRootDrainingTargetMissing { fleet_subnet_root })?;
    if current.fleet_subnet_roots[target_index].status != FleetSubnetRootStatus::Active {
        return Err(FleetRegistryOpsError::FleetSubnetRootDrainingRequiresActive);
    }

    let mut next = current.clone();
    next.revision = next
        .revision
        .checked_add(1)
        .ok_or(FleetRegistryOpsError::RevisionExhausted)?;
    next.fleet_subnet_roots[target_index].status = FleetSubnetRootStatus::Draining;
    validation::validate(expected_authority, topology, &next)?;
    Ok(next)
}

fn compile_removed(
    expected_authority: &FleetRegistryAuthority,
    topology: &ComponentTopology,
    current: &FleetRegistry,
    fleet_subnet_root: Principal,
) -> Result<FleetRegistry, FleetRegistryOpsError> {
    validation::validate(expected_authority, topology, current)?;
    let target_index = current
        .fleet_subnet_roots
        .iter()
        .position(|entry| entry.fleet_subnet_root == fleet_subnet_root)
        .ok_or(FleetRegistryOpsError::FleetSubnetRootRemovalTargetMissing { fleet_subnet_root })?;
    if current.fleet_subnet_roots[target_index].status != FleetSubnetRootStatus::Draining {
        return Err(FleetRegistryOpsError::FleetSubnetRootRemovalRequiresDraining);
    }

    let mut next = current.clone();
    next.revision = next
        .revision
        .checked_add(1)
        .ok_or(FleetRegistryOpsError::RevisionExhausted)?;
    next.fleet_subnet_roots[target_index].status = FleetSubnetRootStatus::Removed;
    validation::validate(expected_authority, topology, &next)?;
    Ok(next)
}

fn canonical_bytes(
    expected_authority: &FleetRegistryAuthority,
    topology: &ComponentTopology,
    registry: &FleetRegistry,
) -> Result<Vec<u8>, FleetRegistryOpsError> {
    validation::validate(expected_authority, topology, registry)?;

    let mut encoder = CanonicalEncoder::new();
    encode_authority(&mut encoder, &registry.authority);
    encoder.u64(registry.revision);
    encoder.u64(registry.component_specs.len() as u64);
    for component_spec in &registry.component_specs {
        encode_component_spec(&mut encoder, component_spec);
    }
    encoder.u64(registry.fleet_subnet_roots.len() as u64);
    for root in &registry.fleet_subnet_roots {
        encode_root(&mut encoder, root);
    }
    encoder.u64(registry.services.len() as u64);
    for service in &registry.services {
        encode_service(&mut encoder, service);
    }
    encoder.finish()
}

fn encode_authority(encoder: &mut CanonicalEncoder, authority: &FleetRegistryAuthority) {
    let binding = &authority.binding;
    encoder.bytes(binding.fleet.fleet.canonical_network_id.as_bytes());
    encoder.bytes(binding.fleet.fleet.fleet_id.as_bytes());
    encoder.string(binding.fleet.app.as_str());
    encoder.bytes(binding.coordinator_subnet.as_principal().as_slice());
    encoder.bytes(binding.coordinator.as_slice());
    encoder.u64(authority.epoch);
}

fn encode_component_spec(encoder: &mut CanonicalEncoder, entry: &FleetComponentSpecEntry) {
    encoder.string(entry.component_spec.as_str());
    encoder.bytes(&entry.spec_hash);
    encoder.string(entry.component_role.as_str());
    encoder.u32(entry.maximum_fleet_instances);
}

fn encode_root(encoder: &mut CanonicalEncoder, root: &FleetSubnetRootEntry) {
    encoder.bytes(root.placement_subnet.as_principal().as_slice());
    encoder.bytes(root.fleet_subnet_root.as_slice());
    encoder.u64(root.component_admissions.len() as u64);
    for admission in &root.component_admissions {
        encode_admission(encoder, admission);
    }
    encoder.bytes(root.component_topology_digest.as_bytes());
    encoder.bytes(root.active_release_set.release_build_id.as_bytes());
    encoder.bytes(root.active_release_set.manifest_digest.as_bytes());
    encode_limits(encoder, &root.limits);
    encoder.u8(status_tag(root.status));
}

fn encode_admission(encoder: &mut CanonicalEncoder, admission: &ComponentSpecAdmission) {
    encoder.string(admission.component_spec.as_str());
    encoder.bytes(&admission.spec_hash);
    encoder.u32(admission.maximum_root_instances);
}

fn encode_limits(encoder: &mut CanonicalEncoder, limits: &FleetSubnetRootLimits) {
    encoder.u32(limits.maximum_component_instances);
    encoder.u64(limits.maximum_registry_bytes);
    encoder.u64(limits.maximum_wasm_store_bytes);
    encoder.u32(limits.canister_pool.minimum_size);
    encoder.u32(limits.canister_pool.maximum_size);
    encoder.u128(limits.canister_pool.canister_cycles.to_u128());
    encoder.u64(limits.cycles_funding.window_secs);
    encoder.u128(limits.cycles_funding.maximum_cycles.to_u128());
    encoder.u32(limits.maximum_group_placements);
}

fn encode_service(encoder: &mut CanonicalEncoder, service: &FleetServiceBinding) {
    encoder.string(service.service.as_str());
    encoder.string(service.role.as_str());
    encoder.string(service.component_spec.as_str());
    encoder.u8(service_mode_tag(service.mode));
    encode_service_placement(encoder, service.placement);
    encoder.u64(service.members.len() as u64);
    for member in &service.members {
        encode_service_member(encoder, member);
    }
}

fn encode_service_placement(
    encoder: &mut CanonicalEncoder,
    placement: FleetServicePlacementPolicy,
) {
    encoder.u32(placement.maximum_members_per_root);
    encoder.u32(placement.minimum_distinct_roots);
}

fn encode_service_member(encoder: &mut CanonicalEncoder, member: &FleetServiceComponentBinding) {
    encoder.u8(service_member_purpose_tag(member.member_purpose));
    encoder.bytes(member.component.as_bytes());
    encoder.bytes(member.fleet_subnet_root.as_slice());
    encoder.bytes(member.canister_id.as_slice());
    encoder.string(member.group_placement.deployment.as_str());
    encoder.u32(member.group_placement.ordinal);
    encoder.u64(member.member_path.len() as u64);
    for segment in member.member_path.as_slice() {
        encoder.string(segment.as_str());
    }
}

const fn service_mode_tag(mode: FleetServiceMode) -> u8 {
    match mode {
        FleetServiceMode::AuthorityReplica => 0,
        FleetServiceMode::ActivePool => 1,
    }
}

const fn service_member_purpose_tag(purpose: FleetServiceMemberPurpose) -> u8 {
    match purpose {
        FleetServiceMemberPurpose::Authority => 0,
        FleetServiceMemberPurpose::Replica => 1,
        FleetServiceMemberPurpose::PoolMember => 2,
    }
}

const fn status_tag(status: FleetSubnetRootStatus) -> u8 {
    match status {
        FleetSubnetRootStatus::Joining => 0,
        FleetSubnetRootStatus::Active => 1,
        FleetSubnetRootStatus::Draining => 2,
        FleetSubnetRootStatus::Removed => 3,
    }
}

struct CanonicalEncoder {
    bytes: Vec<u8>,
}

impl CanonicalEncoder {
    fn new() -> Self {
        let mut encoder = Self { bytes: Vec::new() };
        encoder.bytes(FLEET_REGISTRY_DOMAIN);
        encoder.u32(FLEET_REGISTRY_SCHEMA_VERSION);
        encoder
    }

    fn bytes(&mut self, value: &[u8]) {
        self.u64(value.len() as u64);
        self.bytes.extend_from_slice(value);
    }

    fn string(&mut self, value: &str) {
        self.bytes(value.as_bytes());
    }

    fn u8(&mut self, value: u8) {
        self.bytes.push(value);
    }

    fn u32(&mut self, value: u32) {
        self.bytes.extend_from_slice(&value.to_be_bytes());
    }

    fn u64(&mut self, value: u64) {
        self.bytes.extend_from_slice(&value.to_be_bytes());
    }

    fn u128(&mut self, value: u128) {
        self.bytes.extend_from_slice(&value.to_be_bytes());
    }

    fn finish(self) -> Result<Vec<u8>, FleetRegistryOpsError> {
        if self.bytes.len() > MAX_FLEET_REGISTRY_CANONICAL_BYTES {
            return Err(FleetRegistryOpsError::CanonicalBytesExceeded {
                actual_bytes: self.bytes.len(),
                maximum_bytes: MAX_FLEET_REGISTRY_CANONICAL_BYTES,
            });
        }
        Ok(self.bytes)
    }
}