ic-bn-lib 0.8.1

Internet Computer Boundary Nodes shared modules
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
// Needed for certain macros
#![recursion_limit = "256"]
#![warn(clippy::nursery)]
#![warn(tail_expr_drop_order)]
#![allow(clippy::cognitive_complexity)]
#![allow(clippy::field_reassign_with_default)]
#![allow(clippy::collapsible_if)]

#[cfg(feature = "custom-domains")]
pub mod custom_domains;
pub mod dns;
pub mod health;
pub mod http;
pub mod ic;
#[cfg(feature = "lb")]
pub mod lb;
pub mod network;
#[cfg(feature = "pubsub")]
pub mod pubsub;
#[cfg(all(target_os = "linux", feature = "sev-snp"))]
pub mod sev_snp;
#[cfg(feature = "smtp")]
pub mod smtp;
pub mod tasks;
pub mod tests;
pub mod tls;
#[cfg(feature = "vector")]
pub mod vector;

use std::{fmt::Display, fs::File, net::IpAddr, path::Path, time::Duration};

use anyhow::{Context, anyhow};
use bytes::Bytes;
use candid::Principal;
use futures::StreamExt;
use serde::{Deserialize, Serialize};
use strum::{Display, EnumString, IntoStaticStr};
use tokio::io::AsyncWriteExt;

pub use hickory_proto;
pub use hickory_resolver;
pub use hyper;
pub use hyper_util;
pub use ic_agent;
pub use ic_transport_types;
pub use ipnet;
#[cfg(feature = "smtp")]
pub use mail_auth;
pub use prometheus;
#[cfg(feature = "acme")]
pub use rcgen;
pub use reqwest;
pub use rustls;
#[cfg(feature = "acme-alpn")]
pub use rustls_acme;
pub use show_option;
pub use uuid;

/// Converts a string representation to an `EmailAddress`. Panics when an error occurs.
#[macro_export]
macro_rules! email {
    ($email:expr) => {{ $crate::smtp::address::EmailAddress::from_text($email).unwrap() }};
}

/// Converts a string representation to a `candid::Principal`. Panics when an error occurs.
#[macro_export]
macro_rules! principal {
    ($id:expr) => {{ candid::Principal::from_text($id).unwrap() }};
}

#[doc(hidden)]
pub use regex as __regex;

/// Converts a string representation to a `regex::Regex`. Panics when an error occurs.
/// Regex crate also has a regex! macro, but it returns a reference to a static value.
#[macro_export]
macro_rules! regex {
    ($id:expr) => {{ $crate::__regex::Regex::new($id).unwrap() }};
}

/// tdb26-jop6k-aogll-7ltgs-eruif-6kk7m-qpktf-gdiqx-mxtrf-vb5e6-eqe
pub const MAINNET_ROOT_SUBNET_ID: Principal = Principal::from_slice(&[
    207, 242, 128, 227, 45, 127, 92, 205, 34, 70, 136, 47, 148, 175, 178, 15, 84, 202, 97, 162, 23,
    101, 231, 18, 212, 61, 39, 137, 2,
]);

/// Generic error
#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error(transparent)]
    Generic(#[from] anyhow::Error),
}

/// Error to be used with `retry_async` macro
/// which indicates whether it should be retried or not.
#[derive(thiserror::Error, Debug)]
pub enum RetryError {
    #[error("Permanent error: {0:?}")]
    Permanent(anyhow::Error),
    #[error("Transient error: {0:?}")]
    Transient(anyhow::Error),
}

/// Type of IC API request
#[derive(
    Debug,
    Default,
    Clone,
    Copy,
    Display,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
    IntoStaticStr,
    EnumString,
    Serialize,
    Deserialize,
)]
#[strum(serialize_all = "snake_case")]
#[serde(rename_all = "snake_case")]
pub enum RequestType {
    #[default]
    Unknown,
    Status,
    QueryV2,
    QueryV3,
    QuerySubnetV3,
    CallV2,
    CallV3,
    CallV4,
    CallSubnetV4,
    ReadStateV2,
    ReadStateV3,
    ReadStateSubnetV2,
    ReadStateSubnetV3,
}

impl RequestType {
    pub const fn is_query(&self) -> bool {
        matches!(self, Self::QueryV2 | Self::QueryV3 | Self::QuerySubnetV3)
    }

    pub const fn is_call(&self) -> bool {
        matches!(
            self,
            Self::CallV2 | Self::CallV3 | Self::CallV4 | Self::CallSubnetV4
        )
    }

