@aztec/simulator 0.47.1 → 0.49.2

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 (103) hide show
  1. package/dest/acvm/acvm.d.ts +2 -2
  2. package/dest/acvm/acvm.d.ts.map +1 -1
  3. package/dest/acvm/acvm.js +12 -7
  4. package/dest/acvm/oracle/oracle.d.ts +3 -3
  5. package/dest/acvm/oracle/oracle.d.ts.map +1 -1
  6. package/dest/acvm/oracle/oracle.js +8 -9
  7. package/dest/acvm/oracle/typed_oracle.d.ts +5 -5
  8. package/dest/acvm/oracle/typed_oracle.d.ts.map +1 -1
  9. package/dest/acvm/oracle/typed_oracle.js +6 -6
  10. package/dest/avm/avm_gas.d.ts +1 -0
  11. package/dest/avm/avm_gas.d.ts.map +1 -1
  12. package/dest/avm/avm_gas.js +142 -79
  13. package/dest/avm/fixtures/index.d.ts.map +1 -1
  14. package/dest/avm/fixtures/index.js +2 -2
  15. package/dest/avm/opcodes/accrued_substate.js +2 -2
  16. package/dest/avm/opcodes/commitment.js +4 -4
  17. package/dest/avm/opcodes/conversion.js +2 -2
  18. package/dest/avm/opcodes/external_calls.js +4 -4
  19. package/dest/avm/opcodes/instruction.d.ts +2 -1
  20. package/dest/avm/opcodes/instruction.d.ts.map +1 -1
  21. package/dest/avm/opcodes/instruction.js +8 -5
  22. package/dest/avm/opcodes/memory.js +2 -2
  23. package/dest/avm/opcodes/storage.js +3 -3
  24. package/dest/avm/serialization/instruction_serialization.js +2 -2
  25. package/dest/client/client_execution_context.d.ts +8 -8
  26. package/dest/client/client_execution_context.d.ts.map +1 -1
  27. package/dest/client/client_execution_context.js +22 -23
  28. package/dest/client/db_oracle.d.ts +4 -3
  29. package/dest/client/db_oracle.d.ts.map +1 -1
  30. package/dest/client/execution_note_cache.d.ts +17 -13
  31. package/dest/client/execution_note_cache.d.ts.map +1 -1
  32. package/dest/client/execution_note_cache.js +65 -26
  33. package/dest/client/execution_result.d.ts +3 -2
  34. package/dest/client/execution_result.d.ts.map +1 -1
  35. package/dest/client/execution_result.js +20 -9
  36. package/dest/client/private_execution.js +3 -3
  37. package/dest/client/simulator.d.ts +7 -6
  38. package/dest/client/simulator.d.ts.map +1 -1
  39. package/dest/client/simulator.js +14 -12
  40. package/dest/client/test_utils.d.ts +9 -0
  41. package/dest/client/test_utils.d.ts.map +1 -0
  42. package/dest/client/test_utils.js +21 -0
  43. package/dest/client/view_data_oracle.d.ts +2 -1
  44. package/dest/client/view_data_oracle.d.ts.map +1 -1
  45. package/dest/client/view_data_oracle.js +4 -3
  46. package/dest/index.d.ts +0 -1
  47. package/dest/index.d.ts.map +1 -1
  48. package/dest/index.js +1 -2
  49. package/dest/providers/acvm_native.js +2 -2
  50. package/dest/public/abstract_phase_manager.d.ts.map +1 -1
  51. package/dest/public/abstract_phase_manager.js +3 -3
  52. package/dest/public/executor.d.ts +4 -1
  53. package/dest/public/executor.d.ts.map +1 -1
  54. package/dest/public/executor.js +10 -2
  55. package/dest/public/executor_metrics.d.ts +10 -0
  56. package/dest/public/executor_metrics.d.ts.map +1 -0
  57. package/dest/public/executor_metrics.js +32 -0
  58. package/dest/public/fee_payment.d.ts +2 -2
  59. package/dest/public/fee_payment.d.ts.map +1 -1
  60. package/dest/public/fee_payment.js +9 -10
  61. package/dest/public/hints_builder.d.ts.map +1 -1
  62. package/dest/public/hints_builder.js +1 -1
  63. package/dest/public/public_processor.d.ts +6 -5
  64. package/dest/public/public_processor.d.ts.map +1 -1
  65. package/dest/public/public_processor.js +38 -18
  66. package/dest/public/public_processor_metrics.d.ts +19 -0
  67. package/dest/public/public_processor_metrics.d.ts.map +1 -0
  68. package/dest/public/public_processor_metrics.js +59 -0
  69. package/package.json +9 -9
  70. package/src/acvm/acvm.ts +14 -5
  71. package/src/acvm/oracle/oracle.ts +8 -23
  72. package/src/acvm/oracle/typed_oracle.ts +8 -21
  73. package/src/avm/avm_gas.ts +145 -79
  74. package/src/avm/fixtures/index.ts +1 -0
  75. package/src/avm/opcodes/accrued_substate.ts +1 -1
  76. package/src/avm/opcodes/commitment.ts +3 -3
  77. package/src/avm/opcodes/conversion.ts +1 -1
  78. package/src/avm/opcodes/external_calls.ts +3 -3
  79. package/src/avm/opcodes/instruction.ts +7 -4
  80. package/src/avm/opcodes/memory.ts +1 -1
  81. package/src/avm/opcodes/storage.ts +2 -2
  82. package/src/avm/serialization/instruction_serialization.ts +1 -1
  83. package/src/client/client_execution_context.ts +23 -20
  84. package/src/client/db_oracle.ts +9 -3
  85. package/src/client/execution_note_cache.ts +76 -25
  86. package/src/client/execution_result.ts +29 -9
  87. package/src/client/private_execution.ts +2 -2
  88. package/src/client/simulator.ts +18 -14
  89. package/src/client/test_utils.ts +30 -0
  90. package/src/client/view_data_oracle.ts +2 -1
  91. package/src/index.ts +0 -1
  92. package/src/providers/acvm_native.ts +1 -1
  93. package/src/public/abstract_phase_manager.ts +1 -2
  94. package/src/public/executor.ts +14 -1
  95. package/src/public/executor_metrics.ts +48 -0
  96. package/src/public/fee_payment.ts +8 -10
  97. package/src/public/hints_builder.ts +2 -2
  98. package/src/public/public_processor.ts +52 -20
  99. package/src/public/public_processor_metrics.ts +92 -0
  100. package/dest/utils.d.ts +0 -12
  101. package/dest/utils.d.ts.map +0 -1
  102. package/dest/utils.js +0 -11
  103. package/src/utils.ts +0 -18
