canic-cli 0.92.2

Operator CLI for Canic fleet setup, builds, evidence, catalog, backup, and restore workflows
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
use canic_core::cdk::utils::hash::{decode_hex, hex_bytes, sha256_bytes};
use rustix::fs::{FlockOperation, flock};
use serde::{Deserialize, Serialize};
use std::{
    fmt, fs, io,
    io::Write,
    path::{Path, PathBuf},
    time::{SystemTime, UNIX_EPOCH},
};

const PENDING_OPERATION_LOG_SCHEMA_VERSION: u32 = 1;
const PENDING_OPERATION_ENTRY_SCHEMA_VERSION: u32 = 1;
const ICP_REFILL_COMMAND_KIND: &str = "icp.refill.v1";
const CYCLES_CONVERT_COMMAND: &str = "canic cycles convert";
const OPERATION_ID_SOURCE_GENERATED: &str = "generated";
const STATUS_COMPLETED: &str = "completed";
const STATUS_PENDING_SEND: &str = "pending_send";

///
/// PendingIcpRefillOperationInput
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) struct PendingIcpRefillOperationInput<'a> {
    pub(super) icp_root: &'a Path,
    pub(super) network: &'a str,
    pub(super) deployment: &'a str,
    pub(super) source: Option<&'a str>,
    pub(super) source_canister_id: &'a str,
    pub(super) source_subaccount: Option<[u8; 32]>,
    pub(super) target: Option<&'a str>,
    pub(super) target_canister_id: &'a str,
    pub(super) amount_e8s: u64,
    pub(super) created_at_unix_nanos: u128,
}

///
/// PendingOperationReserveResult
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub(super) struct PendingOperationReserveResult {
    pub(super) operation_id: [u8; 32],
    pub(super) operation_key: String,
    pub(super) reused: bool,
    pub(super) path: PathBuf,
}

///
/// PendingOperationLogError
///

#[derive(Debug)]
pub(super) struct PendingOperationLogError {
    path: PathBuf,
    message: String,
}

impl PendingOperationLogError {
    fn new(path: PathBuf, message: impl Into<String>) -> Self {
        Self {
            path,
            message: message.into(),
        }
    }
}

impl fmt::Display for PendingOperationLogError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "{} ({})", self.message, self.path.display())
    }
}

impl std::error::Error for PendingOperationLogError {}

///
/// PendingOperationLog
///

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
struct PendingOperationLog {
    schema_version: u32,
    operations: Vec<PendingOperationRecord>,
}

impl PendingOperationLog {
    const fn empty() -> Self {
        Self {
            schema_version: PENDING_OPERATION_LOG_SCHEMA_VERSION,
            operations: Vec::new(),
        }
    }
}

///
/// PendingOperationRecord
///

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
struct PendingOperationRecord {
    schema_version: u32,
    command_kind: String,
    operation_key: String,
    operation_id: String,
    operation_id_source: String,
    status: String,
    cli_command: String,
    network: String,
    deployment: String,
    source: Option<String>,
    source_canister_id: String,
    source_subaccount: Option<String>,
    target: Option<String>,
    target_canister_id: String,
    amount_e8s: u64,
    created_at_unix_nanos: String,
    completed_at_unix_nanos: Option<String>,
}

///
/// PendingOperationLogLock
///

struct PendingOperationLogLock {
    _file: fs::File,
}

impl PendingOperationLogLock {
    fn acquire(path: &Path) -> Result<Self, PendingOperationLogError> {
        let Some(parent) = path.parent() else {
            return Err(PendingOperationLogError::new(
                path.to_path_buf(),
                "pending operation log path has no parent",
            ));
        };
        fs::create_dir_all(parent).map_err(|err| {
            PendingOperationLogError::new(
                parent.to_path_buf(),
                format!("failed to create pending operation log directory: {err}"),
            )
        })?;
        let lock_path = pending_operation_lock_path(path);
        let file = fs::OpenOptions::new()
            .read(true)
            .write(true)
            .create(true)
            .truncate(false)
            .open(&lock_path)
            .map_err(|err| {
                PendingOperationLogError::new(
                    lock_path.clone(),
                    format!("failed to open pending operation log lock: {err}"),
                )
            })?;
        flock(&file, FlockOperation::LockExclusive).map_err(|err| {
            PendingOperationLogError::new(
                lock_path,
                format!("failed to lock pending operation log: {err}"),
            )
        })?;
        Ok(Self { _file: file })
    }
}

