@aztec/world-state 0.0.1-commit.b655e406 → 0.0.1-commit.b6e433891

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 (59) hide show
  1. package/dest/index.d.ts +1 -1
  2. package/dest/instrumentation/instrumentation.d.ts +1 -1
  3. package/dest/instrumentation/instrumentation.d.ts.map +1 -1
  4. package/dest/instrumentation/instrumentation.js +17 -41
  5. package/dest/native/bench_metrics.d.ts +1 -1
  6. package/dest/native/bench_metrics.d.ts.map +1 -1
  7. package/dest/native/fork_checkpoint.d.ts +7 -1
  8. package/dest/native/fork_checkpoint.d.ts.map +1 -1
  9. package/dest/native/fork_checkpoint.js +15 -3
  10. package/dest/native/index.d.ts +1 -1
  11. package/dest/native/merkle_trees_facade.d.ts +15 -9
  12. package/dest/native/merkle_trees_facade.d.ts.map +1 -1
  13. package/dest/native/merkle_trees_facade.js +47 -13
  14. package/dest/native/message.d.ts +25 -15
  15. package/dest/native/message.d.ts.map +1 -1
  16. package/dest/native/message.js +14 -13
  17. package/dest/native/native_world_state.d.ts +15 -12
  18. package/dest/native/native_world_state.d.ts.map +1 -1
  19. package/dest/native/native_world_state.js +23 -18
  20. package/dest/native/native_world_state_instance.d.ts +12 -3
  21. package/dest/native/native_world_state_instance.d.ts.map +1 -1
  22. package/dest/native/native_world_state_instance.js +25 -4
  23. package/dest/native/world_state_ops_queue.d.ts +1 -1
  24. package/dest/native/world_state_ops_queue.d.ts.map +1 -1
  25. package/dest/synchronizer/config.d.ts +3 -5
  26. package/dest/synchronizer/config.d.ts.map +1 -1
  27. package/dest/synchronizer/config.js +7 -9
  28. package/dest/synchronizer/errors.d.ts +1 -1
  29. package/dest/synchronizer/errors.d.ts.map +1 -1
  30. package/dest/synchronizer/factory.d.ts +5 -4
  31. package/dest/synchronizer/factory.d.ts.map +1 -1
  32. package/dest/synchronizer/factory.js +5 -5
  33. package/dest/synchronizer/index.d.ts +1 -1
  34. package/dest/synchronizer/server_world_state_synchronizer.d.ts +11 -28
  35. package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -1
  36. package/dest/synchronizer/server_world_state_synchronizer.js +149 -80
  37. package/dest/test/index.d.ts +1 -1
  38. package/dest/test/utils.d.ts +12 -5
  39. package/dest/test/utils.d.ts.map +1 -1
  40. package/dest/test/utils.js +54 -50
  41. package/dest/testing.d.ts +2 -2
  42. package/dest/testing.d.ts.map +1 -1
  43. package/dest/testing.js +1 -1
  44. package/dest/world-state-db/index.d.ts +1 -1
  45. package/dest/world-state-db/merkle_tree_db.d.ts +9 -19
  46. package/dest/world-state-db/merkle_tree_db.d.ts.map +1 -1
  47. package/package.json +12 -12
  48. package/src/instrumentation/instrumentation.ts +17 -41
  49. package/src/native/fork_checkpoint.ts +19 -3
  50. package/src/native/merkle_trees_facade.ts +55 -14
  51. package/src/native/message.ts +38 -26
  52. package/src/native/native_world_state.ts +52 -28
  53. package/src/native/native_world_state_instance.ts +34 -4
  54. package/src/synchronizer/config.ts +8 -19
  55. package/src/synchronizer/factory.ts +8 -2
  56. package/src/synchronizer/server_world_state_synchronizer.ts +176 -105
  57. package/src/test/utils.ts +87 -92
  58. package/src/testing.ts +1 -1
  59. package/src/world-state-db/merkle_tree_db.ts +12 -19
@@ -1,8 +1,7 @@
1
- import type { Fr } from '@aztec/foundation/fields';
2
- import type { IndexedTreeSnapshot, TreeSnapshot } from '@aztec/merkle-tree';
1
+ import type { BlockNumber } from '@aztec/foundation/branded-types';
2
+ import type { Fr } from '@aztec/foundation/curves/bn254';
3
3
  import type { L2Block } from '@aztec/stdlib/block';