    pub const fn is_read_state(&self) -> bool {
        matches!(
            self,
            Self::ReadStateV2
                | Self::ReadStateV3
                | Self::ReadStateSubnetV2
                | Self::ReadStateSubnetV3
        )
    }
}

/// Parses size string as a binary (1k = 1024 etc) in u64
pub fn parse_size(s: &str) -> Result<u64, parse_size::Error> {
    parse_size::Config::new().with_binary().parse_size(s)
}

/// Parses size string as a binary (1k = 1024 etc) in usize
pub fn parse_size_usize(s: &str) -> Result<usize, parse_size::Error> {
    parse_size(s).map(|x| x as usize)
}

/// Parses size string as a decimal (1k = 1000 etc) in u64
pub fn parse_size_decimal(s: &str) -> Result<u64, parse_size::Error> {
    parse_size::Config::new().parse_size(s)
}

/// Parses size string as a decimal (1k = 1000 etc) in usize
pub fn parse_size_decimal_usize(s: &str) -> Result<usize, parse_size::Error> {
    parse_size_decimal(s).map(|x| x as usize)
}

/// Downloads the given url to given path.
/// Destination folder must exist.
pub fn download_url_to(url: &str, path: &Path) -> Result<u64, Error> {
    let mut r = reqwest::blocking::get(url).context("unable to perform HTTP request")?;
    if !r.status().is_success() {
        return Err(anyhow!("incorrect HTTP code: {}", r.status()).into());
    }

    let mut file = File::create(path).context("could not create file")?;
    Ok(r.copy_to(&mut file)
        .context("unable to write body to file")?)
}

/// Downloads the given url and returns it as Bytes
pub fn download_url(url: &str) -> Result<Bytes, Error> {
    let r = reqwest::blocking::get(url).context("unable to perform HTTP request")?;
    if !r.status().is_success() {
        return Err(anyhow!("incorrect HTTP code: {}", r.status()).into());
    }

    Ok(r.bytes().context("unable to fetch file")?)
}

/// Downloads the given url to given path.
/// Destination folder must exist.
pub async fn download_url_to_async(url: &str, path: &Path) -> Result<(), Error> {
    let r = reqwest::get(url)
        .await
        .context("unable to perform HTTP request")?;
    if !r.status().is_success() {
        return Err(anyhow!("incorrect HTTP code: {}", r.status()).into());
    }

    let mut file = tokio::fs::File::create(path)
        .await
        .context("could not create file")?;

    let mut stream = r.bytes_stream();
    while let Some(v) = stream.next().await {
        file.write(&v.context("unable to read chunk")?)
            .await
            .context("unable to write chunk")?;
    }

    Ok(())
}

/// Downloads the given url and returns it as Bytes
pub async fn download_url_async(url: &str) -> Result<Bytes, Error> {
    let r = reqwest::get(url)
        .await
        .context("unable to perform HTTP request")?;

    if !r.status().is_success() {
        return Err(anyhow!("incorrect HTTP code: {}", r.status()).into());
    }

    Ok(r.bytes().await.context("unable to fetch file")?)
}

/// Retrying async closures/functions holding mutable references is a pain in Rust.
/// So, for now, we'll have to use a macro to work that around.
#[macro_export]
macro_rules! retry_async {
    ($f:expr, $timeout:expr, $delay:expr) => {{
        use rand::{Rng, SeedableRng};
        // SmallRng is Send which we require
        let mut rng = rand::rngs::SmallRng::from_entropy();

        let start = std::time::Instant::now();
        let mut delay = $delay;

        let result = loop {
            // Bound this attempt by the *remaining* budget, not the original
            // `$timeout` - otherwise a dependency that consistently takes
            // just under `$timeout` before failing can make the loop's total
            // wall-clock time run to several multiples of `$timeout`.
            let attempt_timeout = $timeout.saturating_sub(start.elapsed());
            if attempt_timeout == std::time::Duration::ZERO {
                break Err(anyhow::anyhow!("Timed out"));
            }

            // Run the function wrapping it into Tokio timeout future so
            // its execution time doesn't exceed our configured limit
            let Ok(res) = tokio::time::timeout(attempt_timeout, $f).await else {
                break Err(anyhow::anyhow!("Timed out"));
            };

            let err = match res {
                Ok(v) => break Ok(v),
                Err($crate::RetryError::Permanent(e)) => break Err(e),
                Err($crate::RetryError::Transient(e)) => e,
            };

            let left = $timeout.saturating_sub(start.elapsed());
            if left == std::time::Duration::ZERO {
                break Err(err);
            }

            delay = left.min(delay * 2);
            // Generate a random jitter in 0.0..0.1 range
            let jitter: f64 = (rng.r#gen::<f64>() / 10.0);
            let d64 = delay.as_secs_f64();
            delay = Duration::from_secs_f64(d64.mul_add(0.95, d64 * jitter));
            tokio::time::sleep(delay).await;
        };

        result
    }};

    ($f:expr, $timeout:expr) => {
        retry_async!($f, $timeout, Duration::from_millis(500))
    };

    ($f:expr) => {
        retry_async!($f, Duration::from_secs(60), Duration::from_millis(500))
    };
}

/// Returns family of an IP address
pub trait IpFamily {
    fn family(&self) -> &'static str;
}

impl IpFamily for IpAddr {
    fn family(&self) -> &'static str {
        if self.is_ipv4() { "v4" } else { "v6" }
    }
}

