use bytes::Bytes;
use candid::Encode;
use sha2::{Digest, Sha256};
use std::time::Duration;
use crate::smtp::ic::candid::{
Envelope, Header, Message, SHA256_LEN, SMTP_UPLOAD_PROTOCOL_VERSION, SmtpRequest,
SmtpUploadChunk, SmtpUploadCommit, body_sha256,
};
#[derive(thiserror::Error, Clone, Debug, Eq, PartialEq)]
pub enum UploadPlanError {
#[error("unable to encode the message for size measurement: {0}")]
Encode(String),
#[error(
"header block is too large to upload: {overhead} bytes of overhead leaves \
only {available} bytes per chunk, need at least {needed}"
)]
HeadersTooLarge {
overhead: usize,
available: usize,
needed: usize,
},
#[error("cannot chunk an empty body")]
EmptyBody,
#[error("no envelope to measure the chunk size against")]
NoEnvelopes,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct IcSmtpUploadConfig {
pub max_ingress_size: usize,
pub chunk_size: usize,
pub max_inflight_bytes: usize,
pub global_concurrency: usize,
pub chunk_retries: usize,
pub delivery_timeout: Duration,
pub capabilities_cache_ttl: Duration,
}
impl Default for IcSmtpUploadConfig {
fn default() -> Self {
Self {
max_ingress_size: crate::ic::DEFAULT_MAX_INGRESS_MESSAGE_SIZE,
chunk_size: 1024 * 1024,
max_inflight_bytes: 2 * 1024 * 1024,
global_concurrency: 16,
chunk_retries: 2,
delivery_timeout: Duration::from_secs(90),
capabilities_cache_ttl: Duration::from_secs(600),
}
}
}
impl IcSmtpUploadConfig {
pub const fn concurrency(&self, chunk_size: usize) -> usize {
if chunk_size == 0 {
return 1;
}
let n = self.max_inflight_bytes / chunk_size;
if n == 0 { 1 } else { n }
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct UploadPlan {
pub chunk_size: usize,
pub total_chunks: u32,
pub body_size: usize,
pub payload_sha256: Vec<[u8; SHA256_LEN]>,
pub body_sha256: [u8; SHA256_LEN],
}
impl UploadPlan {
pub const fn range(&self, index: u32) -> (usize, usize) {
let start = (index as usize) * self.chunk_size;
let end = start.saturating_add(self.chunk_size);
(
if start > self.body_size {
self.body_size
} else {
start
},
if end > self.body_size {
self.body_size
} else {
end
},
)
}
}
const fn leb128_len(mut n: usize) -> usize {
let mut len = 1;
while n >= 0x80 {
n >>= 7;
len += 1;
}
len
}
pub fn single_shot_encoded_len(
headers: &[Header],
envelope: &Envelope,
message_id: &str,
body_len: usize,
) -> Result<usize, UploadPlanError> {
let probe = SmtpRequest {
message: Some(Message {
headers: headers.to_vec(),
body: vec![],
}),
envelope: Some(envelope.clone()),
gateway_flags: None,
message_id: Some(message_id.to_string()),
};
let base = Encode!(&probe)
.map_err(|e| UploadPlanError::Encode(e.to_string()))?
.len();
Ok(base - leb128_len(0) + leb128_len(body_len) + body_len)
}
fn chunk_overhead(
headers: &[Header],
envelope: &Envelope,
message_id: &str,
) -> Result<usize, UploadPlanError> {
let probe = SmtpUploadChunk {
version: SMTP_UPLOAD_PROTOCOL_VERSION,
message_id: message_id.to_string(),
envelope: envelope.clone(),
index: 0,
total_chunks: 0,
chunk_size: 0,
body_size: 0,
payload_sha256: vec![0; SHA256_LEN],
payload: vec![],
headers: Some(headers.to_vec()),
gateway_flags: None,
};
Ok(Encode!(&probe)
.map_err(|e| UploadPlanError::Encode(e.to_string()))?
.len())
}
const MIN_PAYLOAD_SHARE: usize = 8;
pub fn plan_chunks(
headers: &[Header],
envelopes: &[&Envelope],
message_id: &str,
body: &Bytes,
max_ingress_size: usize,
max_payload: usize,
) -> Result<UploadPlan, UploadPlanError> {
if body.is_empty() {
return Err(UploadPlanError::EmptyBody);
}
if envelopes.is_empty() {
return Err(UploadPlanError::NoEnvelopes);
}
let mut overhead = 0;
for envelope in envelopes {
overhead = overhead.max(chunk_overhead(headers, envelope, message_id)?);
}
let overhead = overhead.saturating_add(64);
let available = max_ingress_size.saturating_sub(overhead);
let needed = (max_ingress_size / MIN_PAYLOAD_SHARE).max(1);
if available < needed {
return Err(UploadPlanError::HeadersTooLarge {
overhead,
available,
needed,
});
}
let chunk_size = max_payload.min(available).max(1);
let total_chunks = body.len().div_ceil(chunk_size) as u32;
let mut payload_sha256 = Vec::with_capacity(total_chunks as usize);
for i in 0..total_chunks as usize {
let start = i * chunk_size;
let end = (start + chunk_size).min(body.len());
payload_sha256.push(Sha256::digest(&body[start..end]).into());
}
Ok(UploadPlan {
chunk_size,
total_chunks,
body_size: body.len(),
body_sha256: body_sha256(&payload_sha256),
payload_sha256,
})
}
pub fn build_commit(plan: &UploadPlan, message_id: &str) -> SmtpUploadCommit {
SmtpUploadCommit {
version: SMTP_UPLOAD_PROTOCOL_VERSION,
message_id: message_id.to_string(),
body_sha256: plan.body_sha256.to_vec(),
total_chunks: plan.total_chunks,
}
}
pub fn build_chunk(
plan: &UploadPlan,
index: u32,
body: &Bytes,
headers: &[Header],
envelope: &Envelope,
message_id: &str,
) -> SmtpUploadChunk {
let (start, end) = plan.range(index);
SmtpUploadChunk {
version: SMTP_UPLOAD_PROTOCOL_VERSION,
message_id: message_id.to_string(),
envelope: envelope.clone(),
index,
total_chunks: plan.total_chunks,
chunk_size: plan.chunk_size as u64,
body_size: plan.body_size as u64,
payload_sha256: plan.payload_sha256[index as usize].to_vec(),
payload: body[start..end].to_vec(),
headers: (index == 0).then(|| headers.to_vec()),
gateway_flags: None,
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::smtp::ic::candid::Address;
fn envelope(n: usize) -> Envelope {
Envelope {
from: Address {
user: "sender".into(),
domain: "example.com".into(),
},
to: (0..n)
.map(|i| Address {
user: format!("rcpt{i}"),
domain: "qoctq-giaaa-aaaaa-aaaea-cai.icp0.io".into(),
})
.collect(),
}
}
fn headers(n: usize) -> Vec<Header> {
(0..n)
.map(|i| Header {
name: format!("X-Header-{i}"),
value: format!(" value number {i} with some padding\n"),
})
.collect()
}
const MID: &str = "0193f0a1-2b3c-7d4e-8f90-a1b2c3d4e5f6";
#[test]
fn test_leb128_len() {
assert_eq!(leb128_len(0), 1);
assert_eq!(leb128_len(127), 1);
assert_eq!(leb128_len(128), 2);
assert_eq!(leb128_len(16383), 2);
assert_eq!(leb128_len(16384), 3);
assert_eq!(leb128_len(2_097_152), 4);
}
#[test]
fn test_single_shot_encoded_len_is_exact() {
let env = envelope(3);
let hdrs = headers(12);
for body_len in [0, 1, 127, 128, 255, 16383, 16384, 100_000] {
let predicted = single_shot_encoded_len(&hdrs, &env, MID, body_len).unwrap();
let actual = Encode!(&SmtpRequest {
message: Some(Message {
headers: hdrs.clone(),
body: vec![0xab; body_len],
}),
envelope: Some(env.clone()),
gateway_flags: None,
message_id: Some(MID.to_string()),
})
.unwrap()
.len();
assert_eq!(predicted, actual, "body_len {body_len}");
}
}
#[test]
fn test_every_built_chunk_fits_the_ingress_budget() {
let env = envelope(10);
let budget = crate::ic::DEFAULT_MAX_INGRESS_MESSAGE_SIZE;
for header_count in [1, 50, 500, 2000] {
let hdrs = headers(header_count);
let body = Bytes::from(vec![0x5a; 9_000_000]);
let plan = plan_chunks(&hdrs, &[&env], MID, &body, budget, usize::MAX).unwrap();
for i in 0..plan.total_chunks {
let chunk = build_chunk(&plan, i, &body, &hdrs, &env, MID);
let encoded = Encode!(&chunk).unwrap().len();
assert!(
encoded <= budget,
"header_count {header_count}, chunk {i}: {encoded} > {budget}"
);
}
}
}
#[test]
fn test_chunk_boundaries() {
let env = envelope(1);
let hdrs = headers(2);
let budget = crate::ic::DEFAULT_MAX_INGRESS_MESSAGE_SIZE;
let body = Bytes::from(vec![1u8; 300_000]);
let plan = plan_chunks(&hdrs, &[&env], MID, &body, budget, 100_000).unwrap();
assert_eq!(plan.total_chunks, 3);
assert_eq!(plan.range(0), (0, 100_000));
assert_eq!(plan.range(2), (200_000, 300_000));
let body = Bytes::from(vec![1u8; 250_001]);
let plan = plan_chunks(&hdrs, &[&env], MID, &body, budget, 100_000).unwrap();
assert_eq!(plan.total_chunks, 3);
assert_eq!(plan.range(2), (200_000, 250_001));
let last = build_chunk(&plan, 2, &body, &hdrs, &env, MID);
assert_eq!(last.payload.len(), 50_001);
let body = Bytes::from(vec![1u8; 10]);
let plan = plan_chunks(&hdrs, &[&env], MID, &body, budget, 100_000).unwrap();
assert_eq!(plan.total_chunks, 1);
assert_eq!(plan.range(0), (0, 10));
let body = Bytes::from(vec![1u8; 100_000]);
let plan = plan_chunks(&hdrs, &[&env], MID, &body, budget, 100_000).unwrap();
assert_eq!(plan.total_chunks, 1);
}
#[test]
fn test_reassembly_and_digest_chain() {
let env = envelope(2);
let hdrs = headers(5);
let body = Bytes::from((0..250_003u32).map(|i| (i % 251) as u8).collect::<Vec<_>>());
let plan = plan_chunks(
&hdrs,
&[&env],
MID,
&body,
crate::ic::DEFAULT_MAX_INGRESS_MESSAGE_SIZE,
70_000,
)
.unwrap();
let mut reassembled = vec![0u8; plan.body_size];
let mut rolling = Sha256::new();
for i in 0..plan.total_chunks {
let chunk = build_chunk(&plan, i, &body, &hdrs, &env, MID);
let d: [u8; SHA256_LEN] = Sha256::digest(&chunk.payload).into();
assert_eq!(d.to_vec(), chunk.payload_sha256, "chunk {i}");
rolling.update(d);
let off = (chunk.index as usize) * (chunk.chunk_size as usize);
reassembled[off..off + chunk.payload.len()].copy_from_slice(&chunk.payload);
assert_eq!(chunk.total_chunks, plan.total_chunks);
assert_eq!(chunk.body_size, plan.body_size as u64);
assert_eq!(chunk.headers.is_some(), i == 0);
}
assert_eq!(reassembled, body.as_ref());
let derived: [u8; SHA256_LEN] = rolling.finalize().into();
assert_eq!(derived, plan.body_sha256);
}
#[test]
fn test_body_digest_distinguishes_messages() {
let env = envelope(1);
let hdrs = headers(1);
let budget = crate::ic::DEFAULT_MAX_INGRESS_MESSAGE_SIZE;
let a = Bytes::from(vec![0u8; 200_000]);
let mut b_vec = vec![0u8; 200_000];
b_vec[199_999] = 1;
let b = Bytes::from(b_vec);
let pa = plan_chunks(&hdrs, &[&env], MID, &a, budget, 100_000).unwrap();
let pb = plan_chunks(&hdrs, &[&env], MID, &b, budget, 100_000).unwrap();
assert_ne!(pa.body_sha256, pb.body_sha256);
}
#[test]
fn test_empty_body_is_rejected() {
assert_eq!(
plan_chunks(
&headers(1),
&[&envelope(1)],
MID,
&Bytes::new(),
crate::ic::DEFAULT_MAX_INGRESS_MESSAGE_SIZE,
100_000,
)
.unwrap_err(),
UploadPlanError::EmptyBody
);
}
#[test]
fn test_headers_too_large() {
let env = envelope(1);
let hdrs = (0..20_000)
.map(|i| Header {
name: format!("X-Pad-{i}"),
value: "x".repeat(100),
})
.collect::<Vec<_>>();
let err = plan_chunks(
&hdrs,
&[&env],
MID,
&Bytes::from(vec![0u8; 10_000_000]),
crate::ic::DEFAULT_MAX_INGRESS_MESSAGE_SIZE,
usize::MAX,
)
.unwrap_err();
assert!(
matches!(err, UploadPlanError::HeadersTooLarge { .. }),
"got {err:?}"
);
}
#[test]
fn test_max_payload_clamps_chunk_size() {
let plan = plan_chunks(
&headers(1),
&[&envelope(1)],
MID,
&Bytes::from(vec![0u8; 1_000_000]),
crate::ic::DEFAULT_MAX_INGRESS_MESSAGE_SIZE,
64 * 1024,
)
.unwrap();
assert_eq!(plan.chunk_size, 64 * 1024);
assert_eq!(plan.total_chunks, 1_000_000u32.div_ceil(65_536));
}
#[test]
fn test_concurrency_derivation() {
let cfg = IcSmtpUploadConfig::default();
assert_eq!(cfg.concurrency(1024 * 1024), 2);
assert_eq!(cfg.concurrency(256 * 1024), 8);
assert_eq!(cfg.concurrency(8 * 1024 * 1024), 1);
assert_eq!(cfg.concurrency(0), 1);
}
#[test]
fn test_chunk_size_equals_the_measured_budget_when_it_binds() {
let hdrs = headers(20);
let env = envelope(3);
let body = Bytes::from(vec![0u8; 2_000_000]);
let budget = 256 * 1024;
let plan = plan_chunks(&hdrs, &[&env], MID, &body, budget, usize::MAX).unwrap();
let overhead = chunk_overhead(&hdrs, &env, MID).unwrap() + 64;
assert_eq!(plan.chunk_size, budget - overhead);
let encoded = Encode!(&build_chunk(&plan, 0, &body, &hdrs, &env, MID))
.unwrap()
.len();
assert!(encoded <= budget, "{encoded} > {budget}");
}
#[test]
fn test_chunk_size_is_measured_against_the_tightest_envelope() {
let hdrs = headers(5);
let body = Bytes::from(vec![0u8; 2_000_000]);
let budget = 256 * 1024;
let narrow = envelope(1);
let wide = envelope(10);
let plan = |envs: &[&Envelope]| {
plan_chunks(&hdrs, envs, MID, &body, budget, usize::MAX)
.unwrap()
.chunk_size
};
let narrow_only = plan(&[&narrow]);
let wide_only = plan(&[&wide]);
assert!(
wide_only < narrow_only,
"a wider envelope must leave less payload: {wide_only} vs {narrow_only}"
);
assert_eq!(plan(&[&narrow, &wide]), wide_only);
assert_eq!(plan(&[&wide, &narrow]), wide_only);
}
#[test]
fn test_headers_leaving_a_tiny_payload_are_rejected() {
let budget = crate::ic::DEFAULT_MAX_INGRESS_MESSAGE_SIZE;
let env = envelope(1);
let hdrs = (0..950)
.map(|i| Header {
name: format!("X-Pad-{i}"),
value: "x".repeat(2000),
})
.collect::<Vec<_>>();
let available = budget - (chunk_overhead(&hdrs, &env, MID).unwrap() + 64);
assert!(
available > 0,
"precondition: the old guard would allow this"
);
assert!(available < budget / MIN_PAYLOAD_SHARE);
let err = plan_chunks(
&hdrs,
&[&env],
MID,
&Bytes::from(vec![0u8; 10_000_000]),
budget,
usize::MAX,
)
.unwrap_err();
assert!(
matches!(err, UploadPlanError::HeadersTooLarge { .. }),
"got {err:?}"
);
}
#[test]
fn test_no_envelopes_is_rejected() {
assert_eq!(
plan_chunks(
&headers(1),
&[],
MID,
&Bytes::from(vec![0u8; 1000]),
crate::ic::DEFAULT_MAX_INGRESS_MESSAGE_SIZE,
1024,
)
.unwrap_err(),
UploadPlanError::NoEnvelopes
);
}
}