Assistant: codex Assistant-Model: gpt-6-astra Assistant-Session: 01a0e6f0-a6f6-7b93-9124-55ef584930e2
60 lines
3.1 KiB
Python
60 lines
3.1 KiB
Python
"""Deterministic bounded-domain sweeps; true-time oracle is independent of UTC arithmetic."""
|
|
from itertools import product
|
|
import json
|
|
|
|
from railiance_clock.client import Clock, FileTrust
|
|
from railiance_clock.core import Tick, elapsed
|
|
from railiance_clock.protocol import sign
|
|
|
|
|
|
def test_elapsed_encloses_admitted_drift_and_timer_disagreement():
|
|
start = Tick(0, 0, 'boot')
|
|
for measured, ppm, disagreement in product(
|
|
(0, 1, 999, 1_000_000, 1_000_000_000), (1, 1000, 100000), (0, 1, 1000)):
|
|
lo, hi = elapsed(start, Tick(measured, measured + disagreement, 'boot'),
|
|
ppm=ppm, resolution_ns=1, suspend_tolerance_ns=1000)
|
|
# Assumption being tested: admitted timer error is bounded relative to
|
|
# measured elapsed time, plus resolution and observed clock disagreement.
|
|
drift = measured * ppm // 1_000_000
|
|
assert lo <= max(0, measured - drift) <= hi
|
|
assert lo <= measured + disagreement + drift <= hi
|
|
|
|
|
|
def test_interval_encloses_true_receipt_across_asymmetry_and_drift(setup, monkeypatch):
|
|
path, key, _, _ = setup
|
|
import railiance_clock.client as client
|
|
original_verify = client.verify
|
|
base = 100_000_000_000
|
|
# 324 cases cover each direction of extreme asymmetry, server offset at
|
|
# both edges of its bound, verification delay and admitted timer drift.
|
|
for outbound, inbound, processing, offset, verify_delay, drift_sign in product(
|
|
(0, 1_000_000, 100_000_000), (0, 1_000_000, 100_000_000),
|
|
(0, 10_000_000), (-1_000_000, 0, 1_000_000), (0, 1_000_000, 20_000_000), (-1, 1)):
|
|
class Timer:
|
|
true = 0
|
|
def read(self):
|
|
# 500ppm true-time-relative drift sits within the admitted
|
|
# 1000ppm measured-time-relative bound, including rounding.
|
|
measured = self.true + drift_sign * (self.true * 500 // 1_000_000)
|
|
return Tick(measured, measured, 'boot1')
|
|
timer = Timer()
|
|
data = json.loads(path.read_text())
|
|
from pathlib import Path
|
|
Path(data['rollback_state_file']).write_text(json.dumps(
|
|
{'authority_id': 'railiance01', 'epoch': 'epoch1', 'lower_ns': 0}))
|
|
def transport(request):
|
|
received = base + outbound + offset
|
|
sent = received + processing
|
|
timer.true = outbound + processing + inbound
|
|
return sign(dict(request, epoch='epoch1', policy_id='test-v1', quality='bounded',
|
|
receive_unix_ns=str(received), send_unix_ns=str(sent), error_ns='1000000',
|
|
max_age_ns='5000000000'), key, 'test-key')
|
|
def verify(*args, **kwargs):
|
|
value = original_verify(*args, **kwargs)
|
|
timer.true += verify_delay
|
|
return value
|
|
monkeypatch.setattr(client, 'verify', verify)
|
|
window = Clock(FileTrust(path), timer=timer, transport=transport).read()
|
|
true_now = base + outbound + processing + inbound + verify_delay
|
|
assert window.lower_ns <= true_now <= window.upper_ns, (
|
|
outbound, inbound, processing, offset, verify_delay, drift_sign, window)
|