canic-host 0.110.29

Host-side App build and desired-state Fleet reconciliation library for Canic workspaces
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
//! Bind fresh local identities and reuse the maintained release initializer owner.

use crate::{
    durable_io,
    fleet_ensure::{
        model::{DesiredCanisterKind, DesiredFleet, DesiredPresence},
        ops as ensure_ops,
    },
    local_fleet::{LocalFleetError, model::*, view::LocalRootInstallationView},
};
use candid::Principal;
use canic_core::{cdk::utils::hash::sha256_hex, ids::CanonicalNetworkId};
use std::{
    collections::{BTreeMap, BTreeSet},
    path::Path,
};

fn failure(error: impl std::fmt::Display) -> LocalFleetError {
    LocalFleetError::Preparation(error.to_string())
}

/// Admit only a fresh, exact local two-Root source before allocating any canister.
pub fn begin(
    directory: &Path,
    workspace: &Path,
    record: &LocalFleetRecord,
    source: &DesiredFleet,
) -> Result<LocalPreparationRecord, LocalFleetError> {
    require_workspace(directory, workspace)?;
    if source.environment != super::environment_name(record) {
        return Err(LocalFleetError::Identity);
    }
    let bootstrap = source
        .bootstrap
        .as_ref()
        .ok_or(LocalFleetError::Configuration)?;
    if record
        .release_build_id
        .is_some_and(|release| release != bootstrap.release_build_id)
    {
        return Err(LocalFleetError::Identity);
    }
    let root_key =
        canic_core::cdk::utils::hash::decode_hex(&record.root_key_der_hex).map_err(failure)?;
    let network = CanonicalNetworkId::from_der_root_trust_anchor(&root_key).map_err(failure)?;
    if !bootstrap.fresh_estate
        || bootstrap.canonical_network_id != network
        || source.protocol.is_none()
    {
        return Err(LocalFleetError::Identity);
    }
    let subnets = bootstrap
        .roots
        .iter()
        .map(|root| root.placement_subnet)
        .collect::<BTreeSet<_>>();
    if subnets.len() < 2
        || source.canisters.len() > usize::from(record.configuration.maximum_canisters)
    {
        return Err(LocalFleetError::Capacity);
    }
    if source.canisters.iter().any(|canister| {
        canister.principal.is_some()
            || canister.presence != DesiredPresence::Present
            || canister.replace
    }) {
        return Err(LocalFleetError::Identity);
    }
    for canister in &source.canisters {
        allocation_input(record, source, &canister.name)?;
    }
    for existing in &record.allocations {
        if allocation_input(record, source, &existing.input.name)? != existing.input {
            return Err(LocalFleetError::Identity);
        }
    }
    let release =
        crate::release_build::load_finalized_release_build(workspace, bootstrap.release_build_id)
            .map_err(failure)?;
    if release.record.build_network != canic_core::ids::BuildNetwork::Local {
        return Err(LocalFleetError::Identity);
    }
    let source_sha256 = sha256_hex(&serde_json::to_vec(source)?);
    let workspace_root = workspace.canonicalize()?;
    let path = directory.join("preparation.json");
    match durable_io::read_regular_bytes(&path, 4 * 1024 * 1024) {
        Ok(bytes) => {
            let retained: LocalPreparationRecord = serde_json::from_slice(&bytes)?;
            if retained.schema_version != 1
                || retained.source_sha256 != source_sha256
                || retained.workspace_root != workspace_root
            {
                return Err(LocalFleetError::Identity);
            }
            Ok(retained)
        }
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
            let prepared = LocalPreparationRecord {
                schema_version: 1,
                source_sha256,
                workspace_root,
                prepared: None,
                roots: Vec::new(),
                complete: false,
            };
            save(directory, &prepared)?;
            Ok(prepared)
        }
        Err(error) => Err(error.into()),
    }
}

fn save(directory: &Path, record: &LocalPreparationRecord) -> Result<(), LocalFleetError> {
    let bytes = serde_json::to_vec_pretty(record)?;
    if bytes.len() > 4 * 1024 * 1024 {
        return Err(LocalFleetError::Capacity);
    }
    durable_io::write_bytes(&directory.join("preparation.json"), &bytes)?;
    Ok(())
}

/// Convert each named source role into one explicit simulator allocation input.
pub fn allocation_input(
    record: &LocalFleetRecord,
    source: &DesiredFleet,
    name: &str,
) -> Result<LocalAllocationInput, LocalFleetError> {
    let canister = source
        .canisters
        .iter()
        .find(|canister| canister.name == name)
        .ok_or(LocalFleetError::Identity)?;
    let subnet = Principal::from_text(&canister.subnet).map_err(failure)?;
    let index = record
        .application_subnets
        .iter()
        .position(|known| *known == subnet)
        .ok_or(LocalFleetError::Identity)?;
    let role = match canister.kind {
        DesiredCanisterKind::Coordinator => "fleet_coordinator",
        DesiredCanisterKind::Root => "root",
        DesiredCanisterKind::Store => "wasm_store",
        DesiredCanisterKind::Pool => "pool",
        _ => return Err(LocalFleetError::Configuration),
    };
    Ok(LocalAllocationInput {
        name: name.into(),
        role: role.into(),
        application_subnet: u8::try_from(index).map_err(failure)?,
        controller: Principal::from_text(&source.operator).map_err(failure)?,
    })
}

