feat(fairdraw): surface the validateReveal integrity verdict in the UI
RevealDraw claimed a re-roll after a published commit is auditable+rejected, but validateReveal had zero production callers. The reveal flow now replays the signed log and renders a Draw integrity: valid/rejected badge; a duplicate or pre-commit reveal flips it to rejected.
This commit is contained in:
1 parent
d35d4a3772
commit
480232838f
3 files changed
+142
-2
No files matched your search
@@ -146,6 +146,25 @@
|
||||
color: var(--color-alert);
|
||||
}
|
||||
|
||||
.integrityValid,
|
||||
.integrityRejected {
|
||||
align-self: flex-start;
|
||||
padding: 0.35rem 0.8rem;
|
||||
border-radius: 999px;
|
||||
font-size: 0.78rem;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.integrityValid {
|
||||
background: rgba(46, 204, 113, 0.12);
|
||||
color: #2ecc71;
|
||||
}
|
||||
|
||||
.integrityRejected {
|
||||
background: rgba(229, 72, 77, 0.12);
|
||||
color: var(--color-alert);
|
||||
}
|
||||
|
||||
.winner {
|
||||
font-size: 0.9rem;
|
||||
font-weight: 700;
|
||||
|
||||
@@ -4,7 +4,7 @@ import { act, cleanup, render, screen, waitFor } from "@testing-library/react";
|
||||
import userEvent from "@testing-library/user-event";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { db } from "../../lib/db";
|
||||
import { readAll } from "../../lib/eventlog";
|
||||
import { append, readAll } from "../../lib/eventlog";
|
||||
import {
|
||||
commit,
|
||||
drawOutcome,
|
||||
@@ -351,6 +351,84 @@ describe("RevealDraw append-to-log commit requirement (plan todo 8, require a pu
|
||||
});
|
||||
});
|
||||
|
||||
describe("RevealDraw replay-derived integrity badge (re-roll is auditable and rejected)", () => {
|
||||
const PARTICIPANTS = ["Resident A", "Resident B"];
|
||||
|
||||
it("renders a replay-derived 'valid' integrity badge for a clean single commit+reveal draw", async () => {
|
||||
await init();
|
||||
const user = userEvent.setup();
|
||||
render(<RevealDraw participants={PARTICIPANTS} />);
|
||||
|
||||
await act(async () => {
|
||||
await user.click(screen.getByRole("button", { name: "Start Draw" }));
|
||||
});
|
||||
await waitFor(() =>
|
||||
expect(screen.getByTestId("reveal-draw-commit-hash")).toBeInTheDocument(),
|
||||
);
|
||||
await act(async () => {
|
||||
await user.click(screen.getByRole("button", { name: "Reveal" }));
|
||||
});
|
||||
await waitFor(() => expect(screen.getByTestId("reveal-draw-revealed")).toBeInTheDocument());
|
||||
|
||||
const badge = screen.getByTestId("reveal-draw-integrity-badge");
|
||||
expect(badge).toHaveAttribute("data-state", "valid");
|
||||
expect(badge).toHaveTextContent("Draw integrity: valid");
|
||||
|
||||
// The badge must reflect the SAME validateReveal() verdict an external
|
||||
// auditor would compute over the real, cold-replayed signed log - one
|
||||
// published commit strictly before exactly one verifying reveal.
|
||||
const log = await readAll();
|
||||
const commitEntry = log.find((e) => isFairDrawCommitPayload(e.payload));
|
||||
expect(commitEntry).toBeDefined();
|
||||
const { drawId } = commitEntry!.payload as { drawId: string };
|
||||
expect(log.filter((e) => isFairDrawRevealPayload(e.payload))).toHaveLength(1);
|
||||
expect(await validateReveal(log, drawId)).toBe(true);
|
||||
});
|
||||
|
||||
it("flips the integrity badge to 'rejected' when a second fairdraw.reveal (a re-roll) is appended for the same drawId", async () => {
|
||||
await init();
|
||||
const user = userEvent.setup();
|
||||
|
||||
// Simulate an auditable re-roll: the drawer re-broadcasts a SECOND reveal
|
||||
// for the same published drawId. Both reveals go through the real signed
|
||||
// append() path, so the component's own validateReveal() replay over the
|
||||
// real log (readAll) sees two reveals for one drawId and rejects it.
|
||||
const appendDrawEvent: typeof append = async (payload) => {
|
||||
const result = await append(payload);
|
||||
if ((payload as { type?: string }).type === "fairdraw.reveal") {
|
||||
await append(payload);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
render(<RevealDraw participants={PARTICIPANTS} appendDrawEvent={appendDrawEvent} />);
|
||||
|
||||
await act(async () => {
|
||||
await user.click(screen.getByRole("button", { name: "Start Draw" }));
|
||||
});
|
||||
await waitFor(() =>
|
||||
expect(screen.getByTestId("reveal-draw-commit-hash")).toBeInTheDocument(),
|
||||
);
|
||||
await act(async () => {
|
||||
await user.click(screen.getByRole("button", { name: "Reveal" }));
|
||||
});
|
||||
await waitFor(() => expect(screen.getByTestId("reveal-draw-revealed")).toBeInTheDocument());
|
||||
|
||||
const badge = screen.getByTestId("reveal-draw-integrity-badge");
|
||||
expect(badge).toHaveAttribute("data-state", "rejected");
|
||||
expect(badge).toHaveTextContent("rejected");
|
||||
|
||||
// Prove the log really holds the duplicate reveal the badge rejected -
|
||||
// the verdict is replay-derived, not faked.
|
||||
const log = await readAll();
|
||||
const reveals = log.filter((e) => isFairDrawRevealPayload(e.payload));
|
||||
expect(reveals).toHaveLength(2);
|
||||
const commitEntry = log.find((e) => isFairDrawCommitPayload(e.payload));
|
||||
const { drawId } = commitEntry!.payload as { drawId: string };
|
||||
expect(await validateReveal(log, drawId)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("RevealDraw empty-participants guard", () => {
|
||||
it("shows an empty-state note and no Start Draw button when participants is empty - never commits an unresolvable draw", async () => {
|
||||
const appendDrawEvent = vi.fn().mockResolvedValue({ id: 1 });
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
hashParticipants,
|
||||
isFairDrawCommitPayload,
|
||||
reveal,
|
||||
validateReveal,
|
||||
verify,
|
||||
type Revealed,
|
||||
} from "../../lib/fairdraw";
|
||||
@@ -49,6 +50,17 @@ type Phase =
|
||||
revealed: Revealed;
|
||||
verified: boolean;
|
||||
winner?: string;
|
||||
/**
|
||||
* Replay-derived audit verdict for THIS draw, computed from
|
||||
* `validateReveal()` (src/lib/fairdraw.ts) over the whole signed Event
|
||||
* Log - `true` only if the log holds exactly one published commit
|
||||
* strictly before exactly one verifying reveal for this `drawId`. A
|
||||
* re-roll (a second `fairdraw.reveal` for the same `drawId`) or a
|
||||
* reveal with no prior published commit flips this to `false`, which
|
||||
* is what makes the screen's "re-rolling ... is auditable and
|
||||
* rejected" claim real rather than aspirational.
|
||||
*/
|
||||
integrity: boolean;
|
||||
};
|
||||
|
||||
export interface RevealDrawProps {
|
||||
@@ -236,6 +248,18 @@ export function RevealDraw({
|
||||
}
|
||||
}
|
||||
|
||||
// Replay the signed Event Log and compute THIS draw's audit verdict with
|
||||
// the SAME `validateReveal()` an external log auditor would run - the
|
||||
// function was previously called only from tests, so the screen's
|
||||
// "re-rolling ... is auditable and rejected" claim had no in-UI teeth.
|
||||
// It is a pure read over `{ payload, lamportTs }` entries, identical on a
|
||||
// live log or a cold-replayed one, and flips to `false` the moment a
|
||||
// second `fairdraw.reveal` (a re-roll) or a pre-commit reveal exists for
|
||||
// this `drawId`. Read through the same `readDrawLog` seam the commit gate
|
||||
// uses so it stays the real `readAll()` in production.
|
||||
const auditLog = await (readDrawLog ?? readAll)();
|
||||
const integrity = await validateReveal(auditLog, committed.drawId);
|
||||
|
||||
// Best-effort cross-device sync (plan todo 3): fire-and-forget, never
|
||||
// blocks or affects the local-first reveal/credit writes that just
|
||||
// completed above.
|
||||
@@ -248,8 +272,17 @@ export function RevealDraw({
|
||||
revealed: published,
|
||||
verified,
|
||||
winner,
|
||||
integrity,
|
||||
});
|
||||
}, [phase, participants, participantAccounts, tamperReveal, appendWalletCredit, appendDrawEvent]);
|
||||
}, [
|
||||
phase,
|
||||
participants,
|
||||
participantAccounts,
|
||||
tamperReveal,
|
||||
appendWalletCredit,
|
||||
appendDrawEvent,
|
||||
readDrawLog,
|
||||
]);
|
||||
|
||||
return (
|
||||
<section className={styles.screen} aria-label="Fair Draw">
|
||||
@@ -319,6 +352,16 @@ export function RevealDraw({
|
||||
{phase.verified ? "\u2713 Verified" : "\u2717 Invalid"}
|
||||
</div>
|
||||
|
||||
<div
|
||||
className={phase.integrity ? styles.integrityValid : styles.integrityRejected}
|
||||
data-testid="reveal-draw-integrity-badge"
|
||||
data-state={phase.integrity ? "valid" : "rejected"}
|
||||
>
|
||||
{phase.integrity
|
||||
? "Draw integrity: valid"
|
||||
: "Draw integrity: rejected (duplicate or pre-commit reveal)"}
|
||||
</div>
|
||||
|
||||
{phase.verified ? (
|
||||
<div className={styles.winner} data-testid="reveal-draw-winner">
|
||||
Winner: {phase.winner}
|
||||
|
||||
Reference in new issue
Block a user