Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Battleship: players who cannot lie

Shows: the proof-carrying message (7) · the seat (4) · the public referee (12)

Goal

Two players hide their fleets and take turns shooting. Each must say truthfully whether a shot hit.

On most chains this needs a commitment at the start and a full reveal at the end, and a liar is only caught after the game. Here a player cannot answer falsely: the game’s handler computes the answer from the player’s real hidden fleet, inside a proof.

Actors

ActorWhat it holdsPlacement
(match, battleship)the two boards’ addresses, whose turn it is, the shot waiting for an answer, the results so farpublic PDA
(player, battleship): a boardthe match it plays in, and the fleetprivate, on each player’s account

The match is public because it is the referee. The boards are private because they are the secret.

Messages

/// From a player to their own board.
enum Command {
    Place { game: Actor, fleet: Fleet },
    Fire { at: Square },
    Answer,
}

/// Between actors of the game.
enum Internal {
    Joined,                              // board -> match: take a seat
    Shot { at: Square },                 // board -> match
    WhatWasFired,                        // board -> match
    Incoming { at: Square },             // match -> board, as the reply
    Result { at: Square, hit: bool },    // board -> match
}

The board’s handler

// Every command: only the owner, and never a collected message.
let owner = input.is_authorized() && input.from.is_none();
let (game, fleet): (Actor, Fleet) = decode(&input.pre_state);   // every arm but `Place`

Command::Place { game, fleet } => {
    assert!(owner && fleet.is_legal(), "ships overlap or leave the grid");
    Response::set_state(write_once(&input.pre_state, encode(&(game, fleet))))
        .send_sync(game, &Internal::Joined)
}

Command::Fire { at } => { assert!(owner); Response::keep_state().send_sync(game, &Internal::Shot { at }) }

// "Answer" asks the match which square was fired at...
Command::Answer => { assert!(owner); Response::keep_state().send_sync(game, &Internal::WhatWasFired) }

// ...and the match replies in the same transaction.
Internal::Incoming { at } => {
    assert_eq!(input.from, Some(game), "only my match tells me about shots");
    let hit = fleet.occupies(at);
    Response::keep_state().send_sync(game, &Internal::Result { at, hit })
}

The match’s side

The match is bookkeeping, with one rule: it pins each board’s address when the board joins, and afterwards accepts messages only from those two addresses.

let board = input.from.filter(|_| input.from_own_program()).expect("not a board").account_id;
match message {
    Internal::Joined => game.take_free_seat(board),          // remember this address
    Internal::Shot { at } => { assert_eq!(board, game.shooter()); game.pending = Some(at); }
    Internal::WhatWasFired => { assert_eq!(board, game.defender()); /* reply Incoming */ }
    Internal::Result { at, hit } => { assert_eq!(board, game.defender()); /* record; swap turns */ }
}

How it runs

  1. Place. Each player places a fleet on their own board. The handler checks that it is legal, writes it once, and takes a seat at the match.
  2. Fire. The shooter’s board sends Shot to the match.
  3. Answer. The defender’s board asks the match what was fired, receives Incoming, and sends back Result, all in one private transaction.

What to notice

The player does not choose hit: the handler computes it from the stored fleet, and a proof of any other answer does not exist.

The seat is what makes the fleet the same fleet. A player may own several private accounts, each with a different legal fleet. Without the pinned address, a defender could answer each shot from whichever board says “miss”. The match accepts answers only from the address that joined, and the board behind that address wrote its fleet once.

The board must present the address it joined with every time, which the protocol allows and the reference wallet does not (What works with the reference wallet today).

The match never sees a fleet, and never needs a commitment to one.

The answer is one transaction with a conversation inside it: the private board sends a SyncMessage to the public match, the match replies, the board sends again. The proof covers the board’s two runs; the sequencer runs the match’s two.

The defender must be the one to act: the match cannot push a shot into a private board. That is why the board asks.

The match knows a board only by the address it joined with, which can be a pseudonym.

What leaks

Every shot and every result, as in the board game (whether each square fired at holds a ship), and the two boards’ addresses. The rest of each fleet, including the loser’s surviving ships, stays hidden.

Going further

  • Deadlines. A defender who never answers stalls the game, so each player posts a bond with the match, and anyone can claim it for the other player once the answer is late.
  • Other games, same recipe. A private actor per player for the secret, a public match as referee that pins each player’s seat, every answer a message from that seat.
    • Card games: a card can only be played if it is in the hand, and only once. The hard part is dealing without a trusted dealer.
    • Wordle or Mastermind: the secret is written once before the first guess, so it cannot be adapted.
    • Strategy with fog of war: see Deep Space Radio.
  • What does not work: randomness from the chain. A handler is deterministic, so dice need a commit-and-reveal between the players.