@aztec/txe 0.0.1-commit.0208eb9 → 0.0.1-commit.031e54b

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.
Files changed (158) hide show
  1. package/dest/ContractClassRegistry-PII7Q556.js +3 -0
  2. package/dest/ContractClassRegistry-PII7Q556.js.map +7 -0
  3. package/dest/ContractInstanceRegistry-3MHLGFNZ.js +3 -0
  4. package/dest/ContractInstanceRegistry-3MHLGFNZ.js.map +7 -0
  5. package/dest/FeeJuice-TQV5AQK7.js +3 -0
  6. package/dest/FeeJuice-TQV5AQK7.js.map +7 -0
  7. package/dest/MultiCallEntrypoint-SBJUIJY7.js +3 -0
  8. package/dest/MultiCallEntrypoint-SBJUIJY7.js.map +7 -0
  9. package/dest/SchnorrAccount-MQXPIZ44.js +3 -0
  10. package/dest/SchnorrAccount-MQXPIZ44.js.map +7 -0
  11. package/dest/bin/check_txe_oracle_version.d.ts +2 -0
  12. package/dest/bin/check_txe_oracle_version.d.ts.map +1 -0
  13. package/dest/bin/check_txe_oracle_version.js +61 -0
  14. package/dest/bin/index.js +3 -30
  15. package/dest/bin/index.js.map +7 -0
  16. package/dest/bin/oracle_test_server.d.ts +3 -0
  17. package/dest/bin/oracle_test_server.d.ts.map +1 -0
  18. package/dest/bin/oracle_test_server.js +41 -0
  19. package/dest/chunk-3VX4QA7B.js +265 -0
  20. package/dest/chunk-3VX4QA7B.js.map +7 -0
  21. package/dest/chunk-BJVAAXNA.js +3 -0
  22. package/dest/chunk-BJVAAXNA.js.map +7 -0
  23. package/dest/chunk-FU3LBVE4.js +304 -0
  24. package/dest/chunk-FU3LBVE4.js.map +7 -0
  25. package/dest/constants.d.ts +5 -1
  26. package/dest/constants.d.ts.map +1 -1
  27. package/dest/constants.js +8 -0
  28. package/dest/dispatcher_pool.d.ts +67 -0
  29. package/dest/dispatcher_pool.d.ts.map +1 -0
  30. package/dest/dispatcher_pool.js +284 -0
  31. package/dest/index.d.ts +185 -7
  32. package/dest/index.d.ts.map +1 -1
  33. package/dest/index.js +71 -157
  34. package/dest/metafile.json +38352 -0
  35. package/dest/msgpackr_fr_extension.d.ts +2 -0
  36. package/dest/msgpackr_fr_extension.d.ts.map +1 -0
  37. package/dest/msgpackr_fr_extension.js +21 -0
  38. package/dest/oracle/interfaces.d.ts +56 -30
  39. package/dest/oracle/interfaces.d.ts.map +1 -1
  40. package/dest/oracle/test-resolver/fixtures.d.ts +43 -0
  41. package/dest/oracle/test-resolver/fixtures.d.ts.map +1 -0
  42. package/dest/oracle/test-resolver/fixtures.js +39 -0
  43. package/dest/oracle/test-resolver/index.d.ts +9 -0
  44. package/dest/oracle/test-resolver/index.d.ts.map +1 -0
  45. package/dest/oracle/test-resolver/index.js +33 -0
  46. package/dest/oracle/test-resolver/resolver.d.ts +34 -0
  47. package/dest/oracle/test-resolver/resolver.d.ts.map +1 -0
  48. package/dest/oracle/test-resolver/resolver.js +114 -0
  49. package/dest/oracle/txe_oracle_public_context.d.ts +38 -14
  50. package/dest/oracle/txe_oracle_public_context.d.ts.map +1 -1
  51. package/dest/oracle/txe_oracle_public_context.js +55 -13
  52. package/dest/oracle/txe_oracle_registry.d.ts +147 -0
  53. package/dest/oracle/txe_oracle_registry.d.ts.map +1 -0
  54. package/dest/oracle/txe_oracle_registry.js +562 -0
  55. package/dest/oracle/txe_oracle_top_level_context.d.ts +45 -26
  56. package/dest/oracle/txe_oracle_top_level_context.d.ts.map +1 -1
  57. package/dest/oracle/txe_oracle_top_level_context.js +242 -80
  58. package/dest/oracle/txe_oracle_version.d.ts +17 -0
  59. package/dest/oracle/txe_oracle_version.d.ts.map +1 -0
  60. package/dest/oracle/txe_oracle_version.js +14 -0
  61. package/dest/oracle/txe_private_execution_oracle.d.ts +17 -0
  62. package/dest/oracle/txe_private_execution_oracle.d.ts.map +1 -0
  63. package/dest/oracle/txe_private_execution_oracle.js +15 -0
  64. package/dest/rpc_server.d.ts +14 -0
  65. package/dest/rpc_server.d.ts.map +1 -0
  66. package/dest/rpc_server.js +78 -0
  67. package/dest/rpc_translator.d.ts +96 -228
  68. package/dest/rpc_translator.d.ts.map +1 -1
  69. package/dest/rpc_translator.js +815 -624
  70. package/dest/server.bundle.js +3 -0
  71. package/dest/server.bundle.js.map +7 -0
  72. package/dest/state_machine/archiver.d.ts +6 -5
  73. package/dest/state_machine/archiver.d.ts.map +1 -1
  74. package/dest/state_machine/archiver.js +32 -21
  75. package/dest/state_machine/dummy_p2p_client.d.ts +26 -16
  76. package/dest/state_machine/dummy_p2p_client.d.ts.map +1 -1
  77. package/dest/state_machine/dummy_p2p_client.js +44 -20
  78. package/dest/state_machine/global_variable_builder.d.ts +9 -4
  79. package/dest/state_machine/global_variable_builder.d.ts.map +1 -1
  80. package/dest/state_machine/global_variable_builder.js +9 -3
  81. package/dest/state_machine/index.d.ts +10 -6
  82. package/dest/state_machine/index.d.ts.map +1 -1
  83. package/dest/state_machine/index.js +35 -12
  84. package/dest/state_machine/mock_epoch_cache.d.ts +18 -3
  85. package/dest/state_machine/mock_epoch_cache.d.ts.map +1 -1
  86. package/dest/state_machine/mock_epoch_cache.js +33 -2
  87. package/dest/state_machine/synchronizer.d.ts +5 -5
  88. package/dest/state_machine/synchronizer.d.ts.map +1 -1
  89. package/dest/state_machine/synchronizer.js +4 -4
  90. package/dest/txe_session.d.ts +80 -15
  91. package/dest/txe_session.d.ts.map +1 -1
  92. package/dest/txe_session.js +240 -43
  93. package/dest/utils/block_creation.d.ts +1 -1
  94. package/dest/utils/block_creation.d.ts.map +1 -1
  95. package/dest/utils/block_creation.js +3 -1
  96. package/dest/utils/encoding.d.ts +191 -0
  97. package/dest/utils/encoding.d.ts.map +1 -0
  98. package/dest/{util → utils}/encoding.js +7 -2
  99. package/dest/{util → utils}/expected_failure_error.d.ts +1 -1
  100. package/dest/utils/expected_failure_error.d.ts.map +1 -0
  101. package/dest/{util → utils}/txe_account_store.d.ts +1 -1
  102. package/dest/utils/txe_account_store.d.ts.map +1 -0
  103. package/dest/utils/txe_artifact_resolver.d.ts +37 -0
  104. package/dest/utils/txe_artifact_resolver.d.ts.map +1 -0
  105. package/dest/utils/txe_artifact_resolver.js +161 -0
  106. package/dest/utils/txe_public_contract_data_source.d.ts +20 -0
  107. package/dest/utils/txe_public_contract_data_source.d.ts.map +1 -0
  108. package/dest/{util → utils}/txe_public_contract_data_source.js +6 -25
  109. package/dest/worker.bundle.js +3 -0
  110. package/dest/worker.bundle.js.map +7 -0
  111. package/dest/worker.d.ts +2 -0
  112. package/dest/worker.d.ts.map +1 -0
  113. package/dest/worker.js +92 -0
  114. package/package.json +38 -21
  115. package/src/bin/check_txe_oracle_version.ts +70 -0
  116. package/src/bin/index.ts +11 -2
  117. package/src/bin/oracle_test_server.ts +51 -0
  118. package/src/constants.ts +10 -0
  119. package/src/dispatcher_pool.ts +316 -0
  120. package/src/index.ts +97 -190
  121. package/src/msgpackr_fr_extension.ts +23 -0
  122. package/src/oracle/interfaces.ts +58 -35
  123. package/src/oracle/test-resolver/fixtures.ts +84 -0
  124. package/src/oracle/test-resolver/index.ts +45 -0
  125. package/src/oracle/test-resolver/resolver.ts +165 -0
  126. package/src/oracle/txe_oracle_public_context.ts +72 -12
  127. package/src/oracle/txe_oracle_registry.ts +401 -0
  128. package/src/oracle/txe_oracle_top_level_context.ts +282 -146
  129. package/src/oracle/txe_oracle_version.ts +17 -0
  130. package/src/oracle/txe_private_execution_oracle.ts +30 -0
  131. package/src/rpc_server.ts +87 -0
  132. package/src/rpc_translator.ts +1000 -1006
  133. package/src/state_machine/archiver.ts +44 -20
  134. package/src/state_machine/dummy_p2p_client.ts +70 -32
  135. package/src/state_machine/global_variable_builder.ts +18 -3
  136. package/src/state_machine/index.ts +48 -13
  137. package/src/state_machine/mock_epoch_cache.ts +43 -3
  138. package/src/state_machine/synchronizer.ts +5 -5
  139. package/src/txe_session.ts +356 -96
  140. package/src/utils/block_creation.ts +3 -1
  141. package/src/{util → utils}/encoding.ts +8 -2
  142. package/src/utils/txe_artifact_resolver.ts +217 -0
  143. package/src/{util → utils}/txe_public_contract_data_source.ts +10 -38
  144. package/src/worker.ts +98 -0
  145. package/dest/util/encoding.d.ts +0 -720
  146. package/dest/util/encoding.d.ts.map +0 -1
  147. package/dest/util/expected_failure_error.d.ts.map +0 -1
  148. package/dest/util/txe_account_store.d.ts.map +0 -1
  149. package/dest/util/txe_contract_store.d.ts +0 -12
  150. package/dest/util/txe_contract_store.d.ts.map +0 -1
  151. package/dest/util/txe_contract_store.js +0 -22
  152. package/dest/util/txe_public_contract_data_source.d.ts +0 -21
  153. package/dest/util/txe_public_contract_data_source.d.ts.map +0 -1
  154. package/src/util/txe_contract_store.ts +0 -36
  155. /package/dest/{util → utils}/expected_failure_error.js +0 -0
  156. /package/dest/{util → utils}/txe_account_store.js +0 -0
  157. /package/src/{util → utils}/expected_failure_error.ts +0 -0
  158. /package/src/{util → utils}/txe_account_store.ts +0 -0
