@aztec/world-state 0.0.1-commit.d3ec352c → 0.0.1-commit.e310a4c8

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 (48) hide show
  1. package/dest/instrumentation/instrumentation.d.ts +1 -1
  2. package/dest/instrumentation/instrumentation.d.ts.map +1 -1
  3. package/dest/instrumentation/instrumentation.js +17 -41
  4. package/dest/native/merkle_trees_facade.d.ts +8 -3
  5. package/dest/native/merkle_trees_facade.d.ts.map +1 -1
  6. package/dest/native/merkle_trees_facade.js +35 -6
  7. package/dest/native/message.d.ts +4 -3
  8. package/dest/native/message.d.ts.map +1 -1
  9. package/dest/native/message.js +1 -1
  10. package/dest/native/native_world_state.d.ts +11 -9
  11. package/dest/native/native_world_state.d.ts.map +1 -1
  12. package/dest/native/native_world_state.js +14 -12
  13. package/dest/native/native_world_state_instance.d.ts +3 -3
  14. package/dest/native/native_world_state_instance.d.ts.map +1 -1
  15. package/dest/native/native_world_state_instance.js +3 -3
  16. package/dest/synchronizer/config.d.ts +1 -3
  17. package/dest/synchronizer/config.d.ts.map +1 -1
  18. package/dest/synchronizer/config.js +1 -6
  19. package/dest/synchronizer/factory.d.ts +4 -3
  20. package/dest/synchronizer/factory.d.ts.map +1 -1
  21. package/dest/synchronizer/factory.js +5 -5
  22. package/dest/synchronizer/server_world_state_synchronizer.d.ts +5 -4
  23. package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -1
  24. package/dest/synchronizer/server_world_state_synchronizer.js +72 -43
  25. package/dest/test/utils.d.ts +11 -17
  26. package/dest/test/utils.d.ts.map +1 -1
  27. package/dest/test/utils.js +44 -81
  28. package/dest/testing.d.ts +2 -2
  29. package/dest/testing.d.ts.map +1 -1
  30. package/dest/testing.js +1 -1
  31. package/dest/world-state-db/merkle_tree_db.d.ts +7 -9
  32. package/dest/world-state-db/merkle_tree_db.d.ts.map +1 -1
  33. package/package.json +11 -11
  34. package/src/instrumentation/instrumentation.ts +17 -41
  35. package/src/native/merkle_trees_facade.ts +36 -3
  36. package/src/native/message.ts +3 -2
  37. package/src/native/native_world_state.ts +29 -16
  38. package/src/native/native_world_state_instance.ts +5 -3
  39. package/src/synchronizer/config.ts +1 -14
  40. package/src/synchronizer/factory.ts +7 -1
  41. package/src/synchronizer/server_world_state_synchronizer.ts +76 -59
  42. package/src/test/utils.ts +70 -128
  43. package/src/testing.ts +1 -1
  44. package/src/world-state-db/merkle_tree_db.ts +10 -12
  45. package/dest/synchronizer/utils.d.ts +0 -11
  46. package/dest/synchronizer/utils.d.ts.map +0 -1
  47. package/dest/synchronizer/utils.js +0 -59
  48. package/src/synchronizer/utils.ts +0 -83
@@ -1,17 +1,19 @@
1
+ import { GENESIS_BLOCK_HEADER_HASH, INITIAL_L2_BLOCK_NUM, INITIAL_L2_CHECKPOINT_NUM } from '@aztec/constants';
1
2
  import { BlockNumber } from '@aztec/foundation/branded-types';
2
- import type { Fr } from '@aztec/foundation/fields';
3
+ import type { Fr } from '@aztec/foundation/curves/bn254';
3
4
  import { type Logger, createLogger } from '@aztec/foundation/log';
4
5
  import { promiseWithResolvers } from '@aztec/foundation/promise';
5
6
  import { elapsed } from '@aztec/foundation/timer';
