@aztec/world-state 0.0.0-test.1 → 0.0.1-commit.001888fc

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 (65) hide show
  1. package/dest/index.d.ts +1 -1
  2. package/dest/instrumentation/instrumentation.d.ts +6 -4
  3. package/dest/instrumentation/instrumentation.d.ts.map +1 -1
  4. package/dest/instrumentation/instrumentation.js +25 -41
  5. package/dest/native/bench_metrics.d.ts +23 -0
  6. package/dest/native/bench_metrics.d.ts.map +1 -0
  7. package/dest/native/bench_metrics.js +81 -0
  8. package/dest/native/fork_checkpoint.d.ts +7 -1
  9. package/dest/native/fork_checkpoint.d.ts.map +1 -1
  10. package/dest/native/fork_checkpoint.js +15 -3
  11. package/dest/native/index.d.ts +1 -1
  12. package/dest/native/merkle_trees_facade.d.ts +20 -8
  13. package/dest/native/merkle_trees_facade.d.ts.map +1 -1
  14. package/dest/native/merkle_trees_facade.js +80 -15
  15. package/dest/native/message.d.ts +83 -53
  16. package/dest/native/message.d.ts.map +1 -1
  17. package/dest/native/message.js +61 -61
  18. package/dest/native/native_world_state.d.ts +27 -19
  19. package/dest/native/native_world_state.d.ts.map +1 -1
  20. package/dest/native/native_world_state.js +103 -41
  21. package/dest/native/native_world_state_instance.d.ts +20 -4
  22. package/dest/native/native_world_state_instance.d.ts.map +1 -1
  23. package/dest/native/native_world_state_instance.js +43 -4
  24. package/dest/native/world_state_ops_queue.d.ts +1 -1
  25. package/dest/native/world_state_ops_queue.d.ts.map +1 -1
  26. package/dest/native/world_state_ops_queue.js +1 -1
  27. package/dest/synchronizer/config.d.ts +14 -6
  28. package/dest/synchronizer/config.d.ts.map +1 -1
  29. package/dest/synchronizer/config.js +33 -10
  30. package/dest/synchronizer/errors.d.ts +4 -0
  31. package/dest/synchronizer/errors.d.ts.map +1 -0
  32. package/dest/synchronizer/errors.js +5 -0
  33. package/dest/synchronizer/factory.d.ts +12 -4
  34. package/dest/synchronizer/factory.d.ts.map +1 -1
  35. package/dest/synchronizer/factory.js +13 -8
  36. package/dest/synchronizer/index.d.ts +1 -1
  37. package/dest/synchronizer/server_world_state_synchronizer.d.ts +21 -31
  38. package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -1
  39. package/dest/synchronizer/server_world_state_synchronizer.js +191 -96
  40. package/dest/test/index.d.ts +1 -1
  41. package/dest/test/utils.d.ts +12 -5
  42. package/dest/test/utils.d.ts.map +1 -1
  43. package/dest/test/utils.js +54 -47
  44. package/dest/testing.d.ts +3 -3
  45. package/dest/testing.d.ts.map +1 -1
  46. package/dest/testing.js +7 -11
  47. package/dest/world-state-db/index.d.ts +1 -1
  48. package/dest/world-state-db/merkle_tree_db.d.ts +12 -18
  49. package/dest/world-state-db/merkle_tree_db.d.ts.map +1 -1
  50. package/package.json +23 -24
  51. package/src/instrumentation/instrumentation.ts +31 -43
  52. package/src/native/bench_metrics.ts +91 -0
  53. package/src/native/fork_checkpoint.ts +19 -3
  54. package/src/native/merkle_trees_facade.ts +92 -20
  55. package/src/native/message.ts +105 -75
  56. package/src/native/native_world_state.ts +132 -52
  57. package/src/native/native_world_state_instance.ts +63 -10
  58. package/src/native/world_state_ops_queue.ts +1 -1
  59. package/src/synchronizer/config.ts +55 -21
  60. package/src/synchronizer/errors.ts +5 -0
  61. package/src/synchronizer/factory.ts +39 -10
  62. package/src/synchronizer/server_world_state_synchronizer.ts +227 -121
  63. package/src/test/utils.ts +92 -82
  64. package/src/testing.ts +4 -8
  65. package/src/world-state-db/merkle_tree_db.ts +16 -18
@@ -1,5 +1,6 @@
1
- import type { DataStoreConfig } from '@aztec/kv-store/config';
1
+ import type { LoggerBindings } from '@aztec/foundation/log';
2
2
  import type { L2BlockSource } from '@aztec/stdlib/block';
3
+ import type { DataStoreConfig } from '@aztec/stdlib/kv-store';
3
4
  import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
4
5
  import type { PublicDataTreeLeaf } from '@aztec/stdlib/trees';
5
6
  import { type TelemetryClient, getTelemetryClient } from '@aztec/telemetry-client';
