@aztec/world-state 0.0.1-commit.3100065 → 0.0.1-commit.330febf

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 (45) 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 -495
  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 -233
  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 +105 -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 -223
  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/factory.d.ts +1 -1
  29. package/dest/synchronizer/factory.d.ts.map +1 -1
  30. package/dest/synchronizer/factory.js +1 -1
  31. package/package.json +11 -10
  32. package/src/index.ts +1 -0
  33. package/src/instrumentation/instrumentation.ts +6 -18
  34. package/src/native/index.ts +1 -0
  35. package/src/native/ipc_world_state_instance.ts +625 -503
  36. package/src/native/merkle_trees_facade.ts +59 -104
  37. package/src/native/message.ts +13 -300
  38. package/src/native/native_world_state.ts +136 -156
  39. package/src/native/native_world_state_instance.ts +94 -308
  40. package/src/native/world_state_operation.ts +33 -0
  41. package/src/synchronizer/factory.ts +0 -1
  42. package/dest/native/world_state_ops_queue.d.ts +0 -19
  43. package/dest/native/world_state_ops_queue.d.ts.map +0 -1
  44. package/dest/native/world_state_ops_queue.js +0 -146
  45. package/src/native/world_state_ops_queue.ts +0 -190
@@ -1,318 +1,104 @@
1
- import {
2
- ARCHIVE_HEIGHT,
3
- DomainSeparator,
4
- L1_TO_L2_MSG_TREE_HEIGHT,
5
- MAX_NULLIFIERS_PER_TX,
6
- MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
7
- NOTE_HASH_TREE_HEIGHT,
8
- NULLIFIER_TREE_HEIGHT,
9
- PUBLIC_DATA_TREE_HEIGHT,
10
- } from '@aztec/constants';
11
- import { type Logger, type LoggerBindings, createLogger } from '@aztec/foundation/log';
12
- import { NativeWorldState as BaseNativeWorldState, MsgpackChannel } from '@aztec/native';
13
- import { MerkleTreeId } from '@aztec/stdlib/trees';
14
- import { EMPTY_GENESIS_DATA, type GenesisData } from '@aztec/stdlib/world-state';
15
-
16
- import assert from 'assert';
17
- import { cpus } from 'os';
18
-
19
- import type { WorldStateInstrumentation } from '../instrumentation/instrumentation.js';
20
- import type { WorldStateTreeMapSizes } from '../synchronizer/factory.js';
21
- import {
22
- WorldStateMessageType,
23
- type WorldStateRequest,
24
- type WorldStateRequestCategories,
25
- type WorldStateResponse,
26
- isWithCanonical,
27
- isWithForkId,
28
- isWithRevision,
1
+ import type { BlockNumber } from '@aztec/foundation/branded-types';
2
+ import type { Fr } from '@aztec/foundation/curves/bn254';
3
+ import type { IndexedTreeId, TreeInfo } from '@aztec/stdlib/interfaces/server';
4
+ import type { MerkleTreeId } from '@aztec/stdlib/trees';
5
+ import type { WorldStateRevision } from '@aztec/stdlib/world-state';
6
+
7
+ import type {
8
+ BlockStateReference,
9
+ SerializedBatchInsertionResult,
10
+ SerializedIndexedLeaf,
11
+ SerializedLeafValue,
12
+ SerializedSequentialInsertionResult,
13
+ SerializedSiblingPathAndIndex,
14
+ TreeStateReference,
15
+ WorldStateStatusFull,
16
+ WorldStateStatusSummary,
29
17
  } from './message.js';
30
- import { WorldStateOpsQueue } from './world_state_ops_queue.js';
31
-
32
- const MAX_WORLD_STATE_THREADS = +(process.env.HARDWARE_CONCURRENCY || '16');
33
18
 
19
+ /** Backend-agnostic handle to a running aztec-wsdb world state, accessed by the TS layer. */
34
20
  export interface NativeWorldStateInstance {
35
- call<T extends WorldStateMessageType>(
36
- messageType: T,
37
- body: WorldStateRequest[T] & WorldStateRequestCategories,
38
- ): Promise<WorldStateResponse[T]>;
39
- // TODO(dbanks12): this returns any type, but we should strongly type it
40
- getHandle(): any;
41
- }
42
-
43
- /**
44
- * Strongly-typed interface to access the WorldState class in the native world_state_napi module.
45
- */
46
- export class NativeWorldState implements NativeWorldStateInstance {
47
- private open = true;
48
-
49
- // We maintain a map of queue to fork
50
- private queues = new Map<number, WorldStateOpsQueue>();
51
-
52
- private instance: MsgpackChannel<WorldStateMessageType, WorldStateRequest, WorldStateResponse>;
53
-
54
- /** Creates a new native WorldState instance */
55
- constructor(
56
- private readonly dataDir: string,
57
- private readonly wsTreeMapSizes: WorldStateTreeMapSizes,
58
- private readonly genesis: GenesisData = EMPTY_GENESIS_DATA,
59
- private readonly instrumentation: WorldStateInstrumentation,
60
- bindings?: LoggerBindings,
61
- private readonly log: Logger = createLogger('world-state:database', bindings),
62
- private readonly ephemeral: boolean = false,
63
- ) {
64
- const threads = Math.min(cpus().length, MAX_WORLD_STATE_THREADS);
65
- log.info(
66
- `Creating world state data store at directory ${dataDir} with map sizes ${JSON.stringify(
67
- wsTreeMapSizes,
68
- )} and ${threads} threads (ephemeral=${ephemeral}).`,
69
- );
70
- const prefilledPublicDataBufferArray = genesis.prefilledPublicData.map(d => [
71
- d.slot.toBuffer(),
72
- d.value.toBuffer(),
73
- ]);
74
- // Nullifiers to pre-insert into the genesis nullifier tree (empty by default, so production genesis roots are
75
- // unchanged). The native indexed nullifier tree requires its prefilled leaves to be unique and strictly
76
- // increasing, so we enforce that here before handing them over rather than failing deep inside the C++ tree
77
- // construction.
78
- const prefilledNullifiers = genesis.prefilledNullifiers ?? [];
79
- for (let i = 1; i < prefilledNullifiers.length; i++) {
80
- assert(
81
- prefilledNullifiers[i].toBigInt() > prefilledNullifiers[i - 1].toBigInt(),
82
- 'Prefilled genesis nullifiers must be unique and strictly increasing',
83
- );
84
- }
85
- const prefilledNullifiersBufferArray = prefilledNullifiers.map(n => n.toBuffer());
86
- const ws = new BaseNativeWorldState(
87
- dataDir,
88
- {
89
- [MerkleTreeId.NULLIFIER_TREE]: NULLIFIER_TREE_HEIGHT,
90
- [MerkleTreeId.NOTE_HASH_TREE]: NOTE_HASH_TREE_HEIGHT,
91
- [MerkleTreeId.PUBLIC_DATA_TREE]: PUBLIC_DATA_TREE_HEIGHT,
92
- [MerkleTreeId.L1_TO_L2_MESSAGE_TREE]: L1_TO_L2_MSG_TREE_HEIGHT,
93
- [MerkleTreeId.ARCHIVE]: ARCHIVE_HEIGHT,
94
- },
95
- {
96
- [MerkleTreeId.NULLIFIER_TREE]: 2 * MAX_NULLIFIERS_PER_TX,
97
- [MerkleTreeId.PUBLIC_DATA_TREE]: 2 * MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
98
- },
99
- prefilledPublicDataBufferArray,
100
- prefilledNullifiersBufferArray,
101
- DomainSeparator.BLOCK_HEADER_HASH,
102
- Number(genesis.genesisTimestamp),
103
- {
104
- [MerkleTreeId.NULLIFIER_TREE]: wsTreeMapSizes.nullifierTreeMapSizeKb,
105
- [MerkleTreeId.NOTE_HASH_TREE]: wsTreeMapSizes.noteHashTreeMapSizeKb,
106
- [MerkleTreeId.PUBLIC_DATA_TREE]: wsTreeMapSizes.publicDataTreeMapSizeKb,
107
- [MerkleTreeId.L1_TO_L2_MESSAGE_TREE]: wsTreeMapSizes.messageTreeMapSizeKb,
108
- [MerkleTreeId.ARCHIVE]: wsTreeMapSizes.archiveTreeMapSizeKb,
109
- },
110
- threads,
111
- ephemeral,
112
- );
113
- this.instance = new MsgpackChannel(ws);
114
- // Manually create the queue for the canonical fork
115
- this.queues.set(0, new WorldStateOpsQueue());
116
- }
117
-
118
- public getDataDir() {
119
- return this.dataDir;
120
- }
121
-
122
- public clone() {
123
- return new NativeWorldState(
124
- this.dataDir,
125
- this.wsTreeMapSizes,
126
- this.genesis,
127
- this.instrumentation,
128
- this.log.getBindings(),
129
- this.log,
130
- this.ephemeral,
131
- );
132
- }
133
-
134
- /**
135
- * Gets the native WorldState handle from the underlying native instance.
136
- * We call the getHandle() method on the native WorldState to get a NAPI External
137
- * that wraps the underlying C++ WorldState pointer.
138
- * @returns The NAPI External handle to the native WorldState instance,since
139
- * the NAPI external type is opaque, we return any (we could also use an opaque symbol type)
140
- */
141
- public getHandle(): any {
142
- const worldStateWrapper = (this.instance as any).dest;
143
-
144
- if (!worldStateWrapper) {
145
- throw new Error('No WorldStateWrapper found');
146
- }
147
-
148
- if (typeof worldStateWrapper.getHandle !== 'function') {
149
- throw new Error('WorldStateWrapper does not have getHandle method');
150
- }
151
-
152
- // Call getHandle() to get the NAPI External
153
- try {
154
- return worldStateWrapper.getHandle();
155
- } catch (error) {
156
- this.log.error('Failed to get native WorldState handle', error);
157
- }
158
- }
21
+ getTreeInfo(treeId: MerkleTreeId, revision: WorldStateRevision): Promise<TreeInfo>;
22
+ getStateReference(revision: WorldStateRevision): Promise<Record<number, TreeStateReference>>;
23
+ getInitialStateReference(): Promise<Record<number, TreeStateReference>>;
24
+ getLeafValue(
25
+ treeId: MerkleTreeId,
26
+ revision: WorldStateRevision,
27
+ leafIndex: bigint,
28
+ ): Promise<SerializedLeafValue | undefined>;
29
+ getLeafPreimage(
30
+ treeId: IndexedTreeId,
31
+ revision: WorldStateRevision,
32
+ leafIndex: bigint,
33
+ ): Promise<SerializedIndexedLeaf | undefined>;
34
+ getSiblingPath(treeId: MerkleTreeId, revision: WorldStateRevision, leafIndex: bigint): Promise<Buffer[]>;
35
+ getBlockNumbersForLeafIndices(
36
+ treeId: MerkleTreeId,
37
+ revision: WorldStateRevision,
38
+ leafIndices: bigint[],
39
+ ): Promise<(bigint | undefined)[]>;
40
+ findLeafIndices(
41
+ treeId: MerkleTreeId,
42
+ revision: WorldStateRevision,
43
+ leaves: SerializedLeafValue[],
44
+ startIndex: bigint,
45
+ ): Promise<(bigint | undefined)[]>;
46
+ findLowLeaf(
47
+ treeId: IndexedTreeId,
48
+ revision: WorldStateRevision,
49
+ key: Fr,
50
+ ): Promise<{ index: bigint; alreadyPresent: boolean }>;
51
+ findSiblingPaths(
52
+ treeId: MerkleTreeId,
53
+ revision: WorldStateRevision,
54
+ leaves: SerializedLeafValue[],
55
+ ): Promise<(SerializedSiblingPathAndIndex | undefined)[]>;
56
+ updateArchive(forkId: number, blockStateRef: BlockStateReference, blockHeaderHash: Buffer): Promise<void>;
57
+ appendLeaves(treeId: MerkleTreeId, forkId: number, leaves: SerializedLeafValue[]): Promise<void>;
58
+ batchInsert(
59
+ treeId: IndexedTreeId,
60
+ forkId: number,
61
+ leaves: SerializedLeafValue[],
62
+ subtreeDepth: number,
63
+ ): Promise<SerializedBatchInsertionResult>;
64
+ sequentialInsert(
65
+ treeId: IndexedTreeId,
66
+ forkId: number,
67
+ leaves: SerializedLeafValue[],
68
+ ): Promise<SerializedSequentialInsertionResult>;
69
+ syncBlock(input: {
70
+ blockNumber: BlockNumber;
71
+ blockStateRef: BlockStateReference;
72
+ blockHeaderHash: Buffer;
73
+ expectedArchiveRoot: Buffer;
74
+ expectedPreviousArchiveRoot: Buffer;
75
+ paddedNoteHashes: readonly SerializedLeafValue[];
76
+ paddedL1ToL2Messages: readonly SerializedLeafValue[];
77
+ paddedNullifiers: readonly SerializedLeafValue[];
78
+ publicDataWrites: readonly SerializedLeafValue[];
79
+ }): Promise<WorldStateStatusFull>;
80
+ createFork(input: { latest: boolean; blockNumber: BlockNumber }): Promise<number>;
81
+ deleteFork(forkId: number): Promise<void>;
82
+ finalizeBlocks(toBlockNumber: BlockNumber): Promise<WorldStateStatusSummary>;
83
+ unwindBlocks(toBlockNumber: BlockNumber): Promise<WorldStateStatusFull>;
84
+ removeHistoricalBlocks(toBlockNumber: BlockNumber): Promise<WorldStateStatusFull>;
85
+ getStatus(): Promise<WorldStateStatusSummary>;
86
+ createCheckpoint(forkId: number): Promise<number>;
87
+ commitCheckpoint(forkId: number): Promise<void>;
88
+ revertCheckpoint(forkId: number): Promise<void>;
89
+ commitAllCheckpoints(forkId: number, depth: number): Promise<void>;
90
+ revertAllCheckpoints(forkId: number, depth: number): Promise<void>;
91
+ copyStores(dstPath: string, compact: boolean): Promise<void>;
159
92
 
160
93
  /**
161
- * Sends a message to the native instance and returns the response.
162
- * @param messageType - The type of message to send
163
- * @param body - The message body
164
- * @param responseHandler - A callback accepting the response, executed on the job queue
165
- * @param errorHandler - A callback called on request error, executed on the job queue
166
- * @returns The response to the message
94
+ * IPC path the underlying aztec-wsdb process listens on. The C++ AVM uses this to attach to the
95
+ * same world state instance the TS layer is using.
167
96
  */
168
- public async call<T extends WorldStateMessageType>(
169
- messageType: T,
170
- body: WorldStateRequest[T] & WorldStateRequestCategories,
171
- // allows for the pre-processing of responses on the job queue before being passed back
172
- responseHandler = (response: WorldStateResponse[T]): WorldStateResponse[T] => response,
173
- errorHandler = (_: string) => {},
174
- ): Promise<WorldStateResponse[T]> {
175
- // Here we determine which fork the request is being executed against and whether it requires uncommitted data
176
- // We use the fork Id to select the appropriate request queue and the uncommitted data flag to pass to the queue
177
- let forkId = -1;
178
- // We assume it includes uncommitted unless explicitly told otherwise
179
- let committedOnly = false;
180
-
181
- // Canonical requests ALWAYS go against the canonical fork
182
- // These include things like block syncs/unwinds etc
183
- // These requests don't contain a fork ID
184
- if (isWithCanonical(body)) {
185
- forkId = 0;
186
- } else if (isWithForkId(body)) {
187
- forkId = body.forkId;
188
- } else if (isWithRevision(body)) {
189
- forkId = body.revision.forkId;
190
- committedOnly = body.revision.includeUncommitted === false;
191
- } else {
192
- const _: never = body;
193
- throw new Error(`Unable to determine forkId for message=${WorldStateMessageType[messageType]}`);
194
- }
195
-
196
- // Get the queue or create a new one
197
- let requestQueue = this.queues.get(forkId);
198
- if (requestQueue === undefined) {
199
- requestQueue = new WorldStateOpsQueue();
200
- this.queues.set(forkId, requestQueue);
201
- }
202
-
203
- // Enqueue the request and wait for the response. The per-fork queue is cleaned up in `finally` even on
204
- // error, so the JS-side queues map cannot outlive the native fork (e.g. when the native fork was already
205
- // destroyed by an unwind/historical-prune and DELETE_FORK rejects with "Fork not found").
206
- let shouldDeleteForkQueue = false;
207
- try {
208
- const response = await requestQueue.execute(
209
- async () => {
210
- assert.notEqual(messageType, WorldStateMessageType.CLOSE, 'Use close() to close the native instance');
211
- assert.equal(this.open, true, 'Native instance is closed');
212
- let response: WorldStateResponse[T];
213
- try {
214
- response = await this._sendMessage(messageType, body);
215
- } catch (error: any) {
216
- errorHandler(error.message);
217
- throw error;
218
- }
219
- return responseHandler(response);
220
- },
221
- messageType,
222
- committedOnly,
223
- );
224
- return response;
225
- } catch (err: any) {
226
- shouldDeleteForkQueue = forkId !== 0 && err?.message === 'Fork not found';
227
- throw err;
228
- } finally {
229
- if (messageType === WorldStateMessageType.DELETE_FORK || shouldDeleteForkQueue) {
230
- await requestQueue.stop();
231
- this.queues.delete(forkId);
232
- }
233
- }
234
- }
97
+ getIpcPath(): string;
235
98
 
236
99
  /**
237
- * Stops the native instance.
100
+ * Shut down the world state instance. Cancels any in-flight queues, closes the IPC channel, and
101
+ * terminates the underlying aztec-wsdb process. Idempotent.
238
102
  */
239
- public async close(): Promise<void> {
240
- if (!this.open) {
241
- return;
242
- }
243
- this.open = false;
244
- const queue = this.queues.get(0)!;
245
-
246
- await queue.execute(
247
- async () => {
248
- await this._sendMessage(WorldStateMessageType.CLOSE, { canonical: true });
249
- },
250
- WorldStateMessageType.CLOSE,
251
- false,
252
- );
253
- await queue.stop();
254
- }
255
-
256
- private async _sendMessage<T extends WorldStateMessageType>(
257
- messageType: T,
258
- body: WorldStateRequest[T] & WorldStateRequestCategories,
259
- ): Promise<WorldStateResponse[T]> {
260
- let logMetadata: Record<string, any> = {};
261
-
262
- if (body) {
263
- if ('treeId' in body) {
264
- logMetadata['treeId'] = MerkleTreeId[body.treeId];
265
- }
266
-
267
- if ('revision' in body) {
268
- logMetadata = { ...logMetadata, ...body.revision };
269
- }
270
-
271
- if ('forkId' in body) {
272
- logMetadata['forkId'] = body.forkId;
273
- }
274
-
275
- if ('blockNumber' in body) {
276
- logMetadata['blockNumber'] = body.blockNumber;
277
- }
278
-
279
- if ('toBlockNumber' in body) {
280
- logMetadata['toBlockNumber'] = body.toBlockNumber;
281
- }
282
-
283
- if ('leafIndex' in body) {
284
- logMetadata['leafIndex'] = body.leafIndex;
285
- }
286
-
287
- if ('blockHeaderHash' in body) {
288
- logMetadata['blockHeaderHash'] = '0x' + body.blockHeaderHash.toString('hex');
289
- }
290
-
291
- if ('leaves' in body) {
292
- logMetadata['leavesCount'] = body.leaves.length;
293
- }
294
-
295
- // sync operation
296
- if ('paddedNoteHashes' in body) {
297
- logMetadata['notesCount'] = body.paddedNoteHashes.length;
298
- logMetadata['nullifiersCount'] = body.paddedNullifiers.length;
299
- logMetadata['l1ToL2MessagesCount'] = body.paddedL1ToL2Messages.length;
300
- logMetadata['publicDataWritesCount'] = body.publicDataWrites.length;
301
- }
302
- }
303
-
304
- try {
305
- const { duration, response } = await this.instance.sendMessage(messageType, body);
306
- this.log.trace(`Call ${WorldStateMessageType[messageType]} took (ms)`, {
307
- duration,
308
- ...logMetadata,
309
- });
310
-
311
- this.instrumentation.recordRoundTrip(duration.totalUs, messageType);
312
- return response;
313
- } catch (error) {
314
- this.log.error(`Call ${WorldStateMessageType[messageType]} failed: ${error}`, error, logMetadata);
315
- throw error;
316
- }
317
- }
103
+ close(): Promise<void>;
318
104
  }
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Names of the world-state operations, used as labels for instrumentation and
3
+ * logging. (Read/write ordering is enforced server-side by the wsdb scheduler,
4
+ * so there is no longer a client-side operation queue.)
5
+ */
6
+ export type WorldStateOperationName =
7
+ | 'getTreeInfo'
8
+ | 'getStateReference'
9
+ | 'getInitialStateReference'
10
+ | 'getLeafValue'
11
+ | 'getLeafPreimage'
12
+ | 'getSiblingPath'
13
+ | 'getBlockNumbersForLeafIndices'
14
+ | 'findLeafIndices'
15
+ | 'findLowLeaf'
16
+ | 'findSiblingPaths'
17
+ | 'appendLeaves'
18
+ | 'batchInsert'
19
+ | 'sequentialInsert'
20
+ | 'updateArchive'
21
+ | 'syncBlock'
22
+ | 'createFork'
23
+ | 'deleteFork'
24
+ | 'finalizeBlocks'
25
+ | 'unwindBlocks'
26
+ | 'removeHistoricalBlocks'
27
+ | 'getStatus'
28
+ | 'createCheckpoint'
29
+ | 'commitCheckpoint'
30
+ | 'revertCheckpoint'
31
+ | 'commitAllCheckpoints'
32
+ | 'revertAllCheckpoints'
33
+ | 'copyStores';
@@ -73,7 +73,6 @@ export async function createWorldState(
73
73
  bindings,
74
74
  )
75
75
  : await NativeWorldStateService.tmp(
76
- config.rollupAddress,
77
76
  !['true', '1'].includes(process.env.DEBUG_WORLD_STATE!),
78
77
  genesis,
79
78
  instrumentation,
@@ -1,19 +0,0 @@
1
- import { WorldStateMessageType } from './message.js';
2
- export declare const MUTATING_MSG_TYPES: Set<WorldStateMessageType>;
3
- export declare class WorldStateOpsQueue {
4
- private requests;
5
- private inFlightMutatingCount;
6
- private inFlightCount;
7
- private stopPromise?;
8
- private stopResolve?;
9
- private requestId;
10
- private ops;
11
- execute(request: () => Promise<any>, messageType: WorldStateMessageType, committedOnly: boolean): Promise<any>;
12
- private executeMutating;
13
- private executeNonMutatingUncommitted;
14
- private executeNonMutatingCommitted;
15
- private checkAndEnqueue;
16
- private sendEnqueuedRequest;
17
- stop(): Promise<void>;
18
- }
19
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoid29ybGRfc3RhdGVfb3BzX3F1ZXVlLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvbmF0aXZlL3dvcmxkX3N0YXRlX29wc19xdWV1ZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFFQSxPQUFPLEVBQUUscUJBQXFCLEVBQUUsTUFBTSxjQUFjLENBQUM7QUF5QnJELGVBQU8sTUFBTSxrQkFBa0IsNEJBZ0I3QixDQUFDO0FBR0gscUJBQWEsa0JBQWtCO0lBQzdCLE9BQU8sQ0FBQyxRQUFRLENBQXNCO0lBQ3RDLE9BQU8sQ0FBQyxxQkFBcUIsQ0FBSztJQUNsQyxPQUFPLENBQUMsYUFBYSxDQUFLO0lBQzFCLE9BQU8sQ0FBQyxXQUFXLENBQUMsQ0FBZ0I7SUFDcEMsT0FBTyxDQUFDLFdBQVcsQ0FBQyxDQUFhO0lBQ2pDLE9BQU8sQ0FBQyxTQUFTLENBQUs7SUFDdEIsT0FBTyxDQUFDLEdBQUcsQ0FBd0M7SUFJNUMsT0FBTyxDQUFDLE9BQU8sRUFBRSxNQUFNLE9BQU8sQ0FBQyxHQUFHLENBQUMsRUFBRSxXQUFXLEVBQUUscUJBQXFCLEVBQUUsYUFBYSxFQUFFLE9BQU8sZ0JBc0JyRztJQUdELE9BQU8sQ0FBQyxlQUFlO0lBV3ZCLE9BQU8sQ0FBQyw2QkFBNkI7SUFZckMsT0FBTyxDQUFDLDJCQUEyQjtJQVVuQyxPQUFPLENBQUMsZUFBZTtJQXlDdkIsT0FBTyxDQUFDLG1CQUFtQjtJQWdCcEIsSUFBSSxrQkFnQlY7Q0FDRiJ9
@@ -1 +0,0 @@
1
- {"version":3,"file":"world_state_ops_queue.d.ts","sourceRoot":"","sources":["../../src/native/world_state_ops_queue.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AAyBrD,eAAO,MAAM,kBAAkB,4BAgB7B,CAAC;AAGH,qBAAa,kBAAkB;IAC7B,OAAO,CAAC,QAAQ,CAAsB;IACtC,OAAO,CAAC,qBAAqB,CAAK;IAClC,OAAO,CAAC,aAAa,CAAK;IAC1B,OAAO,CAAC,WAAW,CAAC,CAAgB;IACpC,OAAO,CAAC,WAAW,CAAC,CAAa;IACjC,OAAO,CAAC,SAAS,CAAK;IACtB,OAAO,CAAC,GAAG,CAAwC;IAI5C,OAAO,CAAC,OAAO,EAAE,MAAM,OAAO,CAAC,GAAG,CAAC,EAAE,WAAW,EAAE,qBAAqB,EAAE,aAAa,EAAE,OAAO,gBAsBrG;IAGD,OAAO,CAAC,eAAe;IAWvB,OAAO,CAAC,6BAA6B;IAYrC,OAAO,CAAC,2BAA2B;IAUnC,OAAO,CAAC,eAAe;IAyCvB,OAAO,CAAC,mBAAmB;IAgBpB,IAAI,kBAgBV;CACF"}
@@ -1,146 +0,0 @@
1
- import { promiseWithResolvers } from '@aztec/foundation/promise';
2
- import { WorldStateMessageType } from './message.js';
3
- // These are the set of message types that implement mutating operations
4
- // Messages of these types require exclusive access to their given forks
5
- export const MUTATING_MSG_TYPES = new Set([
6
- WorldStateMessageType.APPEND_LEAVES,
7
- WorldStateMessageType.BATCH_INSERT,
8
- WorldStateMessageType.SEQUENTIAL_INSERT,
9
- WorldStateMessageType.UPDATE_ARCHIVE,
10
- WorldStateMessageType.COMMIT,
11
- WorldStateMessageType.ROLLBACK,
12
- WorldStateMessageType.SYNC_BLOCK,
13
- WorldStateMessageType.CREATE_FORK,
14
- WorldStateMessageType.DELETE_FORK,
15
- WorldStateMessageType.FINALIZE_BLOCKS,
16
- WorldStateMessageType.UNWIND_BLOCKS,
17
- WorldStateMessageType.REMOVE_HISTORICAL_BLOCKS,
18
- WorldStateMessageType.CREATE_CHECKPOINT,
19
- WorldStateMessageType.COMMIT_CHECKPOINT,
20
- WorldStateMessageType.REVERT_CHECKPOINT
21
- ]);
22
- // This class implements the per-fork operation queue
23
- export class WorldStateOpsQueue {
24
- requests = [];
25
- inFlightMutatingCount = 0;
26
- inFlightCount = 0;
27
- stopPromise;
28
- stopResolve;
29
- requestId = 0;
30
- ops = new Map();
31
- // The primary public api, this is where an operation is queued
32
- // We return a promise that will ultimately be resolved/rejected with the response/error generated by the 'request' argument
33
- execute(request, messageType, committedOnly) {
34
- if (this.stopResolve !== undefined) {
35
- throw new Error('Unable to send request to world state, queue already stopped');
36
- }
37
- const op = {
38
- requestId: this.requestId++,
39
- mutating: MUTATING_MSG_TYPES.has(messageType),
40
- request,
41
- promise: promiseWithResolvers()
42
- };
43
- this.ops.set(op.requestId, op);
44
- // Perform the appropriate action based upon the queueing rules
45
- if (op.mutating) {
46
- this.executeMutating(op);
47
- } else if (committedOnly === false) {
48
- this.executeNonMutatingUncommitted(op);
49
- } else {
50
- this.executeNonMutatingCommitted(op);
51
- }
52
- return op.promise.promise;
53
- }
54
- // Mutating requests need exclusive access
55
- executeMutating(op) {
56
- // If nothing is in flight then we send the request immediately
57
- // Otherwise add to the queue
58
- if (this.inFlightCount === 0) {
59
- this.sendEnqueuedRequest(op);
60
- } else {
61
- this.requests.push(op);
62
- }
63
- }
64
- // Non mutating requests including uncommitted state
65
- executeNonMutatingUncommitted(op) {
66
- // If there are no mutating requests in flight and there is nothing queued
67
- // then send the request immediately
68
- // If a mutating request is in flight then we must wait
69
- // If a mutating request is not in flight but something is queued then it must be a mutating request
70
- if (this.inFlightMutatingCount === 0 && this.requests.length === 0) {
71
- this.sendEnqueuedRequest(op);
72
- } else {
73
- this.requests.push(op);
74
- }
75
- }
76
- executeNonMutatingCommitted(op) {
77
- // This is a non-mutating request for committed data
78
- // It can always be sent
79
- op.request().then(op.promise.resolve, op.promise.reject).finally(()=>{
80
- this.ops.delete(op.requestId);
81
- });
82
- }
83
- checkAndEnqueue(completedOp) {
84
- // As request has completed
85
- // First we decrements the relevant in flight counters
86
- if (completedOp.mutating) {
87
- --this.inFlightMutatingCount;
88
- }
89
- --this.inFlightCount;
90
- // If there are still requests in flight then do nothing further
91
- if (this.inFlightCount !== 0) {
92
- return;
93
- }
94
- // No requests in flight, send next queued requests
95
- // We loop and send:
96
- // 1 mutating request if it is next in the queue
97
- // As many non-mutating requests as we encounter until
98
- // we exhaust the queue or we reach a mutating request
99
- while(this.requests.length > 0){
100
- const next = this.requests[0];
101
- if (next.mutating) {
102
- if (this.inFlightCount === 0) {
103
- // send the mutating request
104
- this.requests.shift();
105
- this.sendEnqueuedRequest(next);
106
- }
107
- break;
108
- } else {
109
- // not mutating, send and go round again
110
- this.requests.shift();
111
- this.sendEnqueuedRequest(next);
112
- }
113
- }
114
- // If the queue is empty, there is nothing in flight and we have been told to stop, then resolve the stop promise
115
- if (this.inFlightCount === 0 && this.stopResolve !== undefined) {
116
- this.stopResolve();
117
- }
118
- }
119
- sendEnqueuedRequest(op) {
120
- // Here we increment the in flight counts before sending
121
- ++this.inFlightCount;
122
- if (op.mutating) {
123
- ++this.inFlightMutatingCount;
124
- }
125
- // Make the request and pass the response/error through to the stored promise
126
- op.request().then(op.promise.resolve, op.promise.reject).finally(()=>{
127
- this.checkAndEnqueue(op);
128
- this.ops.delete(op.requestId);
129
- });
130
- }
131
- stop() {
132
- // If there is already a stop promise then return it
133
- if (this.stopPromise) {
134
- return this.stopPromise;
135
- }
136
- // Otherwise create a new one and capture the resolve method
137
- this.stopPromise = new Promise((resolve)=>{
138
- this.stopResolve = resolve;
139
- });
140
- // If no outstanding requests then immediately resolve the promise
141
- if (this.requests.length === 0 && this.inFlightCount === 0 && this.stopResolve !== undefined) {
142
- this.stopResolve();
143
- }
144
- return this.stopPromise;
145
- }
146
- }