[PR #57] [MERGED] SMP: player trades (/trade) with an atomic swap #67

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/57
Author: @ludovic111
Created: 10/30/2026
Status: ✅ Merged
Merged: 10/30/2026
Merged by: @levzzz5154

Base: main ← Head: feat/smp-trade


📝 Commits (3)

  • 1d4cb83 SMP: player trades (/trade) on server menus
  • 7bd4f62 SMP proof: the 26.3 keep_inventory rule
  • 7f1bb69 SMP: pinned on horizon main (f533c3a, hooks 10 merged)

📊 Changes

13 files changed (+1103 additions, -23 deletions)

View changed files

➕ native/smp/.gitignore (+2 -0)
📝 native/smp/Cargo.lock (+1 -1)
📝 native/smp/Cargo.toml (+1 -1)
📝 native/smp/README.md (+23 -7)
📝 native/smp/compatibility.json (+2 -2)
📝 native/smp/mixins/smp/glue.rs (+287 -6)
📝 native/smp/mixins/smp/horizon-mixin.toml (+33 -3)
➕ native/smp/mixins/smp/menu_clicked.rs (+4 -0)
➕ native/smp/mixins/smp/menu_closed.rs (+4 -0)
📝 native/smp/mixins/smp/smp_economy.rs (+7 -0)
➕ native/smp/mixins/smp/smp_trade.rs (+609 -0)
📝 native/smp/proof.py (+128 -3)
📝 native/smp/src/lib.rs (+2 -0)

📄 Description

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

#55 and #56 are merged: this PR is its own three commits on main.

What

The SMP mod's player trades, /trade (design). Players install nothing: the trade window is a vanilla 6-row chest menu the server fills with ordinary items, which a stock Horizon client shows.

Command Does
/trade <player> asks a player to trade (60 s to answer); asking back accepts
/trade accept [player], /trade deny [player] answers
/trade money <amount> money added to your offer (0: none)
/trade cancel ends the trade or withdraws the request

In the menu:

  • your offer is on the left (16 stacks), theirs on the right, each side's money, your confirm and cancel buttons, and whether they have confirmed;
  • click a stack in your inventory to offer it, click it in your offer to take it back;
  • any change clears both confirmations.

No duplication, no loss, by construction.

  • The menu is display only (Horizon API 10: no click moves anything), and the offered stacks stay in their owners' inventories.
  • When both have confirmed:
    1. the trade is logged in audit.log (both players, their items and money);
    2. native_exchange_items checks each offered slot still holds exactly the stack offered, and that it fits whole on the other side, then swaps both offers in one step, or nothing;
    3. then the money moves, its balance checked beforehand.
  • A refused swap leaves everything as it was (trade_failed in the log), and both must confirm again.
  • Every way out ends the trade with nothing moved, so there is nothing to give back: closing the menu, the cancel button, /trade cancel, leaving, dying.
  • A stack that changes while offered (used, dropped, grown by a pickup) leaves the offer.
  • Survival and adventure players only.

Hook points. It uses the server menu hook points and native_exchange_items of horizon-rust/horizon#4534. All three are merged: compatibility.json pins "hooks": 10 on horizon main (f533c3a), native.py check smp ok there.

License

MIT, all original; nothing from other projects. Trade plugins' source was not read.

Proof

native/tools/native.py proof smp on this branch (Horizon e23b4b7): a dedicated server built with the SMP mod, two real clients (horizon-ctl --offscreen, fresh folders) clicking the server's trade menu as a player does (click-slot), every item counted from each player's own client:

  • closing the menu: Alex offered 10 diamonds; Steve's menu showed them; Alex still held them; Alex closed it. Steve's menu closed and he saw "Trade cancelled: Alex closed the trade. Nothing was exchanged."; nothing moved.
  • a full inventory: both confirmed while Steve had no room. "The trade didn't go through: the second player has no room.", nothing moved, and both confirmations were cleared.
  • the cancel button: the trade ended and nothing moved.
  • a change clears the other's confirmation: Steve's /trade money 35 after Alex confirmed turned Alex's button back to "Confirm".
  • through: Alex's 10 diamonds for Steve's 5 emeralds and 35 coins. The clients counted (Alex 0 diamonds, 5 emeralds; Steve 10 diamonds, 0 emeralds), and the balances were 415 / 400.
  • dying (keep_inventory on, so a duplicate would show): "Trade cancelled: Alex is gone.", and the counts were unchanged after he respawned.
  • leaving: Steve quit mid-trade with his diamonds offered. Alex's menu closed and nothing moved; Steve came back with his 10 diamonds.
  • the files: economy.json held 415 / 400 after a restart, and audit.log ended trade, trade_failed, trade, trade_money.

Checks

cargo fmt --check, cargo clippy --all-targets --locked -- -D warnings, cargo test --locked: 21 passed (7 trade tests: requests, expiry and refusal, confirmations cleared by any change, the second confirmation settling, an offer's limits, a changed stack leaving the offer, every way out ending with nothing moved).

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/57 **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-trade` --- ### 📝 Commits (3) - [`1d4cb83`](https://github.com/horizon-rust/horizon-mods/commit/1d4cb83ca2471afe72eb7022c79e67566d3d9f5b) SMP: player trades (/trade) on server menus - [`7bd4f62`](https://github.com/horizon-rust/horizon-mods/commit/7bd4f6207b621e4e636a41c3f47129eddea41865) SMP proof: the 26.3 keep_inventory rule - [`7f1bb69`](https://github.com/horizon-rust/horizon-mods/commit/7f1bb6920b9125fe4901b9bb3be4b7d8218820d6) SMP: pinned on horizon main (f533c3a, hooks 10 merged) ### 📊 Changes **13 files changed** (+1103 additions, -23 deletions) <details> <summary>View changed files</summary> ➕ `native/smp/.gitignore` (+2 -0) 📝 `native/smp/Cargo.lock` (+1 -1) 📝 `native/smp/Cargo.toml` (+1 -1) 📝 `native/smp/README.md` (+23 -7) 📝 `native/smp/compatibility.json` (+2 -2) 📝 `native/smp/mixins/smp/glue.rs` (+287 -6) 📝 `native/smp/mixins/smp/horizon-mixin.toml` (+33 -3) ➕ `native/smp/mixins/smp/menu_clicked.rs` (+4 -0) ➕ `native/smp/mixins/smp/menu_closed.rs` (+4 -0) 📝 `native/smp/mixins/smp/smp_economy.rs` (+7 -0) ➕ `native/smp/mixins/smp/smp_trade.rs` (+609 -0) 📝 `native/smp/proof.py` (+128 -3) 📝 `native/smp/src/lib.rs` (+2 -0) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. **#55 and #56 are merged: this PR is its own three commits on main.** ## What The SMP mod's player trades, `/trade` ([design](https://github.com/horizon-rust/horizon/blob/feat/smp-design/docs/smp.md)). Players install nothing: the trade window is a vanilla 6-row chest menu the server fills with ordinary items, which a stock Horizon client shows. | Command | Does | |---|---| | `/trade <player>` | asks a player to trade (60 s to answer); asking back accepts | | `/trade accept [player]`, `/trade deny [player]` | answers | | `/trade money <amount>` | money added to your offer (0: none) | | `/trade cancel` | ends the trade or withdraws the request | In the menu: - your offer is on the left (16 stacks), theirs on the right, each side's money, your confirm and cancel buttons, and whether they have confirmed; - click a stack in your inventory to offer it, click it in your offer to take it back; - any change clears both confirmations. **No duplication, no loss, by construction.** - The menu is display only (Horizon API 10: no click moves anything), and the offered stacks stay in their owners' inventories. - When both have confirmed: 1. the trade is logged in `audit.log` (both players, their items and money); 2. `native_exchange_items` checks each offered slot still holds exactly the stack offered, and that it fits whole on the other side, then swaps both offers in one step, or nothing; 3. then the money moves, its balance checked beforehand. - A refused swap leaves everything as it was (`trade_failed` in the log), and both must confirm again. - Every way out ends the trade with nothing moved, so there is nothing to give back: closing the menu, the cancel button, `/trade cancel`, leaving, dying. - A stack that changes while offered (used, dropped, grown by a pickup) leaves the offer. - Survival and adventure players only. **Hook points.** It uses the server menu hook points and `native_exchange_items` of horizon-rust/horizon#4534. All three are merged: `compatibility.json` pins `"hooks": 10` on horizon main (f533c3a), `native.py check smp` ok there. ## License MIT, all original; nothing from other projects. Trade plugins' source was not read. ## Proof `native/tools/native.py proof smp` on this branch (Horizon e23b4b7): a dedicated server built with the SMP mod, two real clients (`horizon-ctl --offscreen`, fresh folders) clicking the server's trade menu as a player does (`click-slot`), every item counted from each player's own client: - **closing the menu**: Alex offered 10 diamonds; Steve's menu showed them; Alex still held them; Alex closed it. Steve's menu closed and he saw "Trade cancelled: Alex closed the trade. Nothing was exchanged."; nothing moved. - **a full inventory**: both confirmed while Steve had no room. "The trade didn't go through: the second player has no room.", nothing moved, and both confirmations were cleared. - **the cancel button**: the trade ended and nothing moved. - **a change clears the other's confirmation**: Steve's `/trade money 35` after Alex confirmed turned Alex's button back to "Confirm". - **through**: Alex's 10 diamonds for Steve's 5 emeralds and 35 coins. The clients counted (Alex 0 diamonds, 5 emeralds; Steve 10 diamonds, 0 emeralds), and the balances were 415 / 400. - **dying** (`keep_inventory` on, so a duplicate would show): "Trade cancelled: Alex is gone.", and the counts were unchanged after he respawned. - **leaving**: Steve quit mid-trade with his diamonds offered. Alex's menu closed and nothing moved; Steve came back with his 10 diamonds. - **the files**: `economy.json` held 415 / 400 after a restart, and `audit.log` ended `trade, trade_failed, trade, trade_money`. ## Checks `cargo fmt --check`, `cargo clippy --all-targets --locked -- -D warnings`, `cargo test --locked`: 21 passed (7 trade tests: requests, expiry and refusal, confirmations cleared by any change, the second confirmation settling, an offer's limits, a changed stack leaving the offer, every way out ending with nothing moved). ## 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#67
No description provided.