/// Bind source logical identities to the complete local allocation set before initialization.
pub fn bind(
    directory: &Path,
    record: &LocalFleetRecord,
    source: &DesiredFleet,
    preparation: &LocalPreparationRecord,
) -> Result<LocalPreparationRecord, LocalFleetError> {
    let mut desired = source.clone();
    let principals = record
        .allocations
        .iter()
        .map(|entry| {
            Ok((
                entry.input.name.clone(),
                entry
                    .canister_id
                    .ok_or(LocalFleetError::Identity)?
                    .to_text(),
            ))
        })
        .collect::<Result<BTreeMap<_, _>, LocalFleetError>>()?;
    if principals.len() != source.canisters.len() {
        return Err(LocalFleetError::Identity);
    }
    for canister in &mut desired.canisters {
        canister.principal = Some(
            principals
                .get(&canister.name)
                .ok_or(LocalFleetError::Identity)?
                .clone(),
        );
        for name in &canister.controller_canisters {
            canister.controllers.push(
                principals
                    .get(name)
                    .ok_or(LocalFleetError::Identity)?
                    .clone(),
            );
        }
        canister.controller_canisters.clear();
        canister.controllers.sort();
        canister.controllers.dedup();
    }
    if preparation
        .prepared
        .as_ref()
        .is_some_and(|previous| previous != &desired)
    {
        return Err(LocalFleetError::Identity);
    }
    let mut next = preparation.clone();
    next.prepared = Some(desired);
    save(directory, &next)?;
    Ok(next)
}

/// Compile exact Root initialization through the same release authority used by Fleet Ensure.
pub fn root_installations(
    preparation: &LocalPreparationRecord,
) -> Result<Vec<LocalRootInstallationView>, LocalFleetError> {
    let desired = preparation
        .prepared
        .as_ref()
        .ok_or(LocalFleetError::Identity)?;
    let (_, desired_sha256) = desired_document(desired)?;
    let operation_id = crate::fleet_ensure::policy::operation_id(
        &desired_sha256,
        &desired.environment,
        &desired.fleet,
    );
    let principals = desired
        .canisters
        .iter()
        .map(|canister| {
            Ok((
                canister.name.clone(),
                canister
                    .principal
                    .clone()
                    .ok_or(LocalFleetError::Identity)?,
            ))
        })
        .collect::<Result<BTreeMap<_, _>, LocalFleetError>>()?;
    let authorities =
        ensure_ops::compile_root_authorities(&preparation.workspace_root, desired, &principals)
            .map_err(failure)?;
    authorities
        .into_iter()
        .map(|(name, authority)| {
            let configured = desired
                .canisters
                .iter()
                .find(|canister| canister.name == name)
                .ok_or(LocalFleetError::Identity)?;
            let path = configured.wasm.as_ref().ok_or(LocalFleetError::Identity)?;
            let wasm = durable_io::read_regular_bytes(
                &preparation.workspace_root.join(path),
                32 * 1024 * 1024,
            )?;
            let wasm_sha256 = sha256_hex(&wasm);
            let arguments = ensure_ops::compile_arguments(&ensure_ops::CanicInitRequest {
                desired,
                init: configured
                    .canic_init
                    .as_ref()
                    .ok_or(LocalFleetError::Identity)?,
                operation_id: &operation_id,
                principals: &principals,
                root: &preparation.workspace_root,
                wasm: path,
                wasm_sha256: &wasm_sha256,
            })
            .map_err(failure)?;
            Ok(LocalRootInstallationView {
                canister_id: Principal::from_text(
                    principals.get(&name).ok_or(LocalFleetError::Identity)?,
                )
                .map_err(failure)?,
                name,
                operator: Principal::from_text(&desired.operator).map_err(failure)?,
                wasm,
                wasm_sha256,
                arguments_sha256: sha256_hex(&arguments),
                arguments,
                authority,
            })
        })
        .collect()
}

/// Serialize one current desired document; its byte hash also binds the existing Ensure operation ID.
pub fn desired_document(desired: &DesiredFleet) -> Result<(Vec<u8>, String), LocalFleetError> {
    let text = toml::to_string_pretty(desired).map_err(failure)?;
    let bytes = text.into_bytes();
    let digest = sha256_hex(&bytes);
    Ok((bytes, digest))
}

