@aztec/stdlib 5.0.0-rc.1 → 5.0.0-rc.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (136) hide show
  1. package/dest/abi/buffer.d.ts +6 -2
  2. package/dest/abi/buffer.d.ts.map +1 -1
  3. package/dest/abi/buffer.js +13 -1
  4. package/dest/avm/avm.js +1 -1
  5. package/dest/aztec-address/index.d.ts +35 -5
  6. package/dest/aztec-address/index.d.ts.map +1 -1
  7. package/dest/aztec-address/index.js +32 -8
  8. package/dest/block/l2_block_source.d.ts +11 -25
  9. package/dest/block/l2_block_source.d.ts.map +1 -1
  10. package/dest/block/l2_block_source.js +0 -1
  11. package/dest/block/l2_block_stream/interfaces.d.ts +25 -5
  12. package/dest/block/l2_block_stream/interfaces.d.ts.map +1 -1
  13. package/dest/block/l2_block_stream/l2_block_stream.d.ts +29 -7
  14. package/dest/block/l2_block_stream/l2_block_stream.d.ts.map +1 -1
  15. package/dest/block/l2_block_stream/l2_block_stream.js +165 -152
  16. package/dest/block/l2_block_stream/l2_tips_store_base.d.ts +10 -2
  17. package/dest/block/l2_block_stream/l2_tips_store_base.d.ts.map +1 -1
  18. package/dest/block/l2_block_stream/l2_tips_store_base.js +33 -15
  19. package/dest/block/test/l2_tips_store_test_suite.d.ts +1 -1
  20. package/dest/block/test/l2_tips_store_test_suite.d.ts.map +1 -1
  21. package/dest/block/test/l2_tips_store_test_suite.js +94 -3
  22. package/dest/checkpoint/index.d.ts +2 -1
  23. package/dest/checkpoint/index.d.ts.map +1 -1
  24. package/dest/checkpoint/index.js +1 -0
  25. package/dest/checkpoint/simulation_overrides.d.ts +61 -0
  26. package/dest/checkpoint/simulation_overrides.d.ts.map +1 -0
  27. package/dest/checkpoint/simulation_overrides.js +98 -0
  28. package/dest/contract/interfaces/contract_data_source.d.ts +3 -5
  29. package/dest/contract/interfaces/contract_data_source.d.ts.map +1 -1
  30. package/dest/file-store/factory.d.ts +1 -1
  31. package/dest/file-store/factory.d.ts.map +1 -1
  32. package/dest/file-store/factory.js +7 -0
  33. package/dest/file-store/index.d.ts +2 -1
  34. package/dest/file-store/index.d.ts.map +1 -1
  35. package/dest/file-store/index.js +1 -0
  36. package/dest/file-store/memory.d.ts +24 -0
  37. package/dest/file-store/memory.d.ts.map +1 -0
  38. package/dest/file-store/memory.js +80 -0
  39. package/dest/interfaces/archiver.js +1 -1
  40. package/dest/interfaces/aztec-node-debug.d.ts +26 -1
  41. package/dest/interfaces/aztec-node-debug.d.ts.map +1 -1
  42. package/dest/interfaces/aztec-node-debug.js +20 -0
  43. package/dest/interfaces/aztec-node.d.ts +20 -20
  44. package/dest/interfaces/aztec-node.d.ts.map +1 -1
  45. package/dest/interfaces/aztec-node.js +8 -15
  46. package/dest/interfaces/chain_tips.d.ts +8 -47
  47. package/dest/interfaces/chain_tips.d.ts.map +1 -1
  48. package/dest/interfaces/chain_tips.js +1 -6
  49. package/dest/interfaces/checkpoint_parameter.d.ts +3 -3
  50. package/dest/interfaces/checkpoint_parameter.d.ts.map +1 -1
  51. package/dest/interfaces/checkpoint_parameter.js +3 -3
  52. package/dest/interfaces/prover-node.d.ts +6 -7
  53. package/dest/interfaces/prover-node.d.ts.map +1 -1
  54. package/dest/interfaces/prover-node.js +15 -10
  55. package/dest/kernel/hints/read_request.js +1 -1
  56. package/dest/kernel/log_hash.d.ts +1 -1
  57. package/dest/kernel/log_hash.d.ts.map +1 -1
  58. package/dest/kernel/log_hash.js +2 -2
  59. package/dest/kernel/note_hash.js +1 -1
  60. package/dest/kernel/nullifier.js +1 -1
  61. package/dest/kernel/private_log_data.d.ts +1 -1
  62. package/dest/kernel/private_log_data.d.ts.map +1 -1
  63. package/dest/kernel/private_log_data.js +1 -1
  64. package/dest/logs/app_tagging_secret.d.ts +21 -6
  65. package/dest/logs/app_tagging_secret.d.ts.map +1 -1
  66. package/dest/logs/app_tagging_secret.js +32 -22
  67. package/dest/logs/contract_class_log.d.ts +1 -2
  68. package/dest/logs/contract_class_log.d.ts.map +1 -1
  69. package/dest/logs/contract_class_log.js +0 -4
  70. package/dest/logs/message_context.d.ts +5 -15
  71. package/dest/logs/message_context.d.ts.map +1 -1
  72. package/dest/logs/message_context.js +1 -66
  73. package/dest/logs/pending_tagged_log.d.ts +6 -8
  74. package/dest/logs/pending_tagged_log.d.ts.map +1 -1
  75. package/dest/logs/pending_tagged_log.js +1 -31
  76. package/dest/p2p/checkpoint_attestation.d.ts +2 -1
  77. package/dest/p2p/checkpoint_attestation.d.ts.map +1 -1
  78. package/dest/p2p/checkpoint_proposal.d.ts +2 -1
  79. package/dest/p2p/checkpoint_proposal.d.ts.map +1 -1
  80. package/dest/p2p/consensus_payload.d.ts +2 -1
  81. package/dest/p2p/consensus_payload.d.ts.map +1 -1
  82. package/dest/rollup/checkpoint_header.d.ts +8 -3
  83. package/dest/rollup/checkpoint_header.d.ts.map +1 -1
  84. package/dest/rollup/checkpoint_header.js +20 -10
  85. package/dest/tests/factories.d.ts +1 -1
  86. package/dest/tests/factories.d.ts.map +1 -1
  87. package/dest/tests/factories.js +3 -12
  88. package/dest/trees/nullifier_membership_witness.d.ts +5 -8
  89. package/dest/trees/nullifier_membership_witness.d.ts.map +1 -1
  90. package/dest/trees/nullifier_membership_witness.js +0 -9
  91. package/dest/trees/public_data_witness.d.ts +7 -17
  92. package/dest/trees/public_data_witness.d.ts.map +1 -1
  93. package/dest/trees/public_data_witness.js +0 -27
  94. package/dest/tx/execution_payload.d.ts +1 -1
  95. package/dest/tx/execution_payload.d.ts.map +1 -1
  96. package/dest/tx/execution_payload.js +1 -1
  97. package/dest/tx/global_variable_builder.d.ts +2 -12
  98. package/dest/tx/global_variable_builder.d.ts.map +1 -1
  99. package/dest/tx/global_variables.js +2 -2
  100. package/package.json +8 -8
  101. package/src/abi/buffer.ts +13 -1
  102. package/src/avm/avm.ts +1 -1
  103. package/src/aztec-address/index.ts +36 -6
  104. package/src/block/l2_block_source.ts +10 -15
  105. package/src/block/l2_block_stream/interfaces.ts +25 -5
  106. package/src/block/l2_block_stream/l2_block_stream.ts +222 -172
  107. package/src/block/l2_block_stream/l2_tips_store_base.ts +36 -15
  108. package/src/block/test/l2_tips_store_test_suite.ts +73 -4
  109. package/src/checkpoint/index.ts +1 -0
  110. package/src/checkpoint/simulation_overrides.ts +171 -0
  111. package/src/contract/interfaces/contract_data_source.ts +2 -4
  112. package/src/file-store/factory.ts +7 -0
  113. package/src/file-store/index.ts +1 -0
  114. package/src/file-store/memory.ts +94 -0
  115. package/src/interfaces/archiver.ts +1 -1
  116. package/src/interfaces/aztec-node-debug.ts +37 -0
  117. package/src/interfaces/aztec-node.ts +30 -33
  118. package/src/interfaces/chain_tips.ts +8 -16
  119. package/src/interfaces/checkpoint_parameter.ts +3 -3
  120. package/src/interfaces/prover-node.ts +19 -11
  121. package/src/kernel/hints/read_request.ts +1 -1
  122. package/src/kernel/log_hash.ts +5 -2
  123. package/src/kernel/note_hash.ts +1 -1
  124. package/src/kernel/nullifier.ts +1 -1
  125. package/src/kernel/private_log_data.ts +4 -1
  126. package/src/logs/app_tagging_secret.ts +39 -23
  127. package/src/logs/contract_class_log.ts +0 -9
  128. package/src/logs/message_context.ts +7 -65
  129. package/src/logs/pending_tagged_log.ts +6 -33
  130. package/src/rollup/checkpoint_header.ts +16 -2
  131. package/src/tests/factories.ts +3 -6
  132. package/src/trees/nullifier_membership_witness.ts +0 -11
  133. package/src/trees/public_data_witness.ts +0 -31
  134. package/src/tx/execution_payload.ts +2 -1
  135. package/src/tx/global_variable_builder.ts +1 -17
  136. package/src/tx/global_variables.ts +2 -2
