[PR #58] [MERGED] SMP: coinflips with held stakes (/coinflip), off by default #66

Closed
opened 2026-11-03 04:02:58 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon-mods/pull/58
Author: @ludovic111
Created: 10/30/2026
Status: ✅ Merged
Merged: 10/30/2026
Merged by: @levzzz5154

Base: main ← Head: feat/smp-coinflip


📝 Commits (2)

  • 5dad864 SMP: coinflips with held stakes (/coinflip), off by default
  • 9121109 Merge main into feat/smp-coinflip

📊 Changes

9 files changed (+676 additions, -22 deletions)

View changed files

📝 native/smp/Cargo.lock (+1 -1)
📝 native/smp/Cargo.toml (+1 -1)
📝 native/smp/README.md (+12 -4)
📝 native/smp/mixins/smp/glue.rs (+40 -6)
📝 native/smp/mixins/smp/horizon-mixin.toml (+7 -1)
📝 native/smp/mixins/smp/smp_economy.rs (+106 -2)
➕ native/smp/mixins/smp/smp_games.rs (+457 -0)
📝 native/smp/proof.py (+50 -7)
📝 native/smp/src/lib.rs (+2 -0)

📄 Description

🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself.

Stacked on #57 (trades): review only the last commit.

What

Phase 3 of the official server's SMP (design): coinflips with an escrow, using only the in-game currency. There are no new Horizon hook points, and players install nothing (chat commands).

Command Does
/coinflip <amount> [player] stakes an amount for anyone (or one named player) to match
/coinflip accept <player> matches it; a fair coin decides; the winner gets both stakes
/coinflip cancel, /coinflip list withdraws yours; lists those waiting for you

Off by default. The owner turns it on in config.json. The gambling section is added with enabled: false, min_bet 10, max_bet 1000, cooldown_seconds 30, offer_seconds 60 and fee_percent 0.

The escrow.

  • A stake leaves the balance when it is offered and is held in economy.json itself (held, by UUID), so one atomic write covers balances and stakes: a crash can't lose the money or count it twice.
  • It comes back when the offer is cancelled, isn't taken in time, or its player leaves. It also comes back when the economy next opens, for an offer the server stopped with (held_returned).
  • Accepting holds the second stake; then one award moves both stakes to the winner (the house fee, 0 by default, vanishes from the economy). If that step is refused, both stakes are released.
  • Every step is a line in audit.log: coinflip_stake, coinflip_win (with the stakes), coinflip_cancel, coinflip_expired, held_returned.

The coin. A fresh SipHash key from the system's randomness (RandomState), never the world's random, so no seed or tick predicts it.

Mojang's rules. In-game currency only: nothing is bought with real money or paid out of the game. Limits and a cooldown per player apply.

The lottery comes next, in its own PR.

License

MIT, all original.

Proof

native.py proof smp (Horizon e23b4b7, #4534), after the economy, market and trade checks, with gambling.enabled set before the first start:

  • the config was filled in with the other defaults;
  • /coinflip 5 was refused ("A coinflip is 10 coins to 1,000 coins.");
  • Alex's /coinflip 100 Steve held the stake (balance 415 → 315), and Steve was told;
  • Steve accepted. This run, Alex lost and Steve won 200, and both balances were right;
  • Steve's next /coinflip 50 got "Wait 15 s before your next coinflip.", then went through after the cooldown;
  • the server stopped with that 50 still held. The balances plus the held money still added up to 815;
  • after the restart the stake was back (held_returned), held was gone from economy.json, and the total was 815.

The proof world is now peaceful: a slime had killed a client mid-trade.

Checks

cargo fmt --check, cargo clippy --all-targets --locked -- -D warnings, and cargo test --locked: 25 passed. The 4 coinflip tests cover:

  • the stake held, then the winner taking both (either side of the coin), with the total kept;
  • the limits, a self-flip, an unknown name, too little money, and the cooldown;
  • off by default;
  • cancel, expiry, leaving and a restart each giving the stake back.

Cost per tick (55 bots)

tools/server_bench.py --servers horizon --bots 55 --behaviour walk --warmup 30 --seconds 120 on the Mac mini (M4 Pro, shared and noisy), Horizon f533c3a, fast profile. Same commit and profile both sides: the stock engine (SMP off) against the engine built with this mod (SMP on, 55 accounts opened). The pairs ran in alternating order, each in fresh scratch homes.

Pair SMP MSPT P95 P99 RSS MB
1 (before the fix) off 6.18 7.13 8.17 1492
1 (before the fix) on 8.95 11.42 16.17 1461
2 on 6.09 7.10 7.78 1470
2 off 6.14 7.20 8.15 1468
3 off 6.02 7.04 7.90 1475
3 on 6.17 7.26 8.12 1485

Pair 1 found a real cost: the audit log was sync_data'd at the end of every tick, even with nothing appended, about +2.8 ms. It now syncs only after a tick that wrote to it (commit "the audit log syncs only after a tick that wrote to it", in #57). After the fix, SMP on averages 6.13 ms against 6.08 ms off: +0.05 ms, within the run-to-run spread (0.12 ms). Chunks sent per second were the same (557 on both sides). The bots don't run SMP commands, so this measures the quiet tick the server pays all the time; a command costs only on the tick it runs.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/horizon-rust/horizon-mods/pull/58 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/30/2026 **Status:** ✅ Merged **Merged:** 10/30/2026 **Merged by:** [@levzzz5154](https://github.com/levzzz5154) **Base:** `main` ← **Head:** `feat/smp-coinflip` --- ### 📝 Commits (2) - [`5dad864`](https://github.com/horizon-rust/horizon-mods/commit/5dad8648b397c74727af1ff40585d421e3e3d59b) SMP: coinflips with held stakes (/coinflip), off by default - [`9121109`](https://github.com/horizon-rust/horizon-mods/commit/912110971793bf99c6165bcf479ea4a8eb09caa2) Merge main into feat/smp-coinflip ### 📊 Changes **9 files changed** (+676 additions, -22 deletions) <details> <summary>View changed files</summary> 📝 `native/smp/Cargo.lock` (+1 -1) 📝 `native/smp/Cargo.toml` (+1 -1) 📝 `native/smp/README.md` (+12 -4) 📝 `native/smp/mixins/smp/glue.rs` (+40 -6) 📝 `native/smp/mixins/smp/horizon-mixin.toml` (+7 -1) 📝 `native/smp/mixins/smp/smp_economy.rs` (+106 -2) ➕ `native/smp/mixins/smp/smp_games.rs` (+457 -0) 📝 `native/smp/proof.py` (+50 -7) 📝 `native/smp/src/lib.rs` (+2 -0) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. **Stacked on #57 (trades): review only the last commit.** ## What Phase 3 of the official server's SMP ([design](https://github.com/horizon-rust/horizon/blob/feat/smp-design/docs/smp.md)): coinflips with an escrow, using only the in-game currency. There are no new Horizon hook points, and players install nothing (chat commands). | Command | Does | |---|---| | `/coinflip <amount> [player]` | stakes an amount for anyone (or one named player) to match | | `/coinflip accept <player>` | matches it; a fair coin decides; the winner gets both stakes | | `/coinflip cancel`, `/coinflip list` | withdraws yours; lists those waiting for you | **Off by default.** The owner turns it on in `config.json`. The `gambling` section is added with `enabled: false`, `min_bet` 10, `max_bet` 1000, `cooldown_seconds` 30, `offer_seconds` 60 and `fee_percent` 0. **The escrow.** - A stake leaves the balance when it is offered and is **held in `economy.json` itself** (`held`, by UUID), so one atomic write covers balances and stakes: a crash can't lose the money or count it twice. - It comes back when the offer is cancelled, isn't taken in time, or its player leaves. It also comes back when the economy next opens, for an offer the server stopped with (`held_returned`). - Accepting holds the second stake; then one `award` moves both stakes to the winner (the house fee, 0 by default, vanishes from the economy). If that step is refused, both stakes are released. - Every step is a line in `audit.log`: `coinflip_stake`, `coinflip_win` (with the stakes), `coinflip_cancel`, `coinflip_expired`, `held_returned`. **The coin.** A fresh SipHash key from the system's randomness (`RandomState`), never the world's random, so no seed or tick predicts it. **Mojang's rules.** In-game currency only: nothing is bought with real money or paid out of the game. Limits and a cooldown per player apply. The lottery comes next, in its own PR. ## License MIT, all original. ## Proof `native.py proof smp` (Horizon e23b4b7, #4534), after the economy, market and trade checks, with `gambling.enabled` set before the first start: - the config was filled in with the other defaults; - `/coinflip 5` was refused ("A coinflip is 10 coins to 1,000 coins."); - Alex's `/coinflip 100 Steve` held the stake (balance 415 → 315), and Steve was told; - Steve accepted. This run, Alex lost and Steve won 200, and both balances were right; - Steve's next `/coinflip 50` got "Wait 15 s before your next coinflip.", then went through after the cooldown; - the server stopped with that 50 still held. The balances plus the held money still added up to 815; - after the restart the stake was back (`held_returned`), `held` was gone from `economy.json`, and the total was 815. The proof world is now peaceful: a slime had killed a client mid-trade. ## Checks `cargo fmt --check`, `cargo clippy --all-targets --locked -- -D warnings`, and `cargo test --locked`: 25 passed. The 4 coinflip tests cover: - the stake held, then the winner taking both (either side of the coin), with the total kept; - the limits, a self-flip, an unknown name, too little money, and the cooldown; - off by default; - cancel, expiry, leaving and a restart each giving the stake back. ## Cost per tick (55 bots) `tools/server_bench.py --servers horizon --bots 55 --behaviour walk --warmup 30 --seconds 120` on the Mac mini (M4 Pro, shared and noisy), Horizon f533c3a, `fast` profile. Same commit and profile both sides: the stock engine (SMP off) against the engine built with this mod (SMP on, 55 accounts opened). The pairs ran in alternating order, each in fresh scratch homes. | Pair | SMP | MSPT | P95 | P99 | RSS MB | |---|---|---|---|---|---| | 1 (before the fix) | off | 6.18 | 7.13 | 8.17 | 1492 | | 1 (before the fix) | on | **8.95** | 11.42 | 16.17 | 1461 | | 2 | on | 6.09 | 7.10 | 7.78 | 1470 | | 2 | off | 6.14 | 7.20 | 8.15 | 1468 | | 3 | off | 6.02 | 7.04 | 7.90 | 1475 | | 3 | on | 6.17 | 7.26 | 8.12 | 1485 | Pair 1 found a real cost: the audit log was `sync_data`'d at the end of every tick, even with nothing appended, about +2.8 ms. It now syncs only after a tick that wrote to it (commit "the audit log syncs only after a tick that wrote to it", in #57). After the fix, SMP on averages 6.13 ms against 6.08 ms off: +0.05 ms, within the run-to-run spread (0.12 ms). Chunks sent per second were the same (557 on both sides). The bots don't run SMP commands, so this measures the quiet tick the server pays all the time; a command costs only on the tick it runs. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-03 04:02:58 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
horizon-rust/horizon-mods#66
No description provided.