@aztec/world-state 0.0.1-commit.993d240 → 0.0.1-commit.9a89641

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