@@ -1,14 +1,25 @@
1
- import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
1
+ import { BlockNumber } from '@aztec/foundation/branded-types';
2
2
  import { AbortError } from '@aztec/foundation/error';
3
3
  import { createLogger } from '@aztec/foundation/log';
4
4
  import { RunningPromise } from '@aztec/foundation/running-promise';
5
5
 
6
- import type { PublishedCheckpoint } from '../../checkpoint/published_checkpoint.js';
7
- import { type L2BlockId, type L2BlockSource, makeL2BlockId } from '../l2_block_source.js';
8
- import type { L2BlockStreamEvent, L2BlockStreamEventHandler, L2BlockStreamLocalDataProvider } from './interfaces.js';
9
-
10
- /** Maximum number of checkpoints to prefetch at once during sync. Matches MAX_RPC_CHECKPOINTS_LEN. */
11
- export const CHECKPOINT_PREFETCH_LIMIT = 50;
6
+ import type { L2Block } from '../l2_block.js';
7
+ import {
8
+ type L2BlockId,
9
+ type L2BlockSource,
10
+ type L2TipId,
11
+ type LocalL2Tips,
12
+ makeL2BlockId,
13
+ } from '../l2_block_source.js';
14
+ import type {
15
+ L2BlockStreamEvent,
16
+ L2BlockStreamEventHandler,
17
+ L2BlockStreamLocalDataProvider,
18
+ LocalL2BlockId,
19
+ } from './interfaces.js';
20
+
21
+ /** Subset of the block source the stream depends on. Checkpoint payloads are no longer fetched here. */
22
+ type L2BlockStreamSource = Pick<L2BlockSource, 'getBlocks' | 'getBlockData' | 'getL2Tips'>;
12
23
 
