@aztec/world-state 0.0.1-commit.e2b2873ed → 0.0.1-commit.e304674f1

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 (41) hide show
  1. package/dest/native/fork_checkpoint.d.ts +7 -1
  2. package/dest/native/fork_checkpoint.d.ts.map +1 -1
  3. package/dest/native/fork_checkpoint.js +15 -3
  4. package/dest/native/merkle_trees_facade.d.ts +5 -5
  5. package/dest/native/merkle_trees_facade.d.ts.map +1 -1
  6. package/dest/native/merkle_trees_facade.js +9 -6
  7. package/dest/native/message.d.ts +12 -4
  8. package/dest/native/message.d.ts.map +1 -1
  9. package/dest/native/native_world_state.d.ts +7 -5
  10. package/dest/native/native_world_state.d.ts.map +1 -1
  11. package/dest/native/native_world_state.js +14 -9
  12. package/dest/native/native_world_state_instance.d.ts +4 -4
  13. package/dest/native/native_world_state_instance.d.ts.map +1 -1
  14. package/dest/native/native_world_state_instance.js +8 -7
  15. package/dest/native/world_state_ops_queue.js +5 -5
  16. package/dest/synchronizer/config.d.ts +3 -3
  17. package/dest/synchronizer/config.d.ts.map +1 -1
  18. package/dest/synchronizer/config.js +6 -3
  19. package/dest/synchronizer/factory.d.ts +5 -5
  20. package/dest/synchronizer/factory.d.ts.map +1 -1
  21. package/dest/synchronizer/factory.js +6 -5
  22. package/dest/synchronizer/server_world_state_synchronizer.d.ts +4 -4
  23. package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -1
  24. package/dest/synchronizer/server_world_state_synchronizer.js +85 -16
  25. package/dest/testing.d.ts +4 -3
  26. package/dest/testing.d.ts.map +1 -1
  27. package/dest/testing.js +10 -6
  28. package/dest/world-state-db/merkle_tree_db.d.ts +1 -10
  29. package/dest/world-state-db/merkle_tree_db.d.ts.map +1 -1
  30. package/package.json +9 -10
  31. package/src/native/fork_checkpoint.ts +19 -3
  32. package/src/native/merkle_trees_facade.ts +14 -7
  33. package/src/native/message.ts +13 -3
  34. package/src/native/native_world_state.ts +12 -18
  35. package/src/native/native_world_state_instance.ts +10 -6
  36. package/src/native/world_state_ops_queue.ts +5 -5
  37. package/src/synchronizer/config.ts +7 -5
  38. package/src/synchronizer/factory.ts +7 -7
  39. package/src/synchronizer/server_world_state_synchronizer.ts +96 -19
  40. package/src/testing.ts +8 -9
  41. package/src/world-state-db/merkle_tree_db.ts +0 -10
@@ -284,6 +284,16 @@ interface WithForkId {
284
284
  forkId: number;
285
285
  }
286
286
 
287
+ interface CreateCheckpointResponse {
288
+ depth: number;
289
+ }
290
+
291
+ /** Request to commit/revert all checkpoints down to a target depth. The resulting depth after the operation equals the given depth. */
292
+ interface CheckpointDepthRequest extends WithForkId {
293
+ /** The target depth after the operation. All checkpoints above this depth are committed/reverted. */
294
+ depth: number;
295
+ }
296
+
287
297
  interface WithWorldStateRevision {
288
298
  revision: WorldStateRevision;
289
299
  }
