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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/dest/instrumentation/instrumentation.d.ts +3 -4
  2. package/dest/instrumentation/instrumentation.d.ts.map +1 -1
  3. package/dest/instrumentation/instrumentation.js +9 -4
  4. package/dest/native/ipc_world_state_instance.d.ts +22 -59
  5. package/dest/native/ipc_world_state_instance.d.ts.map +1 -1
  6. package/dest/native/ipc_world_state_instance.js +495 -520
  7. package/dest/native/merkle_trees_facade.d.ts +3 -4
  8. package/dest/native/merkle_trees_facade.d.ts.map +1 -1
  9. package/dest/native/merkle_trees_facade.js +109 -37
  10. package/dest/native/message.d.ts +233 -14
  11. package/dest/native/message.d.ts.map +1 -1
  12. package/dest/native/message.js +42 -0
  13. package/dest/native/native_world_state.d.ts +28 -14
  14. package/dest/native/native_world_state.d.ts.map +1 -1
  15. package/dest/native/native_world_state.js +102 -101
  16. package/dest/native/native_world_state_instance.d.ts +42 -58
  17. package/dest/native/native_world_state_instance.d.ts.map +1 -1
  18. package/dest/native/native_world_state_instance.js +222 -5
  19. package/dest/native/world_state_ops_queue.d.ts +19 -0
  20. package/dest/native/world_state_ops_queue.d.ts.map +1 -0
  21. package/dest/native/world_state_ops_queue.js +146 -0
  22. package/dest/synchronizer/errors.d.ts +8 -1
  23. package/dest/synchronizer/errors.d.ts.map +1 -1
  24. package/dest/synchronizer/errors.js +8 -1
  25. package/dest/synchronizer/factory.d.ts +1 -1
  26. package/dest/synchronizer/factory.d.ts.map +1 -1
  27. package/dest/synchronizer/factory.js +1 -1
  28. package/dest/synchronizer/index.d.ts +2 -1
  29. package/dest/synchronizer/index.d.ts.map +1 -1
  30. package/dest/synchronizer/index.js +1 -0
  31. package/dest/synchronizer/server_world_state_synchronizer.d.ts +11 -6
  32. package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -1
  33. package/dest/synchronizer/server_world_state_synchronizer.js +55 -43
  34. package/dest/test/utils.d.ts +1 -1
  35. package/dest/test/utils.d.ts.map +1 -1
  36. package/dest/test/utils.js +6 -1
  37. package/dest/testing.d.ts +2 -2
  38. package/dest/testing.d.ts.map +1 -1
  39. package/dest/testing.js +15 -4
  40. package/package.json +10 -12
  41. package/src/instrumentation/instrumentation.ts +18 -6
  42. package/src/native/ipc_world_state_instance.ts +503 -608
  43. package/src/native/merkle_trees_facade.ts +104 -59
  44. package/src/native/message.ts +300 -13
  45. package/src/native/native_world_state.ts +156 -121
  46. package/src/native/native_world_state_instance.ts +307 -96
  47. package/src/native/world_state_ops_queue.ts +190 -0
  48. package/src/synchronizer/errors.ts +8 -0
  49. package/src/synchronizer/factory.ts +1 -0
  50. package/src/synchronizer/index.ts +1 -0
  51. package/src/synchronizer/server_world_state_synchronizer.ts +63 -30
  52. package/src/test/utils.ts +10 -1
  53. package/src/testing.ts +14 -4
  54. package/dest/native/world_state_operation.d.ts +0 -7
  55. package/dest/native/world_state_operation.d.ts.map +0 -1
  56. package/dest/native/world_state_operation.js +0 -5
  57. package/src/native/world_state_operation.ts +0 -33
@@ -1,107 +1,318 @@
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,
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,
17
29
  } 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
+
34
+ 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
+ }
18
42
 
19
43
  /**
20
- * Backend-agnostic handle to a running aztec-wsdb world state, accessed by the TS layer.
21
- *
22
- * The legacy in-process NAPI implementation has been removed; the C++ AVM (NAPI) now connects to
23
- * the same aztec-wsdb process using the IPC path returned by {@link getIpcPath}.
44
+ * Strongly-typed interface to access the WorldState class in the native world_state_napi module.
24
45
  */
