refactor(payment): tighten quote-freshness, raise skew to 300s#83
Merged
jacderida merged 1 commit intoWithAutonomi:rc-2026.4.3from Apr 29, 2026
Conversation
…window to 300s `validate_quote_timestamps` had an unreachable `else` arm: if `now.duration_since(quote.timestamp)` returned `Err` (quote in the future), then `quote.timestamp.duration_since(now)` necessarily returns `Ok`, so the path that returned "has invalid timestamp" could never fire. Replaced the nested match with `SystemTimeError::duration()`, dropping the dead arm and its misleading error string. Also switched the boundary checks from `Duration::as_secs() > CONST` to `Duration > Duration::from_secs(CONST)`. The old form floored fractional seconds, silently widening both windows by up to ~1s past the documented limits. Behavior change: forward-skew tolerance bumped from 60s to 300s and the constant renamed `QUOTE_CLOCK_SKEW_TOLERANCE_SECS` -> `QUOTE_FUTURE_SKEW_TOLERANCE_SECS`. The previous 60s was too tight for unmanaged P2P nodes with imperfect clock sync; 300s gives realistic drift headroom while still being negligible against the 24h max age. Past-side skew is intentionally absorbed entirely by `QUOTE_MAX_AGE_SECS` — there is no separate past tolerance. Tests updated: the "beyond tolerance" test now uses 360s in the future to clear the new 300s threshold. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Refactors payment quote “freshness” validation in PaymentVerifier to make timestamp checks precise (sub-second correct), remove dead code paths, and relax forward-clock-skew tolerance for real-world P2P node drift.
Changes:
- Simplifies future/past timestamp handling by using
SystemTimeError::duration()and removing an unreachable “invalid timestamp” arm. - Compares
Durationvalues directly instead ofas_secs()to avoid implicit flooring. - Raises forward-skew tolerance to 300s and renames the constant accordingly; updates related tests/comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
jacderida
approved these changes
Apr 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
validate_quote_timestampshad an unreachableelsearm: whennow.duration_since(quote.timestamp)returnsErr,quote.timestamp.duration_since(now)necessarily returnsOk, so the "invalid timestamp" path could never fire. Replaced the nested match withSystemTimeError::duration(), removing the dead arm and its misleading error string.Duration::as_secs() > CONSTwithDuration > Duration::from_secs(CONST). The old form floored fractional seconds, silently widening both windows by up to ~1 s past the documented limits.QUOTE_CLOCK_SKEW_TOLERANCE_SECS→QUOTE_FUTURE_SKEW_TOLERANCE_SECS. 60 s was too tight for unmanaged P2P nodes with imperfect clock sync; 300 s gives realistic drift headroom while remaining negligible against the 24 h max age. Past-side skew is intentionally absorbed entirely byQUOTE_MAX_AGE_SECS— there is no separate past tolerance.No public API change (constant is
pub(crate)-equivalent — module-private). Behavior change is limited to which forward-dated quotes are accepted.Test plan
cargo fmt --all -- --checkcargo clippy --all-targets --all-features -- -D warningscargo test --lib --features test-utils payment::verifier::tests— 44/44 passtest_quote_within_clock_skew_tolerance_accepted(30 s future, well within 300 s) ✓test_quote_just_beyond_clock_skew_tolerance_rejectedupdated to 360 s future, now exceeds the 300 s threshold ✓test_quote_23h_old_still_accepted— max-age window unchanged ✓test_future_timestamp_rejected— still rejects far-future timestamps ✓🤖 Generated with Claude Code