@@ -487,8 +497,8 @@ export type WorldStateRequest = {
487
497
  [WorldStateMessageType.CREATE_CHECKPOINT]: WithForkId;
488
498
  [WorldStateMessageType.COMMIT_CHECKPOINT]: WithForkId;
489
499
  [WorldStateMessageType.REVERT_CHECKPOINT]: WithForkId;
490
- [WorldStateMessageType.COMMIT_ALL_CHECKPOINTS]: WithForkId;
491
- [WorldStateMessageType.REVERT_ALL_CHECKPOINTS]: WithForkId;
500
+ [WorldStateMessageType.COMMIT_ALL_CHECKPOINTS]: CheckpointDepthRequest;
501
+ [WorldStateMessageType.REVERT_ALL_CHECKPOINTS]: CheckpointDepthRequest;
492
502
 
493
503
  [WorldStateMessageType.COPY_STORES]: CopyStoresRequest;
494
504
 
@@ -529,7 +539,7 @@ export type WorldStateResponse = {
529
539
 
530
540
  [WorldStateMessageType.GET_STATUS]: WorldStateStatusSummary;
531
541
 
532
- [WorldStateMessageType.CREATE_CHECKPOINT]: void;
542
+ [WorldStateMessageType.CREATE_CHECKPOINT]: CreateCheckpointResponse;
533
543
  [WorldStateMessageType.COMMIT_CHECKPOINT]: void;
534
544
  [WorldStateMessageType.REVERT_CHECKPOINT]: void;
535
545
  [WorldStateMessageType.COMMIT_ALL_CHECKPOINTS]: void;
@@ -14,8 +14,8 @@ import type {
14
14
  } from '@aztec/stdlib/interfaces/server';
15
15
  import type { SnapshotDataKeys } from '@aztec/stdlib/snapshots';
16
16
  import { MerkleTreeId, NullifierLeaf, type NullifierLeafPreimage, PublicDataTreeLeaf } from '@aztec/stdlib/trees';
17
- import { BlockHeader, PartialStateReference, StateReference } from '@aztec/stdlib/tx';
18
- import { WorldStateRevision } from '@aztec/stdlib/world-state';
17
+ import { BlockHeader, GlobalVariables, PartialStateReference, StateReference } from '@aztec/stdlib/tx';
18
+ import { EMPTY_GENESIS_DATA, type GenesisData, WorldStateRevision } from '@aztec/stdlib/world-state';
19
19
  import { getTelemetryClient } from '@aztec/telemetry-client';
20
20
 
21
21
  import assert from 'assert/strict';
@@ -53,6 +53,7 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
53
53
  protected instance: NativeWorldState,
54
54
  protected readonly worldStateInstrumentation: WorldStateInstrumentation,
55
55
  protected readonly log: Logger,
56
+ private readonly genesis: GenesisData = EMPTY_GENESIS_DATA,
56
57
  private readonly cleanup = () => Promise.resolve(),
57
58
  ) {}
58
59
 
@@ -60,7 +61,7 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
60
61
  rollupAddress: EthAddress,
61
62
  dataDir: string,
62
63
  wsTreeMapSizes: WorldStateTreeMapSizes,
63
- prefilledPublicData: PublicDataTreeLeaf[] = [],
64
+ genesis: GenesisData = EMPTY_GENESIS_DATA,
64
65
  instrumentation = new WorldStateInstrumentation(getTelemetryClient()),
65
66
  bindings?: LoggerBindings,
66
67
  cleanup = () => Promise.resolve(),
@@ -73,14 +74,12 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
73
74
  rollupAddress,
74
75
  dataDirectory: worldStateDirectory,
75
76
  onOpen: (dir: string) => {
76
- return Promise.resolve(
77
- new NativeWorldState(dir, wsTreeMapSizes, prefilledPublicData, instrumentation, bindings),
78
- );
77
+ return Promise.resolve(new NativeWorldState(dir, wsTreeMapSizes, genesis, instrumentation, bindings));
79
78
  },
80
79
  });
81
80
 
82
81
  const [instance] = await versionManager.open();
83
- const worldState = new this(instance, instrumentation, log, cleanup);
82
+ const worldState = new this(instance, instrumentation, log, genesis, cleanup);
84
83
  try {
85
84
  await worldState.init();
86
85
  } catch (e) {
@@ -94,7 +93,7 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
94
93
  static async tmp(
95
94
  rollupAddress = EthAddress.ZERO,
96
95
  cleanupTmpDir = true,
97
- prefilledPublicData: PublicDataTreeLeaf[] = [],
96
+ genesis: GenesisData = EMPTY_GENESIS_DATA,
98
97
  instrumentation = new WorldStateInstrumentation(getTelemetryClient()),
99
98
  bindings?: LoggerBindings,
100
99
  ): Promise<NativeWorldStateService> {
@@ -120,15 +119,7 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
120
119
  }
121
120
  };
122
121
 
123
- return this.new(
124
- rollupAddress,
125
- dataDir,
126
- worldStateTreeMapSizes,
127
- prefilledPublicData,
128
- instrumentation,
129
- bindings,
130
- cleanup,
131
- );
122
+ return this.new(rollupAddress, dataDir, worldStateTreeMapSizes, genesis, instrumentation, bindings, cleanup);
132
123
  }