4
- import type { ForkMerkleTreeOperations, MerkleTreeReadOperations } from '@aztec/stdlib/interfaces/server';
5
- import type { MerkleTreeId } from '@aztec/stdlib/trees';
4
+ import type { ForkMerkleTreeOperations, MerkleTreeReadOperations, ReadonlyWorldStateAccess } from '@aztec/stdlib/interfaces/server';
6
5
  import type { WorldStateStatusFull, WorldStateStatusSummary } from '../native/message.js';
7
6
  /**
8
7
  *
@@ -21,22 +20,13 @@ import type { WorldStateStatusFull, WorldStateStatusSummary } from '../native/me
21
20
  */
22
21
  export declare const INITIAL_NULLIFIER_TREE_SIZE: number;
23
22
  export declare const INITIAL_PUBLIC_DATA_TREE_SIZE: number;
24
- export type TreeSnapshots = {
25
- [MerkleTreeId.NULLIFIER_TREE]: IndexedTreeSnapshot;
26
- [MerkleTreeId.NOTE_HASH_TREE]: TreeSnapshot<Fr>;
27
- [MerkleTreeId.PUBLIC_DATA_TREE]: IndexedTreeSnapshot;
28
- [MerkleTreeId.L1_TO_L2_MESSAGE_TREE]: TreeSnapshot<Fr>;
29
- [MerkleTreeId.ARCHIVE]: TreeSnapshot<Fr>;
30
- };
31
- export interface MerkleTreeAdminDatabase extends ForkMerkleTreeOperations {
23
+ export interface MerkleTreeAdminDatabase extends ForkMerkleTreeOperations, ReadonlyWorldStateAccess {
32
24
  /**
33
25
  * Handles a single L2 block (i.e. Inserts the new note hashes into the merkle tree).
34
26
  * @param block - The L2 block to handle.
35
27
  * @param l1ToL2Messages - The L1 to L2 messages for the block.
36
- * @param isFirstBlock - Whether the block is the first block in a checkpoint. Temporary hack to only insert l1 to l2
37
- * messages for the first block in a checkpoint. TODO(#17027) Remove this.
38
28
  */
39
- handleL2BlockAndMessages(block: L2Block, l1ToL2Messages: Fr[], isFirstBlock?: boolean): Promise<WorldStateStatusFull>;
29
+ handleL2BlockAndMessages(block: L2Block, l1ToL2Messages: Fr[]): Promise<WorldStateStatusFull>;
40
30
  /**
41
31
  * Gets a handle that allows reading the latest committed state
42
32
  */
@@ -46,19 +36,19 @@ export interface MerkleTreeAdminDatabase extends ForkMerkleTreeOperations {
46
36
  * @param toBlockNumber The block number of the new oldest historical block
47
37
  * @returns The new WorldStateStatus
48
38
  */
49
- removeHistoricalBlocks(toBlockNumber: bigint): Promise<WorldStateStatusFull>;
39
+ removeHistoricalBlocks(toBlockNumber: BlockNumber): Promise<WorldStateStatusFull>;
50
40
  /**
51
41
  * Removes all pending blocks down to but not including the given block number
52
42
  * @param toBlockNumber The block number of the new tip of the pending chain,
53
43
  * @returns The new WorldStateStatus
54
44
  */
55
- unwindBlocks(toBlockNumber: bigint): Promise<WorldStateStatusFull>;
45
+ unwindBlocks(toBlockNumber: BlockNumber): Promise<WorldStateStatusFull>;
56
46
  /**
57
47
  * Advances the finalized block number to be the number provided
58
48
  * @param toBlockNumber The block number that is now the tip of the finalized chain
59
49
  * @returns The new WorldStateStatus
60
50
  */
61
- setFinalized(toBlockNumber: bigint): Promise<WorldStateStatusSummary>;
51
+ setFinalized(toBlockNumber: BlockNumber): Promise<WorldStateStatusSummary>;
62
52
  /**
63
53
  * Gets the current status summary of the database.
64
54
  * @returns The current WorldStateStatus.
@@ -69,4 +59,4 @@ export interface MerkleTreeAdminDatabase extends ForkMerkleTreeOperations {
69
59
  /** Deletes the db. */
70
60
  clear(): Promise<void>;
71
61
  }
72
- //# sourceMappingURL=merkle_tree_db.d.ts.map
62
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWVya2xlX3RyZWVfZGIuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy93b3JsZC1zdGF0ZS1kYi9tZXJrbGVfdHJlZV9kYi50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFDQSxPQUFPLEtBQUssRUFBRSxXQUFXLEVBQUUsTUFBTSxpQ0FBaUMsQ0FBQztBQUNuRSxPQUFPLEtBQUssRUFBRSxFQUFFLEVBQUUsTUFBTSxnQ0FBZ0MsQ0FBQztBQUN6RCxPQUFPLEtBQUssRUFBRSxPQUFPLEVBQUUsTUFBTSxxQkFBcUIsQ0FBQztBQUNuRCxPQUFPLEtBQUssRUFDVix3QkFBd0IsRUFDeEIsd0JBQXdCLEVBQ3hCLHdCQUF3QixFQUN6QixNQUFNLGlDQUFpQyxDQUFDO0FBRXpDLE9BQU8sS0FBSyxFQUFFLG9CQUF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,YAAY,CAAC,aAAa,EAAE,WAAW,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAAC;IAExE;;;;OAIG;IACH,YAAY,CAAC,aAAa,EAAE,WAAW,GAAG,OAAO,CAAC,uBAAuB,CAAC,CAAC;IAE3E;;;OAGG;IACH,gBAAgB,IAAI,OAAO,CAAC,uBAAuB,CAAC,CAAC;IAErD,yBAAyB;IACzB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAEvB,sBAAsB;IACtB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aztec/world-state",
3
- "version": "0.0.1-commit.b655e406",
3
+ "version": "0.0.1-commit.b6e433891",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./dest/index.js",
@@ -18,8 +18,8 @@
18
18
  "tsconfig": "./tsconfig.json"
19
19
  },
20
20
  "scripts": {
21
- "build": "yarn clean && tsc -b",
22
- "build:dev": "tsc -b --watch",
21
+ "build": "yarn clean && ../scripts/tsc.sh",
22
+ "build:dev": "../scripts/tsc.sh --watch",
23
23
  "clean": "rm -rf ./dest .tsbuildinfo",
24
24
  "test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules ../node_modules/.bin/jest --passWithNoTests --maxWorkers=${JEST_MAX_WORKERS:-8}"
25
25
  },
@@ -64,22 +64,22 @@
64
64
  ]
