offline_protocol/constants.rs
1//! Constants used throughout the offline-protocol crate.
2
3/// Default time-to-live for messages.
4pub const DEFAULT_INITIAL_TTL: u8 = 8;
5
6/// Metadata key for ACK messages indicating which message ID the ACK is for.
7pub const ACK_FOR_KEY: &str = "ack_for";
8
9/// Metadata key for ACK messages indicating the hop count.
10pub const ACK_HOP_COUNT_KEY: &str = "ack_hop_count";
11
12/// Metadata key for ACK messages indicating the transport used.
13pub const ACK_TRANSPORT_KEY: &str = "ack_transport";
14
15/// Maximum number of entries in the outbox before evicting oldest entries.
16/// This prevents unbounded memory growth when messages cannot be delivered.
17pub const MAX_OUTBOX_ENTRIES: usize = 500;
18
19/// Absolute cap on a restored outbox entry's total lifetime, as a multiple
20/// of `outbox_max_lifetime_ms`, measured from `first_sent_at`.
21///
22/// The restore path refreshes the carrier-relative TTL of an entry that
23/// lapsed while the process was down (restart ⇒ fresh delivery window).
24/// Without an absolute bound, an app used briefly once past each lifetime
25/// keeps re-granting the entry a fresh window forever, so "expiry is
26/// terminal" would only hold in-process. Past this cap the entry is dropped
27/// at restore with a terminal `message_failed` instead of refreshed
28/// (4 × 7 days = 28 days at the default lifetime).
29pub const OUTBOX_ABSOLUTE_LIFETIME_FACTOR: i32 = 4;
30
31/// Maximum number of messages to keep in history for topology visualization.
32/// This prevents unbounded memory growth while maintaining enough history
33/// for accurate network statistics.
34pub const MAX_MESSAGE_HISTORY: usize = 1000;
35
36/// Number of messages to remove when history exceeds the maximum.
37/// This provides a buffer to avoid frequent cleanup operations.
38pub const HISTORY_CLEANUP_BATCH_SIZE: usize = 100;
39
40/// Threshold for compacting observed stats to prevent unbounded growth.
41pub const OBSERVED_STATS_COMPACT_THRESHOLD: u32 = 10_000;
42
43/// EMA (Exponential Moving Average) alpha for latency tracking.
44pub const LATENCY_EMA_ALPHA: f32 = 0.3;
45
46/// EMA alpha for hop count tracking.
47pub const HOP_COUNT_EMA_ALPHA: f32 = 0.2;
48
49/// Default chunk size for file transfers (32 KB).
50pub const DEFAULT_CHUNK_SIZE: usize = 32 * 1024;
51
52/// Chunk size for BLE file transfers (4 KB).
53/// Reduces BLE double-fragmentation: ~22 BLE fragments per chunk instead of ~240.
54pub const CHUNK_SIZE_BLE: usize = 4 * 1024;
55
56/// Chunk size for Internet file transfers (256 KB).
57/// Fewer round-trips over WebSocket where bandwidth is plentiful.
58pub const CHUNK_SIZE_INTERNET: usize = 256 * 1024;
59
60/// Maximum chunks in flight for BLE media transfers.
61/// BLE's low bandwidth means fewer concurrent chunks to avoid congestion.
62pub const MEDIA_WINDOW_SIZE_BLE: usize = 2;
63
64/// Maximum chunks in flight for Internet media transfers.
65pub const MEDIA_WINDOW_SIZE_INTERNET: usize = 8;
66
67/// Default maximum chunks in flight for media transfers.
68pub const DEFAULT_MEDIA_WINDOW_SIZE: usize = 4;
69
70/// Maximum entries in the dedicated media outbox.
71/// Smaller than the main outbox since the sliding window limits in-flight chunks.
72pub const MAX_MEDIA_OUTBOX_ENTRIES: usize = 100;
73
74/// Maximum persisted outbound media transfer descriptors restored after a
75/// restart (newest-first). Small on purpose: each maps to a
76/// `MediaResendRequired` the app is expected to act on, and concurrent
77/// transfers are already capped per peer.
78pub const MAX_MEDIA_DESCRIPTORS: usize = 16;
79
80/// Maximum concurrent outbound media transfers per recipient when chunks are
81/// MLS-encrypted.
82///
83/// Encrypted chunks share the recipient's 1:1 session ratchet with text, and
84/// the receiver retains out-of-order message keys for only
85/// `SENDER_RATCHET_OUT_OF_ORDER_TOLERANCE` (32) generations. Each transfer
86/// keeps up to `MEDIA_WINDOW_SIZE_INTERNET` (8) chunks in flight, so two
87/// concurrent transfers bound the media-side in-flight gap at 16
88/// generations, leaving room for up to 16 further messages interleaved on
89/// the same ratchet before a delayed chunk's key is deleted. That residual
90/// is not a guarantee: text sends are unbounded, and a chunk delayed while
91/// 32+ interleaved messages decrypt first is permanently undecryptable (the
92/// receiver surfaces this as `MessageDecryptionFailed`). Without this cap,
93/// concurrent transfers alone could push a delayed chunk beyond the
94/// tolerance and permanently stall its transfer.
95pub const MAX_CONCURRENT_MEDIA_TRANSFERS_PER_PEER: usize = 2;
96
97/// Metadata key indicating preferred transport for a message.
98pub const TRANSPORT_PREFERENCE_KEY: &str = "transport_preference";
99
100/// Transport preference value: prefer internet transport.
101pub const TRANSPORT_PREFERENCE_INTERNET: &str = "internet";
102
103/// Metadata key carrying the original content type on file-chunk messages.
104pub const ORIGINAL_CONTENT_TYPE_KEY: &str = "original_content_type";
105
106/// Maximum number of times a message may be forwarded. Prevents unbounded
107/// forwarding chains from malicious or buggy clients.
108pub const MAX_FORWARD_COUNT: u32 = 100;
109
110/// Maximum messages to send in a single flush operation.
111/// Prevents blocking the process loop when many messages are pending.
112pub const FLUSH_BATCH_LIMIT: usize = 20;
113
114/// Default route quality assigned when learning a route from a relayed message.
115/// Conservative (0.5) because the transport layer doesn't expose the immediate
116/// peer identity, so the route may be sub-optimal for multi-hop return paths.
117pub const RELAY_LEARNED_ROUTE_QUALITY: f32 = 0.5;