133
124
 
134
125
  protected async init() {
@@ -256,7 +247,10 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
256
247
 
257
248
  private async buildInitialHeader(): Promise<BlockHeader> {
258
249
  const state = await this.getInitialStateReference();
259
- return BlockHeader.empty({ state });
250
+ return BlockHeader.empty({
251
+ state,
252
+ globalVariables: GlobalVariables.empty({ timestamp: this.genesis.genesisTimestamp }),
253
+ });
260
254
  }
261
255
 
262
256
  private sanitizeAndCacheSummaryFromFull(response: WorldStateStatusFull) {
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  ARCHIVE_HEIGHT,
3
- GeneratorIndex,
3
+ DomainSeparator,
4
4
  L1_TO_L2_MSG_TREE_HEIGHT,
5
5
  MAX_NULLIFIERS_PER_TX,
6
6
  MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
@@ -11,7 +11,7 @@ import {
11
11
  import { type Logger, type LoggerBindings, createLogger } from '@aztec/foundation/log';
12
12
  import { NativeWorldState as BaseNativeWorldState, MsgpackChannel } from '@aztec/native';
13
13
  import { MerkleTreeId } from '@aztec/stdlib/trees';
14
- import type { PublicDataTreeLeaf } from '@aztec/stdlib/trees';
14
+ import { EMPTY_GENESIS_DATA, type GenesisData } from '@aztec/stdlib/world-state';
15
15
 
16
16
  import assert from 'assert';
17
17
  import { cpus } from 'os';
@@ -55,7 +55,7 @@ export class NativeWorldState implements NativeWorldStateInstance {
55
55
  constructor(
56
56
  private readonly dataDir: string,
57
57
  private readonly wsTreeMapSizes: WorldStateTreeMapSizes,
58
- private readonly prefilledPublicData: PublicDataTreeLeaf[] = [],
58
+ private readonly genesis: GenesisData = EMPTY_GENESIS_DATA,
59
59
  private readonly instrumentation: WorldStateInstrumentation,
60
60
  bindings?: LoggerBindings,
61
61
  private readonly log: Logger = createLogger('world-state:database', bindings),
@@ -66,7 +66,10 @@ export class NativeWorldState implements NativeWorldStateInstance {
66
66
  wsTreeMapSizes,
67
67
  )} and ${threads} threads.`,
68
68
  );
69
- const prefilledPublicDataBufferArray = prefilledPublicData.map(d => [d.slot.toBuffer(), d.value.toBuffer()]);
69
+ const prefilledPublicDataBufferArray = genesis.prefilledPublicData.map(d => [
70
+ d.slot.toBuffer(),
71
+ d.value.toBuffer(),
72
+ ]);
70
73
  const ws = new BaseNativeWorldState(
71
74
  dataDir,
72
75
  {
@@ -81,7 +84,8 @@ export class NativeWorldState implements NativeWorldStateInstance {
81
84
  [MerkleTreeId.PUBLIC_DATA_TREE]: 2 * MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
82
85
  },
83
86
  prefilledPublicDataBufferArray,
84
- GeneratorIndex.BLOCK_HEADER_HASH,
87
+ DomainSeparator.BLOCK_HEADER_HASH,
88
+ Number(genesis.genesisTimestamp),
85
89
  {
86
90
  [MerkleTreeId.NULLIFIER_TREE]: wsTreeMapSizes.nullifierTreeMapSizeKb,
87
91
  [MerkleTreeId.NOTE_HASH_TREE]: wsTreeMapSizes.noteHashTreeMapSizeKb,
@@ -104,7 +108,7 @@ export class NativeWorldState implements NativeWorldStateInstance {
104
108
  return new NativeWorldState(
105
109
  this.dataDir,
106
110
  this.wsTreeMapSizes,
107
- this.prefilledPublicData,
111
+ this.genesis,
108
112
  this.instrumentation,
109
113
  this.log.getBindings(),
110
114
  this.log,
@@ -96,7 +96,7 @@ export class WorldStateOpsQueue {
96
96
  // then send the request immediately
97
97
  // If a mutating request is in flight then we must wait
98
98
  // If a mutating request is not in flight but something is queued then it must be a mutating request
99
- if (this.inFlightMutatingCount == 0 && this.requests.length == 0) {
99
+ if (this.inFlightMutatingCount === 0 && this.requests.length === 0) {
100
100
  this.sendEnqueuedRequest(op);
101
101
  } else {
102
102
  this.requests.push(op);
@@ -122,7 +122,7 @@ export class WorldStateOpsQueue {
122
122
  --this.inFlightCount;
123
123
 
124
124
  // If there are still requests in flight then do nothing further
125
- if (this.inFlightCount != 0) {
125
+ if (this.inFlightCount !== 0) {
126
126
  return;
127
127
  }
128
128
 
@@ -134,7 +134,7 @@ export class WorldStateOpsQueue {
134
134
  while (this.requests.length > 0) {
135
135
  const next = this.requests[0];
136
136
  if (next.mutating) {
137
- if (this.inFlightCount == 0) {
137
+ if (this.inFlightCount === 0) {
138
138
  // send the mutating request
139
139
  this.requests.shift();
140
140
  this.sendEnqueuedRequest(next);
@@ -149,7 +149,7 @@ export class WorldStateOpsQueue {
149
149
  }
150
150
 
151
151
  // If the queue is empty, there is nothing in flight and we have been told to stop, then resolve the stop promise
152
- if (this.inFlightCount == 0 && this.stopResolve !== undefined) {
152
+ if (this.inFlightCount === 0 && this.stopResolve !== undefined) {
153
153
  this.stopResolve();
154
154
  }
155
155
  }
@@ -182,7 +182,7 @@ export class WorldStateOpsQueue {
182
182
  });
183
183
 
184
184
  // If no outstanding requests then immediately resolve the promise
185
- if (this.requests.length == 0 && this.inFlightCount == 0 && this.stopResolve !== undefined) {
185
+ if (this.requests.length === 0 && this.inFlightCount === 0 && this.stopResolve !== undefined) {
186
186
  this.stopResolve();
187
187
  }
188
188
  return this.stopPromise;
@@ -29,8 +29,8 @@ export interface WorldStateConfig {
29
29
  /** Optional directory for the world state DB, if unspecified will default to the general data directory */
30
30
  worldStateDataDirectory?: string;
31
31
 
32
- /** The number of historic blocks to maintain */
33
- worldStateBlockHistory: number;
32
+ /** The number of historic checkpoints worth of blocks to maintain */
33
+ worldStateCheckpointHistory: number;
34
34
  }
35
35
 
36
36
  export const worldStateConfigMappings: ConfigMappingsType<WorldStateConfig> = {
@@ -84,9 +84,11 @@ export const worldStateConfigMappings: ConfigMappingsType<WorldStateConfig> = {
84
84
  env: 'WS_DATA_DIRECTORY',
85
85
  description: 'Optional directory for the world state database',
86
86
  },
87
- worldStateBlockHistory: {
88
- env: 'WS_NUM_HISTORIC_BLOCKS',
89
- description: 'The number of historic blocks to maintain. Values less than 1 mean all history is maintained',
87
+ worldStateCheckpointHistory: {
88
+ env: 'WS_NUM_HISTORIC_CHECKPOINTS',
89
+ description:
90
+ 'The number of historic checkpoints worth of blocks to maintain. Values less than 1 mean all history is maintained',
91
+ fallback: ['WS_NUM_HISTORIC_BLOCKS'],
90
92
  ...numberConfigHelper(64),
91
93
  },
92
94
  };
@@ -1,8 +1,8 @@
1
1
  import type { LoggerBindings } from '@aztec/foundation/log';
2
- import type { DataStoreConfig } from '@aztec/kv-store/config';
3
2
  import type { L2BlockSource } from '@aztec/stdlib/block';
3
+ import type { DataStoreConfig } from '@aztec/stdlib/kv-store';
4
4
  import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
5
- import type { PublicDataTreeLeaf } from '@aztec/stdlib/trees';
5
+ import { EMPTY_GENESIS_DATA, type GenesisData } from '@aztec/stdlib/world-state';
6
6
  import { type TelemetryClient, getTelemetryClient } from '@aztec/telemetry-client';
7
7
 
8
8
  import { WorldStateInstrumentation } from '../instrumentation/instrumentation.js';
@@ -21,12 +21,12 @@ export interface WorldStateTreeMapSizes {
21
21
  export async function createWorldStateSynchronizer(
22
22
  config: WorldStateConfig & DataStoreConfig,
23
23
  l2BlockSource: L2BlockSource & L1ToL2MessageSource,
24
- prefilledPublicData: PublicDataTreeLeaf[] = [],
24
+ genesis: GenesisData = EMPTY_GENESIS_DATA,
25
25
  client: TelemetryClient = getTelemetryClient(),
26
26
  bindings?: LoggerBindings,
27
27
  ) {
28
28
  const instrumentation = new WorldStateInstrumentation(client);
29
- const merkleTrees = await createWorldState(config, prefilledPublicData, instrumentation, bindings);
29
+ const merkleTrees = await createWorldState(config, genesis, instrumentation, bindings);
30
30
  return new ServerWorldStateSynchronizer(merkleTrees, l2BlockSource, config, instrumentation);
31
31
  }
32
32
 
@@ -42,7 +42,7 @@ export async function createWorldState(
42
42
  | 'publicDataTreeMapSizeKb'
43
43
  > &
44
44
  Pick<DataStoreConfig, 'dataDirectory' | 'dataStoreMapSizeKb' | 'l1Contracts'>,
45
- prefilledPublicData: PublicDataTreeLeaf[] = [],
45
+ genesis: GenesisData = EMPTY_GENESIS_DATA,
46
46
  instrumentation: WorldStateInstrumentation = new WorldStateInstrumentation(getTelemetryClient()),
47
47
  bindings?: LoggerBindings,
48
48
  ) {
@@ -66,14 +66,14 @@ export async function createWorldState(
66
66
  config.l1Contracts.rollupAddress,
67
67
  dataDirectory,
68
68
  wsTreeMapSizes,
69
- prefilledPublicData,
69
+ genesis,
70
70
  instrumentation,
71
71
  bindings,
72
72
  )
73
73
  : await NativeWorldStateService.tmp(
74
74
  config.l1Contracts.rollupAddress,
75
75
  !['true', '1'].includes(process.env.DEBUG_WORLD_STATE!),
76
- prefilledPublicData,
76
+ genesis,
77
77
  instrumentation,
78
78
  bindings,
79
79
  );
@@ -1,10 +1,11 @@
1
- import { GENESIS_BLOCK_HEADER_HASH, INITIAL_L2_BLOCK_NUM, INITIAL_L2_CHECKPOINT_NUM } from '@aztec/constants';
2
- import { BlockNumber } from '@aztec/foundation/branded-types';
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
3
  import type { Fr } from '@aztec/foundation/curves/bn254';
4
4
  import { type Logger, createLogger } from '@aztec/foundation/log';
5
5
  import { promiseWithResolvers } from '@aztec/foundation/promise';
6
6
  import { elapsed } from '@aztec/foundation/timer';
7
7
  import {
8
+ type BlockHash,
8
9
  GENESIS_CHECKPOINT_HEADER_HASH,
9
10
  type L2Block,
10
11
  type L2BlockId,
@@ -64,7 +65,7 @@ export class ServerWorldStateSynchronizer
64
65
  private readonly log: Logger = createLogger('world_state'),
65
66
  ) {
66
67
  this.merkleTreeCommitted = this.merkleTreeDb.getCommitted();
67
- this.historyToKeep = config.worldStateBlockHistory < 1 ? undefined : config.worldStateBlockHistory;
68
+ this.historyToKeep = config.worldStateCheckpointHistory < 1 ? undefined : config.worldStateCheckpointHistory;
68
69
  this.log.info(
69
70
  `Created world state synchroniser with block history of ${
70
71
  this.historyToKeep === undefined ? 'infinity' : this.historyToKeep
@@ -177,13 +178,10 @@ export class ServerWorldStateSynchronizer
177
178
  /**
178
179
  * Forces an immediate sync.
179
180
  * @param targetBlockNumber - The target block number that we must sync to. Will download unproven blocks if needed to reach it.
180
- * @param skipThrowIfTargetNotReached - Whether to skip throwing if the target block number is not reached.
181
+ * @param blockHash - If provided, verifies the block at targetBlockNumber matches this hash. On mismatch, triggers a resync (reorg detection).
181
182
  * @returns A promise that resolves with the block number the world state was synced to
182
183
  */
183
- public async syncImmediate(
184
- targetBlockNumber?: BlockNumber,
185
- skipThrowIfTargetNotReached?: boolean,
186
- ): Promise<BlockNumber> {
184
+ public async syncImmediate(targetBlockNumber?: BlockNumber, blockHash?: BlockHash): Promise<BlockNumber> {
187
185
  if (this.currentState !== WorldStateRunningState.RUNNING) {
188
186
  throw new Error(`World State is not running. Unable to perform sync.`);
189
187
  }
@@ -195,7 +193,19 @@ export class ServerWorldStateSynchronizer
195
193
  // If we have been given a block number to sync to and we have reached that number then return
196
194
  const currentBlockNumber = await this.getLatestBlockNumber();
197
195
  if (targetBlockNumber !== undefined && targetBlockNumber <= currentBlockNumber) {
198
- 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
+ );
199
209
  }
200
210
  this.log.debug(`World State at ${currentBlockNumber} told to sync to ${targetBlockNumber ?? 'latest'}`);
201
211
 
@@ -213,7 +223,7 @@ export class ServerWorldStateSynchronizer
213
223
 
214
224
  // If we have been given a block number to sync to and we have not reached that number then fail
215
225
  const updatedBlockNumber = await this.getLatestBlockNumber();
216
- if (!skipThrowIfTargetNotReached && targetBlockNumber !== undefined && targetBlockNumber > updatedBlockNumber) {
226
+ if (targetBlockNumber !== undefined && targetBlockNumber > updatedBlockNumber) {
217
227
  throw new WorldStateSynchronizerError(
218
228
  `Unable to sync to block number ${targetBlockNumber} (last synced is ${updatedBlockNumber})`,
219
229
  {
@@ -227,6 +237,24 @@ export class ServerWorldStateSynchronizer
227
237
  );
228
238
  }
229
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
+ }
256
+ }
257
+
230
258
  return updatedBlockNumber;
231
259
  }
232
260
 
@@ -263,15 +291,19 @@ export class ServerWorldStateSynchronizer
263
291
  proposed: latestBlockId,
264
292
  checkpointed: {
265
293
  block: { number: INITIAL_L2_BLOCK_NUM, hash: GENESIS_BLOCK_HEADER_HASH.toString() },
266
- checkpoint: { number: INITIAL_L2_CHECKPOINT_NUM, hash: genesisCheckpointHeaderHash },
294
+ checkpoint: { number: INITIAL_CHECKPOINT_NUMBER, hash: genesisCheckpointHeaderHash },
295
+ },
296
+ proposedCheckpoint: {
297
+ block: { number: INITIAL_L2_BLOCK_NUM, hash: GENESIS_BLOCK_HEADER_HASH.toString() },
298
+ checkpoint: { number: INITIAL_CHECKPOINT_NUMBER, hash: genesisCheckpointHeaderHash },
267
299
  },
268
300
  finalized: {
269
301
  block: { number: status.finalizedBlockNumber, hash: finalizedBlockHash ?? '' },
270
- checkpoint: { number: INITIAL_L2_CHECKPOINT_NUM, hash: genesisCheckpointHeaderHash },
302
+ checkpoint: { number: INITIAL_CHECKPOINT_NUMBER, hash: genesisCheckpointHeaderHash },
271
303
  },
272
304
  proven: {
273
305
  block: { number: provenBlockNumber, hash: provenBlockHash ?? '' },
274
- checkpoint: { number: INITIAL_L2_CHECKPOINT_NUM, hash: genesisCheckpointHeaderHash },
306
+ checkpoint: { number: INITIAL_CHECKPOINT_NUMBER, hash: genesisCheckpointHeaderHash },
275
307
  },
276
308
  };
277
309
  }
@@ -360,16 +392,59 @@ export class ServerWorldStateSynchronizer
360
392
 
361
393
  private async handleChainFinalized(blockNumber: BlockNumber) {
362
394
  this.log.verbose(`Finalized chain is now at block ${blockNumber}`);
395
+ // If the finalized block number is older than the oldest available block in world state,
396
+ // skip entirely. The finalized block number can jump backwards (e.g. when the finalization
397
+ // heuristic changes) and try to read block data that has already been pruned. When this
398
+ // happens, there is nothing useful to do — the native world state is already finalized
399
+ // past this point and pruning has already happened.
400
+ const currentSummary = await this.merkleTreeDb.getStatusSummary();
401
+ if (blockNumber < currentSummary.oldestHistoricalBlock || blockNumber < 1) {
402
+ this.log.trace(
403
+ `Finalized block ${blockNumber} is older than the oldest available block ${currentSummary.oldestHistoricalBlock}. Skipping.`,
404
+ );
405
+ return;
406
+ }
363
407
  const summary = await this.merkleTreeDb.setFinalized(blockNumber);
364
408
  if (this.historyToKeep === undefined) {
365
409
  return;
366
410
  }
367
- const newHistoricBlock = summary.finalizedBlockNumber - this.historyToKeep + 1;
368
- if (newHistoricBlock <= 1) {
411
+ // Get the checkpointed block for the finalized block number
412
+ const finalisedCheckpoint = await this.l2BlockSource.getCheckpointedBlock(summary.finalizedBlockNumber);
413
+ if (finalisedCheckpoint === undefined) {
414
+ this.log.warn(
415
+ `Failed to retrieve checkpointed block for finalized block number: ${summary.finalizedBlockNumber}`,
416
+ );
417
+ return;
418
+ }
419
+ // Compute the required historic checkpoint number
420
+ const newHistoricCheckpointNumber = finalisedCheckpoint.checkpointNumber - this.historyToKeep + 1;
421
+ if (newHistoricCheckpointNumber <= 1) {
422
+ return;
423
+ }
424
+ // Retrieve the historic checkpoint
425
+ const historicCheckpoints = await this.l2BlockSource.getCheckpoints(
426
+ CheckpointNumber(newHistoricCheckpointNumber),
427
+ 1,
428
+ );
429
+ if (historicCheckpoints.length === 0 || historicCheckpoints[0] === undefined) {
430
+ this.log.warn(`Failed to retrieve checkpoint number ${newHistoricCheckpointNumber} from Archiver`);
369
431
  return;
370
432
  }
371
- this.log.verbose(`Pruning historic blocks to ${newHistoricBlock}`);
372
- const status = await this.merkleTreeDb.removeHistoricalBlocks(BlockNumber(newHistoricBlock));
433
+ const historicCheckpoint = historicCheckpoints[0];
434
+ if (historicCheckpoint.checkpoint.blocks.length === 0 || historicCheckpoint.checkpoint.blocks[0] === undefined) {
435
+ this.log.warn(`Retrieved checkpoint number ${newHistoricCheckpointNumber} has no blocks!`);
436
+ return;
437
+ }
438
+ // Find the block at the start of the checkpoint and remove blocks up to this one
439
+ const newHistoricBlock = historicCheckpoint.checkpoint.blocks[0];
440
+ if (newHistoricBlock.number <= currentSummary.oldestHistoricalBlock) {
441
+ this.log.debug(
442
+ `Historic block ${newHistoricBlock.number} is not newer than oldest available ${currentSummary.oldestHistoricalBlock}. Skipping prune.`,
443
+ );
444
+ return;
445
+ }
446
+ this.log.verbose(`Pruning historic blocks to ${newHistoricBlock.number}`);
447
+ const status = await this.merkleTreeDb.removeHistoricalBlocks(BlockNumber(newHistoricBlock.number));
373
448
  this.log.debug(`World state summary `, status.summary);
374
449
  }
375
450
 
@@ -380,9 +455,11 @@ export class ServerWorldStateSynchronizer
380
455
  }
381
456
 
382
457
  private async handleChainPruned(blockNumber: BlockNumber) {
383
- this.log.warn(`Chain pruned to block ${blockNumber}`);
458
+ this.log.info(`Chain pruned to block ${blockNumber}`);
384
459
  const status = await this.merkleTreeDb.unwindBlocks(blockNumber);
385
- this.provenBlockNumber = undefined;
460
+ if (this.provenBlockNumber !== undefined && this.provenBlockNumber > blockNumber) {
461
+ this.provenBlockNumber = undefined;
462
+ }
386
463
  this.instrumentation.updateWorldStateMetrics(status);
387
464
  }
388
465
 
package/src/testing.ts CHANGED
@@ -3,22 +3,19 @@ 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';
6
+ import type { GenesisData } from '@aztec/stdlib/world-state';
6
7
 
7
8
  import { NativeWorldStateService } from './native/index.js';
8
9
 
9
- async function generateGenesisValues(prefilledPublicData: PublicDataTreeLeaf[]) {
10
- if (!prefilledPublicData.length) {
10
+ async function generateGenesisValues(genesis: GenesisData) {
11
+ if (!genesis.prefilledPublicData.length && genesis.genesisTimestamp === 0n) {
11
12
  return {
12
13
  genesisArchiveRoot: new Fr(GENESIS_ARCHIVE_ROOT),
13
14
  };
14
15
  }
15
16
 
16
17
  // Create a temporary world state to compute the genesis values.
17
- const ws = await NativeWorldStateService.tmp(
18
- undefined /* rollupAddress */,
19
- true /* cleanupTmpDir */,
20
- prefilledPublicData,
21
- );
18
+ const ws = await NativeWorldStateService.tmp(undefined /* rollupAddress */, true /* cleanupTmpDir */, genesis);
22
19
  const genesisArchiveRoot = new Fr((await ws.getCommitted().getTreeInfo(MerkleTreeId.ARCHIVE)).root);
23
20
  await ws.close();
24
21
 
@@ -33,6 +30,7 @@ export async function getGenesisValues(
33
30
  initialAccounts: AztecAddress[],
34
31
  initialAccountFeeJuice = defaultInitialAccountFeeJuice,
35
32
  genesisPublicData: PublicDataTreeLeaf[] = [],
33
+ genesisTimestamp: bigint = 0n,
36
34
  ) {
37
35
  // Top up the accounts with fee juice.
38
36
  let prefilledPublicData = await Promise.all(
@@ -46,11 +44,12 @@ export async function getGenesisValues(
46
44
 
47
45
  prefilledPublicData.sort((a, b) => (b.slot.lt(a.slot) ? 1 : -1));
48
46
 
49
- const { genesisArchiveRoot } = await generateGenesisValues(prefilledPublicData);
47
+ const genesis: GenesisData = { prefilledPublicData, genesisTimestamp };
48
+ const { genesisArchiveRoot } = await generateGenesisValues(genesis);
50
49
 
51
50
  return {
52
51
  genesisArchiveRoot,
53
- prefilledPublicData,
52
+ genesis,
54
53
  fundingNeeded: BigInt(initialAccounts.length) * initialAccountFeeJuice.toBigInt(),
55
54
  };
56
55
  }
@@ -1,14 +1,12 @@
1
1
  import { MAX_NULLIFIERS_PER_TX, MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX } from '@aztec/constants';
2
2
  import type { BlockNumber } from '@aztec/foundation/branded-types';
3
3
  import type { Fr } from '@aztec/foundation/curves/bn254';
4
- import type { IndexedTreeSnapshot, TreeSnapshot } from '@aztec/merkle-tree';
5
4
  import type { L2Block } from '@aztec/stdlib/block';
6
5
  import type {
7
6
  ForkMerkleTreeOperations,
8
7
  MerkleTreeReadOperations,
9
8
  ReadonlyWorldStateAccess,
10
9
  } from '@aztec/stdlib/interfaces/server';
11
- import type { MerkleTreeId } from '@aztec/stdlib/trees';
12
10
 
13
11
  import type { WorldStateStatusFull, WorldStateStatusSummary } from '../native/message.js';
14
12
 
@@ -31,14 +29,6 @@ export const INITIAL_NULLIFIER_TREE_SIZE = 2 * MAX_NULLIFIERS_PER_TX;
31
29
 
32
30
  export const INITIAL_PUBLIC_DATA_TREE_SIZE = 2 * MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX;
33
31
 
34
- export type TreeSnapshots = {
35
- [MerkleTreeId.NULLIFIER_TREE]: IndexedTreeSnapshot;
36
- [MerkleTreeId.NOTE_HASH_TREE]: TreeSnapshot<Fr>;
37
- [MerkleTreeId.PUBLIC_DATA_TREE]: IndexedTreeSnapshot;
38
- [MerkleTreeId.L1_TO_L2_MESSAGE_TREE]: TreeSnapshot<Fr>;
39
- [MerkleTreeId.ARCHIVE]: TreeSnapshot<Fr>;
40
- };
41
-
42
32
  export interface MerkleTreeAdminDatabase extends ForkMerkleTreeOperations, ReadonlyWorldStateAccess {
43
33
  /**
44
34
  * Handles a single L2 block (i.e. Inserts the new note hashes into the merkle tree).