@aztec/simulator 0.41.0 → 0.42.0

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 (204) 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 +3 -3
  4. package/dest/acvm/oracle/oracle.d.ts +6 -4
  5. package/dest/acvm/oracle/oracle.d.ts.map +1 -1
  6. package/dest/acvm/oracle/oracle.js +20 -11
  7. package/dest/acvm/oracle/typed_oracle.d.ts +5 -3
  8. package/dest/acvm/oracle/typed_oracle.d.ts.map +1 -1
  9. package/dest/acvm/oracle/typed_oracle.js +10 -4
  10. package/dest/acvm/serialize.js +2 -2
  11. package/dest/avm/avm_gas.d.ts +1 -5
  12. package/dest/avm/avm_gas.d.ts.map +1 -1
  13. package/dest/avm/avm_gas.js +67 -73
  14. package/dest/avm/avm_simulator.d.ts +5 -0
  15. package/dest/avm/avm_simulator.d.ts.map +1 -1
  16. package/dest/avm/avm_simulator.js +8 -1
  17. package/dest/avm/fixtures/index.d.ts +3 -0
  18. package/dest/avm/fixtures/index.d.ts.map +1 -1
  19. package/dest/avm/fixtures/index.js +13 -1
  20. package/dest/avm/index.d.ts +2 -0
  21. package/dest/avm/index.d.ts.map +1 -0
  22. package/dest/avm/index.js +2 -0
  23. package/dest/avm/journal/host_storage.d.ts +1 -1
  24. package/dest/avm/journal/host_storage.d.ts.map +1 -1
  25. package/dest/avm/journal/journal.d.ts +2 -0
  26. package/dest/avm/journal/journal.d.ts.map +1 -1
  27. package/dest/avm/journal/journal.js +6 -2
  28. package/dest/avm/journal/trace.d.ts +2 -0
  29. package/dest/avm/journal/trace.d.ts.map +1 -1
  30. package/dest/avm/journal/trace.js +7 -1
  31. package/dest/avm/journal/trace_types.d.ts +1 -0
  32. package/dest/avm/journal/trace_types.d.ts.map +1 -1
  33. package/dest/avm/journal/trace_types.js +1 -1
  34. package/dest/avm/opcodes/arithmetic.d.ts +1 -7
  35. package/dest/avm/opcodes/arithmetic.d.ts.map +1 -1
  36. package/dest/avm/opcodes/arithmetic.js +1 -12
  37. package/dest/avm/opcodes/conversion.js +2 -2
  38. package/dest/avm/opcodes/external_calls.d.ts.map +1 -1
  39. package/dest/avm/opcodes/external_calls.js +4 -3
  40. package/dest/avm/opcodes/instruction.d.ts +1 -1
  41. package/dest/avm/opcodes/instruction.d.ts.map +1 -1
  42. package/dest/avm/opcodes/instruction.js +6 -4
  43. package/dest/avm/opcodes/memory.d.ts +0 -4
  44. package/dest/avm/opcodes/memory.d.ts.map +1 -1
  45. package/dest/avm/opcodes/memory.js +1 -7
  46. package/dest/avm/opcodes/storage.d.ts +0 -5
  47. package/dest/avm/opcodes/storage.d.ts.map +1 -1
  48. package/dest/avm/opcodes/storage.js +1 -7
  49. package/dest/avm/serialization/bytecode_serialization.js +2 -2
  50. package/dest/client/client_execution_context.d.ts +11 -19
  51. package/dest/client/client_execution_context.d.ts.map +1 -1
  52. package/dest/client/client_execution_context.js +26 -43
  53. package/dest/client/db_oracle.d.ts +7 -1
  54. package/dest/client/db_oracle.d.ts.map +1 -1
  55. package/dest/client/execution_note_cache.d.ts +0 -17
  56. package/dest/client/execution_note_cache.d.ts.map +1 -1
  57. package/dest/client/execution_note_cache.js +1 -24
  58. package/dest/client/execution_result.d.ts +8 -4
  59. package/dest/client/execution_result.d.ts.map +1 -1
  60. package/dest/client/execution_result.js +16 -6
  61. package/dest/client/private_execution.d.ts +2 -3
  62. package/dest/client/private_execution.d.ts.map +1 -1
  63. package/dest/client/private_execution.js +20 -9
  64. package/dest/client/simulator.d.ts +0 -16
  65. package/dest/client/simulator.d.ts.map +1 -1
  66. package/dest/client/simulator.js +8 -25
  67. package/dest/client/unconstrained_execution.d.ts +2 -3
  68. package/dest/client/unconstrained_execution.d.ts.map +1 -1
  69. package/dest/client/unconstrained_execution.js +5 -7
  70. package/dest/client/view_data_oracle.d.ts +2 -0
  71. package/dest/client/view_data_oracle.d.ts.map +1 -1
  72. package/dest/client/view_data_oracle.js +7 -1
  73. package/dest/index.d.ts +3 -1
  74. package/dest/index.d.ts.map +1 -1
  75. package/dest/index.js +4 -2
  76. package/dest/mocks/fixtures.d.ts +14 -10
  77. package/dest/mocks/fixtures.d.ts.map +1 -1
  78. package/dest/mocks/fixtures.js +22 -16
  79. package/dest/{simulator → providers}/acvm_native.d.ts.map +1 -1
  80. package/dest/{simulator → providers}/acvm_native.js +13 -14
  81. package/dest/providers/acvm_wasm.d.ts.map +1 -0
  82. package/dest/providers/acvm_wasm.js +15 -0
  83. package/dest/providers/index.d.ts.map +1 -0
  84. package/dest/{simulator → providers}/index.js +1 -1
  85. package/dest/{simulator → providers}/simulation_provider.d.ts.map +1 -1
  86. package/dest/{simulator → providers}/simulation_provider.js +1 -1
  87. package/dest/public/abstract_phase_manager.d.ts +43 -42
  88. package/dest/public/abstract_phase_manager.d.ts.map +1 -1
  89. package/dest/public/abstract_phase_manager.js +77 -106
  90. package/dest/public/app_logic_phase_manager.d.ts +6 -7
  91. package/dest/public/app_logic_phase_manager.d.ts.map +1 -1
  92. package/dest/public/app_logic_phase_manager.js +14 -14
  93. package/dest/public/{db.d.ts → db_interfaces.d.ts} +3 -3
  94. package/dest/public/db_interfaces.d.ts.map +1 -0
  95. package/dest/public/db_interfaces.js +2 -0
  96. package/dest/public/execution.d.ts +16 -9
  97. package/dest/public/execution.d.ts.map +1 -1
  98. package/dest/public/execution.js +1 -1
  99. package/dest/public/executor.d.ts +3 -30
  100. package/dest/public/executor.d.ts.map +1 -1
  101. package/dest/public/executor.js +32 -282
  102. package/dest/public/fee_payment.d.ts +11 -0
  103. package/dest/public/fee_payment.d.ts.map +1 -0
  104. package/dest/public/fee_payment.js +24 -0
  105. package/dest/public/hints_builder.d.ts +1 -0
  106. package/dest/public/hints_builder.d.ts.map +1 -1
  107. package/dest/public/hints_builder.js +6 -2
  108. package/dest/public/index.d.ts +4 -2
  109. package/dest/public/index.d.ts.map +1 -1
  110. package/dest/public/index.js +5 -3
  111. package/dest/public/phase_manager_factory.d.ts +1 -1
  112. package/dest/public/phase_manager_factory.d.ts.map +1 -1
  113. package/dest/public/{public_executor.d.ts → public_db_sources.d.ts} +3 -2
  114. package/dest/public/public_db_sources.d.ts.map +1 -0
  115. package/dest/public/public_db_sources.js +198 -0
  116. package/dest/public/public_kernel.js +14 -14
  117. package/dest/public/public_processor.d.ts +6 -4
  118. package/dest/public/public_processor.d.ts.map +1 -1
  119. package/dest/public/public_processor.js +46 -27
  120. package/dest/public/setup_phase_manager.d.ts +6 -8
  121. package/dest/public/setup_phase_manager.d.ts.map +1 -1
  122. package/dest/public/setup_phase_manager.js +11 -20
  123. package/dest/public/tail_phase_manager.d.ts +4 -10
  124. package/dest/public/tail_phase_manager.d.ts.map +1 -1
  125. package/dest/public/tail_phase_manager.js +15 -35
  126. package/dest/public/teardown_phase_manager.d.ts +6 -8
  127. package/dest/public/teardown_phase_manager.d.ts.map +1 -1
  128. package/dest/public/teardown_phase_manager.js +15 -21
  129. package/dest/public/transitional_adaptors.d.ts +1 -1
  130. package/dest/public/transitional_adaptors.d.ts.map +1 -1
  131. package/dest/public/transitional_adaptors.js +8 -6
  132. package/dest/public/utils.d.ts +2 -2
  133. package/dest/public/utils.d.ts.map +1 -1
  134. package/dest/public/utils.js +21 -14
  135. package/package.json +12 -9
  136. package/src/acvm/acvm.ts +0 -3
  137. package/src/acvm/oracle/oracle.ts +36 -13
  138. package/src/acvm/oracle/typed_oracle.ts +12 -3
  139. package/src/acvm/serialize.ts +1 -1
  140. package/src/avm/avm_gas.ts +66 -73
  141. package/src/avm/avm_simulator.ts +9 -0
  142. package/src/avm/fixtures/index.ts +20 -0
  143. package/src/avm/index.ts +1 -0
  144. package/src/avm/journal/host_storage.ts +1 -1
  145. package/src/avm/journal/journal.ts +6 -1
  146. package/src/avm/journal/trace.ts +12 -0
  147. package/src/avm/journal/trace_types.ts +1 -0
  148. package/src/avm/opcodes/arithmetic.ts +1 -14
  149. package/src/avm/opcodes/conversion.ts +1 -1
  150. package/src/avm/opcodes/external_calls.ts +3 -1
  151. package/src/avm/opcodes/instruction.ts +5 -3
  152. package/src/avm/opcodes/memory.ts +1 -8
  153. package/src/avm/opcodes/storage.ts +1 -8
  154. package/src/avm/serialization/bytecode_serialization.ts +1 -1
  155. package/src/client/client_execution_context.ts +33 -45
  156. package/src/client/db_oracle.ts +8 -1
  157. package/src/client/execution_note_cache.ts +0 -28
  158. package/src/client/execution_result.ts +21 -7
  159. package/src/client/private_execution.ts +36 -21
  160. package/src/client/simulator.ts +7 -27
  161. package/src/client/unconstrained_execution.ts +9 -12
  162. package/src/client/view_data_oracle.ts +8 -0
  163. package/src/index.ts +3 -1
  164. package/src/mocks/fixtures.ts +30 -32
  165. package/src/{simulator → providers}/acvm_native.ts +21 -19
  166. package/src/{simulator → providers}/acvm_wasm.ts +2 -16
  167. package/src/public/abstract_phase_manager.ts +138 -205
  168. package/src/public/app_logic_phase_manager.ts +23 -39
  169. package/src/public/{db.ts → db_interfaces.ts} +2 -2
  170. package/src/public/execution.ts +15 -8
  171. package/src/public/executor.ts +51 -398
  172. package/src/public/fee_payment.ts +26 -0
  173. package/src/public/hints_builder.ts +6 -0
  174. package/src/public/index.ts +4 -2
  175. package/src/public/phase_manager_factory.ts +1 -1
  176. package/src/public/{public_executor.ts → public_db_sources.ts} +3 -2
  177. package/src/public/public_kernel.ts +24 -24
  178. package/src/public/public_processor.ts +77 -33
  179. package/src/public/setup_phase_manager.ts +20 -45
  180. package/src/public/tail_phase_manager.ts +17 -65
  181. package/src/public/teardown_phase_manager.ts +22 -41
  182. package/src/public/transitional_adaptors.ts +7 -11
  183. package/src/public/utils.ts +20 -14
  184. package/dest/public/db.d.ts.map +0 -1
  185. package/dest/public/db.js +0 -2
  186. package/dest/public/public_execution_context.d.ts +0 -121
  187. package/dest/public/public_execution_context.d.ts.map +0 -1
  188. package/dest/public/public_execution_context.js +0 -214
  189. package/dest/public/public_executor.d.ts.map +0 -1
  190. package/dest/public/public_executor.js +0 -197
  191. package/dest/public/state_actions.d.ts +0 -39
  192. package/dest/public/state_actions.d.ts.map +0 -1
  193. package/dest/public/state_actions.js +0 -80
  194. package/dest/simulator/acvm_wasm.d.ts.map +0 -1
  195. package/dest/simulator/acvm_wasm.js +0 -22
  196. package/dest/simulator/index.d.ts.map +0 -1
  197. package/src/public/public_execution_context.ts +0 -289
  198. package/src/public/state_actions.ts +0 -102
  199. /package/dest/{simulator → providers}/acvm_native.d.ts +0 -0
  200. /package/dest/{simulator → providers}/acvm_wasm.d.ts +0 -0
  201. /package/dest/{simulator → providers}/index.d.ts +0 -0
  202. /package/dest/{simulator → providers}/simulation_provider.d.ts +0 -0
  203. /package/src/{simulator → providers}/index.ts +0 -0
  204. /package/src/{simulator → providers}/simulation_provider.ts +0 -0
