Solana's account model, for systems programmers

Programs hold no state, every account has one owner, and every transaction declares what it touches up front. What that means for how you write a program.

On this page
  1. Everything is an account
  2. Ownership is the security model
  3. Programs are stateless
  4. Transactions declare their accounts up front
  5. Rent
  6. Rules I'm writing programs by

Everything is an account

On Ethereum a contract owns its storage. On Solana a program owns nothing inside itself. All state, including the program's own code, lives in accounts, and an account is a record in one large key-value store:

Field What it holds
key 32-byte address, usually an ed25519 public key
lamports Balance. 1 SOL = 1,000,000,000 lamports
data Byte array, up to 10 MiB
owner Address of the program allowed to change data
executable Whether data is a program that can be called

The closest picture from C: a flat address space of fixed-size buffers, each tagged with the one process allowed to write it. A program is a function that takes a list of those buffers, checks them and mutates some of them. No globals. Nothing kept between calls.

Ownership is the security model

The runtime enforces three rules. Your code doesn't have to:

  1. Only the owner program can change an account's data.
  2. Only the owner can debit lamports. Anyone can credit them.
  3. Only the owner can reassign the owner, and only when the data is zeroed.

A wallet is an account owned by the System Program, with no data. "Sending SOL" is asking the System Program to debit your account; it agrees because your signature is on the transaction. A program that stores a user's balance creates a new account and makes itself the owner. After that, no other program can write it.

The runtime guarantees an account your program owns wasn't written by anyone else. It does not guarantee that the account a caller passed in is the one you expected. That gap is where most Solana bugs come from: missing checks, not broken logic. Every account a program receives needs its owner, address and type checked.

Programs are stateless

A deployed program is an account with executable set. With the upgradeable loader it is two accounts: the program account you call, and a program-data account holding the bytecode and the upgrade authority. Neither stores application state.

This took me longest. A counter program doesn't have a counter. It has a function that takes a counter account as input, and the same program can serve a thousand counter accounts. The token program works the same way: one program, millions of token accounts.

// Every Solana program, without a framework.
pub fn process_instruction(
    program_id: &Pubkey,      // this program's own address
    accounts: &[AccountInfo], // every account the instruction may read or write
    data: &[u8],              // instruction arguments, encoded however you like
) -> ProgramResult {
    // check, deserialize, mutate, return
    Ok(())
}

Transactions declare their accounts up front

A transaction lists every account its instructions will touch and marks each one writable or read-only, signer or not. The runtime schedules from that list. Two transactions writing different accounts can run in parallel on different cores. Two writing the same account run one after the other.

That is lock scheduling with the lock set known in advance. Three consequences for program design:

  • Hot accounts serialise. If every user writes one global stats account, every transaction waits on the same write lock. Per-user accounts don't.
  • No discovery at run time. A program can't ask for "all accounts I own" during execution. The client works out which accounts are needed and passes them in. Program derived addresses exist for this: addresses both sides can compute from known inputs.
  • Transactions are small. The limit is 1,232 bytes and each address costs 32, so a legacy transaction holds a few dozen accounts. Version 0 transactions fit more through address lookup tables.

Rent

Every validator stores every account, so an account has to hold a minimum balance for its size. With that balance it is rent-exempt, and new accounts must be. The minimum is two years of rent at the current rate:

minimum = (128 + data_len) bytes × 3,480 lamports per byte-year × 2 years

The 128 bytes are the account's own metadata. An empty account needs 890,880 lamports (just under 0.0009 SOL). A 1 KiB account needs 8,017,920 lamports (about 0.0080 SOL). The Rent sysvar computes this on-chain, and solana rent <bytes> does it from the command line.

The deposit isn't spent. Closing an account zeroes it and returns the lamports. So programs should let users close accounts they no longer need, and size accounts for what they hold, not for "just in case".

Rules I'm writing programs by

  • Design the accounts first. Which accounts exist, who owns them, who pays for them, and what their addresses derive from.
  • Check every input account. Owner, address, signer, writable, type. Frameworks like Anchor make the checks declarative, but each one still has to be asked for.
  • Size accounts on purpose. Space costs rent up front, and an account can only grow by 10 KiB per instruction.
  • Avoid shared writable state. Spread writes across per-user or per-market accounts so transactions run in parallel.
  • Count compute. Every instruction runs under a hard budget. That is the next thing I'm measuring.