From 058432130628d00764663ecf819a9adc6793768d Mon Sep 17 00:00:00 2001 From: salmonik Date: Sun, 8 Nov 2026 03:29:35 +0100 Subject: v5.0.0: protocol v5 and the browser frontend Protocol v5 replaces newline lines with bounded [u32 len][text] frames and REQ/RES/EVT envelopes. Requests carry correlation ids and the server replays the last 256 replies per connection, so a gateway retry cannot double-debit and a late reply cannot clobber a newer request. HELLO/PONG carry server time; crash rounds announce started_at and push coalesced 10 Hz ticks. Crash now commits sha256(seed) before a round and reveals the seed when it ends, so clients recompute the crash point. Adds the webd/ gateway: one blocking thread, one ClientCore and one pinned upstream TLS connection per session, an HttpOnly session cookie, takeover, and a 60-second grace window. The browser sends Command JSON and renders View JSON; it never speaks the game protocol or stores credentials. Adds the vanilla TypeScript browser app at /play/ (login, lobby, all four games, history, notices, crash verification), the nginx /play and /ws routes, and the headless DOM smoke test. webd and the app stay out of the TUI/GUI/server build graph. The client/server version moves to 5.0.0 in lockstep; v4 peers are refused at HELLO. --- src/server/games/crash.rs | 76 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 69 insertions(+), 7 deletions(-) (limited to 'src/server/games/crash.rs') diff --git a/src/server/games/crash.rs b/src/server/games/crash.rs index ba73c7f..1728afc 100644 --- a/src/server/games/crash.rs +++ b/src/server/games/crash.rs @@ -1,5 +1,8 @@ use super::traits::{Game, GameError, GamePhase, GameResult, GameState}; -use crate::crash::{crash_point_from_uniform, payout, CrashState}; +use crate::crash::{ + CrashState, SEED_LEN, commit_seed, crash_point_from_seed, payout, seed_to_uniform, +}; +use rand::RngCore; use serde::{Deserialize, Serialize}; use std::collections::{HashMap, VecDeque}; use std::sync::{Arc, Mutex}; @@ -64,7 +67,14 @@ pub struct CrashRound { pub bet: u64, /// Drawn at start, revealed only when the round ends. pub crash_point: f64, + /// The 32 random bytes the crash point commits to, hex encoded. Kept on + /// the server until the round ends. + pub seed: String, + /// `sha256(seed)` in hex, sent with the start reply. + pub commit: String, pub started_at: f64, + /// Server Unix time at start, milliseconds, for clock sync. + pub started_at_ms: u64, /// `running`, `cashed`, `crashed` or `forfeit`. pub phase: String, pub result: String, @@ -74,11 +84,15 @@ pub struct CrashRound { } impl CrashRound { - pub fn new(bet: u64, crash_point: f64, started_at: f64) -> Self { + pub fn new(bet: u64, seed: [u8; SEED_LEN], started_at: f64, started_at_ms: u64) -> Self { + let crash_point = crash_point_from_seed(&seed); Self { bet, crash_point, + seed: seed_hex(&seed), + commit: commit_seed(&seed), started_at, + started_at_ms, phase: "running".to_string(), result: "playing".to_string(), multiplier: 1.0, @@ -120,6 +134,7 @@ impl CrashRound { balance, multiplier: self.multiplier, potential: payout(self.bet, self.multiplier), + started_at: self.started_at_ms, crash_point: if self.is_running() { None } else { @@ -127,6 +142,12 @@ impl CrashRound { }, payout: self.payout, result: self.result.clone(), + commit: Some(self.commit.clone()), + seed: if self.is_running() { + None + } else { + Some(self.seed.clone()) + }, } } @@ -146,6 +167,34 @@ pub struct CrashGame { uniforms: VecDeque, } +/// Hex encode a round seed. Lowercase, no separators. +fn seed_hex(seed: &[u8]) -> String { + let mut out = String::with_capacity(seed.len() * 2); + for byte in seed { + use std::fmt::Write as _; + let _ = write!(out, "{byte:02x}"); + } + out +} + +/// Draw a seed whose committed point matches a queued test uniform. +/// +/// The first eight bytes are the big-endian integer closest to +/// `uniform * 2^64`; the remaining bytes are zero. This keeps the existing +/// deterministic crash-point tests meaningful while every round still has a +/// real seed. +fn seed_from_uniform(uniform: f64) -> [u8; SEED_LEN] { + let uniform = if uniform.is_finite() { + uniform.clamp(0.0, 0.999_999_999_999_999_9) + } else { + 0.0 + }; + let value = (uniform * 18_446_744_073_709_551_616.0) as u64; + let mut seed = [0u8; SEED_LEN]; + seed[..8].copy_from_slice(&value.to_be_bytes()); + seed +} + impl CrashGame { pub fn new() -> Self { Self { @@ -163,12 +212,25 @@ impl CrashGame { } } + /// Draw the next round seed. Tests supply uniforms; production uses the + /// operating system's random source. + pub fn next_seed(&mut self) -> [u8; SEED_LEN] { + if let Some(uniform) = self.uniforms.pop_front() { + return seed_from_uniform(uniform); + } + let mut seed = [0u8; SEED_LEN]; + rand::thread_rng().fill_bytes(&mut seed); + seed + } + + /// Kept for callers that only need the point a queued uniform encodes. pub fn next_crash_point(&mut self) -> f64 { - let uniform = self - .uniforms - .pop_front() - .unwrap_or_else(rand::random::); - crash_point_from_uniform(uniform) + crash_point_from_seed(&self.next_seed()) + } + + /// The uniform encoded by a seed, exposed for verification tests. + pub fn uniform_of(seed: &[u8; SEED_LEN]) -> f64 { + seed_to_uniform(seed) } } -- cgit v1.2.3