6
- import type {
7
- L2BlockId,
8
- L2BlockNew,
9
- L2BlockSource,
7
+ import {
8
+ GENESIS_CHECKPOINT_HEADER_HASH,
9
+ type L2Block,
10
+ type L2BlockId,
11
+ type L2BlockSource,
10
12
  L2BlockStream,
11
- L2BlockStreamEvent,
12
- L2BlockStreamEventHandler,
13
- L2BlockStreamLocalDataProvider,
14
- L2Tips,
13
+ type L2BlockStreamEvent,
14
+ type L2BlockStreamEventHandler,
15
+ type L2BlockStreamLocalDataProvider,
16
+ type L2Tips,
15
17
  } from '@aztec/stdlib/block';
16
18
  import {
17
19
  WorldStateRunningState,
@@ -23,14 +25,13 @@ import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
23
25
  import type { SnapshotDataKeys } from '@aztec/stdlib/snapshots';
24
26
  import type { L2BlockHandledStats } from '@aztec/stdlib/stats';
25
27
  import { MerkleTreeId, type MerkleTreeReadOperations, type MerkleTreeWriteOperations } from '@aztec/stdlib/trees';
26
- import { TraceableL2BlockStream, getTelemetryClient } from '@aztec/telemetry-client';
28
+ import { getTelemetryClient } from '@aztec/telemetry-client';
27
29
 
28
30
  import { WorldStateInstrumentation } from '../instrumentation/instrumentation.js';
29
31
  import type { WorldStateStatusFull } from '../native/message.js';
30
32
  import type { MerkleTreeAdminDatabase } from '../world-state-db/merkle_tree_db.js';
31
33
  import type { WorldStateConfig } from './config.js';
32
34
  import { WorldStateSynchronizerError } from './errors.js';
33
- import { findFirstBlocksInCheckpoints } from './utils.js';
34
35
 
35
36
  export type { SnapshotDataKeys };
36
37
 
@@ -47,7 +48,6 @@ export class ServerWorldStateSynchronizer
47
48
  private latestBlockNumberAtStart = BlockNumber.ZERO;
48
49
  private historyToKeep: number | undefined;
49
50
  private currentState: WorldStateRunningState = WorldStateRunningState.IDLE;
50
- private latestBlockHashQuery: { blockNumber: BlockNumber; hash: string | undefined } | undefined = undefined;
51
51
 
52
52
  private syncPromise = promiseWithResolvers<void>();
53
53
  protected blockStream: L2BlockStream | undefined;
@@ -80,8 +80,8 @@ export class ServerWorldStateSynchronizer
80
80
  return this.merkleTreeDb.getSnapshot(blockNumber);
81
81
  }
82
82
 
83
- public fork(blockNumber?: BlockNumber): Promise<MerkleTreeWriteOperations> {
84
- return this.merkleTreeDb.fork(blockNumber);
83
+ public fork(blockNumber?: BlockNumber, opts?: { closeDelayMs?: number }): Promise<MerkleTreeWriteOperations> {
84
+ return this.merkleTreeDb.fork(blockNumber, opts);
85
85
  }
86
86
 
87
87
  public backupTo(dstPath: string, compact?: boolean): Promise<Record<Exclude<SnapshotDataKeys, 'archiver'>, string>> {
@@ -101,11 +101,7 @@ export class ServerWorldStateSynchronizer
101
101
  }
102
102
 
103
103
  // Get the current latest block number
104
- this.latestBlockNumberAtStart = BlockNumber(
105
- await (this.config.worldStateProvenBlocksOnly
106
- ? this.l2BlockSource.getProvenBlockNumber()
107
- : this.l2BlockSource.getBlockNumber()),
108
- );
104
+ this.latestBlockNumberAtStart = BlockNumber(await this.l2BlockSource.getBlockNumber());
109
105
 
110
106
  const blockToDownloadFrom = (await this.getLatestBlockNumber()) + 1;
111
107
 
@@ -127,12 +123,11 @@ export class ServerWorldStateSynchronizer
127
123
  }
128
124
 
129
125
  protected createBlockStream(): L2BlockStream {
130
- const tracer = this.instrumentation.telemetry.getTracer('WorldStateL2BlockStream');
131
126
  const logger = createLogger('world-state:block_stream');
132
- return new TraceableL2BlockStream(this.l2BlockSource, this, this, tracer, 'WorldStateL2BlockStream', logger, {
133
- proven: this.config.worldStateProvenBlocksOnly,
127
+ return new L2BlockStream(this.l2BlockSource, this, this, logger, {
134
128
  pollIntervalMS: this.config.worldStateBlockCheckIntervalMS,
135
129
  batchSize: this.config.worldStateBlockRequestBatchSize,
130
+ ignoreCheckpoints: true,
136
131
  });
137
132
  }
138
133
 
@@ -161,7 +156,7 @@ export class ServerWorldStateSynchronizer
161
156
  }
162
157
 
163
158
  public async getLatestBlockNumber() {
164
- return (await this.getL2Tips()).latest.number;
159
+ return (await this.getL2Tips()).proposed.number;
165
160
  }
166
161
 
167
162
  public async stopSync() {
@@ -240,27 +235,44 @@ export class ServerWorldStateSynchronizer
240
235
  if (number === BlockNumber.ZERO) {
241
236
  return (await this.merkleTreeCommitted.getInitialHeader().hash()).toString();
242
237
  }
243
- if (this.latestBlockHashQuery?.hash === undefined || number !== this.latestBlockHashQuery.blockNumber) {
244
- this.latestBlockHashQuery = {
245
- hash: await this.merkleTreeCommitted
246
- .getLeafValue(MerkleTreeId.ARCHIVE, BigInt(number))
247
- .then(leaf => leaf?.toString()),
248
- blockNumber: number,
249
- };
250
- }
251
- return this.latestBlockHashQuery.hash;
238
+ return this.merkleTreeCommitted.getLeafValue(MerkleTreeId.ARCHIVE, BigInt(number)).then(leaf => leaf?.toString());
252
239
  }
253
240
 
254
241
  /** Returns the latest L2 block number for each tip of the chain (latest, proven, finalized). */
255
242
  public async getL2Tips(): Promise<L2Tips> {
256
243
  const status = await this.merkleTreeDb.getStatusSummary();
257
- const unfinalizedBlockHash = await this.getL2BlockHash(status.unfinalizedBlockNumber);
244
+ const unfinalizedBlockHashPromise = this.getL2BlockHash(status.unfinalizedBlockNumber);
245
+ const finalizedBlockHashPromise = this.getL2BlockHash(status.finalizedBlockNumber);
246
+
247
+ const provenBlockNumber = this.provenBlockNumber ?? status.finalizedBlockNumber;
248
+ const provenBlockHashPromise =
249
+ this.provenBlockNumber === undefined ? finalizedBlockHashPromise : this.getL2BlockHash(this.provenBlockNumber);
250
+
251
+ const [unfinalizedBlockHash, finalizedBlockHash, provenBlockHash] = await Promise.all([
252
+ unfinalizedBlockHashPromise,
253
+ finalizedBlockHashPromise,
254
+ provenBlockHashPromise,
255
+ ]);
258
256
  const latestBlockId: L2BlockId = { number: status.unfinalizedBlockNumber, hash: unfinalizedBlockHash! };
259
257
 
258
+ // World state doesn't track checkpointed blocks or checkpoints themselves.
259
+ // but we use a block stream so we need to provide 'local' L2Tips.
260
+ // We configure the block stream to ignore checkpoints and set checkpoint values to genesis here.
261
+ const genesisCheckpointHeaderHash = GENESIS_CHECKPOINT_HEADER_HASH.toString();
260
262
  return {
261
- latest: latestBlockId,
262
- finalized: { number: status.finalizedBlockNumber, hash: '' },
263
- proven: { number: this.provenBlockNumber ?? status.finalizedBlockNumber, hash: '' }, // TODO(palla/reorg): Using finalized as proven for now
263
+ proposed: latestBlockId,
264
+ checkpointed: {
265
+ block: { number: INITIAL_L2_BLOCK_NUM, hash: GENESIS_BLOCK_HEADER_HASH.toString() },
266
+ checkpoint: { number: INITIAL_L2_CHECKPOINT_NUM, hash: genesisCheckpointHeaderHash },
267
+ },
268
+ finalized: {
269
+ block: { number: status.finalizedBlockNumber, hash: finalizedBlockHash ?? '' },
270
+ checkpoint: { number: INITIAL_L2_CHECKPOINT_NUM, hash: genesisCheckpointHeaderHash },
271
+ },
272
+ proven: {
273
+ block: { number: provenBlockNumber, hash: provenBlockHash ?? '' },
274
+ checkpoint: { number: INITIAL_L2_CHECKPOINT_NUM, hash: genesisCheckpointHeaderHash },
275
+ },
264
276
  };
265
277
  }
266
278
 
@@ -268,16 +280,16 @@ export class ServerWorldStateSynchronizer
268
280
  public async handleBlockStreamEvent(event: L2BlockStreamEvent): Promise<void> {
269
281
  switch (event.type) {
270
282
  case 'blocks-added':
271
- await this.handleL2Blocks(event.blocks.map(b => b.block.toL2Block()));
283
+ await this.handleL2Blocks(event.blocks);
272
284
  break;
273
285
  case 'chain-pruned':
274
- await this.handleChainPruned(BlockNumber(event.block.number));
286
+ await this.handleChainPruned(event.block.number);
275
287
  break;
276
288
  case 'chain-proven':
277
- await this.handleChainProven(BlockNumber(event.block.number));
289
+ await this.handleChainProven(event.block.number);
278
290
  break;
279
291
  case 'chain-finalized':
280
- await this.handleChainFinalized(BlockNumber(event.block.number));
292
+ await this.handleChainFinalized(event.block.number);
281
293
  break;
282
294
  }
283
295
  }
@@ -287,16 +299,25 @@ export class ServerWorldStateSynchronizer
287
299
  * @param l2Blocks - The L2 blocks to handle.
288
300
  * @returns Whether the block handled was produced by this same node.
289
301
  */
290
- private async handleL2Blocks(l2Blocks: L2BlockNew[]) {
291
- this.log.trace(`Handling L2 blocks ${l2Blocks[0].number} to ${l2Blocks.at(-1)!.number}`);
292
-
293
- const firstBlocksInCheckpoints = await findFirstBlocksInCheckpoints(l2Blocks, this.l2BlockSource);
302
+ private async handleL2Blocks(l2Blocks: L2Block[]) {
303
+ this.log.debug(`Handling L2 blocks ${l2Blocks[0].number} to ${l2Blocks.at(-1)!.number}`);
304
+
305
+ // Fetch the L1->L2 messages for the first block in a checkpoint.
306
+ const messagesForBlocks = new Map<BlockNumber, Fr[]>();
307
+ await Promise.all(
308
+ l2Blocks
309
+ .filter(b => b.indexWithinCheckpoint === 0)
310
+ .map(async block => {
311
+ const l1ToL2Messages = await this.l2BlockSource.getL1ToL2Messages(block.checkpointNumber);
312
+ messagesForBlocks.set(block.number, l1ToL2Messages);
313
+ }),
314
+ );
294
315
 
295
316
  let updateStatus: WorldStateStatusFull | undefined = undefined;
296
317
  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));
318
+ const [duration, result] = await elapsed(() =>
319
+ this.handleL2Block(block, messagesForBlocks.get(block.number) ?? []),
320
+ );
300
321
  this.log.info(`World state updated with L2 block ${block.number}`, {
301
322
  eventName: 'l2-block-handled',
302
323
  duration,
@@ -319,18 +340,15 @@ export class ServerWorldStateSynchronizer
319
340
  * @param l1ToL2Messages - The L1 to L2 messages for the block.
320
341
  * @returns Whether the block handled was produced by this same node.
321
342
  */
322
- private async handleL2Block(
323
- l2Block: L2BlockNew,
324
- l1ToL2Messages: Fr[],
325
- isFirstBlock: boolean,
326
- ): Promise<WorldStateStatusFull> {
327
- // If the above check succeeds, we can proceed to handle the block.
328
- this.log.trace(`Pushing L2 block ${l2Block.number} to merkle tree db `, {
343
+ private async handleL2Block(l2Block: L2Block, l1ToL2Messages: Fr[]): Promise<WorldStateStatusFull> {
344
+ this.log.debug(`Pushing L2 block ${l2Block.number} to merkle tree db `, {
329
345
  blockNumber: l2Block.number,
330
346
  blockHash: await l2Block.hash().then(h => h.toString()),
331
347
  l1ToL2Messages: l1ToL2Messages.map(msg => msg.toString()),
348
+ blockHeader: l2Block.header.toInspect(),
349
+ blockStats: l2Block.getStats(),
332
350
  });
333
- const result = await this.merkleTreeDb.handleL2BlockAndMessages(l2Block, l1ToL2Messages, isFirstBlock);
351
+ const result = await this.merkleTreeDb.handleL2BlockAndMessages(l2Block, l1ToL2Messages);
334
352
 
335
353
  if (this.currentState === WorldStateRunningState.SYNCHING && l2Block.number >= this.latestBlockNumberAtStart) {
336
354
  this.setCurrentState(WorldStateRunningState.RUNNING);
@@ -346,12 +364,12 @@ export class ServerWorldStateSynchronizer
346
364
  if (this.historyToKeep === undefined) {
347
365
  return;
348
366
  }
349
- const newHistoricBlock = BlockNumber(summary.finalizedBlockNumber - this.historyToKeep + 1);
350
- if (newHistoricBlock <= BlockNumber(1)) {
367
+ const newHistoricBlock = summary.finalizedBlockNumber - this.historyToKeep + 1;
368
+ if (newHistoricBlock <= 1) {
351
369
  return;
352
370
  }
353
371
  this.log.verbose(`Pruning historic blocks to ${newHistoricBlock}`);
354
- const status = await this.merkleTreeDb.removeHistoricalBlocks(newHistoricBlock);
372
+ const status = await this.merkleTreeDb.removeHistoricalBlocks(BlockNumber(newHistoricBlock));
355
373
  this.log.debug(`World state summary `, status.summary);
356
374
  }
357
375
 
@@ -364,7 +382,6 @@ export class ServerWorldStateSynchronizer
364
382
  private async handleChainPruned(blockNumber: BlockNumber) {
365
383
  this.log.warn(`Chain pruned to block ${blockNumber}`);
366
384
  const status = await this.merkleTreeDb.unwindBlocks(blockNumber);
367
- this.latestBlockHashQuery = undefined;
368
385
  this.provenBlockNumber = undefined;
369
386
  this.instrumentation.updateWorldStateMetrics(status);
370
387
  }
package/src/test/utils.ts CHANGED
@@ -4,129 +4,100 @@ import {
4
4
  NULLIFIER_SUBTREE_HEIGHT,
5
5
  NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP,
6
6
  } from '@aztec/constants';
7
- import { BlockNumber, type CheckpointNumber, SlotNumber } from '@aztec/foundation/branded-types';
7
+ import { asyncMap } from '@aztec/foundation/async-map';
8
+ import { BlockNumber, type CheckpointNumber, IndexWithinCheckpoint } from '@aztec/foundation/branded-types';
8
9
  import { padArrayEnd } from '@aztec/foundation/collection';
9
- import { Fr } from '@aztec/foundation/fields';
10
- import { L2BlockNew } from '@aztec/stdlib/block';
11
- import { Checkpoint } from '@aztec/stdlib/checkpoint';
10
+ import { Fr } from '@aztec/foundation/curves/bn254';
11
+ import { L2Block } from '@aztec/stdlib/block';
12
12
  import type {
13
13
  IndexedTreeId,
14
14
  MerkleTreeReadOperations,
15
15
  MerkleTreeWriteOperations,
16
16
  } from '@aztec/stdlib/interfaces/server';
17
- import { computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
17
+ import { mockCheckpointAndMessages, mockL1ToL2Messages } from '@aztec/stdlib/testing';
18
18
  import { AppendOnlyTreeSnapshot, MerkleTreeId } from '@aztec/stdlib/trees';
19
+ import { BlockHeader } from '@aztec/stdlib/tx';
19
20
 
20
21
  import type { NativeWorldStateService } from '../native/native_world_state.js';
21
22
 
22
- export async function mockBlock(
23
- blockNum: BlockNumber,
24
- size: number,
25
- fork: MerkleTreeWriteOperations,
26
- maxEffects: number | undefined = 1000, // Defaults to the maximum tx effects.
27
- numL1ToL2Messages: number = NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP,
28
- isFirstBlock: boolean = true,
29
- ) {
30
- const l2Block = await L2BlockNew.random(blockNum, { txsPerBlock: size, txOptions: { maxEffects } });
31
- const l1ToL2Messages = mockL1ToL2Messages(numL1ToL2Messages);
23
+ export async function updateBlockState(block: L2Block, l1ToL2Messages: Fr[], fork: MerkleTreeWriteOperations) {
24
+ const insertData = async (
25
+ treeId: IndexedTreeId,
26
+ data: Buffer[][],
27
+ subTreeHeight: number,
28
+ fork: MerkleTreeWriteOperations,
29
+ ) => {
30
+ for (const dataBatch of data) {
31
+ await fork.batchInsert(treeId, dataBatch, subTreeHeight);
32
+ }
33
+ };
32
34
 
33
- {
34
- const insertData = async (
35
- treeId: IndexedTreeId,
36
- data: Buffer[][],
37
- subTreeHeight: number,
38
- fork: MerkleTreeWriteOperations,
39
- ) => {
40
- for (const dataBatch of data) {
41
- await fork.batchInsert(treeId, dataBatch, subTreeHeight);
42
- }
43
- };
44
-
45
- const publicDataInsert = insertData(
46
- MerkleTreeId.PUBLIC_DATA_TREE,
47
- l2Block.body.txEffects.map(txEffect => txEffect.publicDataWrites.map(write => write.toBuffer())),
48
- 0,
49
- fork,
50
- );
51
- const nullifierInsert = insertData(
52
- MerkleTreeId.NULLIFIER_TREE,
53
- l2Block.body.txEffects.map(txEffect =>
54
- padArrayEnd(txEffect.nullifiers, Fr.ZERO, MAX_NULLIFIERS_PER_TX).map(nullifier => nullifier.toBuffer()),
55
- ),
56
- NULLIFIER_SUBTREE_HEIGHT,
57
- fork,
58
- );
59
- const noteHashesPadded = l2Block.body.txEffects.flatMap(txEffect =>
60
- padArrayEnd(txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX),
61
- );
62
-
63
- const l1ToL2MessagesPadded = isFirstBlock
35
+ const publicDataInsert = insertData(
36
+ MerkleTreeId.PUBLIC_DATA_TREE,
37
+ block.body.txEffects.map(txEffect => txEffect.publicDataWrites.map(write => write.toBuffer())),
38
+ 0,
39
+ fork,
40
+ );
41
+ const nullifierInsert = insertData(
42
+ MerkleTreeId.NULLIFIER_TREE,
43
+ block.body.txEffects.map(txEffect =>
44
+ padArrayEnd(txEffect.nullifiers, Fr.ZERO, MAX_NULLIFIERS_PER_TX).map(nullifier => nullifier.toBuffer()),
45
+ ),
46
+ NULLIFIER_SUBTREE_HEIGHT,
47
+ fork,
48
+ );
49
+ const noteHashesPadded = block.body.txEffects.flatMap(txEffect =>
50
+ padArrayEnd(txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX),
51
+ );
52
+
53
+ const l1ToL2MessagesPadded =
54
+ block.indexWithinCheckpoint === 0
64
55
  ? padArrayEnd<Fr, number>(l1ToL2Messages, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP)
65
56
  : l1ToL2Messages;
66
57
 
67
- const noteHashInsert = fork.appendLeaves(MerkleTreeId.NOTE_HASH_TREE, noteHashesPadded);
68
- const messageInsert = fork.appendLeaves(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, l1ToL2MessagesPadded);
69
- await Promise.all([publicDataInsert, nullifierInsert, noteHashInsert, messageInsert]);
70
- }
58
+ const noteHashInsert = fork.appendLeaves(MerkleTreeId.NOTE_HASH_TREE, noteHashesPadded);
59
+ const messageInsert = fork.appendLeaves(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, l1ToL2MessagesPadded);
60
+ await Promise.all([publicDataInsert, nullifierInsert, noteHashInsert, messageInsert]);
71
61
 
72
62
  const state = await fork.getStateReference();
73
- l2Block.header.state = state;
74
- await fork.updateArchive(l2Block.header);
63
+ block.header = BlockHeader.from({ ...block.header, state });
64
+ await fork.updateArchive(block.header);
75
65
 
76
66
  const archiveState = await fork.getTreeInfo(MerkleTreeId.ARCHIVE);
77
67
 
78
- l2Block.archive = new AppendOnlyTreeSnapshot(Fr.fromBuffer(archiveState.root), Number(archiveState.size));
68
+ block.archive = new AppendOnlyTreeSnapshot(Fr.fromBuffer(archiveState.root), Number(archiveState.size));
69
+ }
70
+
71
+ export async function mockBlock(
72
+ blockNum: BlockNumber,
73
+ size: number,
74
+ fork: MerkleTreeWriteOperations,
75
+ maxEffects: number | undefined = 1000, // Defaults to the maximum tx effects.
76
+ numL1ToL2Messages: number = NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP,
77
+ isFirstBlockInCheckpoint: boolean = true,
78
+ ) {
79
+ const block = await L2Block.random(blockNum, {
80
+ indexWithinCheckpoint: isFirstBlockInCheckpoint ? IndexWithinCheckpoint(0) : IndexWithinCheckpoint(1),
81
+ txsPerBlock: size,
82
+ txOptions: { maxEffects },
83
+ });
84
+ const l1ToL2Messages = mockL1ToL2Messages(numL1ToL2Messages);
85
+
86
+ await updateBlockState(block, l1ToL2Messages, fork);
79
87
 
80
88
  return {
81
- block: l2Block,
89
+ block,
82
90
  messages: l1ToL2Messages,
83
91
  };
84
92
  }
85
93
 
86
94
  export async function mockEmptyBlock(blockNum: BlockNumber, fork: MerkleTreeWriteOperations) {
87
- const l2Block = L2BlockNew.empty();
95
+ const l2Block = L2Block.empty();
88
96
  const l1ToL2Messages = Array(16).fill(0).map(Fr.zero);
89
97
 
90
98
  l2Block.header.globalVariables.blockNumber = blockNum;
91
99
 
92
- // Sync the append only trees
93
- {
94
- const noteHashesPadded = l2Block.body.txEffects.flatMap(txEffect =>
95
- padArrayEnd(txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX),
96
- );
97
- await fork.appendLeaves(MerkleTreeId.NOTE_HASH_TREE, noteHashesPadded);
98
-
99
- const l1ToL2MessagesPadded = padArrayEnd<Fr, number>(l1ToL2Messages, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP);
100
- await fork.appendLeaves(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, l1ToL2MessagesPadded);
101
- }
102
-
103
- // Sync the indexed trees
104
- {
105
- // We insert the public data tree leaves with one batch per tx to avoid updating the same key twice
106
- for (const txEffect of l2Block.body.txEffects) {
107
- await fork.batchInsert(
108
- MerkleTreeId.PUBLIC_DATA_TREE,
109
- txEffect.publicDataWrites.map(write => write.toBuffer()),
110
- 0,
111
- );
112
-
113
- const nullifiersPadded = padArrayEnd(txEffect.nullifiers, Fr.ZERO, MAX_NULLIFIERS_PER_TX);
114
-
115
- await fork.batchInsert(
116
- MerkleTreeId.NULLIFIER_TREE,
117
- nullifiersPadded.map(nullifier => nullifier.toBuffer()),
118
- NULLIFIER_SUBTREE_HEIGHT,
119
- );
120
- }
121
- }
122
-
123
- const state = await fork.getStateReference();
124
- l2Block.header.state = state;
125
- await fork.updateArchive(l2Block.header);
126
-
127
- const archiveState = await fork.getTreeInfo(MerkleTreeId.ARCHIVE);
128
-
129
- l2Block.archive = new AppendOnlyTreeSnapshot(Fr.fromBuffer(archiveState.root), Number(archiveState.size));
100
+ await updateBlockState(l2Block, l1ToL2Messages, fork);
130
101
 
131
102
  return {
132
103
  block: l2Block,
@@ -155,44 +126,15 @@ export async function mockBlocks(
155
126
  return { blocks, messages: messagesArray };
156
127
  }
157
128
 
158
- export function mockL1ToL2Messages(numL1ToL2Messages: number) {
159
- return Array(numL1ToL2Messages).fill(0).map(Fr.random);
160
- }
161
-
162
129
  export async function mockCheckpoint(
163
130
  checkpointNumber: CheckpointNumber,
164
- {
165
- startBlockNumber = BlockNumber(1),
166
- numBlocks = 1,
167
- numTxsPerBlock = 1,
168
- numL1ToL2Messages = 1,
169
- fork,
170
- }: {
171
- startBlockNumber?: BlockNumber;
172
- numBlocks?: number;
173
- numTxsPerBlock?: number;
174
- numL1ToL2Messages?: number;
175
- fork?: MerkleTreeWriteOperations;
176
- } = {},
131
+ fork: MerkleTreeWriteOperations,
132
+ options: Partial<Parameters<typeof mockCheckpointAndMessages>[1]> = {},
177
133
  ) {
178
- const slotNumber = SlotNumber(checkpointNumber * 10);
179
- const blocksAndMessages = [];
180
- for (let i = 0; i < numBlocks; i++) {
181
- const blockNumber = BlockNumber(startBlockNumber + i);
182
- const { block, messages } = fork
183
- ? await mockBlock(blockNumber, numTxsPerBlock, fork, blockNumber === startBlockNumber ? numL1ToL2Messages : 0)
184
- : {
185
- block: await L2BlockNew.random(blockNumber, { txsPerBlock: numTxsPerBlock, slotNumber }),
186
- messages: mockL1ToL2Messages(numL1ToL2Messages),
187
- };
188
- blocksAndMessages.push({ block, messages });
189
- }
190
-
191
- const messages = blocksAndMessages[0].messages;
192
- const inHash = computeInHashFromL1ToL2Messages(messages);
193
- const checkpoint = await Checkpoint.random(checkpointNumber, { numBlocks: 0, slotNumber, inHash });
194
- checkpoint.blocks = blocksAndMessages.map(({ block }) => block);
195
-
134
+ const { checkpoint, messages } = await mockCheckpointAndMessages(checkpointNumber, options);
135
+ await asyncMap(checkpoint.blocks, async (block, i) => {
136
+ await updateBlockState(block, i === 0 ? messages : [], fork);
137
+ });
196
138
  return { checkpoint, messages };
197
139
  }
198
140
 
package/src/testing.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { GENESIS_ARCHIVE_ROOT } from '@aztec/constants';
2
- import { Fr } from '@aztec/foundation/fields';
2
+ import { Fr } from '@aztec/foundation/curves/bn254';
3
3
  import { computeFeePayerBalanceLeafSlot } from '@aztec/protocol-contracts/fee-juice';
4
4
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
5
5
  import { MerkleTreeId, PublicDataTreeLeaf } from '@aztec/stdlib/trees';
@@ -1,9 +1,13 @@
1
1
  import { MAX_NULLIFIERS_PER_TX, MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX } from '@aztec/constants';
2
- import { BlockNumber } from '@aztec/foundation/branded-types';
3
- import type { Fr } from '@aztec/foundation/fields';
2
+ import type { BlockNumber } from '@aztec/foundation/branded-types';
3
+ import type { Fr } from '@aztec/foundation/curves/bn254';
4
4
  import type { IndexedTreeSnapshot, TreeSnapshot } from '@aztec/merkle-tree';
5
- import type { L2BlockNew } from '@aztec/stdlib/block';
6
- import type { ForkMerkleTreeOperations, MerkleTreeReadOperations } from '@aztec/stdlib/interfaces/server';
5
+ import type { L2Block } from '@aztec/stdlib/block';
6
+ import type {
7
+ ForkMerkleTreeOperations,
8
+ MerkleTreeReadOperations,
9
+ ReadonlyWorldStateAccess,
10
+ } from '@aztec/stdlib/interfaces/server';
7
11
  import type { MerkleTreeId } from '@aztec/stdlib/trees';
8
12
 
9
13
  import type { WorldStateStatusFull, WorldStateStatusSummary } from '../native/message.js';
@@ -35,19 +39,13 @@ export type TreeSnapshots = {
35
39
  [MerkleTreeId.ARCHIVE]: TreeSnapshot<Fr>;
36
40
  };
37
41
 
38
- export interface MerkleTreeAdminDatabase extends ForkMerkleTreeOperations {
42
+ export interface MerkleTreeAdminDatabase extends ForkMerkleTreeOperations, ReadonlyWorldStateAccess {
39
43
  /**
40
44
  * Handles a single L2 block (i.e. Inserts the new note hashes into the merkle tree).
41
45
  * @param block - The L2 block to handle.
42
46
  * @param l1ToL2Messages - The L1 to L2 messages for the block.
43
- * @param isFirstBlock - Whether the block is the first block in a checkpoint. The messages are padded and inserted
44
- * to the tree for the first block in a checkpoint.
45
47
  */
46
- handleL2BlockAndMessages(
47
- block: L2BlockNew,
48
- l1ToL2Messages: Fr[],
49
- isFirstBlock: boolean,
50
- ): Promise<WorldStateStatusFull>;
48
+ handleL2BlockAndMessages(block: L2Block, l1ToL2Messages: Fr[]): Promise<WorldStateStatusFull>;
51
49
 
52
50
  /**
53
51
  * Gets a handle that allows reading the latest committed state
@@ -1,11 +0,0 @@
1
- import type { Fr } from '@aztec/foundation/fields';
2
- import type { L2BlockNew, L2BlockSource } from '@aztec/stdlib/block';
3
- import { type L1ToL2MessageSource } from '@aztec/stdlib/messaging';
4
- /**
5
- * Determine which blocks in the given array are the first block in a checkpoint.
6
- * @param blocks - The candidate blocks, sorted by block number in ascending order.
7
- * @param l2BlockSource - The L2 block source to use to fetch the checkpoints, block headers and L1->L2 messages.
8
- * @returns A map of block numbers that begin a checkpoint to the L1->L2 messages for that checkpoint.
9
- */
10
- export declare function findFirstBlocksInCheckpoints(blocks: L2BlockNew[], l2BlockSource: L2BlockSource & L1ToL2MessageSource): Promise<Map<number, Fr[]>>;
11
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXRpbHMuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9zeW5jaHJvbml6ZXIvdXRpbHMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQ0EsT0FBTyxLQUFLLEVBQUUsRUFBRSxFQUFFLE1BQU0sMEJBQTBCLENBQUM7QUFDbkQsT0FBTyxLQUFLLEVBQUUsVUFBVSxFQUFFLGFBQWEsRUFBRSxNQUFNLHFCQUFxQixDQUFDO0FBRXJFLE9BQU8sRUFBRSxLQUFLLG1CQUFtQixFQUFtQyxNQUFNLHlCQUF5QixDQUFDO0FBRXBHOzs7OztHQUtHO0FBQ0gsd0JBQXNCLDRCQUE0QixDQUNoRCxNQUFNLEVBQUUsVUFBVSxFQUFFLEVBQ3BCLGFBQWEsRUFBRSxhQUFhLEdBQUcsbUJBQW1CLEdBQ2pELE9BQU8sQ0FBQyxHQUFHLENBQUMsTUFBTSxFQUFFLEVBQUUsRUFBRSxDQUFDLENBQUMsQ0FtRTVCIn0=
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/synchronizer/utils.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,0BAA0B,CAAC;AACnD,OAAO,KAAK,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAErE,OAAO,EAAE,KAAK,mBAAmB,EAAmC,MAAM,yBAAyB,CAAC;AAEpG;;;;;GAKG;AACH,wBAAsB,4BAA4B,CAChD,MAAM,EAAE,UAAU,EAAE,EACpB,aAAa,EAAE,aAAa,GAAG,mBAAmB,GACjD,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,EAAE,EAAE,CAAC,CAAC,CAmE5B"}
@@ -1,59 +0,0 @@
1
- import { BlockNumber } from '@aztec/foundation/branded-types';
2
- import { computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
3
- /**
4
- * Determine which blocks in the given array are the first block in a checkpoint.
5
- * @param blocks - The candidate blocks, sorted by block number in ascending order.
6
- * @param l2BlockSource - The L2 block source to use to fetch the checkpoints, block headers and L1->L2 messages.
7
- * @returns A map of block numbers that begin a checkpoint to the L1->L2 messages for that checkpoint.
8
- */ export async function findFirstBlocksInCheckpoints(blocks, l2BlockSource) {
9
- // Select the blocks that are the final block within each group of identical slot numbers.
10
- let seenSlot;
11
- const maybeLastBlocks = [
12
- ...blocks
13
- ].reverse().filter((b)=>{
14
- if (b.header.globalVariables.slotNumber !== seenSlot) {
15
- seenSlot = b.header.globalVariables.slotNumber;
16
- return true;
17
- }
18
- return false;
19
- }).reverse();
20
- // Try to fetch the checkpoints for those blocks. If undefined (which should only occur for blocks.at(-1)),
21
- // then the block is not the last one in a checkpoint.
22
- // If we are not checking the inHashes below, only blocks.at(-1) would need its checkpoint header fetched.
23
- const checkpointedBlocks = (await Promise.all(maybeLastBlocks.map(async (b)=>({
24
- blockNumber: b.number,
25
- // A checkpoint's archive root is the archive root of its last block.
26
- checkpoint: await l2BlockSource.getCheckpointByArchive(b.archive.root)
27
- })))).filter((b)=>b.checkpoint !== undefined);
28
- // Verify that the L1->L2 messages hash to the checkpoint's inHash.
29
- const checkpointedL1ToL2Messages = await Promise.all(checkpointedBlocks.map((b)=>l2BlockSource.getL1ToL2MessagesForCheckpoint(b.checkpoint.number)));
30
- checkpointedBlocks.forEach((b, i)=>{
31
- const computedInHash = computeInHashFromL1ToL2Messages(checkpointedL1ToL2Messages[i]);
32
- const inHash = b.checkpoint.header.contentCommitment.inHash;
33
- if (!computedInHash.equals(inHash)) {
34
- throw new Error('Obtained L1 to L2 messages failed to be hashed to the checkpoint inHash');
35
- }
36
- });
37
- // Compute the first block numbers, which should be right after each checkpointed block. Exclude blocks that haven't
38
- // been added yet.
39
- const firstBlockNumbers = checkpointedBlocks.map((b)=>BlockNumber(b.blockNumber + 1)).filter((n)=>n <= blocks.at(-1).number);
40
- // Check if blocks[0] is the first block in a checkpoint.
41
- if (blocks[0].number === 1) {
42
- firstBlockNumbers.push(blocks[0].number);
43
- } else {
44
- const lastBlockHeader = await l2BlockSource.getBlockHeader(BlockNumber(blocks[0].number - 1));
45
- if (!lastBlockHeader) {
46
- throw new Error(`Failed to get block ${blocks[0].number - 1}`);
47
- }
48
- if (lastBlockHeader.globalVariables.slotNumber !== blocks[0].header.globalVariables.slotNumber) {
49
- firstBlockNumbers.push(blocks[0].number);
50
- }
51
- }
52
- // Fetch the L1->L2 messages for the first blocks and assign them to the map.
53
- const messagesByBlockNumber = new Map();
54
- await Promise.all(firstBlockNumbers.map(async (blockNumber)=>{
55
- const l1ToL2Messages = await l2BlockSource.getL1ToL2Messages(blockNumber);
56
- messagesByBlockNumber.set(blockNumber, l1ToL2Messages);
57
- }));
58
- return messagesByBlockNumber;
59
- }