65
65
  },
66
66
  "dependencies": {
67
- "@aztec/constants": "0.0.1-commit.b655e406",
68
- "@aztec/foundation": "0.0.1-commit.b655e406",
69
- "@aztec/kv-store": "0.0.1-commit.b655e406",
70
- "@aztec/merkle-tree": "0.0.1-commit.b655e406",
71
- "@aztec/native": "0.0.1-commit.b655e406",
72
- "@aztec/protocol-contracts": "0.0.1-commit.b655e406",
73
- "@aztec/stdlib": "0.0.1-commit.b655e406",
74
- "@aztec/telemetry-client": "0.0.1-commit.b655e406",
67
+ "@aztec/constants": "0.0.1-commit.b6e433891",
68
+ "@aztec/foundation": "0.0.1-commit.b6e433891",
69
+ "@aztec/kv-store": "0.0.1-commit.b6e433891",
70
+ "@aztec/native": "0.0.1-commit.b6e433891",
71
+ "@aztec/protocol-contracts": "0.0.1-commit.b6e433891",
72
+ "@aztec/stdlib": "0.0.1-commit.b6e433891",
73
+ "@aztec/telemetry-client": "0.0.1-commit.b6e433891",
75
74
  "tslib": "^2.4.0",
76
75
  "zod": "^3.23.8"
77
76
  },
