Skip to main content

ant_node/replication/
config.rs

1//! Tunable parameters for the replication subsystem.
2//!
3//! All values below are a reference profile used for logic validation.
4//! Parameter safety constraints (Section 4):
5//! 1. `1 <= QUORUM_THRESHOLD <= CLOSE_GROUP_SIZE`
6//! 2. Effective paid-list threshold is per-key dynamic:
7//!    `ConfirmNeeded(K) = floor(PaidGroupSize(K)/2)+1`
8//! 3. If constraints are violated at runtime reconfiguration, node MUST reject
9//!    the config.
10
11#![allow(clippy::module_name_repetitions)]
12
13use std::time::Duration;
14
15use rand::Rng;
16
17use crate::ant_protocol::CLOSE_GROUP_SIZE;
18
19// ---------------------------------------------------------------------------
20// Static constants (compile-time reference profile)
21// ---------------------------------------------------------------------------
22
23/// Maximum number of peers per k-bucket in the Kademlia routing table.
24pub const K_BUCKET_SIZE: usize = 20;
25
26/// Full-network target for required positive presence votes.
27///
28/// Effective per-key threshold is
29/// `QuorumNeeded(K) = min(QUORUM_THRESHOLD, floor(|QuorumTargets|/2)+1)`.
30pub const QUORUM_THRESHOLD: usize = 4; // floor(CLOSE_GROUP_SIZE / 2) + 1
31
32/// Maximum number of closest nodes tracking paid status for a key.
33pub const PAID_LIST_CLOSE_GROUP_SIZE: usize = 20;
34
35/// Number of closest peers to self eligible for neighbor sync.
36pub const NEIGHBOR_SYNC_SCOPE: usize = 20;
37
38/// Number of close-neighbor peers synced concurrently per round-robin repair
39/// round.
40pub const NEIGHBOR_SYNC_PEER_COUNT: usize = 4;
41
42/// Minimum neighbor-sync cadence. Actual interval is randomized within
43/// `[min, max]`.
44const NEIGHBOR_SYNC_INTERVAL_MIN_SECS: u64 = 10 * 60;
45/// Maximum neighbor-sync cadence.
46const NEIGHBOR_SYNC_INTERVAL_MAX_SECS: u64 = 20 * 60;
47
48/// Neighbor sync cadence range (min).
49pub const NEIGHBOR_SYNC_INTERVAL_MIN: Duration =
50    Duration::from_secs(NEIGHBOR_SYNC_INTERVAL_MIN_SECS);
51
52/// Neighbor sync cadence range (max).
53pub const NEIGHBOR_SYNC_INTERVAL_MAX: Duration =
54    Duration::from_secs(NEIGHBOR_SYNC_INTERVAL_MAX_SECS);
55
56/// Per-peer minimum spacing between successive syncs with the same peer.
57const NEIGHBOR_SYNC_COOLDOWN_SECS: u64 = 60 * 60; // 1 hour
58/// Per-peer minimum spacing between successive syncs with the same peer.
59pub const NEIGHBOR_SYNC_COOLDOWN: Duration = Duration::from_secs(NEIGHBOR_SYNC_COOLDOWN_SECS);
60
61/// Minimum self-lookup cadence.
62const SELF_LOOKUP_INTERVAL_MIN_SECS: u64 = 5 * 60;
63/// Maximum self-lookup cadence.
64const SELF_LOOKUP_INTERVAL_MAX_SECS: u64 = 10 * 60;
65
66/// Periodic self-lookup cadence range (min) to keep close neighborhood
67/// current.
68pub const SELF_LOOKUP_INTERVAL_MIN: Duration = Duration::from_secs(SELF_LOOKUP_INTERVAL_MIN_SECS);
69
70/// Periodic self-lookup cadence range (max).
71pub const SELF_LOOKUP_INTERVAL_MAX: Duration = Duration::from_secs(SELF_LOOKUP_INTERVAL_MAX_SECS);
72
73/// Maximum number of concurrent outbound replication sends.
74///
75/// Caps how many fresh-replication chunk transfers can be in-flight at once
76/// across the entire replication engine. Prevents bandwidth saturation on
77/// home broadband connections when multiple chunks arrive simultaneously.
78/// Each send transfers up to 4 MB (`MAX_CHUNK_SIZE`), so a limit of 3 means
79/// at most ~12 MB queued for the upload link at any instant.
80pub const MAX_CONCURRENT_REPLICATION_SENDS: usize = 3;
81
82/// Concurrent fetches cap, derived from hardware thread count.
83///
84/// Uses `std::thread::available_parallelism()` so the node scales to the
85/// machine it runs on.  Falls back to 4 if the OS query fails.
86const AVAILABLE_PARALLELISM_FALLBACK: usize = 4;
87
88/// Returns the number of hardware threads available, used as the fetch
89/// concurrency limit.
90#[allow(clippy::incompatible_msrv)] // NonZero::get is stable since 1.79; MSRV lint conflicts with redundant_closure
91pub fn max_parallel_fetch() -> usize {
92    std::thread::available_parallelism()
93        .map_or(AVAILABLE_PARALLELISM_FALLBACK, std::num::NonZero::get)
94}
95
96/// Minimum audit-scheduler cadence.
97const AUDIT_TICK_INTERVAL_MIN_SECS: u64 = 30 * 60;
98/// Maximum audit-scheduler cadence.
99const AUDIT_TICK_INTERVAL_MAX_SECS: u64 = 60 * 60;
100
101/// Audit scheduler cadence range (min).
102pub const AUDIT_TICK_INTERVAL_MIN: Duration = Duration::from_secs(AUDIT_TICK_INTERVAL_MIN_SECS);
103
104/// Audit scheduler cadence range (max).
105pub const AUDIT_TICK_INTERVAL_MAX: Duration = Duration::from_secs(AUDIT_TICK_INTERVAL_MAX_SECS);
106
107/// Base audit response deadline (independent of challenge size).
108const AUDIT_RESPONSE_BASE_SECS: u64 = 6;
109/// Per-chunk allowance added to the base audit response deadline.
110const AUDIT_RESPONSE_PER_CHUNK_MS: u64 = 10;
111
112/// Maximum duration a peer may claim bootstrap status before penalties apply.
113const BOOTSTRAP_CLAIM_GRACE_PERIOD_SECS: u64 = 24 * 60 * 60; // 24 h
114/// Maximum duration a peer may claim bootstrap status before penalties apply.
115pub const BOOTSTRAP_CLAIM_GRACE_PERIOD: Duration =
116    Duration::from_secs(BOOTSTRAP_CLAIM_GRACE_PERIOD_SECS);
117
118/// Minimum continuous out-of-range duration before pruning a key.
119const PRUNE_HYSTERESIS_DURATION_SECS: u64 = 6 * 60 * 60; // 6 h
120/// Minimum continuous out-of-range duration before pruning a key.
121pub const PRUNE_HYSTERESIS_DURATION: Duration = Duration::from_secs(PRUNE_HYSTERESIS_DURATION_SECS);
122
123/// Protocol identifier for replication operations.
124pub const REPLICATION_PROTOCOL_ID: &str = "autonomi.ant.replication.v1";
125
126/// 10 MiB — maximum replication wire message size (accommodates hint batches).
127const REPLICATION_MESSAGE_SIZE_MIB: usize = 10;
128/// Maximum replication wire message size.
129pub const MAX_REPLICATION_MESSAGE_SIZE: usize = REPLICATION_MESSAGE_SIZE_MIB * 1024 * 1024;
130
131/// Verification request timeout (per-batch).
132const VERIFICATION_REQUEST_TIMEOUT_SECS: u64 = 15;
133/// Verification request timeout (per-batch).
134pub const VERIFICATION_REQUEST_TIMEOUT: Duration =
135    Duration::from_secs(VERIFICATION_REQUEST_TIMEOUT_SECS);
136
137/// Fetch request timeout.
138const FETCH_REQUEST_TIMEOUT_SECS: u64 = 30;
139/// Fetch request timeout.
140pub const FETCH_REQUEST_TIMEOUT: Duration = Duration::from_secs(FETCH_REQUEST_TIMEOUT_SECS);
141
142/// Maximum age for pending-verification entries before stale eviction.
143const PENDING_VERIFY_MAX_AGE_SECS: u64 = 30 * 60;
144/// Maximum age for pending-verification entries before stale eviction.
145pub const PENDING_VERIFY_MAX_AGE: Duration = Duration::from_secs(PENDING_VERIFY_MAX_AGE_SECS);
146
147/// Trust event weight for confirmed audit failures.
148pub const AUDIT_FAILURE_TRUST_WEIGHT: f64 = 2.0;
149
150/// Seconds to wait for `DhtNetworkEvent::BootstrapComplete` before proceeding
151/// with bootstrap sync. Covers bootstrap nodes with no peers to connect to.
152const BOOTSTRAP_COMPLETE_TIMEOUT_SECS: u64 = 60;
153
154// ---------------------------------------------------------------------------
155// Runtime-configurable wrapper
156// ---------------------------------------------------------------------------
157
158/// Runtime-configurable replication parameters.
159///
160/// Validated on construction — node rejects invalid configs.
161#[derive(Debug, Clone)]
162pub struct ReplicationConfig {
163    /// Close-group width and target holder count per key.
164    pub close_group_size: usize,
165    /// Required positive presence votes for quorum.
166    pub quorum_threshold: usize,
167    /// Maximum closest nodes tracking paid status for a key.
168    pub paid_list_close_group_size: usize,
169    /// Number of closest peers to self eligible for neighbor sync.
170    pub neighbor_sync_scope: usize,
171    /// Peers synced concurrently per round-robin repair round.
172    pub neighbor_sync_peer_count: usize,
173    /// Neighbor sync cadence range (min).
174    pub neighbor_sync_interval_min: Duration,
175    /// Neighbor sync cadence range (max).
176    pub neighbor_sync_interval_max: Duration,
177    /// Minimum spacing between successive syncs with the same peer.
178    pub neighbor_sync_cooldown: Duration,
179    /// Self-lookup cadence range (min).
180    pub self_lookup_interval_min: Duration,
181    /// Self-lookup cadence range (max).
182    pub self_lookup_interval_max: Duration,
183    /// Audit scheduler cadence range (min).
184    pub audit_tick_interval_min: Duration,
185    /// Audit scheduler cadence range (max).
186    pub audit_tick_interval_max: Duration,
187    /// Base audit response deadline (chunk-independent component).
188    pub audit_response_base: Duration,
189    /// Per-chunk allowance added to the base audit response deadline.
190    pub audit_response_per_chunk: Duration,
191    /// Maximum duration a peer may claim bootstrap status.
192    pub bootstrap_claim_grace_period: Duration,
193    /// Minimum continuous out-of-range duration before pruning a key.
194    pub prune_hysteresis_duration: Duration,
195    /// Verification request timeout (per-batch).
196    pub verification_request_timeout: Duration,
197    /// Fetch request timeout.
198    pub fetch_request_timeout: Duration,
199    /// Seconds to wait for `DhtNetworkEvent::BootstrapComplete` before
200    /// proceeding with bootstrap sync (covers bootstrap nodes with no peers).
201    pub bootstrap_complete_timeout_secs: u64,
202}
203
204impl Default for ReplicationConfig {
205    fn default() -> Self {
206        Self {
207            close_group_size: CLOSE_GROUP_SIZE,
208            quorum_threshold: QUORUM_THRESHOLD,
209            paid_list_close_group_size: PAID_LIST_CLOSE_GROUP_SIZE,
210            neighbor_sync_scope: NEIGHBOR_SYNC_SCOPE,
211            neighbor_sync_peer_count: NEIGHBOR_SYNC_PEER_COUNT,
212            neighbor_sync_interval_min: NEIGHBOR_SYNC_INTERVAL_MIN,
213            neighbor_sync_interval_max: NEIGHBOR_SYNC_INTERVAL_MAX,
214            neighbor_sync_cooldown: NEIGHBOR_SYNC_COOLDOWN,
215            self_lookup_interval_min: SELF_LOOKUP_INTERVAL_MIN,
216            self_lookup_interval_max: SELF_LOOKUP_INTERVAL_MAX,
217            audit_tick_interval_min: AUDIT_TICK_INTERVAL_MIN,
218            audit_tick_interval_max: AUDIT_TICK_INTERVAL_MAX,
219            audit_response_base: Duration::from_secs(AUDIT_RESPONSE_BASE_SECS),
220            audit_response_per_chunk: Duration::from_millis(AUDIT_RESPONSE_PER_CHUNK_MS),
221            bootstrap_claim_grace_period: BOOTSTRAP_CLAIM_GRACE_PERIOD,
222            prune_hysteresis_duration: PRUNE_HYSTERESIS_DURATION,
223            verification_request_timeout: VERIFICATION_REQUEST_TIMEOUT,
224            fetch_request_timeout: FETCH_REQUEST_TIMEOUT,
225            bootstrap_complete_timeout_secs: BOOTSTRAP_COMPLETE_TIMEOUT_SECS,
226        }
227    }
228}
229
230impl ReplicationConfig {
231    /// Validate safety constraints. Returns `Err` with a description if any
232    /// constraint is violated.
233    ///
234    /// # Errors
235    ///
236    /// Returns a human-readable message describing the first violated
237    /// constraint.
238    pub fn validate(&self) -> Result<(), String> {
239        if self.close_group_size == 0 {
240            return Err("close_group_size must be >= 1".to_string());
241        }
242        if self.quorum_threshold == 0 || self.quorum_threshold > self.close_group_size {
243            return Err(format!(
244                "quorum_threshold ({}) must satisfy 1 <= quorum_threshold <= close_group_size ({})",
245                self.quorum_threshold, self.close_group_size,
246            ));
247        }
248        if self.paid_list_close_group_size == 0 {
249            return Err("paid_list_close_group_size must be >= 1".to_string());
250        }
251        if self.neighbor_sync_interval_min > self.neighbor_sync_interval_max {
252            return Err(format!(
253                "neighbor_sync_interval_min ({:?}) must be <= neighbor_sync_interval_max ({:?})",
254                self.neighbor_sync_interval_min, self.neighbor_sync_interval_max,
255            ));
256        }
257        if self.audit_tick_interval_min > self.audit_tick_interval_max {
258            return Err(format!(
259                "audit_tick_interval_min ({:?}) must be <= audit_tick_interval_max ({:?})",
260                self.audit_tick_interval_min, self.audit_tick_interval_max,
261            ));
262        }
263        if self.self_lookup_interval_min > self.self_lookup_interval_max {
264            return Err(format!(
265                "self_lookup_interval_min ({:?}) must be <= self_lookup_interval_max ({:?})",
266                self.self_lookup_interval_min, self.self_lookup_interval_max,
267            ));
268        }
269        if self.neighbor_sync_peer_count == 0 {
270            return Err("neighbor_sync_peer_count must be >= 1".to_string());
271        }
272        if self.neighbor_sync_scope == 0 {
273            return Err("neighbor_sync_scope must be >= 1".to_string());
274        }
275        Ok(())
276    }
277
278    /// Effective quorum votes required for a key given the number of
279    /// reachable quorum targets.
280    ///
281    /// `min(self.quorum_threshold, floor(quorum_targets_count / 2) + 1)`
282    #[must_use]
283    pub fn quorum_needed(&self, quorum_targets_count: usize) -> usize {
284        if quorum_targets_count == 0 {
285            return 0;
286        }
287        let majority = quorum_targets_count / 2 + 1;
288        self.quorum_threshold.min(majority)
289    }
290
291    /// Confirmations required for paid-list consensus given the number of
292    /// peers in the paid-list close group for a key.
293    ///
294    /// `floor(paid_group_size / 2) + 1`
295    #[must_use]
296    pub fn confirm_needed(paid_group_size: usize) -> usize {
297        paid_group_size / 2 + 1
298    }
299
300    /// Returns a random duration in `[neighbor_sync_interval_min,
301    /// neighbor_sync_interval_max]`.
302    #[must_use]
303    pub fn random_neighbor_sync_interval(&self) -> Duration {
304        random_duration_in_range(
305            self.neighbor_sync_interval_min,
306            self.neighbor_sync_interval_max,
307        )
308    }
309
310    /// Compute the number of keys to sample for an audit round, scaled
311    /// dynamically by the total number of locally stored keys.
312    ///
313    /// Formula: `max(floor(sqrt(total_keys)), 1)`, capped at `total_keys`.
314    #[must_use]
315    pub fn audit_sample_count(total_keys: usize) -> usize {
316        #[allow(
317            clippy::cast_possible_truncation,
318            clippy::cast_sign_loss,
319            clippy::cast_precision_loss
320        )]
321        let sqrt = (total_keys as f64).sqrt() as usize;
322        sqrt.max(1).min(total_keys)
323    }
324
325    /// Maximum number of keys to accept in an incoming audit challenge.
326    ///
327    /// Scales dynamically: `2 * audit_sample_count(stored_chunks)`. The 2x
328    /// margin accounts for the challenger having a larger store than us and
329    /// therefore sampling more keys.
330    #[must_use]
331    pub fn max_incoming_audit_keys(stored_chunks: usize) -> usize {
332        // Allow at least 1 key so a newly-joined node can still be audited.
333        (2 * Self::audit_sample_count(stored_chunks)).max(1)
334    }
335
336    /// Compute the audit response timeout for a challenge with `chunk_count`
337    /// keys: `base + per_chunk * chunk_count`.
338    #[must_use]
339    pub fn audit_response_timeout(&self, chunk_count: usize) -> Duration {
340        let chunks = u32::try_from(chunk_count).unwrap_or(u32::MAX);
341        self.audit_response_base + self.audit_response_per_chunk * chunks
342    }
343
344    /// Returns a random duration in `[audit_tick_interval_min,
345    /// audit_tick_interval_max]`.
346    #[must_use]
347    pub fn random_audit_tick_interval(&self) -> Duration {
348        random_duration_in_range(self.audit_tick_interval_min, self.audit_tick_interval_max)
349    }
350
351    /// Returns a random duration in `[self_lookup_interval_min,
352    /// self_lookup_interval_max]`.
353    #[must_use]
354    pub fn random_self_lookup_interval(&self) -> Duration {
355        random_duration_in_range(self.self_lookup_interval_min, self.self_lookup_interval_max)
356    }
357}
358
359/// Pick a random `Duration` uniformly in `[min, max]` at millisecond
360/// granularity.
361///
362/// When `min == max` the result is deterministic.
363fn random_duration_in_range(min: Duration, max: Duration) -> Duration {
364    if min == max {
365        return min;
366    }
367    // Our intervals are minutes/hours, well within u64 range. Saturate to
368    // u64::MAX on the impossible overflow path to avoid a lossy cast.
369    let to_u64_millis = |d: Duration| -> u64 { u64::try_from(d.as_millis()).unwrap_or(u64::MAX) };
370    let chosen = rand::thread_rng().gen_range(to_u64_millis(min)..=to_u64_millis(max));
371    Duration::from_millis(chosen)
372}
373
374// ---------------------------------------------------------------------------
375// Tests
376// ---------------------------------------------------------------------------
377
378#[cfg(test)]
379#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
380mod tests {
381    use super::*;
382
383    #[test]
384    fn defaults_pass_validation() {
385        let config = ReplicationConfig::default();
386        assert!(config.validate().is_ok(), "default config must be valid");
387    }
388
389    #[test]
390    fn quorum_threshold_zero_rejected() {
391        let config = ReplicationConfig {
392            quorum_threshold: 0,
393            ..ReplicationConfig::default()
394        };
395        assert!(config.validate().is_err());
396    }
397
398    #[test]
399    fn quorum_threshold_exceeds_close_group_rejected() {
400        let defaults = ReplicationConfig::default();
401        let config = ReplicationConfig {
402            quorum_threshold: defaults.close_group_size + 1,
403            ..defaults
404        };
405        assert!(config.validate().is_err());
406    }
407
408    #[test]
409    fn close_group_size_zero_rejected() {
410        let config = ReplicationConfig {
411            close_group_size: 0,
412            ..ReplicationConfig::default()
413        };
414        assert!(config.validate().is_err());
415    }
416
417    #[test]
418    fn paid_list_close_group_size_zero_rejected() {
419        let config = ReplicationConfig {
420            paid_list_close_group_size: 0,
421            ..ReplicationConfig::default()
422        };
423        assert!(config.validate().is_err());
424    }
425
426    #[test]
427    fn neighbor_sync_interval_inverted_rejected() {
428        let config = ReplicationConfig {
429            neighbor_sync_interval_min: Duration::from_secs(100),
430            neighbor_sync_interval_max: Duration::from_secs(50),
431            ..ReplicationConfig::default()
432        };
433        assert!(config.validate().is_err());
434    }
435
436    #[test]
437    fn audit_tick_interval_inverted_rejected() {
438        let config = ReplicationConfig {
439            audit_tick_interval_min: Duration::from_secs(100),
440            audit_tick_interval_max: Duration::from_secs(50),
441            ..ReplicationConfig::default()
442        };
443        assert!(config.validate().is_err());
444    }
445
446    #[test]
447    fn self_lookup_interval_inverted_rejected() {
448        let config = ReplicationConfig {
449            self_lookup_interval_min: Duration::from_secs(100),
450            self_lookup_interval_max: Duration::from_secs(50),
451            ..ReplicationConfig::default()
452        };
453        assert!(config.validate().is_err());
454    }
455
456    #[test]
457    fn neighbor_sync_peer_count_zero_rejected() {
458        let config = ReplicationConfig {
459            neighbor_sync_peer_count: 0,
460            ..ReplicationConfig::default()
461        };
462        assert!(config.validate().is_err());
463    }
464
465    #[test]
466    fn audit_sample_count_scales_with_sqrt() {
467        // Empty store
468        assert_eq!(ReplicationConfig::audit_sample_count(0), 0);
469
470        // Single key
471        assert_eq!(ReplicationConfig::audit_sample_count(1), 1);
472
473        // Small stores: sqrt(3)=1
474        assert_eq!(ReplicationConfig::audit_sample_count(3), 1);
475
476        // sqrt scaling
477        assert_eq!(ReplicationConfig::audit_sample_count(4), 2);
478        assert_eq!(ReplicationConfig::audit_sample_count(25), 5);
479        assert_eq!(ReplicationConfig::audit_sample_count(100), 10);
480        assert_eq!(ReplicationConfig::audit_sample_count(1_000), 31);
481        assert_eq!(ReplicationConfig::audit_sample_count(10_000), 100);
482        assert_eq!(ReplicationConfig::audit_sample_count(1_000_000), 1_000);
483    }
484
485    #[test]
486    fn max_incoming_audit_keys_scales_dynamically() {
487        // Empty store: at least 1 key accepted.
488        assert_eq!(ReplicationConfig::max_incoming_audit_keys(0), 1);
489
490        // 1 chunk: 2 * sqrt(1) = 2.
491        assert_eq!(ReplicationConfig::max_incoming_audit_keys(1), 2);
492
493        // 100 chunks: 2 * sqrt(100) = 20.
494        assert_eq!(ReplicationConfig::max_incoming_audit_keys(100), 20);
495
496        // 1M chunks: 2 * sqrt(1_000_000) = 2_000.
497        assert_eq!(ReplicationConfig::max_incoming_audit_keys(1_000_000), 2_000);
498
499        // 5M chunks: 2 * sqrt(5_000_000) = 4_472.
500        assert_eq!(ReplicationConfig::max_incoming_audit_keys(5_000_000), 4_472);
501    }
502
503    #[test]
504    fn quorum_needed_uses_smaller_of_threshold_and_majority() {
505        let config = ReplicationConfig::default();
506
507        // With 7 targets: majority = 7/2+1 = 4, threshold = 4 → min = 4
508        assert_eq!(config.quorum_needed(7), 4);
509
510        // With 3 targets: majority = 3/2+1 = 2, threshold = 4 → min = 2
511        assert_eq!(config.quorum_needed(3), 2);
512
513        // With 0 targets: quorum is impossible — returns 0
514        assert_eq!(config.quorum_needed(0), 0);
515
516        // With 100 targets: majority = 51, threshold = 4 → min = 4
517        assert_eq!(config.quorum_needed(100), 4);
518    }
519
520    #[test]
521    fn confirm_needed_is_strict_majority() {
522        assert_eq!(ReplicationConfig::confirm_needed(1), 1);
523        assert_eq!(ReplicationConfig::confirm_needed(2), 2);
524        assert_eq!(ReplicationConfig::confirm_needed(3), 2);
525        assert_eq!(ReplicationConfig::confirm_needed(4), 3);
526        assert_eq!(ReplicationConfig::confirm_needed(20), 11);
527    }
528
529    #[test]
530    fn random_intervals_within_bounds() {
531        let config = ReplicationConfig::default();
532
533        // Run several iterations to exercise randomness.
534        let iterations = 50;
535        for _ in 0..iterations {
536            let ns = config.random_neighbor_sync_interval();
537            assert!(ns >= config.neighbor_sync_interval_min);
538            assert!(ns <= config.neighbor_sync_interval_max);
539
540            let at = config.random_audit_tick_interval();
541            assert!(at >= config.audit_tick_interval_min);
542            assert!(at <= config.audit_tick_interval_max);
543
544            let sl = config.random_self_lookup_interval();
545            assert!(sl >= config.self_lookup_interval_min);
546            assert!(sl <= config.self_lookup_interval_max);
547        }
548    }
549
550    #[test]
551    fn random_interval_equal_bounds_is_deterministic() {
552        let fixed = Duration::from_secs(42);
553        let config = ReplicationConfig {
554            neighbor_sync_interval_min: fixed,
555            neighbor_sync_interval_max: fixed,
556            ..ReplicationConfig::default()
557        };
558        assert_eq!(config.random_neighbor_sync_interval(), fixed);
559    }
560
561    // -----------------------------------------------------------------------
562    // Section 18 scenarios
563    // -----------------------------------------------------------------------
564
565    /// Scenario 18: Invalid runtime config is rejected by `validate()`.
566    #[test]
567    fn scenario_18_invalid_config_rejected() {
568        // quorum_threshold > close_group_size -> validation fails.
569        let config = ReplicationConfig {
570            quorum_threshold: 10,
571            close_group_size: 7,
572            ..ReplicationConfig::default()
573        };
574        let err = config.validate().unwrap_err();
575        assert!(
576            err.contains("quorum_threshold"),
577            "error should mention quorum_threshold: {err}"
578        );
579
580        // close_group_size = 0 -> validation fails.
581        let config = ReplicationConfig {
582            close_group_size: 0,
583            ..ReplicationConfig::default()
584        };
585        let err = config.validate().unwrap_err();
586        assert!(
587            err.contains("close_group_size"),
588            "error should mention close_group_size: {err}"
589        );
590
591        // neighbor_sync interval min > max -> validation fails.
592        let config = ReplicationConfig {
593            neighbor_sync_interval_min: Duration::from_secs(200),
594            neighbor_sync_interval_max: Duration::from_secs(100),
595            ..ReplicationConfig::default()
596        };
597        let err = config.validate().unwrap_err();
598        assert!(
599            err.contains("neighbor_sync_interval"),
600            "error should mention neighbor_sync_interval: {err}"
601        );
602
603        // self_lookup interval min > max -> validation fails.
604        let config = ReplicationConfig {
605            self_lookup_interval_min: Duration::from_secs(999),
606            self_lookup_interval_max: Duration::from_secs(1),
607            ..ReplicationConfig::default()
608        };
609        let err = config.validate().unwrap_err();
610        assert!(
611            err.contains("self_lookup_interval"),
612            "error should mention self_lookup_interval: {err}"
613        );
614
615        // audit_tick interval min > max -> validation fails.
616        let config = ReplicationConfig {
617            audit_tick_interval_min: Duration::from_secs(500),
618            audit_tick_interval_max: Duration::from_secs(10),
619            ..ReplicationConfig::default()
620        };
621        let err = config.validate().unwrap_err();
622        assert!(
623            err.contains("audit_tick_interval"),
624            "error should mention audit_tick_interval: {err}"
625        );
626    }
627
628    /// Scenario 26: Dynamic paid-list threshold for undersized set.
629    /// With PaidGroupSize=8, `ConfirmNeeded` = floor(8/2)+1 = 5.
630    #[test]
631    fn scenario_26_dynamic_paid_threshold_undersized() {
632        assert_eq!(ReplicationConfig::confirm_needed(8), 5, "floor(8/2)+1 = 5");
633
634        // Additional boundary checks for small paid groups.
635        assert_eq!(
636            ReplicationConfig::confirm_needed(1),
637            1,
638            "single peer requires 1 confirmation"
639        );
640        assert_eq!(
641            ReplicationConfig::confirm_needed(2),
642            2,
643            "2 peers require 2 confirmations"
644        );
645        assert_eq!(
646            ReplicationConfig::confirm_needed(3),
647            2,
648            "3 peers require 2 confirmations"
649        );
650        assert_eq!(
651            ReplicationConfig::confirm_needed(0),
652            1,
653            "0 peers yields floor(0/2)+1 = 1 (degenerate case)"
654        );
655    }
656
657    /// Scenario 31: Consecutive audit ticks occur on randomized intervals
658    /// bounded by the configured `[audit_tick_interval_min, audit_tick_interval_max]`
659    /// window.
660    #[test]
661    fn scenario_31_audit_cadence_within_jitter_bounds() {
662        let config = ReplicationConfig {
663            audit_tick_interval_min: Duration::from_secs(1800),
664            audit_tick_interval_max: Duration::from_secs(3600),
665            ..ReplicationConfig::default()
666        };
667
668        // Sample many intervals and verify each is within bounds.
669        let iterations = 100;
670        let mut saw_different = false;
671        let mut prev = Duration::ZERO;
672
673        for _ in 0..iterations {
674            let interval = config.random_audit_tick_interval();
675            assert!(
676                interval >= config.audit_tick_interval_min,
677                "interval {interval:?} below min {:?}",
678                config.audit_tick_interval_min,
679            );
680            assert!(
681                interval <= config.audit_tick_interval_max,
682                "interval {interval:?} above max {:?}",
683                config.audit_tick_interval_max,
684            );
685            if interval != prev && prev != Duration::ZERO {
686                saw_different = true;
687            }
688            prev = interval;
689        }
690
691        // With 100 samples from a 30-minute range, at least two should differ
692        // (probabilistically near-certain).
693        assert!(
694            saw_different,
695            "audit intervals should exhibit randomized jitter across samples"
696        );
697    }
698}