@aztec/txe 0.0.1-commit.fce3e4f → 0.0.1-commit.ffe5b04ea

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 (78) hide show
  1. package/dest/constants.d.ts +3 -0
  2. package/dest/constants.d.ts.map +1 -0
  3. package/dest/constants.js +2 -0
  4. package/dest/index.d.ts +1 -1
  5. package/dest/index.d.ts.map +1 -1
  6. package/dest/index.js +91 -56
  7. package/dest/oracle/interfaces.d.ts +33 -29
  8. package/dest/oracle/interfaces.d.ts.map +1 -1
  9. package/dest/oracle/txe_oracle_public_context.d.ts +16 -16
  10. package/dest/oracle/txe_oracle_public_context.d.ts.map +1 -1
  11. package/dest/oracle/txe_oracle_public_context.js +20 -22
  12. package/dest/oracle/txe_oracle_top_level_context.d.ts +36 -27
  13. package/dest/oracle/txe_oracle_top_level_context.d.ts.map +1 -1
  14. package/dest/oracle/txe_oracle_top_level_context.js +206 -98
  15. package/dest/rpc_translator.d.ts +91 -79
  16. package/dest/rpc_translator.d.ts.map +1 -1
  17. package/dest/rpc_translator.js +330 -170
  18. package/dest/state_machine/archiver.d.ts +21 -52
  19. package/dest/state_machine/archiver.d.ts.map +1 -1
  20. package/dest/state_machine/archiver.js +63 -94
  21. package/dest/state_machine/dummy_p2p_client.d.ts +20 -15
  22. package/dest/state_machine/dummy_p2p_client.d.ts.map +1 -1
  23. package/dest/state_machine/dummy_p2p_client.js +42 -25
  24. package/dest/state_machine/global_variable_builder.d.ts +6 -5
  25. package/dest/state_machine/global_variable_builder.d.ts.map +1 -1
  26. package/dest/state_machine/global_variable_builder.js +13 -1
  27. package/dest/state_machine/index.d.ts +7 -7
  28. package/dest/state_machine/index.d.ts.map +1 -1
  29. package/dest/state_machine/index.js +40 -23
  30. package/dest/state_machine/mock_epoch_cache.d.ts +9 -6
  31. package/dest/state_machine/mock_epoch_cache.d.ts.map +1 -1
  32. package/dest/state_machine/mock_epoch_cache.js +14 -7
  33. package/dest/state_machine/synchronizer.d.ts +3 -2
  34. package/dest/state_machine/synchronizer.d.ts.map +1 -1
  35. package/dest/state_machine/synchronizer.js +5 -4
  36. package/dest/txe_session.d.ts +26 -15
  37. package/dest/txe_session.d.ts.map +1 -1
  38. package/dest/txe_session.js +161 -55
  39. package/dest/util/encoding.d.ts +618 -19
  40. package/dest/util/encoding.d.ts.map +1 -1
  41. package/dest/util/encoding.js +1 -1
  42. package/dest/util/txe_account_store.d.ts +10 -0
  43. package/dest/util/txe_account_store.d.ts.map +1 -0
  44. package/dest/util/{txe_account_data_provider.js → txe_account_store.js} +1 -1
  45. package/dest/util/txe_public_contract_data_source.d.ts +8 -8
  46. package/dest/util/txe_public_contract_data_source.d.ts.map +1 -1
  47. package/dest/util/txe_public_contract_data_source.js +12 -29
  48. package/dest/utils/block_creation.d.ts +21 -6
  49. package/dest/utils/block_creation.d.ts.map +1 -1
  50. package/dest/utils/block_creation.js +38 -4
  51. package/dest/utils/tx_effect_creation.d.ts +3 -3
  52. package/dest/utils/tx_effect_creation.d.ts.map +1 -1
  53. package/dest/utils/tx_effect_creation.js +4 -7
  54. package/package.json +18 -18
  55. package/src/constants.ts +3 -0
  56. package/src/index.ts +103 -63
  57. package/src/oracle/interfaces.ts +36 -32
  58. package/src/oracle/txe_oracle_public_context.ts +21 -28
  59. package/src/oracle/txe_oracle_top_level_context.ts +251 -138
  60. package/src/rpc_translator.ts +368 -174
  61. package/src/state_machine/archiver.ts +59 -114
  62. package/src/state_machine/dummy_p2p_client.ts +58 -33
  63. package/src/state_machine/global_variable_builder.ts +21 -4
  64. package/src/state_machine/index.ts +60 -21
  65. package/src/state_machine/mock_epoch_cache.ts +15 -11
  66. package/src/state_machine/synchronizer.ts +6 -5
  67. package/src/txe_session.ts +222 -103
  68. package/src/util/encoding.ts +1 -1
  69. package/src/util/{txe_account_data_provider.ts → txe_account_store.ts} +1 -1
  70. package/src/util/txe_public_contract_data_source.ts +20 -45
  71. package/src/utils/block_creation.ts +49 -15
  72. package/src/utils/tx_effect_creation.ts +5 -12
  73. package/dest/util/txe_account_data_provider.d.ts +0 -10
  74. package/dest/util/txe_account_data_provider.d.ts.map +0 -1
  75. package/dest/util/txe_contract_data_provider.d.ts +0 -12
  76. package/dest/util/txe_contract_data_provider.d.ts.map +0 -1
  77. package/dest/util/txe_contract_data_provider.js +0 -22
  78. package/src/util/txe_contract_data_provider.ts +0 -36
