@aztec/world-state 0.0.1-commit.d3ec352c → 0.0.1-commit.d58ff9d0

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 (80) 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 +21 -50
  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 +2 -1
  11. package/dest/native/index.d.ts.map +1 -1
  12. package/dest/native/index.js +1 -0
  13. package/dest/native/ipc_world_state_instance.d.ts +89 -0
  14. package/dest/native/ipc_world_state_instance.d.ts.map +1 -0
  15. package/dest/native/ipc_world_state_instance.js +661 -0
  16. package/dest/native/merkle_trees_facade.d.ts +18 -9
  17. package/dest/native/merkle_trees_facade.d.ts.map +1 -1
  18. package/dest/native/merkle_trees_facade.js +90 -113
  19. package/dest/native/message.d.ts +15 -222
  20. package/dest/native/message.d.ts.map +1 -1
  21. package/dest/native/message.js +1 -43
  22. package/dest/native/native_world_state.d.ts +26 -12
  23. package/dest/native/native_world_state.d.ts.map +1 -1
  24. package/dest/native/native_world_state.js +126 -62
  25. package/dest/native/native_world_state_instance.d.ts +56 -42
  26. package/dest/native/native_world_state_instance.d.ts.map +1 -1
  27. package/dest/native/native_world_state_instance.js +1 -203
  28. package/dest/native/world_state_operation.d.ts +7 -0
  29. package/dest/native/world_state_operation.d.ts.map +1 -0
  30. package/dest/native/world_state_operation.js +5 -0
  31. package/dest/synchronizer/config.d.ts +3 -5
  32. package/dest/synchronizer/config.d.ts.map +1 -1
  33. package/dest/synchronizer/config.js +15 -18
  34. package/dest/synchronizer/errors.d.ts +8 -1
  35. package/dest/synchronizer/errors.d.ts.map +1 -1
  36. package/dest/synchronizer/errors.js +8 -1
  37. package/dest/synchronizer/factory.d.ts +6 -5
  38. package/dest/synchronizer/factory.d.ts.map +1 -1
  39. package/dest/synchronizer/factory.js +7 -6
  40. package/dest/synchronizer/index.d.ts +2 -1
  41. package/dest/synchronizer/index.d.ts.map +1 -1
  42. package/dest/synchronizer/index.js +1 -0
  43. package/dest/synchronizer/server_world_state_synchronizer.d.ts +16 -10
  44. package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -1
  45. package/dest/synchronizer/server_world_state_synchronizer.js +170 -57
  46. package/dest/test/utils.d.ts +11 -17
  47. package/dest/test/utils.d.ts.map +1 -1
  48. package/dest/test/utils.js +49 -81
  49. package/dest/testing.d.ts +5 -4
  50. package/dest/testing.d.ts.map +1 -1
  51. package/dest/testing.js +15 -8
  52. package/dest/world-state-db/merkle_tree_db.d.ts +7 -18
  53. package/dest/world-state-db/merkle_tree_db.d.ts.map +1 -1
  54. package/package.json +14 -12
  55. package/src/index.ts +1 -0
  56. package/src/instrumentation/instrumentation.ts +23 -59
  57. package/src/native/fork_checkpoint.ts +19 -3
  58. package/src/native/index.ts +1 -0
  59. package/src/native/ipc_world_state_instance.ts +834 -0
  60. package/src/native/merkle_trees_facade.ts +119 -106
  61. package/src/native/message.ts +14 -287
  62. package/src/native/native_world_state.ts +159 -99
  63. package/src/native/native_world_state_instance.ts +94 -279
  64. package/src/native/world_state_operation.ts +33 -0
  65. package/src/synchronizer/config.ts +16 -23
  66. package/src/synchronizer/errors.ts +8 -0
  67. package/src/synchronizer/factory.ts +18 -11
  68. package/src/synchronizer/index.ts +1 -0
  69. package/src/synchronizer/server_world_state_synchronizer.ts +203 -78
  70. package/src/test/utils.ts +79 -128
  71. package/src/testing.ts +13 -11
  72. package/src/world-state-db/merkle_tree_db.ts +10 -22
  73. package/dest/native/world_state_ops_queue.d.ts +0 -19
  74. package/dest/native/world_state_ops_queue.d.ts.map +0 -1
  75. package/dest/native/world_state_ops_queue.js +0 -146
  76. package/dest/synchronizer/utils.d.ts +0 -11
  77. package/dest/synchronizer/utils.d.ts.map +0 -1
  78. package/dest/synchronizer/utils.js +0 -59
  79. package/src/native/world_state_ops_queue.ts +0 -190
  80. package/src/synchronizer/utils.ts +0 -83
