diff --git a/crates/rns-link/src/link.rs b/crates/rns-link/src/link.rs index f0a8bc4..555e05c 100644 --- a/crates/rns-link/src/link.rs +++ b/crates/rns-link/src/link.rs @@ -1374,6 +1374,19 @@ impl Link { self.rtt.map(|r| r.as_secs_f64()).unwrap_or(0.0) } + /// Proof window for an ordinary packet on this established Link. + /// + /// Link traffic uses measured round-trip time times + /// [`TRAFFIC_TIMEOUT_FACTOR`], with a five-millisecond minimum. This is + /// neither Link establishment timing nor the idle keepalive interval. + /// A missing RTT uses a conservative half-second RTT fallback. + pub fn packet_proof_timeout(&self) -> Duration { + self.rtt + .unwrap_or(Duration::from_millis(500)) + .saturating_mul(TRAFFIC_TIMEOUT_FACTOR as u32) + .max(Duration::from_millis(5)) + } + /// Drive the link state machine forward; call periodically from the owning actor. #[tracing::instrument( level = "trace", @@ -1762,6 +1775,21 @@ mod tests { use rns_crypto::ed25519::Ed25519PrivateKey; use rns_crypto::sha::full_hash; + #[test] + fn packet_proof_clock_uses_rtt_not_link_keepalive() { + let (mut link, _) = Link::new_initiator([0x61; 16], 1); + assert_eq!(link.packet_proof_timeout(), Duration::from_secs(3)); + for (rtt, expected) in [ + (Duration::ZERO, Duration::from_millis(5)), + (Duration::from_millis(200), Duration::from_millis(1200)), + (Duration::from_secs(90), Duration::from_secs(540)), + (Duration::MAX, Duration::MAX), + ] { + link.rtt = Some(rtt); + assert_eq!(link.packet_proof_timeout(), expected); + } + } + #[test] fn test_initiator_creation() { let dest_hash = [0xAA; 16]; diff --git a/crates/rns-protocol/src/resource.rs b/crates/rns-protocol/src/resource.rs index b328b44..b762f43 100644 --- a/crates/rns-protocol/src/resource.rs +++ b/crates/rns-protocol/src/resource.rs @@ -1511,6 +1511,37 @@ impl OutboundTransfer { } } + /// Exact finite wait owned by this sender, as `(started_at, timeout)`. + /// + /// Embedders can mirror this deadline without imposing an absolute age + /// limit on a healthy Resource. Advertisement retries are included in the + /// returned envelope; transferring/proof waits advance only when the + /// receiver requests new parts. A delayed observation must retain its + /// original `started_at`, not restart the timeout on receipt. Terminal or + /// not-yet-advertised transfers have no active wait. + pub fn timeout_window(&self) -> Option<(Instant, Duration)> { + match self.resource.state { + ResourceState::Advertised => { + let attempt = self + .rtt + .saturating_mul(rns_link::constants::TRAFFIC_TIMEOUT_FACTOR as u32) + .saturating_add(Duration::from_secs_f64(PROCESSING_GRACE)); + Some(( + self.started_at, + attempt.saturating_mul( + MAX_ADV_RETRIES + .saturating_sub(self.retries) + .saturating_add(1) as u32, + ), + )) + } + ResourceState::Transferring | ResourceState::AwaitingProof => { + Some((self.last_progress, self.progress_timeout())) + } + _ => None, + } + } + /// Consume a hashmap-update frame from the receiver. /// /// Wire layout: @@ -3580,16 +3611,41 @@ mod tests { #[test] fn resource_watchdog_saturates_extreme_public_rtt_without_panicking() { let (_, mut sender, mut receiver) = loss_test_pair(8192, Duration::MAX); + assert_eq!(sender.timeout_window().unwrap().1, Duration::MAX); assert_eq!(sender.check_timeout(), TransferAction::None); let TransferAction::SendRequest(request) = receiver.request_next() else { panic!() }; sender.handle_request(&request); assert_eq!(sender.progress_timeout(), Duration::MAX); + assert_eq!(sender.timeout_window().unwrap().1, Duration::MAX); assert_eq!(sender.check_timeout(), TransferAction::None); assert_eq!(sender.tick(), TransferAction::None); } + #[test] + fn resource_timeout_observation_tracks_only_owned_bounded_phases() { + let mut sender = + OutboundTransfer::new(vec![0x81; 8192], false, Duration::from_secs(2)).unwrap(); + assert!(sender.timeout_window().is_none()); + assert!(matches!( + sender.tick(), + TransferAction::SendAdvertisement(_) + )); + let first = sender.timeout_window().unwrap(); + assert_eq!(first.1, Duration::from_secs(65)); + sender.started_at = Instant::now() - Duration::from_secs(14); + assert!(matches!( + sender.check_timeout(), + TransferAction::SendAdvertisement(_) + )); + let retry = sender.timeout_window().unwrap(); + assert_eq!(retry.1, Duration::from_secs(52)); + assert!(retry.0 >= first.0); + sender.handle_cancel(); + assert!(sender.timeout_window().is_none()); + } + #[test] fn resource_request_cancel_is_visible_to_poll_only_owners() { let (_, mut sender, _) = loss_test_pair(20_000, Duration::from_secs(1));