@@ -1,35 +1,41 @@
1
1
  import { CONTRACT_INSTANCE_REGISTRY_CONTRACT_ADDRESS, DEFAULT_DA_GAS_LIMIT, DEFAULT_L2_GAS_LIMIT, DEFAULT_TEARDOWN_DA_GAS_LIMIT, DEFAULT_TEARDOWN_L2_GAS_LIMIT, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP } from '@aztec/constants';
2
- import { Schnorr } from '@aztec/foundation/crypto';
3
- import { Fr } from '@aztec/foundation/fields';
2
+ import { BlockNumber } from '@aztec/foundation/branded-types';
3
+ import { Schnorr } from '@aztec/foundation/crypto/schnorr';
4
+ import { Fr } from '@aztec/foundation/curves/bn254';
4
5
  import { LogLevels, applyStringFormatting, createLogger } from '@aztec/foundation/log';
5
6
  import { TestDateProvider } from '@aztec/foundation/timer';
6
7
  import { ORACLE_VERSION, enrichPublicSimulationError } from '@aztec/pxe/server';
7
8
  import { ExecutionNoteCache, ExecutionTaggingIndexCache, HashedValuesCache, Oracle, PrivateExecutionOracle, UtilityExecutionOracle, executePrivateFunction, generateSimulatedProvingResult } from '@aztec/pxe/simulator';
8
9
  import { ExecutionError, WASMSimulator, createSimulationError, extractCallStack, resolveAssertionMessageFromError, toACVMWitness, witnessMapToFields } from '@aztec/simulator/client';
9
- import { GuardedMerkleTreeOperations, PublicContractsDB, PublicProcessor, PublicTxSimulator } from '@aztec/simulator/server';
10
- import { FunctionSelector, FunctionType } from '@aztec/stdlib/abi';
10
+ import { CppPublicTxSimulator, GuardedMerkleTreeOperations, PublicContractsDB, PublicProcessor } from '@aztec/simulator/server';
11
+ import { FunctionCall, FunctionSelector, FunctionType } from '@aztec/stdlib/abi';
11
12
  import { AuthWitness } from '@aztec/stdlib/auth-witness';
12
13
  import { PublicSimulatorConfig } from '@aztec/stdlib/avm';
13
14
  import { AztecAddress } from '@aztec/stdlib/aztec-address';
14
- import { Body, L2Block } from '@aztec/stdlib/block';
15
15
  import { computePartialAddress } from '@aztec/stdlib/contract';
16
16
  import { Gas, GasFees, GasSettings } from '@aztec/stdlib/gas';
17
17
  import { computeCalldataHash, computeProtocolNullifier, siloNullifier } from '@aztec/stdlib/hash';
18
18
  import { PartialPrivateTailPublicInputsForPublic, PrivateKernelTailCircuitPublicInputs, PrivateToPublicAccumulatedData, PublicCallRequest } from '@aztec/stdlib/kernel';
19
19
  import { ChonkProof } from '@aztec/stdlib/proofs';
20
- import { makeAppendOnlyTreeSnapshot, makeGlobalVariables } from '@aztec/stdlib/testing';
20
+ import { makeGlobalVariables } from '@aztec/stdlib/testing';
21
21
  import { MerkleTreeId } from '@aztec/stdlib/trees';
22
22
  import { CallContext, HashedValues, PrivateExecutionResult, Tx, TxConstantData, TxContext, TxEffect, TxHash, collectNested } from '@aztec/stdlib/tx';
23
23
  import { ForkCheckpoint } from '@aztec/world-state';
24
+ import { DEFAULT_ADDRESS } from '../constants.js';
24
25
  import { TXEPublicContractDataSource } from '../util/txe_public_contract_data_source.js';
