The pattern language
The recurring moves of actor programs on LEZ, which the catalog cites by number. Patterns 1 to 6 are about writing a correct handler. Patterns 7 to 12 are where public and private actors meet. Patterns 13 to 18 are about time, custody and exactly-once.
1. Tell, do not write
A handler changes only its own actor. To change another actor, send it a message and let its handler decide.
Example. A native Transfer does not write the recipient’s balance. It debits its own and sends the recipient’s balance actor Credit (lee/state_machine/core/src/native_token.rs).
2. Route on the sender first
Before decoding a message, look at input.from, decide which messages this sender may send, then decode with that type.
Why. Messages are untyped bytes. A reply format and a request format can collide. The sender is the one field a program cannot forge.
3. Internal messages
A message that only the program’s own actors may send is guarded with input.from_own_program().
Example. Credit in the native token. Without the guard, anyone could mint.
4. Know who is speaking: the owner, or a seat
The owner: an operation only the account’s owner may start checks input.is_authorized() and input.from.is_none(). The flag alone says the owner’s credential is in the transaction, not that the owner sent this message. See Authorization.
A seat: when a shared actor needs to know which participant is speaking, each participant gets an actor of the same program on their own account. The participant talks to their seat, which does the owner check above. The seat talks to the shared actor, which checks input.from_own_program() and the seat’s account id.
Why a seat. A sender’s account id proves nothing by itself: any program can be run on any public account without its owner. A seat is a sender whose code you wrote.
5. Reply with context
To read another actor’s value, send a request that carries your own context, and have the other actor reply with the value and the context back.
// request, in an actor you write
enum Request { Query { payload: Vec<u8> } }
// reply, sent to input.from
enum Reply { Answer { value: u64, payload: Vec<u8> } }
The asking handler runs twice: once to ask, once to continue. The returned context saves it from storing intermediate state. Always check that the reply’s sender is the actor you asked.
The shipped readers do not return context. The clock’s ReadTimestamp replies Reply::Timestamp(ms) (lez/programs/clock/core/src/lib.rs) and the native token’s ReadState replies StateReply { state }, so a handler that reads them keeps its context in its own state.
6. Receive without failing
A handler that receives value by AsyncMessage must accept every legitimate message, because a stored message is never returned.
Example. The native token’s Credit reads an empty balance as zero, so the first credit creates the balance. It refuses only a credit that no native Transfer sent, and an overflow.
7. The proof-carrying message
A private actor of program P sends a message to a public actor of P. The public actor checks input.from_own_program().
Because the private handler ran inside the proof, the public actor now knows a fact about hidden state: “an actor running my code, in a state my code allowed, decided to send this”. It does not know which account.
Anonymous votes, hidden multisig members, credentials and honest hidden-information games all use it.
8. Pseudonym
A private actor presents a blinded address as its sender. The public side sees a stable name it can store, count and reply to, plus the program and the message, but not the account.
- One address per relationship: linkable inside it, unlinkable across.
- A fresh address per action: unlinkable by address, though timing, amounts and fees can still link actions.
Pattern 7 says what kind of actor spoke. Pattern 8 says which one, under a name of its choosing.
The name hides the account from observers and public actors, but not from a private party who sends to that address: sealing a message needs the recipient’s keys and blinding factor, which reveal the account. See Private accounts.
9. Async message for strangers
Send value and notifications to other people with an AsyncMessage, not a SyncMessage. The same code then serves public recipients (delivered at once) and private ones (stored).
10. Accept by answering
A request is an AsyncMessage that asks for something: pay this, sign that, swap these. It carries no authority and cannot act. The recipient accepts by sending a transaction of their own that marks the actors the answer needs, and declines by doing nothing.
The handler has two halves. The asking message is unauthenticated and only records the request. The answering message arrives as a root call, so it checks input.is_authorized() && input.from.is_none() and then attaches the grant it was handed to whatever it has to send.
Collecting the request is not consent: a design that waits for collection to authorize something never receives that authority, so it fails without acting.
11. Anyone may poke
A public actor exposes a message that anyone can send and that only moves the actor forward by its own rules: release what is due, close what has expired, pay what is owed.
Why. A program runs only when a transaction reaches it. Poke messages let any interested party, or a paid keeper, supply the transaction.
12. The public referee
A private participant cannot be forced to act, so a public actor holds a bond and a deadline. The participant answers the challenge in time, with a proof-carrying message, or the referee pays the bond to the other side.
Use it for games, disputes, and any protocol with a “your move” step.
13. Seed-authorized custody
Funds live in the balance actor of a PDA. Only the program can move them, by granting the seed on a SyncMessage. Derive the seed from the instance’s identity so instances cannot spend each other’s funds. If a caller supplies the seed and it is stored, check on that first write that it derives the actor’s own account (for a private PDA the derivation also takes the npk and vpk). A stored seed that does not derive the account locks the funds permanently: every later payout fails.
14. The receipt actor
To make something happen exactly once, address it to an actor whose account is derived from the thing’s unique id. The handler requires an empty state and writes a marker.
Example. The bridge’s deposit receipt.
15. Write once
Configuration that must never change is written with write_once, which accepts the first value and any identical repeat, and refuses anything else.
16. Windows for expiry
A handler that must not run outside a period (before it starts or after a deadline) returns a validity window. No message, no clock, no state.
17. Clock assertion
A handler that depends on time passing either carries a validity window (pattern 16), which covers most deadlines, or reads the clock with ReadTimestamp and handles the reply. Read with the coarsest granularity that is precise enough: a private transaction stays valid only while the rounded value it read still holds.
18. Group-owned private PDA
Shared hidden state: a private PDA whose keys come from a group secret. Any member can operate it; only the program can authorize spending from it. Combine with pattern 13 for a hidden vault.
Anti-patterns
- Marking more actors
:signthan the call needs. The root program holds every grant you mark and can pass any of them on. - A reply with fast-changing data to a private actor. The proof goes stale as soon as the value changes.
- One enum for everything a program sends and receives. It forces handlers for messages that should never arrive.
- State that grows without bound. Nothing prunes it, and every later run pays to load it.
- Deriving meaning from an address. An address is a name. Check the sender and the state.