@aztec/world-state 0.0.0-test.0

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 (74) hide show
  1. package/README.md +40 -0
  2. package/dest/index.d.ts +5 -0
  3. package/dest/index.d.ts.map +1 -0
  4. package/dest/index.js +4 -0
  5. package/dest/instrumentation/instrumentation.d.ts +22 -0
  6. package/dest/instrumentation/instrumentation.d.ts.map +1 -0
  7. package/dest/instrumentation/instrumentation.js +117 -0
  8. package/dest/native/fork_checkpoint.d.ts +10 -0
  9. package/dest/native/fork_checkpoint.d.ts.map +1 -0
  10. package/dest/native/fork_checkpoint.js +26 -0
  11. package/dest/native/index.d.ts +3 -0
  12. package/dest/native/index.d.ts.map +1 -0
  13. package/dest/native/index.js +2 -0
  14. package/dest/native/merkle_trees_facade.d.ts +42 -0
  15. package/dest/native/merkle_trees_facade.d.ts.map +1 -0
  16. package/dest/native/merkle_trees_facade.js +240 -0
  17. package/dest/native/message.d.ts +331 -0
  18. package/dest/native/message.d.ts.map +1 -0
  19. package/dest/native/message.js +192 -0
  20. package/dest/native/native_world_state.d.ts +57 -0
  21. package/dest/native/native_world_state.d.ts.map +1 -0
  22. package/dest/native/native_world_state.js +229 -0
  23. package/dest/native/native_world_state_instance.d.ts +33 -0
  24. package/dest/native/native_world_state_instance.d.ts.map +1 -0
  25. package/dest/native/native_world_state_instance.js +164 -0
  26. package/dest/native/world_state_ops_queue.d.ts +19 -0
  27. package/dest/native/world_state_ops_queue.d.ts.map +1 -0
  28. package/dest/native/world_state_ops_queue.js +146 -0
  29. package/dest/synchronizer/config.d.ts +23 -0
  30. package/dest/synchronizer/config.d.ts.map +1 -0
  31. package/dest/synchronizer/config.js +39 -0
  32. package/dest/synchronizer/factory.d.ts +12 -0
  33. package/dest/synchronizer/factory.d.ts.map +1 -0
  34. package/dest/synchronizer/factory.js +24 -0
  35. package/dest/synchronizer/index.d.ts +3 -0
  36. package/dest/synchronizer/index.d.ts.map +1 -0
  37. package/dest/synchronizer/index.js +2 -0
  38. package/dest/synchronizer/server_world_state_synchronizer.d.ts +79 -0
  39. package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -0
  40. package/dest/synchronizer/server_world_state_synchronizer.js +277 -0
  41. package/dest/test/index.d.ts +2 -0
  42. package/dest/test/index.d.ts.map +1 -0
  43. package/dest/test/index.js +1 -0
  44. package/dest/test/utils.d.ts +19 -0
  45. package/dest/test/utils.d.ts.map +1 -0
  46. package/dest/test/utils.js +99 -0
  47. package/dest/testing.d.ts +10 -0
  48. package/dest/testing.d.ts.map +1 -0
  49. package/dest/testing.js +37 -0
  50. package/dest/world-state-db/index.d.ts +3 -0
  51. package/dest/world-state-db/index.d.ts.map +1 -0
  52. package/dest/world-state-db/index.js +1 -0
  53. package/dest/world-state-db/merkle_tree_db.d.ts +68 -0
  54. package/dest/world-state-db/merkle_tree_db.d.ts.map +1 -0
  55. package/dest/world-state-db/merkle_tree_db.js +17 -0
  56. package/package.json +98 -0
  57. package/src/index.ts +4 -0
  58. package/src/instrumentation/instrumentation.ts +174 -0
  59. package/src/native/fork_checkpoint.ts +30 -0
  60. package/src/native/index.ts +2 -0
  61. package/src/native/merkle_trees_facade.ts +331 -0
  62. package/src/native/message.ts +541 -0
  63. package/src/native/native_world_state.ts +317 -0
  64. package/src/native/native_world_state_instance.ts +238 -0
  65. package/src/native/world_state_ops_queue.ts +190 -0
  66. package/src/synchronizer/config.ts +68 -0
  67. package/src/synchronizer/factory.ts +53 -0
  68. package/src/synchronizer/index.ts +2 -0
  69. package/src/synchronizer/server_world_state_synchronizer.ts +344 -0
  70. package/src/test/index.ts +1 -0
  71. package/src/test/utils.ts +153 -0
  72. package/src/testing.ts +60 -0
  73. package/src/world-state-db/index.ts +3 -0
  74. package/src/world-state-db/merkle_tree_db.ts +79 -0