@@ -8,18 +8,18 @@ import {
8
8
  import { createDebugLogger } from '@aztec/foundation/log';
9
9
  import { elapsed } from '@aztec/foundation/timer';
10
10
  import {
11
- PublicKernelAppLogicArtifact,
12
- PublicKernelSetupArtifact,
13
- PublicKernelTailArtifact,
14
- PublicKernelTeardownArtifact,
15
- convertPublicInnerRollupInputsToWitnessMap,
16
- convertPublicInnerRollupOutputFromWitnessMap,
17
- convertPublicSetupRollupInputsToWitnessMap,
18
- convertPublicSetupRollupOutputFromWitnessMap,
19
- convertPublicTailInputsToWitnessMap,
20
- convertPublicTailOutputFromWitnessMap,
21
- convertPublicTeardownRollupInputsToWitnessMap,
22
- convertPublicTeardownRollupOutputFromWitnessMap,
11
+ SimulatedPublicKernelAppLogicArtifact,
12
+ SimulatedPublicKernelSetupArtifact,
13
+ SimulatedPublicKernelTailArtifact,
14
+ SimulatedPublicKernelTeardownArtifact,
15
+ convertSimulatedPublicInnerInputsToWitnessMap,
16
+ convertSimulatedPublicInnerOutputFromWitnessMap,
17
+ convertSimulatedPublicSetupInputsToWitnessMap,
18
+ convertSimulatedPublicSetupOutputFromWitnessMap,
19
+ convertSimulatedPublicTailInputsToWitnessMap,
20
+ convertSimulatedPublicTailOutputFromWitnessMap,
21
+ convertSimulatedPublicTeardownInputsToWitnessMap,
22
+ convertSimulatedPublicTeardownOutputFromWitnessMap,
23
23
  } from '@aztec/noir-protocol-circuits-types';
24
24
  import { type SimulationProvider, WASMSimulator } from '@aztec/simulator';
25
25
 
@@ -47,11 +47,11 @@ export class RealPublicKernelCircuitSimulator implements PublicKernelCircuitSimu
47
47
  if (!input.previousKernel.publicInputs.needsSetup) {
48
48
  throw new Error(`Expected previous kernel inputs to need setup`);
49
49
  }
50
- const inputWitness = convertPublicSetupRollupInputsToWitnessMap(input);
50
+ const inputWitness = convertSimulatedPublicSetupInputsToWitnessMap(input);
51
51
  const [duration, witness] = await elapsed(() =>
52
- this.wasmSimulator.simulateCircuit(inputWitness, PublicKernelSetupArtifact),
52
+ this.wasmSimulator.simulateCircuit(inputWitness, SimulatedPublicKernelSetupArtifact),
53
53
  );
54
- const result = convertPublicSetupRollupOutputFromWitnessMap(witness);
54
+ const result = convertSimulatedPublicSetupOutputFromWitnessMap(witness);
55
55
  this.log.debug(`Simulated public kernel setup circuit`, {
56
56
  eventName: 'circuit-simulation',
57
57
  circuitName: 'public-kernel-setup',
@@ -73,11 +73,11 @@ export class RealPublicKernelCircuitSimulator implements PublicKernelCircuitSimu
73
73
  if (!input.previousKernel.publicInputs.needsAppLogic) {
74
74
  throw new Error(`Expected previous kernel inputs to need app logic`);
75
75
  }
76
- const inputWitness = convertPublicInnerRollupInputsToWitnessMap(input);
76
+ const inputWitness = convertSimulatedPublicInnerInputsToWitnessMap(input);
77
77
  const [duration, witness] = await elapsed(() =>
78
- this.wasmSimulator.simulateCircuit(inputWitness, PublicKernelAppLogicArtifact),
78
+ this.wasmSimulator.simulateCircuit(inputWitness, SimulatedPublicKernelAppLogicArtifact),
79
79
  );
80
- const result = convertPublicInnerRollupOutputFromWitnessMap(witness);
80
+ const result = convertSimulatedPublicInnerOutputFromWitnessMap(witness);
81
81
  this.log.debug(`Simulated public kernel app logic circuit`, {
82
82
  eventName: 'circuit-simulation',
83
83
  circuitName: 'public-kernel-app-logic',
@@ -99,11 +99,11 @@ export class RealPublicKernelCircuitSimulator implements PublicKernelCircuitSimu
99
99
  if (!input.previousKernel.publicInputs.needsTeardown) {
100
100
  throw new Error(`Expected previous kernel inputs to need teardown`);
101
101
  }
102
- const inputWitness = convertPublicTeardownRollupInputsToWitnessMap(input);
102
+ const inputWitness = convertSimulatedPublicTeardownInputsToWitnessMap(input);
103
103
  const [duration, witness] = await elapsed(() =>
104
- this.wasmSimulator.simulateCircuit(inputWitness, PublicKernelTeardownArtifact),
104
+ this.wasmSimulator.simulateCircuit(inputWitness, SimulatedPublicKernelTeardownArtifact),
105
105
  );
106
- const result = convertPublicTeardownRollupOutputFromWitnessMap(witness);
106
+ const result = convertSimulatedPublicTeardownOutputFromWitnessMap(witness);
107
107
  this.log.debug(`Simulated public kernel teardown circuit`, {
108
108
  eventName: 'circuit-simulation',
109
109
  circuitName: 'public-kernel-teardown',
@@ -122,11 +122,11 @@ export class RealPublicKernelCircuitSimulator implements PublicKernelCircuitSimu
122
122
  public async publicKernelCircuitTail(
123
123
  input: PublicKernelTailCircuitPrivateInputs,
124
124
  ): Promise<KernelCircuitPublicInputs> {
125
- const inputWitness = convertPublicTailInputsToWitnessMap(input);
125
+ const inputWitness = convertSimulatedPublicTailInputsToWitnessMap(input);
126
126
  const [duration, witness] = await elapsed(() =>
127
- this.wasmSimulator.simulateCircuit(inputWitness, PublicKernelTailArtifact),
127
+ this.wasmSimulator.simulateCircuit(inputWitness, SimulatedPublicKernelTailArtifact),
128
128
  );
129
- const result = convertPublicTailOutputFromWitnessMap(witness);
129
+ const result = convertSimulatedPublicTailOutputFromWitnessMap(witness);
130
130
  this.log.debug(`Simulated public kernel tail circuit`, {
131
131
  eventName: 'circuit-simulation',
132
132
  circuitName: 'public-kernel-tail',
@@ -3,20 +3,32 @@ import {
3
3
  type FailedTx,
4
4
  NestedProcessReturnValues,
5
5
  type ProcessedTx,
6
- type PublicKernelRequest,
6
+ type PublicProvingRequest,
7
7
  type SimulationError,
8
8
  Tx,
9
9
  type TxValidator,
10
- makeEmptyProcessedTx,
11
10
  makeProcessedTx,
12
- toTxEffect,
13
11
  validateProcessedTx,
14
12
  } from '@aztec/circuit-types';
15
- import { type TxSequencerProcessingStats } from '@aztec/circuit-types/stats';
16
- import { type GlobalVariables, type Header, type KernelCircuitPublicInputs } from '@aztec/circuits.js';
13
+ import {
14
+ AztecAddress,
15
+ GAS_TOKEN_ADDRESS,
16
+ type GlobalVariables,
17
+ type Header,
18
+ type KernelCircuitPublicInputs,
19
+ MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
20
+ PROTOCOL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
21
+ PublicDataUpdateRequest,
22
+ } from '@aztec/circuits.js';
23
+ import { times } from '@aztec/foundation/collection';
17
24
  import { createDebugLogger } from '@aztec/foundation/log';
18
- import { Timer } from '@aztec/foundation/timer';
19
- import { PublicExecutor, type PublicStateDB, type SimulationProvider } from '@aztec/simulator';
25
+ import {
26
+ PublicExecutor,
27
+ type PublicStateDB,
28
+ type SimulationProvider,
29
+ computeFeePayerBalanceLeafSlot,
30
+ computeFeePayerBalanceStorageSlot,
31
+ } from '@aztec/simulator';
20
32
  import { type ContractDataSource } from '@aztec/types/contracts';
21
33
  import { type MerkleTreeOperations } from '@aztec/world-state';
22
34
 
@@ -26,7 +38,7 @@ import {
26
38
  publicKernelPhaseToKernelType,
27
39
  } from './abstract_phase_manager.js';
28
40
  import { PhaseManagerFactory } from './phase_manager_factory.js';
29
- import { ContractsDataSourcePublicDB, WorldStateDB, WorldStatePublicDB } from './public_executor.js';
41
+ import { ContractsDataSourcePublicDB, WorldStateDB, WorldStatePublicDB } from './public_db_sources.js';
30
42
  import { RealPublicKernelCircuitSimulator } from './public_kernel.js';
31
43
  import { type PublicKernelCircuitSimulator } from './public_kernel_circuit_simulator.js';
32
44
 
@@ -101,7 +113,7 @@ export class PublicProcessor {
101
113
  txs = txs.map(tx => Tx.clone(tx));
102
114
  const result: ProcessedTx[] = [];
103
115
  const failed: FailedTx[] = [];
104
- const returns: NestedProcessReturnValues[] = [];
116
+ let returns: NestedProcessReturnValues[] = [];
105
117
 
106
118
  for (const tx of txs) {
107
119
  // only process up to the limit of the block
@@ -112,7 +124,14 @@ export class PublicProcessor {
112
124
  const [processedTx, returnValues] = !tx.hasPublicCalls()
113
125
  ? [makeProcessedTx(tx, tx.data.toKernelCircuitPublicInputs(), tx.proof, [])]
114
126
  : await this.processTxWithPublicCalls(tx);
127
+
128
+ // Set fee payment update request into the processed tx
129
+ processedTx.finalPublicDataUpdateRequests = await this.createFinalDataUpdateRequests(processedTx);
130
+
131
+ // Commit the state updates from this transaction
132
+ await this.publicStateDB.commit();
115
133
  validateProcessedTx(processedTx);
134
+
116
135
  // Re-validate the transaction
117
136
  if (txValidator) {
118
137
  // Only accept processed transactions that are not double-spends,
@@ -129,10 +148,10 @@ export class PublicProcessor {
129
148
  await blockProver.addNewTx(processedTx);
130
149
  }
131
150
  result.push(processedTx);
132
- returns.push(returnValues?.[0] ?? new NestedProcessReturnValues([]));
151
+ returns = returns.concat(returnValues ?? []);
133
152
  } catch (err: any) {
134
153
  const errorMessage = err instanceof Error ? err.message : 'Unknown error';
135
- this.log.warn(`Failed to process tx ${tx.getTxHash()}: ${errorMessage}`);
154
+ this.log.warn(`Failed to process tx ${tx.getTxHash()}: ${errorMessage} ${err?.stack}`);
136
155
 
137
156
  failed.push({
138
157
  tx,
@@ -146,17 +165,55 @@ export class PublicProcessor {
146
165
  }
147
166
 
148
167
  /**
149
- * Makes an empty processed tx. Useful for padding a block to a power of two number of txs.
150
- * @returns A processed tx with empty data.
168
+ * Creates the final set of data update requests for the transaction. This includes the
169
+ * set of public data update requests as returned by the public kernel, plus a data update
170
+ * request for updating fee balance. It also updates the local public state db.
171
+ * See build_or_patch_payment_update_request in base_rollup_inputs.nr for more details.
151
172
  */
152
- public makeEmptyProcessedTx(): ProcessedTx {
153
- const { chainId, version } = this.globalVariables;
154
- return makeEmptyProcessedTx(this.historicalHeader.clone(), chainId, version);
173
+ private async createFinalDataUpdateRequests(tx: ProcessedTx) {
174
+ const finalPublicDataUpdateRequests = [
175
+ ...tx.data.end.publicDataUpdateRequests,
176
+ ...times(PROTOCOL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, () => PublicDataUpdateRequest.empty()),
177
+ ];
178
+
179
+ const feePayer = tx.data.feePayer;
180
+ if (feePayer.isZero()) {
181
+ return finalPublicDataUpdateRequests;
182
+ }
183
+
184
+ const gasToken = AztecAddress.fromBigInt(GAS_TOKEN_ADDRESS);
185
+ const balanceSlot = computeFeePayerBalanceStorageSlot(feePayer);
186
+ const leafSlot = computeFeePayerBalanceLeafSlot(feePayer);
187
+ const txFee = tx.data.getTransactionFee(this.globalVariables.gasFees);
188
+
189
+ this.log.debug(`Deducting ${txFee} balance in gas tokens for ${feePayer}`);
190
+
191
+ const existingBalanceWriteIndex = finalPublicDataUpdateRequests.findIndex(request =>
192
+ request.leafSlot.equals(leafSlot),
193
+ );
194
+
195
+ const balance =
196
+ existingBalanceWriteIndex > -1
197
+ ? finalPublicDataUpdateRequests[existingBalanceWriteIndex].newValue
198
+ : await this.publicStateDB.storageRead(gasToken, balanceSlot);
199
+
200
+ if (balance.lt(txFee)) {
201
+ throw new Error(`Not enough balance for fee payer to pay for transaction (got ${balance} needs ${txFee})`);
202
+ }
203
+
204
+ const updatedBalance = balance.sub(txFee);
205
+ await this.publicStateDB.storageWrite(gasToken, balanceSlot, updatedBalance);
206
+
207
+ finalPublicDataUpdateRequests[
208
+ existingBalanceWriteIndex > -1 ? existingBalanceWriteIndex : MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX
209
+ ] = new PublicDataUpdateRequest(leafSlot, updatedBalance, 0);
210
+
211
+ return finalPublicDataUpdateRequests;
155
212
  }
156
213
 
157
214
  private async processTxWithPublicCalls(tx: Tx): Promise<[ProcessedTx, NestedProcessReturnValues[]]> {
158
215
  let returnValues: NestedProcessReturnValues[] = [];
159
- const publicRequests: PublicKernelRequest[] = [];
216
+ const publicProvingRequests: PublicProvingRequest[] = [];
160
217
  let phase: AbstractPhaseManager | undefined = PhaseManagerFactory.phaseFromTx(
161
218
  tx,
162
219
  this.db,
@@ -168,22 +225,19 @@ export class PublicProcessor {
168
225
  this.publicStateDB,
169
226
  );
170
227
  this.log.debug(`Beginning processing in phase ${phase?.phase} for tx ${tx.getTxHash()}`);
171
- let proof = tx.proof;
172
228
  let publicKernelPublicInput = tx.data.toPublicKernelCircuitPublicInputs();
173
229
  let finalKernelOutput: KernelCircuitPublicInputs | undefined;
174
230
  let revertReason: SimulationError | undefined;
175
- const timer = new Timer();
176
231
  const gasUsed: ProcessedTx['gasUsed'] = {};
177
232
  while (phase) {
178
- const output = await phase.handle(tx, publicKernelPublicInput, proof);
233
+ const output = await phase.handle(tx, publicKernelPublicInput);
179
234
  gasUsed[publicKernelPhaseToKernelType(phase.phase)] = output.gasUsed;
180
235
  if (phase.phase === PublicKernelPhase.APP_LOGIC) {
181
236
  returnValues = output.returnValues;
182
237
  }
183
- publicRequests.push(...output.kernelRequests);
238
+ publicProvingRequests.push(...output.publicProvingRequests);
184
239
  publicKernelPublicInput = output.publicKernelOutput;
185
240
  finalKernelOutput = output.finalKernelOutput;
186
- proof = output.publicKernelProof;
187
241
  revertReason ??= output.revertReason;
188
242
  phase = PhaseManagerFactory.phaseFromOutput(
189
243
  publicKernelPublicInput,
@@ -202,17 +256,7 @@ export class PublicProcessor {
202
256
  throw new Error('Final public kernel was not executed.');
203
257
  }
204
258
 
205
- const processedTx = makeProcessedTx(tx, finalKernelOutput, proof, publicRequests, revertReason, gasUsed);
206
-
207
- this.log.debug(`Processed public part of ${tx.getTxHash()}`, {
208
- eventName: 'tx-sequencer-processing',
209
- duration: timer.ms(),
210
- effectsSize: toTxEffect(processedTx).toBuffer().length,
211
- publicDataUpdateRequests:
212
- processedTx.data.end.publicDataUpdateRequests.filter(x => !x.leafSlot.isZero()).length ?? 0,
213
- ...tx.getStats(),
214
- } satisfies TxSequencerProcessingStats);
215
-
259
+ const processedTx = makeProcessedTx(tx, finalKernelOutput, tx.proof, publicProvingRequests, revertReason, gasUsed);
216
260
  return [processedTx, returnValues];
217
261
  }
218
262
  }
@@ -1,15 +1,10 @@
1
- import { type PublicKernelRequest, PublicKernelType, type Tx } from '@aztec/circuit-types';
2
- import {
3
- type GlobalVariables,
4
- type Header,
5
- type Proof,
6
- type PublicKernelCircuitPublicInputs,
7
- } from '@aztec/circuits.js';
1
+ import { PublicKernelType, type PublicProvingRequest, type Tx } from '@aztec/circuit-types';
2
+ import { type GlobalVariables, type Header, type PublicKernelCircuitPublicInputs } from '@aztec/circuits.js';
8
3
  import { type PublicExecutor, type PublicStateDB } from '@aztec/simulator';
9
4
  import { type MerkleTreeOperations } from '@aztec/world-state';
10
5
 
11
- import { AbstractPhaseManager, PublicKernelPhase } from './abstract_phase_manager.js';
12
- import { type ContractsDataSourcePublicDB } from './public_executor.js';
6
+ import { AbstractPhaseManager, PublicKernelPhase, makeAvmProvingRequest } from './abstract_phase_manager.js';
7
+ import { type ContractsDataSourcePublicDB } from './public_db_sources.js';
13
8
  import { type PublicKernelCircuitSimulator } from './public_kernel_circuit_simulator.js';
14
9
 
15
10
  /**
@@ -29,46 +24,26 @@ export class SetupPhaseManager extends AbstractPhaseManager {
29
24
  super(db, publicExecutor, publicKernel, globalVariables, historicalHeader, phase);
30
25
  }
31
26
 
32
- override async handle(
33
- tx: Tx,
34
- previousPublicKernelOutput: PublicKernelCircuitPublicInputs,
35
- previousPublicKernelProof: Proof,
36
- ) {
27
+ override async handle(tx: Tx, previousPublicKernelOutput: PublicKernelCircuitPublicInputs) {
37
28
  this.log.verbose(`Processing tx ${tx.getTxHash()}`);
38
- const [
39
- kernelInputs,
40
- publicKernelOutput,
41
- publicKernelProof,
42
- newUnencryptedFunctionLogs,
43
- revertReason,
44
- _returnValues,
45
- gasUsed,
46
- ] = await this.processEnqueuedPublicCalls(tx, previousPublicKernelOutput, previousPublicKernelProof).catch(
47
- // the abstract phase manager throws if simulation gives error in a non-revertible phase
48
- async err => {
49
- await this.publicStateDB.rollbackToCommit();
50
- throw err;
51
- },
52
- );
53
- tx.unencryptedLogs.addFunctionLogs(newUnencryptedFunctionLogs);
29
+ // TODO(#6464): Should we allow emitting contracts in the private setup phase?
30
+ // if so, this should only add contracts that were deployed during private app logic.
31
+ await this.publicContractsDB.addNewContracts(tx);
32
+ const { publicProvingInformation, kernelOutput, newUnencryptedLogs, revertReason, gasUsed } =
33
+ await this.processEnqueuedPublicCalls(tx, previousPublicKernelOutput).catch(
34
+ // the abstract phase manager throws if simulation gives error in a non-revertible phase
35
+ async err => {
36
+ await this.publicStateDB.rollbackToCommit();
37
+ throw err;
38
+ },
39
+ );
40
+ tx.unencryptedLogs.addFunctionLogs(newUnencryptedLogs);
54
41
  await this.publicStateDB.checkpoint();
55
42
 
56
43
  // Return a list of setup proving requests
57
- const kernelRequests = kernelInputs.map(input => {
58
- const request: PublicKernelRequest = {
59
- type: PublicKernelType.SETUP,
60
- inputs: input,
61
- };
62
- return request;
44
+ const publicProvingRequests: PublicProvingRequest[] = publicProvingInformation.map(info => {
45
+ return makeAvmProvingRequest(info, PublicKernelType.SETUP);
63
46
  });
64
- return {
65
- kernelRequests,
66
- kernelInputs,
67
- publicKernelOutput,
68
- publicKernelProof,
69
- revertReason,
70
- returnValues: [],
71
- gasUsed,
72
- };
47
+ return { publicProvingRequests, publicKernelOutput: kernelOutput, revertReason, returnValues: [], gasUsed };
73
48
  }
74
49
  }
@@ -1,28 +1,20 @@
1
1
  import { type PublicKernelRequest, PublicKernelType, type Tx } from '@aztec/circuit-types';
2
2
  import {
3
- type Fr,
3
+ CombineHints,
4
4
  type GlobalVariables,
5
5
  type Header,
6
6
  type KernelCircuitPublicInputs,
7
- type LogHash,
8
- MAX_NEW_NOTE_HASHES_PER_TX,
9
7
  MAX_NEW_NULLIFIERS_PER_TX,
10
8
  MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
11
- type MAX_UNENCRYPTED_LOGS_PER_TX,
12
- type NoteHash,
13
- type Proof,
14
9
  type PublicKernelCircuitPublicInputs,
15
10
  PublicKernelTailCircuitPrivateInputs,
16
- makeEmptyProof,
17
11
  mergeAccumulatedData,
18
- sortByCounter,
19
12
  } from '@aztec/circuits.js';
20
- import { type Tuple } from '@aztec/foundation/serialize';
21
13
  import { type PublicExecutor, type PublicStateDB } from '@aztec/simulator';
22
14
  import { type MerkleTreeOperations } from '@aztec/world-state';
23
15
 
24
16
  import { AbstractPhaseManager, PublicKernelPhase } from './abstract_phase_manager.js';
25
- import { type ContractsDataSourcePublicDB } from './public_executor.js';
17
+ import { type ContractsDataSourcePublicDB } from './public_db_sources.js';
26
18
  import { type PublicKernelCircuitSimulator } from './public_kernel_circuit_simulator.js';
27
19
 
28
20
  export class TailPhaseManager extends AbstractPhaseManager {
@@ -39,80 +31,46 @@ export class TailPhaseManager extends AbstractPhaseManager {
39
31
  super(db, publicExecutor, publicKernel, globalVariables, historicalHeader, phase);
40
32
  }
41
33
 
42
- override async handle(
43
- tx: Tx,
44
- previousPublicKernelOutput: PublicKernelCircuitPublicInputs,
45
- previousPublicKernelProof: Proof,
46
- ) {
34
+ override async handle(tx: Tx, previousPublicKernelOutput: PublicKernelCircuitPublicInputs) {
47
35
  this.log.verbose(`Processing tx ${tx.getTxHash()}`);
48
- const [inputs, finalKernelOutput] = await this.runTailKernelCircuit(
49
- previousPublicKernelOutput,
50
- previousPublicKernelProof,
51
- ).catch(
36
+ const [inputs, finalKernelOutput] = await this.simulate(previousPublicKernelOutput).catch(
52
37
  // the abstract phase manager throws if simulation gives error in non-revertible phase
53
38
  async err => {
54
39
  await this.publicStateDB.rollbackToCommit();
55
40
  throw err;
56
41
  },
57
42
  );
58
- // commit the state updates from this transaction
59
- await this.publicStateDB.commit();
60
43
 
61
44
  // Return a tail proving request
62
- const request: PublicKernelRequest = {
45
+ const kernelRequest: PublicKernelRequest = {
63
46
  type: PublicKernelType.TAIL,
64
47
  inputs: inputs,
65
48
  };
66
49
 
67
50
  return {
68
- kernelRequests: [request],
51
+ publicProvingRequests: [kernelRequest],
69
52
  publicKernelOutput: previousPublicKernelOutput,
70
53
  finalKernelOutput,
71
- publicKernelProof: makeEmptyProof(),
72
- revertReason: undefined,
73
54
  returnValues: [],
74
- gasUsed: undefined,
75
55
  };
76
56
  }
77
57
 
78
- private async runTailKernelCircuit(
79
- previousOutput: PublicKernelCircuitPublicInputs,
80
- previousProof: Proof,
81
- ): Promise<[PublicKernelTailCircuitPrivateInputs, KernelCircuitPublicInputs]> {
82
- // Temporary hack. Should sort them in the tail circuit.
83
- previousOutput.end.unencryptedLogsHashes = this.sortLogsHashes<typeof MAX_UNENCRYPTED_LOGS_PER_TX>(
84
- previousOutput.end.unencryptedLogsHashes,
85
- );
86
- const [inputs, output] = await this.simulate(previousOutput, previousProof);
87
-
88
- // Temporary hack. Should sort them in the tail circuit.
89
- const noteHashes = mergeAccumulatedData(
90
- previousOutput.endNonRevertibleData.newNoteHashes,
91
- previousOutput.end.newNoteHashes,
92
- MAX_NEW_NOTE_HASHES_PER_TX,
93
- );
94
- output.end.newNoteHashes = this.sortNoteHashes<typeof MAX_NEW_NOTE_HASHES_PER_TX>(noteHashes);
95
-
96
- return [inputs, output];
97
- }
98
-
99
58
  private async simulate(
100
59
  previousOutput: PublicKernelCircuitPublicInputs,
101
- previousProof: Proof,
102
60
  ): Promise<[PublicKernelTailCircuitPrivateInputs, KernelCircuitPublicInputs]> {
103
- const inputs = await this.buildPrivateInputs(previousOutput, previousProof);
61
+ const inputs = await this.buildPrivateInputs(previousOutput);
104
62
  // We take a deep copy (clone) of these to pass to the prover
105
63
  return [inputs.clone(), await this.publicKernel.publicKernelCircuitTail(inputs)];
106
64
  }
107
65
 
108
- private async buildPrivateInputs(previousOutput: PublicKernelCircuitPublicInputs, previousProof: Proof) {
109
- const previousKernel = this.getPreviousKernelData(previousOutput, previousProof);
66
+ private async buildPrivateInputs(previousOutput: PublicKernelCircuitPublicInputs) {
67
+ const previousKernel = this.getPreviousKernelData(previousOutput);
110
68
 
111
- const { validationRequests, endNonRevertibleData, end } = previousOutput;
69
+ const { validationRequests, endNonRevertibleData: nonRevertibleData, end: revertibleData } = previousOutput;
112
70
 
113
71
  const pendingNullifiers = mergeAccumulatedData(
114
- endNonRevertibleData.newNullifiers,
115
- end.newNullifiers,
72
+ nonRevertibleData.newNullifiers,
73
+ revertibleData.newNullifiers,
116
74
  MAX_NEW_NULLIFIERS_PER_TX,
117
75
  );
118
76
 
@@ -127,8 +85,8 @@ export class TailPhaseManager extends AbstractPhaseManager {
127
85
  );
128
86
 
129
87
  const pendingPublicDataWrites = mergeAccumulatedData(
130
- endNonRevertibleData.publicDataUpdateRequests,
131
- end.publicDataUpdateRequests,
88
+ nonRevertibleData.publicDataUpdateRequests,
89
+ revertibleData.publicDataUpdateRequests,
132
90
  MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
133
91
  );
134
92
 
@@ -145,6 +103,8 @@ export class TailPhaseManager extends AbstractPhaseManager {
145
103
 
146
104
  const currentState = await this.db.getStateReference();
147
105
 
106
+ const hints = CombineHints.fromPublicData({ nonRevertibleData, revertibleData });
107
+
148
108
  return new PublicKernelTailCircuitPrivateInputs(
149
109
  previousKernel,
150
110
  nullifierReadRequestHints,
@@ -152,15 +112,7 @@ export class TailPhaseManager extends AbstractPhaseManager {
152
112
  publicDataHints,
153
113
  publicDataReadRequestHints,
154
114
  currentState.partial,
115
+ hints,
155
116
  );
156
117
  }
157
-
158
- private sortNoteHashes<N extends number>(noteHashes: Tuple<NoteHash, N>): Tuple<Fr, N> {
159
- return sortByCounter(noteHashes).map(n => n.value) as Tuple<Fr, N>;
160
- }
161
-
162
- private sortLogsHashes<N extends number>(unencryptedLogsHashes: Tuple<LogHash, N>): Tuple<LogHash, N> {
163
- // TODO(6052): logs here may have duplicate counters from nested calls
164
- return sortByCounter(unencryptedLogsHashes);
165
- }
166
118
  }
@@ -1,10 +1,9 @@
1
- import { type PublicKernelRequest, PublicKernelType, type Tx } from '@aztec/circuit-types';
1
+ import { PublicKernelType, type PublicProvingRequest, type Tx } from '@aztec/circuit-types';
2
2
  import {
3
3
  type Fr,
4
4
  type Gas,
5
5
  type GlobalVariables,
6
6
  type Header,
7
- type Proof,
8
7
  type PublicKernelCircuitPublicInputs,
9
8
  } from '@aztec/circuits.js';
10
9
  import { type PublicExecutor, type PublicStateDB } from '@aztec/simulator';
@@ -12,8 +11,8 @@ import { type MerkleTreeOperations } from '@aztec/world-state';
12
11
 
13
12
  import { inspect } from 'util';
14
13
 
15
- import { AbstractPhaseManager, PublicKernelPhase } from './abstract_phase_manager.js';
16
- import { type ContractsDataSourcePublicDB } from './public_executor.js';
14
+ import { AbstractPhaseManager, PublicKernelPhase, makeAvmProvingRequest } from './abstract_phase_manager.js';
15
+ import { type ContractsDataSourcePublicDB } from './public_db_sources.js';
17
16
  import { type PublicKernelCircuitSimulator } from './public_kernel_circuit_simulator.js';
18
17
 
19
18
  /**
@@ -33,47 +32,29 @@ export class TeardownPhaseManager extends AbstractPhaseManager {
33
32
  super(db, publicExecutor, publicKernel, globalVariables, historicalHeader, phase);
34
33
  }
35
34
 
36
- override async handle(
37
- tx: Tx,
38
- previousPublicKernelOutput: PublicKernelCircuitPublicInputs,
39
- previousPublicKernelProof: Proof,
40
- ) {
35
+ override async handle(tx: Tx, previousPublicKernelOutput: PublicKernelCircuitPublicInputs) {
41
36
  this.log.verbose(`Processing tx ${tx.getTxHash()}`);
42
- const [
43
- kernelInputs,
44
- publicKernelOutput,
45
- publicKernelProof,
46
- newUnencryptedFunctionLogs,
47
- revertReason,
48
- _returnValues,
49
- gasUsed,
50
- ] = await this.processEnqueuedPublicCalls(tx, previousPublicKernelOutput, previousPublicKernelProof).catch(
51
- // the abstract phase manager throws if simulation gives error in a non-revertible phase
52
- async err => {
53
- await this.publicStateDB.rollbackToCommit();
54
- throw err;
55
- },
56
- );
57
- tx.unencryptedLogs.addFunctionLogs(newUnencryptedFunctionLogs);
58
- await this.publicStateDB.checkpoint();
37
+ const { publicProvingInformation, kernelOutput, newUnencryptedLogs, revertReason, gasUsed } =
38
+ await this.processEnqueuedPublicCalls(tx, previousPublicKernelOutput).catch(
39
+ // the abstract phase manager throws if simulation gives error in a non-revertible phase
40
+ async err => {
41
+ await this.publicStateDB.rollbackToCommit();
42
+ throw err;
43
+ },
44
+ );
45
+ if (revertReason) {
46
+ await this.publicStateDB.rollbackToCheckpoint();
47
+ } else {
48
+ // TODO(#6464): Should we allow emitting contracts in the public teardown phase?
49
+ // if so, we should insert them here
50
+ tx.unencryptedLogs.addFunctionLogs(newUnencryptedLogs);
51
+ }
59
52
 
60
53
  // Return a list of teardown proving requests
61
- const kernelRequests = kernelInputs.map(input => {
62
- const request: PublicKernelRequest = {
63
- type: PublicKernelType.TEARDOWN,
64
- inputs: input,
65
- };
66
- return request;
54
+ const publicProvingRequests: PublicProvingRequest[] = publicProvingInformation.map(info => {
55
+ return makeAvmProvingRequest(info, PublicKernelType.TEARDOWN);
67
56
  });
68
- return {
69
- kernelRequests,
70
- kernelInputs,
71
- publicKernelOutput,
72
- publicKernelProof,
73
- revertReason,
74
- returnValues: [],
75
- gasUsed,
76
- };
57
+ return { publicProvingRequests, publicKernelOutput: kernelOutput, revertReason, returnValues: [], gasUsed };
77
58
  }
78
59
 
79
60
  protected override getTransactionFee(tx: Tx, previousPublicKernelOutput: PublicKernelCircuitPublicInputs): Fr {
@@ -1,13 +1,6 @@
1
1
  // All code in this file needs to die once the public executor is phased out in favor of the AVM.
2
2
  import { UnencryptedFunctionL2Logs } from '@aztec/circuit-types';
3
- import {
4
- CallContext,
5
- FunctionData,
6
- type Gas,
7
- type GasSettings,
8
- type GlobalVariables,
9
- type Header,
10
- } from '@aztec/circuits.js';
3
+ import { CallContext, type Gas, type GasSettings, type GlobalVariables, type Header } from '@aztec/circuits.js';
11
4
  import { Fr } from '@aztec/foundation/fields';
12
5
 
13
6
  import { promisify } from 'util';
@@ -48,7 +41,7 @@ export function createAvmExecutionEnvironment(
48
41
  current.args,
49
42
  gasSettings,
50
43
  transactionFee,
51
- current.functionData.selector,
44
+ current.functionSelector,
52
45
  );
53
46
  }
54
47
 
@@ -65,12 +58,11 @@ export function createPublicExecution(
65
58
  isStaticCall: avmEnvironment.isStaticCall,
66
59
  sideEffectCounter: startSideEffectCounter,
67
60
  });
68
- const functionData = new FunctionData(avmEnvironment.temporaryFunctionSelector, /*isPrivate=*/ false);
69
61
  const execution: PublicExecution = {
70
62
  contractAddress: avmEnvironment.address,
71
63
  callContext,
72
64
  args: calldata,
73
- functionData,
65
+ functionSelector: avmEnvironment.temporaryFunctionSelector,
74
66
  };
75
67
  return execution;
76
68
  }
@@ -81,6 +73,7 @@ export function convertAvmResultsToPxResult(
81
73
  fromPx: PublicExecution,
82
74
  startGas: Gas,
83
75
  endAvmContext: AvmContext,
76
+ bytecode: Buffer | undefined,
84
77
  ): PublicExecutionResult {
85
78
  const endPersistableState = endAvmContext.persistableState;
86
79
  const endMachineState = endAvmContext.machineState;
@@ -100,6 +93,9 @@ export function convertAvmResultsToPxResult(
100
93
  startGasLeft: startGas,
101
94
  endGasLeft: endMachineState.gasLeft,
102
95
  transactionFee: endAvmContext.environment.transactionFee,
96
+ bytecode: bytecode,
97
+ calldata: endAvmContext.environment.calldata,
98
+ avmHints: endPersistableState.trace.toHints(),
103
99
  };
104
100
  }
105
101