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

Deep Space Radio: a game of private ships

Shows: the owner check (4) · the proof-carrying message (7) · pseudonym (8) · async message for strangers (9) · clock assertion (17); and, under Going further, accept by answering (10) · the public referee (12) · group-owned private PDA (18)

Goal

A space game. Every player commands a ship whose position and cargo are hidden from everyone else. Stations are public places. Players talk by radio, which can carry cargo.

Each game mechanic is a protocol feature under another name:

In the gameIn the protocol
a shipa private actor
fog of warprivate state
the laws of physicsthe handler, proven on every move
a stationa public actor
a radio messagean AsyncMessage
an encrypted channela sealed AsyncMessage from a private sender
a call signa blinded address
accepting a tradeanswering with an authorized root call
dockinga SyncMessage from private to public

Actors

ActorWhat it holdsPlacement
(player, space): a shipposition, cargo, time of last moveprivate
(station, space)its location, the call signs docked therepublic PDA

Messages

/// From the player to their own ship.
enum Command {
    Spawn { now: Timestamp },
    Move { to: Position, now: Timestamp },
    Radio { to: AccountId, text: String, cargo: u64 },
    Dock { station: AccountId },
}

/// Between actors of the game.
enum Internal {
    Signal { text: String, cargo: u64 },
    Docking { at: Position },
}

Movement: the laws of physics are a handler

Command::Move { to, now } => {
    assert!(input.is_authorized() && input.from.is_none(), "only the captain commands");
    let mut ship = Ship::from_bytes(&input.pre_state);
    let elapsed = now - ship.last_move;
    assert!(ship.position.distance(&to) <= elapsed * MAX_SPEED, "too far, too fast");
    ship.position = to;
    ship.last_move = now;
    Response::set_state(ship.to_bytes())
        .timestamp_window(now..)        // `now` cannot be in the future
}

Observers see a private transaction, not the move. A move that breaks the speed limit cannot be proven, so cannot happen.

Without a Spawn, a ship’s first Move counts from time zero and may go anywhere. Spawn runs once and sets the first position (a fixed start) and last_move. Its window bounds the claimed time on both sides, since a lower bound alone would let a captain claim to have spawned long ago:

Command::Spawn { now } => {
    assert!(input.is_authorized() && input.from.is_none(), "only the captain commands");
    assert!(input.pre_state.is_empty(), "already spawned");
    let ship = Ship { position: START, cargo: 0, last_move: now };
    Response::set_state(ship.to_bytes())
        .try_timestamp_window(now..now + SPAWN_SLACK)   // `now` is at most SPAWN_SLACK old
        .expect("SPAWN_SLACK is not zero")
}

Every command checks that it comes from the captain and from nobody else. is_authorized() alone is not enough: if the captain marks the ship in a call to another program, that program holds the ship’s grant and can attach it to its own Command::Radio { to: thief, cargo: everything }. A stranger’s command collected from the mailbox fails the check anyway, because a stored message arrives with no grant.

Radio

Command::Radio { to, text, cargo } => {
    assert!(input.is_authorized() && input.from.is_none(), "only the captain commands");
    let mut ship = Ship::from_bytes(&input.pre_state);
    ship.cargo = ship.cargo.checked_sub(cargo).expect("you do not carry that much");
    Response::set_state(ship.to_bytes())
        .send_async(Actor::new(to, input.receiver.program_account_id), &Internal::Signal { text, cargo })
}

// On the receiving ship, when its captain collects the message:
Internal::Signal { cargo, .. } => {
    assert!(input.from_own_program(), "not a ship");
    let mut ship = Ship::from_bytes(&input.pre_state);
    ship.cargo += cargo;
    Response::set_state(ship.to_bytes())
}

Different ways to use the same two arms:

  • Encrypted, ship to ship. The sender is private and seals the AsyncMessage, which needs the recipient’s keys, and the factor behind its address when that address is blinded. Only the recipient reads it, and observers cannot tell who is talking to whom.
  • Call signs. The ship sends under a blinded address: observers and stations see a call sign and cannot link two call signs of one ship. The protocol lets a ship reuse a call sign, for a station’s records or a combat challenge; the reference wallet does not (What works with the reference wallet today). A ship that wants a reply must give the other captain its keys and the factor behind the call sign, and that captain then knows which ship it is: call signs hide a ship from the galaxy, not from the ships it talks with.
  • Distress call. A Signal under a call sign that is never used again.
  • Cargo pod. A Signal with cargo. The sender’s hold shrinks when it is sent, and the receiver’s grows when it is collected. Uncollected cargo drifts in space.
  • Listening is free, answering is a choice. The captain’s wallet reads every message sent to the ship; the ship only changes when the captain collects one.

Docking: proving where you are

Command::Dock { station } => {
    assert!(input.is_authorized() && input.from.is_none(), "only the captain commands");
    let ship = Ship::from_bytes(&input.pre_state);
    Response::keep_state()
        .send_sync(Actor::new(station, input.receiver.program_account_id), &Internal::Docking { at: ship.position })
}

// On the station:
Internal::Docking { at } => {
    assert!(input.from_own_program(), "not a ship");
    let mut station = Station::from_bytes(&input.pre_state);
    assert_eq!(at, station.location, "you are not here");
    station.docked.push(input.from.unwrap().account_id);   // a call sign
    Response::set_state(station.to_bytes())
}

The station is public. It learns that a real ship is at its dock, under a call sign, but not which ship, where it came from, or what it carries. The ship cannot lie about its position: at is read from its state by the game’s own code, inside the proof.

Docking is the one moment a ship reveals where it is, and the player chooses it.

What an observer sees

EventOn chain
a movea private transaction: unlinkable notes, and its validity window, which shows the now the move claimed
ship-to-ship radioa sealed message
dockingthe station, a call sign, the dock’s position

Going further

  • Trading. An offer is a Signal that asks for something back. It records the offer and nothing more; the other captain accepts by sending their own call that marks whatever the trade has to move (pattern 10).
  • Broadcasts. A public beacon actor that logs what ships send it.
  • Combat. Nobody can make a ship collect a torpedo, so contested space needs a public referee holding bonds (pattern 12): a challenged call sign answers “hit” or “miss” in time, by a message its handler computes from the real position, or it loses the bond.
  • Fleets. A fleet is a private PDA owned by a group (pattern 18). Any officer holding the group’s keys can run its handler. A PDA has no signing key, so a root call never authorizes it and the captain check always fails: the fleet takes orders from its officers’ own ships instead, as a seat does (pattern 4).

What does not work

  • Real time. Every action is a proof, so the game suits minutes per move.
  • Automatic detection. Two ships at the same place do not know it until one of them acts.