@@ -1,190 +0,0 @@
1
- import { promiseWithResolvers } from '@aztec/foundation/promise';
2
-
3
- import { WorldStateMessageType } from './message.js';
4
-
5
- /**
6
- * This is the implementation for queueing requests to the world state.
7
- * Requests need to be queued for the world state to ensure that writes are correctly ordered
8
- * and reads return the correct data.
9
- * Due to the nature of the NAPI we can't really do this there.
10
- *
11
- * The rules for queueing are as follows:
12
- *
13
- * 1. Reads of committed state never need to be queued. LMDB uses MVCC to ensure readers see a consistent view of the DB.
14
- * 2. Reads of uncommitted state can happen concurrently with other reads of uncommitted state on the same fork (or reads of committed state)
15
- * 3. All writes require exclusive access to their respective fork
16
- *
17
- */
18
-
19
- type WorldStateOp = {
20
- requestId: number;
21
- mutating: boolean;
22
- request: () => Promise<any>;
23
- promise: PromiseWithResolvers<any>;
24
- };
25
-
26
- // These are the set of message types that implement mutating operations
27
- // Messages of these types require exclusive access to their given forks
28
- export const MUTATING_MSG_TYPES = new Set([
29
- WorldStateMessageType.APPEND_LEAVES,
30
- WorldStateMessageType.BATCH_INSERT,
31
- WorldStateMessageType.SEQUENTIAL_INSERT,
32
- WorldStateMessageType.UPDATE_ARCHIVE,
33
- WorldStateMessageType.COMMIT,
34
- WorldStateMessageType.ROLLBACK,
35
- WorldStateMessageType.SYNC_BLOCK,
36
- WorldStateMessageType.CREATE_FORK,
37
- WorldStateMessageType.DELETE_FORK,
38
- WorldStateMessageType.FINALIZE_BLOCKS,
39
- WorldStateMessageType.UNWIND_BLOCKS,
40
- WorldStateMessageType.REMOVE_HISTORICAL_BLOCKS,
41
- WorldStateMessageType.CREATE_CHECKPOINT,
42
- WorldStateMessageType.COMMIT_CHECKPOINT,
43
- WorldStateMessageType.REVERT_CHECKPOINT,
44
- ]);
45
-
46
- // This class implements the per-fork operation queue
47
- export class WorldStateOpsQueue {
48
- private requests: WorldStateOp[] = [];
49
- private inFlightMutatingCount = 0;
50
- private inFlightCount = 0;
51
- private stopPromise?: Promise<void>;
52
- private stopResolve?: () => void;
53
- private requestId = 0;
54
- private ops: Map<number, WorldStateOp> = new Map();
55
-
56
- // The primary public api, this is where an operation is queued
57
- // We return a promise that will ultimately be resolved/rejected with the response/error generated by the 'request' argument
58
- public execute(request: () => Promise<any>, messageType: WorldStateMessageType, committedOnly: boolean) {
59
- if (this.stopResolve !== undefined) {
60
- throw new Error('Unable to send request to world state, queue already stopped');
61
- }
62
-
63
- const op: WorldStateOp = {
64
- requestId: this.requestId++,
65
- mutating: MUTATING_MSG_TYPES.has(messageType),
66
- request,
67
- promise: promiseWithResolvers(),
68
- };
69
- this.ops.set(op.requestId, op);
70
-
71
- // Perform the appropriate action based upon the queueing rules
72
- if (op.mutating) {
73
- this.executeMutating(op);
74
- } else if (committedOnly === false) {
75
- this.executeNonMutatingUncommitted(op);
76
- } else {
77
- this.executeNonMutatingCommitted(op);
78
- }
79
- return op.promise.promise;
80
- }
81
-
82
- // Mutating requests need exclusive access
83
- private executeMutating(op: WorldStateOp) {
84
- // If nothing is in flight then we send the request immediately
85
- // Otherwise add to the queue
86
- if (this.inFlightCount === 0) {
87
- this.sendEnqueuedRequest(op);
88
- } else {
89
- this.requests.push(op);
90
- }
91
- }
92
-
93
- // Non mutating requests including uncommitted state
94
- private executeNonMutatingUncommitted(op: WorldStateOp) {
95
- // If there are no mutating requests in flight and there is nothing queued
96
- // then send the request immediately
97
- // If a mutating request is in flight then we must wait
98
- // If a mutating request is not in flight but something is queued then it must be a mutating request
99
- if (this.inFlightMutatingCount == 0 && this.requests.length == 0) {
100
- this.sendEnqueuedRequest(op);
101
- } else {
102
- this.requests.push(op);
103
- }
104
- }
105
-
106
- private executeNonMutatingCommitted(op: WorldStateOp) {
107
- // This is a non-mutating request for committed data
108
- // It can always be sent
109
- op.request()
110
- .then(op.promise.resolve, op.promise.reject)
111
- .finally(() => {
112
- this.ops.delete(op.requestId);
113
- });
114
- }
115
-
116
- private checkAndEnqueue(completedOp: WorldStateOp) {
117
- // As request has completed
118
- // First we decrements the relevant in flight counters
119
- if (completedOp.mutating) {
120
- --this.inFlightMutatingCount;
121
- }
122
- --this.inFlightCount;
123
-
124
- // If there are still requests in flight then do nothing further
125
- if (this.inFlightCount != 0) {
126
- return;
127
- }
128
-
129
- // No requests in flight, send next queued requests
130
- // We loop and send:
131
- // 1 mutating request if it is next in the queue
132
- // As many non-mutating requests as we encounter until
133
- // we exhaust the queue or we reach a mutating request
134
- while (this.requests.length > 0) {
135
- const next = this.requests[0];
136
- if (next.mutating) {
137
- if (this.inFlightCount == 0) {
138
- // send the mutating request
139
- this.requests.shift();
140
- this.sendEnqueuedRequest(next);
141
- }
142
- // this request is mutating, we need to stop here
143
- break;
144
- } else {
145
- // not mutating, send and go round again
146
- this.requests.shift();
147
- this.sendEnqueuedRequest(next);
148
- }
149
- }
150
-
151
- // If the queue is empty, there is nothing in flight and we have been told to stop, then resolve the stop promise
152
- if (this.inFlightCount == 0 && this.stopResolve !== undefined) {
153
- this.stopResolve();
154
- }
155
- }
156
-
157
- private sendEnqueuedRequest(op: WorldStateOp) {
158
- // Here we increment the in flight counts before sending
159
- ++this.inFlightCount;
160
- if (op.mutating) {
161
- ++this.inFlightMutatingCount;
162
- }
163
-
164
- // Make the request and pass the response/error through to the stored promise
165
- op.request()
166
- .then(op.promise.resolve, op.promise.reject)
167
- .finally(() => {
168
- this.checkAndEnqueue(op);
169
- this.ops.delete(op.requestId);
170
- });
171
- }
172
-
173
- public stop() {
174
- // If there is already a stop promise then return it
175
- if (this.stopPromise) {
176
- return this.stopPromise;
177
- }
178
-
179
- // Otherwise create a new one and capture the resolve method
180
- this.stopPromise = new Promise(resolve => {
181
- this.stopResolve = resolve;
182
- });
183
-
184
- // If no outstanding requests then immediately resolve the promise
185
- if (this.requests.length == 0 && this.inFlightCount == 0 && this.stopResolve !== undefined) {
186
- this.stopResolve();
187
- }
188
- return this.stopPromise;
189
- }
190
- }
@@ -1,83 +0,0 @@
1
- import { BlockNumber, type SlotNumber } from '@aztec/foundation/branded-types';
2
- import type { Fr } from '@aztec/foundation/fields';
3
- import type { L2BlockNew, L2BlockSource } from '@aztec/stdlib/block';
4
- import type { Checkpoint } from '@aztec/stdlib/checkpoint';
5
- import { type L1ToL2MessageSource, computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
6
-
7
- /**
8
- * Determine which blocks in the given array are the first block in a checkpoint.
9
- * @param blocks - The candidate blocks, sorted by block number in ascending order.
10
- * @param l2BlockSource - The L2 block source to use to fetch the checkpoints, block headers and L1->L2 messages.
11
- * @returns A map of block numbers that begin a checkpoint to the L1->L2 messages for that checkpoint.
12
- */
13
- export async function findFirstBlocksInCheckpoints(
14
- blocks: L2BlockNew[],
15
- l2BlockSource: L2BlockSource & L1ToL2MessageSource,
16
- ): Promise<Map<number, Fr[]>> {
17
- // Select the blocks that are the final block within each group of identical slot numbers.
18
- let seenSlot: SlotNumber | undefined;
19
- const maybeLastBlocks = [...blocks]
20
- .reverse()
21
- .filter(b => {
22
- if (b.header.globalVariables.slotNumber !== seenSlot) {
23
- seenSlot = b.header.globalVariables.slotNumber;
24
- return true;
25
- }
26
- return false;
27
- })
28
- .reverse();
29
-
30
- // Try to fetch the checkpoints for those blocks. If undefined (which should only occur for blocks.at(-1)),
31
- // then the block is not the last one in a checkpoint.
32
- // If we are not checking the inHashes below, only blocks.at(-1) would need its checkpoint header fetched.
33
- const checkpointedBlocks = (
34
- await Promise.all(
35
- maybeLastBlocks.map(async b => ({
36
- blockNumber: b.number,
37
- // A checkpoint's archive root is the archive root of its last block.
38
- checkpoint: await l2BlockSource.getCheckpointByArchive(b.archive.root),
39
- })),
40
- )
41
- ).filter(b => b.checkpoint !== undefined) as { blockNumber: BlockNumber; checkpoint: Checkpoint }[];
42
-
43
- // Verify that the L1->L2 messages hash to the checkpoint's inHash.
44
- const checkpointedL1ToL2Messages: Fr[][] = await Promise.all(
45
- checkpointedBlocks.map(b => l2BlockSource.getL1ToL2MessagesForCheckpoint(b.checkpoint!.number)),
46
- );
47
- checkpointedBlocks.forEach((b, i) => {
48
- const computedInHash = computeInHashFromL1ToL2Messages(checkpointedL1ToL2Messages[i]);
49
- const inHash = b.checkpoint.header.contentCommitment.inHash;
50
- if (!computedInHash.equals(inHash)) {
51
- throw new Error('Obtained L1 to L2 messages failed to be hashed to the checkpoint inHash');
52
- }
53
- });
54
-
55
- // Compute the first block numbers, which should be right after each checkpointed block. Exclude blocks that haven't
56
- // been added yet.
57
- const firstBlockNumbers = checkpointedBlocks
58
- .map(b => BlockNumber(b.blockNumber + 1))
59
- .filter(n => n <= blocks.at(-1)!.number);
60
- // Check if blocks[0] is the first block in a checkpoint.
61
- if (blocks[0].number === 1) {
62
- firstBlockNumbers.push(blocks[0].number);
63
- } else {
64
- const lastBlockHeader = await l2BlockSource.getBlockHeader(BlockNumber(blocks[0].number - 1));
65
- if (!lastBlockHeader) {
66
- throw new Error(`Failed to get block ${blocks[0].number - 1}`);
67
- }
68
- if (lastBlockHeader.globalVariables.slotNumber !== blocks[0].header.globalVariables.slotNumber) {
69
- firstBlockNumbers.push(blocks[0].number);
70
- }
71
- }
72
-
73
- // Fetch the L1->L2 messages for the first blocks and assign them to the map.
74
- const messagesByBlockNumber = new Map<BlockNumber, Fr[]>();
75
- await Promise.all(
76
- firstBlockNumbers.map(async blockNumber => {
77
- const l1ToL2Messages = await l2BlockSource.getL1ToL2Messages(blockNumber);
78
- messagesByBlockNumber.set(blockNumber, l1ToL2Messages);
79
- }),
80
- );
81
-
82
- return messagesByBlockNumber;
83
- }