Skip to content

Commit 56844bb

Browse files
committed
Fix imports, add worker death
1 parent 2560347 commit 56844bb

File tree

12 files changed

+62
-28
lines changed

12 files changed

+62
-28
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@ docs/docs/protocol-specs/public-vm/gen/
3131
# for those who use Claude Code
3232
CLAUDE.md
3333
.claude
34+
35+
# 0x flamegraphs
36+
*.0x

cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
"Fieldable",
126126
"Filecoin",
127127
"filestat",
128+
"flamegraphs",
128129
"Flashbots",
129130
"flatmap",
130131
"foundryup",

yarn-project/txe/src/bin/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env -S node --no-warnings
2-
import { createLogger } from '@aztec/aztec.js';
32
import { startHttpRpcServer } from '@aztec/foundation/json-rpc/server';
3+
import { createLogger } from '@aztec/foundation/log';
44

55
import { createTXERpcServer } from '../index.js';
66

yarn-project/txe/src/index.ts

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
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';
123
import { createSafeJsonRpcServer } from '@aztec/foundation/json-rpc/server';
134
import type { Logger } from '@aztec/foundation/log';
5+
import { Timer } from '@aztec/foundation/timer';
146
import { type ProtocolContract, protocolContractNames } from '@aztec/protocol-contracts';
157
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';
1717
import type { ApiSchemaFor, ZodFor } from '@aztec/stdlib/schemas';
1818

1919
import { createHash } from 'crypto';
@@ -41,6 +41,7 @@ import type { ContractArtifactWithHash } from './util/txe_contract_data_provider
4141
interface SessionWorker {
4242
worker: Worker;
4343
processing: boolean;
44+
timeout?: NodeJS.Timeout;
4445
}
4546

