@aztec/world-state 0.0.1-commit.2f68f620 → 0.0.1-commit.3100065

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 (57) hide show
  1. package/dest/instrumentation/instrumentation.d.ts +3 -4
  2. package/dest/instrumentation/instrumentation.d.ts.map +1 -1
  3. package/dest/instrumentation/instrumentation.js +9 -4
  4. package/dest/native/ipc_world_state_instance.d.ts +22 -59
  5. package/dest/native/ipc_world_state_instance.d.ts.map +1 -1
  6. package/dest/native/ipc_world_state_instance.js +495 -520
  7. package/dest/native/merkle_trees_facade.d.ts +3 -4
  8. package/dest/native/merkle_trees_facade.d.ts.map +1 -1
  9. package/dest/native/merkle_trees_facade.js +109 -37
  10. package/dest/native/message.d.ts +233 -14
  11. package/dest/native/message.d.ts.map +1 -1
  12. package/dest/native/message.js +42 -0
  13. package/dest/native/native_world_state.d.ts +28 -14
  14. package/dest/native/native_world_state.d.ts.map +1 -1
  15. package/dest/native/native_world_state.js +102 -101
  16. package/dest/native/native_world_state_instance.d.ts +42 -58
  17. package/dest/native/native_world_state_instance.d.ts.map +1 -1
  18. package/dest/native/native_world_state_instance.js +222 -5
  19. package/dest/native/world_state_ops_queue.d.ts +19 -0
  20. package/dest/native/world_state_ops_queue.d.ts.map +1 -0
  21. package/dest/native/world_state_ops_queue.js +146 -0
  22. package/dest/synchronizer/errors.d.ts +8 -1
  23. package/dest/synchronizer/errors.d.ts.map +1 -1
  24. package/dest/synchronizer/errors.js +8 -1
  25. package/dest/synchronizer/factory.d.ts +1 -1
  26. package/dest/synchronizer/factory.d.ts.map +1 -1
  27. package/dest/synchronizer/factory.js +1 -1
  28. package/dest/synchronizer/index.d.ts +2 -1
  29. package/dest/synchronizer/index.d.ts.map +1 -1
  30. package/dest/synchronizer/index.js +1 -0
  31. package/dest/synchronizer/server_world_state_synchronizer.d.ts +11 -6
  32. package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -1
  33. package/dest/synchronizer/server_world_state_synchronizer.js +55 -43
  34. package/dest/test/utils.d.ts +1 -1
  35. package/dest/test/utils.d.ts.map +1 -1
  36. package/dest/test/utils.js +6 -1
  37. package/dest/testing.d.ts +2 -2
  38. package/dest/testing.d.ts.map +1 -1
  39. package/dest/testing.js +15 -4
  40. package/package.json +10 -12
  41. package/src/instrumentation/instrumentation.ts +18 -6
  42. package/src/native/ipc_world_state_instance.ts +503 -608
  43. package/src/native/merkle_trees_facade.ts +104 -59
  44. package/src/native/message.ts +300 -13
  45. package/src/native/native_world_state.ts +156 -121
  46. package/src/native/native_world_state_instance.ts +307 -96
  47. package/src/native/world_state_ops_queue.ts +190 -0
  48. package/src/synchronizer/errors.ts +8 -0
  49. package/src/synchronizer/factory.ts +1 -0
  50. package/src/synchronizer/index.ts +1 -0
  51. package/src/synchronizer/server_world_state_synchronizer.ts +63 -30
  52. package/src/test/utils.ts +10 -1
  53. package/src/testing.ts +14 -4
  54. package/dest/native/world_state_operation.d.ts +0 -7
  55. package/dest/native/world_state_operation.d.ts.map +0 -1
  56. package/dest/native/world_state_operation.js +0 -5
  57. package/src/native/world_state_operation.ts +0 -33
@@ -22,13 +22,14 @@ import {
22
22
  PublicDataTreeLeafPreimage,
23
23
  } from '@aztec/stdlib/trees';
24
24
  import { type BlockHeader, PartialStateReference, StateReference } from '@aztec/stdlib/tx';
25
- import type { WorldStateRevision } from '@aztec/stdlib/world-state';
25
+ import { type WorldStateRevision, WorldStateRevisionWithHandle } from '@aztec/stdlib/world-state';
26
26
 
27
27
  import assert from 'assert';
28
28
 
29
29
  import {
30
30
  type SerializedIndexedLeaf,
31
31
  type SerializedLeafValue,
32
+ WorldStateMessageType,
32
33
  blockStateReference,
33
34
  treeStateReferenceToSnapshot,
34
35
  } from './message.js';
@@ -45,12 +46,8 @@ export class MerkleTreesFacade implements MerkleTreeReadOperations {
45
46
  return this.initialHeader;
46
47
  }
47
48
 
