mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-10 16:04:04 +00:00
fix(multiplayer): make match promotion replay lifecycle-safe
This commit is contained in:
@@ -45,7 +45,7 @@ const AcceptedMatchInsertSQL = `INSERT INTO matches
|
||||
VALUES ($1, $2, 'ALLOCATING', $3, $4, NULLIF($5, ''))
|
||||
ON CONFLICT (match_id) DO NOTHING`
|
||||
|
||||
const AcceptedMatchSelectSQL = `SELECT playlist, state, region, protocol_version, arena_path, server_id
|
||||
const AcceptedMatchSelectSQL = `SELECT playlist, region, protocol_version, arena_path
|
||||
FROM matches
|
||||
WHERE match_id = $1
|
||||
FOR UPDATE`
|
||||
@@ -99,6 +99,9 @@ func PromoteStoredAcceptedProposal(ctx context.Context, db *sql.DB, proposalID s
|
||||
if err := rows.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
return CreateMatchFromAcceptedProposal(ctx, db, plan, now)
|
||||
}
|
||||
|
||||
@@ -206,6 +209,9 @@ func acceptedProposalParticipants(ctx context.Context, tx *sql.Tx, plan Accepted
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(participants) != len(plan.Players) {
|
||||
return nil, fmt.Errorf("proposal participants do not match accepted plan")
|
||||
}
|
||||
@@ -218,14 +224,16 @@ func acceptedProposalParticipants(ctx context.Context, tx *sql.Tx, plan Accepted
|
||||
}
|
||||
|
||||
func verifyAcceptedMatchReplay(ctx context.Context, tx *sql.Tx, plan AcceptedMatchPlan, playlist domain.Playlist, tickets map[string]string) error {
|
||||
var existingPlaylist, state, region string
|
||||
var existingPlaylist, region string
|
||||
var protocol int
|
||||
var arenaPath sql.NullString
|
||||
var serverID sql.NullString
|
||||
if err := tx.QueryRowContext(ctx, AcceptedMatchSelectSQL, plan.MatchID).Scan(&existingPlaylist, &state, ®ion, &protocol, &arenaPath, &serverID); err != nil {
|
||||
if err := tx.QueryRowContext(ctx, AcceptedMatchSelectSQL, plan.MatchID).Scan(&existingPlaylist, ®ion, &protocol, &arenaPath); err != nil {
|
||||
return err
|
||||
}
|
||||
if existingPlaylist != string(playlist) || state != string(domain.Allocating) || region != plan.Region || protocol != plan.Protocol || arenaPath.String != plan.ArenaPath || arenaPath.Valid != (plan.ArenaPath != "") || serverID.Valid {
|
||||
// Match state and server ownership are intentionally absent: allocation may
|
||||
// advance immediately after the first promotion commits. A retry after a
|
||||
// lost API response is valid whenever the immutable topology still matches.
|
||||
if existingPlaylist != string(playlist) || region != plan.Region || protocol != plan.Protocol || arenaPath.String != plan.ArenaPath || arenaPath.Valid != (plan.ArenaPath != "") {
|
||||
return domain.ErrConflict
|
||||
}
|
||||
rows, err := tx.QueryContext(ctx, AcceptedMatchParticipantsSQL, plan.MatchID)
|
||||
|
||||
@@ -12,6 +12,7 @@ func TestAcceptedMatchSQLPreservesAtomicProposalToMatchBoundary(t *testing.T) {
|
||||
AcceptedProposalLockSQL: {"FOR UPDATE", "proposal_id = $1"},
|
||||
AcceptedProposalParticipantsSQL: {"response", "ORDER BY player_id", "FOR UPDATE"},
|
||||
AcceptedMatchInsertSQL: {"'ALLOCATING'", "ON CONFLICT (match_id) DO NOTHING"},
|
||||
AcceptedMatchSelectSQL: {"playlist", "region", "protocol_version", "arena_path", "FOR UPDATE"},
|
||||
AcceptedTicketSQL: {"state = 'ACCEPTED'", "state = 'PROPOSED'", "revision = revision + 1"},
|
||||
AcceptedMatchParticipantInsertSQL: {"match_participants", "slot", "team"},
|
||||
}
|
||||
@@ -24,6 +25,12 @@ func TestAcceptedMatchSQLPreservesAtomicProposalToMatchBoundary(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAcceptedMatchReplayDoesNotDependOnMutableLifecycleFields(t *testing.T) {
|
||||
if contains(AcceptedMatchSelectSQL, "state") || contains(AcceptedMatchSelectSQL, "server_id") {
|
||||
t.Fatalf("accepted promotion replay is coupled to mutable lifecycle fields: %s", AcceptedMatchSelectSQL)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAcceptedMatchPlanRejectsInvalidPlansBeforeDatabaseUse(t *testing.T) {
|
||||
valid := AcceptedMatchPlan{
|
||||
MatchID: "match-1", ProposalID: "proposal-1", Region: "EU", Protocol: 1,
|
||||
|
||||
@@ -252,10 +252,16 @@ func TestPostgreSQLAcceptedProposalPromotesOneAtomicAllocatingMatch(t *testing.T
|
||||
if err := CreateMatchFromAcceptedProposal(ctx, db, plan, now.Add(time.Second)); err != nil {
|
||||
t.Fatalf("identical match promotion replay: %v", err)
|
||||
}
|
||||
if _, err := db.ExecContext(ctx, `UPDATE matches SET state = 'LIVE' WHERE match_id = 'promote-match'`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := CreateMatchFromAcceptedProposal(ctx, db, plan, now.Add(2*time.Second)); err != nil {
|
||||
t.Fatalf("promotion replay after match lifecycle advanced: %v", err)
|
||||
}
|
||||
conflict := plan
|
||||
conflict.Players = append([]MatchPlayer(nil), plan.Players...)
|
||||
conflict.Players[1].Slot = 4
|
||||
if err := CreateMatchFromAcceptedProposal(ctx, db, conflict, now.Add(2*time.Second)); err == nil {
|
||||
if err := CreateMatchFromAcceptedProposal(ctx, db, conflict, now.Add(3*time.Second)); err == nil {
|
||||
t.Fatal("conflicting match promotion replay was accepted")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user