fix(sim): cap crash point so extreme seeds cannot overflow or bankrupt

At u=1 the unsigned quotient exceeded int64 and wrapped negative, so the
rarest and most valuable outcome silently became an instant 1.00x loss.
At u=2 it produced a 2.1-billion-times payout the house could never
cover, which would have left settlement failing and the player unpaid.
The crash point is now capped at the largest multiplier the curve can
express, which is unreachable anyway since the round hits its tick
ceiling first.

FromInt now panics outside the Q32.32 integer range instead of wrapping
a positive input into a negative value.

Raises coverage to 88% overall; adds a Makefile with db-reset, since the
append-only ledger steadily consumes bridge headroom across test runs.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
drjones
2026-08-05 15:52:50 +00:00
parent f2c02e2bde
commit dee3becd47
14 changed files with 790 additions and 42 deletions

View File

@@ -91,6 +91,17 @@ func (f *fixture) startRun() {
}
}
// forceCrashPoint pins the round's crash point so cash-out tests do not depend
// on a random draw. A genuine 1.00x crash is an instant bust where nobody can
// cash out, which is correct behaviour but useless for testing the cash-out
// path.
func (f *fixture) forceCrashPoint(multiplier int64) {
f.t.Helper()
f.room.mu.Lock()
f.room.crashPoint = fixed.FromInt(multiplier)
f.room.mu.Unlock()
}
// advanceTo moves the round to a specific tick without settling.
func (f *fixture) advanceTo(tick int) {
f.t.Helper()
@@ -257,6 +268,7 @@ func TestCashOutOnlyWhileRunning(t *testing.T) {
t.Fatal("cash out accepted during betting")
}
f.startRun()
f.forceCrashPoint(100)
if _, err := f.room.CashOut(id); err != nil {
t.Fatalf("cash out rejected while running: %v", err)
}
@@ -268,6 +280,7 @@ func TestCannotCashOutTwice(t *testing.T) {
f.openBetting()
_ = f.room.PlaceBet(f.ctx, id, pk, "a", 1_000)
f.startRun()
f.forceCrashPoint(100)
if _, err := f.room.CashOut(id); err != nil {
t.Fatal(err)
@@ -282,6 +295,7 @@ func TestCannotCashOutWithoutABet(t *testing.T) {
id, _ := f.player("a", 10_000)
f.openBetting()
f.startRun()
f.forceCrashPoint(100)
if _, err := f.room.CashOut(id); err == nil {
t.Fatal("cash out accepted with no bet placed")
@@ -574,6 +588,7 @@ func TestConcurrentCashOutsYieldOne(t *testing.T) {
f.openBetting()
_ = f.room.PlaceBet(f.ctx, id, pk, "a", 5_000)
f.startRun()
f.forceCrashPoint(100)
const attempts = 10
var wg sync.WaitGroup
@@ -606,6 +621,7 @@ func TestSnapshotReportsCashOutMultiplier(t *testing.T) {
f.openBetting()
_ = f.room.PlaceBet(f.ctx, id, pk, "nick", 5_000)
f.startRun()
f.forceCrashPoint(100)
if _, err := f.room.CashOut(id); err != nil {
t.Fatal(err)