@aztec/txe 0.0.1-commit.96dac018d → 0.0.1-commit.993d240
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/ContractClassRegistry-KKNT527R.js +3 -0
- package/dest/ContractClassRegistry-KKNT527R.js.map +7 -0
- package/dest/ContractInstanceRegistry-GSKBNL3Q.js +3 -0
- package/dest/ContractInstanceRegistry-GSKBNL3Q.js.map +7 -0
- package/dest/FeeJuice-GUJ5O6GB.js +3 -0
- package/dest/FeeJuice-GUJ5O6GB.js.map +7 -0
- package/dest/MultiCallEntrypoint-ZQPCKVKL.js +3 -0
- package/dest/MultiCallEntrypoint-ZQPCKVKL.js.map +7 -0
- package/dest/SchnorrAccount-QIXE5DXA.js +3 -0
- package/dest/SchnorrAccount-QIXE5DXA.js.map +7 -0
- package/dest/bin/check_txe_oracle_version.d.ts +2 -0
- package/dest/bin/check_txe_oracle_version.d.ts.map +1 -0
- package/dest/bin/check_txe_oracle_version.js +32 -0
- package/dest/bin/index.js +3 -30
- package/dest/bin/index.js.map +7 -0
- package/dest/chunk-BJVAAXNA.js +3 -0
- package/dest/chunk-BJVAAXNA.js.map +7 -0
- package/dest/chunk-DF6RMOC6.js +304 -0
- package/dest/chunk-DF6RMOC6.js.map +7 -0
- package/dest/chunk-PPJTNNUG.js +265 -0
- package/dest/chunk-PPJTNNUG.js.map +7 -0
- package/dest/constants.d.ts +3 -1
- package/dest/constants.d.ts.map +1 -1
- package/dest/constants.js +5 -0
- package/dest/dispatcher_pool.d.ts +67 -0
- package/dest/dispatcher_pool.d.ts.map +1 -0
- package/dest/dispatcher_pool.js +284 -0
- package/dest/index.d.ts +188 -7
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +160 -119
- package/dest/metafile.json +38147 -0
- package/dest/msgpackr_fr_extension.d.ts +2 -0
- package/dest/msgpackr_fr_extension.d.ts.map +1 -0
- package/dest/msgpackr_fr_extension.js +21 -0
- package/dest/oracle/interfaces.d.ts +39 -28
- package/dest/oracle/interfaces.d.ts.map +1 -1
- package/dest/oracle/txe_oracle_public_context.d.ts +18 -13
- package/dest/oracle/txe_oracle_public_context.d.ts.map +1 -1
- package/dest/oracle/txe_oracle_public_context.js +27 -12
- package/dest/oracle/txe_oracle_top_level_context.d.ts +36 -20
- package/dest/oracle/txe_oracle_top_level_context.d.ts.map +1 -1
- package/dest/oracle/txe_oracle_top_level_context.js +125 -66
- package/dest/oracle/txe_private_execution_oracle.d.ts +17 -0
- package/dest/oracle/txe_private_execution_oracle.d.ts.map +1 -0
- package/dest/oracle/txe_private_execution_oracle.js +15 -0
- package/dest/rpc_server.d.ts +14 -0
- package/dest/rpc_server.d.ts.map +1 -0
- package/dest/rpc_server.js +78 -0
- package/dest/rpc_translator.d.ts +139 -83
- package/dest/rpc_translator.d.ts.map +1 -1
- package/dest/rpc_translator.js +568 -244
- package/dest/server.bundle.js +3 -0
- package/dest/server.bundle.js.map +7 -0
- package/dest/state_machine/archiver.d.ts +4 -4
- package/dest/state_machine/archiver.d.ts.map +1 -1
- package/dest/state_machine/archiver.js +22 -20
- package/dest/state_machine/dummy_p2p_client.d.ts +13 -7
- package/dest/state_machine/dummy_p2p_client.d.ts.map +1 -1
- package/dest/state_machine/dummy_p2p_client.js +16 -4
- package/dest/state_machine/global_variable_builder.d.ts +9 -4
- package/dest/state_machine/global_variable_builder.d.ts.map +1 -1
- package/dest/state_machine/global_variable_builder.js +9 -3
- package/dest/state_machine/index.d.ts +7 -3
- package/dest/state_machine/index.d.ts.map +1 -1
- package/dest/state_machine/index.js +23 -5
- package/dest/state_machine/mock_epoch_cache.d.ts +18 -3
- package/dest/state_machine/mock_epoch_cache.d.ts.map +1 -1
- package/dest/state_machine/mock_epoch_cache.js +35 -2
- package/dest/state_machine/synchronizer.d.ts +5 -5
- package/dest/state_machine/synchronizer.d.ts.map +1 -1
- package/dest/state_machine/synchronizer.js +4 -4
- package/dest/txe_oracle_version.d.ts +17 -0
- package/dest/txe_oracle_version.d.ts.map +1 -0
- package/dest/txe_oracle_version.js +14 -0
- package/dest/txe_session.d.ts +64 -6
- package/dest/txe_session.d.ts.map +1 -1
- package/dest/txe_session.js +159 -31
- package/dest/utils/encoding.d.ts +191 -0
- package/dest/utils/encoding.d.ts.map +1 -0
- package/dest/{util → utils}/encoding.js +7 -2
- package/dest/{util → utils}/expected_failure_error.d.ts +1 -1
- package/dest/utils/expected_failure_error.d.ts.map +1 -0
- package/dest/{util → utils}/txe_account_store.d.ts +1 -1
- package/dest/utils/txe_account_store.d.ts.map +1 -0
- package/dest/utils/txe_public_contract_data_source.d.ts +20 -0
- package/dest/utils/txe_public_contract_data_source.d.ts.map +1 -0
- package/dest/{util → utils}/txe_public_contract_data_source.js +1 -3
- package/dest/worker.bundle.js +3 -0
- package/dest/worker.bundle.js.map +7 -0
- package/dest/worker.d.ts +2 -0
- package/dest/worker.d.ts.map +1 -0
- package/dest/worker.js +92 -0
- package/package.json +37 -21
- package/src/bin/check_txe_oracle_version.ts +37 -0
- package/src/bin/index.ts +11 -2
- package/src/constants.ts +6 -0
- package/src/dispatcher_pool.ts +316 -0
- package/src/index.ts +178 -126
- package/src/msgpackr_fr_extension.ts +23 -0
- package/src/oracle/interfaces.ts +43 -34
- package/src/oracle/txe_oracle_public_context.ts +42 -12
- package/src/oracle/txe_oracle_top_level_context.ts +146 -77
- package/src/oracle/txe_private_execution_oracle.ts +30 -0
- package/src/rpc_server.ts +87 -0
- package/src/rpc_translator.ts +647 -327
- package/src/state_machine/archiver.ts +33 -18
- package/src/state_machine/dummy_p2p_client.ts +31 -11
- package/src/state_machine/global_variable_builder.ts +18 -3
- package/src/state_machine/index.ts +25 -6
- package/src/state_machine/mock_epoch_cache.ts +46 -3
- package/src/state_machine/synchronizer.ts +5 -5
- package/src/txe_oracle_version.ts +17 -0
- package/src/txe_session.ts +253 -33
- package/src/{util → utils}/encoding.ts +8 -2
- package/src/{util → utils}/txe_public_contract_data_source.ts +0 -2
- package/src/worker.ts +98 -0
- package/dest/util/encoding.d.ts +0 -720
- package/dest/util/encoding.d.ts.map +0 -1
- package/dest/util/expected_failure_error.d.ts.map +0 -1
- package/dest/util/txe_account_store.d.ts.map +0 -1
- package/dest/util/txe_public_contract_data_source.d.ts +0 -20
- package/dest/util/txe_public_contract_data_source.d.ts.map +0 -1
- /package/dest/{util → utils}/expected_failure_error.js +0 -0
- /package/dest/{util → utils}/txe_account_store.js +0 -0
- /package/src/{util → utils}/expected_failure_error.ts +0 -0
- /package/src/{util → utils}/txe_account_store.ts +0 -0
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
import { getSchnorrAccountContractArtifact } from '@aztec/accounts/schnorr/lazy';
|
|
2
|
+
import { BackendType, Barretenberg, BarretenbergSync } from '@aztec/bb.js';
|
|
3
|
+
import type { Logger } from '@aztec/foundation/log';
|
|
4
|
+
import { openEphemeralStore } from '@aztec/kv-store/lmdb-v2';
|
|
5
|
+
import { LazyProtocolContractsProvider } from '@aztec/protocol-contracts/providers/lazy';
|
|
6
|
+
import { ContractStore } from '@aztec/pxe/client/lazy';
|
|
7
|
+
import { getStandardAuthRegistry } from '@aztec/standard-contracts/auth-registry';
|
|
8
|
+
import { getContractClassFromArtifact } from '@aztec/stdlib/contract';
|
|
9
|
+
|
|
10
|
+
import { existsSync } from 'node:fs';
|
|
11
|
+
import { cpus } from 'node:os';
|
|
12
|
+
import { fileURLToPath } from 'node:url';
|
|
13
|
+
import { Worker } from 'node:worker_threads';
|
|
14
|
+
|
|
15
|
+
import { TXE_REQUIRED_PROTOCOL_CONTRACTS } from './index.js';
|
|
16
|
+
import type { TXEForeignCallInput } from './index.js';
|
|
17
|
+
import type { ForeignCallResult } from './utils/encoding.js';
|
|
18
|
+
|
|
19
|
+
void Barretenberg.initSingleton({ backend: BackendType.Wasm, skipSrsInit: true, threads: 1 });
|
|
20
|
+
void BarretenbergSync.initSingleton({ backend: BackendType.Wasm });
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Opens a fresh LMDB in a tmp dir and writes the protocol contracts in
|
|
24
|
+
* {@link TXE_REQUIRED_PROTOCOL_CONTRACTS} plus the standard AuthRegistry and the SchnorrAccount artifact,
|
|
25
|
+
* returning the directory path and the SchnorrAccount class id (hex). The store handle is intentionally kept
|
|
26
|
+
* alive: closing it would trigger the ephemeral-store cleanup hook and remove the tmp
|
|
27
|
+
* directory, so any worker that has not yet cloned would find it missing.
|
|
28
|
+
*/
|
|
29
|
+
export async function buildSharedContractStore(): Promise<{ dataDir: string; schnorrClassId: string }> {
|
|
30
|
+
const kvStore = await openEphemeralStore('txe-shared-contracts', undefined, 2);
|
|
31
|
+
const dataDir = kvStore.dataDirectory;
|
|
32
|
+
const contractStore = new ContractStore(kvStore);
|
|
33
|
+
const provider = new LazyProtocolContractsProvider();
|
|
34
|
+
const [protocolContracts, standardContracts, schnorrArtifact] = await Promise.all([
|
|
35
|
+
Promise.all(TXE_REQUIRED_PROTOCOL_CONTRACTS.map(name => provider.getProtocolContractArtifact(name))),
|
|
36
|
+
Promise.all([getStandardAuthRegistry()]),
|
|
37
|
+
getSchnorrAccountContractArtifact(),
|
|
38
|
+
]);
|
|
39
|
+
const schnorrClass = await getContractClassFromArtifact(schnorrArtifact);
|
|
40
|
+
await Promise.all([
|
|
41
|
+
...[...protocolContracts, ...standardContracts].flatMap(({ instance, artifact, contractClass }) => [
|
|
42
|
+
contractStore.addContractArtifact(artifact, contractClass),
|
|
43
|
+
contractStore.addContractInstance(instance),
|
|
44
|
+
]),
|
|
45
|
+
contractStore.addContractArtifact(schnorrArtifact, schnorrClass),
|
|
46
|
+
]);
|
|
47
|
+
return { dataDir, schnorrClassId: schnorrClass.id.toString() };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Resolves `worker.bundle.js` whether this code is running unbundled (next to dispatcher_pool.js
|
|
52
|
+
* inside `dest/`) or bundled into `dest/bin/index.js` (one directory deeper). `import.meta.url`
|
|
53
|
+
* refers to whichever module the calling code actually lives in; we try both relative locations
|
|
54
|
+
* and use whichever exists.
|
|
55
|
+
*/
|
|
56
|
+
function resolveWorkerBundlePath(): URL {
|
|
57
|
+
const candidates = [new URL('./worker.bundle.js', import.meta.url), new URL('../worker.bundle.js', import.meta.url)];
|
|
58
|
+
return candidates.find(u => existsSync(fileURLToPath(u))) ?? candidates[0];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
interface SerializedError {
|
|
62
|
+
message: string;
|
|
63
|
+
name?: string;
|
|
64
|
+
stack?: string;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
type WorkerMessage =
|
|
68
|
+
| { type: 'result'; requestId: number; ok: true; value: ForeignCallResult }
|
|
69
|
+
| { type: 'result'; requestId: number; ok: false; error: SerializedError }
|
|
70
|
+
| {
|
|
71
|
+
type: 'memstat';
|
|
72
|
+
sessions: number;
|
|
73
|
+
rss: number;
|
|
74
|
+
heapTotal: number;
|
|
75
|
+
heapUsed: number;
|
|
76
|
+
external: number;
|
|
77
|
+
arrayBuffers: number;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
interface WorkerSlot {
|
|
81
|
+
worker: Worker;
|
|
82
|
+
sessions: Set<number>;
|
|
83
|
+
inFlightRequestIds: Set<number>;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
interface PendingRequest {
|
|
87
|
+
resolve: (value: ForeignCallResult) => void;
|
|
88
|
+
reject: (err: Error) => void;
|
|
89
|
+
workerIdx: number;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function deserializeError(payload: SerializedError | undefined): Error {
|
|
93
|
+
if (!payload) {
|
|
94
|
+
return new Error('Worker returned an error with no payload');
|
|
95
|
+
}
|
|
96
|
+
const err = new Error(payload.message);
|
|
97
|
+
if (payload.name) {
|
|
98
|
+
err.name = payload.name;
|
|
99
|
+
}
|
|
100
|
+
if (payload.stack) {
|
|
101
|
+
err.stack = payload.stack;
|
|
102
|
+
}
|
|
103
|
+
return err;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export interface TXEDispatcherPoolOptions {
|
|
107
|
+
/** Number of worker threads */
|
|
108
|
+
workers?: number;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Main-thread router that owns a pool of TXE worker threads. Each worker runs its own
|
|
113
|
+
* {@link TXEDispatcher} and handles foreign-call requests for the sessions assigned to it.
|
|
114
|
+
*
|
|
115
|
+
* Routing is sticky by `session_id`: new sessions go to a freshly spawned worker (up to
|
|
116
|
+
* `maxWorkers`) or, once the cap is reached, to the existing worker with the fewest sessions.
|
|
117
|
+
* Each session's state — TXESession, native world state, KV stores — stays single-threaded
|
|
118
|
+
* within its worker; different sessions run in parallel across workers.
|
|
119
|
+
*
|
|
120
|
+
* The main thread builds a shared protocol-contracts LMDB once and passes its path via
|
|
121
|
+
* `workerData`. Workers clone the data file on demand instead of re-registering the contracts.
|
|
122
|
+
*/
|
|
123
|
+
export class TXEDispatcherPool {
|
|
124
|
+
private readonly workers: WorkerSlot[] = [];
|
|
125
|
+
private readonly sessionToWorker = new Map<number, number>();
|
|
126
|
+
private readonly pending = new Map<number, PendingRequest>();
|
|
127
|
+
private nextRequestId = 0;
|
|
128
|
+
private readonly maxWorkers: number;
|
|
129
|
+
private readonly readyPromise: Promise<void>;
|
|
130
|
+
private contractStoreSourceDir?: string;
|
|
131
|
+
private schnorrClassId?: string;
|
|
132
|
+
private workerPath?: URL;
|
|
133
|
+
|
|
134
|
+
constructor(
|
|
135
|
+
private readonly logger: Logger,
|
|
136
|
+
opts: TXEDispatcherPoolOptions = {},
|
|
137
|
+
) {
|
|
138
|
+
// TXE still doesn't scale well beyond 16 workers due to contention
|
|
139
|
+
this.maxWorkers = Math.max(1, opts.workers ?? Math.min(cpus().length, 16));
|
|
140
|
+
this.readyPromise = this.init();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Resolves once the shared protocol-contracts store is built. Workers spawn lazily on demand. */
|
|
144
|
+
public ready(): Promise<void> {
|
|
145
|
+
return this.readyPromise;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
private async init(): Promise<void> {
|
|
149
|
+
const t0 = Date.now();
|
|
150
|
+
const { dataDir, schnorrClassId } = await buildSharedContractStore();
|
|
151
|
+
this.contractStoreSourceDir = dataDir;
|
|
152
|
+
this.schnorrClassId = schnorrClassId;
|
|
153
|
+
this.workerPath = resolveWorkerBundlePath();
|
|
154
|
+
this.logger.debug(`TXE dispatcher pool ready (lazy spawn, cap=${this.maxWorkers})`, {
|
|
155
|
+
contractStoreSourceDir: dataDir,
|
|
156
|
+
schnorrClassId,
|
|
157
|
+
ms: Date.now() - t0,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Spawns a fresh worker and returns its index in `this.workers`. Caller must have awaited
|
|
163
|
+
* `init()` so the shared contract store path is set. Messages posted before the worker has
|
|
164
|
+
* finished loading are queued by Node's worker_threads transport.
|
|
165
|
+
*/
|
|
166
|
+
private spawnWorker(): number {
|
|
167
|
+
const workerIdx = this.workers.length;
|
|
168
|
+
const w = new Worker(this.workerPath!, {
|
|
169
|
+
workerData: { contractStoreSourceDir: this.contractStoreSourceDir, schnorrClassId: this.schnorrClassId },
|
|
170
|
+
});
|
|
171
|
+
const slot: WorkerSlot = {
|
|
172
|
+
worker: w,
|
|
173
|
+
sessions: new Set(),
|
|
174
|
+
inFlightRequestIds: new Set(),
|
|
175
|
+
};
|
|
176
|
+
this.workers.push(slot);
|
|
177
|
+
w.on('message', (msg: WorkerMessage) => this.handleMessage(workerIdx, msg));
|
|
178
|
+
w.on('error', err => this.handleWorkerError(workerIdx, err));
|
|
179
|
+
w.on('exit', code => {
|
|
180
|
+
if (code !== 0) {
|
|
181
|
+
this.handleWorkerError(workerIdx, new Error(`Worker ${workerIdx} exited with code ${code}`));
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
this.logger.debug(`Spawning TXE worker ${workerIdx} on demand`, {
|
|
185
|
+
cap: this.maxWorkers,
|
|
186
|
+
poolSize: this.workers.length,
|
|
187
|
+
});
|
|
188
|
+
return workerIdx;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** Routes a session-dispose request to the worker that owns the session. Fire-and-forget. */
|
|
192
|
+
disposeSession(sessionId: number): void {
|
|
193
|
+
const workerIdx = this.sessionToWorker.get(sessionId);
|
|
194
|
+
if (workerIdx === undefined) {
|
|
195
|
+
throw new Error(`disposeSession: no worker mapped for session ${sessionId}`);
|
|
196
|
+
}
|
|
197
|
+
this.sessionToWorker.delete(sessionId);
|
|
198
|
+
const slot = this.workers[workerIdx];
|
|
199
|
+
if (!slot) {
|
|
200
|
+
throw new Error(`disposeSession: worker ${workerIdx} (session ${sessionId}) missing from pool`);
|
|
201
|
+
}
|
|
202
|
+
slot.sessions.delete(sessionId);
|
|
203
|
+
slot.worker.postMessage({ type: 'dispose-session', sessionId });
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// eslint-disable-next-line camelcase
|
|
207
|
+
async resolve_foreign_call(callData: TXEForeignCallInput): Promise<ForeignCallResult> {
|
|
208
|
+
// Make sure the shared contract store + worker bundle path are in place before we spawn.
|
|
209
|
+
await this.readyPromise;
|
|
210
|
+
const sessionId = callData.session_id;
|
|
211
|
+
let workerIdx = this.sessionToWorker.get(sessionId);
|
|
212
|
+
if (workerIdx === undefined) {
|
|
213
|
+
workerIdx = this.pickOrSpawnWorker();
|
|
214
|
+
this.sessionToWorker.set(sessionId, workerIdx);
|
|
215
|
+
this.workers[workerIdx].sessions.add(sessionId);
|
|
216
|
+
this.logger.debug(`Routing new session ${sessionId} to worker ${workerIdx}`);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const slot = this.workers[workerIdx];
|
|
220
|
+
const requestId = this.nextRequestId++;
|
|
221
|
+
return new Promise<ForeignCallResult>((resolve, reject) => {
|
|
222
|
+
this.pending.set(requestId, { resolve, reject, workerIdx: workerIdx! });
|
|
223
|
+
slot.inFlightRequestIds.add(requestId);
|
|
224
|
+
slot.worker.postMessage({ type: 'foreign-call', requestId, callData });
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** Terminates all workers. Used by tests; production TXE exits the process directly. */
|
|
229
|
+
async shutdown(): Promise<void> {
|
|
230
|
+
const slots = this.workers.splice(0, this.workers.length);
|
|
231
|
+
await Promise.all(slots.map(s => s.worker.terminate()));
|
|
232
|
+
for (const [requestId, pending] of this.pending) {
|
|
233
|
+
pending.reject(new Error('TXE dispatcher pool was shut down'));
|
|
234
|
+
this.pending.delete(requestId);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Returns the index of the worker that should handle a new session. Spawns a fresh worker if
|
|
240
|
+
* the pool hasn't reached its cap; otherwise picks the existing worker with the fewest
|
|
241
|
+
* assigned sessions. Always returns a valid index into `this.workers`.
|
|
242
|
+
*/
|
|
243
|
+
private pickOrSpawnWorker(): number {
|
|
244
|
+
if (this.workers.length < this.maxWorkers) {
|
|
245
|
+
return this.spawnWorker();
|
|
246
|
+
}
|
|
247
|
+
let minIdx = 0;
|
|
248
|
+
let minCount = this.workers[0].sessions.size;
|
|
249
|
+
for (let i = 1; i < this.workers.length; i++) {
|
|
250
|
+
const count = this.workers[i].sessions.size;
|
|
251
|
+
if (count < minCount) {
|
|
252
|
+
minIdx = i;
|
|
253
|
+
minCount = count;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return minIdx;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
private handleMessage(workerIdx: number, msg: WorkerMessage): void {
|
|
260
|
+
switch (msg.type) {
|
|
261
|
+
case 'result':
|
|
262
|
+
this.handleResult(workerIdx, msg);
|
|
263
|
+
return;
|
|
264
|
+
case 'memstat':
|
|
265
|
+
this.logger.debug(`worker ${workerIdx} memstat`, {
|
|
266
|
+
worker: workerIdx,
|
|
267
|
+
sessions: msg.sessions,
|
|
268
|
+
rssMiB: Math.round(msg.rss / 1024 / 1024),
|
|
269
|
+
heapTotalMiB: Math.round(msg.heapTotal / 1024 / 1024),
|
|
270
|
+
heapUsedMiB: Math.round(msg.heapUsed / 1024 / 1024),
|
|
271
|
+
externalMiB: Math.round(msg.external / 1024 / 1024),
|
|
272
|
+
arrayBuffersMiB: Math.round(msg.arrayBuffers / 1024 / 1024),
|
|
273
|
+
});
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
private handleResult(workerIdx: number, msg: WorkerMessage & { type: 'result' }): void {
|
|
279
|
+
const pending = this.pending.get(msg.requestId);
|
|
280
|
+
if (!pending) {
|
|
281
|
+
throw new Error(`handleResult: request ${msg.requestId} (worker ${workerIdx}) not in pending map`);
|
|
282
|
+
}
|
|
283
|
+
this.pending.delete(msg.requestId);
|
|
284
|
+
const slot = this.workers[workerIdx];
|
|
285
|
+
if (!slot) {
|
|
286
|
+
throw new Error(`handleResult: worker ${workerIdx} (request ${msg.requestId}) missing from pool`);
|
|
287
|
+
}
|
|
288
|
+
slot.inFlightRequestIds.delete(msg.requestId);
|
|
289
|
+
if (msg.ok) {
|
|
290
|
+
pending.resolve(msg.value);
|
|
291
|
+
} else {
|
|
292
|
+
pending.reject(deserializeError(msg.error));
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
private handleWorkerError(workerIdx: number, err: Error): void {
|
|
297
|
+
this.logger.error(`TXE worker ${workerIdx} crashed; sessions assigned to it will fail`, err);
|
|
298
|
+
const slot = this.workers[workerIdx];
|
|
299
|
+
if (!slot) {
|
|
300
|
+
throw new Error(`handleWorkerError: worker ${workerIdx} missing from pool (orig err: ${err.message})`);
|
|
301
|
+
}
|
|
302
|
+
for (const requestId of slot.inFlightRequestIds) {
|
|
303
|
+
const pending = this.pending.get(requestId);
|
|
304
|
+
if (!pending) {
|
|
305
|
+
throw new Error(`handleWorkerError: in-flight request ${requestId} (worker ${workerIdx}) not in pending map`);
|
|
306
|
+
}
|
|
307
|
+
pending.reject(err);
|
|
308
|
+
this.pending.delete(requestId);
|
|
309
|
+
}
|
|
310
|
+
slot.inFlightRequestIds.clear();
|
|
311
|
+
for (const sessionId of slot.sessions) {
|
|
312
|
+
this.sessionToWorker.delete(sessionId);
|
|
313
|
+
}
|
|
314
|
+
slot.sessions.clear();
|
|
315
|
+
}
|
|
316
|
+
}
|