@aztec/world-state 0.0.1-commit.c7c42ec → 0.0.1-commit.c80b6263

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 (36) 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 +1 -1
  5. package/dest/native/merkle_trees_facade.d.ts.map +1 -1
  6. package/dest/native/merkle_trees_facade.js +15 -4
  7. package/dest/native/native_world_state.d.ts +7 -7
  8. package/dest/native/native_world_state.d.ts.map +1 -1
  9. package/dest/native/native_world_state.js +8 -7
  10. package/dest/native/native_world_state_instance.d.ts +3 -3
  11. package/dest/native/native_world_state_instance.d.ts.map +1 -1
  12. package/dest/native/native_world_state_instance.js +2 -2
  13. package/dest/synchronizer/config.d.ts +1 -3
  14. package/dest/synchronizer/config.d.ts.map +1 -1
  15. package/dest/synchronizer/config.js +1 -6
  16. package/dest/synchronizer/factory.d.ts +4 -3
  17. package/dest/synchronizer/factory.d.ts.map +1 -1
  18. package/dest/synchronizer/factory.js +5 -5
  19. package/dest/synchronizer/server_world_state_synchronizer.d.ts +2 -3
  20. package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -1
  21. package/dest/synchronizer/server_world_state_synchronizer.js +50 -24
  22. package/dest/test/utils.d.ts +7 -7
  23. package/dest/test/utils.d.ts.map +1 -1
  24. package/dest/test/utils.js +5 -5
  25. package/dest/world-state-db/merkle_tree_db.d.ts +3 -3
  26. package/dest/world-state-db/merkle_tree_db.d.ts.map +1 -1
  27. package/package.json +11 -11
  28. package/src/instrumentation/instrumentation.ts +17 -41
  29. package/src/native/merkle_trees_facade.ts +10 -1
  30. package/src/native/native_world_state.ts +23 -9
  31. package/src/native/native_world_state_instance.ts +4 -2
  32. package/src/synchronizer/config.ts +1 -14
  33. package/src/synchronizer/factory.ts +7 -1
  34. package/src/synchronizer/server_world_state_synchronizer.ts +48 -36
  35. package/src/test/utils.ts +6 -6
  36. package/src/world-state-db/merkle_tree_db.ts +2 -2
@@ -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
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,7 +25,7 @@ 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';
@@ -46,7 +48,6 @@ export class ServerWorldStateSynchronizer
46
48
  private latestBlockNumberAtStart = BlockNumber.ZERO;
47
49
  private historyToKeep: number | undefined;
48
50
  private currentState: WorldStateRunningState = WorldStateRunningState.IDLE;
49
- private latestBlockHashQuery: { blockNumber: BlockNumber; hash: string | undefined } | undefined = undefined;
50
51
 
51
52
  private syncPromise = promiseWithResolvers<void>();
52
53
  protected blockStream: L2BlockStream | undefined;
@@ -100,11 +101,7 @@ export class ServerWorldStateSynchronizer
100
101
  }
101
102
 
102
103
  // Get the current latest block number
103
- this.latestBlockNumberAtStart = BlockNumber(
104
- await (this.config.worldStateProvenBlocksOnly
105
- ? this.l2BlockSource.getProvenBlockNumber()
106
- : this.l2BlockSource.getBlockNumber()),
107
- );
104
+ this.latestBlockNumberAtStart = BlockNumber(await this.l2BlockSource.getBlockNumber());
108
105
 
109
106
  const blockToDownloadFrom = (await this.getLatestBlockNumber()) + 1;
110
107
 
@@ -126,12 +123,11 @@ export class ServerWorldStateSynchronizer
126
123
  }
127
124
 
