Accounts, actors and state
The account
An account is a nonce and a map (lee/state_machine/core/src/account.rs):
pub struct Account {
pub nonce: Nonce,
pub data: AccountData,
}
pub struct AccountData {
pub actor_states: BTreeMap<AccountId, ActorState>,
}
The key is a program’s account id. The value is a byte string that only that program’s handler is given and can change (the exception is the signature program, which holds a commitment to the account’s current signing key, has no handler and is written by the engine; see Keys, nonces and rotation). Three programs holding state on an account make three entries.
There is no balance field: the native balance is the entry under the native token program (NATIVE_TOKEN_PROGRAM_ID), ordinary actor state.
The actor
An actor is one entry of one account:
pub struct Actor {
pub account_id: AccountId,
pub program_account_id: AccountId,
}
Read it as “this program, on this account”:
| Actor | Meaning |
|---|---|
(alice, native token) | Alice’s native balance. Actor::native_balance(alice) builds it. |
(alice, token) | Alice’s holding in the token program. |
(vault_pda, multisig) | A multisig’s configuration, on an account the multisig program controls. |
(clock account, clock) | The clock. clock_core::clock_actor() builds it. |
Every message is addressed to an actor: the program id says which code runs, the account id which state it runs on.
The one rule
A handler reads and writes the state of the actor it is running for, and nothing else.
It does not see the account’s other entries or other accounts: the engine passes it no state but its own. Another actor’s state can reach it only inside a message, as that sender’s claim (Replies). If the token program needs Bob’s holding to grow, Alice’s token actor sends a message to Bob’s token actor, and Bob’s actor updates itself.
This rule keeps handlers small: each has one state to validate and one to produce.
Account ids
An account id is 32 bytes; its derivation decides who controls the account.
| Kind | Derived from | Controlled by |
|---|---|---|
| Public, key-owned | its first signing key | a signature by its current key |
| Public PDA | a program’s id and a seed | that program, by granting the seed |
| Private, regular | a nullifier public key and a viewing public key | the holder of the secret keys |
| Private PDA | a program’s id, a seed, and the two public keys | the program for spending, the key holder for operating |
| Builtin program | fixed by the protocol (a constant, or a name for system programs, the built-ins deployed as bytecode: Built-in programs) | the protocol; the sequencer committee upgrades system programs |
To operate a private PDA is to hold its keys, and so be the one who can run its handlers in a private transaction: read and update its state and collect its messages. Spending from it still needs a seed grant from the program.
The exact derivations are in Account-id derivations. PDAs are explained in Authorization.
A private account can also be named by a blinded address: a hash of the account id and a secret factor. Without the factor, the address alone does not reveal the account. Blinded addresses give a private actor pseudonyms; see Private accounts.
Public and private state
The two kinds of account hold the same Account structure, kept in different places.
- Public. The chain stores the account in the clear; anyone can read every actor state on it.
- Private. The chain stores only a commitment to the account; the owner keeps the contents. Each update publishes a nullifier for the old commitment and a new commitment.
A program, by default, does not need to choose which kind it runs on. The user who builds the transaction decides, by using a public or a private account. The same handler code handles both. It can still infer the kind where its inputs give it away. A message can say so and the program can route on it, but that is the sender’s claim, not the engine’s. A program that knows the seed can also tell its public PDA from a private one: an account id equal to AccountId::for_public_pda of its own program account id and that seed is the public PDA, since a private PDA’s id also hashes the owner’s keys and so never matches.
What an account is not
- Not owned by a program. It is a container on which many programs keep state.
- Not a balance. The balance is one actor among several.
- Not readable by other programs’ handlers (clients can read public accounts). A handler asks for a value with a message and receives a reply (Handlers and messages).