diff --git a/rotatyvote/app.py b/rotatyvote/app.py index 412656f..aff5ab9 100644 --- a/rotatyvote/app.py +++ b/rotatyvote/app.py @@ -736,10 +736,14 @@ async def voter_vote(request: Request): tokens = d.get("presence") if not isinstance(tokens, list): return err("presence_required") - window = config.TOKEN_GRACE_MS + need * p["interval_ms"] - slots = {resolve_token(p, str(t)[:64], window) for t in tokens[:20]} + # a slow phone can take well over need * interval to see enough distinct codes, so the codes may span + # the same slack as an enrollment, as long as one of them is fresh: the phone sees the screen right now + tokens = [str(t)[:64] for t in tokens[:20]] + window = config.ATTEMPT_SLACK_MS + need * p["interval_ms"] + slots = {resolve_token(p, t, window) for t in tokens} slots.discard(None) - if len(slots) < need: + fresh = any(resolve_token(p, t, config.TOKEN_GRACE_MS) is not None for t in tokens) + if len(slots) < need or not fresh: return err("presence_required") e = db.cast(pid, qid, v["id"], choices) if e: diff --git a/static/js/vote.js b/static/js/vote.js index 084bf77..8ebaa69 100644 --- a/static/js/vote.js +++ b/static/js/vote.js @@ -453,9 +453,14 @@ function collectPresence(n) { paint(); $("#p-msg").textContent = ""; dlg.showModal(); + const seenAt = new Map(); const sc = makeScanner($("#p-video"), (t) => { if (got.length >= n || got.includes(t)) return; + // the server takes codes spread over ~8 s + n intervals: on a slow phone the oldest ones lapse, drop them + const now = performance.now(); + while (got.length && now - seenAt.get(got[0]) > 9000) got.shift(); got.push(t); + seenAt.set(t, now); paint(); navigator.vibrate?.(15); if (got.length >= n) setTimeout(() => finish(got), FILL_MS); diff --git a/tests/test_e2e.py b/tests/test_e2e.py index 04cfaa5..9834b16 100644 --- a/tests/test_e2e.py +++ b/tests/test_e2e.py @@ -518,9 +518,10 @@ def test_vote_needs_fresh_presence_scans(server): await asyncio.sleep(3.5) # photo of the screen taken earlier r = await a.post(url, json={"question_id": qid, "choices": [0], "presence": old}) assert r.json()["error"] == "presence_required" - fresh = [await next_token() for _ in range(3)] - r = await a.post(url, json={"question_id": qid, "choices": [1], "presence": fresh}) - assert r.status_code == 200 + # slow phone: two codes read a while ago and one just now, all distinct, counts + mixed = old[1:] + [await next_token()] + r = await a.post(url, json={"question_id": qid, "choices": [1], "presence": mixed}) + assert r.status_code == 200, r.text await a.aclose() asyncio.run(run())