forked from Sub-Rosa-Issue/sub-rosa-issue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettlement-guard.test.ts
More file actions
365 lines (310 loc) · 12.5 KB
/
Copy pathsettlement-guard.test.ts
File metadata and controls
365 lines (310 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Copyright (c) 2026 Sub Rosa contributors
// settlement-guard.test.ts
//
// Tests for Issue #79 — keeper duplicate-settlement suppression.
//
// All tests are fully offline: no RPC, no real transactions, no Drand.
// The keeper is exercised through the SettlementGuard helper directly, and
// through a thin mock of the closeRound / settle code-path that mirrors
// keeper.ts's actual state-machine logic.
import { describe, it } from "node:test";
import assert from "node:assert/strict";
import { createSettlementGuard } from "./settlement-guard.js";
import type {
DuplicateSkipEvent,
SettlementGuard,
} from "./settlement-guard.js";
// ---------------------------------------------------------------------------
// Minimal mock helpers
// ---------------------------------------------------------------------------
/** Build a fake on-chain round object at a given status tag. */
function mockRound(tag: string) {
return {
status: { tag },
reveal_round: 1n,
reveal_deadline: BigInt(1_700_000_000 - 3600),
winner: tag === "Cleared" ? "GABC" : undefined,
};
}
/**
* A tiny simulation of the settle phase in keeper.ts's closeRound, wired
* through a SettlementGuard. Returns a structured description of what
* happened so tests can assert without parsing log strings.
*/
async function simulateSettle(
guard: SettlementGuard,
roundId: bigint,
opts: {
onChainStatus: string;
/** If provided, sdk.settle() throws this error. */
settleThrows?: Error;
log?: (msg: string) => void;
},
): Promise<{
settled: boolean;
skipped: boolean;
skipEvent?: DuplicateSkipEvent;
retriedAsRetryable: boolean;
finalGuardStatus: string;
}> {
const log = opts.log ?? (() => {});
const result = {
settled: false,
skipped: false,
skipEvent: undefined as DuplicateSkipEvent | undefined,
retriedAsRetryable: false,
finalGuardStatus: "",
};
// Mirror the guard check that a real keeper would do before sdk.settle().
const check = guard.canSettle(roundId);
if (!check.allowed) {
result.skipped = true;
result.skipEvent = check.event;
log(
`[settlement_skipped_duplicate] round=${roundId} reason=${check.event.skippedDuplicateReason} lastReason=${check.event.lastReason}`,
);
result.finalGuardStatus = guard.getEntry(roundId)!.status;
return result;
}
// On-chain terminal statuses: guard them without dispatching a tx.
if (opts.onChainStatus === "Settled" || opts.onChainStatus === "Voided") {
guard.markTerminal(roundId, `on-chain status is ${opts.onChainStatus}`);
result.finalGuardStatus = guard.getEntry(roundId)!.status;
return result;
}
// Attempt settlement.
guard.markSubmitted(roundId);
try {
if (opts.settleThrows) throw opts.settleThrows;
// Mock successful settle — no real tx.
result.settled = true;
guard.markTerminal(roundId, "settled ok");
log(`settled round ${roundId}`);
} catch (e) {
const msg = e instanceof Error ? e.message : String(e);
// Classify: AlreadySettled is terminal; other errors are retryable.
if (msg.includes("AlreadySettled") || msg.includes("WrongStatus")) {
guard.markTerminal(roundId, `skipped: ${msg}`);
result.skipped = true;
} else {
guard.markRetryable(roundId, msg);
result.retriedAsRetryable = true;
}
}
result.finalGuardStatus = guard.getEntry(roundId)!.status;
return result;
}
// ---------------------------------------------------------------------------
// 1. Guard state-machine unit tests
// ---------------------------------------------------------------------------
describe("SettlementGuard state machine", () => {
it("allows the first settlement attempt (pending → submitted → terminal)", () => {
const guard = createSettlementGuard();
const id = 1n;
// Before any call, canSettle allows.
const first = guard.canSettle(id);
assert.equal(first.allowed, true);
// After markSubmitted, canSettle blocks.
guard.markSubmitted(id);
const second = guard.canSettle(id);
assert.equal(second.allowed, false);
if (!second.allowed) {
assert.equal(second.event.skippedDuplicateReason, "submitted");
assert.equal(second.event.event, "settlement_skipped_duplicate");
assert.equal(second.event.roundId, "1");
}
// After markTerminal, canSettle still blocks with "terminal".
guard.markTerminal(id, "settled ok");
const third = guard.canSettle(id);
assert.equal(third.allowed, false);
if (!third.allowed) {
assert.equal(third.event.skippedDuplicateReason, "terminal");
}
});
it("skips a round already in submitted status (duplicate observation)", () => {
const guard = createSettlementGuard();
const id = 42n;
guard.markSubmitted(id);
const check = guard.canSettle(id);
assert.equal(check.allowed, false);
if (!check.allowed) {
assert.equal(check.event.skippedDuplicateReason, "submitted");
assert.equal(check.event.roundId, "42");
// Structured event must carry the field name the issue requires.
assert.ok(
"skippedDuplicateReason" in check.event,
"event must include skippedDuplicateReason",
);
}
});
it("skips a terminal round without attempting another settle", () => {
const guard = createSettlementGuard();
const id = 7n;
guard.markTerminal(id, "settled via concurrent keeper");
const check = guard.canSettle(id);
assert.equal(check.allowed, false);
if (!check.allowed) {
assert.equal(check.event.skippedDuplicateReason, "terminal");
assert.equal(check.event.lastReason, "settled via concurrent keeper");
}
});
it("makes a failed attempt retryable — does not permanently suppress it", () => {
const guard = createSettlementGuard();
const id = 99n;
// Simulate: submitted, then a network timeout.
guard.markSubmitted(id);
guard.markRetryable(id, "rpc timeout");
// Must be allowed again on the next polling cycle.
const check = guard.canSettle(id);
assert.equal(check.allowed, true, "retryable round must be re-allowed");
assert.equal(guard.getEntry(id)!.status, "pending");
assert.match(guard.getEntry(id)!.reason, /retryable/);
});
it("does not bleed state between independent rounds", () => {
const guard = createSettlementGuard();
guard.markSubmitted(10n);
guard.markTerminal(10n, "settled ok");
// Round 11 and 12 must remain independently settable.
assert.equal(guard.canSettle(11n).allowed, true);
assert.equal(guard.canSettle(12n).allowed, true);
// Round 10 must still be terminal.
assert.equal(guard.canSettle(10n).allowed, false);
});
it("getEntry returns undefined for an untracked round", () => {
const guard = createSettlementGuard();
assert.equal(guard.getEntry(999n), undefined);
});
it("entries() reflects all tracked rounds in insertion order", () => {
const guard = createSettlementGuard();
guard.canSettle(3n); // triggers getOrCreate
guard.canSettle(1n);
guard.canSettle(2n);
const ids = guard.entries().map((e) => e.roundId);
assert.deepEqual(ids, [3n, 1n, 2n]);
});
});
// ---------------------------------------------------------------------------
// 2. Duplicate-settlement simulation tests (mirrors keeper.ts's settle phase)
// ---------------------------------------------------------------------------
describe("Keeper duplicate-settlement suppression (simulated)", () => {
it("does not attempt a duplicate settle when the same round is observed twice", async () => {
const guard = createSettlementGuard();
const id = 5n;
// First observation — round is Cleared, settle succeeds.
const first = await simulateSettle(guard, id, { onChainStatus: "Cleared" });
assert.equal(first.settled, true);
assert.equal(first.skipped, false);
// Second observation — same round, same polling cycle.
const second = await simulateSettle(guard, id, { onChainStatus: "Cleared" });
assert.equal(second.settled, false);
assert.equal(second.skipped, true);
assert.ok(second.skipEvent, "must emit a skip event");
assert.equal(second.skipEvent!.skippedDuplicateReason, "terminal");
assert.equal(second.finalGuardStatus, "terminal");
});
it("skips settlement when the round is already terminal on-chain (Settled)", async () => {
const guard = createSettlementGuard();
const id = 8n;
const res = await simulateSettle(guard, id, { onChainStatus: "Settled" });
// No tx dispatched (settled = false); guard moves to terminal.
assert.equal(res.settled, false);
assert.equal(res.finalGuardStatus, "terminal");
});
it("skips settlement when the round is already terminal on-chain (Voided)", async () => {
const guard = createSettlementGuard();
const id = 9n;
const res = await simulateSettle(guard, id, { onChainStatus: "Voided" });
assert.equal(res.settled, false);
assert.equal(res.finalGuardStatus, "terminal");
});
it("contract AlreadySettled error marks the round terminal, not retryable", async () => {
const guard = createSettlementGuard();
const id = 15n;
const res = await simulateSettle(guard, id, {
onChainStatus: "Cleared",
settleThrows: new Error("AlreadySettled"),
});
assert.equal(res.skipped, true);
assert.equal(res.retriedAsRetryable, false);
assert.equal(res.finalGuardStatus, "terminal");
// And a subsequent observation must also be skipped.
const again = await simulateSettle(guard, id, { onChainStatus: "Cleared" });
assert.equal(again.skipped, true);
assert.ok(again.skipEvent);
assert.equal(again.skipEvent!.skippedDuplicateReason, "terminal");
});
it("retryable network error resets to pending so next cycle can retry", async () => {
const guard = createSettlementGuard();
const id = 20n;
const first = await simulateSettle(guard, id, {
onChainStatus: "Cleared",
settleThrows: new Error("rpc connection timeout"),
});
assert.equal(first.retriedAsRetryable, true);
assert.equal(first.finalGuardStatus, "pending");
// Next cycle: allowed to retry.
const retry = await simulateSettle(guard, id, { onChainStatus: "Cleared" });
assert.equal(retry.settled, true);
assert.equal(retry.finalGuardStatus, "terminal");
});
it("independent rounds are never affected by each other's state", async () => {
const guard = createSettlementGuard();
// Settle round 30 and mark it terminal.
await simulateSettle(guard, 30n, { onChainStatus: "Cleared" });
assert.equal(guard.getEntry(30n)!.status, "terminal");
// Rounds 31 and 32 must still be allowed.
const r31 = await simulateSettle(guard, 31n, { onChainStatus: "Cleared" });
const r32 = await simulateSettle(guard, 32n, { onChainStatus: "Cleared" });
assert.equal(r31.settled, true);
assert.equal(r32.settled, true);
// Round 30 is still blocked.
const r30again = await simulateSettle(guard, 30n, {
onChainStatus: "Cleared",
});
assert.equal(r30again.skipped, true);
});
it("skip event includes skippedDuplicateReason and roundId as a string", async () => {
const guard = createSettlementGuard();
const id = 77n;
guard.markTerminal(id, "pre-settled externally");
const check = guard.canSettle(id);
assert.equal(check.allowed, false);
if (!check.allowed) {
const ev = check.event;
assert.equal(typeof ev.roundId, "string", "roundId must be a string");
assert.ok(
"skippedDuplicateReason" in ev,
"event must have skippedDuplicateReason field",
);
assert.equal(ev.event, "settlement_skipped_duplicate");
assert.equal(ev.skippedDuplicateReason, "terminal");
assert.equal(ev.lastReason, "pre-settled externally");
}
});
});
// ---------------------------------------------------------------------------
// 3. Bounded-retry regression guard
// ---------------------------------------------------------------------------
describe("Bounded retry invariant", () => {
it("a round that always fails stays retryable — never permanently suppressed", async () => {
const guard = createSettlementGuard();
const id = 50n;
const maxAttempts = 5;
for (let attempt = 0; attempt < maxAttempts; attempt++) {
const res = await simulateSettle(guard, id, {
onChainStatus: "Cleared",
settleThrows: new Error("transient rpc error"),
});
assert.equal(
res.retriedAsRetryable,
true,
`attempt ${attempt + 1} must remain retryable`,
);
assert.equal(
guard.getEntry(id)!.status,
"pending",
`guard must be pending after attempt ${attempt + 1}`,
);
}
});
});