feat(tournament): scheduled events with prize pools

Entry fees collect into a real ledger account rather than a number in a
row, so tournament money obeys the same double-entry invariants as
everything else and every movement is explained by a posting.

Settlement distributes the entire pool: dividing a pool across percentage
shares leaves a remainder, and dropping it would destroy money and break
conservation, so it goes to first place. Settlement claims the tournament
before paying, so two instances cannot both pay out. Cancellation refunds
every entrant and asserts the pool empties exactly.

18 tests including concurrent entry, concurrent settlement, unfunded
entry taking no seat, and books balancing after payout.

Removes an append-only trigger that had been over-applied to entry rows.
An entry is a seat reservation, not a financial record: a seat claimed
but unpaid must be releasable so the player can retry once funded. The
money side stays immutable because it is a ledger posting.

The journey test now derives the expected payout from the published fee
schedule instead of hardcoding it, so it keeps checking something real if
the rake changes.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
drjones
2026-08-05 23:54:18 +00:00
parent ca39e8bad9
commit 8af6fd585e
8 changed files with 1155 additions and 18 deletions

View File

@@ -7,6 +7,7 @@ import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
@@ -483,12 +484,36 @@ func TestFullPlayerJourney(t *testing.T) {
crash := float64(*proof.CrashPoint) / 4294967296.0
// 7. Balance must reflect the outcome exactly: paid at 1.5x if the round
// reached the target, nothing otherwise.
// reached the target, nothing otherwise — net of the disclosed fees.
var after struct {
BalanceMsat int64 `json:"balance_msat"`
}
// Derive the expected payout from the published fee schedule rather than
// hardcoding it. A test that hardcodes the net would silently stop
// checking anything the moment the operator changed the rake.
var sched struct {
Schedule struct {
RakePercent string `json:"rake_percent"`
RoundingUnit string `json:"rounding_unit"`
} `json:"schedule"`
}
alice.do("GET", "/api/fees", nil, &sched)
var rakePct float64
fmt.Sscanf(sched.Schedule.RakePercent, "%f%%", &rakePct)
var roundUnit int64
fmt.Sscanf(sched.Schedule.RoundingUnit, "%d msat", &roundUnit)
if roundUnit < 1 {
roundUnit = 1
}
gross := int64(stake) * 3 / 2
rake := int64(float64(gross) * rakePct / 100)
net := (gross - rake) / roundUnit * roundUnit
// Settlement posts a moment after the reveal; poll briefly.
wantWin := beforeRound - stake + stake*3/2
wantWin := beforeRound - stake + net
wantLose := beforeRound - stake
ok := false
for i := 0; i < 20; i++ {
@@ -504,8 +529,9 @@ func TestFullPlayerJourney(t *testing.T) {
after.BalanceMsat, wantWin, wantLose)
}
if crash >= 1.5 && after.BalanceMsat != wantWin {
t.Fatalf("round crashed at %.2fx, above the 1.50x target, but balance is %d not %d",
crash, after.BalanceMsat, wantWin)
t.Fatalf("round crashed at %.2fx, above the 1.50x target, but balance is %d not %d "+
"(gross %d, rake %d, net %d)",
crash, after.BalanceMsat, wantWin, gross, rake, net)
}
if crash < 1.5 && after.BalanceMsat != wantLose {
t.Fatalf("round crashed at %.2fx, below the 1.50x target, but balance is %d not %d",