GIWA Village: a multiplayer game where the server is optional

GIWA Village: a multiplayer game where the server is optional


GIWA Village is a 3D hanok market town on the GIWA chain. You walk the village as an avatar, open a stall, haggle with other players (and with AI merchants), and go into guild dungeons together.

Most multiplayer games are built around a server that is the authority. We went the other way. The written protocol (PROTOCOL.md) starts with two paths:

  1. The chain is the source of truth. Stalls, trades, guilds, dungeon settlement, titles and presence all live in contracts.
  2. Real-time rooms are an accelerator. A Colyseus server sends 15 Hz snapshots so movement feels smooth when it’s running. In the protocol’s words, nothing the room says is money.

The game has to work with only path 1. Before asking for a seat, the client checks whether a room server is reachable. If not, it carries on without one.

Presence without a game server

“Who else is in the village right now” is normally the first thing a server does. Without one, we put it in a contract that stores nothing and only emits events:

event Beacon(address indexed who, int32 x100, int32 z100, int16 vx100, int16 vz100, uint8 emote);

Each client sends a beacon at most every 2 seconds, only after moving at least 5 cm, with a 15-second heartbeat, and emote = 255 when leaving. Readers poll the logs every 2.5 seconds and dead-reckon each avatar from its last position and velocity for up to 3 seconds, so movement looks continuous. Peers silent for 60 seconds disappear.

It’s coarser than a 15 Hz room, but it works when no server is running, and it costs us nothing to operate.

Deleting 1,500 lines of server

The server used to have a third room for tooling and bots. It trusted wallet addresses without a signature and kept its own copy of stalls and guilds in files, even though the chain was supposed to be canonical.

We deleted it. That removed about 1,500 lines, and the server became a stateless process: two room types and nothing written to disk. Our project rules say to postpone scale work unless it reduces code, and this change reduced code.

Running a server, even an optional one, still taught us something. One forgotten browser tab sitting in a finished dungeon expedition used about 16 KB/s, because the client’s input pump kept the socket alive and the room never closed. That alone would use up a free hosting tier’s 5 GB in a little over four days. Finished expeditions now close after 60 seconds, and tabs idle for five minutes leave the live room. We measured it: before the fix the room was still open 61 seconds after the run ended, and after the fix it closed at 60.0 seconds.

One core, byte-identical to Solidity

Dungeon doors are decided by a hash. Originally the formula was written twice, once in the contract and once in the client. While there are two copies, they can drift without anyone noticing. Bots and tests pass, and only the chain sends you into the trap.

We moved every rule into pure functions in core/ that use viem’s keccak256 and encodePacked, so the TypeScript produces exactly the bytes the contract does. viem is its only dependency. The Solidity side looks like this:

uint8 b = uint8(keccak256(abi.encodePacked(seed, guildId, attempt, step, door))[0]);
uint16 safeLt  = door == 0 ? 218 : door == 1 ? 141 : 64;
uint16 bonusLt = door == 0 ? 218 : door == 1 ? 192 : 154;

npm test checks that the boundary values in the contract and in core/ are identical, and the client imports core/ rather than keeping its own copy.

Don’t ask players to trust the result

Because the rules are pure functions over public inputs, anyone can recompute a dungeon run. scripts/verify-run.mjs takes a run’s settleRun calldata and the weekly seed (a block hash), replays it, and compares the result with the RunSettled event on chain. When we added it, every run settled on chain at that time reproduced byte for byte. The game also has a “verify this result” button right after a run.

There’s a trade-off here that we wrote down instead of hiding. Verifiability and unpredictability sit at opposite ends of the same axis. Once a seed is public, someone can compute a trap-free path offline. For the rewards involved today we chose “anyone can check”. A commit-reveal scheme per expedition is designed and documented for when the stakes are higher.

Using the math to fix the design

The same pure core let us analyse the game, not just test it. Working backward through the door table showed a problem: on floors 0 to 5 the stone door was always the best choice, and from floor 6 going home was always best. Choosing a door wasn’t really a decision.

We added an “omen”, a hint on screen such as “a tailwind blows beyond the wind door”, computed in a separate hash domain so the door outcome didn’t change by a single bit. No contract change and no redeploy.

The first version was wrong in a way the averages hid. The screen said the omen was right “8 times in 10”, and that was true on average. But the stone door, which can never have a tailwind, still showed one on about 3.4% of steps, and its growl was right only 59% of the time. The fix draws a false omen from the door’s own outcome table, so every omen is now right at least 83% of the time (90% on average). The test counts all 65,536 possible values instead of sampling. With honest omens, the best move spreads across all three doors, and the expected number of floors cleared from floor 0 went from 2.29 to 5.66.

What we took away

  • Decide what the server is allowed to be wrong about. Once the protocol said the room is never money, removing the server’s own state was the obvious next step.
  • One implementation of the rules, imported everywhere. Byte-level equality with the contract is a test, not a hope.
  • Check the claim the player actually reads. “8 in 10” was correct on average and wrong for specific doors. Players see a specific door.