pub(super) fn reserve_pending_icp_refill_operation(
    input: &PendingIcpRefillOperationInput<'_>,
    generated_operation_id: [u8; 32],
) -> Result<PendingOperationReserveResult, PendingOperationLogError> {
    let path = pending_operation_log_path(input.icp_root);
    let _lock = PendingOperationLogLock::acquire(&path)?;
    let mut log = read_pending_operation_log(&path)?;
    let operation_key = icp_refill_operation_key(input);

    if let Some(record) = log.operations.iter().rev().find(|record| {
        record.operation_key == operation_key && record.status == STATUS_PENDING_SEND
    }) {
        return Ok(PendingOperationReserveResult {
            operation_id: parse_logged_operation_id(&path, &record.operation_id)?,
            operation_key,
            reused: true,
            path,
        });
    }

    log.operations.push(PendingOperationRecord {
        schema_version: PENDING_OPERATION_ENTRY_SCHEMA_VERSION,
        command_kind: ICP_REFILL_COMMAND_KIND.to_string(),
        operation_key: operation_key.clone(),
        operation_id: hex_bytes(generated_operation_id),
        operation_id_source: OPERATION_ID_SOURCE_GENERATED.to_string(),
        status: STATUS_PENDING_SEND.to_string(),
        cli_command: CYCLES_CONVERT_COMMAND.to_string(),
        network: input.network.to_string(),
        deployment: input.deployment.to_string(),
        source: input.source.map(ToOwned::to_owned),
        source_canister_id: input.source_canister_id.to_string(),
        source_subaccount: input.source_subaccount.map(hex_bytes),
        target: input.target.map(ToOwned::to_owned),
        target_canister_id: input.target_canister_id.to_string(),
        amount_e8s: input.amount_e8s,
        created_at_unix_nanos: input.created_at_unix_nanos.to_string(),
        completed_at_unix_nanos: None,
    });
    write_pending_operation_log(&path, &log)?;

    Ok(PendingOperationReserveResult {
        operation_id: generated_operation_id,
        operation_key,
        reused: false,
        path,
    })
}

pub(super) fn complete_pending_icp_refill_operation(
    icp_root: &Path,
    operation_key: &str,
    operation_id: [u8; 32],
    completed_at_unix_nanos: u128,
) -> Result<(), PendingOperationLogError> {
    let path = pending_operation_log_path(icp_root);
    let _lock = PendingOperationLogLock::acquire(&path)?;
    let mut log = read_pending_operation_log(&path)?;
    let operation_id = hex_bytes(operation_id);
    let Some(record) = log.operations.iter_mut().rev().find(|record| {
        record.operation_key == operation_key
            && record.operation_id == operation_id
            && record.status == STATUS_PENDING_SEND
    }) else {
        return Ok(());
    };
    record.status = STATUS_COMPLETED.to_string();
    record.completed_at_unix_nanos = Some(completed_at_unix_nanos.to_string());
    write_pending_operation_log(&path, &log)
}

fn pending_operation_log_path(icp_root: &Path) -> PathBuf {
    icp_root
        .join(".canic")
        .join("operations")
        .join("pending.json")
}

fn pending_operation_lock_path(log_path: &Path) -> PathBuf {
    let file_name = log_path
        .file_name()
        .and_then(|file_name| file_name.to_str())
        .unwrap_or("pending.json");
    log_path.with_file_name(format!("{file_name}.lock"))
}

fn read_pending_operation_log(
    path: &Path,
) -> Result<PendingOperationLog, PendingOperationLogError> {
    match fs::read_to_string(path) {
        Ok(source) => {
            let log = serde_json::from_str::<PendingOperationLog>(&source).map_err(|err| {
                PendingOperationLogError::new(
                    path.to_path_buf(),
                    format!("pending operation log is not valid JSON: {err}"),
                )
            })?;
            if log.schema_version != PENDING_OPERATION_LOG_SCHEMA_VERSION {
                return Err(PendingOperationLogError::new(
                    path.to_path_buf(),
                    format!(
                        "unsupported pending operation log schema {}, expected {}",
                        log.schema_version, PENDING_OPERATION_LOG_SCHEMA_VERSION
                    ),
                ));
            }
            Ok(log)
        }
        Err(err) if err.kind() == io::ErrorKind::NotFound => Ok(PendingOperationLog::empty()),
        Err(err) => Err(PendingOperationLogError::new(
            path.to_path_buf(),
            format!("failed to read pending operation log: {err}"),
        )),
    }
}