128
125
  protected createBlockStream(): L2BlockStream {
129
- const tracer = this.instrumentation.telemetry.getTracer('WorldStateL2BlockStream');
130
126
  const logger = createLogger('world-state:block_stream');
131
- return new TraceableL2BlockStream(this.l2BlockSource, this, this, tracer, 'WorldStateL2BlockStream', logger, {
132
- proven: this.config.worldStateProvenBlocksOnly,
127
+ return new L2BlockStream(this.l2BlockSource, this, this, logger, {
133
128
  pollIntervalMS: this.config.worldStateBlockCheckIntervalMS,
134
129
  batchSize: this.config.worldStateBlockRequestBatchSize,
130
+ ignoreCheckpoints: true,
135
131
  });
136
132
  }
137
133
 
@@ -160,7 +156,7 @@ export class ServerWorldStateSynchronizer
160
156
  }
161
157
 
162
158
  public async getLatestBlockNumber() {
163
- return (await this.getL2Tips()).latest.number;
159
+ return (await this.getL2Tips()).proposed.number;
164
160
  }
165
161
 
166
162
  public async stopSync() {
@@ -239,27 +235,44 @@ export class ServerWorldStateSynchronizer
239
235
  if (number === BlockNumber.ZERO) {
240
236
  return (await this.merkleTreeCommitted.getInitialHeader().hash()).toString();
241
237
  }
242
- if (this.latestBlockHashQuery?.hash === undefined || number !== this.latestBlockHashQuery.blockNumber) {
243
- this.latestBlockHashQuery = {
244
- hash: await this.merkleTreeCommitted
245
- .getLeafValue(MerkleTreeId.ARCHIVE, BigInt(number))
246
- .then(leaf => leaf?.toString()),
247
- blockNumber: number,
248
- };
249
- }
250
- return this.latestBlockHashQuery.hash;
238
+ return this.merkleTreeCommitted.getLeafValue(MerkleTreeId.ARCHIVE, BigInt(number)).then(leaf => leaf?.toString());
251
239
  }
252
240
 
253
241
  /** Returns the latest L2 block number for each tip of the chain (latest, proven, finalized). */
254
242
  public async getL2Tips(): Promise<L2Tips> {
255
243
  const status = await this.merkleTreeDb.getStatusSummary();
256
- 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
+ ]);
257
256
  const latestBlockId: L2BlockId = { number: status.unfinalizedBlockNumber, hash: unfinalizedBlockHash! };
258
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();
259
262
  return {
260
- latest: latestBlockId,
261
- finalized: { number: status.finalizedBlockNumber, hash: '' },
262
- 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
+ },
263
276
  };
264
277
  }
265
278
 
