@aztec/world-state 0.69.1 → 0.70.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.
@@ -11,7 +11,6 @@ import {
11
11
  PUBLIC_DATA_TREE_HEIGHT,
12
12
  } from '@aztec/circuits.js';
13
13
  import { createLogger } from '@aztec/foundation/log';
14
- import { SerialQueue } from '@aztec/foundation/queue';
15
14
 
16
15
  import assert from 'assert';
17
16
  import bindings from 'bindings';
@@ -25,8 +24,13 @@ import {
25
24
  TypedMessage,
26
25
  WorldStateMessageType,
27
26
  type WorldStateRequest,
27
+ type WorldStateRequestCategories,
28
28
  type WorldStateResponse,
29
+ isWithCanonical,
30
+ isWithForkId,
31
+ isWithRevision,
29
32
  } from './message.js';
33
+ import { WorldStateOpsQueue } from './world_state_ops_queue.js';
30
34
 
31
35
  // small extension to pack an NodeJS Fr instance to a representation that the C++ code can understand
32
36
  // this only works for writes. Unpacking from C++ can't create Fr instances because the data is passed
@@ -49,7 +53,10 @@ const NATIVE_MODULE = bindings(NATIVE_LIBRARY_NAME);
49
53
  const MAX_WORLD_STATE_THREADS = +(process.env.HARDWARE_CONCURRENCY || '16');
50
54
 
51
55
  export interface NativeWorldStateInstance {
52
- call<T extends WorldStateMessageType>(messageType: T, body: WorldStateRequest[T]): Promise<WorldStateResponse[T]>;
56
+ call<T extends WorldStateMessageType>(
57
+ messageType: T,
58
+ body: WorldStateRequest[T] & WorldStateRequestCategories,
59
+ ): Promise<WorldStateResponse[T]>;
53
60
  }
54
61
 
55
62
  /**
@@ -78,8 +85,8 @@ export class NativeWorldState implements NativeWorldStateInstance {
78
85
  /** The actual native instance */
79
86
  private instance: any;
80
87
 
81
- /** Calls to the same instance are serialized */
82
- private queue = new SerialQueue();
88
+ // We maintain a map of queue to fork
89
+ private queues = new Map<number, WorldStateOpsQueue>();
83
90
 
84
91
  /** Creates a new native WorldState instance */
85
92
  constructor(
@@ -109,7 +116,8 @@ export class NativeWorldState implements NativeWorldStateInstance {
109
116
  dbMapSizeKb,
110
117
  threads,
111
118
  );
112
- this.queue.start();
119
+ // Manually create the queue for the canonical fork
120
+ this.queues.set(0, new WorldStateOpsQueue());
113
121
  }
114
122
 
115
123
  /**
@@ -120,25 +128,65 @@ export class NativeWorldState implements NativeWorldStateInstance {
120
128
  * @param errorHandler - A callback called on request error, executed on the job queue
121
129
  * @returns The response to the message
122
130
  */
