|
1 | 1 | import { SchnorrAccountContractArtifact } from '@aztec/accounts/schnorr'; |
2 | | -import { |
3 | | - AztecAddress, |
4 | | - type ContractInstanceWithAddress, |
5 | | - Fr, |
6 | | - type NoirCompiledContract, |
7 | | - PublicKeys, |
8 | | - deriveKeys, |
9 | | - getContractInstanceFromInstantiationParams, |
10 | | - loadContractArtifact, |
11 | | -} from '@aztec/aztec.js'; |
| 2 | +import { Fr } from '@aztec/foundation/fields'; |
12 | 3 | import { createSafeJsonRpcServer } from '@aztec/foundation/json-rpc/server'; |
13 | 4 | import type { Logger } from '@aztec/foundation/log'; |
| 5 | +import { Timer } from '@aztec/foundation/timer'; |
14 | 6 | import { type ProtocolContract, protocolContractNames } from '@aztec/protocol-contracts'; |
15 | 7 | import { BundledProtocolContractsProvider } from '@aztec/protocol-contracts/providers/bundle'; |
16 | | -import { computeArtifactHash } from '@aztec/stdlib/contract'; |
| 8 | +import { loadContractArtifact } from '@aztec/stdlib/abi'; |
| 9 | +import { AztecAddress } from '@aztec/stdlib/aztec-address'; |
| 10 | +import { |
| 11 | + type ContractInstanceWithAddress, |
| 12 | + computeArtifactHash, |
| 13 | + getContractInstanceFromInstantiationParams, |
| 14 | +} from '@aztec/stdlib/contract'; |
| 15 | +import { PublicKeys, deriveKeys } from '@aztec/stdlib/keys'; |
| 16 | +import type { NoirCompiledContract } from '@aztec/stdlib/noir'; |
17 | 17 | import type { ApiSchemaFor, ZodFor } from '@aztec/stdlib/schemas'; |
18 | 18 |
|
19 | 19 | import { createHash } from 'crypto'; |
@@ -41,6 +41,7 @@ import type { ContractArtifactWithHash } from './util/txe_contract_data_provider |
41 | 41 | interface SessionWorker { |
42 | 42 | worker: Worker; |
43 | 43 | processing: boolean; |
| 44 | + timeout?: NodeJS.Timeout; |
44 | 45 | } |
45 | 46 |
|
46 | 47 | const sessionWorkers = new Map<number, SessionWorker>(); |
@@ -93,21 +94,29 @@ class TXEDispatcher { |
93 | 94 | private async createSessionWorker(): Promise<SessionWorker> { |
94 | 95 | // Ensure protocol contracts are loaded |
95 | 96 | if (!this.protocolContracts) { |
| 97 | + this.logger.debug('Loading protocol contracts'); |
| 98 | + const loadTimer = new Timer(); |
96 | 99 | this.protocolContracts = await Promise.all( |
97 | 100 | protocolContractNames.map(name => new BundledProtocolContractsProvider().getProtocolContractArtifact(name)), |
98 | 101 | ); |
| 102 | + this.logger.debug(`Protocol Contract loading took ${loadTimer.ms()}ms`); |
99 | 103 | } |
100 | 104 |
|
101 | 105 | // Serialize protocol contracts for worker |
| 106 | + const serTimer = new Timer(); |
102 | 107 | const serializedProtocolContracts = serializeProtocolContracts(this.protocolContracts); |
| 108 | + this.logger.debug(`Protocol Contract serialization took ${serTimer.ms()}ms`); |
103 | 109 |
|
| 110 | + const createTimer = new Timer(); |
104 | 111 | const worker = new Worker('./dest/session_worker.js', { |
105 | 112 | workerData: { serializedProtocolContracts }, |
106 | 113 | }); |
107 | 114 |
|
108 | 115 | // Wait for the worker to signal it's ready |
109 | 116 | await this.waitForWorkerReady(worker); |
110 | 117 |
|
| 118 | + this.logger.debug(`Worker creation took ${createTimer.ms()}ms`); |
| 119 | + |
111 | 120 | return { |
112 | 121 | worker, |
113 | 122 | processing: false, |
@@ -307,8 +316,23 @@ class TXEDispatcher { |
307 | 316 | // Get or create a worker for this session |
308 | 317 | const sessionWorker = await this.getOrCreateWorker(sessionId); |
309 | 318 |
|
| 319 | + if (sessionWorker.timeout) { |
| 320 | + clearTimeout(sessionWorker.timeout); |
| 321 | + } |
| 322 | + |
310 | 323 | // Send the request to the worker and wait for the response |
311 | | - return await this.sendToWorker(sessionWorker, sessionId, functionName, inputs); |
| 324 | + const result = await this.sendToWorker(sessionWorker, sessionId, functionName, inputs); |
| 325 | + |
| 326 | + sessionWorker.timeout = setTimeout(() => { |
| 327 | + // eslint-disable-next-line @typescript-eslint/no-floating-promises |
| 328 | + (async () => { |
| 329 | + this.logger.debug(`Killing session ${sessionId}`); |
| 330 | + sessionWorkers.delete(sessionId); |
| 331 | + await sessionWorker.worker.terminate(); |
| 332 | + })(); |
| 333 | + }, 1500); |
| 334 | + |
| 335 | + return result; |
312 | 336 | } |
313 | 337 | } |
314 | 338 |
|
|
0 commit comments