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