25
- import { getSingleTxBlockRequestHash, insertTxEffectIntoWorldTrees, makeTXEBlockHeader } from '../utils/block_creation.js';
26
+ import { getSingleTxBlockRequestHash, insertTxEffectIntoWorldTrees, makeTXEBlock } from '../utils/block_creation.js';
26
27
  export class TXEOracleTopLevelContext {
27
28
  stateMachine;
28
- contractDataProvider;
29
+ contractStore;
30
+ noteStore;
29
31
  keyStore;
30
- addressDataProvider;
31
- accountDataProvider;
32
- pxeOracleInterface;
32
+ addressStore;
33
+ accountStore;
34
+ senderTaggingStore;
35
+ recipientTaggingStore;
36
+ senderAddressBookStore;
37
+ capsuleStore;
38
+ privateEventStore;
33
39
  nextBlockTimestamp;
34
40
  version;
35
41
  chainId;
@@ -37,13 +43,18 @@ export class TXEOracleTopLevelContext {
37
43
  isMisc;
38
44
  isTxe;
39
45
  logger;
40
- constructor(stateMachine, contractDataProvider, keyStore, addressDataProvider, accountDataProvider, pxeOracleInterface, nextBlockTimestamp, version, chainId, authwits){
46
+ constructor(stateMachine, contractStore, noteStore, keyStore, addressStore, accountStore, senderTaggingStore, recipientTaggingStore, senderAddressBookStore, capsuleStore, privateEventStore, nextBlockTimestamp, version, chainId, authwits){
41
47
  this.stateMachine = stateMachine;
42
- this.contractDataProvider = contractDataProvider;
48
+ this.contractStore = contractStore;
49
+ this.noteStore = noteStore;
43
50
  this.keyStore = keyStore;
44
- this.addressDataProvider = addressDataProvider;
45
- this.accountDataProvider = accountDataProvider;
46
- this.pxeOracleInterface = pxeOracleInterface;
51
+ this.addressStore = addressStore;
52
+ this.accountStore = accountStore;
53
+ this.senderTaggingStore = senderTaggingStore;
54
+ this.recipientTaggingStore = recipientTaggingStore;
55
+ this.senderAddressBookStore = senderAddressBookStore;
56
+ this.capsuleStore = capsuleStore;
57
+ this.privateEventStore = privateEventStore;
47
58
  this.nextBlockTimestamp = nextBlockTimestamp;
48
59
  this.version = version;
49
60
  this.chainId = chainId;
@@ -53,37 +64,42 @@ export class TXEOracleTopLevelContext {
53
64
  this.logger = createLogger('txe:top_level_context');
54
65
  this.logger.debug('Entering Top Level Context');
55
66
  }
56
- utilityAssertCompatibleOracleVersion(version) {
67
+ assertCompatibleOracleVersion(version) {
57
68
  if (version !== ORACLE_VERSION) {
58
69
  throw new Error(`Incompatible oracle version. TXE is using version '${ORACLE_VERSION}', but got a request for '${version}'.`);
59
70
  }
60
71
  }
61
72
  // This is typically only invoked in private contexts, but it is convenient to also have it in top-level for testing
62
73
  // setup.
63
- utilityGetRandomField() {
74
+ getRandomField() {
64
75
  return Fr.random();
65
76
  }
66
77
  // We instruct users to debug contracts via this oracle, so it makes sense that they'd expect it to also work in tests
67
- utilityDebugLog(level, message, fields) {
78
+ log(level, message, fields) {
68
79
  if (!LogLevels[level]) {
69
- throw new Error(`Invalid debug log level: ${level}`);
80
+ throw new Error(`Invalid log level: ${level}`);
70
81
  }
71
82
  const levelName = LogLevels[level];
72
83
  this.logger[levelName](`${applyStringFormatting(message, fields)}`, {
73
84
  module: `${this.logger.module}:debug_log`
74
85
  });
86
+ return Promise.resolve();
75
87
  }
76
- async txeGetNextBlockNumber() {
77
- return await this.getLastBlockNumber() + 1;
88
+ getDefaultAddress() {
89
+ return DEFAULT_ADDRESS;
78
90
  }
79
- txeGetNextBlockTimestamp() {
91
+ async getNextBlockNumber() {
92
+ return BlockNumber(await this.getLastBlockNumber() + 1);
93
+ }
94
+ getNextBlockTimestamp() {
80
95
  return Promise.resolve(this.nextBlockTimestamp);
81
96
  }
82
- async txeGetLastBlockTimestamp() {
97
+ async getLastBlockTimestamp() {
83
98
  return (await this.stateMachine.node.getBlockHeader('latest')).globalVariables.timestamp;
84
99
  }
85
- async txeGetLastTxEffects() {
86
- const block = await this.stateMachine.archiver.getBlock('latest');
100
+ async getLastTxEffects() {
101
+ const latestBlockNumber = await this.stateMachine.archiver.getBlockNumber();
102
+ const block = await this.stateMachine.archiver.getBlock(latestBlockNumber);
87
103
  if (block.body.txEffects.length != 1) {
88
104
  // Note that calls like env.mine() will result in blocks with no transactions, hitting this
89
105
  throw new Error(`Expected a single transaction in the last block, found ${block.body.txEffects.length}`);
@@ -95,17 +111,39 @@ export class TXEOracleTopLevelContext {
95
111
  nullifiers: txEffects.nullifiers
96
112
  };
97
113
  }
98
- async txeAdvanceBlocksBy(blocks) {
114
+ async syncContractNonOracleMethod(contractAddress, scope, jobId) {
115
+ if (contractAddress.equals(DEFAULT_ADDRESS)) {
116
+ this.logger.debug(`Skipping sync in getPrivateEvents because the events correspond to the default address.`);
117
+ return;
118
+ }
119
+ const blockHeader = await this.stateMachine.anchorBlockStore.getBlockHeader();
120
+ await this.stateMachine.contractSyncService.ensureContractSynced(contractAddress, null, async (call, execScopes)=>{
121
+ await this.executeUtilityCall(call, execScopes, jobId);
122
+ }, blockHeader, jobId, [
123
+ scope
124
+ ]);
125
+ }
126
+ async getPrivateEvents(selector, contractAddress, scope) {
127
+ return (await this.privateEventStore.getPrivateEvents(selector, {
128
+ contractAddress,
129
+ scopes: [
130
+ scope
131
+ ],
132
+ fromBlock: 0,
133
+ toBlock: await this.getLastBlockNumber() + 1
134
+ })).map((e)=>e.packedEvent);
135
+ }
136
+ async advanceBlocksBy(blocks) {
99
137
  this.logger.debug(`time traveling ${blocks} blocks`);
100
138
  for(let i = 0; i < blocks; i++){
101
139
  await this.mineBlock();
102
140
  }
103
141
  }
104
- txeAdvanceTimestampBy(duration) {
142
+ advanceTimestampBy(duration) {
105
143
  this.logger.debug(`time traveling ${duration} seconds`);
106
144
  this.nextBlockTimestamp += duration;
107
145
  }
108
- async txeDeploy(artifact, instance, secret) {
146
+ async deploy(artifact, instance, secret) {
109
147
  // Emit deployment nullifier
110
148
  await this.mineBlock({
111
149
  nullifiers: [
@@ -113,34 +151,34 @@ export class TXEOracleTopLevelContext {
113
151
  ]
114
152
  });
115
153
  if (!secret.equals(Fr.ZERO)) {
116
- await this.txeAddAccount(artifact, instance, secret);
154
+ await this.addAccount(artifact, instance, secret);
117
155
  } else {
118
- await this.contractDataProvider.addContractInstance(instance);
119
- await this.contractDataProvider.addContractArtifact(instance.currentContractClassId, artifact);
156
+ await this.contractStore.addContractInstance(instance);
157
+ await this.contractStore.addContractArtifact(artifact);
120
158
  this.logger.debug(`Deployed ${artifact.name} at ${instance.address}`);
121
159
  }
122
160
  }
123
- async txeAddAccount(artifact, instance, secret) {
161
+ async addAccount(artifact, instance, secret) {
124
162
  const partialAddress = await computePartialAddress(instance);
125
163
  this.logger.debug(`Deployed ${artifact.name} at ${instance.address}`);
126
- await this.contractDataProvider.addContractInstance(instance);
127
- await this.contractDataProvider.addContractArtifact(instance.currentContractClassId, artifact);
164
+ await this.contractStore.addContractInstance(instance);
165
+ await this.contractStore.addContractArtifact(artifact);
128
166
  const completeAddress = await this.keyStore.addAccount(secret, partialAddress);
129
- await this.accountDataProvider.setAccount(completeAddress.address, completeAddress);
130
- await this.addressDataProvider.addCompleteAddress(completeAddress);
167
+ await this.accountStore.setAccount(completeAddress.address, completeAddress);
168
+ await this.addressStore.addCompleteAddress(completeAddress);
131
169
  this.logger.debug(`Created account ${completeAddress.address}`);
132
170
  return completeAddress;
133
171
  }
134
- async txeCreateAccount(secret) {
135
- // This is a footgun !
172
+ async createAccount(secret) {
173
+ // This is a foot gun !
136
174
  const completeAddress = await this.keyStore.addAccount(secret, secret);
137
- await this.accountDataProvider.setAccount(completeAddress.address, completeAddress);
138
- await this.addressDataProvider.addCompleteAddress(completeAddress);
175
+ await this.accountStore.setAccount(completeAddress.address, completeAddress);
176
+ await this.addressStore.addCompleteAddress(completeAddress);
139
177
  this.logger.debug(`Created account ${completeAddress.address}`);
140
178
  return completeAddress;
141
179
  }
142
- async txeAddAuthWitness(address, messageHash) {
143
- const account = await this.accountDataProvider.getAccount(address);
180
+ async addAuthWitness(address, messageHash) {
181
+ const account = await this.accountStore.getAccount(address);
144
182
  const privateKey = await this.keyStore.getMasterSecretKey(account.publicKeys.masterIncomingViewingPublicKey);
145
183
  const schnorr = new Schnorr();
146
184
  const signature = await schnorr.constructSignature(messageHash.toBuffer(), privateKey);
@@ -150,7 +188,7 @@ export class TXEOracleTopLevelContext {
150
188
  this.authwits.set(authWitness.requestHash.toString(), authWitness);
151
189
  }
152
190
  async mineBlock(options = {}) {
153
- const blockNumber = await this.txeGetNextBlockNumber();
191
+ const blockNumber = await this.getNextBlockNumber();
154
192
  const txEffect = TxEffect.empty();
155
193
  txEffect.nullifiers = [
156
194
  getSingleTxBlockRequestHash(blockNumber),
@@ -159,42 +197,85 @@ export class TXEOracleTopLevelContext {
159
197
  txEffect.txHash = new TxHash(new Fr(blockNumber));
160
198
  const forkedWorldTrees = await this.stateMachine.synchronizer.nativeWorldStateService.fork();
161
199
  await insertTxEffectIntoWorldTrees(txEffect, forkedWorldTrees);
162
- const block = new L2Block(makeAppendOnlyTreeSnapshot(), await makeTXEBlockHeader(forkedWorldTrees, makeGlobalVariables(undefined, {
200
+ const globals = makeGlobalVariables(undefined, {
163
201
  blockNumber,
164
202
  timestamp: this.nextBlockTimestamp,
165
203
  version: this.version,
166
204
  chainId: this.chainId
167
- })), new Body([
205
+ });
206
+ const block = await makeTXEBlock(forkedWorldTrees, globals, [
168
207
  txEffect
169
- ]));
208
+ ]);
170
209
  await forkedWorldTrees.close();
171
210
  this.logger.info(`Created block ${blockNumber} with timestamp ${block.header.globalVariables.timestamp}`);
172
211
  await this.stateMachine.handleL2Block(block);
173
212
  }
174
- async txePrivateCallNewFlow(from, targetContractAddress = AztecAddress.zero(), functionSelector = FunctionSelector.empty(), args, argsHash = Fr.zero(), isStaticCall = false) {
175
- this.logger.verbose(`Executing external function ${await this.contractDataProvider.getDebugFunctionName(targetContractAddress, functionSelector)}@${targetContractAddress} isStaticCall=${isStaticCall}`);
176
- const artifact = await this.contractDataProvider.getFunctionArtifact(targetContractAddress, functionSelector);
213
+ async privateCallNewFlow(from, targetContractAddress = AztecAddress.zero(), functionSelector = FunctionSelector.empty(), args, argsHash = Fr.zero(), isStaticCall = false, jobId) {
214
+ this.logger.verbose(`Executing external function ${await this.contractStore.getDebugFunctionName(targetContractAddress, functionSelector)}@${targetContractAddress} isStaticCall=${isStaticCall}`);
215
+ const artifact = await this.contractStore.getFunctionArtifact(targetContractAddress, functionSelector);
177
216
  if (!artifact) {
178
217
  const message = functionSelector.equals(await FunctionSelector.fromSignature('verify_private_authwit(Field)')) ? 'Found no account contract artifact for a private authwit check - use `create_contract_account` instead of `create_light_account` for authwit support.' : 'Function Artifact does not exist';
179
218
  throw new Error(message);
180
219
  }
181
- const blockNumber = await this.txeGetNextBlockNumber();
220
+ // When `from` is the zero address (e.g. when deploying a new account contract), we return an
221
+ // empty scope list which acts as deny-all: no notes are visible and no keys are accessible.
222
+ const effectiveScopes = from.isZero() ? [] : [
223
+ from
224
+ ];
225
+ // Sync notes before executing private function to discover notes from previous transactions
226
+ const utilityExecutor = async (call, execScopes)=>{
227
+ await this.executeUtilityCall(call, execScopes, jobId);
228
+ };
229
+ const blockHeader = await this.stateMachine.anchorBlockStore.getBlockHeader();
230
+ await this.stateMachine.contractSyncService.ensureContractSynced(targetContractAddress, functionSelector, utilityExecutor, blockHeader, jobId, effectiveScopes);
231
+ const blockNumber = await this.getNextBlockNumber();
182
232
  const callContext = new CallContext(from, targetContractAddress, functionSelector, isStaticCall);
183
233
  const gasLimits = new Gas(DEFAULT_DA_GAS_LIMIT, DEFAULT_L2_GAS_LIMIT);
184
234
  const teardownGasLimits = new Gas(DEFAULT_TEARDOWN_DA_GAS_LIMIT, DEFAULT_TEARDOWN_L2_GAS_LIMIT);
185
235
  const gasSettings = new GasSettings(gasLimits, teardownGasLimits, GasFees.empty(), GasFees.empty());
186
236
  const txContext = new TxContext(this.chainId, this.version, gasSettings);
187
- const blockHeader = await this.pxeOracleInterface.getAnchorBlockHeader();
188
237
  const protocolNullifier = await computeProtocolNullifier(getSingleTxBlockRequestHash(blockNumber));
189
238
  const noteCache = new ExecutionNoteCache(protocolNullifier);
239
+ // In production, the account contract sets the min revertible counter before calling the app function.
240
+ // Since TXE bypasses the account contract, we simulate this by setting minRevertibleSideEffectCounter to 1,
241
+ // marking all side effects as revertible.
242
+ const minRevertibleSideEffectCounter = 1;
243
+ await noteCache.setMinRevertibleSideEffectCounter(minRevertibleSideEffectCounter);
190
244
  const taggingIndexCache = new ExecutionTaggingIndexCache();
191
245
  const simulator = new WASMSimulator();
192
- const privateExecutionOracle = new PrivateExecutionOracle(argsHash, txContext, callContext, /** Header of a block whose state is used during private execution (not the block the transaction is included in). */ blockHeader, /** List of transient auth witnesses to be used during this simulation */ Array.from(this.authwits.values()), /** List of transient auth witnesses to be used during this simulation */ [], HashedValuesCache.create([
193
- new HashedValues(args, argsHash)
194
- ]), noteCache, taggingIndexCache, this.pxeOracleInterface, 0, 1, undefined, undefined, /**
195
- * In TXE, the typical transaction entrypoint is skipped, so we need to simulate the actions that such a
196
- * contract would perform, including setting senderForTags.
197
- */ from, simulator);
246
+ const privateExecutionOracle = new PrivateExecutionOracle({
247
+ argsHash,
248
+ txContext,
249
+ callContext,
250
+ anchorBlockHeader: blockHeader,
251
+ utilityExecutor,
252
+ authWitnesses: Array.from(this.authwits.values()),
253
+ capsules: [],
254
+ executionCache: HashedValuesCache.create([
255
+ new HashedValues(args, argsHash)
256
+ ]),
257
+ noteCache,
258
+ taggingIndexCache,
259
+ contractStore: this.contractStore,
260
+ noteStore: this.noteStore,
261
+ keyStore: this.keyStore,
262
+ addressStore: this.addressStore,
263
+ aztecNode: this.stateMachine.node,
264
+ senderTaggingStore: this.senderTaggingStore,
265
+ recipientTaggingStore: this.recipientTaggingStore,
266
+ senderAddressBookStore: this.senderAddressBookStore,
267
+ capsuleStore: this.capsuleStore,
268
+ privateEventStore: this.privateEventStore,
269
+ contractSyncService: this.stateMachine.contractSyncService,
270
+ jobId,
271
+ totalPublicCalldataCount: 0,
272
+ sideEffectCounter: minRevertibleSideEffectCounter,
273
+ scopes: effectiveScopes,
274
+ // In TXE, the typical transaction entrypoint is skipped, so we need to simulate the actions that such a
275
+ // contract would perform, including setting senderForTags.
276
+ senderForTags: from,
277
+ simulator
278
+ });
198
279
  // Note: This is a slight modification of simulator.run without any of the checks. Maybe we should modify simulator.run with a boolean value to skip checks.
199
280
  let result;
200
281
  let executionResult;
@@ -206,21 +287,19 @@ export class TXEOracleTopLevelContext {
206
287
  r.publicInputs.publicTeardownCallRequest
207
288
  ]));
208
289
  const publicFunctionsCalldata = await Promise.all(publicCallRequests.map(async (r)=>{
209
- const calldata = await privateExecutionOracle.privateLoadFromExecutionCache(r.calldataHash);
290
+ const calldata = await privateExecutionOracle.loadFromExecutionCache(r.calldataHash);
210
291
  return new HashedValues(calldata, r.calldataHash);
211
292
  }));
212
- // TXE's top level context does not track side effect counters, and as such, minRevertibleSideEffectCounter is always 0.
213
- // This has the unfortunate consequence of always producing revertible nullifiers, which means we
214
- // must set the firstNullifierHint to Fr.ZERO so the txRequestHash is always used as nonce generator
215
- result = new PrivateExecutionResult(executionResult, Fr.ZERO, publicFunctionsCalldata);
293
+ noteCache.finish();
294
+ const nonceGenerator = noteCache.getNonceGenerator();
295
+ result = new PrivateExecutionResult(executionResult, nonceGenerator, publicFunctionsCalldata);
216
296
  } catch (err) {
217
297
  throw createSimulationError(err instanceof Error ? err : new Error('Unknown error during private execution'));
218
298
  }
219
- // According to the protocol rules, the nonce generator for the note hashes
220
- // can either be the first nullifier in the tx or the hash of the initial tx request
221
- // if there are none.
222
- const nonceGenerator = result.firstNullifier.equals(Fr.ZERO) ? protocolNullifier : result.firstNullifier;
223
- const { publicInputs } = await generateSimulatedProvingResult(result, nonceGenerator, this.contractDataProvider);
299
+ // According to the protocol rules, there must be at least one nullifier in the tx. The first nullifier is used as
300
+ // the nonce generator for the note hashes.
301
+ // We pass the non-zero minRevertibleSideEffectCounter to make sure the side effects are split correctly.
302
+ const { publicInputs } = await generateSimulatedProvingResult(result, (addr, sel)=>this.contractStore.getDebugFunctionName(addr, sel), this.stateMachine.node, minRevertibleSideEffectCounter);
224
303
  const globals = makeGlobalVariables();
225
304
  globals.blockNumber = blockNumber;
226
305
  globals.timestamp = this.nextBlockTimestamp;
@@ -228,7 +307,8 @@ export class TXEOracleTopLevelContext {
228
307
  globals.version = this.version;
229
308
  globals.gasFees = GasFees.empty();
230
309
  const forkedWorldTrees = await this.stateMachine.synchronizer.nativeWorldStateService.fork();
231
- const contractsDB = new PublicContractsDB(new TXEPublicContractDataSource(blockNumber, this.contractDataProvider));
310
+ const bindings = this.logger.getBindings();
311
+ const contractsDB = new PublicContractsDB(new TXEPublicContractDataSource(blockNumber, this.contractStore), bindings);
232
312
  const guardedMerkleTrees = new GuardedMerkleTreeOperations(forkedWorldTrees);
233
313
  const config = PublicSimulatorConfig.from({
234
314
  skipFeeEnforcement: true,
@@ -237,7 +317,7 @@ export class TXEOracleTopLevelContext {
237
317
  collectStatistics: false,
238
318
  collectCallMetadata: true
239
319
  });
240
- const processor = new PublicProcessor(globals, guardedMerkleTrees, contractsDB, new PublicTxSimulator(guardedMerkleTrees, contractsDB, globals, config), new TestDateProvider());
320
+ const processor = new PublicProcessor(globals, guardedMerkleTrees, contractsDB, new CppPublicTxSimulator(guardedMerkleTrees, contractsDB, globals, config, bindings), new TestDateProvider(), undefined, createLogger('simulator:public-processor', bindings));
241
321
  const tx = await Tx.create({
242
322
  data: publicInputs,
243
323
  chonkProof: ChonkProof.empty(),
@@ -258,7 +338,7 @@ export class TXEOracleTopLevelContext {
258
338
  } else if (!processedTx.revertCode.isOK()) {
259
339
  if (processedTx.revertReason) {
260
340
  try {
261
- await enrichPublicSimulationError(processedTx.revertReason, this.contractDataProvider, this.logger);
341
+ await enrichPublicSimulationError(processedTx.revertReason, this.contractStore, this.logger);
262
342
  // eslint-disable-next-line no-empty
263
343
  } catch {}
264
344
  throw new Error(`Contract execution has reverted: ${processedTx.revertReason.getMessage()}`);
@@ -280,22 +360,21 @@ export class TXEOracleTopLevelContext {
280
360
  txEffect.txHash = new TxHash(new Fr(blockNumber));
281
361
  const l1ToL2Messages = Array(NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP).fill(0).map(Fr.zero);
282
362
  await forkedWorldTrees.appendLeaves(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, l1ToL2Messages);
283
- const body = new Body([
363
+ const l2Block = await makeTXEBlock(forkedWorldTrees, globals, [
284
364
  txEffect
285
365
  ]);
286
- const l2Block = new L2Block(makeAppendOnlyTreeSnapshot(), await makeTXEBlockHeader(forkedWorldTrees, globals), body);
287
366
  await this.stateMachine.handleL2Block(l2Block);
288
367
  await forkedWorldTrees.close();
289
368
  return executionResult.returnValues ?? [];
290
369
  }
291
- async txePublicCallNewFlow(from, targetContractAddress, calldata, isStaticCall) {
292
- this.logger.verbose(`Executing public function ${await this.contractDataProvider.getDebugFunctionName(targetContractAddress, FunctionSelector.fromField(calldata[0]))}@${targetContractAddress} isStaticCall=${isStaticCall}`);
293
- const blockNumber = await this.txeGetNextBlockNumber();
370
+ async publicCallNewFlow(from, targetContractAddress, calldata, isStaticCall) {
371
+ this.logger.verbose(`Executing public function ${await this.contractStore.getDebugFunctionName(targetContractAddress, FunctionSelector.fromField(calldata[0]))}@${targetContractAddress} isStaticCall=${isStaticCall}`);
372
+ const blockNumber = await this.getNextBlockNumber();
294
373
  const gasLimits = new Gas(DEFAULT_DA_GAS_LIMIT, DEFAULT_L2_GAS_LIMIT);
295
374
  const teardownGasLimits = new Gas(DEFAULT_TEARDOWN_DA_GAS_LIMIT, DEFAULT_TEARDOWN_L2_GAS_LIMIT);
296
375
  const gasSettings = new GasSettings(gasLimits, teardownGasLimits, GasFees.empty(), GasFees.empty());
297
376
  const txContext = new TxContext(this.chainId, this.version, gasSettings);
298
- const anchorBlockHeader = await this.pxeOracleInterface.getAnchorBlockHeader();
377
+ const anchorBlockHeader = await this.stateMachine.anchorBlockStore.getBlockHeader();
299
378
  const calldataHash = await computeCalldataHash(calldata);
300
379
  const calldataHashedValues = new HashedValues(calldata, calldataHash);
301
380
  const globals = makeGlobalVariables();
@@ -305,7 +384,8 @@ export class TXEOracleTopLevelContext {
305
384
  globals.version = this.version;
306
385
  globals.gasFees = GasFees.empty();
307
386
  const forkedWorldTrees = await this.stateMachine.synchronizer.nativeWorldStateService.fork();
308
- const contractsDB = new PublicContractsDB(new TXEPublicContractDataSource(blockNumber, this.contractDataProvider));
387
+ const bindings2 = this.logger.getBindings();
388
+ const contractsDB = new PublicContractsDB(new TXEPublicContractDataSource(blockNumber, this.contractStore), bindings2);
309
389
  const guardedMerkleTrees = new GuardedMerkleTreeOperations(forkedWorldTrees);
310
390
  const config = PublicSimulatorConfig.from({
311
391
  skipFeeEnforcement: true,
@@ -314,23 +394,21 @@ export class TXEOracleTopLevelContext {
314
394
  collectStatistics: false,
315
395
  collectCallMetadata: true
316
396
  });
317
- const simulator = new PublicTxSimulator(guardedMerkleTrees, contractsDB, globals, config);
318
- const processor = new PublicProcessor(globals, guardedMerkleTrees, contractsDB, simulator, new TestDateProvider());
397
+ const simulator = new CppPublicTxSimulator(guardedMerkleTrees, contractsDB, globals, config, bindings2);
398
+ const processor = new PublicProcessor(globals, guardedMerkleTrees, contractsDB, simulator, new TestDateProvider(), undefined, createLogger('simulator:public-processor', bindings2));
319
399
  // We're simulating a scenario in which private execution immediately enqueues a public call and halts. The private
320
400
  // kernel init would in this case inject a nullifier with the transaction request hash as a non-revertible
321
401
  // side-effect, which the AVM then expects to exist in order to use it as the nonce generator when siloing notes as
322
402
  // unique.
323
403
  const nonRevertibleAccumulatedData = PrivateToPublicAccumulatedData.empty();
324
- if (!isStaticCall) {
325
- nonRevertibleAccumulatedData.nullifiers[0] = getSingleTxBlockRequestHash(blockNumber);
326
- }
404
+ nonRevertibleAccumulatedData.nullifiers[0] = getSingleTxBlockRequestHash(blockNumber);
327
405
  // The enqueued public call itself we make be revertible so that the public execution is itself revertible, as tests
328
406
  // may require producing reverts.
329
407
  const revertibleAccumulatedData = PrivateToPublicAccumulatedData.empty();
330
408
  revertibleAccumulatedData.publicCallRequests[0] = new PublicCallRequest(from, targetContractAddress, isStaticCall, calldataHash);
331
409
  const inputsForPublic = new PartialPrivateTailPublicInputsForPublic(nonRevertibleAccumulatedData, revertibleAccumulatedData, PublicCallRequest.empty());
332
410
  const constantData = new TxConstantData(anchorBlockHeader, txContext, Fr.zero(), Fr.zero());
333
- const txData = new PrivateKernelTailCircuitPublicInputs(constantData, /*gasUsed=*/ new Gas(0, 0), /*feePayer=*/ AztecAddress.zero(), /*includeByTimestamp=*/ 0n, inputsForPublic, undefined);
411
+ const txData = new PrivateKernelTailCircuitPublicInputs(constantData, /*gasUsed=*/ new Gas(0, 0), /*feePayer=*/ AztecAddress.zero(), /*expirationTimestamp=*/ 0n, inputsForPublic, undefined);
334
412
  const tx = await Tx.create({
335
413
  data: txData,
336
414
  chonkProof: ChonkProof.empty(),
@@ -353,7 +431,7 @@ export class TXEOracleTopLevelContext {
353
431
  } else if (!processedTx.revertCode.isOK()) {
354
432
  if (processedTx.revertReason) {
355
433
  try {
356
- await enrichPublicSimulationError(processedTx.revertReason, this.contractDataProvider, this.logger);
434
+ await enrichPublicSimulationError(processedTx.revertReason, this.contractStore, this.logger);
357
435
  // eslint-disable-next-line no-empty
358
436
  } catch {}
359
437
  throw new Error(`Contract execution has reverted: ${processedTx.revertReason.getMessage()}`);
@@ -376,25 +454,37 @@ export class TXEOracleTopLevelContext {
376
454
  txEffect.txHash = new TxHash(new Fr(blockNumber));
377
455
  const l1ToL2Messages = Array(NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP).fill(0).map(Fr.zero);
378
456
  await forkedWorldTrees.appendLeaves(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, l1ToL2Messages);
379
- const body = new Body([
457
+ const l2Block = await makeTXEBlock(forkedWorldTrees, globals, [
380
458
  txEffect
381
459
  ]);
382
- const l2Block = new L2Block(makeAppendOnlyTreeSnapshot(), await makeTXEBlockHeader(forkedWorldTrees, globals), body);
383
460
  await this.stateMachine.handleL2Block(l2Block);
384
461
  await forkedWorldTrees.close();
385
462
  return returnValues ?? [];
386
463
  }
387
- async txeSimulateUtilityFunction(targetContractAddress, functionSelector, args) {
388
- const artifact = await this.contractDataProvider.getFunctionArtifact(targetContractAddress, functionSelector);
464
+ async executeUtilityFunction(targetContractAddress, functionSelector, args, jobId) {
465
+ const artifact = await this.contractStore.getFunctionArtifact(targetContractAddress, functionSelector);
389
466
  if (!artifact) {
390
467
  throw new Error(`Cannot call ${functionSelector} as there is no artifact found at ${targetContractAddress}.`);
391
468
  }
392
- const call = {
469
+ // Sync notes before executing utility function to discover notes from previous transactions
470
+ const blockHeader = await this.stateMachine.anchorBlockStore.getBlockHeader();
471
+ await this.stateMachine.contractSyncService.ensureContractSynced(targetContractAddress, functionSelector, async (call, execScopes)=>{
472
+ await this.executeUtilityCall(call, execScopes, jobId);
473
+ }, blockHeader, jobId, 'ALL_SCOPES');
474
+ const call = FunctionCall.from({
393
475
  name: artifact.name,
476
+ to: targetContractAddress,
394
477
  selector: functionSelector,
395
- to: targetContractAddress
396
- };
397
- const entryPointArtifact = await this.pxeOracleInterface.getFunctionArtifact(call.to, call.selector);
478
+ type: FunctionType.UTILITY,
479
+ hideMsgSender: false,
480
+ isStatic: false,
481
+ args,
482
+ returnTypes: []
483
+ });
484
+ return this.executeUtilityCall(call, 'ALL_SCOPES', jobId);
485
+ }
486
+ async executeUtilityCall(call, scopes, jobId) {
487
+ const entryPointArtifact = await this.contractStore.getFunctionArtifactWithDebugMetadata(call.to, call.selector);
398
488
  if (entryPointArtifact.functionType !== FunctionType.UTILITY) {
399
489
  throw new Error(`Cannot run ${entryPointArtifact.functionType} function as utility`);
400
490
  }
@@ -403,8 +493,25 @@ export class TXEOracleTopLevelContext {
403
493
  selector: call.selector
404
494
  });
405
495
  try {
406
- const oracle = new UtilityExecutionOracle(call.to, [], [], this.pxeOracleInterface);
407
- const acirExecutionResult = await new WASMSimulator().executeUserCircuit(toACVMWitness(0, args), entryPointArtifact, new Oracle(oracle).toACIRCallback()).catch((err)=>{
496
+ const anchorBlockHeader = await this.stateMachine.anchorBlockStore.getBlockHeader();
497
+ const oracle = new UtilityExecutionOracle({
498
+ contractAddress: call.to,
499
+ authWitnesses: [],
500
+ capsules: [],
501
+ anchorBlockHeader,
502
+ contractStore: this.contractStore,
503
+ noteStore: this.noteStore,
504
+ keyStore: this.keyStore,
505
+ addressStore: this.addressStore,
506
+ aztecNode: this.stateMachine.node,
507
+ recipientTaggingStore: this.recipientTaggingStore,
508
+ senderAddressBookStore: this.senderAddressBookStore,
509
+ capsuleStore: this.capsuleStore,
510
+ privateEventStore: this.privateEventStore,
511
+ jobId,
512
+ scopes
513
+ });
514
+ const acirExecutionResult = await new WASMSimulator().executeUserCircuit(toACVMWitness(0, call.args), entryPointArtifact, new Oracle(oracle).toACIRCallback()).catch((err)=>{
408
515
  err.message = resolveAssertionMessageFromError(err, entryPointArtifact);
409
516
  throw new ExecutionError(err.message, {
410
517
  contractAddress: call.to,
@@ -413,10 +520,10 @@ export class TXEOracleTopLevelContext {
413
520
  cause: err
414
521
  });
415
522
  });
416
- this.logger.verbose(`Utility simulation for ${call.to}.${call.selector} completed`);
523
+ this.logger.verbose(`Utility execution for ${call.to}.${call.selector} completed`);
417
524
  return witnessMapToFields(acirExecutionResult.returnWitness);
418
525
  } catch (err) {
419
- throw createSimulationError(err instanceof Error ? err : new Error('Unknown error during utility simulation'));
526
+ throw createSimulationError(err instanceof Error ? err : new Error('Unknown error during utility execution'));
420
527
  }
421
528
  }
422
529
  close() {
@@ -427,6 +534,7 @@ export class TXEOracleTopLevelContext {
427
534
  ];
428
535
  }
429
536
  async getLastBlockNumber() {
430
- return (await this.stateMachine.node.getBlockHeader('latest'))?.globalVariables.blockNumber ?? 0;
537
+ const header = await this.stateMachine.node.getBlockHeader('latest');
538
+ return header ? header.globalVariables.blockNumber : BlockNumber.ZERO;
431
539
  }
432
540
  }