13
24
  /** Creates a stream of events for new blocks, chain tips updates, and reorgs, out of polling an archiver or a node. */
14
25
  export class L2BlockStream {
@@ -17,7 +28,7 @@ export class L2BlockStream {
17
28
  private hasStarted = false;
18
29
 
19
30
  constructor(
20
- private l2BlockSource: Pick<L2BlockSource, 'getBlocks' | 'getBlockData' | 'getL2Tips' | 'getCheckpoints'>,
31
+ private l2BlockSource: L2BlockStreamSource,
21
32
  private localData: L2BlockStreamLocalDataProvider,
22
33
  private handler: L2BlockStreamEventHandler,
23
34
  private readonly log = createLogger('types:block_stream'),
@@ -27,12 +38,23 @@ export class L2BlockStream {
27
38
  startingBlock?: number;
28
39
  /** Instead of downloading all blocks, only fetch the smallest subset that results in reliable reorg detection. */
29
40
  skipFinalized?: boolean;
30
- /** When true, checkpoint events will not be emitted. Blocks are still fetched via checkpoints but only blocks-added events are emitted. */
41
+ /** When true, checkpoint events will not be emitted. Blocks are still fetched but only blocks-added events are emitted. */
31
42
  ignoreCheckpoints?: boolean;
32
- /** Maximum number of checkpoints to prefetch at once during sync. Defaults to CHECKPOINT_PREFETCH_LIMIT (50). */
33
- checkpointPrefetchLimit?: number;
43
+ /**
44
+ * When true, the block download loop is skipped entirely: `getBlocks` is never called and `blocks-added` is
45
+ * never emitted. Only the tip events (`chain-proposed`/`chain-checkpointed`/`chain-proven`/`chain-finalized`)
46
+ * and `chain-pruned` are emitted, driven by the `getL2Tips` snapshot. For consumers that track tips but never
47
+ * consume block payloads.
48
+ */
49
+ tipsOnly?: boolean;
34
50
  } = {},
35
51
  ) {
52
+ if (opts.tipsOnly && (opts.startingBlock !== undefined || opts.batchSize !== undefined || opts.skipFinalized)) {
53
+ throw new Error(
54
+ 'tipsOnly is incompatible with startingBlock, batchSize, and skipFinalized: all three are ' +
55
+ 'block-download options and there is no download loop in tips-only mode.',
56
+ );
57
+ }
36
58
  // Note that RunningPromise is in stopped state by default. This promise won't run until someone invokes `start`,
37
59
  // which makes it run periodically, or `sync`, which triggers it once.
38
60
  // Users of L2BlockStream decide what mode to run it in (_periodically_ vs _manually triggered_).
@@ -66,7 +88,8 @@ export class L2BlockStream {
66
88
 
67
89
  protected async work() {
68
90
  try {
69
- const sourceTips = await this.l2BlockSource.getL2Tips();
91
+ // The source tips snapshot is the plan for this pass; it is re-read after the walk-back if a divergence is found.
92
+ let sourceTips = await this.l2BlockSource.getL2Tips();
70
93
  const localTips = await this.localData.getL2Tips();
71
94
  this.log.trace(`Running L2 block stream`, { sourceTips, localTips });
72
95
 
@@ -77,16 +100,26 @@ export class L2BlockStream {
77
100
  );
78
101
  }
79
102
 
80
- // Check if there was a reorg and emit a chain-pruned event if so.
103
+ // Baseline for the chain-proposed event; captured before the local store mutates during the pass.
104
+ const prePassProposed = localTips.proposed;
105
+
106
+ // Walk back to find a reorg, floored at the local finalized tip (a legitimate reorg can never reach it, since
107
+ // finalized means the proving tx is itself L1-finalized). Seed the cache with ONLY the proposed tip: a stale
108
+ // tier seed at a reorged height equals the local old-fork hash, faking agreement and stopping the walk above the
109
+ // true divergence (an under-deep prune no later pass re-detects), whereas a stale proposed seed only masks the
110
+ // tip for one pass.
81
111
  let latestBlockNumber = localTips.proposed.number;
82
112
  const sourceCache = new BlockHashCache([sourceTips.proposed]);
83
- while (!(await this.areBlockHashesEqualAt(latestBlockNumber, { sourceCache }))) {
113
+ const walkFloor = localTips.finalized.block.number;
114
+ while (
115
+ !(await this.areBlockHashesEqualAt(latestBlockNumber, {
116
+ sourceCache,
117
+ sourceProposed: sourceTips.proposed.number,
118
+ }))
119
+ ) {
84
120
  if (latestBlockNumber === 0) {
85
- // We walked all the way back to genesis and the hashes still differ. This means the
86
- // local store and the source disagree on the genesis block itself — typically because
87
- // they were configured with different `genesisTimestamp`/prefilled state. Continuing
88
- // would underflow into negative block numbers and surface as "block hash not found
89
- // for -1" further down. Fail loudly with a meaningful error instead.
121
+ // Walked back to genesis and the hashes still differ: the two sides disagree on block 0 itself (usually
122
+ // different genesisTimestamp/prefilled state). Fail loudly rather than underflow into negative heights.
90
123
  this.log.error(`Genesis block hash mismatch between local store and source`, {
91
124
  localBlockHash: await this.localData.getL2BlockHash(BlockNumber.ZERO),
92
125
  sourceBlockHash: sourceCache.get(0) ?? (await this.getBlockHashFromSource(BlockNumber.ZERO)),
@@ -97,10 +130,30 @@ export class L2BlockStream {
97
130
  '(e.g. genesisTimestamp or prefilled public data).',
98
131
  );
99
132
  }
133
+ if (latestBlockNumber <= walkFloor) {
134
+ // A mismatch at or below the finalized tip cannot be a reorg (it contradicts L1-finalized state), so stop
135
+ // here and prune at most to the finalized tip rather than pruning finalized state on non-reorg evidence.
136
+ this.log.warn(`Block hash mismatch at or below the local finalized tip; stopping the walk-back here`, {
137
+ blockNumber: latestBlockNumber,
138
+ finalizedBlockNumber: walkFloor,
139
+ localBlockHash: await this.localData.getL2BlockHash(latestBlockNumber),
140
+ sourceBlockHash:
141
+ sourceCache.get(latestBlockNumber) ?? (await this.getBlockHashFromSource(latestBlockNumber)),
142
+ });
143
+ break;
144
+ }
100
145
  latestBlockNumber--;
101
146
  }
102
147
 
148
+ let pruned = false;
103
149
  if (latestBlockNumber < localTips.proposed.number) {
150
+ // Re-read the source tips after the (possibly slow) walk-back so the prune event carries fresh checkpointed
151
+ // and proven tips, and the prune-target clamp, download plan, and tier reconciliation track the post-prune
152
+ // source chain. Append only the re-read proposed tip: it is a fresh entry that cannot poison the (already
153
+ // finished) walk and serves the prune-event hash lookup below.
154
+ sourceTips = await this.l2BlockSource.getL2Tips();
155
+ sourceCache.add(sourceTips.proposed);
156
+
104
157
  latestBlockNumber = BlockNumber(Math.min(latestBlockNumber, sourceTips.proposed.number)); // see #13471
105
158
  const hash = sourceCache.get(latestBlockNumber) ?? (await this.getBlockHashFromSource(latestBlockNumber));
106
159
  if (latestBlockNumber !== 0 && !hash) {
@@ -115,169 +168,42 @@ export class L2BlockStream {
115
168
  checkpointed: sourceTips.checkpointed,
116
169
  proven: sourceTips.proven,
117
170
  });
171
+ pruned = true;
118
172
  }
119
173
 
120
- // If we are just starting, use the starting block number from the options.
121
- const startingBlock = this.opts.startingBlock !== undefined ? BlockNumber(this.opts.startingBlock) : undefined;
122
- if (latestBlockNumber === 0 && startingBlock !== undefined) {
123
- latestBlockNumber = BlockNumber(Math.max(startingBlock - 1, 0));
124
- }
125
-
126
- // Only log this entry once (for sanity)
127
- if (!this.hasStarted) {
128
- this.log.verbose(`Starting sync from block number ${latestBlockNumber}`);
129
- this.hasStarted = true;
130
- }
131
-
132
- let nextBlockNumber = latestBlockNumber + 1;
133
- // When checkpoints are ignored the local provider may omit `checkpointed`; in that case the fallback to
134
- // CheckpointNumber.ZERO is harmless because `nextCheckpointToEmit` is never consumed for emission (Loop 1 and
135
- // the startingBlock/skipFinalized adjustments below only feed checkpoint emission, which is gated off).
136
- let nextCheckpointToEmit = CheckpointNumber(
137
- (localTips.checkpointed?.checkpoint.number ?? CheckpointNumber.ZERO) + 1,
138
- );
139
-
140
- // When startingBlock is set, also skip ahead for checkpoints.
141
- if (
142
- startingBlock !== undefined &&
143
- startingBlock >= 1 &&
144
- nextCheckpointToEmit <= sourceTips.checkpointed.checkpoint.number
145
- ) {
146
- if (startingBlock > sourceTips.checkpointed.block.number) {
147
- // startingBlock is past all checkpointed blocks; skip Loop 1 entirely.
148
- nextCheckpointToEmit = CheckpointNumber(sourceTips.checkpointed.checkpoint.number + 1);
149
- } else {
150
- const startingBlockData = await this.l2BlockSource.getBlockData({ number: startingBlock });
151
- if (startingBlockData) {
152
- nextCheckpointToEmit = CheckpointNumber(Math.max(nextCheckpointToEmit, startingBlockData.checkpointNumber));
153
- }
154
- }
155
- }
156
-
157
- if (this.opts.skipFinalized) {
158
- // When skipping finalized blocks we need to provide reliable reorg detection while fetching as few blocks as
159
- // possible. Finalized blocks cannot be reorged by definition, so we can skip most of them. We do need the very
160
- // last finalized block however in order to guarantee that we will eventually find a block in which our local
161
- // store matches the source.
162
- // If the last finalized block is behind our local tip, there is nothing to skip.
163
- nextBlockNumber = Math.max(sourceTips.finalized.block.number, nextBlockNumber);
164
- // If the next checkpoint to emit is behind the finalized tip then skip forward
165
- nextCheckpointToEmit = CheckpointNumber(Math.max(nextCheckpointToEmit, sourceTips.finalized.checkpoint.number));
166
- }
167
-
168
- // Loop 1: Emit checkpoint events for checkpoints whose blocks are already in local storage.
169
- // This handles the case where blocks were synced as uncheckpointed and later became checkpointed.
170
- // The guard `lastBlockInCheckpoint.number > localTips.proposed.number` ensures we don't emit
171
- // checkpoints for blocks we don't have (e.g., when startingBlock skips earlier blocks).
172
- // Since only one checkpoint can ever be uncheckpointed, this loop should iterate at most once.
173
- if (!this.opts.ignoreCheckpoints) {
174
- let loop1Iterations = 0;
175
- while (nextCheckpointToEmit <= sourceTips.checkpointed.checkpoint.number) {
176
- const checkpoints = await this.l2BlockSource.getCheckpoints({ from: nextCheckpointToEmit, limit: 1 });
177
- if (checkpoints.length === 0) {
178
- break;
179
- }
180
- const lastBlockInCheckpoint = checkpoints[0].checkpoint.blocks.at(-1)!;
181
- // If this checkpoint has blocks we haven't seen yet, stop - they need to be fetched first
182
- if (lastBlockInCheckpoint.number > localTips.proposed.number) {
183
- break;
184
- }
185
- loop1Iterations++;
186
- if (loop1Iterations > 1) {
187
- this.log.warn(
188
- `Emitting multiple checkpoints (${loop1Iterations}) for already-local blocks. ` +
189
- `Next checkpoint: ${nextCheckpointToEmit}, source checkpointed: ${sourceTips.checkpointed.checkpoint.number}`,
190
- );
191
- }
192
- const lastBlockHash = await lastBlockInCheckpoint.hash();
193
- await this.emitEvent({
194
- type: 'chain-checkpointed',
195
- checkpoint: checkpoints[0],
196
- block: makeL2BlockId(lastBlockInCheckpoint.number, lastBlockHash.toString()),
197
- });
198
- nextCheckpointToEmit = CheckpointNumber(nextCheckpointToEmit + 1);
199
- }
200
- }
201
-
202
- // Loop 2: Fetch new checkpointed blocks. For each checkpoint, emit all blocks
203
- // from that checkpoint that we need, then emit the checkpoint event.
204
- // We prefetch multiple checkpoints, then process them one by one.
205
- let prefetchedCheckpoints: PublishedCheckpoint[] = [];
206
- let prefetchIdx = 0;
207
- let nextCheckpointNumber: CheckpointNumber | undefined;
208
-
209
- // Find the starting checkpoint number
210
- if (nextBlockNumber <= sourceTips.checkpointed.block.number) {
211
- const blockData = await this.l2BlockSource.getBlockData({ number: BlockNumber(nextBlockNumber) });
212
- if (blockData) {
213
- nextCheckpointNumber = blockData.checkpointNumber;
214
- }
174
+ // Pass atomicity: a prune mid-download leaves the source unable to serve the planned blocks, so the snapshot's
175
+ // tier tips may reference blocks the consumer never saw — skip tier reconciliation in that case. Tips-only mode
176
+ // has no download plan (the snapshot is one atomic getL2Tips read), so it always reconciles.
177
+ if (!this.opts.tipsOnly && !(await this.downloadBlocks(latestBlockNumber, sourceTips))) {
178
+ return;
215
179
  }
216
180
 
217
- while (nextBlockNumber <= sourceTips.checkpointed.block.number && nextCheckpointNumber !== undefined) {
218
- // Refill the prefetch buffer when exhausted
219
- if (prefetchIdx >= prefetchedCheckpoints.length) {
220
- const prefetchLimit = this.opts.checkpointPrefetchLimit ?? CHECKPOINT_PREFETCH_LIMIT;
221
- prefetchedCheckpoints = await this.l2BlockSource.getCheckpoints({
222
- from: nextCheckpointNumber,
223
- limit: prefetchLimit,
224
- });
225
- prefetchIdx = 0;
226
- if (prefetchedCheckpoints.length === 0) {
227
- break;
228
- }
229
- }
230
-
231
- const checkpoint = prefetchedCheckpoints[prefetchIdx]!;
232
-
233
- // Get all blocks from this checkpoint that we need, respecting batchSize
234
- const limit = Math.min(this.opts.batchSize ?? 50, sourceTips.checkpointed.block.number - nextBlockNumber + 1);
235
- const blocksForCheckpoint = checkpoint.checkpoint.blocks
236
- .filter(b => b.number >= nextBlockNumber)
237
- .slice(0, limit);
238
- if (blocksForCheckpoint.length === 0) {
239
- break;
240
- }
241
- await this.emitEvent({ type: 'blocks-added', blocks: blocksForCheckpoint });
242
- nextBlockNumber = blocksForCheckpoint.at(-1)!.number + 1;
243
-
244
- // If we've reached the end of this checkpoint, emit the checkpoint event and move to next
245
- const lastBlockInCheckpoint = checkpoint.checkpoint.blocks.at(-1)!;
246
- if (nextBlockNumber > lastBlockInCheckpoint.number) {
247
- if (!this.opts.ignoreCheckpoints) {
248
- const lastBlockHash = await lastBlockInCheckpoint.hash();
249
- await this.emitEvent({
250
- type: 'chain-checkpointed',
251
- checkpoint,
252
- block: makeL2BlockId(lastBlockInCheckpoint.number, lastBlockHash.toString()),
253
- });
254
- }
255
- prefetchIdx++;
256
- nextCheckpointNumber = CheckpointNumber(nextCheckpointNumber + 1);
257
- }
181
+ // End-of-pass reconciliation: chain-proposed fires against the pre-pass baseline (a post-prune re-read would
182
+ // equal the source tip and suppress it), then the tiers highest-to-lowest so the finalized <= proven <=
183
+ // checkpointed <= proposed invariant holds mid-pass.
184
+ if (this.blockTipDiffers(prePassProposed, sourceTips.proposed)) {
185
+ await this.emitEvent({ type: 'chain-proposed', block: sourceTips.proposed });
258
186
  }
259
187
 
260
- // Loop 3: Fetch any remaining uncheckpointed (proposed) blocks.
261
- while (nextBlockNumber <= sourceTips.proposed.number) {
262
- const limit = Math.min(this.opts.batchSize ?? 50, sourceTips.proposed.number - nextBlockNumber + 1);
263
- this.log.trace(`Requesting blocks from ${nextBlockNumber} limit ${limit}`);
264
- const blocks = await this.l2BlockSource.getBlocks({ from: BlockNumber(nextBlockNumber), limit });
265
- if (blocks.length === 0) {
266
- break;
267
- }
268
- await this.emitEvent({ type: 'blocks-added', blocks });
269
- nextBlockNumber = blocks.at(-1)!.number + 1;
188
+ const reconcileTips = pruned ? await this.localData.getL2Tips() : localTips;
189
+ if (!this.opts.ignoreCheckpoints && this.tipDiffers(reconcileTips.checkpointed?.block, sourceTips.checkpointed)) {
190
+ await this.emitEvent({
191
+ type: 'chain-checkpointed',
192
+ block: sourceTips.checkpointed.block,
193
+ checkpoint: sourceTips.checkpointed.checkpoint,
194
+ });
270
195
  }
271
-
272
- // Update the proven and finalized tips.
273
- if (localTips.proven !== undefined && sourceTips.proven.block.number !== localTips.proven.block.number) {
196
+ if (reconcileTips.proven !== undefined && this.tipDiffers(reconcileTips.proven.block, sourceTips.proven)) {
274
197
  await this.emitEvent({
275
198
  type: 'chain-proven',
276
199
  block: sourceTips.proven.block,
277
200
  checkpoint: sourceTips.proven.checkpoint,
278
201
  });
279
202
  }
280
- if (localTips.finalized !== undefined && sourceTips.finalized.block.number !== localTips.finalized.block.number) {
203
+ if (
204
+ reconcileTips.finalized !== undefined &&
205
+ this.tipDiffers(reconcileTips.finalized.block, sourceTips.finalized)
206
+ ) {
281
207
  await this.emitEvent({
282
208
  type: 'chain-finalized',
283
209
  block: sourceTips.finalized.block,
@@ -292,12 +218,114 @@ export class L2BlockStream {
292
218
  }
293
219
  }
294
220
 
221
+ /**
222
+ * Downloads every block from the post-prune cursor through the source's proposed tip, emitting `blocks-added`
223
+ * events. The return value gates tier-cursor advancement (pass atomicity): tier tips may only be reconciled when
224
+ * the plan that backs them ran to the proposed tip.
225
+ * @returns `true` if the plan completed (caught up, or delivered the proposed tip with a matching hash); `false` if
226
+ * the source no longer has a promised block, or served a fork at the proposed height mid-pass.
227
+ */
228
+ private async downloadBlocks(latestBlockNumber: BlockNumber, sourceTips: LocalL2Tips): Promise<boolean> {
229
+ // The post-prune cursor: the highest block number both sides agree on. Block downloads resume from here.
230
+ let nextBlockNumber = latestBlockNumber + 1;
231
+
232
+ // From a fresh local store, fast-forward past history the consumer doesn't care about.
233
+ const startingBlock = this.opts.startingBlock !== undefined ? BlockNumber(this.opts.startingBlock) : undefined;
234
+ if (latestBlockNumber === 0 && startingBlock !== undefined) {
235
+ nextBlockNumber = Math.max(startingBlock, 1);
236
+ }
237
+
238
+ if (this.opts.skipFinalized) {
239
+ // Finalized blocks cannot be reorged, so skip them — but keep the last finalized block as the guaranteed point
240
+ // where local and source agree, the floor the walk-back terminates against.
241
+ nextBlockNumber = Math.max(sourceTips.finalized.block.number, nextBlockNumber);
242
+ }
243
+
244
+ if (!this.hasStarted) {
245
+ this.log.verbose(`Starting sync from block number ${nextBlockNumber - 1}`);
246
+ this.hasStarted = true;
247
+ }
248
+
249
+ let lastDeliveredBlock: L2Block | undefined;
250
+
251
+ while (nextBlockNumber <= sourceTips.proposed.number) {
252
+ const limit = Math.min(this.opts.batchSize ?? 50, sourceTips.proposed.number - nextBlockNumber + 1);
253
+ this.log.trace(`Requesting blocks from ${nextBlockNumber} limit ${limit}`);
254
+ const blocks = await this.l2BlockSource.getBlocks({ from: BlockNumber(nextBlockNumber), limit });
255
+ if (blocks.length === 0) {
256
+ // The source no longer has a block the snapshot promised: the snapshot is provably stale, so report the plan
257
+ // incomplete and skip reconciliation this pass.
258
+ this.log.warn(`Block source returned no blocks for a promised range; skipping reconciliation this pass`, {
259
+ from: nextBlockNumber,
260
+ limit,
261
+ sourceProposed: sourceTips.proposed.number,
262
+ });
263
+ return false;
264
+ }
265
+ await this.emitEvent({ type: 'blocks-added', blocks });
266
+ lastDeliveredBlock = blocks.at(-1)!;
267
+ nextBlockNumber = lastDeliveredBlock.number + 1;
268
+ }
269
+
270
+ if (lastDeliveredBlock === undefined) {
271
+ // Loop never ran: caught up before the plan started, or startingBlock past the tip (A-1061). Trivially complete.
272
+ return true;
273
+ }
274
+
275
+ // Complete iff the block delivered at the proposed height carries the snapshot's proposed hash; a different hash
276
+ // means a same-height fork swap happened mid-pass, so the snapshot is stale.
277
+ const deliveredHash = (await lastDeliveredBlock.hash()).toString();
278
+ if (deliveredHash !== sourceTips.proposed.hash) {
279
+ this.log.warn(`Delivered proposed-block hash differs from snapshot; skipping reconciliation this pass`, {
280
+ blockNumber: lastDeliveredBlock.number,
281
+ deliveredHash,
282
+ snapshotHash: sourceTips.proposed.hash,
283
+ });
284
+ return false;
285
+ }
286
+ return true;
287
+ }
288
+
289
+ /**
290
+ * Returns whether the source tip differs from the local one and therefore warrants a tier event. Compares block
291
+ * number and, when both hashes are known, block hash. The hash comparison is skipped when the local hash is
292
+ * undefined or missing: world-state legitimately reports `undefined` hashes for tips ahead of its synced range,
293
+ * and comparing against an undefined hash would re-emit the event on every poll.
294
+ */
295
+ private tipDiffers(localBlock: LocalL2BlockId | undefined, sourceTip: L2TipId): boolean {
296
+ return this.blockTipDiffers(localBlock, sourceTip.block);
297
+ }
298
+
299
+ /**
300
+ * Block-only variant of {@link tipDiffers} for the proposed tip (an {@link L2BlockId}, with no checkpoint). Compares
301
+ * block number and, when the local hash is known, block hash. The hash comparison is skipped when the local hash is
302
+ * undefined: world-state reports `undefined` for its proposed hash, and a strict comparison would re-emit
303
+ * `chain-proposed` on every poll for it. ({@link L2TipsStoreBase} consumers always carry a hash, so the leniency is
304
+ * inert for them.)
305
+ */
306
+ private blockTipDiffers(localBlock: LocalL2BlockId | undefined, sourceBlock: L2BlockId): boolean {
307
+ if (localBlock === undefined) {
308
+ return true;
309
+ }
310
+ if (sourceBlock.number !== localBlock.number) {
311
+ return true;
312
+ }
313
+ if (localBlock.hash === undefined) {
314
+ return false;
315
+ }
316
+ return sourceBlock.hash !== localBlock.hash;
317
+ }
318
+
295
319
  /**
296
320
  * Returns whether the source and local agree on the block hash at a given height.
297
321
  * @param blockNumber - The block number to test.
298
- * @param args - A cache of data already requested from source, to avoid re-requesting it.
322
+ * @param args - A cache of data already requested from source (to avoid re-requesting it) and the source's
323
+ * advertised proposed tip from the pass snapshot (to detect an incoherent source).
299
324
  */
300
- private async areBlockHashesEqualAt(blockNumber: BlockNumber, args: { sourceCache: BlockHashCache }) {
325
+ private async areBlockHashesEqualAt(
326
+ blockNumber: BlockNumber,
327
+ args: { sourceCache: BlockHashCache; sourceProposed: BlockNumber },
328
+ ) {
301
329
  const localBlockHash = await this.localData.getL2BlockHash(blockNumber);
302
330
  if (!localBlockHash && this.opts.skipFinalized) {
303
331
  // Failing to find a block hash when skipping finalized blocks can be highly problematic as we'd potentially need
@@ -307,12 +335,28 @@ export class L2BlockStream {
307
335
  this.log.error(`No local block hash for block number ${blockNumber}`);
308
336
  throw new AbortError();
309
337
  }
338
+ if (!localBlockHash) {
339
+ // A missing local hash compares UNEQUAL: treating both-undefined as equal would stop the walk above the true
340
+ // divergence (an under-deep prune no later pass re-detects). Over-deep is the safe direction, and block 0 always
341
+ // resolves via the store's initialBlockHash so the walk always terminates.
342
+ this.log.trace(`No local block hash for block number ${blockNumber}; treating as unequal`);
343
+ return false;
344
+ }
310
345
 
311
346
  const sourceBlockHashFromCache = args.sourceCache.get(blockNumber);
312
347
  const sourceBlockHash = args.sourceCache.get(blockNumber) ?? (await this.getBlockHashFromSource(blockNumber));
313
348
  if (!sourceBlockHashFromCache && sourceBlockHash) {
314
349
  args.sourceCache.add({ number: blockNumber, hash: sourceBlockHash });
315
350
  }
351
+ if (!sourceBlockHash && blockNumber !== 0 && blockNumber <= args.sourceProposed) {
352
+ // No source data at or below the source's own proposed tip: the source contradicts itself (mid-reorg unwind or
353
+ // a transient read failure), so skip this pass.
354
+ this.log.warn(`Source has no data for a block at or below its proposed tip; skipping this sync pass`, {
355
+ blockNumber,
356
+ sourceProposed: args.sourceProposed,
357
+ });
358
+ throw new AbortError();
359
+ }
316
360
 
317
361
  this.log.trace(`Comparing block hashes for block ${blockNumber}`, { localBlockHash, sourceBlockHash });
318
362
  return localBlockHash === sourceBlockHash;
@@ -327,7 +371,13 @@ export class L2BlockStream {
327
371
 
328
372
  private async emitEvent(event: L2BlockStreamEvent) {
329
373
  this.log.debug(
330
- `Emitting ${event.type} (${event.type === 'blocks-added' ? event.blocks.length : event.type === 'chain-checkpointed' ? event.checkpoint.checkpoint.number : event.block.number})`,
374
+ `Emitting ${event.type} (${
375
+ event.type === 'blocks-added'
376
+ ? event.blocks.length
377
+ : event.type === 'chain-checkpointed'
378
+ ? event.checkpoint.number
379
+ : event.block.number
380
+ })`,
331
381
  );
332
382
  await this.handler.handleBlockStreamEvent(event);
333
383
  if (!this.isRunning() && !this.isSyncing) {
@@ -76,11 +76,30 @@ export abstract class L2TipsStoreBase implements L2BlockStreamEventHandler, L2Bl
76
76
  });
77
77
  }
78
78
 
79
+ /**
80
+ * Records `(number → hash)` witnesses into the block-hash index without moving any tip cursor. In tips-only mode
81
+ * the hash history is sparse (one anchor per tip-moving poll), so a consumer that materializes per-height state
82
+ * should witness those heights or its prunes are over-deep by the gap to the nearest anchor. Witnesses are compared
83
+ * against the source, not trusted, so they cannot cause under-deep prunes; this is always safe to call.
84
+ */
85
+ public async recordBlockHashes(blocks: L2BlockId[]): Promise<void> {
86
+ await this.runInTransaction(async () => {
87
+ for (const block of blocks) {
88
+ if (block.hash) {
89
+ await this.setBlockHash(block.number, block.hash);
90
+ }
91
+ }
92
+ });
93
+ }
94
+
79
95
  public async handleBlockStreamEvent(event: L2BlockStreamEvent): Promise<void> {
80
96
  switch (event.type) {
81
97
  case 'blocks-added':
82
98
  await this.handleBlocksAdded(event);
83
99
  break;
100
+ case 'chain-proposed':
101
+ await this.handleChainProposed(event);
102
+ break;
84
103
  case 'chain-checkpointed':
85
104
  await this.handleChainCheckpointed(event);
86
105
  break;
@@ -148,17 +167,22 @@ export abstract class L2TipsStoreBase implements L2BlockStreamEventHandler, L2Bl
148
167
  });
149
168
  }
150
169
 
170
+ private async handleChainProposed(event: L2BlockStreamEvent): Promise<void> {
171
+ if (event.type !== 'chain-proposed') {
172
+ return;
173
+ }
174
+ // Records the proposed tip into the block-hash index the walk-back reads. In tips-only mode this is the sole
175
+ // writer of proposed-tip history, leaving one sparse anchor per tip-moving pass for reorg detection.
176
+ await this.runInTransaction(() => this.saveTag('proposed', event.block));
177
+ }
178
+
151
179
  private async handleChainCheckpointed(event: L2BlockStreamEvent): Promise<void> {
152
180
  if (event.type !== 'chain-checkpointed') {
153
181
  return;
154
182
  }
155
183
  await this.runInTransaction(async () => {
156
- const checkpointId: CheckpointId = {
157
- number: event.checkpoint.checkpoint.number,
158
- hash: event.checkpoint.checkpoint.hash().toString(),
159
- };
160
184
  await this.saveTag('checkpointed', event.block);
161
- await this.setTipCheckpoint('checkpointed', checkpointId);
185
+ await this.setTipCheckpoint('checkpointed', event.checkpoint);
162
186
  });
163
187
  }
164
188
 
@@ -167,18 +191,15 @@ export abstract class L2TipsStoreBase implements L2BlockStreamEventHandler, L2Bl
167
191
  return;
168
192
  }
169
193
  await this.runInTransaction(async () => {
170
- // A prune is a rollback: the proposed tip moves to the prune target unconditionally, but
171
- // checkpoint-bearing cursors may only move backward. Forward-advancing them onto an
172
- // uncheckpointed block leaves them on a block with no recorded checkpoint id, which getCheckpointId
173
- // would then throw on.
194
+ // A prune is a rollback: the proposed tip moves to the prune target unconditionally, but checkpoint-bearing
195
+ // cursors may only move backward (advancing one onto an uncheckpointed block would leave it without a recorded
196
+ // checkpoint id, which getCheckpointId throws on).
174
197
  await this.saveTag('proposed', event.block);
175
198
 
176
- // Clamp each checkpoint-bearing cursor down to its OWN source tip when it leads it. Clamping the proven
177
- // cursor onto the checkpointed tip would transiently report unproven blocks as proven (the source's proven
178
- // tip can sit below its checkpointed tip after a proof-tx reorg), until the corrective chain-proven event
179
- // lands at the end of the same sync iteration. The event carries a valid (block, id) pair for each
180
- // boundary, so the clamped cursor always resolves to a recorded id. The source guarantees proven <=
181
- // checkpointed, so clamping each cursor to its own tip preserves the local proven <= checkpointed invariant.
199
+ // Clamp each checkpoint-bearing cursor down to its OWN source tip when it leads it; the event carries a valid
200
+ // (block, id) pair for each. Clamping the proven cursor onto the (possibly higher) checkpointed tip instead
201
+ // would transiently report unproven blocks as proven. The source guarantees proven <= checkpointed, so per-tip
202
+ // clamping preserves the local invariant.
182
203
  for (const { tag, sourceTip } of [
183
204
  { tag: 'checkpointed', sourceTip: event.checkpointed },
184
205
  { tag: 'proven', sourceTip: event.proven },