@@ -0,0 +1,541 @@
1
+ import { Fr } from '@aztec/foundation/fields';
2
+ import type { Tuple } from '@aztec/foundation/serialize';
3
+ import { AppendOnlyTreeSnapshot, MerkleTreeId } from '@aztec/stdlib/trees';
4
+ import type { StateReference } from '@aztec/stdlib/tx';
5
+ import type { UInt32 } from '@aztec/stdlib/types';
6
+
7
+ export enum WorldStateMessageType {
8
+ GET_TREE_INFO = 100,
9
+ GET_STATE_REFERENCE,
10
+ GET_INITIAL_STATE_REFERENCE,
11
+
12
+ GET_LEAF_VALUE,
13
+ GET_LEAF_PREIMAGE,
14
+ GET_SIBLING_PATH,
15
+ GET_BLOCK_NUMBERS_FOR_LEAF_INDICES,
16
+
17
+ FIND_LEAF_INDICES,
18
+ FIND_LOW_LEAF,
19
+
20
+ APPEND_LEAVES,
21
+ BATCH_INSERT,
22
+ SEQUENTIAL_INSERT,
23
+
24
+ UPDATE_ARCHIVE,
25
+
26
+ COMMIT,
27
+ ROLLBACK,
28
+
29
+ SYNC_BLOCK,
30
+
31
+ CREATE_FORK,
32
+ DELETE_FORK,
33
+
34
+ FINALISE_BLOCKS,
35
+ UNWIND_BLOCKS,
36
+ REMOVE_HISTORICAL_BLOCKS,
37
+
38
+ GET_STATUS,
39
+
40
+ CREATE_CHECKPOINT,
41
+ COMMIT_CHECKPOINT,
42
+ REVERT_CHECKPOINT,
43
+
44
+ CLOSE = 999,
45
+ }
46
+
47
+ interface WithTreeId {
48
+ treeId: MerkleTreeId;
49
+ }
50
+
51
+ export interface WorldStateStatusSummary {
52
+ /** Last block number that can still be unwound. */
53
+ unfinalisedBlockNumber: bigint;
54
+ /** Last block number that is finalised and cannot be unwound. */
55
+ finalisedBlockNumber: bigint;
56
+ /** Oldest block still available for historical queries and forks. */
57
+ oldestHistoricalBlock: bigint;
58
+ /** Whether the trees are in sync with each other */
59
+ treesAreSynched: boolean;
60
+ }
61
+
62
+ export interface TreeMeta {
63
+ /** The name of the tree */
64
+ name: string;
65
+ /** The depth of the tree */
66
+ depth: number;
67
+ /** The current size of the tree (number of leaves) */
68
+ size: bigint;
69
+ /** The committed size of the tree */
70
+ committedSize: bigint;
71
+ /** The current root of the tree */
72
+ root: Fr;
73
+ /** The tree's initial size */
74
+ initialSize: bigint;
75
+ /** The tree's initial root value */
76
+ initialRoot: Fr;
77
+ /** The current oldest historical block number of the tree */
78
+ oldestHistoricBlock: bigint;
79
+ /** The current unfinalised block number of the tree */
80
+ unfinalisedBlockHeight: bigint;
81
+ /** The current finalised block number of the tree */
82
+ finalisedBlockHeight: bigint;
83
+ }
84
+
85
+ export interface DBStats {
86
+ /** The name of the DB */
87
+ name: string;
88
+ /** The total number of key/value pairs in the DB */
89
+ numDataItems: bigint;
90
+ /** The current mapped size of the DB */
91
+ totalUsedSize: bigint;
92
+ }
93
+
94
+ export interface TreeDBStats {
95
+ /** The configured max size of the DB mapping file (effectively the max possible size of the DB) */
96
+ mapSize: bigint;
97
+ /** Stats for the 'blocks' DB */
98
+ blocksDBStats: DBStats;
99
+ /** Stats for the 'nodes' DB */
100
+ nodesDBStats: DBStats;
101
+ /** Stats for the 'leaf pre-images' DB */
102
+ leafPreimagesDBStats: DBStats;
103
+ /** Stats for the 'leaf indices' DB */
104
+ leafIndicesDBStats: DBStats;
105
+ /** Stats for the 'block indices' DB */
106
+ blockIndicesDBStats: DBStats;
107
+ }
108
+
109
+ export interface WorldStateMeta {
110
+ /** Tree meta for the note hash tree */
111
+ noteHashTreeMeta: TreeMeta;
112
+ /** Tree meta for the message tree */
113
+ messageTreeMeta: TreeMeta;
114
+ /** Tree meta for the archive tree */
115
+ archiveTreeMeta: TreeMeta;
116
+ /** Tree meta for the public data tree */
117
+ publicDataTreeMeta: TreeMeta;
118
+ /** Tree meta for the nullifier tree */
119
+ nullifierTreeMeta: TreeMeta;
120
+ }
121
+
122
+ export interface WorldStateDBStats {
123
+ /** Full stats for the note hash tree */
124
+ noteHashTreeStats: TreeDBStats;
125
+ /** Full stats for the message tree */
126
+ messageTreeStats: TreeDBStats;
127
+ /** Full stats for the archive tree */
128
+ archiveTreeStats: TreeDBStats;
129
+ /** Full stats for the public data tree */
130
+ publicDataTreeStats: TreeDBStats;
131
+ /** Full stats for the nullifier tree */
132
+ nullifierTreeStats: TreeDBStats;
133
+ }
134
+
135
+ export interface WorldStateStatusFull {
136
+ summary: WorldStateStatusSummary;
137
+ dbStats: WorldStateDBStats;
138
+ meta: WorldStateMeta;
139
+ }
140
+
141
+ export function buildEmptyDBStats() {
142
+ return {
143
+ name: '',
144
+ numDataItems: 0n,
145
+ totalUsedSize: 0n,
146
+ } as DBStats;
147
+ }
148
+
149
+ export function buildEmptyTreeDBStats() {
150
+ return {
151
+ mapSize: 0n,
152
+ blocksDBStats: buildEmptyDBStats(),
153
+ nodesDBStats: buildEmptyDBStats(),
154
+ leafIndicesDBStats: buildEmptyDBStats(),
155
+ leafKeysDBStats: buildEmptyDBStats(),
156
+ leafPreimagesDBStats: buildEmptyDBStats(),
157
+ blockIndicesDBStats: buildEmptyDBStats(),
158
+ } as TreeDBStats;
159
+ }
160
+
161
+ export function buildEmptyTreeMeta() {
162
+ return {
163
+ name: '',
164
+ depth: 0,
165
+ size: 0n,
166
+ committedSize: 0n,
167
+ unfinalisedBlockHeight: 0n,
168
+ finalisedBlockHeight: 0n,
169
+ oldestHistoricBlock: 0n,
170
+ root: Fr.ZERO,
171
+ initialRoot: Fr.ZERO,
172
+ initialSize: 0n,
173
+ } as TreeMeta;
174
+ }
175
+
176
+ export function buildEmptyWorldStateMeta() {
177
+ return {
178
+ noteHashTreeMeta: buildEmptyTreeMeta(),
179
+ messageTreeMeta: buildEmptyTreeMeta(),
180
+ publicDataTreeMeta: buildEmptyTreeMeta(),
181
+ nullifierTreeMeta: buildEmptyTreeMeta(),
182
+ archiveTreeMeta: buildEmptyTreeMeta(),
183
+ } as WorldStateMeta;
184
+ }
185
+
186
+ export function buildEmptyWorldStateDBStats() {
187
+ return {
188
+ noteHashTreeStats: buildEmptyTreeDBStats(),
189
+ archiveTreeStats: buildEmptyTreeDBStats(),
190
+ messageTreeStats: buildEmptyTreeDBStats(),
191
+ publicDataTreeStats: buildEmptyTreeDBStats(),
192
+ nullifierTreeStats: buildEmptyTreeDBStats(),
193
+ } as WorldStateDBStats;
194
+ }
195
+
196
+ export function buildEmptyWorldStateSummary() {
197
+ return {
198
+ unfinalisedBlockNumber: 0n,
199
+ finalisedBlockNumber: 0n,
200
+ oldestHistoricalBlock: 0n,
201
+ treesAreSynched: true,
202
+ } as WorldStateStatusSummary;
203
+ }
204
+
205
+ export function buildEmptyWorldStateStatusFull() {
206
+ return {
207
+ meta: buildEmptyWorldStateMeta(),
208
+ dbStats: buildEmptyWorldStateDBStats(),
209
+ summary: buildEmptyWorldStateSummary(),
210
+ } as WorldStateStatusFull;
211
+ }
212
+
213
+ export function sanitiseSummary(summary: WorldStateStatusSummary) {
214
+ summary.finalisedBlockNumber = BigInt(summary.finalisedBlockNumber);
215
+ summary.unfinalisedBlockNumber = BigInt(summary.unfinalisedBlockNumber);
216
+ summary.oldestHistoricalBlock = BigInt(summary.oldestHistoricalBlock);
217
+ return summary;
218
+ }
219
+
220
+ export function sanitiseDBStats(stats: DBStats) {
221
+ stats.numDataItems = BigInt(stats.numDataItems);
222
+ stats.totalUsedSize = BigInt(stats.totalUsedSize);
223
+ return stats;
224
+ }
225
+
226
+ export function sanitiseMeta(meta: TreeMeta) {
227
+ meta.committedSize = BigInt(meta.committedSize);
228
+ meta.finalisedBlockHeight = BigInt(meta.finalisedBlockHeight);
229
+ meta.initialSize = BigInt(meta.initialSize);
230
+ meta.oldestHistoricBlock = BigInt(meta.oldestHistoricBlock);
231
+ meta.size = BigInt(meta.size);
232
+ meta.unfinalisedBlockHeight = BigInt(meta.unfinalisedBlockHeight);
233
+ return meta;
234
+ }
235
+
236
+ export function sanitiseTreeDBStats(stats: TreeDBStats) {
237
+ stats.blocksDBStats = sanitiseDBStats(stats.blocksDBStats);
238
+ stats.leafIndicesDBStats = sanitiseDBStats(stats.leafIndicesDBStats);
239
+ stats.leafPreimagesDBStats = sanitiseDBStats(stats.leafPreimagesDBStats);
240
+ stats.blockIndicesDBStats = sanitiseDBStats(stats.blockIndicesDBStats);
241
+ stats.nodesDBStats = sanitiseDBStats(stats.nodesDBStats);
242
+ stats.mapSize = BigInt(stats.mapSize);
243
+ return stats;
244
+ }
245
+
246
+ export function sanitiseWorldStateDBStats(stats: WorldStateDBStats) {
247
+ stats.archiveTreeStats = sanitiseTreeDBStats(stats.archiveTreeStats);
248
+ stats.messageTreeStats = sanitiseTreeDBStats(stats.messageTreeStats);
249
+ stats.noteHashTreeStats = sanitiseTreeDBStats(stats.noteHashTreeStats);
250
+ stats.nullifierTreeStats = sanitiseTreeDBStats(stats.nullifierTreeStats);
251
+ stats.publicDataTreeStats = sanitiseTreeDBStats(stats.publicDataTreeStats);
252
+ return stats;
253
+ }
254
+
255
+ export function sanitiseWorldStateTreeMeta(meta: WorldStateMeta) {
256
+ meta.archiveTreeMeta = sanitiseMeta(meta.archiveTreeMeta);
257
+ meta.messageTreeMeta = sanitiseMeta(meta.messageTreeMeta);
258
+ meta.noteHashTreeMeta = sanitiseMeta(meta.noteHashTreeMeta);
259
+ meta.nullifierTreeMeta = sanitiseMeta(meta.nullifierTreeMeta);
260
+ meta.publicDataTreeMeta = sanitiseMeta(meta.publicDataTreeMeta);
261
+ return meta;
262
+ }
263
+
264
+ export function sanitiseFullStatus(status: WorldStateStatusFull) {
265
+ status.dbStats = sanitiseWorldStateDBStats(status.dbStats);
266
+ status.summary = sanitiseSummary(status.summary);
267
+ status.meta = sanitiseWorldStateTreeMeta(status.meta);
268
+ return status;
269
+ }
270
+
271
+ interface WithForkId {
272
+ forkId: number;
273
+ }
274
+
275
+ interface WithWorldStateRevision {
276
+ revision: WorldStateRevision;
277
+ }
278
+
279
+ interface WithCanonicalForkId {
280
+ canonical: true;
281
+ }
282
+
283
+ interface WithLeafIndex {
284
+ leafIndex: bigint;
285
+ }
286
+
287
+ export type SerializedLeafValue =
288
+ | Buffer // Fr
289
+ | { value: Buffer } // NullifierLeaf
290
+ | { value: Buffer; slot: Buffer }; // PublicDataTreeLeaf
291
+
292
+ export type SerializedIndexedLeaf = {
293
+ value: Exclude<SerializedLeafValue, Buffer>;
294
+ nextIndex: bigint | number;
295
+ nextValue: Buffer; // Fr
296
+ };
297
+
298
+ interface WithLeafValues {
299
+ leaves: SerializedLeafValue[];
300
+ }
301
+
302
+ interface BlockShiftRequest extends WithCanonicalForkId {
303
+ toBlockNumber: bigint;
304
+ }
305
+
306
+ interface WithLeaves {
307
+ leaves: SerializedLeafValue[];
308
+ }
309
+
310
+ interface GetTreeInfoRequest extends WithTreeId, WithWorldStateRevision {}
311
+ interface GetTreeInfoResponse {
312
+ treeId: MerkleTreeId;
313
+ depth: UInt32;
314
+ size: bigint | number;
315
+ root: Buffer;
316
+ }
317
+
318
+ interface GetBlockNumbersForLeafIndicesRequest extends WithTreeId, WithWorldStateRevision {
319
+ leafIndices: bigint[];
320
+ }
321
+
322
+ interface GetBlockNumbersForLeafIndicesResponse {
323
+ blockNumbers: bigint[];
324
+ }
325
+
326
+ interface GetSiblingPathRequest extends WithTreeId, WithLeafIndex, WithWorldStateRevision {}
327
+ type GetSiblingPathResponse = Buffer[];
328
+
329
+ interface GetStateReferenceRequest extends WithWorldStateRevision {}
330
+ interface GetStateReferenceResponse {
331
+ state: Record<MerkleTreeId, TreeStateReference>;
332
+ }
333
+
334
+ interface GetLeafRequest extends WithTreeId, WithWorldStateRevision, WithLeafIndex {}
335
+ type GetLeafResponse = SerializedLeafValue | undefined;
336
+
337
+ interface GetLeafPreImageRequest extends WithTreeId, WithLeafIndex, WithWorldStateRevision {}
338
+ type GetLeafPreImageResponse = SerializedIndexedLeaf | undefined;
339
+
340
+ interface FindLeafIndicesRequest extends WithTreeId, WithLeafValues, WithWorldStateRevision {
341
+ startIndex: bigint;
342
+ }
343
+ interface FindLeafIndicesResponse {
344
+ indices: bigint[];
345
+ }
346
+
347
+ interface FindLowLeafRequest extends WithTreeId, WithWorldStateRevision {
348
+ key: Fr;
349
+ }
350
+ interface FindLowLeafResponse {
351
+ index: bigint | number;
352
+ alreadyPresent: boolean;
353
+ }
354
+
355
+ interface AppendLeavesRequest extends WithTreeId, WithForkId, WithLeaves {}
356
+
357
+ interface BatchInsertRequest extends WithTreeId, WithForkId, WithLeaves {
358
+ subtreeDepth: number;
359
+ }
360
+
361
+ interface BatchInsertResponse {
362
+ low_leaf_witness_data: ReadonlyArray<{
363
+ leaf: SerializedIndexedLeaf;
364
+ index: bigint | number;
365
+ path: Tuple<Buffer, number>;
366
+ }>;
367
+ sorted_leaves: ReadonlyArray<[SerializedLeafValue, UInt32]>;
368
+ subtree_path: Tuple<Buffer, number>;
369
+ }
370
+
371
+ interface SequentialInsertRequest extends WithTreeId, WithForkId, WithLeaves {}
372
+
373
+ interface SequentialInsertResponse {
374
+ low_leaf_witness_data: ReadonlyArray<{
375
+ leaf: SerializedIndexedLeaf;
376
+ index: bigint | number;
377
+ path: Tuple<Buffer, number>;
378
+ }>;
379
+ insertion_witness_data: ReadonlyArray<{
380
+ leaf: SerializedIndexedLeaf;
381
+ index: bigint | number;
382
+ path: Tuple<Buffer, number>;
383
+ }>;
384
+ }
385
+
386
+ interface UpdateArchiveRequest extends WithForkId {
387
+ blockStateRef: BlockStateReference;
388
+ blockHeaderHash: Buffer;
389
+ }
390
+
391
+ interface SyncBlockRequest extends WithCanonicalForkId {
392
+ blockNumber: number;
393
+ blockStateRef: BlockStateReference;
394
+ blockHeaderHash: Fr;
395
+ paddedNoteHashes: readonly SerializedLeafValue[];
396
+ paddedL1ToL2Messages: readonly SerializedLeafValue[];
397
+ paddedNullifiers: readonly SerializedLeafValue[];
398
+ publicDataWrites: readonly SerializedLeafValue[];
399
+ }
400
+
401
+ interface CreateForkRequest extends WithCanonicalForkId {
402
+ latest: boolean;
403
+ blockNumber: number;
404
+ }
405
+
406
+ interface CreateForkResponse {
407
+ forkId: number;
408
+ }
409
+
410
+ interface DeleteForkRequest extends WithForkId {}
411
+
412
+ export type WorldStateRequestCategories = WithForkId | WithWorldStateRevision | WithCanonicalForkId;
413
+
414
+ export function isWithForkId(body: WorldStateRequestCategories): body is WithForkId {
415
+ return body && 'forkId' in body;
416
+ }
417
+
418
+ export function isWithRevision(body: WorldStateRequestCategories): body is WithWorldStateRevision {
419
+ return body && 'revision' in body;
420
+ }
421
+
422
+ export function isWithCanonical(body: WorldStateRequestCategories): body is WithCanonicalForkId {
423
+ return body && 'canonical' in body;
424
+ }
425
+
426
+ export type WorldStateRequest = {
427
+ [WorldStateMessageType.GET_TREE_INFO]: GetTreeInfoRequest;
428
+ [WorldStateMessageType.GET_STATE_REFERENCE]: GetStateReferenceRequest;
429
+ [WorldStateMessageType.GET_INITIAL_STATE_REFERENCE]: WithCanonicalForkId;
430
+
431
+ [WorldStateMessageType.GET_LEAF_VALUE]: GetLeafRequest;
432
+ [WorldStateMessageType.GET_LEAF_PREIMAGE]: GetLeafPreImageRequest;
433
+ [WorldStateMessageType.GET_SIBLING_PATH]: GetSiblingPathRequest;
434
+ [WorldStateMessageType.GET_BLOCK_NUMBERS_FOR_LEAF_INDICES]: GetBlockNumbersForLeafIndicesRequest;
435
+
436
+ [WorldStateMessageType.FIND_LEAF_INDICES]: FindLeafIndicesRequest;
437
+ [WorldStateMessageType.FIND_LOW_LEAF]: FindLowLeafRequest;
438
+
439
+ [WorldStateMessageType.APPEND_LEAVES]: AppendLeavesRequest;
440
+ [WorldStateMessageType.BATCH_INSERT]: BatchInsertRequest;
441
+ [WorldStateMessageType.SEQUENTIAL_INSERT]: SequentialInsertRequest;
442
+
443
+ [WorldStateMessageType.UPDATE_ARCHIVE]: UpdateArchiveRequest;
444
+
445
+ [WorldStateMessageType.COMMIT]: WithCanonicalForkId;
446
+ [WorldStateMessageType.ROLLBACK]: WithCanonicalForkId;
447
+
448
+ [WorldStateMessageType.SYNC_BLOCK]: SyncBlockRequest;
449
+
450
+ [WorldStateMessageType.CREATE_FORK]: CreateForkRequest;
451
+ [WorldStateMessageType.DELETE_FORK]: DeleteForkRequest;
452
+
453
+ [WorldStateMessageType.REMOVE_HISTORICAL_BLOCKS]: BlockShiftRequest;
454
+ [WorldStateMessageType.UNWIND_BLOCKS]: BlockShiftRequest;
455
+ [WorldStateMessageType.FINALISE_BLOCKS]: BlockShiftRequest;
456
+
457
+ [WorldStateMessageType.GET_STATUS]: WithCanonicalForkId;
458
+
459
+ [WorldStateMessageType.CREATE_CHECKPOINT]: WithForkId;
460
+ [WorldStateMessageType.COMMIT_CHECKPOINT]: WithForkId;
461
+ [WorldStateMessageType.REVERT_CHECKPOINT]: WithForkId;
462
+
463
+ [WorldStateMessageType.CLOSE]: WithCanonicalForkId;
464
+ };
465
+
466
+ export type WorldStateResponse = {
467
+ [WorldStateMessageType.GET_TREE_INFO]: GetTreeInfoResponse;
468
+ [WorldStateMessageType.GET_STATE_REFERENCE]: GetStateReferenceResponse;
469
+ [WorldStateMessageType.GET_INITIAL_STATE_REFERENCE]: GetStateReferenceResponse;
470
+
471
+ [WorldStateMessageType.GET_LEAF_VALUE]: GetLeafResponse;
472
+ [WorldStateMessageType.GET_LEAF_PREIMAGE]: GetLeafPreImageResponse;
473
+ [WorldStateMessageType.GET_SIBLING_PATH]: GetSiblingPathResponse;
474
+ [WorldStateMessageType.GET_BLOCK_NUMBERS_FOR_LEAF_INDICES]: GetBlockNumbersForLeafIndicesResponse;
475
+
476
+ [WorldStateMessageType.FIND_LEAF_INDICES]: FindLeafIndicesResponse;
477
+ [WorldStateMessageType.FIND_LOW_LEAF]: FindLowLeafResponse;
478
+
479
+ [WorldStateMessageType.APPEND_LEAVES]: void;
480
+ [WorldStateMessageType.BATCH_INSERT]: BatchInsertResponse;
481
+ [WorldStateMessageType.SEQUENTIAL_INSERT]: SequentialInsertResponse;
482
+
483
+ [WorldStateMessageType.UPDATE_ARCHIVE]: void;
484
+
485
+ [WorldStateMessageType.COMMIT]: void;
486
+ [WorldStateMessageType.ROLLBACK]: void;
487
+
488
+ [WorldStateMessageType.SYNC_BLOCK]: WorldStateStatusFull;
489
+
490
+ [WorldStateMessageType.CREATE_FORK]: CreateForkResponse;
491
+ [WorldStateMessageType.DELETE_FORK]: void;
492
+
493
+ [WorldStateMessageType.REMOVE_HISTORICAL_BLOCKS]: WorldStateStatusFull;
494
+ [WorldStateMessageType.UNWIND_BLOCKS]: WorldStateStatusFull;
495
+ [WorldStateMessageType.FINALISE_BLOCKS]: WorldStateStatusSummary;
496
+
497
+ [WorldStateMessageType.GET_STATUS]: WorldStateStatusSummary;
498
+
499
+ [WorldStateMessageType.CREATE_CHECKPOINT]: void;
500
+ [WorldStateMessageType.COMMIT_CHECKPOINT]: void;
501
+ [WorldStateMessageType.REVERT_CHECKPOINT]: void;
502
+
503
+ [WorldStateMessageType.CLOSE]: void;
504
+ };
505
+
506
+ export type WorldStateRevision = {
507
+ forkId: number;
508
+ blockNumber: number;
509
+ includeUncommitted: boolean;
510
+ };
511
+ export function worldStateRevision(
512
+ includeUncommitted: boolean,
513
+ forkId: number | undefined,
514
+ blockNumber: number | undefined,
515
+ ): WorldStateRevision {
516
+ return {
517
+ forkId: forkId ?? 0,
518
+ blockNumber: blockNumber ?? 0,
519
+ includeUncommitted,
520
+ };
521
+ }
522
+
523
+ type TreeStateReference = readonly [Buffer, number | bigint];
524
+ type BlockStateReference = Map<Exclude<MerkleTreeId, MerkleTreeId.ARCHIVE>, TreeStateReference>;
525
+
526
+ export function treeStateReferenceToSnapshot([root, size]: TreeStateReference): AppendOnlyTreeSnapshot {
527
+ return new AppendOnlyTreeSnapshot(Fr.fromBuffer(root), Number(size));
528
+ }
529
+
530
+ export function treeStateReference(snapshot: AppendOnlyTreeSnapshot) {
531
+ return [snapshot.root.toBuffer(), BigInt(snapshot.nextAvailableLeafIndex)] as const;
532
+ }
533
+
534
+ export function blockStateReference(state: StateReference): BlockStateReference {
535
+ return new Map([
536
+ [MerkleTreeId.NULLIFIER_TREE, treeStateReference(state.partial.nullifierTree)],
537
+ [MerkleTreeId.NOTE_HASH_TREE, treeStateReference(state.partial.noteHashTree)],
538
+ [MerkleTreeId.PUBLIC_DATA_TREE, treeStateReference(state.partial.publicDataTree)],
539
+ [MerkleTreeId.L1_TO_L2_MESSAGE_TREE, treeStateReference(state.l1ToL2MessageTree)],
540
+ ]);
541
+ }