/// Converts bool to yes/no static str
pub trait BoolYesNo {
    fn yesno(&self) -> &'static str;
}

impl BoolYesNo for bool {
    fn yesno(&self) -> &'static str {
        if *self { "yes" } else { "no" }
    }
}

pub trait SerializeOption<T> {
    /// Serializes `Option<T>` as either inner value or provided default
    fn serialize_or<'t, O>(&'t self, otherwise: O) -> SerializeOr<'t, T, O>;
}

pub struct SerializeOr<'t, T, O> {
    option: &'t Option<T>,
    otherwise: O,
}

impl<'t, T: Serialize, O: Serialize> Serialize for SerializeOr<'t, T, O> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        match self.option {
            Some(v) => v.serialize(serializer),
            None => self.otherwise.serialize(serializer),
        }
    }
}

impl<T> SerializeOption<T> for Option<T> {
    fn serialize_or<'t, O>(&'t self, otherwise: O) -> SerializeOr<'t, T, O> {
        SerializeOr {
            option: self,
            otherwise,
        }
    }
}

#[macro_export]
macro_rules! dyn_event {
    ($lvl:ident, $($arg:tt)+) => {
        match $lvl {
            ::tracing::Level::TRACE => ::tracing::trace!($($arg)+),
            ::tracing::Level::DEBUG => ::tracing::debug!($($arg)+),
            ::tracing::Level::INFO => ::tracing::info!($($arg)+),
            ::tracing::Level::WARN => ::tracing::warn!($($arg)+),
            ::tracing::Level::ERROR => ::tracing::error!($($arg)+),
        }
    };
}

pub trait TruncatesString {
    /// Truncates the given string to around n *bytes*,
    /// on the closest UTF-8 code point boundary.
    fn truncate_bytes(&self, n: usize) -> &str;
}

impl TruncatesString for &str {
    fn truncate_bytes(&self, n: usize) -> &str {
        truncate(self, n)
    }
}

/// Truncates the given string to around n *bytes*,
/// on the closest UTF-8 code point boundary.
pub fn truncate(s: &str, n: usize) -> &str {
    let n = s.len().min(n);
    let m = (0..=n)
        .rfind(|m| s.is_char_boundary(*m))
        .unwrap_or_default();
    &s[..m]
}

/// Best-effort (w/o assembly) constant-time comparison for byte slices
pub fn constant_time_eq(a: &[u8], b: &[u8]) -> bool {
    if a.len() != b.len() {
        return false;
    }

    a.iter()
        .zip(b.iter())
        .fold(0u8, |acc, (x, y)| acc | (x ^ y))
        == 0
}

/// Implements display for Duration
pub trait DurationDisplay<'a> {
    /// Implements Display for Duration in a human-friendly format
    fn display(&self) -> DisplayDuration;
}

/// Displays Duration in a human-friendly format
pub struct DisplayDuration(Duration);

impl DurationDisplay<'_> for Duration {
    fn display(&self) -> DisplayDuration {
        DisplayDuration(*self)
    }
}

impl Display for DisplayDuration {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut secs = self.0.as_secs();
        // If the duration is shorter than a second - use a distinct approach
        if secs == 0 {
            let nanos = self.0.as_nanos();
            if nanos < 1000 {
                return write!(f, "{nanos}ns");
            }

            let micros = self.0.as_micros();
            if micros < 1000 {
                return write!(f, "{micros}us");
            }

            return write!(f, "{}ms", self.0.as_millis());
        }

        // If the duration is shorter than a minute,
        // use seconds with a fractional part.
        // Truncate (rather than round) the fraction so that e.g. 59.999s
        // doesn't get displayed as "60.00s".
        if secs < 60 {
            let hundredths = self.0.subsec_millis() / 10;
            return write!(f, "{secs}.{hundredths:02}s");
        }

        // Average year is 365.24 days...
        let years = secs / 365 / 86400;
        if years > 0 {
            secs -= years * 86400 * 365;
            write!(f, "{years}y")?;
        }