fn write_pending_operation_log(
    path: &Path,
    log: &PendingOperationLog,
) -> Result<(), PendingOperationLogError> {
    let Some(parent) = path.parent() else {
        return Err(PendingOperationLogError::new(
            path.to_path_buf(),
            "pending operation log path has no parent",
        ));
    };
    fs::create_dir_all(parent).map_err(|err| {
        PendingOperationLogError::new(
            parent.to_path_buf(),
            format!("failed to create pending operation log directory: {err}"),
        )
    })?;
    let data = serde_json::to_string_pretty(log).map_err(|err| {
        PendingOperationLogError::new(
            path.to_path_buf(),
            format!("failed to serialize pending operation log: {err}"),
        )
    })?;
    let temp_path = pending_temp_path(path);
    {
        let mut temp = fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .open(&temp_path)
            .map_err(|err| {
                PendingOperationLogError::new(
                    temp_path.clone(),
                    format!("failed to create pending operation temp file: {err}"),
                )
            })?;
        temp.write_all(data.as_bytes()).map_err(|err| {
            PendingOperationLogError::new(
                temp_path.clone(),
                format!("failed to write pending operation temp file: {err}"),
            )
        })?;
        temp.sync_all().map_err(|err| {
            PendingOperationLogError::new(
                temp_path.clone(),
                format!("failed to sync pending operation temp file: {err}"),
            )
        })?;
    }
    fs::rename(&temp_path, path).map_err(|err| {
        PendingOperationLogError::new(
            path.to_path_buf(),
            format!("failed to replace pending operation log: {err}"),
        )
    })?;
    sync_directory(parent).map_err(|err| {
        PendingOperationLogError::new(
            parent.to_path_buf(),
            format!("failed to sync pending operation log directory: {err}"),
        )
    })
}

fn pending_temp_path(path: &Path) -> PathBuf {
    let file_name = path
        .file_name()
        .and_then(|file_name| file_name.to_str())
        .unwrap_or("pending.json");
    path.with_file_name(format!(
        "{file_name}.tmp.{}.{}",
        std::process::id(),
        current_unix_nanos()
    ))
}

fn sync_directory(path: &Path) -> io::Result<()> {
    fs::File::open(path).and_then(|dir| dir.sync_all())
}

fn parse_logged_operation_id(
    path: &Path,
    value: &str,
) -> Result<[u8; 32], PendingOperationLogError> {
    let bytes = decode_hex(value).map_err(|err| {
        PendingOperationLogError::new(
            path.to_path_buf(),
            format!("pending operation log has invalid operation_id: {err}"),
        )
    })?;
    <[u8; 32]>::try_from(bytes.as_slice()).map_err(|_| {
        PendingOperationLogError::new(
            path.to_path_buf(),
            format!(
                "pending operation log has invalid operation_id length: expected 32 bytes, got {}",
                bytes.len()
            ),
        )
    })
}

fn icp_refill_operation_key(input: &PendingIcpRefillOperationInput<'_>) -> String {
    let mut bytes = Vec::new();
    extend_key_part(&mut bytes, b"canic:pending-operation:icp-refill:v1");
    extend_key_part(&mut bytes, input.network.as_bytes());
    extend_key_part(&mut bytes, input.deployment.as_bytes());
    extend_key_part(&mut bytes, input.source_canister_id.as_bytes());
    extend_key_part(&mut bytes, input.target_canister_id.as_bytes());
    extend_optional_key_part(
        &mut bytes,
        input.source_subaccount.as_ref().map(AsRef::as_ref),
    );
    extend_key_part(&mut bytes, &input.amount_e8s.to_be_bytes());
    hex_bytes(sha256_bytes(&bytes))
}

fn extend_optional_key_part(bytes: &mut Vec<u8>, part: Option<&[u8]>) {
    match part {
        Some(part) => {
            bytes.push(1);
            extend_key_part(bytes, part);
        }
        None => bytes.push(0),
    }
}

fn extend_key_part(bytes: &mut Vec<u8>, part: &[u8]) {
    bytes.extend_from_slice(&(part.len() as u64).to_be_bytes());
    bytes.extend_from_slice(part);
}

