The event listener¶
How event_listener polls the source chain, keeps its cursor, and makes sure every event is forwarded at least once.
You will learn / You need
You will learn: the polling loop, when the cursor moves and when it holds, what the three data files are for, and how a rejected forward is retried. You need: The static relayer and the relayer concepts.
Where the code lives¶
Everything is in one file, stealth_name_service/backend/testing_backend/src/bin/event_listener.rs. The
listener does not sign or submit transactions. It reads events with JSON-RPC and forwards each one by
HTTP to the event handler, or to the committee when consensus is on.
The loop¶
flowchart TD
A["start"] --> B{"EVENT_LISTENER_RESET_CURSOR?"}
B -- yes --> C["delete cursor, events,<br/>processed files"]
B -- no --> D
C --> D["load_cursor_or_init()"]
D --> E["query one page<br/>after cursor"]
E --> F["process_page()"]
F --> G["persist processed set"]
G --> H{"every event<br/>forwarded?"}
H -- no --> I["hold cursor<br/>retry next poll"]
H -- yes --> J["save page cursor"]
J --> K{"has next page?"}
K -- yes --> E
K -- no --> L["sleep POLL_MS"]
I --> L
L --> E
fetch_and_process_events() drains every available page in one pass, then main() sleeps
EVENT_LISTENER_POLL_MS (default 50 ms). A fetch error
is logged as Error fetching events and the loop continues.
Configuration¶
Config::from_env() reads everything once. A blank value counts as unset.
| Variable | Default | Effect |
|---|---|---|
EVENT_LISTENER_RPC_URL |
https://api.testnet.iota.cafe:443 |
JSON-RPC endpoint of the source chain. |
EVENT_LISTENER_PACKAGE_ID |
DEFAULT_PACKAGE_ID in the source |
Package whose module is watched. |
EVENT_LISTENER_MODULE |
coordinator |
Module filter. |
EVENT_LISTENER_EVENT_TYPE |
unset | Optional exact event type filter. |
EVENT_HANDLER_URL |
http://event_handler:5000/api/handle_event |
Forward target without consensus. |
CONSENSUS_INGEST_URL |
DEFAULT_INGEST_URL in src/consensus/config.rs |
Forward target when CONSENSUS_ENABLED is on. |
EVENT_LISTENER_POLL_MS |
50 | Sleep between passes. |
EVENT_LISTENER_DEDUPE_CAPACITY |
10000 | Size of the processed set. |
EVENT_LISTENER_RESET_CURSOR |
true | Anything except false wipes the state on start. |
The full table is in Configuration: Event listener.
Querying events¶
query_events() calls iotax_queryEvents with a MoveModule filter, the current cursor, a page size
of PAGE_LIMIT (100) and ascending order. The response is an EventPage of IotaEvents plus
nextCursor and hasNextPage. An EventID is (txDigest, eventSeq); eventSeq may arrive as a
string or a number, and both parse. A JSON-RPC error becomes JSON-RPC error: ….
The TLS client accepts invalid certificates (danger_accept_invalid_certs), so the listener works
against nodes with self-signed certificates.
The cursor¶
load_cursor_or_init() decides where to start:
- If
data/cursor.jsonexists, it resumes from the storedlast_cursor. - Otherwise it queries one page in descending order and uses the newest event id as the start. Events emitted before the listener started are never forwarded.
With the default EVENT_LISTENER_RESET_CURSOR=true the file is deleted at every start, so every run
starts from the chain tip. Set it to false to resume after a restart.
The cursor moves only after a whole page has been forwarded. If any event on the page failed, the cursor stays and the next pass re-reads the same page.
The data files¶
All three live under data/, which compose mounts from ./events_data on the host (shared with the
handler, which writes bridge_results.csv there).
| File | Constant | Content | Written |
|---|---|---|---|
data/cursor.json |
CURSOR_FILE |
{"last_cursor": EventID} |
After each fully forwarded page. |
data/processed_events.json |
PROCESSED_FILE |
Ids of forwarded events, oldest first | After each page, only if changed. |
data/events.jsonl |
EVENTS_FILE |
One JSON line per forwarded event | On each successful forward. |
De-duplication: ProcessedEvents¶
Holding the cursor means a page is read again, so events that already went through must be skipped.
ProcessedEvents keeps a HashSet for lookup and a VecDeque for order. At capacity it evicts the
oldest id. It is persisted only when dirty. A file that does not parse is logged
(Could not parse …; starting empty) and replaced.
An event is recorded only after a successful forward (a 2xx reply). The comment in process_page()
explains why: an event recorded before a failed forward would never be retried.
Events filtered out by EVENT_LISTENER_EVENT_TYPE are also recorded, so they never hold the cursor.
The set is the listener's only replay guard. It is bounded and local, which is why replay protection is an off-chain property of the relayer: see Replay protection is off-chain.
Forwarding¶
process_page() takes one timestamp, detected_at_ms, for the whole page, then POSTs each new event in
order. The body is a ForwardEventPayload:
| Field | Source |
|---|---|
source_event_tx_digest, source_event_seq |
The event id. |
source_event_timestamp_ms |
The event's timestampMs. |
detected_at_ms |
Listener clock at the start of the page. |
parsed_json |
The event's parsedJson, unchanged. |
sequenceDiagram
participant L as event_listener
participant T as handler or committee
loop each new event on the page
L->>T: POST ForwardEventPayload
alt 2xx
T-->>L: 202
L->>L: mark processed, append events.jsonl
else non-2xx or network error
T-->>L: 429 / 503 / error
L->>L: all_forwarded = false
end
end
L->>L: cursor moves only if all_forwarded
Forwards are sequential: one request in flight at a time. The loop does not stop at the first failure; it keeps going through the page. A failed event is retried on the next pass, so events after it may reach the target first. Order is not preserved across a failure.
Failure handling¶
| Response | Meaning | Listener action |
|---|---|---|
202 from the handler |
Queued | Processed. |
202 from the committee with rejected or duplicate |
The committee will not order it | Processed; not retried. |
429 from the handler |
Queue full | Handler rejected …:… with status 429; will retry; cursor held. |
403 from the handler |
Delivery gate on, request unsigned | Retried forever. With consensus on, point the listener at the committee, not the handler. |
503 from the committee |
Below quorum or ingest queue full | Retried; cursor held. |
| Network error | Target down | Forwarding error …; will retry; cursor held. |
Delivery is at least once. A forward that succeeded but whose reply was lost is sent again; the handler and the committee both de-duplicate.
Tests that pin the behaviour¶
event_listener.rs has no unit tests. Its behaviour is exercised end to end by the Track B and Track C
runs and by consensus_smoke.sh. The payload shape it
sends is pinned on the receiving side: decodes_the_listeners_payload_shape and
delivered_event_is_a_superset_of_the_listener_payload in src/consensus/tx.rs, and
payload_parses_with_and_without_committee_metadata in src/bin/event_handler.rs.