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

Where state lives

Every piece of state a program keeps is an actor on some account. The kind of account decides who can read it, who can change it, and who has to be online for it to change.

PlacementWho readsWho can run the handlerWho authorizesReached by
Public, key-ownedeveryonethe sequencer, in any transaction that declares itthe owner’s signatureSyncMessage or AsyncMessage, from any transaction that declares it
Public PDAeveryonethe sequencer, in any transaction that declares itthe program, by seedSyncMessage or AsyncMessage, from any transaction that declares it
Private, regularthe ownerthe owner, in their own proofthe owner’s authorization key (ask)SyncMessage from the owner’s own transaction; AsyncMessage from anyone
Private PDAthe key holderthe key holderthe program, by seedsame as private
Group-owned private PDAevery group memberany group memberthe program, by seedsame as private
The mailboxclear: everyone; sealed: the recipient (and the sender)nobody; messages waitnot applicableAsyncMessage

Choosing

Must anyone be able to move it forward? Then it is public. Pools, tallies, auctions, referees and anything with a deadline belong on a public PDA.

Is it one person’s secret? Then it is a private actor on their account. Balances, ballots, badges, hands of cards, a ship’s position.

Is it a group’s secret? Then it is a group-owned private PDA. A hidden treasury, a shared ledger.

Is it in transit? Then it is a message. Value on its way to someone is a stored AsyncMessage, not a field in somebody’s state.

Mixed placements in one program

Many designs use two placements and connect them with messages.

  • Public hub, private spokes. A public actor holds the shared state. Each participant holds a private actor of the same program. Spokes speak to the hub with proof-carrying messages (pattern 7). The hub speaks to spokes with AsyncMessages (pattern 9). Voting, credentials and the space game have this shape.
  • Private hub, any spokes. A group-owned private PDA holds the shared state. Participants send it AsyncMessages. One member at a time collects them. The hidden multisig has this shape.

The same program can support both, if its members always speak by AsyncMessage and its hub never assumes it is public. The multisig entry works this through.

A note on “who can read”

  • Reading a public actor from off chain is free. Reading it from a program takes a request and a reply.
  • A private actor is read by the wallets that hold its viewing key: its owner’s, or every member’s for a group-owned PDA. Another program sees its state only when its handler sends it, as the native token’s ReadState reply does (lee/state_machine/core/src/native_token.rs), and a reply to a public actor is public.
  • A sealed message is read by its recipient’s wallet, before any handler runs. The recipient chooses what to collect, though the reference wallet also claims pending native credits on its own (Collecting).