Skip to main content

offline_protocol_reliability/
constants.rs

1//! Reliability layer constants.
2
3/// Default ACK timeout in milliseconds.
4pub const DEFAULT_ACK_TIMEOUT_MS: u64 = 10000;
5
6/// Maximum number of pending ACKs to track.
7pub const DEFAULT_MAX_PENDING_ACKS: usize = 1000;
8
9/// Default maximum number of retries per message.
10pub const DEFAULT_MAX_RETRIES: u32 = 10;
11
12/// Initial retry delay in milliseconds.
13pub const DEFAULT_INITIAL_DELAY_MS: u64 = 1000;
14
15/// Maximum retry delay in milliseconds.
16///
17/// 5 minutes. Delivery latency does not ride on this ceiling: outbox
18/// flushes (start, transport reconnect, peer rediscovery, session
19/// establishment) bypass backoff timers entirely. The periodic retry is a
20/// safety net, and a lower ceiling only multiplies futile send attempts —
21/// and per-failure `MessageRetrying` events — while a peer stays offline
22/// within the 7-day outbox window.
23pub const DEFAULT_MAX_DELAY_MS: u64 = 300_000;
24
25/// Backoff multiplier for exponential backoff.
26pub const DEFAULT_BACKOFF_MULTIPLIER: f32 = 2.0;
27
28/// Maximum lifetime for messages in outbox (milliseconds).
29///
30/// 7 days, matching the app-layer presence-flush window: a peer is
31/// considered recoverable for up to 7 days offline, so queued messages
32/// must survive at least that long before the outbox gives up on them.
33pub const DEFAULT_OUTBOX_LIFETIME_MS: u64 = 604_800_000;
34
35/// Maximum lifetime for messages waiting on MLS session establishment.
36///
37/// This is deliberately finite so an unresolved or permanently invalid
38/// recipient cannot remain in durable `pending_messages` forever.
39pub const DEFAULT_PENDING_MESSAGE_LIFETIME_MS: u64 = 604_800_000;
40
41/// Estimated size of an ACK entry in bytes (message ID + metadata).
42pub const ESTIMATED_ACK_SIZE_BYTES: usize = 40;
43
44/// Network size threshold below which all ACKs are relayed.
45pub const SMALL_NETWORK_RELAY_THRESHOLD: usize = 5;
46
47/// Density factor numerator for relay probability calculation.
48pub const RELAY_DENSITY_FACTOR_NUMERATOR: f32 = 5.0;
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    /// The RN native bridges rebuild the whole `RetryConfig` from JSON in
55    /// `updateRetryConfig`, filling absent fields from hardcoded fallbacks.
56    /// A fallback that drifts from the Rust defaults silently overrides the
57    /// SDK default for every field the app didn't set (this happened once:
58    /// `maxRetries` 3 vs 10). This test pins the bridge literals to the
59    /// constants above; when a default changes here, it fails until both
60    /// bridge files are updated.
61    #[test]
62    fn rn_bridge_retry_fallbacks_match_rust_defaults() {
63        let rn_root =
64            std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../bindings/react-native");
65        let kotlin_path =
66            rn_root.join("android/src/main/java/com/offlineprotocol/OfflineProtocolModule.kt");
67        let swift_path = rn_root.join("ios/OfflineProtocolModule.swift");
68
69        // The guard only applies in the repo checkout; skip when the
70        // bindings tree isn't present (e.g. a vendored crate).
71        let (Ok(kotlin), Ok(swift)) = (
72            std::fs::read_to_string(&kotlin_path),
73            std::fs::read_to_string(&swift_path),
74        ) else {
75            eprintln!("bindings tree not present, skipping RN fallback drift check");
76            return;
77        };
78
79        let ack_expected = [
80            (
81                format!("json.optLong(\"defaultTimeoutMs\", {DEFAULT_ACK_TIMEOUT_MS})"),
82                format!(
83                    "(config[\"defaultTimeoutMs\"] as? NSNumber)?.uint64Value ?? {DEFAULT_ACK_TIMEOUT_MS}"
84                ),
85            ),
86            (
87                format!("json.optLong(\"maxPendingAcks\", {DEFAULT_MAX_PENDING_ACKS})"),
88                format!(
89                    "(config[\"maxPendingAcks\"] as? NSNumber)?.uint64Value ?? {DEFAULT_MAX_PENDING_ACKS}"
90                ),
91            ),
92        ];
93        for (kotlin_fallback, swift_fallback) in &ack_expected {
94            assert!(
95                kotlin.contains(kotlin_fallback),
96                "RN Android bridge ACK fallback drifted from the Rust default: \
97                 expected `{kotlin_fallback}` in {}",
98                kotlin_path.display()
99            );
100            assert!(
101                swift.contains(swift_fallback),
102                "RN iOS bridge ACK fallback drifted from the Rust default: \
103                 expected `{swift_fallback}` in {}",
104                swift_path.display()
105            );
106        }
107
108        let kotlin_expected = [
109            format!("json.optInt(\"maxRetries\", {DEFAULT_MAX_RETRIES})"),
110            format!("json.optLong(\"initialDelayMs\", {DEFAULT_INITIAL_DELAY_MS})"),
111            format!("json.optLong(\"maxDelayMs\", {DEFAULT_MAX_DELAY_MS})"),
112            format!("json.optDouble(\"backoffMultiplier\", {DEFAULT_BACKOFF_MULTIPLIER:.1})"),
113            format!("json.optLong(\"outboxMaxLifetimeMs\", {DEFAULT_OUTBOX_LIFETIME_MS})"),
114            format!(
115                "json.optLong(\"pendingMessageMaxLifetimeMs\", {DEFAULT_PENDING_MESSAGE_LIFETIME_MS})"
116            ),
117        ];
118        for expected in &kotlin_expected {
119            assert!(
120                kotlin.contains(expected),
121                "RN Android bridge retry fallback drifted from the Rust default: \
122                 expected `{expected}` in {}",
123                kotlin_path.display()
124            );
125        }
126
127        let swift_expected = [
128            format!("(config[\"maxRetries\"] as? NSNumber)?.uint32Value ?? {DEFAULT_MAX_RETRIES}"),
129            format!(
130                "(config[\"initialDelayMs\"] as? NSNumber)?.uint64Value ?? {DEFAULT_INITIAL_DELAY_MS}"
131            ),
132            format!("(config[\"maxDelayMs\"] as? NSNumber)?.uint64Value ?? {DEFAULT_MAX_DELAY_MS}"),
133            format!(
134                "(config[\"backoffMultiplier\"] as? NSNumber)?.floatValue ?? {DEFAULT_BACKOFF_MULTIPLIER:.1}"
135            ),
136            format!(
137                "(config[\"outboxMaxLifetimeMs\"] as? NSNumber)?.uint64Value ?? {DEFAULT_OUTBOX_LIFETIME_MS}"
138            ),
139            format!(
140                "(config[\"pendingMessageMaxLifetimeMs\"] as? NSNumber)?.uint64Value ?? {DEFAULT_PENDING_MESSAGE_LIFETIME_MS}"
141            ),
142        ];
143        for expected in &swift_expected {
144            assert!(
145                swift.contains(expected),
146                "RN iOS bridge retry fallback drifted from the Rust default: \
147                 expected `{expected}` in {}",
148                swift_path.display()
149            );
150        }
151    }
152}