@@ -9,44 +10,72 @@ import { NativeWorldStateService } from '../native/native_world_state.js';
9
10
  import type { WorldStateConfig } from './config.js';
10
11
  import { ServerWorldStateSynchronizer } from './server_world_state_synchronizer.js';
11
12
 
13
+ export interface WorldStateTreeMapSizes {
14
+ archiveTreeMapSizeKb: number;
15
+ nullifierTreeMapSizeKb: number;
16
+ noteHashTreeMapSizeKb: number;
17
+ messageTreeMapSizeKb: number;
18
+ publicDataTreeMapSizeKb: number;
19
+ }
20
+
12
21
  export async function createWorldStateSynchronizer(
13
22
  config: WorldStateConfig & DataStoreConfig,
14
23
  l2BlockSource: L2BlockSource & L1ToL2MessageSource,
15
24
  prefilledPublicData: PublicDataTreeLeaf[] = [],
16
25
  client: TelemetryClient = getTelemetryClient(),
26
+ bindings?: LoggerBindings,
17
27
  ) {
18
28
  const instrumentation = new WorldStateInstrumentation(client);
19
- const merkleTrees = await createWorldState(config, prefilledPublicData, instrumentation);
29
+ const merkleTrees = await createWorldState(config, prefilledPublicData, instrumentation, bindings);
20
30
  return new ServerWorldStateSynchronizer(merkleTrees, l2BlockSource, config, instrumentation);
21
31
  }
22
32
 
