Skip to main content

oxabl_cache_format/
limits.rs

1//! Size limits of the protocol, enforced by every decoder and encoder.
2
3use crate::error::{FormatError, Result};
4
5/// Lookup ids per request.
6pub const LOOKUP_IDS: usize = 10_000;
7/// Entries per commit.
8pub const COMMIT_ENTRIES: usize = 2_000;
9/// Segments per pack.
10pub const PACK_SEGMENTS: usize = 2_000;
11/// Pack object size in bytes.
12pub const PACK_BYTES: u64 = 32 * 1024 * 1024;
13/// Files per artifact set.
14pub const FILES_PER_SET: usize = 16;
15/// Length of one path in bytes.
16pub const PATH_BYTES: usize = 1_024;
17/// Total path bytes per artifact set.
18pub const SET_PATH_BYTES: usize = 3_072;
19/// Sealed record size in bytes.
20pub const SEALED_RECORD_BYTES: usize = 4_096;
21/// Sealed record lengths are multiples of this.
22pub const RECORD_ALIGN: usize = 256;
23/// Default plaintext per artifact set.
24pub const DEFAULT_PLAINTEXT_BYTES: u64 = 64 * 1024 * 1024;
25/// Upper bound of the configurable plaintext per artifact set.
26pub const MAX_PLAINTEXT_BYTES: u64 = 1024 * 1024 * 1024;
27/// Roster size in bytes.
28pub const ROSTER_BYTES: usize = 1024 * 1024;
29/// Members per roster.
30pub const ROSTER_MEMBERS: usize = 1_000;
31/// Signatures on a roster.
32pub const ROSTER_SIGNATURES: usize = 4;
33/// Characters in a Bech32 string.
34pub const BECH32_CHARS: usize = 1_024;
35
36/// Limits a reader applies that a deployment may configure.
37#[derive(Debug, Clone, Copy, PartialEq, Eq)]
38pub struct Limits {
39    plaintext_bytes: u64,
40}
41
42impl Limits {
43    /// Limits with the given plaintext cap per artifact set, at most [`MAX_PLAINTEXT_BYTES`].
44    pub fn new(plaintext_bytes: u64) -> Result<Self> {
45        if plaintext_bytes > MAX_PLAINTEXT_BYTES {
46            return Err(FormatError::LimitExceeded("plaintext per artifact set"));
47        }
48        Ok(Self { plaintext_bytes })
49    }
50
51    /// Plaintext cap per artifact set.
52    pub fn plaintext_bytes(&self) -> u64 {
53        self.plaintext_bytes
54    }
55}
56
57impl Default for Limits {
58    fn default() -> Self {
59        Self {
60            plaintext_bytes: DEFAULT_PLAINTEXT_BYTES,
61        }
62    }
63}