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

System programs and the block

The block

A block is a list of transactions. The sequencer adds two of its own at the end: the fee distribution, then the clock tick. The clock transaction must be exactly the one expected for the block’s id and timestamp, or the block is invalid (lez/chain_state/src/apply.rs).

User transactions cannot modify the clock account or the three fee accounts (state, escrow, inbox), with one exception: a private transaction’s in-proof fee credit to the fee inbox. A transaction that pays a declared fee and would change one of them is reverted and still pays that fee; any other such transaction makes the block that carries it invalid.

Native token

The native token is a small, complete actor program, built into the engine rather than deployed (lee/state_machine/core/src/native_token.rs).

pub enum Message {
    Transfer { to: AccountId, amount: Balance },
    Credit(Balance),
    ReadState,
}
  • Transfer debits the balance actor it is sent to, which needs a grant the caller attaches, and sends Credit to the destination as an async message.
  • Credit is accepted only from the native token program itself.
  • ReadState replies to the sender with a StateReply (its own type, not a variant of this one), handing back the grant the reader lent (with_lent_grant) so the reader can act on what it read.

There is one actor per account: Actor::native_balance(account_id). A program that holds funds uses custody_transfer with a PDA seed (see Authorization).

Clock

The clock program (lez/programs/clock) is one actor at a fixed account, CLOCK_PROGRAM_ACCOUNT_ID, holding { block_id, timestamp }. The sequencer sends it Tick as the last transaction of every block, so every other transaction in block N sees block N-1’s time.

A program reads it, and the read is rounded down:

MessageReplyRounded to
ReadBlockId { granularity }Reply::BlockId(u64)a multiple of granularity blocks
ReadTimestamp { granularity }Reply::Timestamp(ms)a multiple of granularity milliseconds

With a granularity of 50 at block 57 you are told 50: the reader chooses the coarseness per read.

Reading has two constraints. Only an actor can read the clock, so a transaction whose root goes straight to it fails. And the reply arrives as a message, so the reader needs actor_entrypoint!(raw ..): the typed entrypoint would try to decode the reply as the reader’s own message type.

test_programs/guest/src/bin/cooldown.rs, abbreviated to its control flow, sends a read to the clock, recognises the reply by its sender, and only then does its work:

lee_core::actor_entrypoint!(raw handle_message);

fn handle_message(input: &ReceiveInput) -> Response {
    let clock = clock_core::clock_actor();
    if input.from != Some(clock) {
        return Response::keep_state().send_sync(
            clock,
            &clock_core::Message::ReadTimestamp { granularity: 1 },
        );
    }
    let Reply::Timestamp(now) = Reply::from_message(&input.message) else {
        panic!("Cooldown reads only the clock's timestamp");
    };
    // ... compare `now` against the stored timestamp, then record it
}

Reply::from_message decodes without verifying who sent it, so a program that skips the input.from check accepts a forged reply from anything that can reach it.

Reading costs validity. A private transaction that read the clock stays valid only while the rounded value is still the one it read. A granularity of 1 expires almost immediately.

Gating on time without reading

Most deadlines need no read. A response can carry a validity window, and the block being built either satisfies it or the transaction’s effects do not apply: a public transaction outside its window is reverted with OutOfValidityWindow and still pays its fee. A private transaction’s proof carries its private handlers’ windows, checked before anything runs: if they exclude the block, the sequencer skips the transaction and it pays nothing. Its public handlers’ windows are checked at settlement and fail like a public handler’s panic (What the handler returns): reverted and charged when the proof carries a fee fallback or a public signer declares the fee, dropped unpaid otherwise. The reference wallet’s simulation does not check windows, so it will send a transaction that is not yet or no longer valid. test_programs/guest/src/bin/time_locked_transfer.rs, without its doc comment and imports:

lee_core::actor_entrypoint!(handle_message);

fn handle_message(
    input: &ReceiveInput,
    (amount, deadline, receiver): (u128, Timestamp, AccountId),
) -> Response {
    let payer = Actor::native_balance(input.receiver.account_id);
    Response::keep_state().timestamp_window(deadline..).send(
        SyncMessage::new(
            payer,
            &native_token::Message::Transfer {
                to: receiver,
                amount,
            },
        )
        .with_grants([payer]),
    )
}

