@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.
- package/dest/instrumentation/instrumentation.d.ts +3 -4
- package/dest/instrumentation/instrumentation.d.ts.map +1 -1
- package/dest/instrumentation/instrumentation.js +9 -4
- package/dest/native/ipc_world_state_instance.d.ts +22 -59
- package/dest/native/ipc_world_state_instance.d.ts.map +1 -1
- package/dest/native/ipc_world_state_instance.js +495 -520
- package/dest/native/merkle_trees_facade.d.ts +3 -4
- package/dest/native/merkle_trees_facade.d.ts.map +1 -1
- package/dest/native/merkle_trees_facade.js +109 -37
- package/dest/native/message.d.ts +233 -14
- package/dest/native/message.d.ts.map +1 -1
- package/dest/native/message.js +42 -0
- package/dest/native/native_world_state.d.ts +28 -14
- package/dest/native/native_world_state.d.ts.map +1 -1
- package/dest/native/native_world_state.js +102 -101
- package/dest/native/native_world_state_instance.d.ts +42 -58
- package/dest/native/native_world_state_instance.d.ts.map +1 -1
- package/dest/native/native_world_state_instance.js +222 -5
- package/dest/native/world_state_ops_queue.d.ts +19 -0
- package/dest/native/world_state_ops_queue.d.ts.map +1 -0
- package/dest/native/world_state_ops_queue.js +146 -0
- package/dest/synchronizer/errors.d.ts +8 -1
- package/dest/synchronizer/errors.d.ts.map +1 -1
- package/dest/synchronizer/errors.js +8 -1
- package/dest/synchronizer/factory.d.ts +1 -1
- package/dest/synchronizer/factory.d.ts.map +1 -1
- package/dest/synchronizer/factory.js +1 -1
- package/dest/synchronizer/index.d.ts +2 -1
- package/dest/synchronizer/index.d.ts.map +1 -1
- package/dest/synchronizer/index.js +1 -0
- package/dest/synchronizer/server_world_state_synchronizer.d.ts +11 -6
- package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -1
- package/dest/synchronizer/server_world_state_synchronizer.js +55 -43
- package/dest/test/utils.d.ts +1 -1
- package/dest/test/utils.d.ts.map +1 -1
- package/dest/test/utils.js +6 -1
- package/dest/testing.d.ts +2 -2
- package/dest/testing.d.ts.map +1 -1
- package/dest/testing.js +15 -4
- package/package.json +10 -12
- package/src/instrumentation/instrumentation.ts +18 -6
- package/src/native/ipc_world_state_instance.ts +503 -608
- package/src/native/merkle_trees_facade.ts +104 -59
- package/src/native/message.ts +300 -13
- package/src/native/native_world_state.ts +156 -121
- package/src/native/native_world_state_instance.ts +307 -96
- package/src/native/world_state_ops_queue.ts +190 -0
- package/src/synchronizer/errors.ts +8 -0
- package/src/synchronizer/factory.ts +1 -0
- package/src/synchronizer/index.ts +1 -0
- package/src/synchronizer/server_world_state_synchronizer.ts +63 -30
- package/src/test/utils.ts +10 -1
- package/src/testing.ts +14 -4
- package/dest/native/world_state_operation.d.ts +0 -7
- package/dest/native/world_state_operation.d.ts.map +0 -1
- package/dest/native/world_state_operation.js +0 -5
- package/src/native/world_state_operation.ts +0 -33
|
@@ -1,107 +1,318 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
*
|
|
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
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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
|
-
*
|
|
98
|
-
*
|
|
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
|
-
|
|
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
|
-
*
|
|
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
|
}
|