@@ -267,7 +280,7 @@ export class ServerWorldStateSynchronizer
267
280
  public async handleBlockStreamEvent(event: L2BlockStreamEvent): Promise<void> {
268
281
  switch (event.type) {
269
282
  case 'blocks-added':
270
- await this.handleL2Blocks(event.blocks.map(b => b.block.toL2Block()));
283
+ await this.handleL2Blocks(event.blocks);
271
284
  break;
272
285
  case 'chain-pruned':
273
286
  await this.handleChainPruned(event.block.number);
@@ -286,7 +299,7 @@ export class ServerWorldStateSynchronizer
286
299
  * @param l2Blocks - The L2 blocks to handle.
287
300
  * @returns Whether the block handled was produced by this same node.
288
301
  */
289
- private async handleL2Blocks(l2Blocks: L2BlockNew[]) {
302
+ private async handleL2Blocks(l2Blocks: L2Block[]) {
290
303
  this.log.trace(`Handling L2 blocks ${l2Blocks[0].number} to ${l2Blocks.at(-1)!.number}`);
291
304
 
292
305
  // Fetch the L1->L2 messages for the first block in a checkpoint.
@@ -327,7 +340,7 @@ export class ServerWorldStateSynchronizer
327
340
  * @param l1ToL2Messages - The L1 to L2 messages for the block.
328
341
  * @returns Whether the block handled was produced by this same node.
329
342
  */
330
- private async handleL2Block(l2Block: L2BlockNew, l1ToL2Messages: Fr[]): Promise<WorldStateStatusFull> {
343
+ private async handleL2Block(l2Block: L2Block, l1ToL2Messages: Fr[]): Promise<WorldStateStatusFull> {
331
344
  this.log.trace(`Pushing L2 block ${l2Block.number} to merkle tree db `, {
332
345
  blockNumber: l2Block.number,
333
346
  blockHash: await l2Block.hash().then(h => h.toString()),
@@ -367,7 +380,6 @@ export class ServerWorldStateSynchronizer
367
380
  private async handleChainPruned(blockNumber: BlockNumber) {
368
381
  this.log.warn(`Chain pruned to block ${blockNumber}`);
369
382
  const status = await this.merkleTreeDb.unwindBlocks(blockNumber);
370
- this.latestBlockHashQuery = undefined;
371
383
  this.provenBlockNumber = undefined;
372
384
  this.instrumentation.updateWorldStateMetrics(status);
373
385
  }
package/src/test/utils.ts CHANGED
@@ -5,10 +5,10 @@ import {
5
5
  NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP,
6
6
  } from '@aztec/constants';
7
7
  import { asyncMap } from '@aztec/foundation/async-map';
8
- import { BlockNumber, type CheckpointNumber } from '@aztec/foundation/branded-types';
8
+ import { BlockNumber, type CheckpointNumber, IndexWithinCheckpoint } from '@aztec/foundation/branded-types';
9
9
  import { padArrayEnd } from '@aztec/foundation/collection';
10
10
  import { Fr } from '@aztec/foundation/curves/bn254';
11
- import { L2BlockNew } from '@aztec/stdlib/block';
11
+ import { L2Block } from '@aztec/stdlib/block';
12
12
  import type {
13
13
  IndexedTreeId,
14
14
  MerkleTreeReadOperations,
@@ -20,7 +20,7 @@ import { BlockHeader } from '@aztec/stdlib/tx';
20
20
 
21
21
  import type { NativeWorldStateService } from '../native/native_world_state.js';
22
22
 
23
- export async function updateBlockState(block: L2BlockNew, l1ToL2Messages: Fr[], fork: MerkleTreeWriteOperations) {
23
+ export async function updateBlockState(block: L2Block, l1ToL2Messages: Fr[], fork: MerkleTreeWriteOperations) {
24
24
  const insertData = async (
25
25
  treeId: IndexedTreeId,
26
26
  data: Buffer[][],
@@ -76,8 +76,8 @@ export async function mockBlock(
76
76
  numL1ToL2Messages: number = NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP,
77
77
  isFirstBlockInCheckpoint: boolean = true,
78
78
  ) {
79
- const block = await L2BlockNew.random(blockNum, {
80
- indexWithinCheckpoint: isFirstBlockInCheckpoint ? 0 : 1,
79
+ const block = await L2Block.random(blockNum, {
80
+ indexWithinCheckpoint: isFirstBlockInCheckpoint ? IndexWithinCheckpoint(0) : IndexWithinCheckpoint(1),
81
81
  txsPerBlock: size,
82
82
  txOptions: { maxEffects },
83
83
  });
@@ -92,7 +92,7 @@ export async function mockBlock(
92
92
  }
93
93
 
94
94
  export async function mockEmptyBlock(blockNum: BlockNumber, fork: MerkleTreeWriteOperations) {
95
- const l2Block = L2BlockNew.empty();
95
+ const l2Block = L2Block.empty();
96
96
  const l1ToL2Messages = Array(16).fill(0).map(Fr.zero);
97
97
 
98
98
  l2Block.header.globalVariables.blockNumber = blockNum;
@@ -2,7 +2,7 @@ import { MAX_NULLIFIERS_PER_TX, MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX } f
2
2
  import type { BlockNumber } from '@aztec/foundation/branded-types';
3
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';
5
+ import type { L2Block } from '@aztec/stdlib/block';
6
6
  import type {
7
7
  ForkMerkleTreeOperations,
8
8
  MerkleTreeReadOperations,
@@ -45,7 +45,7 @@ export interface MerkleTreeAdminDatabase extends ForkMerkleTreeOperations, Reado
45
45
  * @param block - The L2 block to handle.
46
46
  * @param l1ToL2Messages - The L1 to L2 messages for the block.
47
47
  */
48
- handleL2BlockAndMessages(block: L2BlockNew, l1ToL2Messages: Fr[]): Promise<WorldStateStatusFull>;
48
+ handleL2BlockAndMessages(block: L2Block, l1ToL2Messages: Fr[]): Promise<WorldStateStatusFull>;
49
49
 
50
50
  /**
51
51
  * Gets a handle that allows reading the latest committed state