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/crash.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'src/crash.rs') diff --git a/src/crash.rs b/src/crash.rs index 9d6e98c..40b8743 100644 --- a/src/crash.rs +++ b/src/crash.rs @@ -6,6 +6,7 @@ //! clock, never from anything the client sends. use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; use std::time::Duration; /// `exp(GROWTH_RATE * t)` doubles the multiplier every five seconds. @@ -47,6 +48,53 @@ pub fn crash_point_from_uniform(u: f64) -> f64 { ((1.0 - HOUSE_EDGE) / (1.0 - u)).clamp(1.0, MAX_MULTIPLIER) } +/// How many bytes a round seed contains. The first eight bytes choose the +/// crash point; the rest are revealed and hashed with it. +pub const SEED_LEN: usize = 32; + +/// Hex-encoded SHA-256 of a round seed, sent before the round starts. +pub fn commit_seed(seed: &[u8]) -> String { + let digest = Sha256::digest(seed); + format!("{digest:x}") +} + +/// Uniform draw encoded by a seed: the first eight bytes, big-endian. +pub fn seed_to_uniform(seed: &[u8]) -> f64 { + let mut bytes = [0u8; 8]; + let take = seed.len().min(8); + bytes[..take].copy_from_slice(&seed[..take]); + u64::from_be_bytes(bytes) as f64 / 18_446_744_073_709_551_616.0 +} + +/// The crash point a seed commits to. A client recomputes this after the +/// reveal and checks it against the published commit. +pub fn crash_point_from_seed(seed: &[u8]) -> f64 { + crash_point_from_uniform(seed_to_uniform(seed)) +} + +/// Decode a lowercase or uppercase hex seed. Returns `None` for odd lengths, +/// bad digits or a seed that is not exactly [`SEED_LEN`] bytes. +pub fn seed_from_hex(raw: &str) -> Option> { + if raw.len() != SEED_LEN * 2 { + return None; + } + let mut seed = Vec::with_capacity(SEED_LEN); + for pair in raw.as_bytes().chunks_exact(2) { + let high = (pair[0] as char).to_digit(16)?; + let low = (pair[1] as char).to_digit(16)?; + seed.push((high * 16 + low) as u8); + } + Some(seed) +} + +/// Check a revealed seed against the commit published before the round. +pub fn verify_seed(seed_hex: &str, commit: &str) -> bool { + let Some(seed) = seed_from_hex(seed_hex) else { + return false; + }; + commit_seed(&seed).eq_ignore_ascii_case(commit) +} + /// Whole-Kr cashout value. Balances are integers, so the payout is floored. pub fn payout(bet: u64, multiplier: f64) -> u64 { if bet == 0 || !multiplier.is_finite() || multiplier <= 0.0 { @@ -72,10 +120,16 @@ pub struct CrashState { pub multiplier: f64, /// `floor(bet * multiplier)` for the value above. pub potential: u64, + /// Server Unix time when the round started, milliseconds. + pub started_at: u64, /// Hidden until the round ends. pub crash_point: Option, pub payout: u64, pub result: String, + /// `sha256(seed)`, present for the whole round. + pub commit: Option, + /// The seed itself, present only after the round ends. + pub seed: Option, } impl CrashState { -- cgit v1.2.3