@aztec/world-state 0.69.1-devnet → 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.
@@ -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
+ }