4647
const sessionWorkers = new Map<number, SessionWorker>();
@@ -93,21 +94,29 @@ class TXEDispatcher {
9394
private async createSessionWorker(): Promise<SessionWorker> {
9495
// Ensure protocol contracts are loaded
9596
if (!this.protocolContracts) {
97+
this.logger.debug('Loading protocol contracts');
98+
const loadTimer = new Timer();
9699
this.protocolContracts = await Promise.all(
97100
protocolContractNames.map(name => new BundledProtocolContractsProvider().getProtocolContractArtifact(name)),
98101
);
102+
this.logger.debug(`Protocol Contract loading took ${loadTimer.ms()}ms`);
99103
}
100104

101105
// Serialize protocol contracts for worker
106+
const serTimer = new Timer();
102107
const serializedProtocolContracts = serializeProtocolContracts(this.protocolContracts);
108+
this.logger.debug(`Protocol Contract serialization took ${serTimer.ms()}ms`);
103109

110+
const createTimer = new Timer();
104111
const worker = new Worker('./dest/session_worker.js', {
105112
workerData: { serializedProtocolContracts },
106113
});
107114

108115
// Wait for the worker to signal it's ready
109116
await this.waitForWorkerReady(worker);
110117

118+
this.logger.debug(`Worker creation took ${createTimer.ms()}ms`);
119+
111120
return {
112121
worker,
113122
processing: false,
@@ -307,8 +316,23 @@ class TXEDispatcher {
307316
// Get or create a worker for this session
308317
const sessionWorker = await this.getOrCreateWorker(sessionId);
309318

319+
if (sessionWorker.timeout) {
320+
clearTimeout(sessionWorker.timeout);
321+
}
322+
310323
// 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;
312336
}
313337
}
314338

yarn-project/txe/src/oracle/txe_oracle.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { type AztecNode, Body, L2Block, Note } from '@aztec/aztec.js';
21
import {
32
CONTRACT_INSTANCE_REGISTRY_CONTRACT_ADDRESS,
43
DEFAULT_DA_GAS_LIMIT,
@@ -26,7 +25,7 @@ import {
2625
PrivateEventDataProvider,
2726
TaggingDataProvider,
2827
enrichPublicSimulationError,
29-
} from '@aztec/pxe/server';
28+
} from '@aztec/pxe/client/lazy';
3029
import {
3130
ExecutionNoteCache,
3231
HashedValuesCache,
@@ -60,6 +59,7 @@ import {
6059
import { AuthWitness } from '@aztec/stdlib/auth-witness';
6160
import { PublicDataWrite } from '@aztec/stdlib/avm';
6261
import { AztecAddress } from '@aztec/stdlib/aztec-address';
62+
import { Body, L2Block } from '@aztec/stdlib/block';
6363
import { type ContractInstance, type ContractInstanceWithAddress, computePartialAddress } from '@aztec/stdlib/contract';
6464
import { Gas, GasFees, GasSettings } from '@aztec/stdlib/gas';
6565
import {
@@ -71,7 +71,7 @@ import {
7171
siloNoteHash,
7272
siloNullifier,
7373
} from '@aztec/stdlib/hash';
74-
import type { MerkleTreeReadOperations, MerkleTreeWriteOperations } from '@aztec/stdlib/interfaces/server';
74+
import type { AztecNode, MerkleTreeReadOperations, MerkleTreeWriteOperations } from '@aztec/stdlib/interfaces/server';
7575
import {
7676
type KeyValidationRequest,
7777
PartialPrivateTailPublicInputsForPublic,
@@ -81,7 +81,7 @@ import {
8181
PublicCallRequest,
8282
} from '@aztec/stdlib/kernel';
8383
import { ContractClassLog, IndexedTaggingSecret, PrivateLog, type PublicLog } from '@aztec/stdlib/logs';
84-
import type { NoteStatus } from '@aztec/stdlib/note';
84+
import { Note, type NoteStatus } from '@aztec/stdlib/note';
8585
import { ClientIvcProof } from '@aztec/stdlib/proofs';
8686
import {
8787
makeAppendOnlyTreeSnapshot,
@@ -335,7 +335,7 @@ export class TXE extends TXETypedOracle {
335335
inputs.callContext = new CallContext(this.msgSender, this.contractAddress, this.functionSelector, isStaticCall);
336336
inputs.startSideEffectCounter = sideEffectsCounter;
337337

338-
this.logger.info(`Created private context for block ${blockNumber}`);
338+
this.logger.verbose(`Created private context for block ${blockNumber}`);
339339

340340
return inputs;
341341
}
@@ -705,7 +705,7 @@ export class TXE extends TXETypedOracle {
705705
header.globalVariables.version = new Fr(this.ROLLUP_VERSION);
706706
header.globalVariables.chainId = new Fr(this.CHAIN_ID);
707707

708-
this.logger.info(`Created block ${blockNumber} with timestamp ${header.globalVariables.timestamp}`);
708+
this.logger.verbose(`Created block ${blockNumber} with timestamp ${header.globalVariables.timestamp}`);
709709

710710
l2Block.header = header;
711711

yarn-project/txe/src/oracle/txe_typed_oracle.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import type { CompleteAddress, ContractArtifact, ContractInstanceWithAddress, TxHash } from '@aztec/aztec.js';
21
import type { Fr } from '@aztec/foundation/fields';
32
import { TypedOracle } from '@aztec/pxe/simulator';
4-
import type { FunctionSelector } from '@aztec/stdlib/abi';
3+
import type { ContractArtifact, FunctionSelector } from '@aztec/stdlib/abi';
54
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
5+
import type { CompleteAddress, ContractInstanceWithAddress } from '@aztec/stdlib/contract';
66
import type { PrivateContextInputs } from '@aztec/stdlib/kernel';
7+
import type { TxHash } from '@aztec/stdlib/tx';
78
import type { UInt64 } from '@aztec/stdlib/types';
89

910
class OracleMethodNotAvailableError extends Error {

yarn-project/txe/src/session_worker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Timer } from '@aztec/foundation/timer';
12
import type { ProtocolContract } from '@aztec/protocol-contracts';
23

34
import { parentPort, workerData } from 'worker_threads';

yarn-project/txe/src/state_machine/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { type AztecNodeConfig, AztecNodeService } from '@aztec/aztec-node';
22
import { TestCircuitVerifier } from '@aztec/bb-prover/test';
33
import { createLogger } from '@aztec/foundation/log';
44
import type { AztecAsyncKVStore } from '@aztec/kv-store';
5-
import { SyncDataProvider } from '@aztec/pxe/server';
5+
import { SyncDataProvider } from '@aztec/pxe/client/lazy';
66
import type { L2Block } from '@aztec/stdlib/block';
77
import type { AztecNode } from '@aztec/stdlib/interfaces/client';
88
import { getPackageVersion } from '@aztec/stdlib/update-checker';

yarn-project/txe/src/txe_service/txe_service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { type ContractInstanceWithAddress, Fr, Point } from '@aztec/aztec.js';
1+
import { Fr, Point } from '@aztec/foundation/fields';
22
import { packAsRetrievedNote } from '@aztec/pxe/simulator';
33
import { type ContractArtifact, FunctionSelector, NoteSelector } from '@aztec/stdlib/abi';
44
import { AztecAddress } from '@aztec/stdlib/aztec-address';
5+
import type { ContractInstanceWithAddress } from '@aztec/stdlib/contract';
56
import { MerkleTreeId } from '@aztec/stdlib/trees';
67

78
import type { TXETypedOracle } from '../oracle/txe_typed_oracle.js';

yarn-project/txe/src/txe_session.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { AztecAddress } from '@aztec/aztec.js';
21
import { type Logger, createLogger } from '@aztec/foundation/log';
32
import { KeyStore } from '@aztec/key-store';
43
import { openTmpStore } from '@aztec/kv-store/lmdb-v2';
@@ -9,7 +8,8 @@ import {
98
NoteDataProvider,
109
PrivateEventDataProvider,
1110
TaggingDataProvider,
12-
} from '@aztec/pxe/server';
11+
} from '@aztec/pxe/client/lazy';
12+
import { AztecAddress } from '@aztec/stdlib/aztec-address';
1313

1414
import { TXE } from './oracle/txe_oracle.js';
1515
import { TXEStateMachine } from './state_machine/index.js';

0 commit comments

Comments
 (0)