        let days = secs / 86400;
        if days > 0 {
            secs -= days * 86400;
            write!(f, "{days}d")?;
        }

        let hours = secs / 3600;
        if hours > 0 {
            secs -= hours * 3600;
            write!(f, "{hours}h")?;
        }

        let mins = secs / 60;
        if mins > 0 {
            secs -= mins * 60;
            write!(f, "{mins}m")?;
        }

        if secs > 0 {
            write!(f, "{secs}s")?;
        }

        Ok(())
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_truncate() {
        assert_eq!(truncate("foobarbaz", 4), "foob");
        assert_eq!(truncate("tättähäärä härkä", 12), "tättähää");
        assert_eq!(truncate("", 99), "");
        assert_eq!(truncate("🏁", 2), "");
        assert_eq!(truncate("foobarbaz", 99), "foobarbaz");

        assert_eq!("tättähäärä härkä".truncate_bytes(12), "tättähää");
    }

    #[tokio::test]
    async fn test_retry_async_bounds_total_time_to_timeout() {
        use std::time::Duration;

        let timeout = Duration::from_millis(300);
        let base_delay = Duration::from_millis(20);
        let attempt_duration = Duration::from_millis(200);

        let start = std::time::Instant::now();
        let result: Result<(), anyhow::Error> = retry_async! {
            async {
                tokio::time::sleep(attempt_duration).await;
                Err::<(), _>(RetryError::Transient(anyhow::anyhow!("always fails")))
            },
            timeout,
            base_delay
        };
        let elapsed = start.elapsed();

        assert!(result.is_err());
        // Each attempt (200ms) comfortably fits within the overall timeout
        // (300ms) on its own, so a per-attempt timeout that isn't shrunk to
        // the remaining budget lets a second full attempt run past the
        // deadline (previously observed ~440ms total here instead of
        // staying close to the configured 300ms).
        assert!(
            elapsed < timeout + Duration::from_millis(100),
            "retry_async! ran for {elapsed:?}, expected to stay close to the {timeout:?} budget"
        );
    }

    #[test]
    fn test_constant_time_eq() {
        assert!(constant_time_eq(b"foo", b"foo"));
        assert!(!constant_time_eq(b"foo", b"bar"));
        assert!(!constant_time_eq(b"foobar", b"bar"));
    }

    #[test]
    fn test_duration_display() {
        assert_eq!(
            Duration::from_secs(31626061).display().to_string(),
            "1y1d1h1m1s"
        );
        assert_eq!(Duration::from_hours(72).display().to_string(), "3d");
        assert_eq!(Duration::from_hours(80).display().to_string(), "3d8h");
        assert_eq!(Duration::from_mins(4812).display().to_string(), "3d8h12m");
        assert_eq!(
            Duration::from_secs(288735).display().to_string(),
            "3d8h12m15s"
        );
        assert_eq!(
            Duration::from_millis(288735123).display().to_string(),
            "3d8h12m15s"
        );

        // Sub-second formatting
        assert_eq!(Duration::ZERO.display().to_string(), "0ns");
        assert_eq!(Duration::from_nanos(999).display().to_string(), "999ns");
        assert_eq!(Duration::from_micros(1).display().to_string(), "1us");
        assert_eq!(Duration::from_micros(999).display().to_string(), "999us");
        // Exactly 1000us should roll over to the ms branch, not stay "1000us"
        assert_eq!(Duration::from_micros(1000).display().to_string(), "1ms");
        assert_eq!(Duration::from_millis(1).display().to_string(), "1ms");
        assert_eq!(Duration::from_millis(999).display().to_string(), "999ms");

        // Sub-minute fractional seconds
        assert_eq!(Duration::from_millis(1001).display().to_string(), "1.00s");
        assert_eq!(Duration::from_millis(59494).display().to_string(), "59.49s");
        assert_eq!(Duration::from_millis(59999).display().to_string(), "59.99s");

        // Minute/hour/day/year boundaries, exact and with skipped middle units
        assert_eq!(Duration::from_millis(60001).display().to_string(), "1m");
        assert_eq!(Duration::from_secs(60).display().to_string(), "1m");
        assert_eq!(Duration::from_secs(3600).display().to_string(), "1h");
        assert_eq!(Duration::from_secs(3605).display().to_string(), "1h5s");
        assert_eq!(Duration::from_secs(86400).display().to_string(), "1d");
        assert_eq!(Duration::from_secs(86400 + 5).display().to_string(), "1d5s");
        assert_eq!(Duration::from_secs(365 * 86400).display().to_string(), "1y");
    }
}