/// Reserve a bounded first-install attempt, or retain an already verified replay.
pub fn install_intent(
    directory: &Path,
    preparation: &LocalPreparationRecord,
    target: &LocalRootInstallationView,
    install_needed: bool,
) -> Result<LocalPreparationRecord, LocalFleetError> {
    let mut next = preparation.clone();
    let maximum = next
        .prepared
        .as_ref()
        .ok_or(LocalFleetError::Identity)?
        .maximum_stalled_observations;
    if let Some(entry) = next
        .roots
        .iter_mut()
        .find(|entry| entry.name == target.name)
    {
        if entry.wasm_sha256 != target.wasm_sha256
            || entry.arguments_sha256 != target.arguments_sha256
        {
            return Err(LocalFleetError::Identity);
        }
        if entry.verified && install_needed {
            return Err(LocalFleetError::Identity);
        }
        if install_needed {
            if entry.attempts >= maximum {
                return Err(LocalFleetError::Capacity);
            }
            entry.attempts += 1;
        }
    } else {
        if !install_needed {
            return Err(LocalFleetError::Identity);
        }
        if maximum == 0 {
            return Err(LocalFleetError::Capacity);
        }
        next.roots.push(LocalRootInstallRecord {
            name: target.name.clone(),
            wasm_sha256: target.wasm_sha256.clone(),
            arguments_sha256: target.arguments_sha256.clone(),
            attempts: 1,
            verified: false,
        });
    }
    save(directory, &next)?;
    Ok(next)
}

/// Mark only a Root whose installed authority was read and matched after submission.
pub fn verified_root(
    directory: &Path,
    preparation: &LocalPreparationRecord,
    name: &str,
) -> Result<LocalPreparationRecord, LocalFleetError> {
    let mut next = preparation.clone();
    next.roots
        .iter_mut()
        .find(|entry| entry.name == name)
        .ok_or(LocalFleetError::Identity)?
        .verified = true;
    save(directory, &next)?;
    Ok(next)
}

/// Retain the prepared desired bytes and freeze this local instance to their exact release.
pub fn finish(
    directory: &Path,
    record: &LocalFleetRecord,
    preparation: &LocalPreparationRecord,
) -> Result<LocalFleetRecord, LocalFleetError> {
    let desired = preparation
        .prepared
        .as_ref()
        .ok_or(LocalFleetError::Identity)?;
    let release = desired
        .bootstrap
        .as_ref()
        .ok_or(LocalFleetError::Identity)?
        .release_build_id;
    if record
        .release_build_id
        .is_some_and(|prior| prior != release)
    {
        return Err(LocalFleetError::Identity);
    }
    if preparation.roots.iter().any(|root| !root.verified) {
        return Err(LocalFleetError::Identity);
    }
    let (bytes, _) = desired_document(desired)?;
    durable_io::write_bytes(&directory.join("desired.toml"), &bytes)?;
    let mut next_preparation = preparation.clone();
    next_preparation.complete = true;
    save(directory, &next_preparation)?;
    let mut next = record.clone();
    next.release_build_id = Some(release);
    super::write_record(directory, &next)?;
    Ok(next)
}

/// Resolve a declared controller change from the complete prepared identity map.
pub fn controllers(
    desired: &DesiredFleet,
    name: &str,
) -> Result<crate::local_fleet::view::LocalControllersView, LocalFleetError> {
    let canister = desired
        .canisters
        .iter()
        .find(|canister| canister.name == name)
        .ok_or(LocalFleetError::Identity)?;
    let mut controllers = canister
        .controllers
        .iter()
        .map(|text| Principal::from_text(text).map_err(failure))
        .collect::<Result<Vec<_>, _>>()?;
    controllers.sort();
    Ok(crate::local_fleet::view::LocalControllersView {
        canister_id: Principal::from_text(
            canister
                .principal
                .as_deref()
                .ok_or(LocalFleetError::Identity)?,
        )
        .map_err(failure)?,
        operator: Principal::from_text(&desired.operator).map_err(failure)?,
        controllers,
    })
}

/// Return the exact prepared input through Fleet Ensure's maintained document contract.
pub fn loaded(
    preparation: &LocalPreparationRecord,
) -> Result<crate::fleet_ensure::LoadedDesiredFleet, LocalFleetError> {
    let desired = preparation
        .prepared
        .clone()
        .ok_or(LocalFleetError::Identity)?;
    let (_, sha256) = desired_document(&desired)?;
    Ok(crate::fleet_ensure::LoadedDesiredFleet { desired, sha256 })
}

/// Enroll the observed local trust through the existing network owner.
pub fn enroll(
    directory: &Path,
    workspace: &Path,
    environment: &str,
    record: &LocalFleetRecord,
) -> Result<(), LocalFleetError> {
    require_workspace(directory, workspace)?;
    if environment != super::environment_name(record) {
        return Err(LocalFleetError::Identity);
    }
    let key =
        canic_core::cdk::utils::hash::decode_hex(&record.root_key_der_hex).map_err(failure)?;
    let path = directory.join("root-key.der");
    durable_io::write_bytes(&path, &key)?;
    crate::network::enroll_network(crate::network::NetworkEnrollmentOptions {
        workspace_root: workspace,
        environment,
        root_key: &path,
        fingerprint: &sha256_hex(&key),
    })
    .map_err(failure)?;
    Ok(())
}

/// Keep the deployment workspace bound to the same owner that holds this local session lock.
pub fn require_workspace(directory: &Path, workspace: &Path) -> Result<(), LocalFleetError> {
    if directory.ancestors().nth(3) != Some(workspace.canonicalize()?.as_path()) {
        return Err(LocalFleetError::Identity);
    }
    Ok(())
}