fn current_unix_nanos() -> u128 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_or(0, |duration| duration.as_nanos())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_support::temp_dir;
    use std::sync::{Arc, Barrier};

    #[test]
    fn pending_log_path_is_project_local() {
        assert_eq!(
            pending_operation_log_path(Path::new("/tmp/canic")),
            PathBuf::from("/tmp/canic/.canic/operations/pending.json")
        );
    }

    #[test]
    fn reserve_writes_generated_operation_before_send() {
        let root = temp_dir("canic-cli-pending-operation-write");
        let input = sample_input(&root);
        let operation_id = [7; 32];
        let result = reserve_pending_icp_refill_operation(&input, operation_id)
            .expect("reserve pending operation");

        assert!(!result.reused);
        assert_eq!(result.operation_id, operation_id);
        assert!(result.path.is_file());

        let log = read_pending_operation_log(&result.path).expect("read log");
        assert_eq!(log.schema_version, 1);
        assert_eq!(log.operations.len(), 1);
        let record = &log.operations[0];
        assert_eq!(record.command_kind, ICP_REFILL_COMMAND_KIND);
        assert_eq!(record.operation_id, hex_bytes(operation_id));
        assert_eq!(record.operation_id_source, OPERATION_ID_SOURCE_GENERATED);
        assert_eq!(record.status, STATUS_PENDING_SEND);
        assert_eq!(record.cli_command, CYCLES_CONVERT_COMMAND);
        assert_eq!(record.network, "ic");
        assert_eq!(record.deployment, "demo");
        assert_eq!(record.source_canister_id, "source-canister");
        assert_eq!(record.target_canister_id, "target-canister");
        assert_eq!(record.amount_e8s, 100_000_000);
    }

    #[test]
    fn reserve_reuses_matching_pending_send_operation() {
        let root = temp_dir("canic-cli-pending-operation-reuse");
        let input = sample_input(&root);
        let first =
            reserve_pending_icp_refill_operation(&input, [3; 32]).expect("reserve first operation");
        let second = reserve_pending_icp_refill_operation(&input, [4; 32])
            .expect("reserve second operation");

        assert!(!first.reused);
        assert!(second.reused);
        assert_eq!(second.operation_id, [3; 32]);

        let log = read_pending_operation_log(&second.path).expect("read log");
        assert_eq!(log.operations.len(), 1);
    }

    #[test]
    fn concurrent_reservations_share_one_pending_operation() {
        const WORKERS: usize = 8;

        let root = Arc::new(temp_dir("canic-cli-pending-operation-concurrent"));
        let barrier = Arc::new(Barrier::new(WORKERS));
        #[expect(
            clippy::needless_collect,
            reason = "all workers must be spawned before any barrier participant is joined"
        )]
        let handles = (0..WORKERS)
            .map(|index| {
                let root = Arc::clone(&root);
                let barrier = Arc::clone(&barrier);
                std::thread::spawn(move || {
                    let input = sample_input(&root);
                    barrier.wait();
                    let operation_byte = u8::try_from(index).expect("worker index fits in u8");
                    reserve_pending_icp_refill_operation(&input, [operation_byte; 32])
                        .expect("reserve concurrent operation")
                })
            })
            .collect::<Vec<_>>();
        let results = handles
            .into_iter()
            .map(|handle| handle.join().expect("join reservation worker"))
            .collect::<Vec<_>>();

        let operation_id = results[0].operation_id;
        assert!(
            results
                .iter()
                .all(|result| result.operation_id == operation_id)
        );
        assert_eq!(results.iter().filter(|result| !result.reused).count(), 1);
        let log = read_pending_operation_log(&results[0].path).expect("read concurrent log");
        assert_eq!(log.operations.len(), 1);
    }

    #[test]
    fn completed_pending_operation_is_not_reused() {
        let root = temp_dir("canic-cli-pending-operation-completed");
        let input = sample_input(&root);
        let first =
            reserve_pending_icp_refill_operation(&input, [3; 32]).expect("reserve first operation");
        complete_pending_icp_refill_operation(&root, &first.operation_key, [3; 32], 222)
            .expect("complete operation");

        let second = reserve_pending_icp_refill_operation(&input, [4; 32])
            .expect("reserve second operation");

        assert!(!second.reused);
        assert_eq!(second.operation_id, [4; 32]);

        let log = read_pending_operation_log(&second.path).expect("read log");
        assert_eq!(log.operations.len(), 2);
        assert_eq!(log.operations[0].status, STATUS_COMPLETED);
        assert_eq!(
            log.operations[0].completed_at_unix_nanos.as_deref(),
            Some("222")
        );
        assert_eq!(log.operations[1].status, STATUS_PENDING_SEND);
    }

    fn sample_input(root: &Path) -> PendingIcpRefillOperationInput<'_> {
        PendingIcpRefillOperationInput {
            icp_root: root,
            network: "ic",
            deployment: "demo",
            source: Some("funding_hub"),
            source_canister_id: "source-canister",
            source_subaccount: None,
            target: Some("app"),
            target_canister_id: "target-canister",
            amount_e8s: 100_000_000,
            created_at_unix_nanos: 111,
        }
    }
}