78
77
  "devDependencies": {
79
- "@aztec/archiver": "0.0.1-commit.b655e406",
78
+ "@aztec/archiver": "0.0.1-commit.b6e433891",
80
79
  "@jest/globals": "^30.0.0",
81
80
  "@types/jest": "^30.0.0",
82
81
  "@types/node": "^22.15.17",
82
+ "@typescript/native-preview": "7.0.0-dev.20260113.1",
83
83
  "jest": "^30.0.0",
84
84
  "jest-mock-extended": "^4.0.0",
85
85
  "ts-node": "^10.9.1",
@@ -7,7 +7,7 @@ import {
7
7
  Metrics,
8
8
  type TelemetryClient,
9
9
  type UpDownCounter,
10
- ValueType,
10
+ createUpDownCounterWithDefault,
11
11
  } from '@aztec/telemetry-client';
12
12
 
13
13
  import {
@@ -46,55 +46,31 @@ export class WorldStateInstrumentation {
46
46
  private log: Logger = createLogger('world-state:instrumentation'),
47
47
  ) {
48
48
  const meter = telemetry.getMeter('World State');
49
- this.dbMapSize = meter.createGauge(Metrics.WORLD_STATE_DB_MAP_SIZE, {
50
- description: `The current configured map size for each merkle tree`,
51
- valueType: ValueType.INT,
52
- });
49
+ this.dbMapSize = meter.createGauge(Metrics.WORLD_STATE_DB_MAP_SIZE);
53
50
 
54
- this.dbPhysicalSize = meter.createGauge(Metrics.WORLD_STATE_DB_PHYSICAL_SIZE, {
55
- description: `The current physical disk space used for each database`,
56
- valueType: ValueType.INT,
57
- });
51
+ this.dbPhysicalSize = meter.createGauge(Metrics.WORLD_STATE_DB_PHYSICAL_SIZE);
58
52
 
59
- this.treeSize = meter.createGauge(Metrics.WORLD_STATE_TREE_SIZE, {
60
- description: `The current number of leaves in each merkle tree`,
61
- valueType: ValueType.INT,
62
- });
53
+ this.treeSize = meter.createGauge(Metrics.WORLD_STATE_TREE_SIZE);
63
54
 
64
- this.unfinalizedHeight = meter.createGauge(Metrics.WORLD_STATE_UNFINALIZED_HEIGHT, {
65
- description: `The unfinalized block height of each merkle tree`,
66
- valueType: ValueType.INT,
67
- });
55
+ this.unfinalizedHeight = meter.createGauge(Metrics.WORLD_STATE_UNFINALIZED_HEIGHT);
68
56
 
69
- this.finalizedHeight = meter.createGauge(Metrics.WORLD_STATE_FINALIZED_HEIGHT, {
70
- description: `The finalized block height of each merkle tree`,
71
- valueType: ValueType.INT,
72
- });
57
+ this.finalizedHeight = meter.createGauge(Metrics.WORLD_STATE_FINALIZED_HEIGHT);
73
58
 
74
- this.oldestBlock = meter.createGauge(Metrics.WORLD_STATE_OLDEST_BLOCK, {
75
- description: `The oldest historical block of each merkle tree`,
76
- valueType: ValueType.INT,
77
- });
59
+ this.oldestBlock = meter.createGauge(Metrics.WORLD_STATE_OLDEST_BLOCK);
78
60
 
79
- this.dbUsedSize = meter.createGauge(Metrics.WORLD_STATE_DB_USED_SIZE, {
80
- description: `The current used database size for each db of each merkle tree`,
81
- valueType: ValueType.INT,
82
- });
61
+ this.dbUsedSize = meter.createGauge(Metrics.WORLD_STATE_DB_USED_SIZE);
83
62
 
84
- this.dbNumItems = meter.createGauge(Metrics.WORLD_STATE_DB_NUM_ITEMS, {
85
- description: `The current number of items in each database of each merkle tree`,
86
- valueType: ValueType.INT,
87
- });
63
+ this.dbNumItems = meter.createGauge(Metrics.WORLD_STATE_DB_NUM_ITEMS);
88
64
 
89
- this.requestHistogram = meter.createHistogram(Metrics.WORLD_STATE_REQUEST_TIME, {
90
- description: 'The round trip time of world state requests',
91
- unit: 'us',
92
- valueType: ValueType.INT,
93
- });
65
+ this.requestHistogram = meter.createHistogram(Metrics.WORLD_STATE_REQUEST_TIME);
94
66
 
95
- this.criticalErrors = meter.createUpDownCounter(Metrics.WORLD_STATE_CRITICAL_ERROR_COUNT, {
96
- description: 'The number of critical errors in the world state',
97
- valueType: ValueType.INT,
67
+ this.criticalErrors = createUpDownCounterWithDefault(meter, Metrics.WORLD_STATE_CRITICAL_ERROR_COUNT, {
68
+ [Attributes.ERROR_TYPE]: [
69
+ 'synch_pending_block',
70
+ 'finalize_block',
71
+ 'prune_pending_block',
72
+ 'prune_historical_block',
73
+ ],
98
74
  });
99
75
  }
100
76
 
@@ -3,11 +3,14 @@ import type { MerkleTreeCheckpointOperations } from '@aztec/stdlib/interfaces/se
3
3
  export class ForkCheckpoint {
4
4
  private completed = false;
5
5
 
6
- private constructor(private readonly fork: MerkleTreeCheckpointOperations) {}
6
+ private constructor(
7
+ private readonly fork: MerkleTreeCheckpointOperations,
8
+ public readonly depth: number,
9
+ ) {}
7
10
 
8
11
  static async new(fork: MerkleTreeCheckpointOperations): Promise<ForkCheckpoint> {
9
- await fork.createCheckpoint();
10
- return new ForkCheckpoint(fork);
12
+ const depth = await fork.createCheckpoint();
13
+ return new ForkCheckpoint(fork, depth);
11
14
  }
12
15
 
13
16
  async commit(): Promise<void> {
@@ -27,4 +30,17 @@ export class ForkCheckpoint {
27
30
  await this.fork.revertCheckpoint();
28
31
  this.completed = true;
29
32
  }
33
+
34
+ /**
35
+ * Reverts this checkpoint and any nested checkpoints created on top of it,
36
+ * leaving the checkpoint depth at the level it was before this checkpoint was created.
37
+ */
38
+ async revertToCheckpoint(): Promise<void> {
39
+ if (this.completed) {
40
+ return;
41
+ }
42
+
43
+ await this.fork.revertAllCheckpointsTo(this.depth - 1);
44
+ this.completed = true;
45
+ }
30
46
  }
@@ -1,5 +1,8 @@
1
- import { Fr } from '@aztec/foundation/fields';
1
+ import { BlockNumber } from '@aztec/foundation/branded-types';
2
+ import { Fr } from '@aztec/foundation/curves/bn254';
3
+ import { createLogger } from '@aztec/foundation/log';
2
4
  import { serializeToBuffer } from '@aztec/foundation/serialize';
5
+ import { sleep } from '@aztec/foundation/sleep';
3
6
  import { type IndexedTreeLeafPreimage, SiblingPath } from '@aztec/foundation/trees';
4
7
  import type {
5
8
  BatchInsertionResult,
@@ -18,7 +21,7 @@ import {
18
21
  PublicDataTreeLeafPreimage,
19
22
  } from '@aztec/stdlib/trees';
20
23
  import { type BlockHeader, PartialStateReference, StateReference } from '@aztec/stdlib/tx';
21
- import type { WorldStateRevision } from '@aztec/stdlib/world-state';
24
+ import { type WorldStateRevision, WorldStateRevisionWithHandle } from '@aztec/stdlib/world-state';
22
25
 
23
26
  import assert from 'assert';
24
27
 
@@ -42,8 +45,8 @@ export class MerkleTreesFacade implements MerkleTreeReadOperations {
42
45
  return this.initialHeader;
43
46
  }
44
47
 
45
- getRevision(): WorldStateRevision {
46
- return this.revision;
48
+ getRevision(): WorldStateRevisionWithHandle {
49
+ return WorldStateRevisionWithHandle.fromWorldStateRevision(this.revision, this.instance.getHandle());
47
50
  }
48
51
 
49
52
  findLeafIndices(treeId: MerkleTreeId, values: MerkleTreeLeafType<MerkleTreeId>[]): Promise<(bigint | undefined)[]> {
@@ -191,19 +194,26 @@ export class MerkleTreesFacade implements MerkleTreeReadOperations {
191
194
  async getBlockNumbersForLeafIndices<ID extends MerkleTreeId>(
192
195
  treeId: ID,
193
196
  leafIndices: bigint[],
194
- ): Promise<(bigint | undefined)[]> {
197
+ ): Promise<(BlockNumber | undefined)[]> {
195
198
  const response = await this.instance.call(WorldStateMessageType.GET_BLOCK_NUMBERS_FOR_LEAF_INDICES, {
196
199
  treeId,
197
200
  revision: this.revision,
198
201
  leafIndices,
199
202
  });
200
203
 
201
- return response.blockNumbers.map(x => (x === undefined || x === null ? undefined : BigInt(x)));
204
+ return response.blockNumbers.map(x => (x === undefined || x === null ? undefined : BlockNumber(Number(x))));
202
205
  }
203
206
  }
204
207
 
205
208
  export class MerkleTreesForkFacade extends MerkleTreesFacade implements MerkleTreeWriteOperations {
206
- constructor(instance: NativeWorldStateInstance, initialHeader: BlockHeader, revision: WorldStateRevision) {
209
+ private log = createLogger('world-state:merkle-trees-fork-facade');
210
+
211
+ constructor(
212
+ instance: NativeWorldStateInstance,
213
+ initialHeader: BlockHeader,
214
+ revision: WorldStateRevision,
215
+ private opts: { closeDelayMs?: number },
216
+ ) {
207
217
  assert.notEqual(revision.forkId, 0, 'Fork ID must be set');
208
218
  assert.equal(revision.includeUncommitted, true, 'Fork must include uncommitted data');
209
219
  super(instance, initialHeader, revision);
@@ -282,12 +292,37 @@ export class MerkleTreesForkFacade extends MerkleTreesFacade implements MerkleTr
282
292
 
283
293
  public async close(): Promise<void> {
284
294
  assert.notEqual(this.revision.forkId, 0, 'Fork ID must be set');
285
- await this.instance.call(WorldStateMessageType.DELETE_FORK, { forkId: this.revision.forkId });
295
+ try {
296
+ await this.instance.call(WorldStateMessageType.DELETE_FORK, { forkId: this.revision.forkId });
297
+ } catch (err: any) {
298
+ // Ignore errors due to native instance being closed during shutdown.
299
+ // This can happen when validators are still processing block proposals while the node is stopping.
300
+ if (err?.message === 'Native instance is closed') {
301
+ return;
302
+ }
303
+ throw err;
304
+ }
286
305
  }
287
306
 
288
- public async createCheckpoint(): Promise<void> {
307
+ async [Symbol.asyncDispose](): Promise<void> {
308
+ if (this.opts.closeDelayMs) {
309
+ void sleep(this.opts.closeDelayMs)
310
+ .then(() => this.close())
311
+ .catch(err => {
312
+ if (err && 'message' in err && err.message === 'Native instance is closed') {
313
+ return; // Ignore errors due to native instance being closed
314
+ }
315
+ this.log.warn('Error closing MerkleTreesForkFacade after delay', { err });
316
+ });
317
+ } else {
318
+ await this.close();
319
+ }
320
+ }
321
+
322
+ public async createCheckpoint(): Promise<number> {
289
323
  assert.notEqual(this.revision.forkId, 0, 'Fork ID must be set');
290
- await this.instance.call(WorldStateMessageType.CREATE_CHECKPOINT, { forkId: this.revision.forkId });
324
+ const resp = await this.instance.call(WorldStateMessageType.CREATE_CHECKPOINT, { forkId: this.revision.forkId });
325
+ return resp.depth;
291
326
  }
292
327
 
293
328
  public async commitCheckpoint(): Promise<void> {
@@ -300,14 +335,20 @@ export class MerkleTreesForkFacade extends MerkleTreesFacade implements MerkleTr
300
335
  await this.instance.call(WorldStateMessageType.REVERT_CHECKPOINT, { forkId: this.revision.forkId });
301
336
  }
302
337
 
303
- public async commitAllCheckpoints(): Promise<void> {
338
+ public async commitAllCheckpointsTo(depth: number): Promise<void> {
304
339
  assert.notEqual(this.revision.forkId, 0, 'Fork ID must be set');
305
- await this.instance.call(WorldStateMessageType.COMMIT_ALL_CHECKPOINTS, { forkId: this.revision.forkId });
340
+ await this.instance.call(WorldStateMessageType.COMMIT_ALL_CHECKPOINTS, {
341
+ forkId: this.revision.forkId,
342
+ depth,
343
+ });
306
344
  }
307
345
 
308
- public async revertAllCheckpoints(): Promise<void> {
346
+ public async revertAllCheckpointsTo(depth: number): Promise<void> {
309
347
  assert.notEqual(this.revision.forkId, 0, 'Fork ID must be set');
310
- await this.instance.call(WorldStateMessageType.REVERT_ALL_CHECKPOINTS, { forkId: this.revision.forkId });
348
+ await this.instance.call(WorldStateMessageType.REVERT_ALL_CHECKPOINTS, {
349
+ forkId: this.revision.forkId,
350
+ depth,
351
+ });
311
352
  }
312
353
  }
313
354
 
@@ -1,5 +1,7 @@
1
- import { Fr } from '@aztec/foundation/fields';
1
+ import { BlockNumber } from '@aztec/foundation/branded-types';
2
+ import { Fr } from '@aztec/foundation/curves/bn254';
2
3
  import type { Tuple } from '@aztec/foundation/serialize';
4
+ import type { BlockHash } from '@aztec/stdlib/block';
3
5
  import { AppendOnlyTreeSnapshot, MerkleTreeId } from '@aztec/stdlib/trees';
4
6
  import type { StateReference } from '@aztec/stdlib/tx';
5
7
  import type { UInt32 } from '@aztec/stdlib/types';
@@ -56,11 +58,11 @@ interface WithTreeId {
56
58
 
57
59
  export interface WorldStateStatusSummary {
58
60
  /** Last block number that can still be unwound. */
59
- unfinalizedBlockNumber: bigint;
61
+ unfinalizedBlockNumber: BlockNumber;
60
62
  /** Last block number that is finalized and cannot be unwound. */
61
- finalizedBlockNumber: bigint;
63
+ finalizedBlockNumber: BlockNumber;
62
64
  /** Oldest block still available for historical queries and forks. */
63
- oldestHistoricalBlock: bigint;
65
+ oldestHistoricalBlock: BlockNumber;
64
66
  /** Whether the trees are in sync with each other */
65
67
  treesAreSynched: boolean;
66
68
  }
@@ -81,11 +83,11 @@ export interface TreeMeta {
81
83
  /** The tree's initial root value */
82
84
  initialRoot: Fr;
83
85
  /** The current oldest historical block number of the tree */
84
- oldestHistoricBlock: bigint;
86
+ oldestHistoricBlock: BlockNumber;
85
87
  /** The current unfinalized block number of the tree */
86
- unfinalizedBlockHeight: bigint;
88
+ unfinalizedBlockHeight: BlockNumber;
87
89
  /** The current finalized block number of the tree */
88
- finalizedBlockHeight: bigint;
90
+ finalizedBlockHeight: BlockNumber;
89
91
  }
90
92
 
91
93
  export interface DBStats {
@@ -173,9 +175,9 @@ export function buildEmptyTreeMeta() {
173
175
  depth: 0,
174
176
  size: 0n,
175
177
  committedSize: 0n,
176
- unfinalizedBlockHeight: 0n,
177
- finalizedBlockHeight: 0n,
178
- oldestHistoricBlock: 0n,
178
+ unfinalizedBlockHeight: BlockNumber.ZERO,
179
+ finalizedBlockHeight: BlockNumber.ZERO,
180
+ oldestHistoricBlock: BlockNumber.ZERO,
179
181
  root: Fr.ZERO,
180
182
  initialRoot: Fr.ZERO,
181
183
  initialSize: 0n,
@@ -204,9 +206,9 @@ export function buildEmptyWorldStateDBStats() {
204
206
 
205
207
  export function buildEmptyWorldStateSummary() {
206
208
  return {
207
- unfinalizedBlockNumber: 0n,
208
- finalizedBlockNumber: 0n,
209
- oldestHistoricalBlock: 0n,
209
+ unfinalizedBlockNumber: BlockNumber.ZERO,
210
+ finalizedBlockNumber: BlockNumber.ZERO,
211
+ oldestHistoricalBlock: BlockNumber.ZERO,
210
212
  treesAreSynched: true,
211
213
  } as WorldStateStatusSummary;
212
214
  }
@@ -220,9 +222,9 @@ export function buildEmptyWorldStateStatusFull() {
220
222
  }
221
223
 
222
224
  export function sanitizeSummary(summary: WorldStateStatusSummary) {
223
- summary.finalizedBlockNumber = BigInt(summary.finalizedBlockNumber);
224
- summary.unfinalizedBlockNumber = BigInt(summary.unfinalizedBlockNumber);
225
- summary.oldestHistoricalBlock = BigInt(summary.oldestHistoricalBlock);
225
+ summary.finalizedBlockNumber = BlockNumber.fromBigInt(BigInt(summary.finalizedBlockNumber));
226
+ summary.unfinalizedBlockNumber = BlockNumber.fromBigInt(BigInt(summary.unfinalizedBlockNumber));
227
+ summary.oldestHistoricalBlock = BlockNumber.fromBigInt(BigInt(summary.oldestHistoricalBlock));
226
228
  return summary;
227
229
  }
228
230
 
@@ -234,11 +236,11 @@ export function sanitizeDBStats(stats: DBStats) {
234
236
 
235
237
  export function sanitizeMeta(meta: TreeMeta) {
236
238
  meta.committedSize = BigInt(meta.committedSize);
237
- meta.finalizedBlockHeight = BigInt(meta.finalizedBlockHeight);
239
+ meta.finalizedBlockHeight = BlockNumber.fromBigInt(BigInt(meta.finalizedBlockHeight));
238
240
  meta.initialSize = BigInt(meta.initialSize);
239
- meta.oldestHistoricBlock = BigInt(meta.oldestHistoricBlock);
241
+ meta.oldestHistoricBlock = BlockNumber.fromBigInt(BigInt(meta.oldestHistoricBlock));
240
242
  meta.size = BigInt(meta.size);
241
- meta.unfinalizedBlockHeight = BigInt(meta.unfinalizedBlockHeight);
243
+ meta.unfinalizedBlockHeight = BlockNumber.fromBigInt(BigInt(meta.unfinalizedBlockHeight));
242
244
  return meta;
243
245
  }
244
246
 
@@ -282,6 +284,16 @@ interface WithForkId {
282
284
  forkId: number;
283
285
  }
284
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
+
285
297
  interface WithWorldStateRevision {
286
298
  revision: WorldStateRevision;
287
299
  }
@@ -310,7 +322,7 @@ interface WithLeafValues {
310
322
  }
311
323
 
312
324
  interface BlockShiftRequest extends WithCanonicalForkId {
313
- toBlockNumber: bigint;
325
+ toBlockNumber: BlockNumber;
314
326
  }
315
327
 
316
328
  interface WithLeaves {
@@ -409,9 +421,9 @@ interface UpdateArchiveRequest extends WithForkId {
409
421
  }
410
422
 
411
423
  interface SyncBlockRequest extends WithCanonicalForkId {
412
- blockNumber: number;
424
+ blockNumber: BlockNumber;
413
425
  blockStateRef: BlockStateReference;
414
- blockHeaderHash: Fr;
426
+ blockHeaderHash: BlockHash;
415
427
  paddedNoteHashes: readonly SerializedLeafValue[];
416
428
  paddedL1ToL2Messages: readonly SerializedLeafValue[];
417
429
  paddedNullifiers: readonly SerializedLeafValue[];
@@ -420,7 +432,7 @@ interface SyncBlockRequest extends WithCanonicalForkId {
420
432
 
421
433
  interface CreateForkRequest extends WithCanonicalForkId {
422
434
  latest: boolean;
423
- blockNumber: number;
435
+ blockNumber: BlockNumber;
424
436
  }
425
437
 
426
438
  interface CreateForkResponse {
@@ -485,8 +497,8 @@ export type WorldStateRequest = {
485
497
  [WorldStateMessageType.CREATE_CHECKPOINT]: WithForkId;
486
498
  [WorldStateMessageType.COMMIT_CHECKPOINT]: WithForkId;
487
499
  [WorldStateMessageType.REVERT_CHECKPOINT]: WithForkId;
488
- [WorldStateMessageType.COMMIT_ALL_CHECKPOINTS]: WithForkId;
489
- [WorldStateMessageType.REVERT_ALL_CHECKPOINTS]: WithForkId;
500
+ [WorldStateMessageType.COMMIT_ALL_CHECKPOINTS]: CheckpointDepthRequest;
501
+ [WorldStateMessageType.REVERT_ALL_CHECKPOINTS]: CheckpointDepthRequest;
490
502
 
491
503
  [WorldStateMessageType.COPY_STORES]: CopyStoresRequest;
492
504
 
@@ -527,7 +539,7 @@ export type WorldStateResponse = {
527
539
 
528
540
  [WorldStateMessageType.GET_STATUS]: WorldStateStatusSummary;
529
541
 
530
- [WorldStateMessageType.CREATE_CHECKPOINT]: void;
542
+ [WorldStateMessageType.CREATE_CHECKPOINT]: CreateCheckpointResponse;
531
543
  [WorldStateMessageType.COMMIT_CHECKPOINT]: void;
532
544
  [WorldStateMessageType.REVERT_CHECKPOINT]: void;
533
545
  [WorldStateMessageType.COMMIT_ALL_CHECKPOINTS]: void;