@aztec/world-state 0.0.1-commit.9b94fc1 → 0.0.1-commit.d3ec352c

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.
@@ -1,3 +1,4 @@
1
+ import { BlockNumber } from '@aztec/foundation/branded-types';
1
2
  import { Fr } from '@aztec/foundation/fields';
2
3
  import type { Tuple } from '@aztec/foundation/serialize';
3
4
  import { AppendOnlyTreeSnapshot, MerkleTreeId } from '@aztec/stdlib/trees';
@@ -56,11 +57,11 @@ interface WithTreeId {
56
57
 
57
58
  export interface WorldStateStatusSummary {
58
59
  /** Last block number that can still be unwound. */
59
- unfinalizedBlockNumber: bigint;
60
+ unfinalizedBlockNumber: BlockNumber;
60
61
  /** Last block number that is finalized and cannot be unwound. */
61
- finalizedBlockNumber: bigint;
62
+ finalizedBlockNumber: BlockNumber;
62
63
  /** Oldest block still available for historical queries and forks. */
63
- oldestHistoricalBlock: bigint;
64
+ oldestHistoricalBlock: BlockNumber;
64
65
  /** Whether the trees are in sync with each other */
65
66
  treesAreSynched: boolean;
66
67
  }
@@ -81,11 +82,11 @@ export interface TreeMeta {
81
82
  /** The tree's initial root value */
82
83
  initialRoot: Fr;
83
84
  /** The current oldest historical block number of the tree */
84
- oldestHistoricBlock: bigint;
85
+ oldestHistoricBlock: BlockNumber;
85
86
  /** The current unfinalized block number of the tree */
86
- unfinalizedBlockHeight: bigint;
87
+ unfinalizedBlockHeight: BlockNumber;
87
88
  /** The current finalized block number of the tree */
88
- finalizedBlockHeight: bigint;
89
+ finalizedBlockHeight: BlockNumber;
89
90
  }
90
91
 
91
92
  export interface DBStats {
@@ -173,9 +174,9 @@ export function buildEmptyTreeMeta() {
173
174
  depth: 0,
174
175
  size: 0n,
175
176
  committedSize: 0n,
176
- unfinalizedBlockHeight: 0n,
177
- finalizedBlockHeight: 0n,
178
- oldestHistoricBlock: 0n,
177
+ unfinalizedBlockHeight: BlockNumber.ZERO,
178
+ finalizedBlockHeight: BlockNumber.ZERO,
179
+ oldestHistoricBlock: BlockNumber.ZERO,
179
180
  root: Fr.ZERO,
180
181
  initialRoot: Fr.ZERO,
181
182
  initialSize: 0n,
@@ -204,9 +205,9 @@ export function buildEmptyWorldStateDBStats() {
204
205
 
205
206
  export function buildEmptyWorldStateSummary() {
206
207
  return {
207
- unfinalizedBlockNumber: 0n,
208
- finalizedBlockNumber: 0n,
209
- oldestHistoricalBlock: 0n,
208
+ unfinalizedBlockNumber: BlockNumber.ZERO,
209
+ finalizedBlockNumber: BlockNumber.ZERO,
210
+ oldestHistoricalBlock: BlockNumber.ZERO,
210
211
  treesAreSynched: true,
211
212
  } as WorldStateStatusSummary;
212
213
  }
@@ -220,9 +221,9 @@ export function buildEmptyWorldStateStatusFull() {
220
221
  }
221
222
 
222
223
  export function sanitizeSummary(summary: WorldStateStatusSummary) {
223
- summary.finalizedBlockNumber = BigInt(summary.finalizedBlockNumber);
224
- summary.unfinalizedBlockNumber = BigInt(summary.unfinalizedBlockNumber);
225
- summary.oldestHistoricalBlock = BigInt(summary.oldestHistoricalBlock);
224
+ summary.finalizedBlockNumber = BlockNumber.fromBigInt(BigInt(summary.finalizedBlockNumber));
225
+ summary.unfinalizedBlockNumber = BlockNumber.fromBigInt(BigInt(summary.unfinalizedBlockNumber));
226
+ summary.oldestHistoricalBlock = BlockNumber.fromBigInt(BigInt(summary.oldestHistoricalBlock));
226
227
  return summary;
227
228
  }
228
229
 
@@ -234,11 +235,11 @@ export function sanitizeDBStats(stats: DBStats) {
234
235
 
235
236
  export function sanitizeMeta(meta: TreeMeta) {
236
237
  meta.committedSize = BigInt(meta.committedSize);
237
- meta.finalizedBlockHeight = BigInt(meta.finalizedBlockHeight);
238
+ meta.finalizedBlockHeight = BlockNumber.fromBigInt(BigInt(meta.finalizedBlockHeight));
238
239
  meta.initialSize = BigInt(meta.initialSize);
239
- meta.oldestHistoricBlock = BigInt(meta.oldestHistoricBlock);
240
+ meta.oldestHistoricBlock = BlockNumber.fromBigInt(BigInt(meta.oldestHistoricBlock));
240
241
  meta.size = BigInt(meta.size);
241
- meta.unfinalizedBlockHeight = BigInt(meta.unfinalizedBlockHeight);
242
+ meta.unfinalizedBlockHeight = BlockNumber.fromBigInt(BigInt(meta.unfinalizedBlockHeight));
242
243
  return meta;
243
244
  }
244
245
 
@@ -310,7 +311,7 @@ interface WithLeafValues {
310
311
  }
311
312
 
312
313
  interface BlockShiftRequest extends WithCanonicalForkId {
313
- toBlockNumber: bigint;
314
+ toBlockNumber: BlockNumber;
314
315
  }
315
316
 
316
317
  interface WithLeaves {
@@ -409,7 +410,7 @@ interface UpdateArchiveRequest extends WithForkId {
409
410
  }
410
411
 
411
412
  interface SyncBlockRequest extends WithCanonicalForkId {
412
- blockNumber: number;
413
+ blockNumber: BlockNumber;
413
414
  blockStateRef: BlockStateReference;
414
415
  blockHeaderHash: Fr;
415
416
  paddedNoteHashes: readonly SerializedLeafValue[];
@@ -420,7 +421,7 @@ interface SyncBlockRequest extends WithCanonicalForkId {
420
421
 
421
422
  interface CreateForkRequest extends WithCanonicalForkId {
422
423
  latest: boolean;
423
- blockNumber: number;
424
+ blockNumber: BlockNumber;
424
425
  }
425
426
 
426
427
  interface CreateForkResponse {
@@ -1,10 +1,11 @@
1
1
  import { MAX_NOTE_HASHES_PER_TX, MAX_NULLIFIERS_PER_TX, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP } from '@aztec/constants';
2
+ import { BlockNumber } from '@aztec/foundation/branded-types';
2
3
  import { fromEntries, padArrayEnd } from '@aztec/foundation/collection';
3
4
  import { EthAddress } from '@aztec/foundation/eth-address';
4
5
  import { Fr } from '@aztec/foundation/fields';
5
6
  import { tryRmDir } from '@aztec/foundation/fs';
6
7
  import { type Logger, createLogger } from '@aztec/foundation/log';
7
- import type { L2Block, L2BlockNew } from '@aztec/stdlib/block';
8
+ import type { L2BlockNew } from '@aztec/stdlib/block';
8
9
  import { DatabaseVersionManager } from '@aztec/stdlib/database-version';
9
10
  import type {
10
11
  IndexedTreeId,
@@ -150,7 +151,7 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
150
151
  return new MerkleTreesFacade(this.instance, this.initialHeader!, WorldStateRevision.empty());
151
152
  }
152
153
 
153
- public getSnapshot(blockNumber: number): MerkleTreeReadOperations {
154
+ public getSnapshot(blockNumber: BlockNumber): MerkleTreeReadOperations {
154
155
  return new MerkleTreesFacade(
155
156
  this.instance,
156
157
  this.initialHeader!,
@@ -158,16 +159,20 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
158
159
  );
159
160
  }
160
161
 
161
- public async fork(blockNumber?: number): Promise<MerkleTreeWriteOperations> {
162
+ public async fork(blockNumber?: BlockNumber): Promise<MerkleTreeWriteOperations> {
162
163
  const resp = await this.instance.call(WorldStateMessageType.CREATE_FORK, {
163
164
  latest: blockNumber === undefined,
164
- blockNumber: blockNumber ?? 0,
165
+ blockNumber: blockNumber ?? BlockNumber.ZERO,
165
166
  canonical: true,
166
167
  });
167
168
  return new MerkleTreesForkFacade(
168
169
  this.instance,
169
170
  this.initialHeader!,
170
- new WorldStateRevision(/*forkId=*/ resp.forkId, /* blockNumber=*/ 0, /* includeUncommitted=*/ true),
171
+ new WorldStateRevision(
172
+ /*forkId=*/ resp.forkId,
173
+ /* blockNumber=*/ BlockNumber.ZERO,
174
+ /* includeUncommitted=*/ true,
175
+ ),
171
176
  );
172
177
  }
173
178
 
@@ -176,20 +181,26 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
176
181
  }
177
182
 
178
183
  public async handleL2BlockAndMessages(
179
- l2Block: L2Block | L2BlockNew,
184
+ l2Block: L2BlockNew,
180
185
  l1ToL2Messages: Fr[],
181
- // TODO(#17027)
182
- // Temporary hack to only insert l1 to l2 messages for the first block in a checkpoint.
183
- isFirstBlock = true,
186
+ isFirstBlock: boolean,
184
187
  ): Promise<WorldStateStatusFull> {
185
- // We have to pad both the values within tx effects because that's how the trees are built by circuits.
186
- const paddedNoteHashes = l2Block.body.txEffects.flatMap(txEffect =>
187
- padArrayEnd(txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX),
188
- );
188
+ if (!isFirstBlock && l1ToL2Messages.length > 0) {
189
+ throw new Error(
190
+ `L1 to L2 messages must be empty for non-first blocks, but got ${l1ToL2Messages.length} messages for block ${l2Block.number}.`,
191
+ );
192
+ }
193
+
194
+ // We have to pad the given l1 to l2 messages, and the note hashes and nullifiers within tx effects, because that's
195
+ // how the trees are built by circuits.
189
196
  const paddedL1ToL2Messages = isFirstBlock
190
197
  ? padArrayEnd<Fr, number>(l1ToL2Messages, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP)
191
198
  : [];
192
199
 
200
+ const paddedNoteHashes = l2Block.body.txEffects.flatMap(txEffect =>
201
+ padArrayEnd(txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX),
202
+ );
203
+
193
204
  const paddedNullifiers = l2Block.body.txEffects
194
205
  .flatMap(txEffect => padArrayEnd(txEffect.nullifiers, Fr.ZERO, MAX_NULLIFIERS_PER_TX))
195
206
  .map(nullifier => new NullifierLeaf(nullifier));
@@ -256,7 +267,7 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
256
267
  * @param toBlockNumber The block number that is now the tip of the finalized chain
257
268
  * @returns The new WorldStateStatus
258
269
  */
259
- public async setFinalized(toBlockNumber: bigint) {
270
+ public async setFinalized(toBlockNumber: BlockNumber) {
260
271
  try {
261
272
  await this.instance.call(
262
273
  WorldStateMessageType.FINALIZE_BLOCKS,
@@ -279,7 +290,7 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
279
290
  * @param toBlockNumber The block number of the new oldest historical block
280
291
  * @returns The new WorldStateStatus
281
292
  */
282
- public async removeHistoricalBlocks(toBlockNumber: bigint) {
293
+ public async removeHistoricalBlocks(toBlockNumber: BlockNumber) {
283
294
  try {
284
295
  return await this.instance.call(
285
296
  WorldStateMessageType.REMOVE_HISTORICAL_BLOCKS,
@@ -301,7 +312,7 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
301
312
  * @param toBlockNumber The block number of the new tip of the pending chain,
302
313
  * @returns The new WorldStateStatus
303
314
  */
304
- public async unwindBlocks(toBlockNumber: bigint) {
315
+ public async unwindBlocks(toBlockNumber: BlockNumber) {
305
316
  try {
306
317
  return await this.instance.call(
307
318
  WorldStateMessageType.UNWIND_BLOCKS,
@@ -1,13 +1,11 @@
1
- import { L1_TO_L2_MSG_SUBTREE_HEIGHT } from '@aztec/constants';
2
- import { SHA256Trunc } from '@aztec/foundation/crypto';
1
+ import { BlockNumber } from '@aztec/foundation/branded-types';
3
2
  import type { Fr } from '@aztec/foundation/fields';
4
3
  import { type Logger, createLogger } from '@aztec/foundation/log';
5
4
  import { promiseWithResolvers } from '@aztec/foundation/promise';
6
5
  import { elapsed } from '@aztec/foundation/timer';
7
- import { MerkleTreeCalculator } from '@aztec/foundation/trees';
8
6
  import type {
9
- L2Block,
10
7
  L2BlockId,
8
+ L2BlockNew,
11
9
  L2BlockSource,
12
10
  L2BlockStream,
13
11
  L2BlockStreamEvent,
@@ -32,6 +30,7 @@ import type { WorldStateStatusFull } from '../native/message.js';
32
30
  import type { MerkleTreeAdminDatabase } from '../world-state-db/merkle_tree_db.js';
33
31
  import type { WorldStateConfig } from './config.js';
34
32
  import { WorldStateSynchronizerError } from './errors.js';
33
+ import { findFirstBlocksInCheckpoints } from './utils.js';
35
34
 
36
35
  export type { SnapshotDataKeys };
37
36
 
@@ -45,17 +44,17 @@ export class ServerWorldStateSynchronizer
45
44
  {
46
45
  private readonly merkleTreeCommitted: MerkleTreeReadOperations;
47
46
 
48
- private latestBlockNumberAtStart = 0;
47
+ private latestBlockNumberAtStart = BlockNumber.ZERO;
49
48
  private historyToKeep: number | undefined;
50
49
  private currentState: WorldStateRunningState = WorldStateRunningState.IDLE;
51
- private latestBlockHashQuery: { blockNumber: number; hash: string | undefined } | undefined = undefined;
50
+ private latestBlockHashQuery: { blockNumber: BlockNumber; hash: string | undefined } | undefined = undefined;
52
51
 
53
52
  private syncPromise = promiseWithResolvers<void>();
54
53
  protected blockStream: L2BlockStream | undefined;
55
54
 
56
55
  // WorldState doesn't track the proven block number, it only tracks the latest tips of the pending chain and the finalized chain
57
56
  // store the proven block number here, in the synchronizer, so that we don't end up spamming the logs with 'chain-proved' events
58
- private provenBlockNumber: bigint | undefined;
57
+ private provenBlockNumber: BlockNumber | undefined;
59
58
 
60
59
  constructor(
61
60
  private readonly merkleTreeDb: MerkleTreeAdminDatabase,
@@ -77,11 +76,11 @@ export class ServerWorldStateSynchronizer
77
76
  return this.merkleTreeDb.getCommitted();
78
77
  }
79
78
 
80
- public getSnapshot(blockNumber: number): MerkleTreeReadOperations {
79
+ public getSnapshot(blockNumber: BlockNumber): MerkleTreeReadOperations {
81
80
  return this.merkleTreeDb.getSnapshot(blockNumber);
82
81
  }
83
82
 
84
- public fork(blockNumber?: number): Promise<MerkleTreeWriteOperations> {
83
+ public fork(blockNumber?: BlockNumber): Promise<MerkleTreeWriteOperations> {
85
84
  return this.merkleTreeDb.fork(blockNumber);
86
85
  }
87
86
 
@@ -102,9 +101,11 @@ export class ServerWorldStateSynchronizer
102
101
  }
103
102
 
104
103
  // Get the current latest block number
105
- this.latestBlockNumberAtStart = await (this.config.worldStateProvenBlocksOnly
106
- ? this.l2BlockSource.getProvenBlockNumber()
107
- : this.l2BlockSource.getBlockNumber());
104
+ this.latestBlockNumberAtStart = BlockNumber(
105
+ await (this.config.worldStateProvenBlocksOnly
106
+ ? this.l2BlockSource.getProvenBlockNumber()
107
+ : this.l2BlockSource.getBlockNumber()),
108
+ );
108
109
 
109
110
  const blockToDownloadFrom = (await this.getLatestBlockNumber()) + 1;
110
111
 
@@ -147,10 +148,10 @@ export class ServerWorldStateSynchronizer
147
148
  public async status(): Promise<WorldStateSynchronizerStatus> {
148
149
  const summary = await this.merkleTreeDb.getStatusSummary();
149
150
  const status: WorldStateSyncStatus = {
150
- latestBlockNumber: Number(summary.unfinalizedBlockNumber),
151
- latestBlockHash: (await this.getL2BlockHash(Number(summary.unfinalizedBlockNumber))) ?? '',
152
- finalizedBlockNumber: Number(summary.finalizedBlockNumber),
153
- oldestHistoricBlockNumber: Number(summary.oldestHistoricalBlock),
151
+ latestBlockNumber: summary.unfinalizedBlockNumber,
152
+ latestBlockHash: (await this.getL2BlockHash(summary.unfinalizedBlockNumber)) ?? '',
153
+ finalizedBlockNumber: summary.finalizedBlockNumber,
154
+ oldestHistoricBlockNumber: summary.oldestHistoricalBlock,
154
155
  treesAreSynched: summary.treesAreSynched,
155
156
  };
156
157
  return {
@@ -184,7 +185,10 @@ export class ServerWorldStateSynchronizer
184
185
  * @param skipThrowIfTargetNotReached - Whether to skip throwing if the target block number is not reached.
185
186
  * @returns A promise that resolves with the block number the world state was synced to
186
187
  */
187
- public async syncImmediate(targetBlockNumber?: number, skipThrowIfTargetNotReached?: boolean): Promise<number> {
188
+ public async syncImmediate(
189
+ targetBlockNumber?: BlockNumber,
190
+ skipThrowIfTargetNotReached?: boolean,
191
+ ): Promise<BlockNumber> {
188
192
  if (this.currentState !== WorldStateRunningState.RUNNING) {
189
193
  throw new Error(`World State is not running. Unable to perform sync.`);
190
194
  }
@@ -202,7 +206,7 @@ export class ServerWorldStateSynchronizer
202
206
 
203
207
  // If the archiver is behind the target block, force an archiver sync
204
208
  if (targetBlockNumber) {
205
- const archiverLatestBlock = await this.l2BlockSource.getBlockNumber();
209
+ const archiverLatestBlock = BlockNumber(await this.l2BlockSource.getBlockNumber());
206
210
  if (archiverLatestBlock < targetBlockNumber) {
207
211
  this.log.debug(`Archiver is at ${archiverLatestBlock} behind target block ${targetBlockNumber}.`);
208
212
  await this.l2BlockSource.syncImmediate();
@@ -232,8 +236,8 @@ export class ServerWorldStateSynchronizer
232
236
  }
233
237
 
234
238
  /** Returns the L2 block hash for a given number. Used by the L2BlockStream for detecting reorgs. */
235
- public async getL2BlockHash(number: number): Promise<string | undefined> {
236
- if (number === 0) {
239
+ public async getL2BlockHash(number: BlockNumber): Promise<string | undefined> {
240
+ if (number === BlockNumber.ZERO) {
237
241
  return (await this.merkleTreeCommitted.getInitialHeader().hash()).toString();
238
242
  }
239
243
  if (this.latestBlockHashQuery?.hash === undefined || number !== this.latestBlockHashQuery.blockNumber) {
@@ -250,13 +254,13 @@ export class ServerWorldStateSynchronizer
250
254
  /** Returns the latest L2 block number for each tip of the chain (latest, proven, finalized). */
251
255
  public async getL2Tips(): Promise<L2Tips> {
252
256
  const status = await this.merkleTreeDb.getStatusSummary();
253
- const unfinalizedBlockHash = await this.getL2BlockHash(Number(status.unfinalizedBlockNumber));
254
- const latestBlockId: L2BlockId = { number: Number(status.unfinalizedBlockNumber), hash: unfinalizedBlockHash! };
257
+ const unfinalizedBlockHash = await this.getL2BlockHash(status.unfinalizedBlockNumber);
258
+ const latestBlockId: L2BlockId = { number: status.unfinalizedBlockNumber, hash: unfinalizedBlockHash! };
255
259
 
256
260
  return {
257
261
  latest: latestBlockId,
258
- finalized: { number: Number(status.finalizedBlockNumber), hash: '' },
259
- proven: { number: Number(this.provenBlockNumber ?? status.finalizedBlockNumber), hash: '' }, // TODO(palla/reorg): Using finalized as proven for now
262
+ finalized: { number: status.finalizedBlockNumber, hash: '' },
263
+ proven: { number: this.provenBlockNumber ?? status.finalizedBlockNumber, hash: '' }, // TODO(palla/reorg): Using finalized as proven for now
260
264
  };
261
265
  }
262
266
 
@@ -264,16 +268,16 @@ export class ServerWorldStateSynchronizer
264
268
  public async handleBlockStreamEvent(event: L2BlockStreamEvent): Promise<void> {
265
269
  switch (event.type) {
266
270
  case 'blocks-added':
267
- await this.handleL2Blocks(event.blocks.map(b => b.block));
271
+ await this.handleL2Blocks(event.blocks.map(b => b.block.toL2Block()));
268
272
  break;
269
273
  case 'chain-pruned':
270
- await this.handleChainPruned(event.block.number);
274
+ await this.handleChainPruned(BlockNumber(event.block.number));
271
275
  break;
272
276
  case 'chain-proven':
273
- await this.handleChainProven(event.block.number);
277
+ await this.handleChainProven(BlockNumber(event.block.number));
274
278
  break;
275
279
  case 'chain-finalized':
276
- await this.handleChainFinalized(event.block.number);
280
+ await this.handleChainFinalized(BlockNumber(event.block.number));
277
281
  break;
278
282
  }
279
283
  }
@@ -283,22 +287,23 @@ export class ServerWorldStateSynchronizer
283
287
  * @param l2Blocks - The L2 blocks to handle.
284
288
  * @returns Whether the block handled was produced by this same node.
285
289
  */
286
- private async handleL2Blocks(l2Blocks: L2Block[]) {
290
+ private async handleL2Blocks(l2Blocks: L2BlockNew[]) {
287
291
  this.log.trace(`Handling L2 blocks ${l2Blocks[0].number} to ${l2Blocks.at(-1)!.number}`);
288
292
 
289
- const messagePromises = l2Blocks.map(block => this.l2BlockSource.getL1ToL2Messages(block.number));
290
- const l1ToL2Messages: Fr[][] = await Promise.all(messagePromises);
291
- let updateStatus: WorldStateStatusFull | undefined = undefined;
293
+ const firstBlocksInCheckpoints = await findFirstBlocksInCheckpoints(l2Blocks, this.l2BlockSource);
292
294
 
293
- for (let i = 0; i < l2Blocks.length; i++) {
294
- const [duration, result] = await elapsed(() => this.handleL2Block(l2Blocks[i], l1ToL2Messages[i]));
295
- this.log.info(`World state updated with L2 block ${l2Blocks[i].number}`, {
295
+ let updateStatus: WorldStateStatusFull | undefined = undefined;
296
+ for (const block of l2Blocks) {
297
+ const l1ToL2Messages = firstBlocksInCheckpoints.get(block.number) ?? [];
298
+ const isFirstBlock = firstBlocksInCheckpoints.has(block.number);
299
+ const [duration, result] = await elapsed(() => this.handleL2Block(block, l1ToL2Messages, isFirstBlock));
300
+ this.log.info(`World state updated with L2 block ${block.number}`, {
296
301
  eventName: 'l2-block-handled',
297
302
  duration,
298
- unfinalizedBlockNumber: result.summary.unfinalizedBlockNumber,
299
- finalizedBlockNumber: result.summary.finalizedBlockNumber,
300
- oldestHistoricBlock: result.summary.oldestHistoricalBlock,
301
- ...l2Blocks[i].getStats(),
303
+ unfinalizedBlockNumber: BigInt(result.summary.unfinalizedBlockNumber),
304
+ finalizedBlockNumber: BigInt(result.summary.finalizedBlockNumber),
305
+ oldestHistoricBlock: BigInt(result.summary.oldestHistoricalBlock),
306
+ ...block.getStats(),
302
307
  } satisfies L2BlockHandledStats);
303
308
  updateStatus = result;
304
309
  }
@@ -314,20 +319,18 @@ export class ServerWorldStateSynchronizer
314
319
  * @param l1ToL2Messages - The L1 to L2 messages for the block.
315
320
  * @returns Whether the block handled was produced by this same node.
316
321
  */
317
- private async handleL2Block(l2Block: L2Block, l1ToL2Messages: Fr[]): Promise<WorldStateStatusFull> {
318
- // First we check that the L1 to L2 messages hash to the block inHash.
319
- // Note that we cannot optimize this check by checking the root of the subtree after inserting the messages
320
- // to the real L1_TO_L2_MESSAGE_TREE (like we do in merkleTreeDb.handleL2BlockAndMessages(...)) because that
321
- // tree uses pedersen and we don't have access to the converted root.
322
- await this.verifyMessagesHashToInHash(l1ToL2Messages, l2Block.header.contentCommitment.inHash);
323
-
322
+ private async handleL2Block(
323
+ l2Block: L2BlockNew,
324
+ l1ToL2Messages: Fr[],
325
+ isFirstBlock: boolean,
326
+ ): Promise<WorldStateStatusFull> {
324
327
  // If the above check succeeds, we can proceed to handle the block.
325
328
  this.log.trace(`Pushing L2 block ${l2Block.number} to merkle tree db `, {
326
329
  blockNumber: l2Block.number,
327
330
  blockHash: await l2Block.hash().then(h => h.toString()),
328
331
  l1ToL2Messages: l1ToL2Messages.map(msg => msg.toString()),
329
332
  });
330
- const result = await this.merkleTreeDb.handleL2BlockAndMessages(l2Block, l1ToL2Messages);
333
+ const result = await this.merkleTreeDb.handleL2BlockAndMessages(l2Block, l1ToL2Messages, isFirstBlock);
331
334
 
332
335
  if (this.currentState === WorldStateRunningState.SYNCHING && l2Block.number >= this.latestBlockNumberAtStart) {
333
336
  this.setCurrentState(WorldStateRunningState.RUNNING);
@@ -337,14 +340,14 @@ export class ServerWorldStateSynchronizer
337
340
  return result;
338
341
  }
339
342
 
340
- private async handleChainFinalized(blockNumber: number) {
343
+ private async handleChainFinalized(blockNumber: BlockNumber) {
341
344
  this.log.verbose(`Finalized chain is now at block ${blockNumber}`);
342
- const summary = await this.merkleTreeDb.setFinalized(BigInt(blockNumber));
345
+ const summary = await this.merkleTreeDb.setFinalized(blockNumber);
343
346
  if (this.historyToKeep === undefined) {
344
347
  return;
345
348
  }
346
- const newHistoricBlock = summary.finalizedBlockNumber - BigInt(this.historyToKeep) + 1n;
347
- if (newHistoricBlock <= 1) {
349
+ const newHistoricBlock = BlockNumber(summary.finalizedBlockNumber - this.historyToKeep + 1);
350
+ if (newHistoricBlock <= BlockNumber(1)) {
348
351
  return;
349
352
  }
350
353
  this.log.verbose(`Pruning historic blocks to ${newHistoricBlock}`);
@@ -352,15 +355,15 @@ export class ServerWorldStateSynchronizer
352
355
  this.log.debug(`World state summary `, status.summary);
353
356
  }
354
357
 
355
- private handleChainProven(blockNumber: number) {
356
- this.provenBlockNumber = BigInt(blockNumber);
358
+ private handleChainProven(blockNumber: BlockNumber) {
359
+ this.provenBlockNumber = blockNumber;
357
360
  this.log.debug(`Proven chain is now at block ${blockNumber}`);
358
361
  return Promise.resolve();
359
362
  }
360
363
 
361
- private async handleChainPruned(blockNumber: number) {
364
+ private async handleChainPruned(blockNumber: BlockNumber) {
362
365
  this.log.warn(`Chain pruned to block ${blockNumber}`);
363
- const status = await this.merkleTreeDb.unwindBlocks(BigInt(blockNumber));
366
+ const status = await this.merkleTreeDb.unwindBlocks(blockNumber);
364
367
  this.latestBlockHashQuery = undefined;
365
368
  this.provenBlockNumber = undefined;
366
369
  this.instrumentation.updateWorldStateMetrics(status);
@@ -374,24 +377,4 @@ export class ServerWorldStateSynchronizer
374
377
  this.currentState = newState;
375
378
  this.log.debug(`Moved to state ${WorldStateRunningState[this.currentState]}`);
376
379
  }
377
-
378
- /**
379
- * Verifies that the L1 to L2 messages hash to the block inHash.
380
- * @param l1ToL2Messages - The L1 to L2 messages for the block.
381
- * @param inHash - The inHash of the block.
382
- * @throws If the L1 to L2 messages do not hash to the block inHash.
383
- */
384
- protected async verifyMessagesHashToInHash(l1ToL2Messages: Fr[], inHash: Fr) {
385
- const treeCalculator = await MerkleTreeCalculator.create(
386
- L1_TO_L2_MSG_SUBTREE_HEIGHT,
387
- Buffer.alloc(32),
388
- (lhs, rhs) => Promise.resolve(new SHA256Trunc().hash(lhs, rhs)),
389
- );
390
-
391
- const root = await treeCalculator.computeTreeRoot(l1ToL2Messages.map(msg => msg.toBuffer()));
392
-
393
- if (!root.equals(inHash.toBuffer())) {
394
- throw new Error('Obtained L1 to L2 messages failed to be hashed to the block inHash');
395
- }
396
- }
397
380
  }
@@ -0,0 +1,83 @@
1
+ import { BlockNumber, type SlotNumber } from '@aztec/foundation/branded-types';
2
+ import type { Fr } from '@aztec/foundation/fields';
3
+ import type { L2BlockNew, L2BlockSource } from '@aztec/stdlib/block';
4
+ import type { Checkpoint } from '@aztec/stdlib/checkpoint';
5
+ import { type L1ToL2MessageSource, computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
6
+
7
+ /**
8
+ * Determine which blocks in the given array are the first block in a checkpoint.
9
+ * @param blocks - The candidate blocks, sorted by block number in ascending order.
10
+ * @param l2BlockSource - The L2 block source to use to fetch the checkpoints, block headers and L1->L2 messages.
11
+ * @returns A map of block numbers that begin a checkpoint to the L1->L2 messages for that checkpoint.
12
+ */
13
+ export async function findFirstBlocksInCheckpoints(
14
+ blocks: L2BlockNew[],
15
+ l2BlockSource: L2BlockSource & L1ToL2MessageSource,
16
+ ): Promise<Map<number, Fr[]>> {
17
+ // Select the blocks that are the final block within each group of identical slot numbers.
18
+ let seenSlot: SlotNumber | undefined;
19
+ const maybeLastBlocks = [...blocks]
20
+ .reverse()
21
+ .filter(b => {
22
+ if (b.header.globalVariables.slotNumber !== seenSlot) {
23
+ seenSlot = b.header.globalVariables.slotNumber;
24
+ return true;
25
+ }
26
+ return false;
27
+ })
28
+ .reverse();
29
+
30
+ // Try to fetch the checkpoints for those blocks. If undefined (which should only occur for blocks.at(-1)),
31
+ // then the block is not the last one in a checkpoint.
32
+ // If we are not checking the inHashes below, only blocks.at(-1) would need its checkpoint header fetched.
33
+ const checkpointedBlocks = (
34
+ await Promise.all(
35
+ maybeLastBlocks.map(async b => ({
36
+ blockNumber: b.number,
37
+ // A checkpoint's archive root is the archive root of its last block.
38
+ checkpoint: await l2BlockSource.getCheckpointByArchive(b.archive.root),
39
+ })),
40
+ )
41
+ ).filter(b => b.checkpoint !== undefined) as { blockNumber: BlockNumber; checkpoint: Checkpoint }[];
42
+
43
+ // Verify that the L1->L2 messages hash to the checkpoint's inHash.
44
+ const checkpointedL1ToL2Messages: Fr[][] = await Promise.all(
45
+ checkpointedBlocks.map(b => l2BlockSource.getL1ToL2MessagesForCheckpoint(b.checkpoint!.number)),
46
+ );
47
+ checkpointedBlocks.forEach((b, i) => {
48
+ const computedInHash = computeInHashFromL1ToL2Messages(checkpointedL1ToL2Messages[i]);
49
+ const inHash = b.checkpoint.header.contentCommitment.inHash;
50
+ if (!computedInHash.equals(inHash)) {
51
+ throw new Error('Obtained L1 to L2 messages failed to be hashed to the checkpoint inHash');
52
+ }
53
+ });
54
+
55
+ // Compute the first block numbers, which should be right after each checkpointed block. Exclude blocks that haven't
56
+ // been added yet.
57
+ const firstBlockNumbers = checkpointedBlocks
58
+ .map(b => BlockNumber(b.blockNumber + 1))
59
+ .filter(n => n <= blocks.at(-1)!.number);
60
+ // Check if blocks[0] is the first block in a checkpoint.
61
+ if (blocks[0].number === 1) {
62
+ firstBlockNumbers.push(blocks[0].number);
63
+ } else {
64
+ const lastBlockHeader = await l2BlockSource.getBlockHeader(BlockNumber(blocks[0].number - 1));
65
+ if (!lastBlockHeader) {
66
+ throw new Error(`Failed to get block ${blocks[0].number - 1}`);
67
+ }
68
+ if (lastBlockHeader.globalVariables.slotNumber !== blocks[0].header.globalVariables.slotNumber) {
69
+ firstBlockNumbers.push(blocks[0].number);
70
+ }
71
+ }
72
+
73
+ // Fetch the L1->L2 messages for the first blocks and assign them to the map.
74
+ const messagesByBlockNumber = new Map<BlockNumber, Fr[]>();
75
+ await Promise.all(
76
+ firstBlockNumbers.map(async blockNumber => {
77
+ const l1ToL2Messages = await l2BlockSource.getL1ToL2Messages(blockNumber);
78
+ messagesByBlockNumber.set(blockNumber, l1ToL2Messages);
79
+ }),
80
+ );
81
+
82
+ return messagesByBlockNumber;
83
+ }