123
- public call<T extends WorldStateMessageType>(
131
+ public async call<T extends WorldStateMessageType>(
124
132
  messageType: T,
125
- body: WorldStateRequest[T],
133
+ body: WorldStateRequest[T] & WorldStateRequestCategories,
126
134
  // allows for the pre-processing of responses on the job queue before being passed back
127
135
  responseHandler = (response: WorldStateResponse[T]): WorldStateResponse[T] => response,
128
136
  errorHandler = (_: string) => {},
129
137
  ): Promise<WorldStateResponse[T]> {
130
- return this.queue.put(async () => {
131
- assert.notEqual(messageType, WorldStateMessageType.CLOSE, 'Use close() to close the native instance');
132
- assert.equal(this.open, true, 'Native instance is closed');
133
- let response: WorldStateResponse[T];
134
- try {
135
- response = await this._sendMessage(messageType, body);
136
- } catch (error: any) {
137
- errorHandler(error.message);
138
- throw error;
139
- }
140
- return responseHandler(response);
141
- });
138
+ // Here we determine which fork the request is being executed against and whether it requires uncommitted data
139
+ // We use the fork Id to select the appropriate request queue and the uncommitted data flag to pass to the queue
140
+ let forkId = -1;
141
+ // We assume it includes uncommitted unless explicitly told otherwise
142
+ let committedOnly = false;
143
+
144
+ // Canonical requests ALWAYS go against the canonical fork
145
+ // These include things like block syncs/unwinds etc
146
+ // These requests don't contain a fork ID
147
+ if (isWithCanonical(body)) {
148
+ forkId = 0;
149
+ } else if (isWithForkId(body)) {
150
+ forkId = body.forkId;
151
+ } else if (isWithRevision(body)) {
152
+ forkId = body.revision.forkId;
153
+ committedOnly = body.revision.includeUncommitted === false;
154
+ } else {
155
+ const _: never = body;
156
+ throw new Error(`Unable to determine forkId for message=${WorldStateMessageType[messageType]}`);
157
+ }
158
+
159
+ // Get the queue or create a new one
160
+ let requestQueue = this.queues.get(forkId);
161
+ if (requestQueue === undefined) {
162
+ requestQueue = new WorldStateOpsQueue();
163
+ this.queues.set(forkId, requestQueue);
164
+ }
165
+
166
+ // Enqueue the request and wait for the response
167
+ const response = await requestQueue.execute(
168
+ async () => {
169
+ assert.notEqual(messageType, WorldStateMessageType.CLOSE, 'Use close() to close the native instance');
170
+ assert.equal(this.open, true, 'Native instance is closed');
171
+ let response: WorldStateResponse[T];
172
+ try {
173
+ response = await this._sendMessage(messageType, body);
174
+ } catch (error: any) {
175
+ errorHandler(error.message);
176
+ throw error;
177
+ }
178
+ return responseHandler(response);
179
+ },
180
+ messageType,
181
+ committedOnly,
182
+ );
183
+
184
+ // If the request was to delete the fork then we clean it up here
185
+ if (messageType === WorldStateMessageType.DELETE_FORK) {
186
+ await requestQueue.stop();
187
+ this.queues.delete(forkId);
188
+ }
189
+ return response;
142
190
  }
143
191
 
144
192
  /**
@@ -149,13 +197,21 @@ export class NativeWorldState implements NativeWorldStateInstance {
149
197
  return;
150
198
  }
151
199
  this.open = false;
152
- await this._sendMessage(WorldStateMessageType.CLOSE, undefined);
153
- await this.queue.end();
200
+ const queue = this.queues.get(0)!;
201
+
202
+ await queue.execute(
203
+ async () => {
204
+ await this._sendMessage(WorldStateMessageType.CLOSE, { canonical: true });
205
+ },
206
+ WorldStateMessageType.CLOSE,
207
+ false,
208
+ );
209
+ await queue.stop();
154
210
  }
155
211
 
156
212
  private async _sendMessage<T extends WorldStateMessageType>(
157
213
  messageType: T,
158
- body: WorldStateRequest[T],
214
+ body: WorldStateRequest[T] & WorldStateRequestCategories,
159
215
  ): Promise<WorldStateResponse[T]> {
160
216
  const messageId = this.nextMessageId++;
161
217
  if (body) {
@@ -0,0 +1,187 @@
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.FINALISE_BLOCKS,
39
+ WorldStateMessageType.UNWIND_BLOCKS,
40
+ WorldStateMessageType.REMOVE_HISTORICAL_BLOCKS,
41
+ ]);
42
+
43
+ // This class implements the per-fork operation queue
44
+ export class WorldStateOpsQueue {
45
+ private requests: WorldStateOp[] = [];
46
+ private inFlightMutatingCount = 0;
47
+ private inFlightCount = 0;
48
+ private stopPromise?: Promise<void>;
49
+ private stopResolve?: () => void;
50
+ private requestId = 0;
51
+ private ops: Map<number, WorldStateOp> = new Map();
52
+
53
+ // The primary public api, this is where an operation is queued
54
+ // We return a promise that will ultimately be resolved/rejected with the response/error generated by the 'request' argument
55
+ public execute(request: () => Promise<any>, messageType: WorldStateMessageType, committedOnly: boolean) {
56
+ if (this.stopResolve !== undefined) {
57
+ throw new Error('Unable to send request to world state, queue already stopped');
58
+ }
59
+
60
+ const op: WorldStateOp = {
61
+ requestId: this.requestId++,
62
+ mutating: MUTATING_MSG_TYPES.has(messageType),
63
+ request,
64
+ promise: promiseWithResolvers(),
65
+ };
66
+ this.ops.set(op.requestId, op);
67
+
68
+ // Perform the appropriate action based upon the queueing rules
69
+ if (op.mutating) {
70
+ this.executeMutating(op);
71
+ } else if (committedOnly === false) {
72
+ this.executeNonMutatingUncommitted(op);
73
+ } else {
74
+ this.executeNonMutatingCommitted(op);
75
+ }
76
+ return op.promise.promise;
77
+ }
78
+
79
+ // Mutating requests need exclusive access
80
+ private executeMutating(op: WorldStateOp) {
81
+ // If nothing is in flight then we send the request immediately
82
+ // Otherwise add to the queue
83
+ if (this.inFlightCount === 0) {
84
+ this.sendEnqueuedRequest(op);
85
+ } else {
86
+ this.requests.push(op);
87
+ }
88
+ }
89
+
90
+ // Non mutating requests including uncommitted state
91
+ private executeNonMutatingUncommitted(op: WorldStateOp) {
92
+ // If there are no mutating requests in flight and there is nothing queued
93
+ // then send the request immediately
94
+ // If a mutating request is in flight then we must wait
95
+ // If a mutating request is not in flight but something is queued then it must be a mutating request
96
+ if (this.inFlightMutatingCount == 0 && this.requests.length == 0) {
97
+ this.sendEnqueuedRequest(op);
98
+ } else {
99
+ this.requests.push(op);
100
+ }
101
+ }
102
+
103
+ private executeNonMutatingCommitted(op: WorldStateOp) {
104
+ // This is a non-mutating request for committed data
105
+ // It can always be sent
106
+ op.request()
107
+ .then(op.promise.resolve, op.promise.reject)
108
+ .finally(() => {
109
+ this.ops.delete(op.requestId);
110
+ });
111
+ }
112
+
113
+ private checkAndEnqueue(completedOp: WorldStateOp) {
114
+ // As request has completed
115
+ // First we decrements the relevant in flight counters
116
+ if (completedOp.mutating) {
117
+ --this.inFlightMutatingCount;
118
+ }
119
+ --this.inFlightCount;
120
+
121
+ // If there are still requests in flight then do nothing further
122
+ if (this.inFlightCount != 0) {
123
+ return;
124
+ }
125
+
126
+ // No requests in flight, send next queued requests
127
+ // We loop and send:
128
+ // 1 mutating request if it is next in the queue
129
+ // As many non-mutating requests as we encounter until
130
+ // we exhaust the queue or we reach a mutating request
131
+ while (this.requests.length > 0) {
132
+ const next = this.requests[0];
133
+ if (next.mutating) {
134
+ if (this.inFlightCount == 0) {
135
+ // send the mutating request
136
+ this.requests.shift();
137
+ this.sendEnqueuedRequest(next);
138
+ }
139
+ // this request is mutating, we need to stop here
140
+ break;
141
+ } else {
142
+ // not mutating, send and go round again
143
+ this.requests.shift();
144
+ this.sendEnqueuedRequest(next);
145
+ }
146
+ }
147
+
148
+ // If the queue is empty, there is nothing in flight and we have been told to stop, then resolve the stop promise
149
+ if (this.inFlightCount == 0 && this.stopResolve !== undefined) {
150
+ this.stopResolve();
151
+ }
152
+ }
153
+
154
+ private sendEnqueuedRequest(op: WorldStateOp) {
155
+ // Here we increment the in flight counts before sending
156
+ ++this.inFlightCount;
157
+ if (op.mutating) {
158
+ ++this.inFlightMutatingCount;
159
+ }
160
+
161
+ // Make the request and pass the response/error through to the stored promise
162
+ op.request()
163
+ .then(op.promise.resolve, op.promise.reject)
164
+ .finally(() => {
165
+ this.checkAndEnqueue(op);
166
+ this.ops.delete(op.requestId);
167
+ });
168
+ }
169
+
170
+ public stop() {
171
+ // If there is already a stop promise then return it
172
+ if (this.stopPromise) {
173
+ return this.stopPromise;
174
+ }
175
+
176
+ // Otherwise create a new one and capture the resolve method
177
+ this.stopPromise = new Promise(resolve => {
178
+ this.stopResolve = resolve;
179
+ });
180
+
181
+ // If no outstanding requests then immediately resolve the promise
182
+ if (this.requests.length == 0 && this.inFlightCount == 0 && this.stopResolve !== undefined) {
183
+ this.stopResolve();
184
+ }
185
+ return this.stopPromise;
186
+ }
187
+ }
package/src/test/utils.ts CHANGED
@@ -3,7 +3,6 @@ import {
3
3
  MerkleTreeId,
4
4
  type MerkleTreeReadOperations,
5
5
  type MerkleTreeWriteOperations,
6
- TxEffect,
7
6
  } from '@aztec/circuit-types';
8
7
  import {
9
8
  AppendOnlyTreeSnapshot,
@@ -21,15 +20,9 @@ export async function mockBlock(blockNum: number, size: number, fork: MerkleTree
21
20
  const l2Block = L2Block.random(blockNum, size);
22
21
  const l1ToL2Messages = Array(16).fill(0).map(Fr.random);
23
22
 
24
- const paddedTxEffects = padArrayEnd(
25
- l2Block.body.txEffects,
26
- TxEffect.empty(),
27
- l2Block.body.numberOfTxsIncludingPadded,
28
- );
29
-
30
23
  // Sync the append only trees
31
24
  {
32
- const noteHashesPadded = paddedTxEffects.flatMap(txEffect =>
25
+ const noteHashesPadded = l2Block.body.txEffects.flatMap(txEffect =>
33
26
  padArrayEnd(txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX),
34
27
  );
35
28
  await fork.appendLeaves(MerkleTreeId.NOTE_HASH_TREE, noteHashesPadded);
@@ -41,7 +34,7 @@ export async function mockBlock(blockNum: number, size: number, fork: MerkleTree
41
34
  // Sync the indexed trees
42
35
  {
43
36
  // We insert the public data tree leaves with one batch per tx to avoid updating the same key twice
44
- for (const txEffect of paddedTxEffects) {
37
+ for (const txEffect of l2Block.body.txEffects) {
45
38
  await fork.batchInsert(
46
39
  MerkleTreeId.PUBLIC_DATA_TREE,
47
40
  txEffect.publicDataWrites.map(write => write.toBuffer()),
@@ -1,4 +1,4 @@
1
- import { type L2Block, MerkleTreeId, type SiblingPath, TxEffect } from '@aztec/circuit-types';
1
+ import { type L2Block, MerkleTreeId, type SiblingPath } from '@aztec/circuit-types';
2
2
  import {
3
3
  type BatchInsertionResult,
4
4
  type IndexedTreeId,
@@ -647,17 +647,11 @@ export class MerkleTrees implements MerkleTreeAdminDatabase {
647
647
  this.log.verbose(`Block ${l2Block.number} is not ours, rolling back world state and committing state from chain`);
648
648
  await this.#rollback();
649
649
 
650
- // We have to pad both the tx effects and the values within tx effects because that's how the trees are built
651
- // by circuits.
652
- const paddedTxEffects = padArrayEnd(
653
- l2Block.body.txEffects,
654
- TxEffect.empty(),
655
- l2Block.body.numberOfTxsIncludingPadded,
656
- );
650
+ // We have to pad the values within tx effects because that's how the trees are built by circuits.
657
651
 
658
652
  // Sync the append only trees
659
653
  {
660
- const noteHashesPadded = paddedTxEffects.flatMap(txEffect =>
654
+ const noteHashesPadded = l2Block.body.txEffects.flatMap(txEffect =>
661
655
  padArrayEnd(txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX),
662
656
  );
663
657
  await this.#appendLeaves(MerkleTreeId.NOTE_HASH_TREE, noteHashesPadded);
@@ -668,7 +662,7 @@ export class MerkleTrees implements MerkleTreeAdminDatabase {
668
662
 
669
663
  // Sync the indexed trees
670
664
  {
671
- const nullifiersPadded = paddedTxEffects.flatMap(txEffect =>
665
+ const nullifiersPadded = l2Block.body.txEffects.flatMap(txEffect =>
672
666
  padArrayEnd(txEffect.nullifiers, Fr.ZERO, MAX_NULLIFIERS_PER_TX),
673
667
  );
674
668
  await (this.trees[MerkleTreeId.NULLIFIER_TREE] as StandardIndexedTree).batchInsert(
@@ -679,7 +673,7 @@ export class MerkleTrees implements MerkleTreeAdminDatabase {
679
673
  const publicDataTree = this.trees[MerkleTreeId.PUBLIC_DATA_TREE] as StandardIndexedTree;
680
674
 
681
675
  // We insert the public data tree leaves with one batch per tx to avoid updating the same key twice
682
- for (const txEffect of paddedTxEffects) {
676
+ for (const txEffect of l2Block.body.txEffects) {
683
677
  const publicDataWrites = padArrayEnd(
684
678
  txEffect.publicDataWrites,
685
679
  PublicDataWrite.empty(),