It keeps no state and never learns the time: the deadline arrives in the message, and timestamp_window(deadline..) makes the transaction valid only from that moment on.

Actor::native_balance(..) addresses the balance actor: a program moves balance by sending to it, not by writing a number.

.with_grants([payer]) passes the grant over payer on to the message it sends (Authorization).

A window is cheaper, needs no reply, and covers “not before” and “not after”. Read the clock only when the program has to store the time or branch on it.

Fee

The fee program holds the fee market’s state (How fees are priced) and distributes each block’s fees: base revenue to the fee escrow, tips and a smoothed payout from the escrow to the producer, the sequencer that built the block. The node is the only caller whose effects apply, and the program refuses a call from another program: Distribute runs in the block’s fee transaction, and Refund runs as a settlement step of a fee-paying transaction that left part of its reserve unspent. Distribute uses the reply pattern: it asks the fee inbox’s native balance actor for its state and pays out when the reply arrives (lez/programs/fee/src/main.rs).

Bridge

The bridge pays out deposits made on L1 from its custody PDA (lez/programs/bridge/src/main.rs). The sequencer injects one transaction per L1 deposit, once the deposit is final on L1, or earlier in a pinned block: a zone block inscribed in the same L1 transaction as a transfer that re-creates the deposit’s notes unchanged, so the block lands on L1 only where the deposit does. Other sequencers and the indexer rebuild each injected deposit from their own view of L1 and refuse a block whose deposit they cannot rebuild byte for byte (check_injected_deposits, lez/chain_state/src/apply.rs).

Withdrawals are not available: wallet bridge withdraw exists, but the program refuses it with Withdraws are disabled in the current version of LEZ, so value moves onto a zone but not yet back off it.

  • The root goes to a receipt actor: a PDA derived from the deposit’s id.
  • The handler requires the receipt to be empty, writes a marker, and pays the recipient from the bridge’s custody PDA.
  • A second delivery of the same deposit finds the marker and fails.

A receipt actor keyed by an id is a simple way to make something happen at most once.

Because of admission, a deposit names its recipient by evidence, not by bare id: the account’s initial key, or the program and seed of a PDA (DepositRecipient, lez/programs/bridge/core/src/deposit.rs).

Sequencer stake

The stake program holds the committee’s stakes and the channel configuration. Staking, unstaking, slashing and channel parameter changes all go through it. When two thirds of the accredited keys hold a standing vote for the same values, those values replace the current ones in that transaction and every vote is cleared. A vote for the values already in force is refused, and only finality_depth is fixed at genesis. A key’s stake is released once the configuration removing it is finality_depth L1 blocks deep, not zone blocks; Finality says how it relates to the L1’s security_param.

It is also where system upgrades are approved. system_upgrader is one of the five programs every zone seeds at genesis, with the clock, fee, bridge and stake programs (builtin_programs, lez/programs/src/lib.rs), the only one seeded immutable, and every other system program’s address is its PDA. Its messages are Schedule, Apply, Cancel and Install. All but Apply carry committee approvals; Apply carries none and is valid only in the block at the activation_height the approved Schedule set. The program is lez/programs/system_upgrader; the operator side is the approve_system_change binary (lez/sequencer/service/src/bin/approve_system_change.rs) and the submitSystemApproval and getSystemProposals RPCs.

getSystemProposals reports the threshold alongside the queue, and on a fresh devnet it answers {"threshold":2,"proposals":[]}. That 2 is a floor, not a count: the threshold is two thirds of the committee but never fewer than two, so a single-key committee cannot upgrade itself unilaterally. Channel params voting needs two thirds with a floor of one, which is why a lone sequencer can change parameters but cannot approve a system change. Exercising it needs a second staked key.

What is not there

  • No randomness beacon.
  • No oracle among the built-in programs (logos-blockchain/lez-programs has a twap_oracle program).
  • No scheduler. Nothing runs unless a transaction delivers a message. Designs that need something to happen “at time T” rely on someone sending a transaction after T.