@@ -2,11 +2,14 @@ import { BlockNumber } from '@aztec/foundation/branded-types';
2
2
  import { Fr } from '@aztec/foundation/curves/bn254';
3
3
  import { type Logger, createLogger } from '@aztec/foundation/log';
4
4
  import { KeyStore } from '@aztec/key-store';
5
- import { openTmpStore } from '@aztec/kv-store/lmdb-v2';
6
- import type { ProtocolContract } from '@aztec/protocol-contracts';
5
+ import type { AztecAsyncKVStore } from '@aztec/kv-store';
6
+ import { openEphemeralStore } from '@aztec/kv-store/lmdb-v2';
7
7
  import {
8
8
  AddressStore,
9
+ AnchorBlockStore,
10
+ CapsuleService,
9
11
  CapsuleStore,
12
+ ContractStore,
10
13
  JobCoordinator,
11
14
  NoteService,
12
15
  NoteStore,
@@ -19,10 +22,11 @@ import {
19
22
  ExecutionNoteCache,
20
23
  ExecutionTaggingIndexCache,
21
24
  HashedValuesCache,
25
+ type IMiscOracle,
22
26
  type IPrivateExecutionOracle,
23
27
  type IUtilityExecutionOracle,
28
+ Option,
24
29
  Oracle,
25
- PrivateExecutionOracle,
26
30
  UtilityExecutionOracle,
27
31
  } from '@aztec/pxe/simulator';
28
32
  import {
@@ -33,28 +37,32 @@ import {
33
37
  resolveAssertionMessageFromError,
34
38
  toACVMWitness,
35
39
  } from '@aztec/simulator/client';
40
+ import { STANDARD_AUTH_REGISTRY_ADDRESS } from '@aztec/standard-contracts/auth-registry/constants';
36
41
  import { FunctionCall, FunctionSelector, FunctionType } from '@aztec/stdlib/abi';
37
42
  import type { AuthWitness } from '@aztec/stdlib/auth-witness';
38
43
  import { AztecAddress } from '@aztec/stdlib/aztec-address';
39
- import { GasSettings } from '@aztec/stdlib/gas';
44
+ import type { GasSettings } from '@aztec/stdlib/gas';
40
45
  import { computeProtocolNullifier } from '@aztec/stdlib/hash';
41
46
  import { PrivateContextInputs } from '@aztec/stdlib/kernel';
42
47
  import { makeGlobalVariables } from '@aztec/stdlib/testing';
43
- import { CallContext, GlobalVariables, TxContext } from '@aztec/stdlib/tx';
48
+ import { CallContext, GlobalVariables, OFFCHAIN_MESSAGE_IDENTIFIER, TxContext } from '@aztec/stdlib/tx';
44
49
 
45
50
  import { z } from 'zod';
46
51
 
47
- import { DEFAULT_ADDRESS } from './constants.js';
52
+ import { DEFAULT_ADDRESS, MAX_OFFCHAIN_EFFECTS_PER_TXE_QUERY, MAX_OFFCHAIN_EFFECT_LEN } from './constants.js';
48
53
  import type { IAvmExecutionOracle, ITxeExecutionOracle } from './oracle/interfaces.js';
49
54
  import { TXEOraclePublicContext } from './oracle/txe_oracle_public_context.js';
50
55
  import { TXEOracleTopLevelContext } from './oracle/txe_oracle_top_level_context.js';
56
+ import { TXE_ORACLE_VERSION_MAJOR, TXE_ORACLE_VERSION_MINOR } from './oracle/txe_oracle_version.js';
57
+ import { TXEPrivateExecutionOracle } from './oracle/txe_private_execution_oracle.js';
51
58
  import { RPCTranslator } from './rpc_translator.js';
59
+ import { TXEArchiver } from './state_machine/archiver.js';
52
60
  import { TXEStateMachine } from './state_machine/index.js';
53
- import type { ForeignCallArgs, ForeignCallResult } from './util/encoding.js';
54
- import { TXEAccountStore } from './util/txe_account_store.js';
55
- import { TXEContractStore } from './util/txe_contract_store.js';
56
61
  import { getSingleTxBlockRequestHash, insertTxEffectIntoWorldTrees, makeTXEBlock } from './utils/block_creation.js';
62
+ import type { ForeignCallArgs, ForeignCallResult } from './utils/encoding.js';
57
63
  import { makeTxEffect } from './utils/tx_effect_creation.js';
64
+ import { TXEAccountStore } from './utils/txe_account_store.js';
65
+ import type { TXEArtifactResolver } from './utils/txe_artifact_resolver.js';
58
66
 
59
67
  /**
60
68
  * A TXE Session can be in one of four states, which change as the test progresses and different oracles are called.
@@ -107,10 +115,80 @@ export type TXEOracleFunctionName = Exclude<
107
115
  >;
108
116
 
109
117
  export interface TXESessionStateHandler {
118
+ /** Records the TXE oracle version reported by the Noir test code for diagnostics. */
119
+ setTxeOracleVersion(major: number, minor: number): void;
120
+
110
121
  enterTopLevelState(): Promise<void>;
111
- enterPublicState(contractAddress?: AztecAddress): Promise<void>;
112
- enterPrivateState(contractAddress?: AztecAddress, anchorBlockNumber?: BlockNumber): Promise<PrivateContextInputs>;
113
- enterUtilityState(contractAddress?: AztecAddress): Promise<void>;
122
+ enterPublicState(contractAddress: Option<AztecAddress>): Promise<void>;
123
+ enterPrivateState(
124
+ contractAddress: Option<AztecAddress>,
125
+ anchorBlockNumber: Option<BlockNumber>,
126
+ gasSettings: GasSettings,
127
+ ): Promise<PrivateContextInputs>;
128
+ enterUtilityState(contractAddress: Option<AztecAddress>): Promise<void>;
129
+
130
+ // TODO(F-335): Exposing the job info is abstraction breakage - drop the following 2 functions.
131
+ cycleJob(): Promise<string>;
132
+ getCurrentJob(): string;
133
+
134
+ /**
135
+ * Runs an executor-style top-level call (private/public call, utility execution) with last-call tracking.
136
+ */
137
+ withTopLevelCallTracking<T>(work: () => Promise<{ result: T; txHash?: Fr }>): Promise<T>;
138
+
139
+ /**
140
+ * Captures a raw offchain effect payload for consumption from test environment. Called by the `emit_offchain_effect`
141
+ * oracle handler whenever a contract function emits an offchain message, at any call depth.
142
+ */
143
+ recordOffchainEffect(data: Fr[]): void;
144
+
145
+ /**
146
+ * Returns the raw offchain effect payloads emitted by the last top-level call. Each payload follows the protocol
147
+ * convention documented on `OFFCHAIN_MESSAGE_IDENTIFIER`, i.e. `[identifier, recipient, ...ciphertext]`. Decoding into
148
+ * `OffchainMessage` structs happens on the Noir side of the test helper. Marks the buffer as queried so the
149
+ * unqueried-messages warning doesn't fire on the next reset.
150
+ */
151
+ getLastCallOffchainEffects(): { effects: Fr[][] };
152
+
153
+ /**
154
+ * Returns the context of the last top-level call: its tx hash (`Fr.ZERO` if the call was tx-less) and the anchor
155
+ * block timestamp captured at the start of the call. Does *not* mark the buffer as queried — context reads are
156
+ * metadata, not effect consumption.
157
+ */
158
+ getLastCallContext(): { txHash: Fr; anchorBlockTimestamp: bigint };
159
+ }
160
+
161
+ /**
162
+ * Session state tracking the most recently completed top-level call: the offchain effect buffer it produced, and the
163
+ * call's context (tx hash + anchor block timestamp). The context is refreshed on every top-level call, independently
164
+ * of whether the call produced offchain effects.
165
+ */
166
+ interface LastCallState {
167
+ /**
168
+ * Raw offchain effect payloads emitted by the currently-executing (or most recently completed) top-level call. Wiped
169
+ * at the start of every top-level entry point, appended to on every `emit_offchain_effect` oracle invocation.
170
+ */
171
+ offchainEffects: Fr[][];
172
+ /**
173
+ * Tracks whether the test has queried `effects` since the last reset. If a new top-level call clobbers the buffer
174
+ * without it being queried first, any accumulated messages are lost and we emit a warning so tests don't silently
175
+ * drop delivery.
176
+ */
177
+ queried: boolean;
178
+ /**
179
+ * Tx hash of the most recently completed top-level call, or `Fr.ZERO` if the call was tx-less (context setters,
180
+ * utility execution). Populated by call executor handlers after execution completes.
181
+ */
182
+ txHash: Fr;
183
+ /**
184
+ * Anchor block timestamp of the most recently completed top-level call, captured from the anchor block header that
185
+ * was active when the call started. Populated by call executor handlers after execution completes.
186
+ */
187
+ anchorBlockTimestamp: bigint;
188
+ }
189
+
190
+ function emptyLastCallState(): LastCallState {
191
+ return { offchainEffects: [], queried: false, txHash: Fr.ZERO, anchorBlockTimestamp: 0n };
114
192
  }
115
193
 
116
194
  /**
@@ -120,16 +198,22 @@ export interface TXESessionStateHandler {
120
198
  export class TXESession implements TXESessionStateHandler {
121
199
  private state: SessionState = { name: 'TOP_LEVEL' };
122
200
  private authwits: Map<string, AuthWitness> = new Map();
201
+ private lastCallInfo: LastCallState = emptyLastCallState();
202
+ private txeOracleVersion: { major: number; minor: number } | undefined;
203
+
204
+ private disposed = false;
123
205
 
124
206
  constructor(
125
207
  private logger: Logger,
208
+ private sessionStore: AztecAsyncKVStore,
126
209
  private stateMachine: TXEStateMachine,
127
210
  private oracleHandler:
211
+ | IMiscOracle
128
212
  | IUtilityExecutionOracle
129
213
  | IPrivateExecutionOracle
130
214
  | IAvmExecutionOracle
131
215
  | ITxeExecutionOracle,
132
- private contractStore: TXEContractStore,
216
+ private contractStore: ContractStore,
133
217
  private noteStore: NoteStore,
134
218
  private keyStore: KeyStore,
135
219
  private addressStore: AddressStore,
@@ -144,14 +228,46 @@ export class TXESession implements TXESessionStateHandler {
144
228
  private chainId: Fr,
145
229
  private version: Fr,
146
230
  private nextBlockTimestamp: bigint,
231
+ private readonly artifactResolver: TXEArtifactResolver,
232
+ private readonly rootPath: string,
233
+ private readonly packageName: string,
147
234
  ) {}
148
235
 
149
- static async init(protocolContracts: ProtocolContract[]) {
150
- const store = await openTmpStore('txe-session');
236
+ /**
237
+ * Closes the per-session `txe-session` LMDB and the `NativeWorldStateService` .
238
+ * Called via IPC when the dispatcher detects the end of a test. Idempotent.
239
+ */
240
+ async dispose(): Promise<void> {
241
+ if (this.disposed) {
242
+ return;
243
+ }
244
+ this.disposed = true;
245
+ try {
246
+ await this.stateMachine.synchronizer.nativeWorldStateService.close();
247
+ } catch (err) {
248
+ this.logger.warn(`Error closing native world state during session dispose`, err);
249
+ }
250
+ try {
251
+ await this.sessionStore.close();
252
+ } catch (err) {
253
+ this.logger.warn(`Error closing session LMDB during dispose`, err);
254
+ }
255
+ }
256
+
257
+ static async init(
258
+ contractStore: ContractStore,
259
+ artifactResolver: TXEArtifactResolver,
260
+ rootPath: string,
261
+ packageName: string,
262
+ ) {
263
+ // Size LMDB's reader slots to the libuv pool (capped to 2 in bin/index.ts via
264
+ // HARDWARE_CONCURRENCY): each native LMDB read needs a libuv worker thread to run, so any
265
+ // slot beyond the pool size would sit idle while still consuming a semaphore + reader-table
266
+ // entry per session.
267
+ const store = await openEphemeralStore('txe-session', undefined, 2);
151
268
 
152
269
  const addressStore = new AddressStore(store);
153
270
  const privateEventStore = new PrivateEventStore(store);
154
- const contractStore = new TXEContractStore(store);
155
271
  const noteStore = new NoteStore(store);
156
272
  const senderTaggingStore = new SenderTaggingStore(store);
157
273
  const recipientTaggingStore = new RecipientTaggingStore(store);
@@ -160,7 +276,6 @@ export class TXESession implements TXESessionStateHandler {
160
276
  const keyStore = new KeyStore(store);
161
277
  const accountStore = new TXEAccountStore(store);
162
278
 
163
- // Create job coordinator and register staged stores
164
279
  const jobCoordinator = new JobCoordinator(store);
165
280
  jobCoordinator.registerStores([
166
281
  capsuleStore,
@@ -170,13 +285,9 @@ export class TXESession implements TXESessionStateHandler {
170
285
  noteStore,
171
286
  ]);
172
287
 
173
- // Register protocol contracts.
174
- for (const { contractClass, instance, artifact } of protocolContracts) {
175
- await contractStore.addContractArtifact(contractClass.id, artifact);
176
- await contractStore.addContractInstance(instance);
177
- }
178
-
179
- const stateMachine = await TXEStateMachine.create(store);
288
+ const archiver = new TXEArchiver(store);
289
+ const anchorBlockStore = new AnchorBlockStore(store);
290
+ const stateMachine = await TXEStateMachine.create(archiver, anchorBlockStore, contractStore, noteStore);
180
291
 
181
292
  const nextBlockTimestamp = BigInt(Math.floor(new Date().getTime() / 1000));
182
293
  const version = new Fr(await stateMachine.node.getVersion());
@@ -184,6 +295,8 @@ export class TXESession implements TXESessionStateHandler {
184
295
 
185
296
  const initialJobId = jobCoordinator.beginJob();
186
297
 
298
+ const logger = createLogger('txe:session');
299
+
187
300
  const topLevelOracleHandler = new TXEOracleTopLevelContext(
188
301
  stateMachine,
189
302
  contractStore,
@@ -196,16 +309,20 @@ export class TXESession implements TXESessionStateHandler {
196
309
  senderAddressBookStore,
197
310
  capsuleStore,
198
311
  privateEventStore,
199
- initialJobId,
200
312
  nextBlockTimestamp,
201
313
  version,
202
314
  chainId,
203
315
  new Map(),
316
+ artifactResolver,
317
+ rootPath,
318
+ packageName,
204
319
  );
205
- await topLevelOracleHandler.txeAdvanceBlocksBy(1);
320
+
321
+ await topLevelOracleHandler.mineDeploymentNullifiers([STANDARD_AUTH_REGISTRY_ADDRESS]);
206
322
 
207
323
  return new TXESession(
208
- createLogger('txe:session'),
324
+ logger,
325
+ store,
209
326
  stateMachine,
210
327
  topLevelOracleHandler,
211
328
  contractStore,
@@ -223,6 +340,9 @@ export class TXESession implements TXESessionStateHandler {
223
340
  version,
224
341
  chainId,
225
342
  nextBlockTimestamp,
343
+ artifactResolver,
344
+ rootPath,
345
+ packageName,
226
346
  );
227
347
  }
228
348
 
@@ -244,7 +364,28 @@ export class TXESession implements TXESessionStateHandler {
244
364
  return translator[validatedFunctionName](...inputs);
245
365
  } catch (error) {
246
366
  if (error instanceof z.ZodError) {
247
- throw new Error(`${functionName} does not correspond to any oracle handler available on RPCTranslator`);
367
+ let versionHint: string;
368
+ if (!this.txeOracleVersion) {
369
+ versionHint =
370
+ ' The test appears to use an older version of Aztec.nr that does not' +
371
+ ' support test environment oracle versioning. Update Aztec.nr to a compatible version.' +
372
+ ' See https://docs.aztec.network/errors/12';
373
+ } else if (this.txeOracleVersion.minor > TXE_ORACLE_VERSION_MINOR) {
374
+ versionHint =
375
+ ` The test uses Aztec.nr test oracle version` +
376
+ ` ${this.txeOracleVersion.major}.${this.txeOracleVersion.minor}, but this test environment` +
377
+ ` only supports up to ${TXE_ORACLE_VERSION_MAJOR}.${TXE_ORACLE_VERSION_MINOR}.` +
378
+ ` Upgrade the Aztec CLI to a compatible version.` +
379
+ ` See https://docs.aztec.network/errors/12`;
380
+ } else {
381
+ versionHint =
382
+ ` The test's oracle version (${this.txeOracleVersion.major}.${this.txeOracleVersion.minor})` +
383
+ ` is compatible with this test environment` +
384
+ ` (${TXE_ORACLE_VERSION_MAJOR}.${TXE_ORACLE_VERSION_MINOR}), so this oracle should be` +
385
+ ` available. This is an unexpected error, please report it.` +
386
+ ` See https://docs.aztec.network/errors/13`;
387
+ }
388
+ throw new Error(`Unknown oracle '${functionName}'.${versionHint}`);
248
389
  } else if (error instanceof Error) {
249
390
  throw new Error(
250
391
  `Execution error while processing function ${functionName} in state ${this.state.name}: ${error.message}`,
@@ -257,6 +398,86 @@ export class TXESession implements TXESessionStateHandler {
257
398
  }
258
399
  }
259
400
 
401
+ getCurrentJob(): string {
402
+ return this.currentJobId;
403
+ }
404
+
405
+ /** Commits the current job and begins a new one. Returns the new job ID. */
406
+ async cycleJob(): Promise<string> {
407
+ await this.jobCoordinator.commitJob(this.currentJobId);
408
+ this.currentJobId = this.jobCoordinator.beginJob();
409
+ return this.currentJobId;
410
+ }
411
+
412
+ private resetLastCall(): void {
413
+ const notQueriedMessageCount = this.lastCallInfo.queried
414
+ ? 0
415
+ : this.lastCallInfo.offchainEffects.filter(payload => payload[0]?.equals(OFFCHAIN_MESSAGE_IDENTIFIER)).length;
416
+ if (notQueriedMessageCount > 0) {
417
+ this.logger.warn(
418
+ `Dropping ${notQueriedMessageCount} unqueried offchain message(s) from the previous top-level call. ` +
419
+ `To deliver them, call \`env.offchain_messages()\` and forward the result to the recipient contract's ` +
420
+ `\`offchain_receive\` utility before issuing another top-level call. To intentionally discard, assign ` +
421
+ `to \`let _ = env.offchain_messages()\` to silence this warning.`,
422
+ );
423
+ }
424
+ this.lastCallInfo = emptyLastCallState();
425
+ }
426
+
427
+ recordOffchainEffect(data: Fr[]): void {
428
+ this.lastCallInfo.offchainEffects.push(data);
429
+ }
430
+
431
+ private setLastCallContext(txHash: Fr, anchorBlockTimestamp: bigint): void {
432
+ this.lastCallInfo.txHash = txHash;
433
+ this.lastCallInfo.anchorBlockTimestamp = anchorBlockTimestamp;
434
+ }
435
+
436
+ async withTopLevelCallTracking<T>(work: () => Promise<{ result: T; txHash?: Fr }>): Promise<T> {
437
+ this.resetLastCall();
438
+ // Capture the anchor *before* `work` runs: private/public executor calls mine a new block as a
439
+ // side effect, and that block's timestamp should not be attributed to this call's anchor.
440
+ const anchorBlockTimestamp = (await this.stateMachine.node.getBlockData('latest'))!.header.globalVariables
441
+ .timestamp;
442
+ const { result, txHash } = await work();
443
+ this.setLastCallContext(txHash ?? Fr.ZERO, anchorBlockTimestamp);
444
+ return result;
445
+ }
446
+
447
+ getLastCallOffchainEffects(): { effects: Fr[][] } {
448
+ this.lastCallInfo.queried = true;
449
+ const effects = this.lastCallInfo.offchainEffects;
450
+
451
+ if (effects.length > MAX_OFFCHAIN_EFFECTS_PER_TXE_QUERY) {
452
+ throw new Error(`${effects.length} offchain effects exceed max ${MAX_OFFCHAIN_EFFECTS_PER_TXE_QUERY}`);
453
+ }
454
+ if (effects.some(e => e.length > MAX_OFFCHAIN_EFFECT_LEN)) {
455
+ throw new Error(`Some offchain effect has length larger than max ${MAX_OFFCHAIN_EFFECT_LEN}`);
456
+ }
457
+
458
+ return { effects };
459
+ }
460
+
461
+ getLastCallContext(): { txHash: Fr; anchorBlockTimestamp: bigint } {
462
+ const { txHash, anchorBlockTimestamp } = this.lastCallInfo;
463
+ return { txHash, anchorBlockTimestamp };
464
+ }
465
+
466
+ setTxeOracleVersion(major: number, minor: number): void {
467
+ if (major !== TXE_ORACLE_VERSION_MAJOR) {
468
+ const hint =
469
+ major > TXE_ORACLE_VERSION_MAJOR
470
+ ? 'The test was compiled with a newer version of Aztec.nr than your test environment supports. Upgrade your test environment to a compatible version.'
471
+ : 'The test was compiled with an older version of Aztec.nr than your test environment supports. Recompile the test with a compatible version of Aztec.nr.';
472
+ throw new Error(
473
+ `Incompatible test environment version: ${hint} See https://docs.aztec.network/errors/12 (expected test oracle major version ${TXE_ORACLE_VERSION_MAJOR}, got ${major})`,
474
+ );
475
+ }
476
+
477
+ this.txeOracleVersion = { major, minor };
478
+ this.logger.debug(`Test compiled with test oracle version ${major}.${minor}`);
479
+ }
480
+
260
481
  async enterTopLevelState() {
261
482
  switch (this.state.name) {
262
483
  case 'PRIVATE': {
@@ -280,8 +501,7 @@ export class TXESession implements TXESessionStateHandler {
280
501
  }
281
502
 
282
503
  // Commit all staged stores from the job that was just completed, then begin a new job
283
- await this.jobCoordinator.commitJob(this.currentJobId);
284
- this.currentJobId = this.jobCoordinator.beginJob();
504
+ await this.cycleJob();
285
505
 
286
506
  this.oracleHandler = new TXEOracleTopLevelContext(
287
507
  this.stateMachine,
@@ -295,11 +515,13 @@ export class TXESession implements TXESessionStateHandler {
295
515
  this.senderAddressBookStore,
296
516
  this.capsuleStore,
297
517
  this.privateEventStore,
298
- this.currentJobId,
299
518
  this.nextBlockTimestamp,
300
519
  this.version,
301
520
  this.chainId,
302
521
  this.authwits,
522
+ this.artifactResolver,
523
+ this.rootPath,
524
+ this.packageName,
303
525
  );
304
526
 
305
527
  this.state = { name: 'TOP_LEVEL' };
@@ -307,20 +529,25 @@ export class TXESession implements TXESessionStateHandler {
307
529
  }
308
530
 
309
531
  async enterPrivateState(
310
- contractAddress: AztecAddress = DEFAULT_ADDRESS,
311
- anchorBlockNumber?: BlockNumber,
532
+ contractAddressOpt: Option<AztecAddress>,
533
+ anchorBlockNumberOpt: Option<BlockNumber>,
534
+ gasSettings: GasSettings,
312
535
  ): Promise<PrivateContextInputs> {
536
+ const contractAddress = contractAddressOpt?.value ?? DEFAULT_ADDRESS;
537
+ const anchorBlockNumber = anchorBlockNumberOpt?.value;
313
538
  this.exitTopLevelState();
539
+ this.resetLastCall();
314
540
 
315
541
  // Private execution has two associated block numbers: the anchor block (i.e. the historical block that is used to
316
542
  // build the proof), and the *next* block, i.e. the one we'll create once the execution ends, and which will contain
317
543
  // a single transaction with the effects of what was done in the test.
318
- const anchorBlock = await this.stateMachine.node.getBlockHeader(anchorBlockNumber ?? 'latest');
544
+ const anchorBlock = await this.stateMachine.node.getBlock(anchorBlockNumber ?? 'latest').then(b => b?.header);
319
545
 
320
546
  await new NoteService(this.noteStore, this.stateMachine.node, anchorBlock!, this.currentJobId).syncNoteNullifiers(
321
547
  contractAddress,
548
+ await this.keyStore.getAccounts(),
322
549
  );
323
- const latestBlock = await this.stateMachine.node.getBlockHeader('latest');
550
+ const latestBlock = await this.stateMachine.node.getBlock('latest').then(b => b?.header);
324
551
 
325
552
  const nextBlockGlobalVariables = makeGlobalVariables(undefined, {
326
553
  blockNumber: BlockNumber(latestBlock!.globalVariables.blockNumber + 1),
@@ -335,29 +562,34 @@ export class TXESession implements TXESessionStateHandler {
335
562
  const taggingIndexCache = new ExecutionTaggingIndexCache();
336
563
 
337
564
  const utilityExecutor = this.utilityExecutorForContractSync(anchorBlock);
338
- this.oracleHandler = new PrivateExecutionOracle(
339
- Fr.ZERO,
340
- new TxContext(this.chainId, this.version, GasSettings.empty()),
341
- new CallContext(AztecAddress.ZERO, contractAddress, FunctionSelector.empty(), false),
342
- anchorBlock!,
565
+ this.oracleHandler = new TXEPrivateExecutionOracle({
566
+ argsHash: Fr.ZERO,
567
+ txContext: new TxContext(this.chainId, this.version, gasSettings),
568
+ callContext: new CallContext(AztecAddress.ZERO, contractAddress, FunctionSelector.empty(), false),
569
+ anchorBlockHeader: anchorBlock!,
343
570
  utilityExecutor,
344
- [],
345
- [],
346
- new HashedValuesCache(),
571
+ authWitnesses: [],
572
+ capsules: [],
573
+ executionCache: new HashedValuesCache(),
347
574
  noteCache,
348
575
  taggingIndexCache,
349
- this.contractStore,
350
- this.noteStore,
351
- this.keyStore,
352
- this.addressStore,
353
- this.stateMachine.node,
354
- this.senderTaggingStore,
355
- this.recipientTaggingStore,
356
- this.senderAddressBookStore,
357
- this.capsuleStore,
358
- this.privateEventStore,
359
- this.currentJobId,
360
- );
576
+ contractStore: this.contractStore,
577
+ noteStore: this.noteStore,
578
+ keyStore: this.keyStore,
579
+ addressStore: this.addressStore,
580
+ aztecNode: this.stateMachine.node,
581
+ senderTaggingStore: this.senderTaggingStore,
582
+ recipientTaggingStore: this.recipientTaggingStore,
583
+ senderAddressBookStore: this.senderAddressBookStore,
584
+ capsuleService: new CapsuleService(this.capsuleStore, await this.keyStore.getAccounts()),
585
+ privateEventStore: this.privateEventStore,
586
+ contractSyncService: this.stateMachine.contractSyncService,
587
+ l2TipsStore: this.stateMachine.l2TipsProvider,
588
+ jobId: this.currentJobId,
589
+ scopes: await this.keyStore.getAccounts(),
590
+ messageContextService: this.stateMachine.messageContextService,
591
+ simulator: new WASMSimulator(),
592
+ });
361
593
 
362
594
  // We store the note and tagging index caches fed into the PrivateExecutionOracle (along with some other auxiliary
363
595
  // data) in order to refer to it later, mimicking the way this object is used by the ContractFunctionSimulator. The
@@ -367,35 +599,47 @@ export class TXESession implements TXESessionStateHandler {
367
599
  this.state = { name: 'PRIVATE', nextBlockGlobalVariables, noteCache, taggingIndexCache };
368
600
  this.logger.debug(`Entered state ${this.state.name}`);
369
601
 
370
- return (this.oracleHandler as PrivateExecutionOracle).getPrivateContextInputs();
602
+ // Record the *resolved* anchor's timestamp — if the caller pinned the anchor to a past block
603
+ // via `anchorBlockNumber`, "latest" would be the wrong anchor for offchain-message semantics.
604
+ this.setLastCallContext(Fr.ZERO, anchorBlock!.globalVariables.timestamp);
605
+
606
+ return (this.oracleHandler as TXEPrivateExecutionOracle).getPrivateContextInputs();
371
607
  }
372
608
 
373
- async enterPublicState(contractAddress?: AztecAddress) {
609
+ async enterPublicState(contractAddressOpt: Option<AztecAddress>) {
610
+ const contractAddress = contractAddressOpt?.value ?? DEFAULT_ADDRESS;
374
611
  this.exitTopLevelState();
612
+ this.resetLastCall();
375
613
 
376
614
  // The PublicContext will create a block with a single transaction in it, containing the effects of what was done in
377
615
  // the test. The block therefore gets the *next* block number and timestamp.
378
- const latestBlockNumber = (await this.stateMachine.node.getBlockHeader('latest'))!.globalVariables.blockNumber;
616
+ const latestHeader = (await this.stateMachine.node.getBlockData('latest'))!.header;
379
617
  const globalVariables = makeGlobalVariables(undefined, {
380
- blockNumber: BlockNumber(latestBlockNumber + 1),
618
+ blockNumber: BlockNumber(latestHeader.globalVariables.blockNumber + 1),
381
619
  timestamp: this.nextBlockTimestamp,
382
620
  version: this.version,
383
621
  chainId: this.chainId,
384
622
  });
385
623
 
386
624
  this.oracleHandler = new TXEOraclePublicContext(
387
- contractAddress ?? DEFAULT_ADDRESS,
625
+ contractAddress,
388
626
  await this.stateMachine.synchronizer.nativeWorldStateService.fork(),
389
627
  getSingleTxBlockRequestHash(globalVariables.blockNumber),
390
628
  globalVariables,
629
+ this.contractStore,
391
630
  );
392
631
 
393
632
  this.state = { name: 'PUBLIC' };
394
633
  this.logger.debug(`Entered state ${this.state.name}`);
634
+
635
+ // Public state is anchored at the latest block.
636
+ this.setLastCallContext(Fr.ZERO, latestHeader.globalVariables.timestamp);
395
637
  }
396
638
 
397
- async enterUtilityState(contractAddress: AztecAddress = DEFAULT_ADDRESS) {
639
+ async enterUtilityState(contractAddressOpt: Option<AztecAddress>) {
640
+ const contractAddress = contractAddressOpt?.value ?? DEFAULT_ADDRESS;
398
641
  this.exitTopLevelState();
642
+ this.resetLastCall();
399
643
 
400
644
  const anchorBlockHeader = await this.stateMachine.anchorBlockStore.getBlockHeader();
401
645
 
@@ -409,27 +653,36 @@ export class TXESession implements TXESessionStateHandler {
409
653
  this.stateMachine.node,
410
654
  anchorBlockHeader,
411
655
  this.currentJobId,
412
- ).syncNoteNullifiers(contractAddress);
656
+ ).syncNoteNullifiers(contractAddress, await this.keyStore.getAccounts());
413
657
 
414
- this.oracleHandler = new UtilityExecutionOracle(
658
+ this.oracleHandler = new UtilityExecutionOracle({
415
659
  contractAddress,
416
- [],
417
- [],
660
+ authWitnesses: [],
661
+ capsules: [],
418
662
  anchorBlockHeader,
419
- this.contractStore,
420
- this.noteStore,
421
- this.keyStore,
422
- this.addressStore,
423
- this.stateMachine.node,
424
- this.recipientTaggingStore,
425
- this.senderAddressBookStore,
426
- this.capsuleStore,
427
- this.privateEventStore,
428
- this.currentJobId,
429
- );
663
+ contractStore: this.contractStore,
664
+ noteStore: this.noteStore,
665
+ keyStore: this.keyStore,
666
+ addressStore: this.addressStore,
667
+ aztecNode: this.stateMachine.node,
668
+ recipientTaggingStore: this.recipientTaggingStore,
669
+ senderAddressBookStore: this.senderAddressBookStore,
670
+ capsuleService: new CapsuleService(this.capsuleStore, await this.keyStore.getAccounts()),
671
+ privateEventStore: this.privateEventStore,
672
+ messageContextService: this.stateMachine.messageContextService,
673
+ contractSyncService: this.stateMachine.contractSyncService,
674
+ l2TipsStore: this.stateMachine.l2TipsProvider,
675
+ jobId: this.currentJobId,
676
+ scopes: await this.keyStore.getAccounts(),
677
+ simulator: new WASMSimulator(),
678
+ utilityExecutor: this.utilityExecutorForContractSync(anchorBlockHeader),
679
+ });
430
680
 
431
681
  this.state = { name: 'UTILITY' };
432
682
  this.logger.debug(`Entered state ${this.state.name}`);
683
+
684
+ // Utility state anchors at whatever the anchor block store is pointing to (tracked as latest).
685
+ this.setLastCallContext(Fr.ZERO, anchorBlockHeader.globalVariables.timestamp);
433
686
  }
434
687
 
435
688
  private exitTopLevelState() {
@@ -439,8 +692,8 @@ export class TXESession implements TXESessionStateHandler {
439
692
 
440
693
  // Note that while all public and private contexts do is build a single block that we then process when exiting
441
694
  // those, the top level context performs a large number of actions not captured in the following 'close' call. Among
442
- // others, it will create empty blocks (via `txeAdvanceBlocksBy` and `deploy`), create blocks with transactions via
443
- // `txePrivateCallNewFlow` and `txePublicCallNewFlow`, add accounts to PXE via `txeAddAccount`, etc. This is a
695
+ // others, it will create empty blocks (via `advanceBlocksBy` and `deploy`), create blocks with transactions via
696
+ // `privateCallNewFlow` and `publicCallNewFlow`, add accounts to PXE via `addAccount`, etc. This is a
444
697
  // slight inconsistency in the working model of this class, but is not too bad.
445
698
  // TODO: it's quite unfortunate that we need to capture the authwits created to later pass them again when the top
446
699
  // level context is re-created. This is because authwits create a temporary utility context that'd otherwise reset
@@ -494,30 +747,37 @@ export class TXESession implements TXESessionStateHandler {
494
747
  }
495
748
 
496
749
  private utilityExecutorForContractSync(anchorBlock: any) {
497
- return async (call: FunctionCall) => {
750
+ return async (call: FunctionCall, scopes: AztecAddress[]) => {
498
751
  const entryPointArtifact = await this.contractStore.getFunctionArtifactWithDebugMetadata(call.to, call.selector);
499
752
  if (entryPointArtifact.functionType !== FunctionType.UTILITY) {
500
753
  throw new Error(`Cannot run ${entryPointArtifact.functionType} function as utility`);
501
754
  }
502
755
 
503
756
  try {
504
- const oracle = new UtilityExecutionOracle(
505
- call.to,
506
- [],
507
- [],
508
- anchorBlock!,
509
- this.contractStore,
510
- this.noteStore,
511
- this.keyStore,
512
- this.addressStore,
513
- this.stateMachine.node,
514
- this.recipientTaggingStore,
515
- this.senderAddressBookStore,
516
- this.capsuleStore,
517
- this.privateEventStore,
518
- this.currentJobId,
519
- );
520
- await new WASMSimulator()
757
+ const simulator = new WASMSimulator();
758
+ const oracle = new UtilityExecutionOracle({
759
+ contractAddress: call.to,
760
+ authWitnesses: [],
761
+ capsules: [],
762
+ anchorBlockHeader: anchorBlock!,
763
+ contractStore: this.contractStore,
764
+ noteStore: this.noteStore,
765
+ keyStore: this.keyStore,
766
+ addressStore: this.addressStore,
767
+ aztecNode: this.stateMachine.node,
768
+ recipientTaggingStore: this.recipientTaggingStore,
769
+ senderAddressBookStore: this.senderAddressBookStore,
770
+ capsuleService: new CapsuleService(this.capsuleStore, scopes),
771
+ privateEventStore: this.privateEventStore,
772
+ messageContextService: this.stateMachine.messageContextService,
773
+ contractSyncService: this.stateMachine.contractSyncService,
774
+ l2TipsStore: this.stateMachine.l2TipsProvider,
775
+ jobId: this.currentJobId,
776
+ scopes,
777
+ simulator,
778
+ utilityExecutor: this.utilityExecutorForContractSync(anchorBlock),
779
+ });
780
+ await simulator
521
781
  .executeUserCircuit(toACVMWitness(0, call.args), entryPointArtifact, new Oracle(oracle).toACIRCallback())
522
782
  .catch((err: Error) => {
523
783
  err.message = resolveAssertionMessageFromError(err, entryPointArtifact);