23
33
  export async function createWorldState(
24
- config: WorldStateConfig & DataStoreConfig,
34
+ config: Pick<
35
+ WorldStateConfig,
36
+ | 'worldStateDataDirectory'
37
+ | 'worldStateDbMapSizeKb'
38
+ | 'archiveTreeMapSizeKb'
39
+ | 'nullifierTreeMapSizeKb'
40
+ | 'noteHashTreeMapSizeKb'
41
+ | 'messageTreeMapSizeKb'
42
+ | 'publicDataTreeMapSizeKb'
43
+ > &
44
+ Pick<DataStoreConfig, 'dataDirectory' | 'dataStoreMapSizeKb' | 'l1Contracts'>,
25
45
  prefilledPublicData: PublicDataTreeLeaf[] = [],
26
46
  instrumentation: WorldStateInstrumentation = new WorldStateInstrumentation(getTelemetryClient()),
47
+ bindings?: LoggerBindings,
27
48
  ) {
28
- const newConfig = {
29
- dataDirectory: config.worldStateDataDirectory ?? config.dataDirectory,
30
- dataStoreMapSizeKB: config.worldStateDbMapSizeKb ?? config.dataStoreMapSizeKB,
31
- } as DataStoreConfig;
49
+ const dataDirectory = config.worldStateDataDirectory ?? config.dataDirectory;
50
+ const dataStoreMapSizeKb = config.worldStateDbMapSizeKb ?? config.dataStoreMapSizeKb;
51
+ const wsTreeMapSizes: WorldStateTreeMapSizes = {
52
+ archiveTreeMapSizeKb: config.archiveTreeMapSizeKb ?? dataStoreMapSizeKb,
53
+ nullifierTreeMapSizeKb: config.nullifierTreeMapSizeKb ?? dataStoreMapSizeKb,
54
+ noteHashTreeMapSizeKb: config.noteHashTreeMapSizeKb ?? dataStoreMapSizeKb,
55
+ messageTreeMapSizeKb: config.messageTreeMapSizeKb ?? dataStoreMapSizeKb,
56
+ publicDataTreeMapSizeKb: config.publicDataTreeMapSizeKb ?? dataStoreMapSizeKb,
57
+ };
32
58
 
33
59
  if (!config.l1Contracts?.rollupAddress) {
34
60
  throw new Error('Rollup address is required to create a world state synchronizer.');
35
61
  }
36
62
 
37
63
  // If a data directory is provided in config, then create a persistent store.
38
- const merkleTrees = newConfig.dataDirectory
64
+ const merkleTrees = dataDirectory
39
65
  ? await NativeWorldStateService.new(
40
66
  config.l1Contracts.rollupAddress,
41
- newConfig.dataDirectory,
42
- newConfig.dataStoreMapSizeKB,
67
+ dataDirectory,
68
+ wsTreeMapSizes,
43
69
  prefilledPublicData,
44
70
  instrumentation,
71
+ bindings,
45
72
  )
46
73
  : await NativeWorldStateService.tmp(
47
74
  config.l1Contracts.rollupAddress,
48
75
  !['true', '1'].includes(process.env.DEBUG_WORLD_STATE!),
49
76
  prefilledPublicData,
77
+ instrumentation,
78
+ bindings,
50
79
  );
51
80
 
52
81
  return merkleTrees;
@@ -1,19 +1,20 @@
1
- import { L1_TO_L2_MSG_SUBTREE_HEIGHT } from '@aztec/constants';
2
- import type { Fr } from '@aztec/foundation/fields';
3
- import { createLogger } from '@aztec/foundation/log';
1
+ import { GENESIS_BLOCK_HEADER_HASH, INITIAL_CHECKPOINT_NUMBER, INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
2
+ import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
3
+ import type { Fr } from '@aztec/foundation/curves/bn254';
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 { MerkleTreeCalculator } from '@aztec/foundation/trees';
7
- import { SHA256Trunc } from '@aztec/merkle-tree';
8
- import type {
9
- L2Block,
10
- L2BlockId,
11
- L2BlockSource,
7
+ import {
8
+ type BlockHash,
9
+ GENESIS_CHECKPOINT_HEADER_HASH,
10
+ type L2Block,
11
+ type L2BlockId,
12
+ type L2BlockSource,
12
13
  L2BlockStream,
13
- L2BlockStreamEvent,
14
- L2BlockStreamEventHandler,
15
- L2BlockStreamLocalDataProvider,
16
- L2Tips,
14
+ type L2BlockStreamEvent,
15
+ type L2BlockStreamEventHandler,
16
+ type L2BlockStreamLocalDataProvider,
17
+ type L2Tips,
17
18
  } from '@aztec/stdlib/block';
18
19
  import {
19
20
  WorldStateRunningState,
@@ -22,14 +23,18 @@ import {
22
23
  type WorldStateSynchronizerStatus,
23
24
  } from '@aztec/stdlib/interfaces/server';
24
25
  import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
26
+ import type { SnapshotDataKeys } from '@aztec/stdlib/snapshots';
25
27
  import type { L2BlockHandledStats } from '@aztec/stdlib/stats';
26
28
  import { MerkleTreeId, type MerkleTreeReadOperations, type MerkleTreeWriteOperations } from '@aztec/stdlib/trees';
27
- import { TraceableL2BlockStream, getTelemetryClient } from '@aztec/telemetry-client';
29
+ import { getTelemetryClient } from '@aztec/telemetry-client';
28
30
 
29
31
  import { WorldStateInstrumentation } from '../instrumentation/instrumentation.js';
30
32
  import type { WorldStateStatusFull } from '../native/message.js';
31
33
  import type { MerkleTreeAdminDatabase } from '../world-state-db/merkle_tree_db.js';
32
34
  import type { WorldStateConfig } from './config.js';
35
+ import { WorldStateSynchronizerError } from './errors.js';
36
+
37
+ export type { SnapshotDataKeys };
33
38
 
34
39
  /**
35
40
  * Synchronizes the world state with the L2 blocks from a L2BlockSource via a block stream.
@@ -41,23 +46,26 @@ export class ServerWorldStateSynchronizer
41
46
  {
42
47
  private readonly merkleTreeCommitted: MerkleTreeReadOperations;
43
48
 
44
- private latestBlockNumberAtStart = 0;
49
+ private latestBlockNumberAtStart = BlockNumber.ZERO;
45
50
  private historyToKeep: number | undefined;
46
51
  private currentState: WorldStateRunningState = WorldStateRunningState.IDLE;
47
- private latestBlockHashQuery: { blockNumber: number; hash: string | undefined } | undefined = undefined;
48
52
 
49
53
  private syncPromise = promiseWithResolvers<void>();
50
54
  protected blockStream: L2BlockStream | undefined;
51
55
 
56
+ // WorldState doesn't track the proven block number, it only tracks the latest tips of the pending chain and the finalized chain
57
+ // 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: BlockNumber | undefined;
59
+
52
60
  constructor(
53
61
  private readonly merkleTreeDb: MerkleTreeAdminDatabase,
54
62
  private readonly l2BlockSource: L2BlockSource & L1ToL2MessageSource,
55
63
  private readonly config: WorldStateConfig,
56
64
  private instrumentation = new WorldStateInstrumentation(getTelemetryClient()),
57
- private readonly log = createLogger('world_state'),
65
+ private readonly log: Logger = createLogger('world_state'),
58
66
  ) {
59
67
  this.merkleTreeCommitted = this.merkleTreeDb.getCommitted();
60
- this.historyToKeep = config.worldStateBlockHistory < 1 ? undefined : config.worldStateBlockHistory;
68
+ this.historyToKeep = config.worldStateCheckpointHistory < 1 ? undefined : config.worldStateCheckpointHistory;
61
69
  this.log.info(
62
70
  `Created world state synchroniser with block history of ${
63
71
  this.historyToKeep === undefined ? 'infinity' : this.historyToKeep
@@ -69,12 +77,20 @@ export class ServerWorldStateSynchronizer
69
77
  return this.merkleTreeDb.getCommitted();
70
78
  }
71
79
 
72
- public getSnapshot(blockNumber: number): MerkleTreeReadOperations {
80
+ public getSnapshot(blockNumber: BlockNumber): MerkleTreeReadOperations {
73
81
  return this.merkleTreeDb.getSnapshot(blockNumber);
74
82
  }
75
83
 
76
- public fork(blockNumber?: number): Promise<MerkleTreeWriteOperations> {
77
- return this.merkleTreeDb.fork(blockNumber);
84
+ public fork(blockNumber?: BlockNumber, opts?: { closeDelayMs?: number }): Promise<MerkleTreeWriteOperations> {
85
+ return this.merkleTreeDb.fork(blockNumber, opts);
86
+ }
87
+
88
+ public backupTo(dstPath: string, compact?: boolean): Promise<Record<Exclude<SnapshotDataKeys, 'archiver'>, string>> {
89
+ return this.merkleTreeDb.backupTo(dstPath, compact);
90
+ }
91
+
92
+ public clear(): Promise<void> {
93
+ return this.merkleTreeDb.clear();
78
94
  }
79
95
 
80
96
  public async start() {
@@ -86,9 +102,7 @@ export class ServerWorldStateSynchronizer
86
102
  }
87
103
 
88
104
  // Get the current latest block number
89
- this.latestBlockNumberAtStart = await (this.config.worldStateProvenBlocksOnly
90
- ? this.l2BlockSource.getProvenBlockNumber()
91
- : this.l2BlockSource.getBlockNumber());
105
+ this.latestBlockNumberAtStart = BlockNumber(await this.l2BlockSource.getBlockNumber());
92
106
 
93
107
  const blockToDownloadFrom = (await this.getLatestBlockNumber()) + 1;
94
108
 
@@ -110,12 +124,11 @@ export class ServerWorldStateSynchronizer
110
124
  }
111
125
 
112
126
  protected createBlockStream(): L2BlockStream {
113
- const tracer = this.instrumentation.telemetry.getTracer('WorldStateL2BlockStream');
114
127
  const logger = createLogger('world-state:block_stream');
115
- return new TraceableL2BlockStream(this.l2BlockSource, this, this, tracer, 'WorldStateL2BlockStream', logger, {
116
- proven: this.config.worldStateProvenBlocksOnly,
128
+ return new L2BlockStream(this.l2BlockSource, this, this, logger, {
117
129
  pollIntervalMS: this.config.worldStateBlockCheckIntervalMS,
118
130
  batchSize: this.config.worldStateBlockRequestBatchSize,
131
+ ignoreCheckpoints: true,
119
132
  });
120
133
  }
121
134
 
@@ -131,10 +144,10 @@ export class ServerWorldStateSynchronizer
131
144
  public async status(): Promise<WorldStateSynchronizerStatus> {
132
145
  const summary = await this.merkleTreeDb.getStatusSummary();
133
146
  const status: WorldStateSyncStatus = {
134
- latestBlockNumber: Number(summary.unfinalisedBlockNumber),
135
- latestBlockHash: (await this.getL2BlockHash(Number(summary.unfinalisedBlockNumber))) ?? '',
136
- finalisedBlockNumber: Number(summary.finalisedBlockNumber),
137
- oldestHistoricBlockNumber: Number(summary.oldestHistoricalBlock),
147
+ latestBlockNumber: summary.unfinalizedBlockNumber,
148
+ latestBlockHash: (await this.getL2BlockHash(summary.unfinalizedBlockNumber)) ?? '',
149
+ finalizedBlockNumber: summary.finalizedBlockNumber,
150
+ oldestHistoricBlockNumber: summary.oldestHistoricalBlock,
138
151
  treesAreSynched: summary.treesAreSynched,
139
152
  };
140
153
  return {
@@ -144,86 +157,168 @@ export class ServerWorldStateSynchronizer
144
157
  }
145
158
 
146
159
  public async getLatestBlockNumber() {
147
- return (await this.getL2Tips()).latest.number;
160
+ return (await this.getL2Tips()).proposed.number;
161
+ }
162
+
163
+ public async stopSync() {
164
+ this.log.debug('Stopping sync...');
165
+ await this.blockStream?.stop();
166
+ this.log.info('Stopped sync');
167
+ }
168
+
169
+ public resumeSync() {
170
+ if (!this.blockStream) {
171
+ throw new Error('Cannot resume sync as block stream is not initialized');
172
+ }
173
+ this.log.debug('Resuming sync...');
174
+ this.blockStream.start();
175
+ this.log.info('Resumed sync');
148
176
  }
149
177
 
150
178
  /**
151
179
  * Forces an immediate sync.
152
- * @param targetBlockNumber - The target block number that we must sync to. Will download unproven blocks if needed to reach it. Throws if cannot be reached.
180
+ * @param targetBlockNumber - The target block number that we must sync to. Will download unproven blocks if needed to reach it.
181
+ * @param blockHash - If provided, verifies the block at targetBlockNumber matches this hash. On mismatch, triggers a resync (reorg detection).
153
182
  * @returns A promise that resolves with the block number the world state was synced to
154
183
  */
155
- public async syncImmediate(targetBlockNumber?: number): Promise<number> {
156
- if (this.currentState !== WorldStateRunningState.RUNNING || this.blockStream === undefined) {
184
+ public async syncImmediate(targetBlockNumber?: BlockNumber, blockHash?: BlockHash): Promise<BlockNumber> {
185
+ if (this.currentState !== WorldStateRunningState.RUNNING) {
157
186
  throw new Error(`World State is not running. Unable to perform sync.`);
158
187
  }
159
188
 
189
+ if (this.blockStream === undefined) {
190
+ throw new Error('Block stream is not initialized. Unable to perform sync.');
191
+ }
192
+
160
193
  // If we have been given a block number to sync to and we have reached that number then return
161
194
  const currentBlockNumber = await this.getLatestBlockNumber();
162
195
  if (targetBlockNumber !== undefined && targetBlockNumber <= currentBlockNumber) {
163
- return currentBlockNumber;
196
+ if (blockHash === undefined) {
197
+ return currentBlockNumber;
198
+ }
199
+
200
+ // If a block hash was provided, verify we're on the expected fork
201
+ const currentHash = await this.getL2BlockHash(targetBlockNumber);
202
+ if (currentHash === blockHash.toString()) {
203
+ return currentBlockNumber;
204
+ }
205
+ // Hash mismatch: a reorg may have occurred, fall through to trigger sync
206
+ this.log.debug(
207
+ `World state block hash mismatch at ${targetBlockNumber} (expected ${blockHash}, got ${currentHash}). Triggering resync.`,
208
+ );
164
209
  }
165
210
  this.log.debug(`World State at ${currentBlockNumber} told to sync to ${targetBlockNumber ?? 'latest'}`);
166
211
 
212
+ // If the archiver is behind the target block, force an archiver sync
213
+ if (targetBlockNumber) {
214
+ const archiverLatestBlock = BlockNumber(await this.l2BlockSource.getBlockNumber());
215
+ if (archiverLatestBlock < targetBlockNumber) {
216
+ this.log.debug(`Archiver is at ${archiverLatestBlock} behind target block ${targetBlockNumber}.`);
217
+ await this.l2BlockSource.syncImmediate();
218
+ }
219
+ }
220
+
167
221
  // Force the block stream to sync against the archiver now
168
222
  await this.blockStream.sync();
169
223
 
170
224
  // If we have been given a block number to sync to and we have not reached that number then fail
171
225
  const updatedBlockNumber = await this.getLatestBlockNumber();
172
226
  if (targetBlockNumber !== undefined && targetBlockNumber > updatedBlockNumber) {
173
- throw new Error(`Unable to sync to block number ${targetBlockNumber} (last synced is ${updatedBlockNumber})`);
227
+ throw new WorldStateSynchronizerError(
228
+ `Unable to sync to block number ${targetBlockNumber} (last synced is ${updatedBlockNumber})`,
229
+ {
230
+ cause: {
231
+ reason: 'block_not_available',
232
+ previousBlockNumber: currentBlockNumber,
233
+ updatedBlockNumber,
234
+ targetBlockNumber,
235
+ },
236
+ },
237
+ );
238
+ }
239
+
240
+ // If a block hash was provided, verify we're on the expected fork after syncing, throw otherwise
241
+ if (blockHash !== undefined && targetBlockNumber !== undefined) {
242
+ const updatedHash = await this.getL2BlockHash(targetBlockNumber);
243
+ if (updatedHash !== blockHash.toString()) {
244
+ throw new WorldStateSynchronizerError(
245
+ `Block hash mismatch at block ${targetBlockNumber} (expected ${blockHash} but got ${updatedHash})`,
246
+ {
247
+ cause: {
248
+ reason: 'block_hash_mismatch',
249
+ targetBlockNumber,
250
+ expectedHash: blockHash.toString(),
251
+ actualHash: updatedHash,
252
+ },
253
+ },
254
+ );
255
+ }
174
256
  }
175
257
 
176
258
  return updatedBlockNumber;
177
259
  }
178
260
 
179
261
  /** Returns the L2 block hash for a given number. Used by the L2BlockStream for detecting reorgs. */
180
- public async getL2BlockHash(number: number): Promise<string | undefined> {
181
- if (number === 0) {
262
+ public async getL2BlockHash(number: BlockNumber): Promise<string | undefined> {
263
+ if (number === BlockNumber.ZERO) {
182
264
  return (await this.merkleTreeCommitted.getInitialHeader().hash()).toString();
183
265
  }
184
- if (this.latestBlockHashQuery?.hash === undefined || number !== this.latestBlockHashQuery.blockNumber) {
185
- this.latestBlockHashQuery = {
186
- hash: await this.merkleTreeCommitted
187
- .getLeafValue(MerkleTreeId.ARCHIVE, BigInt(number))
188
- .then(leaf => leaf?.toString()),
189
- blockNumber: number,
190
- };
191
- }
192
- return this.latestBlockHashQuery.hash;
266
+ return this.merkleTreeCommitted.getLeafValue(MerkleTreeId.ARCHIVE, BigInt(number)).then(leaf => leaf?.toString());
193
267
  }
194
268
 
195
269
  /** Returns the latest L2 block number for each tip of the chain (latest, proven, finalized). */
196
270
  public async getL2Tips(): Promise<L2Tips> {
197
271
  const status = await this.merkleTreeDb.getStatusSummary();
198
- const unfinalisedBlockHash = await this.getL2BlockHash(Number(status.unfinalisedBlockNumber));
199
- const latestBlockId: L2BlockId = { number: Number(status.unfinalisedBlockNumber), hash: unfinalisedBlockHash! };
200
-
272
+ const unfinalizedBlockHashPromise = this.getL2BlockHash(status.unfinalizedBlockNumber);
273
+ const finalizedBlockHashPromise = this.getL2BlockHash(status.finalizedBlockNumber);
274
+
275
+ const provenBlockNumber = this.provenBlockNumber ?? status.finalizedBlockNumber;
276
+ const provenBlockHashPromise =
277
+ this.provenBlockNumber === undefined ? finalizedBlockHashPromise : this.getL2BlockHash(this.provenBlockNumber);
278
+
279
+ const [unfinalizedBlockHash, finalizedBlockHash, provenBlockHash] = await Promise.all([
280
+ unfinalizedBlockHashPromise,
281
+ finalizedBlockHashPromise,
282
+ provenBlockHashPromise,
283
+ ]);
284
+ const latestBlockId: L2BlockId = { number: status.unfinalizedBlockNumber, hash: unfinalizedBlockHash! };
285
+
286
+ // World state doesn't track checkpointed blocks or checkpoints themselves.
287
+ // but we use a block stream so we need to provide 'local' L2Tips.
288
+ // We configure the block stream to ignore checkpoints and set checkpoint values to genesis here.
289
+ const genesisCheckpointHeaderHash = GENESIS_CHECKPOINT_HEADER_HASH.toString();
201
290
  return {
202
- latest: latestBlockId,
203
- finalized: { number: Number(status.finalisedBlockNumber), hash: '' },
204
- proven: { number: Number(status.finalisedBlockNumber), hash: '' }, // TODO(palla/reorg): Using finalised as proven for now
291
+ proposed: latestBlockId,
292
+ checkpointed: {
293
+ block: { number: INITIAL_L2_BLOCK_NUM, hash: GENESIS_BLOCK_HEADER_HASH.toString() },
294
+ checkpoint: { number: INITIAL_CHECKPOINT_NUMBER, hash: genesisCheckpointHeaderHash },
295
+ },
296
+ finalized: {
297
+ block: { number: status.finalizedBlockNumber, hash: finalizedBlockHash ?? '' },
298
+ checkpoint: { number: INITIAL_CHECKPOINT_NUMBER, hash: genesisCheckpointHeaderHash },
299
+ },
300
+ proven: {
301
+ block: { number: provenBlockNumber, hash: provenBlockHash ?? '' },
302
+ checkpoint: { number: INITIAL_CHECKPOINT_NUMBER, hash: genesisCheckpointHeaderHash },
303
+ },
205
304
  };
206
305
  }
207
306
 
208
307
  /** Handles an event emitted by the block stream. */
209
308
  public async handleBlockStreamEvent(event: L2BlockStreamEvent): Promise<void> {
210
- try {
211
- switch (event.type) {
212
- case 'blocks-added':
213
- await this.handleL2Blocks(event.blocks);
214
- break;
215
- case 'chain-pruned':
216
- await this.handleChainPruned(event.blockNumber);
217
- break;
218
- case 'chain-proven':
219
- await this.handleChainProven(event.blockNumber);
220
- break;
221
- case 'chain-finalized':
222
- await this.handleChainFinalized(event.blockNumber);
223
- break;
224
- }
225
- } catch (err) {
226
- this.log.error('Error processing block stream', err);
309
+ switch (event.type) {
310
+ case 'blocks-added':
311
+ await this.handleL2Blocks(event.blocks);
312
+ break;
313
+ case 'chain-pruned':
314
+ await this.handleChainPruned(event.block.number);
315
+ break;
316
+ case 'chain-proven':
317
+ await this.handleChainProven(event.block.number);
318
+ break;
319
+ case 'chain-finalized':
320
+ await this.handleChainFinalized(event.block.number);
321
+ break;
227
322
  }
228
323
  }
229
324
 
@@ -233,21 +328,31 @@ export class ServerWorldStateSynchronizer
233
328
  * @returns Whether the block handled was produced by this same node.
234
329
  */
235
330
  private async handleL2Blocks(l2Blocks: L2Block[]) {
236
- this.log.trace(`Handling L2 blocks ${l2Blocks[0].number} to ${l2Blocks.at(-1)!.number}`);
331
+ this.log.debug(`Handling L2 blocks ${l2Blocks[0].number} to ${l2Blocks.at(-1)!.number}`);
332
+
333
+ // Fetch the L1->L2 messages for the first block in a checkpoint.
334
+ const messagesForBlocks = new Map<BlockNumber, Fr[]>();
335
+ await Promise.all(
336
+ l2Blocks
337
+ .filter(b => b.indexWithinCheckpoint === 0)
338
+ .map(async block => {
339
+ const l1ToL2Messages = await this.l2BlockSource.getL1ToL2Messages(block.checkpointNumber);
340
+ messagesForBlocks.set(block.number, l1ToL2Messages);
341
+ }),
342
+ );
237
343
 
238
- const messagePromises = l2Blocks.map(block => this.l2BlockSource.getL1ToL2Messages(BigInt(block.number)));
239
- const l1ToL2Messages: Fr[][] = await Promise.all(messagePromises);
240
344
  let updateStatus: WorldStateStatusFull | undefined = undefined;
241
-
242
- for (let i = 0; i < l2Blocks.length; i++) {
243
- const [duration, result] = await elapsed(() => this.handleL2Block(l2Blocks[i], l1ToL2Messages[i]));
244
- this.log.verbose(`World state updated with L2 block ${l2Blocks[i].number}`, {
345
+ for (const block of l2Blocks) {
346
+ const [duration, result] = await elapsed(() =>
347
+ this.handleL2Block(block, messagesForBlocks.get(block.number) ?? []),
348
+ );
349
+ this.log.info(`World state updated with L2 block ${block.number}`, {
245
350
  eventName: 'l2-block-handled',
246
351
  duration,
247
- unfinalisedBlockNumber: result.summary.unfinalisedBlockNumber,
248
- finalisedBlockNumber: result.summary.finalisedBlockNumber,
249
- oldestHistoricBlock: result.summary.oldestHistoricalBlock,
250
- ...l2Blocks[i].getStats(),
352
+ unfinalizedBlockNumber: BigInt(result.summary.unfinalizedBlockNumber),
353
+ finalizedBlockNumber: BigInt(result.summary.finalizedBlockNumber),
354
+ oldestHistoricBlock: BigInt(result.summary.oldestHistoricalBlock),
355
+ ...block.getStats(),
251
356
  } satisfies L2BlockHandledStats);
252
357
  updateStatus = result;
253
358
  }
@@ -264,17 +369,12 @@ export class ServerWorldStateSynchronizer
264
369
  * @returns Whether the block handled was produced by this same node.
265
370
  */
266
371
  private async handleL2Block(l2Block: L2Block, l1ToL2Messages: Fr[]): Promise<WorldStateStatusFull> {
267
- // First we check that the L1 to L2 messages hash to the block inHash.
268
- // Note that we cannot optimize this check by checking the root of the subtree after inserting the messages
269
- // to the real L1_TO_L2_MESSAGE_TREE (like we do in merkleTreeDb.handleL2BlockAndMessages(...)) because that
270
- // tree uses pedersen and we don't have access to the converted root.
271
- await this.verifyMessagesHashToInHash(l1ToL2Messages, l2Block.header.contentCommitment.inHash);
272
-
273
- // If the above check succeeds, we can proceed to handle the block.
274
- this.log.trace(`Pushing L2 block ${l2Block.number} to merkle tree db `, {
372
+ this.log.debug(`Pushing L2 block ${l2Block.number} to merkle tree db `, {
275
373
  blockNumber: l2Block.number,
276
374
  blockHash: await l2Block.hash().then(h => h.toString()),
277
375
  l1ToL2Messages: l1ToL2Messages.map(msg => msg.toString()),
376
+ blockHeader: l2Block.header.toInspect(),
377
+ blockStats: l2Block.getStats(),
278
378
  });
279
379
  const result = await this.merkleTreeDb.handleL2BlockAndMessages(l2Block, l1ToL2Messages);
280
380
 
@@ -286,30 +386,56 @@ export class ServerWorldStateSynchronizer
286
386
  return result;
287
387
  }
288
388
 
289
- private async handleChainFinalized(blockNumber: number) {
389
+ private async handleChainFinalized(blockNumber: BlockNumber) {
290
390
  this.log.verbose(`Finalized chain is now at block ${blockNumber}`);
291
- const summary = await this.merkleTreeDb.setFinalised(BigInt(blockNumber));
391
+ const summary = await this.merkleTreeDb.setFinalized(blockNumber);
292
392
  if (this.historyToKeep === undefined) {
293
393
  return;
294
394
  }
295
- const newHistoricBlock = summary.finalisedBlockNumber - BigInt(this.historyToKeep) + 1n;
296
- if (newHistoricBlock <= 1) {
395
+ // Get the checkpointed block for the finalized block number
396
+ const finalisedCheckpoint = await this.l2BlockSource.getCheckpointedBlock(summary.finalizedBlockNumber);
397
+ if (finalisedCheckpoint === undefined) {
398
+ this.log.warn(
399
+ `Failed to retrieve checkpointed block for finalized block number: ${summary.finalizedBlockNumber}`,
400
+ );
401
+ return;
402
+ }
403
+ // Compute the required historic checkpoint number
404
+ const newHistoricCheckpointNumber = finalisedCheckpoint.checkpointNumber - this.historyToKeep + 1;
405
+ if (newHistoricCheckpointNumber <= 1) {
406
+ return;
407
+ }
408
+ // Retrieve the historic checkpoint
409
+ const historicCheckpoints = await this.l2BlockSource.getCheckpoints(
410
+ CheckpointNumber(newHistoricCheckpointNumber),
411
+ 1,
412
+ );
413
+ if (historicCheckpoints.length === 0 || historicCheckpoints[0] === undefined) {
414
+ this.log.warn(`Failed to retrieve checkpoint number ${newHistoricCheckpointNumber} from Archiver`);
297
415
  return;
298
416
  }
299
- this.log.verbose(`Pruning historic blocks to ${newHistoricBlock}`);
300
- const status = await this.merkleTreeDb.removeHistoricalBlocks(newHistoricBlock);
417
+ const historicCheckpoint = historicCheckpoints[0];
418
+ if (historicCheckpoint.checkpoint.blocks.length === 0 || historicCheckpoint.checkpoint.blocks[0] === undefined) {
419
+ this.log.warn(`Retrieved checkpoint number ${newHistoricCheckpointNumber} has no blocks!`);
420
+ return;
421
+ }
422
+ // Find the block at the start of the checkpoint and remove blocks up to this one
423
+ const newHistoricBlock = historicCheckpoint.checkpoint.blocks[0];
424
+ this.log.verbose(`Pruning historic blocks to ${newHistoricBlock.number}`);
425
+ const status = await this.merkleTreeDb.removeHistoricalBlocks(BlockNumber(newHistoricBlock.number));
301
426
  this.log.debug(`World state summary `, status.summary);
302
427
  }
303
428
 
304
- private handleChainProven(blockNumber: number) {
429
+ private handleChainProven(blockNumber: BlockNumber) {
430
+ this.provenBlockNumber = blockNumber;
305
431
  this.log.debug(`Proven chain is now at block ${blockNumber}`);
306
432
  return Promise.resolve();
307
433
  }
308
434
 
309
- private async handleChainPruned(blockNumber: number) {
310
- this.log.warn(`Chain pruned to block ${blockNumber}`);
311
- const status = await this.merkleTreeDb.unwindBlocks(BigInt(blockNumber));
312
- this.latestBlockHashQuery = undefined;
435
+ private async handleChainPruned(blockNumber: BlockNumber) {
436
+ this.log.info(`Chain pruned to block ${blockNumber}`);
437
+ const status = await this.merkleTreeDb.unwindBlocks(blockNumber);
438
+ this.provenBlockNumber = undefined;
313
439
  this.instrumentation.updateWorldStateMetrics(status);
314
440
  }
315
441
 
@@ -321,24 +447,4 @@ export class ServerWorldStateSynchronizer
321
447
  this.currentState = newState;
322
448
  this.log.debug(`Moved to state ${WorldStateRunningState[this.currentState]}`);
323
449
  }
324
-
325
- /**
326
- * Verifies that the L1 to L2 messages hash to the block inHash.
327
- * @param l1ToL2Messages - The L1 to L2 messages for the block.
328
- * @param inHash - The inHash of the block.
329
- * @throws If the L1 to L2 messages do not hash to the block inHash.
330
- */
331
- protected async verifyMessagesHashToInHash(l1ToL2Messages: Fr[], inHash: Buffer) {
332
- const treeCalculator = await MerkleTreeCalculator.create(
333
- L1_TO_L2_MSG_SUBTREE_HEIGHT,
334
- Buffer.alloc(32),
335
- (lhs, rhs) => Promise.resolve(new SHA256Trunc().hash(lhs, rhs)),
336
- );
337
-
338
- const root = await treeCalculator.computeTreeRoot(l1ToL2Messages.map(msg => msg.toBuffer()));
339
-
340
- if (!root.equals(inHash)) {
341
- throw new Error('Obtained L1 to L2 messages failed to be hashed to the block inHash');
342
- }
343
- }
344
450
  }