use super::{Result, invalid};
pub(super) const MAX_COMPACT_COUNT: usize = 12 * 1024 * 1024;
pub(super) const MAX_COMPACT_STATE_COUNT: usize = 1024 * 1024;
pub(super) const MIN_BLOB_ITEM_BYTES: usize = 1;
pub(super) const MIN_TREE_ITEM_BYTES: usize = 1;
pub(super) const MIN_TREE_ENTRY_BYTES: usize = 1 + 1 + 2 + 21;
pub(super) const MIN_STATE_COLUMN_BYTES: usize = 16 + 32 + 18;
pub(super) const MIN_STATE_PARENT_BYTES: usize = 32;
pub(super) const MIN_EXTRA_HEADER_BYTES: usize = 2;
pub(super) const MIN_LINEAGE_BYTES: usize = 1 + 16 + 32;
pub(super) const MIN_VERIFICATION_CUSTOM_BYTES: usize = 2;
pub(super) const MIN_PRINCIPAL_BYTES: usize = 2;
pub(super) const MIN_AGENT_BYTES: usize = 5;
pub(super) fn admit_count(
field: &str,
count: usize,
remaining: usize,
min_item_bytes: usize,
max_count: usize,
) -> Result<()> {
if count > max_count {
return Err(invalid(format!(
"{field} count {count} exceeds maximum {max_count}"
)));
}
let min_item_bytes = min_item_bytes.max(1);
match count.checked_mul(min_item_bytes) {
Some(needed) if needed <= remaining => Ok(()),
_ => Err(invalid(format!(
"{field} count {count} exceeds remaining frame bytes {remaining} at {min_item_bytes} bytes per item"
))),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn pack_output_cap_declaration_fails_before_any_alloc() {
let remaining = 1024 * 1024 * 1024;
let error = admit_count("blob frame", remaining, remaining, 1, MAX_COMPACT_COUNT)
.expect_err("1 GiB declared count must not be admitted");
assert!(
error.to_string().contains("exceeds maximum"),
"count gate must fire before with_capacity, got {error}"
);
}
#[test]
fn encoded_floor_rejects_count_that_fits_remaining_bytes() {
let error = admit_count(
"tree entry",
5,
100,
MIN_TREE_ENTRY_BYTES,
MAX_COMPACT_COUNT,
)
.expect_err("1-byte remaining() floor is not enough");
assert!(error.to_string().contains("bytes per item"), "got {error}");
}
#[test]
fn encoded_floor_accepts_count_that_fits() {
admit_count(
"tree entry",
4,
4 * MIN_TREE_ENTRY_BYTES,
MIN_TREE_ENTRY_BYTES,
MAX_COMPACT_COUNT,
)
.expect("exact floor must be admitted");
}
}