Skip to content

fix(aztec-listener): follow the L2 chain by block hash through the SDK block stream - #112

Draft
alejoamiras wants to merge 31 commits into
follow-chain-by-hash-listener-storefrom
follow-chain-by-hash-listener-stream
Draft

alejoamiras wants to merge 31 commits into
follow-chain-by-hash-listener-storefrom
follow-chain-by-hash-listener-stream

Conversation

@alejoamiras

@alejoamiras alejoamiras commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Part 3 of 6 of the stack that makes the explorer follow the L2 chain by block hash. Closes #97.

The block poller followed the chain by height, so after a prune it skipped the replacing block (#97). It now runs the SDK's L2BlockStream on the store and source from part 2. The stream follows the chain by hash and emits prunes. Each block goes out on one ordered, keyed stream with its pass's proposed tip, from which the API (parts 4 and 5) decides the canonical chain.

What changed

  • src/svcs/poller/pollers/block_poller/index.ts: each poll runs one pass (runPass).
    • currentChain follows the highest rollup version the nodes report (network-client/rollup-version.ts), read every CHAIN_INFO_POLL_INTERVAL_MS. A new version starts a new store from the reporting node's block 0.
    • The listener's start now opens the stream (init()), and fails if no node reports its rollup version in time, or the reporting node does not serve block 0.
    • The source elects a node, then L2BlockStream.sync() runs in batches of AZTEC_BLOCK_STREAM_BATCH_SIZE (default 10, at most the SDK's MAX_RPC_BLOCKS_LEN). An empty store starts at the snapshot's finalized block, inclusive. No history is replayed: the API's reconciliation fills the heights below.
    • The tips are published, and the proven side work runs, only after a pass whose stored tips match its snapshot.
    • processedProposedBlockHeight keeps following the store's tip once the store holds a block, so the previous listener version can resume from it.
    • The full sweep (AZTEC_ENABLE_FULL_SWEEP_CATCHUP) reads one block per idle pass, bounded by the store's tip.
  • block_poller/chain-publisher.ts: ChainPublisher, the stream's event handler.
    • It sends each block of a batch with onOrderedBlock, then stores the batch.
    • It checks each batch: the rollup version, the archive link between its blocks, and the link to the stored parent.
    • A node with only a shorter copy of the stored chain ends the pass and loses the pin (the prefix rule). Passes that end this way log a warning, then an error once they have lasted AZTEC_STREAM_STALL_ALERT_MS.
    • It refuses a prune it cannot confirm against the store, and stores nothing for a prune deeper than AZTEC_MAX_PRUNE_DEPTH. It stores a prune, or a proven tip that moves back, and moves the proven cursor back in one listener transaction.
    • It checks that each tier a node reports names the block the store holds at that height, and ends the pass on a proposed tip it did not send.
  • src/events/emitted/index.ts: onOrderedBlock(block, proposedTip) replaces onBlock. It publishes NEW_BLOCK_EVENT keyed by the network id, with ordered: { proposedTip }, and promotes the block's pending txs to proposed. A block is no longer sent a second time once proven, as block statuses come from the tips. src/svcs/message-bus/index.ts adds a typed publishKeyedMessage.
  • network-client/attested-reads.ts: openAttestedReader reads one node's tips and rollup version before its blocks, and marks a block finalized only at or below that node's finalized tip, on the stream's version. Block range requests (on-l2-block-range-request.ts) and the full sweep answer from one node through it, and their catch-up blocks carry finalized.
  • block_poller/proven-side-work.ts and handle-proven-block-txs.ts: the proven side work (fee juice balances, then proven tx states) runs on at most AZTEC_PROVEN_BLOCKS_PER_PASS (default 200) blocks per pass, read from the pinned node, and only on blocks of the stored chain. A failed block stops the run, and the next pass retries it. A prune, or a proven tip that moves back, moves the cursor back (lowerProcessedProvenBlockHeight, with LEAST).
  • pollers/l2-tips-poller.ts: tips are published from the pass's snapshot (publishTipsIfDue) instead of a separate timer.
  • svcs/database/heights.controller.ts: the chain height writers are removed. txs.controller.ts adds getTxsByHashes.
  • network-client/block-stream-source.ts: a pass ends on its own exit type (PassExit); a snapshot whose tiers are out of order fails the checks; block-0 reads check the genesis hash; pinnedBlockData and pinnedRead read from the pinned node.
  • Node URLs are redacted in the errors the pool's and the election probe's node clients throw (withRedactedErrors in pool.ts, network-client/redact-urls.ts), in the stream's logs (block_poller/stream-logger.ts), and in the logs of a failed pass, the proven side work and the rollup version read. The startup config print shows only each pool URL's origin.
  • Settings: AZTEC_BLOCK_STREAM_BATCH_SIZE and AZTEC_PROVEN_BLOCKS_PER_PASS are new. CATCHUP_POLL_WAIT_TIME_MS and L2_TIPS_POLL_INTERVAL_MS are no longer read. AZTEC_DISABLE_LISTEN_FOR_PROPOSED_BLOCKS now stops the whole stream, its tips and proven side work included; before, proven blocks and tips still went out.
  • network-smoke runs the API's block reconciliation every 5 s.

Also: before each batch goes out, the pass's tip is confirmed on the node. A node whose blocks do not match its own tip ends the pass there, sits out the next election, and then stays benched until its tip changes. A batch is stored only once every block in it is sent; after a failed send, the next pass sends the whole batch again, with its own tip. AZTEC_MAX_PRUNE_DEPTH is capped at CHAIN_WALK_BOUND (10,000), new in @chicmoz-pkg/message-registry, which the API's chain walk takes in part 5; a larger value fails at start. Block range repairs heartbeat before each read and each publish, and the attested reader heartbeats between its two setup reads.

How it was tested

  • tests/unit/chain-publisher.test.ts (new, 24 tests), on the SDK's own L2BlockStream:
    • after a node replaces published heights, the replacing blocks go out, each with its pass's tip, whether the chain regrows below, at or past the stored tip;
    • a pass longer than one batch goes out whole, each block with the pass's tip;
    • a failed send stores no block of its batch, and the next pass sends the whole batch again with its own tip;
    • once the node no longer holds the pass's tip, no batch goes out and nothing is stored;
    • a node whose blocks do not match its tips loses the pin, even to a node at most 2 blocks ahead of it, and sits out the next election once its tip moves ahead;
    • the prefix rule ends the pass, unpins the node and alerts while it lasts;
    • a prune deeper than the guard stores nothing and moves no cursor;
    • broken batch links, a block of another rollup version, unconfirmable prunes and tiers off the stored chain set the node aside, and an honest node is followed;
    • no proposed tip the pass did not send is stored;
    • a prune past a proof, or a proof that moves back, moves the proven cursor back.
  • tests/integration/chain-publisher.integration.test.ts (new, 3 tests, real Postgres): a prune and the proven cursor's move back commit in one transaction, and a prune or a proof that moves back rolls back with the cursor when a later write fails.
  • tests/unit/block-poller.test.ts (new, 12 tests): where an empty store starts; URLs redacted in a failed pass's log; tips and side work only after a settled pass, and none after a failed send; the legacy proposed height follows the store's tip; a new rollup version starts a new store from block 0.
  • tests/unit/block-poller-cursor.test.ts (rewritten for the proven side work): a 201-block advance over two passes, a failed block retried, side work only on blocks of the stored chain, the cursor moved down to a lower proven tip, and the same updates sent when it moves back.
  • tests/unit/block-processing.test.ts: onOrderedBlock sends the block under the network's key with its pass's tip, and promotes its pending txs.
  • tests/unit/attested-reads.test.ts (new), l2-block-range-request.test.ts and full-sweep-catchup.test.ts: one node answers, its tips are read first, and finalized is set only on blocks it attests. The reader heartbeats between its two setup reads; a range repair heartbeats before each read and each publish, and stops on a failed heartbeat.
  • Redaction: node-client.test.ts, stream-logger.test.ts and config-print.test.ts (new).
  • rollup-version.test.ts (new): the highest version a node reports in time. max-prune-depth.test.ts (new): the depth defaults to CHAIN_WALK_BOUND and refuses a larger one.
  • block-stream-source.test.ts gains 2 cases: tips out of order, and a block 0 other than the store's genesis. tests/characterization/producer-payloads.corpus.test.ts is updated for the ordered payload.
  • yarn lint is clean. yarn test passes 464 tests, with 1 skipped, and yarn test:integration passes 139.

Migration and rollout

  • No new migration. This part needs 0005_l2_block_stream.sql from part 2.
  • Release the listener first, then the API (parts 4 and 5) right after. The previous API keeps working with this listener: it ignores ordered and applies its height rules, so until the API is upgraded a stale catch-up block can still move its head back. The next ordered block restores the head. The previous listener also sent each block again once it was proven, and that second copy replaced the stale block; this listener does not. The stale block then stays canonical at its height, under the new API as well, until a reorg or prune reaches that height.
  • On its first start the listener stores nothing until a node is at or above the previous listener's recorded proven height (chainProvenBlockHeight). It then starts at the elected node's finalized block.
  • Ordered blocks share one partition through their key. Keep the block topic's partition count unchanged while this listener runs.
  • Roll back in reverse order, the API first: the new API ignores blocks without ordered, so its head would stall behind the previous listener. The previous listener resumes from processedProposedBlockHeight, which this version keeps at the store's tip. The l2_block_stream_* tables can stay.

🤖 Generated with Claude Code

alejoamiras and others added 30 commits November 27, 2026 19:32
ChainPublisher handles the block stream's events. It checks each one
against the store and the pass's tips, and a rejected event sets the
pinned node aside and ends the pass:

- a batch must carry the stream's rollup version, link block to block,
  and extend the stored block below it, read again from the node;
- a prune must land on a block the store holds, with a stored block
  above it, and stay within AZTEC_MAX_PRUNE_DEPTH;
- a tier event needs ordered tips and the stored hash at its height.

It sends a batch's blocks one by one, each with the pass's proposed
tip, and stores the blocks that went out, so a failed send goes out
again on the next pass. A prune, or a proven tip that moves back,
lowers the proven side work's cursor in the store's transaction.

A node with only a shorter copy of the stored chain ends the pass
without a prune: the handler warns when that wait starts and alerts
while it lasts.

The source gains reads of the pinned node outside the walk bound and
checks the block 0 a pass serves against the store's genesis block.
Blocks go out keyed by the network id, so the topic keeps their order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each tick of the block poller now runs one pass of the SDK block
stream over the elected node, through ChainPublisher, in place of the
proposed and proven height loops. After the pass:

- tips go out only when the store reached the pass's snapshot, taken
  from that snapshot, when they changed or the heartbeat is due; the
  separate tips timer is gone;
- heights.processedProposedBlockHeight follows the store's tip;
- after such a pass the fee juice balance work runs on the proven
  blocks above its cursor, at most AZTEC_PROVEN_SIDE_WORK_MAX of them,
  read from the pinned node, with balances read at each block's hash.
  A block that fails stops the run and goes again on the next pass. It
  covers every stored tx of the block, whatever its state, and the
  cursor drops to the store's proven tip when it is above it, as the
  heights of a new rollup version start over;
- an empty store starts at the elected node's finalized block.

The rollup version comes from the nodes' node info, read again every
CHAIN_INFO_POLL_INTERVAL_MS. A new version starts a new store from the
reporting node's block 0; a node without block 0 stops the listener at
start, and a pass that serves another block 0 alerts.

Range requests and the full sweep read one node per request, its tips
and version first, and mark a block final at or below that node's
finalized tip on the stream's rollup version. The sweep stays at or
below the store's tip.

The stream's own logs go through the listener's logger: a failed send
shows only there.

AZTEC_BLOCK_STREAM_BATCH_SIZE (10, at most 50) and
AZTEC_PROVEN_SIDE_WORK_MAX (200) are new; L2_TIPS_POLL_INTERVAL_MS and
CATCHUP_POLL_WAIT_TIME_MS are gone. AZTEC_DISABLE_LISTEN_FOR_PROPOSED_BLOCKS
now stops the whole stream, with its tips and its proven work.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Nothing calls storeBlockHeights, storeChainProposedBlockHeight or
storeChainProvenBlockHeight since the block stream replaced the height
loops. heights.chainProvenBlockHeight now keeps the old listener's last
value, which the stream's first start reads as its floor, so no writer
of it stays behind.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Every URL in a line the SDK block stream logs, in its message, an error
or its data, is replaced with <url> before the line reaches the
listener's logger.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The store lowers the checkpointed and proven tips to the ones a prune
carries and writes their hashes over the stored ones at those heights.
At or below the prune target no block of the pass writes them back, so
a wrong hash there stayed, the finalized block's included.

ChainPublisher now checks a prune as it checks a tier event before the
store sees it: the pass's tips must be in order, and each tier at or
below the target must name the block the store holds at its height.
Otherwise the node is set aside and nothing is stored.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On an empty store the stream started at the election probe's finalized
block. When the pass's own snapshot then showed a lower tip, the stream
downloaded nothing, still reported that tip, and the store recorded it
although no block up to it had been sent; later passes resumed above
it.

The start now comes from the snapshot the stream itself read, which it
reads before it plans the downloads. ChainPublisher also ends the pass
on a proposed tip the store does not hold with that hash, so a tip is
stored only once its block was sent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The side work reads proven blocks from the pinned node by height after
the pass settled, and the node can serve another block at a height by
then. Before a block's balances and tx states, the side work now
requires the stream's rollup version and the hash the store holds at
that height; below the store's finalized tip, where it keeps no hashes
and a block cannot change, the version alone. Any other block ends the
side work for the pass, which the next pass retries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The SDK block stream catches a handler's error, logs it and resolves,
so a pass whose send or store write failed went on to publish tips and
run the proven side work. ChainPublisher now keeps the first such error
of a pass, ends the pass quietly for the stream, and rethrows the error
from finishPass(), after sync() and before any post-pass work. A pass
that ends on the prefix rule, or on a rejected event, stays quiet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Nothing in this listener reads heights.processedProposedBlockHeight;
the listener's previous version resumes from it after a rollback. It
was written only by a pass that moved the store's tip, so a pass that
failed after storing blocks left it behind for good. Each pass now
writes the store's proposed tip whenever the stored height differs,
before it publishes tips.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ChainPublisher's doc keeps its one invariant, the prefix wait and the
tips publisher lose comments that narrate their code, and the stream
logger's names only bigints, since field elements implement toJSON.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The listener's block stream starts an empty store at the finalized
block, so a fresh explorer-api gets the blocks below it only through
its block reconciliation. When the API's startup reconciliation runs
before any block has arrived it has no upper bound and skips, and the
next request came on the 120 s default cadence, at the edge of the
120 s wait for block 1. The run's explorer-api now reconciles every
5 s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A handler failure from a node read reaches the poll loop through
finishPass(), which logged its stack raw. The loop now logs the error
through inspect(), so its cause is included, and through the stream
logger's URL redaction. File URLs are left alone, as Node prints an ES
module's stack frames as file URLs.

The loop's pass is exported as pollOnce() to test that path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The handler accepted the stream's proposed tip when the store held the
same hash at that height. A prune writes each tier it lowers, hash
included, before any block of the pass is sent, so after an interrupted
prune that hash can sit above the stored tip; a later pass that
downloads nothing would then move the tip over unsent blocks. The
handler now requires the store's tip to already equal the event, which
every valid pass has reached through its blocks, and writes nothing.

The tier-order check moves from the handler to the stream source's
snapshot checks, so the election and every pass refuse tips out of order
before any block is read. The handler's check could no longer fire.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Below the store's finalized tip there is no stored hash to compare, so
the proven side work takes the pinned node's block as it is served.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The handler and the poll loop took any error named AbortError for an
intentional end of the pass. One from a dependency, such as the send,
was rethrown unrecorded; the stream dropped it, and the pass went on to
write the legacy height and sweep as if it had succeeded.

The stream source and the handler now throw PassExit, an AbortError
subclass, so the SDK's stream still ends the pass without logging. Only
that type is an exit: any other error, an AbortError included, is
recorded, rethrown after the pass and logged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…has a block

A pass that elects no node, or follows one at block 0, leaves the new
store empty, and the listener wrote its tip, 0, over the previous
version's proposed height. After a rollback that version lifts a 0 to
the chain's proven height and sends again the blocks it had sent above
it. The height is now written only once the store holds a block.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A store started at the snapshot's finalized block whenever its proposed
tip was 0. A prune to block 0 leaves the tip at 0 over the hashes above
it, so after such a prune whose first send failed, the next pass skipped
the new chain's blocks below its finalized block. A store is now empty
only when it holds no block hash at or above 1, and the next pass starts
at block 1.

The legacy proposed height waits on the same predicate, so a store
pruned to block 0 writes its tip there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The SDK's fetch names the node's URL in the errors it throws, and the
listener logs those errors from many places: the poll loop, the stream
source's election, node info, validator stats and the RPC-node error
event. The pool's client now wraps the fetch so that every error it
throws has its URLs redacted, in place, before the SDK client or any
caller sees it. The offline markers that callNodeFunction looks for
name no URL and stay.

The redaction moves to network-client/redact-urls.ts, and the rollup
version and proven side work warnings go through it too.

The RPC-node error event's message, cause and stack are now redacted
as well.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…attern

The generic URL pattern stops at an apostrophe, which a path can hold,
so the rest of such a URL stayed in the text. The redactor now first
replaces each AZTEC_RPC_URLS entry and its parts, longest first: the
URL as configured and its href, host, hostname, path, query, user and
password, raw and decoded, and each path segment and query value of 8
characters or more. The generic pattern then runs as before, and file
URLs still stay.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The redactor replaced each part of the pool's URLs ignoring case, so a
host named `gateway` turned "Bad Gateway" into "Bad <url>" in every
node's errors, and callNodeFunction no longer set a node offline on
that answer. The parts now match as written: an error names a part as
configured or as the URL parser normalized it, and the redactor lists
both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Node's fetch refuses a URL with userinfo and names the whole URL in its
error, and the election logs that error's message when a probe fails.
Such a URL never works, but it can be configured by mistake. The
probe's client now wraps its fetch like the pool's, so its errors have
their URLs redacted before the SDK client or the election sees them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The startup config printed each pool entry's URL whole. It now prints
the entry's name and the URL's scheme, host and port, with "/…" when the
URL has a path or query, and never its userinfo, path or query. The
print predates the block stream.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…r holds

The stream emits each batch before it checks the snapshot's tip, so blocks went out with a tip the node had since dropped, and the API kept dead blocks below that tip canonical.
Each batch now first confirms that tip against the pinned node, and the pass ends, sending nothing, when the node no longer holds it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ound

AZTEC_MAX_PRUNE_DEPTH had no maximum, so a deeper prune could leave more canonical blocks above an ordered block's tip than the API demotes, halting its ordered topic.
The bound now lives in @chicmoz-pkg/message-registry as CHAIN_WALK_BOUND, and the listener's env takes it as both its maximum and its default.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A range request read up to 200 blocks serially without a heartbeat, so a slow one outlasted the Kafka session and was redelivered and rerun, delaying later repairs.
The handler now awaits the bus's heartbeat before each block read, and a failed heartbeat stops the request and rejects it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A normal pass of 15 blocks at batch size 10 sends every block once, each
with the pass's tip, and stores the pass's tip.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Storing the sent part of a batch let the next pass skip those blocks, so a
tip the node had dropped since stayed theirs. A failed send now stores
nothing, and the next pass sends the whole batch again with its own tip.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… serve

The pass ended, yet the node kept its pin: the sticky election keeps it while
no node leads it by more than two blocks. It is now benched until its tip
changes, through the prefix rule's bench, renamed benchPinnedUntilNewTip.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…and before each publish

The reader's tips and node-info reads ran back to back before the first
heartbeat, and a slow block read was followed by its publish with none
between; either pair can outlast the Kafka session.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… next election

A tip mismatch benched the node only until its tip changed. A node that
kept advancing cleared that bench at its next probe and, still the most
advanced, won every election while each of its passes failed the check,
so a healthy node never got the pin.

The mismatch now also costs the node the next election, which hands the
pin to any other eligible node; the sticky election then keeps that node
while the benched one leads it by no more than the pin's lead. A single
node still runs every pass, since a bench holding every eligible node
clears. The tip check still reads the tip's block through the checked
block read, but without that read's own bench, which had already unpinned
a node with no block at its tip.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…the proven batch setting

Ordered blocks go out with `ordered: { proposedTip }`, and catch-up
blocks a node's reader attests with `finalized: true`, the registry's
new field names. The attested reader's flag is `finalized` too.

AZTEC_PROVEN_SIDE_WORK_MAX becomes AZTEC_PROVEN_BLOCKS_PER_PASS, and
the prefix wait and the rollup version read use the renamed stall
alert and node probe settings. Defaults are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@alejoamiras
alejoamiras added this pull request to stack #116 November 27, 2026 20:45
@alejoamiras alejoamiras changed the title follow chain by hash listener stream fix(aztec-listener): follow the L2 chain by block hash through the SDK block stream Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Block poller never fetches blocks that replace heights it already processed

1 participant