25
- export interface NativeWorldStateInstance {
26
- getTreeInfo(treeId: MerkleTreeId, revision: WorldStateRevision): Promise<TreeInfo>;
27
- getStateReference(revision: WorldStateRevision): Promise<Record<number, TreeStateReference>>;
28
- getInitialStateReference(): Promise<Record<number, TreeStateReference>>;
29
- getLeafValue(
30
- treeId: MerkleTreeId,
31
- revision: WorldStateRevision,
32
- leafIndex: bigint,
33
- ): Promise<SerializedLeafValue | undefined>;
34
- getLeafPreimage(
35
- treeId: IndexedTreeId,
36
- revision: WorldStateRevision,
37
- leafIndex: bigint,
38
- ): Promise<SerializedIndexedLeaf | undefined>;
39
- getSiblingPath(treeId: MerkleTreeId, revision: WorldStateRevision, leafIndex: bigint): Promise<Buffer[]>;
40
- getBlockNumbersForLeafIndices(
41
- treeId: MerkleTreeId,
42
- revision: WorldStateRevision,
43
- leafIndices: bigint[],
44
- ): Promise<(bigint | undefined)[]>;
45
- findLeafIndices(
46
- treeId: MerkleTreeId,
47
- revision: WorldStateRevision,
48
- leaves: SerializedLeafValue[],
49
- startIndex: bigint,
50
- ): Promise<(bigint | undefined)[]>;
51
- findLowLeaf(
52
- treeId: IndexedTreeId,
53
- revision: WorldStateRevision,
54
- key: Fr,
55
- ): Promise<{ index: bigint; alreadyPresent: boolean }>;
56
- findSiblingPaths(
57
- treeId: MerkleTreeId,
58
- revision: WorldStateRevision,
59
- leaves: SerializedLeafValue[],
60
- ): Promise<(SerializedSiblingPathAndIndex | undefined)[]>;
61
- updateArchive(forkId: number, blockStateRef: BlockStateReference, blockHeaderHash: Buffer): Promise<void>;
62
- appendLeaves(treeId: MerkleTreeId, forkId: number, leaves: SerializedLeafValue[]): Promise<void>;
63
- batchInsert(
64
- treeId: IndexedTreeId,
65
- forkId: number,
66
- leaves: SerializedLeafValue[],
67
- subtreeDepth: number,
68
- ): Promise<SerializedBatchInsertionResult>;
69
- sequentialInsert(
70
- treeId: IndexedTreeId,
71
- forkId: number,
72
- leaves: SerializedLeafValue[],
73
- ): Promise<SerializedSequentialInsertionResult>;
74
- syncBlock(input: {
75
- blockNumber: BlockNumber;
76
- blockStateRef: BlockStateReference;
77
- blockHeaderHash: Buffer;
78
- paddedNoteHashes: readonly SerializedLeafValue[];
79
- paddedL1ToL2Messages: readonly SerializedLeafValue[];
80
- paddedNullifiers: readonly SerializedLeafValue[];
81
- publicDataWrites: readonly SerializedLeafValue[];
82
- }): Promise<WorldStateStatusFull>;
83
- createFork(input: { latest: boolean; blockNumber: BlockNumber }): Promise<number>;
84
- deleteFork(forkId: number): Promise<void>;
85
- finalizeBlocks(toBlockNumber: BlockNumber): Promise<WorldStateStatusSummary>;
86
- unwindBlocks(toBlockNumber: BlockNumber): Promise<WorldStateStatusFull>;
87
- removeHistoricalBlocks(toBlockNumber: BlockNumber): Promise<WorldStateStatusFull>;
88
- getStatus(): Promise<WorldStateStatusSummary>;
89
- createCheckpoint(forkId: number): Promise<number>;
90
- commitCheckpoint(forkId: number): Promise<void>;
91
- revertCheckpoint(forkId: number): Promise<void>;
92
- commitAllCheckpoints(forkId: number, depth: number): Promise<void>;
93
- revertAllCheckpoints(forkId: number, depth: number): Promise<void>;
94
- copyStores(dstPath: string, compact: boolean): Promise<void>;
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
+ }
95
159
 
96
160
  /**
97
- * IPC path the underlying aztec-wsdb process listens on. The C++ AVM uses this to attach to the
98
- * same world state instance the TS layer is using.
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
99
167
  */
100
- getIpcPath(): string;
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
+ }
101
235
 
102
236
  /**
103
- * Shut down the world state instance. Cancels any in-flight queues, closes the IPC channel, and
104
- * terminates the underlying aztec-wsdb process. Idempotent.
237
+ * Stops the native instance.
105
238
  */
106
- close(): Promise<void>;
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
+ }
107
318
  }
@@ -0,0 +1,190 @@
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,5 +1,13 @@
1
+ /**
2
+ * Thrown by {@link ServerWorldStateSynchronizer.syncImmediate} when world state cannot be synced to the requested
3
+ * target: either the block is not available from the block source (`block_not_available`, e.g. it was pruned away)
4
+ * or the synced block does not match the requested hash (`block_hash_mismatch`, i.e. a reorg). Both causes are
5
+ * transient from the caller's perspective: re-resolving the query against the current chain and retrying may succeed
6
+ * or produce a more precise error.
7
+ */
1
8
  export class WorldStateSynchronizerError extends Error {
2
9
  constructor(message: string, options?: ErrorOptions) {
3
10
  super(message, options);
11
+ this.name = 'WorldStateSynchronizerError';
4
12
  }
5
13
  }
@@ -73,6 +73,7 @@ export async function createWorldState(
73
73
  bindings,
74
74
  )
75
75
  : await NativeWorldStateService.tmp(
76
+ config.rollupAddress,
76
77
  !['true', '1'].includes(process.env.DEBUG_WORLD_STATE!),
77
78
  genesis,
78
79
  instrumentation,
@@ -1,2 +1,3 @@
1
1
  export * from './server_world_state_synchronizer.js';
2
2
  export * from './factory.js';
3
+ export * from './errors.js';