48
- getRevision(): WorldStateRevision {
49
- return this.revision;
50
- }
51
-
52
- getIpcPath(): string {
53
- return this.instance.getIpcPath();
49
+ getRevision(): WorldStateRevisionWithHandle {
50
+ return WorldStateRevisionWithHandle.fromWorldStateRevision(this.revision, this.instance.getHandle());
54
51
  }
55
52
 
56
53
  findLeafIndices(treeId: MerkleTreeId, values: MerkleTreeLeafType<MerkleTreeId>[]): Promise<(bigint | undefined)[]> {
@@ -61,13 +58,13 @@ export class MerkleTreesFacade implements MerkleTreeReadOperations {
61
58
  treeId: MerkleTreeId,
62
59
  values: MerkleTreeLeafType<MerkleTreeId>[],
63
60
  ): Promise<({ path: SiblingPath<N>; index: bigint } | undefined)[]> {
64
- const paths = await this.instance.findSiblingPaths(
61
+ const response = await this.instance.call(WorldStateMessageType.FIND_SIBLING_PATHS, {
62
+ leaves: values.map(leaf => serializeLeaf(hydrateLeaf(treeId, leaf))),
63
+ revision: this.revision,
65
64
  treeId,
66
- this.revision,
67
- values.map(leaf => serializeLeaf(hydrateLeaf(treeId, leaf))),
68
- );
65
+ });
69
66
 
70
- return paths.map(path => {
67
+ return response.paths.map(path => {
71
68
  if (!path) {
72
69
  return undefined;
73
70
  }
@@ -80,14 +77,14 @@ export class MerkleTreesFacade implements MerkleTreeReadOperations {
80
77
  leaves: MerkleTreeLeafType<MerkleTreeId>[],
81
78
  startIndex: bigint,
82
79
  ): Promise<(bigint | undefined)[]> {
83
- const indices = await this.instance.findLeafIndices(
80
+ const response = await this.instance.call(WorldStateMessageType.FIND_LEAF_INDICES, {
81
+ leaves: leaves.map(leaf => serializeLeaf(hydrateLeaf(treeId, leaf))),
82
+ revision: this.revision,
84
83
  treeId,
85
- this.revision,
86
- leaves.map(leaf => serializeLeaf(hydrateLeaf(treeId, leaf))),
87
84
  startIndex,
88
- );
85
+ });
89
86
 
90
- return indices.map(index => {
87
+ return response.indices.map(index => {
91
88
  if (typeof index === 'number' || typeof index === 'bigint') {
92
89
  return BigInt(index);
93
90
  } else {
@@ -97,7 +94,11 @@ export class MerkleTreesFacade implements MerkleTreeReadOperations {
97
94
  }
98
95
 
99
96
  async getLeafPreimage(treeId: IndexedTreeId, leafIndex: bigint): Promise<IndexedTreeLeafPreimage | undefined> {
100
- const resp = await this.instance.getLeafPreimage(treeId, this.revision, leafIndex);
97
+ const resp = await this.instance.call(WorldStateMessageType.GET_LEAF_PREIMAGE, {
98
+ leafIndex,
99
+ revision: this.revision,
100
+ treeId,
101
+ });
101
102
 
102
103
  return resp ? deserializeIndexedLeaf(resp) : undefined;
103
104
  }
@@ -106,7 +107,11 @@ export class MerkleTreesFacade implements MerkleTreeReadOperations {
106
107
  treeId: ID,
107
108
  leafIndex: bigint,
108
109
  ): Promise<MerkleTreeLeafType<ID> | undefined> {
109
- const resp = await this.instance.getLeafValue(treeId, this.revision, leafIndex);
110
+ const resp = await this.instance.call(WorldStateMessageType.GET_LEAF_VALUE, {
111
+ leafIndex,
112
+ revision: this.revision,
113
+ treeId,
114
+ });
110
115
 
111
116
  if (!resp) {
112
117
  return undefined;
@@ -124,7 +129,11 @@ export class MerkleTreesFacade implements MerkleTreeReadOperations {
124
129
  treeId: IndexedTreeId,
125
130
  value: bigint,
126
131
  ): Promise<{ index: bigint; alreadyPresent: boolean } | undefined> {
127
- const resp = await this.instance.findLowLeaf(treeId, this.revision, new Fr(value));
132
+ const resp = await this.instance.call(WorldStateMessageType.FIND_LOW_LEAF, {
133
+ key: new Fr(value),
134
+ revision: this.revision,
135
+ treeId,
136
+ });
128
137
  return {
129
138
  alreadyPresent: resp.alreadyPresent,
130
139
  index: BigInt(resp.index),
@@ -132,48 +141,68 @@ export class MerkleTreesFacade implements MerkleTreeReadOperations {
132
141
  }
133
142
 
134
143
  async getSiblingPath<N extends number>(treeId: MerkleTreeId, leafIndex: bigint): Promise<SiblingPath<N>> {
135
- const siblingPath = await this.instance.getSiblingPath(treeId, this.revision, leafIndex);
144
+ const siblingPath = await this.instance.call(WorldStateMessageType.GET_SIBLING_PATH, {
145
+ leafIndex,
146
+ revision: this.revision,
147
+ treeId,
148
+ });
136
149
 
137
150
  return new SiblingPath(siblingPath.length, siblingPath) as any;
138
151
  }
139
152
 
140
153
  async getStateReference(): Promise<StateReference> {
141
- const state = await this.instance.getStateReference(this.revision);
154
+ const resp = await this.instance.call(WorldStateMessageType.GET_STATE_REFERENCE, {
155
+ revision: this.revision,
156
+ });
142
157
 
143
158
  return new StateReference(
144
- treeStateReferenceToSnapshot(state[MerkleTreeId.L1_TO_L2_MESSAGE_TREE]),
159
+ treeStateReferenceToSnapshot(resp.state[MerkleTreeId.L1_TO_L2_MESSAGE_TREE]),
145
160
  new PartialStateReference(
146
- treeStateReferenceToSnapshot(state[MerkleTreeId.NOTE_HASH_TREE]),
147
- treeStateReferenceToSnapshot(state[MerkleTreeId.NULLIFIER_TREE]),
148
- treeStateReferenceToSnapshot(state[MerkleTreeId.PUBLIC_DATA_TREE]),
161
+ treeStateReferenceToSnapshot(resp.state[MerkleTreeId.NOTE_HASH_TREE]),
162
+ treeStateReferenceToSnapshot(resp.state[MerkleTreeId.NULLIFIER_TREE]),
163
+ treeStateReferenceToSnapshot(resp.state[MerkleTreeId.PUBLIC_DATA_TREE]),
149
164
  ),
150
165
  );
151
166
  }
152
167
 
153
168
  async getInitialStateReference(): Promise<StateReference> {
154
- const state = await this.instance.getInitialStateReference();
169
+ const resp = await this.instance.call(WorldStateMessageType.GET_INITIAL_STATE_REFERENCE, { canonical: true });
155
170
 
156
171
  return new StateReference(
157
- treeStateReferenceToSnapshot(state[MerkleTreeId.L1_TO_L2_MESSAGE_TREE]),
172
+ treeStateReferenceToSnapshot(resp.state[MerkleTreeId.L1_TO_L2_MESSAGE_TREE]),
158
173
  new PartialStateReference(
159
- treeStateReferenceToSnapshot(state[MerkleTreeId.NOTE_HASH_TREE]),
160
- treeStateReferenceToSnapshot(state[MerkleTreeId.NULLIFIER_TREE]),
161
- treeStateReferenceToSnapshot(state[MerkleTreeId.PUBLIC_DATA_TREE]),
174
+ treeStateReferenceToSnapshot(resp.state[MerkleTreeId.NOTE_HASH_TREE]),
175
+ treeStateReferenceToSnapshot(resp.state[MerkleTreeId.NULLIFIER_TREE]),
176
+ treeStateReferenceToSnapshot(resp.state[MerkleTreeId.PUBLIC_DATA_TREE]),
162
177
  ),
163
178
  );
164
179
  }
165
180
 
166
181
  async getTreeInfo(treeId: MerkleTreeId): Promise<TreeInfo> {
167
- return await this.instance.getTreeInfo(treeId, this.revision);
182
+ const resp = await this.instance.call(WorldStateMessageType.GET_TREE_INFO, {
183
+ treeId: treeId,
184
+ revision: this.revision,
185
+ });
186
+
187
+ return {
188
+ depth: resp.depth,
189
+ root: resp.root,
190
+ size: BigInt(resp.size),
191
+ treeId,
192
+ };
168
193
  }
169
194
 
170
195
  async getBlockNumbersForLeafIndices<ID extends MerkleTreeId>(
171
196
  treeId: ID,
172
197
  leafIndices: bigint[],
173
198
  ): Promise<(BlockNumber | undefined)[]> {
174
- const blockNumbers = await this.instance.getBlockNumbersForLeafIndices(treeId, this.revision, leafIndices);
199
+ const response = await this.instance.call(WorldStateMessageType.GET_BLOCK_NUMBERS_FOR_LEAF_INDICES, {
200
+ treeId,
201
+ revision: this.revision,
202
+ leafIndices,
203
+ });
175
204
 
176
- return blockNumbers.map(x => (x === undefined || x === null ? undefined : BlockNumber(Number(x))));
205
+ return response.blockNumbers.map(x => (x === undefined || x === null ? undefined : BlockNumber(Number(x))));
177
206
  }
178
207
  }
179
208
 
@@ -192,19 +221,19 @@ export class MerkleTreesForkFacade extends MerkleTreesFacade implements MerkleTr
192
221
  super(instance, initialHeader, revision);
193
222
  }
194
223
  async updateArchive(header: BlockHeader): Promise<void> {
195
- await this.instance.updateArchive(
196
- this.revision.forkId,
197
- blockStateReference(header.state),
198
- (await header.hash()).toBuffer(),
199
- );
224
+ await this.instance.call(WorldStateMessageType.UPDATE_ARCHIVE, {
225
+ forkId: this.revision.forkId,
226
+ blockHeaderHash: (await header.hash()).toBuffer(),
227
+ blockStateRef: blockStateReference(header.state),
228
+ });
200
229
  }
201
230
 
202
231
  async appendLeaves<ID extends MerkleTreeId>(treeId: ID, leaves: MerkleTreeLeafType<ID>[]): Promise<void> {
203
- await this.instance.appendLeaves(
232
+ await this.instance.call(WorldStateMessageType.APPEND_LEAVES, {
233
+ leaves: leaves.map(leaf => serializeLeaf(hydrateLeaf(treeId, leaf as any))),
234
+ forkId: this.revision.forkId,
204
235
  treeId,
205
- this.revision.forkId,
206
- leaves.map(leaf => serializeLeaf(hydrateLeaf(treeId, leaf as any))),
207
- );
236
+ });
208
237
  }
209
238
 
210
239
  async batchInsert<TreeHeight extends number, SubtreeSiblingPathHeight extends number, ID extends IndexedTreeId>(
@@ -213,19 +242,24 @@ export class MerkleTreesForkFacade extends MerkleTreesFacade implements MerkleTr
213
242
  subtreeHeight: number,
214
243
  ): Promise<BatchInsertionResult<TreeHeight, SubtreeSiblingPathHeight>> {
215
244
  const leaves = rawLeaves.map((leaf: Buffer) => hydrateLeaf(treeId, leaf)).map(serializeLeaf);
216
- const resp = await this.instance.batchInsert(treeId, this.revision.forkId, leaves, subtreeHeight);
245
+ const resp = await this.instance.call(WorldStateMessageType.BATCH_INSERT, {
246
+ leaves,
247
+ treeId,
248
+ forkId: this.revision.forkId,
249
+ subtreeDepth: subtreeHeight,
250
+ });
217
251
 
218
252
  return {
219
253
  newSubtreeSiblingPath: new SiblingPath<SubtreeSiblingPathHeight>(
220
- resp.subtreePath.length as any,
221
- resp.subtreePath,
254
+ resp.subtree_path.length as any,
255
+ resp.subtree_path,
222
256
  ),
223
- sortedNewLeaves: resp.sortedLeaves
257
+ sortedNewLeaves: resp.sorted_leaves
224
258
  .map(([leaf]) => leaf)
225
259
  .map(deserializeLeafValue)
226
260
  .map(serializeToBuffer),
227
- sortedNewLeavesIndexes: resp.sortedLeaves.map(([, index]) => index),
228
- lowLeavesWitnessData: resp.lowLeafWitnessData.map(data => ({
261
+ sortedNewLeavesIndexes: resp.sorted_leaves.map(([, index]) => index),
262
+ lowLeavesWitnessData: resp.low_leaf_witness_data.map(data => ({
229
263
  index: BigInt(data.index),
230
264
  leafPreimage: deserializeIndexedLeaf(data.leaf),
231
265
  siblingPath: new SiblingPath<TreeHeight>(data.path.length as any, data.path),
@@ -238,15 +272,19 @@ export class MerkleTreesForkFacade extends MerkleTreesFacade implements MerkleTr
238
272
  rawLeaves: Buffer[],
239
273
  ): Promise<SequentialInsertionResult<TreeHeight>> {
240
274
  const leaves = rawLeaves.map((leaf: Buffer) => hydrateLeaf(treeId, leaf)).map(serializeLeaf);
241
- const resp = await this.instance.sequentialInsert(treeId, this.revision.forkId, leaves);
275
+ const resp = await this.instance.call(WorldStateMessageType.SEQUENTIAL_INSERT, {
276
+ leaves,
277
+ treeId,
278
+ forkId: this.revision.forkId,
279
+ });
242
280
 
243
281
  return {
244
- lowLeavesWitnessData: resp.lowLeafWitnessData.map(data => ({
282
+ lowLeavesWitnessData: resp.low_leaf_witness_data.map(data => ({
245
283
  index: BigInt(data.index),
246
284
  leafPreimage: deserializeIndexedLeaf(data.leaf),
247
285
  siblingPath: new SiblingPath<TreeHeight>(data.path.length as any, data.path),
248
286
  })),
249
- insertionWitnessData: resp.insertionWitnessData.map(data => ({
287
+ insertionWitnessData: resp.insertion_witness_data.map(data => ({
250
288
  index: BigInt(data.index),
251
289
  leafPreimage: deserializeIndexedLeaf(data.leaf),
252
290
  siblingPath: new SiblingPath<TreeHeight>(data.path.length as any, data.path),
@@ -266,11 +304,11 @@ export class MerkleTreesForkFacade extends MerkleTreesFacade implements MerkleTr
266
304
 
267
305
  private async doClose(): Promise<void> {
268
306
  try {
269
- await this.instance.deleteFork(this.revision.forkId);
307
+ await this.instance.call(WorldStateMessageType.DELETE_FORK, { forkId: this.revision.forkId });
270
308
  } catch (err: any) {
271
309
  // Ignore errors due to native instance being closed during shutdown.
272
310
  // This can happen when validators are still processing block proposals while the node is stopping.
273
- if (err?.message === 'Native instance is closed' || err?.message === 'IPC instance is closed') {
311
+ if (err?.message === 'Native instance is closed') {
274
312
  return;
275
313
  }
276
314
  // Ignore "Fork not found": the native fork was already destroyed by a pending-chain unwind or a
@@ -297,27 +335,34 @@ export class MerkleTreesForkFacade extends MerkleTreesFacade implements MerkleTr
297
335
 
298
336
  public async createCheckpoint(): Promise<number> {
299
337
  assert.notEqual(this.revision.forkId, 0, 'Fork ID must be set');
300
- return await this.instance.createCheckpoint(this.revision.forkId);
338
+ const resp = await this.instance.call(WorldStateMessageType.CREATE_CHECKPOINT, { forkId: this.revision.forkId });
339
+ return resp.depth;
301
340
  }
302
341
 
303
342
  public async commitCheckpoint(): Promise<void> {
304
343
  assert.notEqual(this.revision.forkId, 0, 'Fork ID must be set');
305
- await this.instance.commitCheckpoint(this.revision.forkId);
344
+ await this.instance.call(WorldStateMessageType.COMMIT_CHECKPOINT, { forkId: this.revision.forkId });
306
345
  }
307
346
 
308
347
  public async revertCheckpoint(): Promise<void> {
309
348
  assert.notEqual(this.revision.forkId, 0, 'Fork ID must be set');
310
- await this.instance.revertCheckpoint(this.revision.forkId);
349
+ await this.instance.call(WorldStateMessageType.REVERT_CHECKPOINT, { forkId: this.revision.forkId });
311
350
  }
312
351
 
313
352
  public async commitAllCheckpointsTo(depth: number): Promise<void> {
314
353
  assert.notEqual(this.revision.forkId, 0, 'Fork ID must be set');
315
- await this.instance.commitAllCheckpoints(this.revision.forkId, depth);
354
+ await this.instance.call(WorldStateMessageType.COMMIT_ALL_CHECKPOINTS, {
355
+ forkId: this.revision.forkId,
356
+ depth,
357
+ });
316
358
  }
317
359
 
318
360
  public async revertAllCheckpointsTo(depth: number): Promise<void> {
319
361
  assert.notEqual(this.revision.forkId, 0, 'Fork ID must be set');
320
- await this.instance.revertAllCheckpoints(this.revision.forkId, depth);
362
+ await this.instance.call(WorldStateMessageType.REVERT_ALL_CHECKPOINTS, {
363
+ forkId: this.revision.forkId,
364
+ depth,
365
+ });
321
366
  }
322
367
  }
323
368
 
@@ -1,7 +1,59 @@
1
1
  import { BlockNumber } from '@aztec/foundation/branded-types';
2
2
  import { Fr } from '@aztec/foundation/curves/bn254';
3
+ import type { Tuple } from '@aztec/foundation/serialize';
3
4
  import { AppendOnlyTreeSnapshot, MerkleTreeId } from '@aztec/stdlib/trees';
4
5
  import type { StateReference } from '@aztec/stdlib/tx';
6
+ import type { UInt32 } from '@aztec/stdlib/types';
7
+ import type { WorldStateRevision } from '@aztec/stdlib/world-state';
8
+
9
+ export enum WorldStateMessageType {
10
+ GET_TREE_INFO = 100,
11
+ GET_STATE_REFERENCE,
12
+ GET_INITIAL_STATE_REFERENCE,
13
+
14
+ GET_LEAF_VALUE,
15
+ GET_LEAF_PREIMAGE,
16
+ GET_SIBLING_PATH,
17
+ GET_BLOCK_NUMBERS_FOR_LEAF_INDICES,
18
+
19
+ FIND_LEAF_INDICES,
20
+ FIND_LOW_LEAF,
21
+ FIND_SIBLING_PATHS,
22
+
23
+ APPEND_LEAVES,
24
+ BATCH_INSERT,
25
+ SEQUENTIAL_INSERT,
26
+
27
+ UPDATE_ARCHIVE,
28
+
29
+ COMMIT,
30
+ ROLLBACK,
31
+
32
+ SYNC_BLOCK,
33
+
34
+ CREATE_FORK,
35
+ DELETE_FORK,
36
+
37
+ FINALIZE_BLOCKS,
38
+ UNWIND_BLOCKS,
39
+ REMOVE_HISTORICAL_BLOCKS,
40
+
41
+ GET_STATUS,
42
+
43
+ CREATE_CHECKPOINT,
44
+ COMMIT_CHECKPOINT,
45
+ REVERT_CHECKPOINT,
46
+ COMMIT_ALL_CHECKPOINTS,
47
+ REVERT_ALL_CHECKPOINTS,
48
+
49
+ COPY_STORES,
50
+
51
+ CLOSE = 999,
52
+ }
53
+
54
+ interface WithTreeId {
55
+ treeId: MerkleTreeId;
56
+ }
5
57
 
6
58
  export interface WorldStateStatusSummary {
7
59
  /** Last block number that can still be unwound. */
@@ -227,6 +279,32 @@ export function sanitizeFullStatus(status: WorldStateStatusFull) {
227
279
  return status;
228
280
  }
229
281
 
282
+ interface WithForkId {
283
+ forkId: number;
284
+ }
285
+
286
+ interface CreateCheckpointResponse {
287
+ depth: number;
288
+ }
289
+
290
+ /** Request to commit/revert all checkpoints down to a target depth. The resulting depth after the operation equals the given depth. */
291
+ interface CheckpointDepthRequest extends WithForkId {
292
+ /** The target depth after the operation. All checkpoints above this depth are committed/reverted. */
293
+ depth: number;
294
+ }
295
+
296
+ interface WithWorldStateRevision {
297
+ revision: WorldStateRevision;
298
+ }
299
+
300
+ interface WithCanonicalForkId {
301
+ canonical: true;
302
+ }
303
+
304
+ interface WithLeafIndex {
305
+ leafIndex: bigint;
306
+ }
307
+
230
308
  export type SerializedLeafValue =
231
309
  | Buffer // Fr
232
310
  | { nullifier: Buffer } // NullifierLeaf
@@ -238,36 +316,245 @@ export type SerializedIndexedLeaf = {
238
316
  nextKey: Buffer; // Fr
239
317
  };
240
318
 
241
- export interface SerializedSiblingPathAndIndex {
319
+ interface WithLeafValues {
320
+ leaves: SerializedLeafValue[];
321
+ }
322
+
323
+ interface BlockShiftRequest extends WithCanonicalForkId {
324
+ toBlockNumber: BlockNumber;
325
+ }
326
+
327
+ interface WithLeaves {
328
+ leaves: SerializedLeafValue[];
329
+ }
330
+
331
+ interface GetTreeInfoRequest extends WithTreeId, WithWorldStateRevision {}
332
+ interface GetTreeInfoResponse {
333
+ treeId: MerkleTreeId;
334
+ depth: UInt32;
335
+ size: bigint | number;
336
+ root: Buffer;
337
+ }
338
+
339
+ interface GetBlockNumbersForLeafIndicesRequest extends WithTreeId, WithWorldStateRevision {
340
+ leafIndices: bigint[];
341
+ }
342
+
343
+ interface GetBlockNumbersForLeafIndicesResponse {
344
+ blockNumbers: bigint[];
345
+ }
346
+
347
+ interface GetSiblingPathRequest extends WithTreeId, WithLeafIndex, WithWorldStateRevision {}
348
+ type GetSiblingPathResponse = Buffer[];
349
+
350
+ interface GetStateReferenceRequest extends WithWorldStateRevision {}
351
+ interface GetStateReferenceResponse {
352
+ state: Record<MerkleTreeId, TreeStateReference>;
353
+ }
354
+
355
+ interface GetLeafRequest extends WithTreeId, WithWorldStateRevision, WithLeafIndex {}
356
+ type GetLeafResponse = SerializedLeafValue | undefined;
357
+
358
+ interface GetLeafPreImageRequest extends WithTreeId, WithLeafIndex, WithWorldStateRevision {}
359
+ type GetLeafPreImageResponse = SerializedIndexedLeaf | undefined;
360
+
361
+ interface FindLeafIndicesRequest extends WithTreeId, WithLeafValues, WithWorldStateRevision {
362
+ startIndex: bigint;
363
+ }
364
+ interface FindLeafIndicesResponse {
365
+ indices: bigint[];
366
+ }
367
+
368
+ interface FindSiblingPathsRequest extends WithTreeId, WithLeafValues, WithWorldStateRevision {}
369
+
370
+ interface SiblingPathAndIndex {
242
371
  index: bigint;
243
372
  path: Buffer[];
244
373
  }
374
+ interface FindSiblingPathsResponse {
375
+ paths: (SiblingPathAndIndex | undefined)[];
376
+ }
245
377
 
246
- export interface SerializedBatchInsertionResult {
247
- lowLeafWitnessData: ReadonlyArray<{
378
+ interface FindLowLeafRequest extends WithTreeId, WithWorldStateRevision {
379
+ key: Fr;
380
+ }
381
+ interface FindLowLeafResponse {
382
+ index: bigint | number;
383
+ alreadyPresent: boolean;
384
+ }
385
+
386
+ interface AppendLeavesRequest extends WithTreeId, WithForkId, WithLeaves {}
387
+
388
+ interface BatchInsertRequest extends WithTreeId, WithForkId, WithLeaves {
389
+ subtreeDepth: number;
390
+ }
391
+
392
+ interface BatchInsertResponse {
393
+ low_leaf_witness_data: ReadonlyArray<{
248
394
  leaf: SerializedIndexedLeaf;
249
395
  index: bigint | number;
250
- path: Buffer[];
396
+ path: Tuple<Buffer, number>;
251
397
  }>;
252
- sortedLeaves: ReadonlyArray<readonly [SerializedLeafValue, number]>;
253
- subtreePath: Buffer[];
398
+ sorted_leaves: ReadonlyArray<[SerializedLeafValue, UInt32]>;
399
+ subtree_path: Tuple<Buffer, number>;
254
400
  }
255
401
 
256
- export interface SerializedSequentialInsertionResult {
257
- lowLeafWitnessData: ReadonlyArray<{
402
+ interface SequentialInsertRequest extends WithTreeId, WithForkId, WithLeaves {}
403
+
404
+ interface SequentialInsertResponse {
405
+ low_leaf_witness_data: ReadonlyArray<{
258
406
  leaf: SerializedIndexedLeaf;
259
407
  index: bigint | number;
260
- path: Buffer[];
408
+ path: Tuple<Buffer, number>;
261
409
  }>;
262
- insertionWitnessData: ReadonlyArray<{
410
+ insertion_witness_data: ReadonlyArray<{
263
411
  leaf: SerializedIndexedLeaf;
264
412
  index: bigint | number;
265
- path: Buffer[];
413
+ path: Tuple<Buffer, number>;
266
414
  }>;
267
415
  }
268
416
 
269
- export type TreeStateReference = readonly [Buffer, number | bigint];
270
- export type BlockStateReference = Map<Exclude<MerkleTreeId, MerkleTreeId.ARCHIVE>, TreeStateReference>;
417
+ interface UpdateArchiveRequest extends WithForkId {
418
+ blockStateRef: BlockStateReference;
419
+ blockHeaderHash: Buffer;
420
+ }
421
+
422
+ interface SyncBlockRequest extends WithCanonicalForkId {
423
+ blockNumber: BlockNumber;
424
+ blockStateRef: BlockStateReference;
425
+ blockHeaderHash: Buffer;
426
+ /** Canonical archive root after this block; world state rejects the sync if its computed root differs. */
427
+ expectedArchiveRoot: Buffer;
428
+ /** Canonical archive root before this block (the block's lastArchive); verified against the local root. */
429
+ expectedPreviousArchiveRoot: Buffer;
430
+ paddedNoteHashes: readonly SerializedLeafValue[];
431
+ paddedL1ToL2Messages: readonly SerializedLeafValue[];
432
+ paddedNullifiers: readonly SerializedLeafValue[];
433
+ publicDataWrites: readonly SerializedLeafValue[];
434
+ }
435
+
436
+ interface CreateForkRequest extends WithCanonicalForkId {
437
+ latest: boolean;
438
+ blockNumber: BlockNumber;
439
+ }
440
+
441
+ interface CreateForkResponse {
442
+ forkId: number;
443
+ }
444
+
445
+ interface DeleteForkRequest extends WithForkId {}
446
+
447
+ interface CopyStoresRequest extends WithCanonicalForkId {
448
+ dstPath: string;
449
+ compact: boolean;
450
+ }
451
+
452
+ export type WorldStateRequestCategories = WithForkId | WithWorldStateRevision | WithCanonicalForkId;
453
+
454
+ export function isWithForkId(body: WorldStateRequestCategories): body is WithForkId {
455
+ return body && 'forkId' in body;
456
+ }
457
+
458
+ export function isWithRevision(body: WorldStateRequestCategories): body is WithWorldStateRevision {
459
+ return body && 'revision' in body;
460
+ }
461
+
462
+ export function isWithCanonical(body: WorldStateRequestCategories): body is WithCanonicalForkId {
463
+ return body && 'canonical' in body;
464
+ }
465
+
466
+ export type WorldStateRequest = {
467
+ [WorldStateMessageType.GET_TREE_INFO]: GetTreeInfoRequest;
468
+ [WorldStateMessageType.GET_STATE_REFERENCE]: GetStateReferenceRequest;
469
+ [WorldStateMessageType.GET_INITIAL_STATE_REFERENCE]: WithCanonicalForkId;
470
+
471
+ [WorldStateMessageType.GET_LEAF_VALUE]: GetLeafRequest;
472
+ [WorldStateMessageType.GET_LEAF_PREIMAGE]: GetLeafPreImageRequest;
473
+ [WorldStateMessageType.GET_SIBLING_PATH]: GetSiblingPathRequest;
474
+ [WorldStateMessageType.GET_BLOCK_NUMBERS_FOR_LEAF_INDICES]: GetBlockNumbersForLeafIndicesRequest;
475
+
476
+ [WorldStateMessageType.FIND_LEAF_INDICES]: FindLeafIndicesRequest;
477
+ [WorldStateMessageType.FIND_LOW_LEAF]: FindLowLeafRequest;
478
+ [WorldStateMessageType.FIND_SIBLING_PATHS]: FindSiblingPathsRequest;
479
+
480
+ [WorldStateMessageType.APPEND_LEAVES]: AppendLeavesRequest;
481
+ [WorldStateMessageType.BATCH_INSERT]: BatchInsertRequest;
482
+ [WorldStateMessageType.SEQUENTIAL_INSERT]: SequentialInsertRequest;
483
+
484
+ [WorldStateMessageType.UPDATE_ARCHIVE]: UpdateArchiveRequest;
485
+
486
+ [WorldStateMessageType.COMMIT]: WithCanonicalForkId;
487
+ [WorldStateMessageType.ROLLBACK]: WithCanonicalForkId;
488
+
489
+ [WorldStateMessageType.SYNC_BLOCK]: SyncBlockRequest;
490
+
491
+ [WorldStateMessageType.CREATE_FORK]: CreateForkRequest;
492
+ [WorldStateMessageType.DELETE_FORK]: DeleteForkRequest;
493
+
494
+ [WorldStateMessageType.REMOVE_HISTORICAL_BLOCKS]: BlockShiftRequest;
495
+ [WorldStateMessageType.UNWIND_BLOCKS]: BlockShiftRequest;
496
+ [WorldStateMessageType.FINALIZE_BLOCKS]: BlockShiftRequest;
497
+
498
+ [WorldStateMessageType.GET_STATUS]: WithCanonicalForkId;
499
+
500
+ [WorldStateMessageType.CREATE_CHECKPOINT]: WithForkId;
501
+ [WorldStateMessageType.COMMIT_CHECKPOINT]: WithForkId;
502
+ [WorldStateMessageType.REVERT_CHECKPOINT]: WithForkId;
503
+ [WorldStateMessageType.COMMIT_ALL_CHECKPOINTS]: CheckpointDepthRequest;
504
+ [WorldStateMessageType.REVERT_ALL_CHECKPOINTS]: CheckpointDepthRequest;
505
+
506
+ [WorldStateMessageType.COPY_STORES]: CopyStoresRequest;
507
+
508
+ [WorldStateMessageType.CLOSE]: WithCanonicalForkId;
509
+ };
510
+
511
+ export type WorldStateResponse = {
512
+ [WorldStateMessageType.GET_TREE_INFO]: GetTreeInfoResponse;
513
+ [WorldStateMessageType.GET_STATE_REFERENCE]: GetStateReferenceResponse;
514
+ [WorldStateMessageType.GET_INITIAL_STATE_REFERENCE]: GetStateReferenceResponse;
515
+
516
+ [WorldStateMessageType.GET_LEAF_VALUE]: GetLeafResponse;
517
+ [WorldStateMessageType.GET_LEAF_PREIMAGE]: GetLeafPreImageResponse;
518
+ [WorldStateMessageType.GET_SIBLING_PATH]: GetSiblingPathResponse;
519
+ [WorldStateMessageType.GET_BLOCK_NUMBERS_FOR_LEAF_INDICES]: GetBlockNumbersForLeafIndicesResponse;
520
+
521
+ [WorldStateMessageType.FIND_LEAF_INDICES]: FindLeafIndicesResponse;
522
+ [WorldStateMessageType.FIND_LOW_LEAF]: FindLowLeafResponse;
523
+ [WorldStateMessageType.FIND_SIBLING_PATHS]: FindSiblingPathsResponse;
524
+
525
+ [WorldStateMessageType.APPEND_LEAVES]: void;
526
+ [WorldStateMessageType.BATCH_INSERT]: BatchInsertResponse;
527
+ [WorldStateMessageType.SEQUENTIAL_INSERT]: SequentialInsertResponse;
528
+
529
+ [WorldStateMessageType.UPDATE_ARCHIVE]: void;
530
+
531
+ [WorldStateMessageType.COMMIT]: void;
532
+ [WorldStateMessageType.ROLLBACK]: void;
533
+
534
+ [WorldStateMessageType.SYNC_BLOCK]: WorldStateStatusFull;
535
+
536
+ [WorldStateMessageType.CREATE_FORK]: CreateForkResponse;
537
+ [WorldStateMessageType.DELETE_FORK]: void;
538
+
539
+ [WorldStateMessageType.REMOVE_HISTORICAL_BLOCKS]: WorldStateStatusFull;
540
+ [WorldStateMessageType.UNWIND_BLOCKS]: WorldStateStatusFull;
541
+ [WorldStateMessageType.FINALIZE_BLOCKS]: WorldStateStatusSummary;
542
+
543
+ [WorldStateMessageType.GET_STATUS]: WorldStateStatusSummary;
544
+
545
+ [WorldStateMessageType.CREATE_CHECKPOINT]: CreateCheckpointResponse;
546
+ [WorldStateMessageType.COMMIT_CHECKPOINT]: void;
547
+ [WorldStateMessageType.REVERT_CHECKPOINT]: void;
548
+ [WorldStateMessageType.COMMIT_ALL_CHECKPOINTS]: void;
549
+ [WorldStateMessageType.REVERT_ALL_CHECKPOINTS]: void;
550
+
551
+ [WorldStateMessageType.COPY_STORES]: void;
552
+
553
+ [WorldStateMessageType.CLOSE]: void;
554
+ };
555
+
556
+ type TreeStateReference = readonly [Buffer, number | bigint];
557
+ type BlockStateReference = Map<Exclude<MerkleTreeId, MerkleTreeId.ARCHIVE>, TreeStateReference>;
271
558
 
272
559
  export function treeStateReferenceToSnapshot([root, size]: TreeStateReference): AppendOnlyTreeSnapshot {
273
560
  return new AppendOnlyTreeSnapshot(Fr.fromBuffer(root), Number(size));