@@ -1,8 +1,8 @@
1
1
  import {
2
- type BlockProver,
3
2
  type FailedTx,
4
3
  NestedProcessReturnValues,
5
4
  type ProcessedTx,
5
+ type ProcessedTxHandler,
6
6
  PublicKernelType,
7
7
  type PublicProvingRequest,
8
8
  type SimulationError,
@@ -13,7 +13,8 @@ import {
13
13
  } from '@aztec/circuit-types';
14
14
  import {
15
15
  AztecAddress,
16
- GAS_TOKEN_ADDRESS,
16
+ ContractClassRegisteredEvent,
17
+ FEE_JUICE_ADDRESS,
17
18
  type GlobalVariables,
18
19
  type Header,
19
20
  type KernelCircuitPublicInputs,
@@ -23,7 +24,9 @@ import {
23
24
  } from '@aztec/circuits.js';
24
25
  import { times } from '@aztec/foundation/collection';
25
26
  import { createDebugLogger } from '@aztec/foundation/log';
27
+ import { Timer } from '@aztec/foundation/timer';
26
28
  import { type ProtocolArtifact } from '@aztec/noir-protocol-circuits-types';
29
+ import { ClassRegistererAddress } from '@aztec/protocol-contracts/class-registerer';
27
30
  import {
28
31
  PublicExecutor,
29
32
  type PublicStateDB,
@@ -40,6 +43,7 @@ import { PhaseManagerFactory } from './phase_manager_factory.js';
40
43
  import { ContractsDataSourcePublicDB, WorldStateDB, WorldStatePublicDB } from './public_db_sources.js';
41
44
  import { RealPublicKernelCircuitSimulator } from './public_kernel.js';
42
45
  import { type PublicKernelCircuitSimulator } from './public_kernel_circuit_simulator.js';
46
+ import { PublicProcessorMetrics } from './public_processor_metrics.js';
43
47
 
44
48
  /**
45
49
  * Creates new instances of PublicProcessor given the provided merkle tree db and contract data source.
@@ -56,18 +60,24 @@ export class PublicProcessorFactory {
56
60
  * Creates a new instance of a PublicProcessor.
57
61
  * @param historicalHeader - The header of a block previous to the one in which the tx is included.
58
62
  * @param globalVariables - The global variables for the block being processed.
59
- * @param newContracts - Provides access to contract bytecode for public executions.
60
63
  * @returns A new instance of a PublicProcessor.
61
64
  */
62
- public create(historicalHeader: Header | undefined, globalVariables: GlobalVariables): PublicProcessor {
63
- historicalHeader = historicalHeader ?? this.merkleTree.getInitialHeader();
64
-
65
+ public create(maybeHistoricalHeader: Header | undefined, globalVariables: GlobalVariables): PublicProcessor {
66
+ const { merkleTree, telemetryClient } = this;
67
+ const historicalHeader = maybeHistoricalHeader ?? merkleTree.getInitialHeader();
65
68
  const publicContractsDB = new ContractsDataSourcePublicDB(this.contractDataSource);
66
- const worldStatePublicDB = new WorldStatePublicDB(this.merkleTree);
67
- const worldStateDB = new WorldStateDB(this.merkleTree);
68
- const publicExecutor = new PublicExecutor(worldStatePublicDB, publicContractsDB, worldStateDB, historicalHeader);
69
+
70
+ const worldStatePublicDB = new WorldStatePublicDB(merkleTree);
71
+ const worldStateDB = new WorldStateDB(merkleTree);
72
+ const publicExecutor = new PublicExecutor(
73
+ worldStatePublicDB,
74
+ publicContractsDB,
75
+ worldStateDB,
76
+ historicalHeader,
77
+ telemetryClient,
78
+ );
69
79
  return new PublicProcessor(
70
- this.merkleTree,
80
+ merkleTree,
71
81
  publicExecutor,
72
82
  new RealPublicKernelCircuitSimulator(this.simulator),
73
83
  globalVariables,
@@ -84,7 +94,7 @@ export class PublicProcessorFactory {
84
94
  * any public function calls in them. Txs with private calls only are unaffected.
85
95
  */
86
96
  export class PublicProcessor {
87
- public readonly tracer: Tracer;
97
+ private metrics: PublicProcessorMetrics;
88
98
  constructor(
89
99
  protected db: MerkleTreeOperations,
90
100
  protected publicExecutor: PublicExecutor,
@@ -96,18 +106,23 @@ export class PublicProcessor {
96
106
  telemetryClient: TelemetryClient,
97
107
  private log = createDebugLogger('aztec:sequencer:public-processor'),
98
108
  ) {
99
- this.tracer = telemetryClient.getTracer('PublicProcessor');
109
+ this.metrics = new PublicProcessorMetrics(telemetryClient, 'PublicProcessor');
110
+ }
111
+
112
+ get tracer(): Tracer {
113
+ return this.metrics.tracer;
100
114
  }
101
115
 
102
116
  /**
103
117
  * Run each tx through the public circuit and the public kernel circuit if needed.
104
118
  * @param txs - Txs to process.
119
+ * @param processedTxHandler - Handler for processed txs in the context of block building or proving.
105
120
  * @returns The list of processed txs with their circuit simulation outputs.
106
121
  */
107
122
  public async process(
108
123
  txs: Tx[],
109
124
  maxTransactions = txs.length,
110
- blockProver?: BlockProver,
125
+ processedTxHandler?: ProcessedTxHandler,
111
126
  txValidator?: TxValidator<ProcessedTx>,
112
127
  ): Promise<[ProcessedTx[], FailedTx[], NestedProcessReturnValues[]]> {
113
128
  // The processor modifies the tx objects in place, so we need to clone them.
@@ -150,9 +165,9 @@ export class PublicProcessor {
150
165
  throw new Error(`Transaction ${invalid[0].hash} invalid after processing public functions`);
151
166
  }
152
167
  }
153
- // if we were given a prover then send the transaction to it for proving
154
- if (blockProver) {
155
- await blockProver.addNewTx(processedTx);
168
+ // if we were given a handler then send the transaction to it for block building or proving
169
+ if (processedTxHandler) {
170
+ await processedTxHandler.addNewTx(processedTx);
156
171
  }
157
172
  result.push(processedTx);
158
173
  returns = returns.concat(returnValues ?? []);
@@ -188,12 +203,12 @@ export class PublicProcessor {
188
203
  return finalPublicDataUpdateRequests;
189
204
  }
190
205
 
191
- const gasToken = AztecAddress.fromBigInt(GAS_TOKEN_ADDRESS);
206
+ const feeJuiceAddress = AztecAddress.fromBigInt(FEE_JUICE_ADDRESS);
192
207
  const balanceSlot = computeFeePayerBalanceStorageSlot(feePayer);
193
208
  const leafSlot = computeFeePayerBalanceLeafSlot(feePayer);
194
209
  const txFee = tx.data.getTransactionFee(this.globalVariables.gasFees);
195
210
 
196
- this.log.debug(`Deducting ${txFee} balance in gas tokens for ${feePayer}`);
211
+ this.log.debug(`Deducting ${txFee} balance in Fee Juice for ${feePayer}`);
197
212
 
198
213
  const existingBalanceWriteIndex = finalPublicDataUpdateRequests.findIndex(request =>
199
214
  request.leafSlot.equals(leafSlot),
@@ -202,14 +217,14 @@ export class PublicProcessor {
202
217
  const balance =
203
218
  existingBalanceWriteIndex > -1
204
219
  ? finalPublicDataUpdateRequests[existingBalanceWriteIndex].newValue
205
- : await this.publicStateDB.storageRead(gasToken, balanceSlot);
220
+ : await this.publicStateDB.storageRead(feeJuiceAddress, balanceSlot);
206
221
 
207
222
  if (balance.lt(txFee)) {
208
223
  throw new Error(`Not enough balance for fee payer to pay for transaction (got ${balance} needs ${txFee})`);
209
224
  }
210
225
 
211
226
  const updatedBalance = balance.sub(txFee);
212
- await this.publicStateDB.storageWrite(gasToken, balanceSlot, updatedBalance);
227
+ await this.publicStateDB.storageWrite(feeJuiceAddress, balanceSlot, updatedBalance);
213
228
 
214
229
  finalPublicDataUpdateRequests[
215
230
  existingBalanceWriteIndex > -1 ? existingBalanceWriteIndex : MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX
@@ -222,6 +237,7 @@ export class PublicProcessor {
222
237
  [Attributes.TX_HASH]: tx.getTxHash().toString(),
223
238
  }))
224
239
  private async processTxWithPublicCalls(tx: Tx): Promise<[ProcessedTx, NestedProcessReturnValues[]]> {
240
+ const timer = new Timer();
225
241
  let returnValues: NestedProcessReturnValues[] = [];
226
242
  const publicProvingRequests: PublicProvingRequest[] = [];
227
243
  let phase: AbstractPhaseManager | undefined = PhaseManagerFactory.phaseFromTx(
@@ -240,8 +256,18 @@ export class PublicProcessor {
240
256
  let finalKernelOutput: KernelCircuitPublicInputs | undefined;
241
257
  let revertReason: SimulationError | undefined;
242
258
  const gasUsed: ProcessedTx['gasUsed'] = {};
259
+ let phaseCount = 0;
243
260
  while (phase) {
261
+ phaseCount++;
262
+ const phaseTimer = new Timer();
244
263
  const output = await phase.handle(tx, publicKernelPublicInput, lastKernelArtifact);
264
+
265
+ if (output.revertReason) {
266
+ this.metrics.recordRevertedPhase(phase.phase);
267
+ } else {
268
+ this.metrics.recordPhaseDuration(phase.phase, phaseTimer.ms());
269
+ }
270
+
245
271
  gasUsed[phase.phase] = output.gasUsed;
246
272
  if (phase.phase === PublicKernelType.APP_LOGIC) {
247
273
  returnValues = output.returnValues;
@@ -265,9 +291,15 @@ export class PublicProcessor {
265
291
  }
266
292
 
267
293
  if (!finalKernelOutput) {
294
+ this.metrics.recordFailedTx();
268
295
  throw new Error('Final public kernel was not executed.');
269
296
  }
270
297
 
298
+ this.metrics.recordClassRegistration(
299
+ ...ContractClassRegisteredEvent.fromLogs(tx.unencryptedLogs.unrollLogs(), ClassRegistererAddress),
300
+ );
301
+
302
+ this.metrics.recordTx(phaseCount, timer.ms());
271
303
  const processedTx = makeProcessedTx(tx, finalKernelOutput, publicProvingRequests, revertReason, gasUsed);
272
304
  return [processedTx, returnValues];
273
305
  }
@@ -0,0 +1,92 @@
1
+ import { type PublicKernelType } from '@aztec/circuit-types';
2
+ import { type ContractClassRegisteredEvent } from '@aztec/circuits.js';
3
+ import {
4
+ Attributes,
5
+ type Histogram,
6
+ Metrics,
7
+ type TelemetryClient,
8
+ type Tracer,
9
+ type UpDownCounter,
10
+ ValueType,
11
+ } from '@aztec/telemetry-client';
12
+
13
+ export class PublicProcessorMetrics {
14
+ public readonly tracer: Tracer;
15
+
16
+ private txDuration: Histogram;
17
+ private txCount: UpDownCounter;
18
+ private txPhaseCount: UpDownCounter;
19
+
20
+ private phaseDuration: Histogram;
21
+ private phaseCount: UpDownCounter;
22
+
23
+ private bytecodeDeployed: Histogram;
24
+
25
+ constructor(client: TelemetryClient, name = 'PublicProcessor') {
26
+ this.tracer = client.getTracer(name);
27
+ const meter = client.getMeter(name);
28
+
29
+ this.txDuration = meter.createHistogram(Metrics.PUBLIC_PROCESSOR_TX_DURATION, {
30
+ description: 'How long it takes to process a transaction',
31
+ unit: 'ms',
32
+ valueType: ValueType.INT,
33
+ });
34
+
35
+ this.txCount = meter.createUpDownCounter(Metrics.PUBLIC_PROCESSOR_TX_COUNT, {
36
+ description: 'Number of transactions processed',
37
+ });
38
+
39
+ this.txPhaseCount = meter.createUpDownCounter(Metrics.PUBLIC_PROCESSOR_TX_PHASE_COUNT, {
40
+ description: 'Number of phases processed',
41
+ });
42
+
43
+ this.phaseDuration = meter.createHistogram(Metrics.PUBLIC_PROCESSOR_PHASE_DURATION, {
44
+ description: 'How long it takes to process a phase',
45
+ unit: 'ms',
46
+ valueType: ValueType.INT,
47
+ });
48
+
49
+ this.phaseCount = meter.createUpDownCounter(Metrics.PUBLIC_PROCESSOR_PHASE_COUNT, {
50
+ description: 'Number of failed phases',
51
+ });
52
+
53
+ this.bytecodeDeployed = meter.createHistogram(Metrics.PUBLIC_PROCESSOR_DEPLOY_BYTECODE_SIZE, {
54
+ description: 'Size of deployed bytecode',
55
+ unit: 'By',
56
+ });
57
+ }
58
+
59
+ recordPhaseDuration(phaseName: PublicKernelType, durationMs: number) {
60
+ this.phaseCount.add(1, { [Attributes.TX_PHASE_NAME]: phaseName, [Attributes.OK]: true });
61
+ this.phaseDuration.record(Math.ceil(durationMs), { [Attributes.TX_PHASE_NAME]: phaseName });
62
+ }
63
+
64
+ recordTx(phaseCount: number, durationMs: number) {
65
+ this.txPhaseCount.add(phaseCount);
66
+ this.txDuration.record(Math.ceil(durationMs));
67
+ this.txCount.add(1, {
68
+ [Attributes.OK]: true,
69
+ });
70
+ }
71
+
72
+ recordFailedTx() {
73
+ this.txCount.add(1, {
74
+ [Attributes.OK]: false,
75
+ });
76
+ }
77
+
78
+ recordRevertedPhase(phaseName: PublicKernelType) {
79
+ this.phaseCount.add(1, { [Attributes.TX_PHASE_NAME]: phaseName, [Attributes.OK]: false });
80
+ }
81
+
82
+ recordClassRegistration(...events: ContractClassRegisteredEvent[]) {
83
+ let totalBytecode = 0;
84
+ for (const event of events) {
85
+ totalBytecode += event.packedPublicBytecode.length;
86
+ }
87
+
88
+ if (totalBytecode > 0) {
89
+ this.bytecodeDeployed.record(totalBytecode);
90
+ }
91
+ }
92
+ }
package/dest/utils.d.ts DELETED
@@ -1,12 +0,0 @@
1
- import { type Fr } from '@aztec/foundation/fields';
2
- /**
3
- * Computes the resulting storage slot for an entry in a mapping.
4
- * @param mappingSlot - The slot of the mapping within state.
5
- * @param key - The key of the mapping.
6
- * @returns The slot in the contract storage where the value is stored.
7
- */
8
- export declare function computeSlotForMapping(mappingSlot: Fr, key: {
9
- /** Serialize to a field. */
10
- toField: () => Fr;
11
- }): Fr;
12
- //# sourceMappingURL=utils.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,EAAE,MAAM,0BAA0B,CAAC;AAEnD;;;;;GAKG;AACH,wBAAgB,qBAAqB,CACnC,WAAW,EAAE,EAAE,EACf,GAAG,EAAE;IACH,4BAA4B;IAC5B,OAAO,EAAE,MAAM,EAAE,CAAC;CACnB,MAGF"}
package/dest/utils.js DELETED
@@ -1,11 +0,0 @@
1
- import { pedersenHash } from '@aztec/foundation/crypto';
2
- /**
3
- * Computes the resulting storage slot for an entry in a mapping.
4
- * @param mappingSlot - The slot of the mapping within state.
5
- * @param key - The key of the mapping.
6
- * @returns The slot in the contract storage where the value is stored.
7
- */
8
- export function computeSlotForMapping(mappingSlot, key) {
9
- return pedersenHash([mappingSlot, key.toField()]);
10
- }
11
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXRpbHMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvdXRpbHMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLFlBQVksRUFBRSxNQUFNLDBCQUEwQixDQUFDO0FBR3hEOzs7OztHQUtHO0FBQ0gsTUFBTSxVQUFVLHFCQUFxQixDQUNuQyxXQUFlLEVBQ2YsR0FHQztJQUVELE9BQU8sWUFBWSxDQUFDLENBQUMsV0FBVyxFQUFFLEdBQUcsQ0FBQyxPQUFPLEVBQUUsQ0FBQyxDQUFDLENBQUM7QUFDcEQsQ0FBQyJ9
package/src/utils.ts DELETED
@@ -1,18 +0,0 @@
1
- import { pedersenHash } from '@aztec/foundation/crypto';
2
- import { type Fr } from '@aztec/foundation/fields';
3
-
4
- /**
5
- * Computes the resulting storage slot for an entry in a mapping.
6
- * @param mappingSlot - The slot of the mapping within state.
7
- * @param key - The key of the mapping.
8
- * @returns The slot in the contract storage where the value is stored.
9
- */
10
- export function computeSlotForMapping(
11
- mappingSlot: Fr,
12
- key: {
13
- /** Serialize to a field. */
14
- toField: () => Fr;
15
- },
16
- ) {
17
- return pedersenHash([mappingSlot, key.toField()]);
18
- }