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

Events

An event is how a program tells the outside world that something happened, without putting it in state.

Emitting

pub struct ProgramEvent {
    /// Selector bytes allowing to distinguish event type. By convention, the
    /// first 8 bytes of `sha256("<program>::<EventName>")`.
    pub selector: [u8; 8],
    /// The arbitrary event-data emitted in the program output.
    pub data: Vec<u8>,
}

A handler attaches one with Response::event(..). The selector lets a consumer filter by event type without decoding the data; the record’s program_account_id says which program emitted it.

Source: lee/state_machine/core/src/program/mod.rs.

Consuming

The indexer records each event with its context:

pub struct EventRecord {
    pub block_id: BlockId,
    pub tx_index: u32,
    pub tx_hash: HashType,
    pub program_account_id: AccountId,
    pub selector: Selector,
    pub data: Vec<u8>,   // base64 over the wire
}

The indexer keeps no events by default: event_filter defaults to keeping nothing, and a query or subscription the filter does not cover is refused as UncoveredEventQuery, not answered with an empty list. Under any filter but archival, that includes one naming no program, and one naming no selector for a program declared with a selector list. The indexer warns at startup: “Configured event filter keeps no events”. Set "event_filter": "archival" to keep everything, as the debug and docker all-in-one configurations do, or name the sources: {"sources": [{"program_id": "<base58 program account id>", "selectors": ["<16 hex chars>"]}]}, omitting selectors to keep all of that program’s events. An empty selectors list, or a program named twice, is refused at startup. Coverage is tracked per block range, so changing the filter later splits history into segments, and a ranged query is answered only if every segment it touches covers it.

Query them with getEvents or stream them with subscribeToEvents. A live stream has no block range, so the filters differ:

pub struct GetEventsFilter {
    pub from_block: Option<BlockId>,
    pub to_block: Option<BlockId>,
    pub tx_hash: Option<HashType>,
    pub program_account_id: Option<AccountId>,
    pub selector: Option<Selector>,
}

pub struct EventSubscriptionFilter {
    pub tx_hash: Option<HashType>,
    pub program_account_id: Option<AccountId>,
    pub selector: Option<Selector>,
}

Every field is optional, except that a getEvents call must carry tx_hash or from_block or is rejected. With tx_hash set, the block range fields are ignored. The normal filter is program_account_id plus selector: one program’s one event type.

A ranged query is checked against the tip first, so FromPastTip, ToPastTip or Inverted come first: on a fresh indexer, blocks 0 to 2000 give to_block 2000 exceeds the indexed tip 0, not a span error. Only a range inside the tip that asks for more than 1000 blocks at once fails with SpanExceeded. The response is separately capped at 8 MiB of estimated encoded size, past which the call fails with EventResponseTooLarge, so a client reads history in pages of ranges.

Source: lez/indexer/service/protocol/src/lib.rs and lez/indexer/service/src/service.rs.

The rule that shapes designs

Events emitted by a private handler are dropped: not encrypted, not deferred, discarded. A private actor’s handler runs inside a proof, and its events do not leave it. Public actors in the same privacy-preserving transaction keep theirs, recorded like any public transaction’s events.

So an announcement an indexer or a UI must see has to come from a public actor. Usually the private actor sends a message to a public one, which emits the event in its own handling: the private side proves the fact, the public side publishes it.

See also What you cannot do privately.

What events are not

They are not state. Nothing reads them back on chain, no handler can see another actor’s events, and a program cannot condition on one. A fact that must be enforced belongs in an actor’s state, where a handler can assert on it. Events are for observers.