@aztec/simulator 0.33.0 → 0.35.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 (186) hide show
  1. package/dest/acvm/acvm.d.ts +4 -1
  2. package/dest/acvm/acvm.d.ts.map +1 -1
  3. package/dest/acvm/acvm.js +5 -5
  4. package/dest/acvm/deserialize.d.ts +3 -5
  5. package/dest/acvm/deserialize.d.ts.map +1 -1
  6. package/dest/acvm/deserialize.js +6 -9
  7. package/dest/acvm/oracle/oracle.d.ts +3 -1
  8. package/dest/acvm/oracle/oracle.d.ts.map +1 -1
  9. package/dest/acvm/oracle/oracle.js +14 -6
  10. package/dest/acvm/oracle/typed_oracle.d.ts +2 -0
  11. package/dest/acvm/oracle/typed_oracle.d.ts.map +1 -1
  12. package/dest/acvm/oracle/typed_oracle.js +7 -1
  13. package/dest/avm/avm_execution_environment.d.ts +6 -3
  14. package/dest/avm/avm_execution_environment.d.ts.map +1 -1
  15. package/dest/avm/avm_execution_environment.js +12 -9
  16. package/dest/avm/avm_gas.js +2 -2
  17. package/dest/avm/avm_machine_state.d.ts +2 -0
  18. package/dest/avm/avm_machine_state.d.ts.map +1 -1
  19. package/dest/avm/avm_machine_state.js +24 -6
  20. package/dest/avm/avm_memory_types.js +6 -6
  21. package/dest/avm/avm_simulator.js +7 -7
  22. package/dest/avm/fixtures/index.d.ts +3 -0
  23. package/dest/avm/fixtures/index.d.ts.map +1 -1
  24. package/dest/avm/fixtures/index.js +11 -4
  25. package/dest/avm/journal/journal.d.ts +15 -13
  26. package/dest/avm/journal/journal.d.ts.map +1 -1
  27. package/dest/avm/journal/journal.js +16 -5
  28. package/dest/avm/journal/trace.d.ts +8 -19
  29. package/dest/avm/journal/trace.d.ts.map +1 -1
  30. package/dest/avm/journal/trace.js +48 -116
  31. package/dest/avm/journal/trace_types.d.ts +23 -4
  32. package/dest/avm/journal/trace_types.d.ts.map +1 -1
  33. package/dest/avm/opcodes/accrued_substate.d.ts +2 -1
  34. package/dest/avm/opcodes/accrued_substate.d.ts.map +1 -1
  35. package/dest/avm/opcodes/accrued_substate.js +14 -6
  36. package/dest/avm/opcodes/context_getters.js +2 -2
  37. package/dest/avm/opcodes/external_calls.d.ts +2 -2
  38. package/dest/avm/opcodes/external_calls.d.ts.map +1 -1
  39. package/dest/avm/opcodes/external_calls.js +26 -10
  40. package/dest/avm/opcodes/hashing.d.ts +8 -8
  41. package/dest/avm/opcodes/hashing.d.ts.map +1 -1
  42. package/dest/avm/opcodes/hashing.js +35 -43
  43. package/dest/avm/serialization/instruction_serialization.d.ts +1 -1
  44. package/dest/avm/serialization/instruction_serialization.d.ts.map +1 -1
  45. package/dest/avm/serialization/instruction_serialization.js +2 -2
  46. package/dest/client/client_execution_context.d.ts +14 -4
  47. package/dest/client/client_execution_context.d.ts.map +1 -1
  48. package/dest/client/client_execution_context.js +28 -13
  49. package/dest/client/private_execution.d.ts +1 -1
  50. package/dest/client/private_execution.d.ts.map +1 -1
  51. package/dest/client/private_execution.js +10 -8
  52. package/dest/client/simulator.d.ts.map +1 -1
  53. package/dest/client/simulator.js +6 -5
  54. package/dest/client/unconstrained_execution.d.ts +1 -1
  55. package/dest/client/unconstrained_execution.d.ts.map +1 -1
  56. package/dest/client/unconstrained_execution.js +6 -5
  57. package/dest/client/view_data_oracle.d.ts +2 -2
  58. package/dest/client/view_data_oracle.d.ts.map +1 -1
  59. package/dest/client/view_data_oracle.js +2 -2
  60. package/dest/common/index.d.ts +1 -1
  61. package/dest/common/index.d.ts.map +1 -1
  62. package/dest/common/index.js +2 -2
  63. package/dest/common/packed_values_cache.d.ts +28 -0
  64. package/dest/common/packed_values_cache.d.ts.map +1 -0
  65. package/dest/common/packed_values_cache.js +50 -0
  66. package/dest/index.d.ts +1 -0
  67. package/dest/index.d.ts.map +1 -1
  68. package/dest/index.js +2 -1
  69. package/dest/mocks/fixtures.d.ts +42 -0
  70. package/dest/mocks/fixtures.d.ts.map +1 -0
  71. package/dest/mocks/fixtures.js +84 -0
  72. package/dest/mocks/index.d.ts +2 -0
  73. package/dest/mocks/index.d.ts.map +1 -0
  74. package/dest/mocks/index.js +2 -0
  75. package/dest/public/abstract_phase_manager.d.ts +82 -0
  76. package/dest/public/abstract_phase_manager.d.ts.map +1 -0
  77. package/dest/public/abstract_phase_manager.js +320 -0
  78. package/dest/public/app_logic_phase_manager.d.ts +29 -0
  79. package/dest/public/app_logic_phase_manager.d.ts.map +1 -0
  80. package/dest/public/app_logic_phase_manager.js +50 -0
  81. package/dest/public/execution.d.ts +3 -0
  82. package/dest/public/execution.d.ts.map +1 -1
  83. package/dest/public/execution.js +1 -1
  84. package/dest/public/executor.d.ts +1 -15
  85. package/dest/public/executor.d.ts.map +1 -1
  86. package/dest/public/executor.js +80 -83
  87. package/dest/public/hints_builder.d.ts +23 -0
  88. package/dest/public/hints_builder.d.ts.map +1 -0
  89. package/dest/public/hints_builder.js +62 -0
  90. package/dest/public/index.d.ts +5 -0
  91. package/dest/public/index.d.ts.map +1 -1
  92. package/dest/public/index.js +6 -1
  93. package/dest/public/phase_manager_factory.d.ts +18 -0
  94. package/dest/public/phase_manager_factory.d.ts.map +1 -0
  95. package/dest/public/phase_manager_factory.js +56 -0
  96. package/dest/public/public_execution_context.d.ts +20 -10
  97. package/dest/public/public_execution_context.d.ts.map +1 -1
  98. package/dest/public/public_execution_context.js +35 -23
  99. package/dest/public/public_executor.d.ts +79 -0
  100. package/dest/public/public_executor.d.ts.map +1 -0
  101. package/dest/public/public_executor.js +198 -0
  102. package/dest/public/public_kernel.d.ts +37 -0
  103. package/dest/public/public_kernel.d.ts.map +1 -0
  104. package/dest/public/public_kernel.js +97 -0
  105. package/dest/public/public_kernel_circuit_simulator.d.ts +31 -0
  106. package/dest/public/public_kernel_circuit_simulator.d.ts.map +1 -0
  107. package/dest/public/public_kernel_circuit_simulator.js +2 -0
  108. package/dest/public/public_processor.d.ts +53 -0
  109. package/dest/public/public_processor.d.ts.map +1 -0
  110. package/dest/public/public_processor.js +144 -0
  111. package/dest/public/setup_phase_manager.d.ts +30 -0
  112. package/dest/public/setup_phase_manager.d.ts.map +1 -0
  113. package/dest/public/setup_phase_manager.js +46 -0
  114. package/dest/public/tail_phase_manager.d.ts +30 -0
  115. package/dest/public/tail_phase_manager.d.ts.map +1 -0
  116. package/dest/public/tail_phase_manager.js +60 -0
  117. package/dest/public/teardown_phase_manager.d.ts +30 -0
  118. package/dest/public/teardown_phase_manager.d.ts.map +1 -0
  119. package/dest/public/teardown_phase_manager.js +46 -0
  120. package/dest/public/transitional_adaptors.d.ts +33 -0
  121. package/dest/public/transitional_adaptors.d.ts.map +1 -0
  122. package/dest/public/transitional_adaptors.js +162 -0
  123. package/dest/public/utils.d.ts +8 -0
  124. package/dest/public/utils.d.ts.map +1 -0
  125. package/dest/public/utils.js +29 -0
  126. package/dest/simulator/acvm_native.d.ts +19 -3
  127. package/dest/simulator/acvm_native.d.ts.map +1 -1
  128. package/dest/simulator/acvm_native.js +75 -48
  129. package/dest/simulator/acvm_wasm.d.ts.map +1 -1
  130. package/dest/simulator/acvm_wasm.js +3 -4
  131. package/package.json +8 -5
  132. package/src/acvm/acvm.ts +8 -5
  133. package/src/acvm/deserialize.ts +5 -9
  134. package/src/acvm/oracle/oracle.ts +15 -5
  135. package/src/acvm/oracle/typed_oracle.ts +8 -0
  136. package/src/avm/avm_execution_environment.ts +17 -17
  137. package/src/avm/avm_gas.ts +1 -1
  138. package/src/avm/avm_machine_state.ts +26 -5
  139. package/src/avm/avm_memory_types.ts +5 -5
  140. package/src/avm/avm_simulator.ts +6 -6
  141. package/src/avm/fixtures/index.ts +14 -1
  142. package/src/avm/journal/journal.ts +37 -17
  143. package/src/avm/journal/trace.ts +59 -121
  144. package/src/avm/journal/trace_types.ts +39 -39
  145. package/src/avm/opcodes/accrued_substate.ts +17 -5
  146. package/src/avm/opcodes/context_getters.ts +1 -1
  147. package/src/avm/opcodes/external_calls.ts +32 -9
  148. package/src/avm/opcodes/hashing.ts +38 -54
  149. package/src/avm/serialization/instruction_serialization.ts +1 -1
  150. package/src/client/client_execution_context.ts +30 -11
  151. package/src/client/private_execution.ts +9 -8
  152. package/src/client/simulator.ts +7 -3
  153. package/src/client/unconstrained_execution.ts +5 -4
  154. package/src/client/view_data_oracle.ts +1 -1
  155. package/src/common/index.ts +1 -1
  156. package/src/common/packed_values_cache.ts +55 -0
  157. package/src/index.ts +1 -0
  158. package/src/mocks/fixtures.ts +169 -0
  159. package/src/mocks/index.ts +1 -0
  160. package/src/public/abstract_phase_manager.ts +571 -0
  161. package/src/public/app_logic_phase_manager.ts +76 -0
  162. package/src/public/execution.ts +4 -0
  163. package/src/public/executor.ts +93 -93
  164. package/src/public/hints_builder.ts +119 -0
  165. package/src/public/index.ts +5 -0
  166. package/src/public/phase_manager_factory.ts +126 -0
  167. package/src/public/public_execution_context.ts +43 -37
  168. package/src/public/public_executor.ts +267 -0
  169. package/src/public/public_kernel.ts +139 -0
  170. package/src/public/public_kernel_circuit_simulator.ts +36 -0
  171. package/src/public/public_processor.ts +212 -0
  172. package/src/public/setup_phase_manager.ts +66 -0
  173. package/src/public/tail_phase_manager.ts +120 -0
  174. package/src/public/teardown_phase_manager.ts +66 -0
  175. package/src/public/transitional_adaptors.ts +249 -0
  176. package/src/public/utils.ts +31 -0
  177. package/src/simulator/acvm_native.ts +94 -47
  178. package/src/simulator/acvm_wasm.ts +7 -3
  179. package/dest/avm/temporary_executor_migration.d.ts +0 -27
  180. package/dest/avm/temporary_executor_migration.d.ts.map +0 -1
  181. package/dest/avm/temporary_executor_migration.js +0 -94
  182. package/dest/common/packed_args_cache.d.ts +0 -28
  183. package/dest/common/packed_args_cache.d.ts.map +0 -1
  184. package/dest/common/packed_args_cache.js +0 -50
  185. package/src/avm/temporary_executor_migration.ts +0 -136
  186. package/src/common/packed_args_cache.ts +0 -55
@@ -0,0 +1,212 @@
1
+ import {
2
+ type BlockProver,
3
+ type FailedTx,
4
+ type ProcessedTx,
5
+ type PublicKernelRequest,
6
+ type SimulationError,
7
+ Tx,
8
+ type TxValidator,
9
+ makeEmptyProcessedTx,
10
+ makeProcessedTx,
11
+ toTxEffect,
12
+ validateProcessedTx,
13
+ } from '@aztec/circuit-types';
14
+ import { type TxSequencerProcessingStats } from '@aztec/circuit-types/stats';
15
+ import { type GlobalVariables, type Header, type KernelCircuitPublicInputs } from '@aztec/circuits.js';
16
+ import { type ProcessReturnValues } from '@aztec/foundation/abi';
17
+ import { createDebugLogger } from '@aztec/foundation/log';
18
+ import { Timer } from '@aztec/foundation/timer';
19
+ import { PublicExecutor, type PublicStateDB, type SimulationProvider } from '@aztec/simulator';
20
+ import { type ContractDataSource } from '@aztec/types/contracts';
21
+ import { type MerkleTreeOperations } from '@aztec/world-state';
22
+
23
+ import { type AbstractPhaseManager, PublicKernelPhase } from './abstract_phase_manager.js';
24
+ import { PhaseManagerFactory } from './phase_manager_factory.js';
25
+ import { ContractsDataSourcePublicDB, WorldStateDB, WorldStatePublicDB } from './public_executor.js';
26
+ import { RealPublicKernelCircuitSimulator } from './public_kernel.js';
27
+ import { type PublicKernelCircuitSimulator } from './public_kernel_circuit_simulator.js';
28
+
29
+ /**
30
+ * Creates new instances of PublicProcessor given the provided merkle tree db and contract data source.
31
+ */
32
+ export class PublicProcessorFactory {
33
+ constructor(
34
+ private merkleTree: MerkleTreeOperations,
35
+ private contractDataSource: ContractDataSource,
36
+ private simulator: SimulationProvider,
37
+ ) {}
38
+
39
+ /**
40
+ * Creates a new instance of a PublicProcessor.
41
+ * @param historicalHeader - The header of a block previous to the one in which the tx is included.
42
+ * @param globalVariables - The global variables for the block being processed.
43
+ * @param newContracts - Provides access to contract bytecode for public executions.
44
+ * @returns A new instance of a PublicProcessor.
45
+ */
46
+ public async create(
47
+ historicalHeader: Header | undefined,
48
+ globalVariables: GlobalVariables,
49
+ ): Promise<PublicProcessor> {
50
+ historicalHeader = historicalHeader ?? (await this.merkleTree.buildInitialHeader());
51
+
52
+ const publicContractsDB = new ContractsDataSourcePublicDB(this.contractDataSource);
53
+ const worldStatePublicDB = new WorldStatePublicDB(this.merkleTree);
54
+ const worldStateDB = new WorldStateDB(this.merkleTree);
55
+ const publicExecutor = new PublicExecutor(worldStatePublicDB, publicContractsDB, worldStateDB, historicalHeader);
56
+ return new PublicProcessor(
57
+ this.merkleTree,
58
+ publicExecutor,
59
+ new RealPublicKernelCircuitSimulator(this.simulator),
60
+ globalVariables,
61
+ historicalHeader,
62
+ publicContractsDB,
63
+ worldStatePublicDB,
64
+ );
65
+ }
66
+ }
67
+
68
+ /**
69
+ * Converts Txs lifted from the P2P module into ProcessedTx objects by executing
70
+ * any public function calls in them. Txs with private calls only are unaffected.
71
+ */
72
+ export class PublicProcessor {
73
+ constructor(
74
+ protected db: MerkleTreeOperations,
75
+ protected publicExecutor: PublicExecutor,
76
+ protected publicKernel: PublicKernelCircuitSimulator,
77
+ protected globalVariables: GlobalVariables,
78
+ protected historicalHeader: Header,
79
+ protected publicContractsDB: ContractsDataSourcePublicDB,
80
+ protected publicStateDB: PublicStateDB,
81
+
82
+ private log = createDebugLogger('aztec:sequencer:public-processor'),
83
+ ) {}
84
+
85
+ /**
86
+ * Run each tx through the public circuit and the public kernel circuit if needed.
87
+ * @param txs - Txs to process.
88
+ * @returns The list of processed txs with their circuit simulation outputs.
89
+ */
90
+ public async process(
91
+ txs: Tx[],
92
+ maxTransactions = txs.length,
93
+ blockProver?: BlockProver,
94
+ txValidator?: TxValidator<ProcessedTx>,
95
+ ): Promise<[ProcessedTx[], FailedTx[], ProcessReturnValues[]]> {
96
+ // The processor modifies the tx objects in place, so we need to clone them.
97
+ txs = txs.map(tx => Tx.clone(tx));
98
+ const result: ProcessedTx[] = [];
99
+ const failed: FailedTx[] = [];
100
+ const returns: ProcessReturnValues[] = [];
101
+
102
+ for (const tx of txs) {
103
+ // only process up to the limit of the block
104
+ if (result.length >= maxTransactions) {
105
+ break;
106
+ }
107
+ try {
108
+ const [processedTx, returnValues] = !tx.hasPublicCalls()
109
+ ? [makeProcessedTx(tx, tx.data.toKernelCircuitPublicInputs(), tx.proof, [])]
110
+ : await this.processTxWithPublicCalls(tx);
111
+ validateProcessedTx(processedTx);
112
+ // Re-validate the transaction
113
+ if (txValidator) {
114
+ // Only accept processed transactions that are not double-spends,
115
+ // public functions emitting nullifiers would pass earlier check but fail here.
116
+ // Note that we're checking all nullifiers generated in the private execution twice,
117
+ // we could store the ones already checked and skip them here as an optimization.
118
+ const [_, invalid] = await txValidator.validateTxs([processedTx]);
119
+ if (invalid.length) {
120
+ throw new Error(`Transaction ${invalid[0].hash} invalid after processing public functions`);
121
+ }
122
+ }
123
+ // if we were given a prover then send the transaction to it for proving
124
+ if (blockProver) {
125
+ await blockProver.addNewTx(processedTx);
126
+ }
127
+ result.push(processedTx);
128
+ returns.push(returnValues);
129
+ } catch (err: any) {
130
+ const errorMessage = err instanceof Error ? err.message : 'Unknown error';
131
+ this.log.warn(`Failed to process tx ${tx.getTxHash()}: ${errorMessage}`);
132
+
133
+ failed.push({
134
+ tx,
135
+ error: err instanceof Error ? err : new Error(errorMessage),
136
+ });
137
+ returns.push([]);
138
+ }
139
+ }
140
+
141
+ return [result, failed, returns];
142
+ }
143
+
144
+ /**
145
+ * Makes an empty processed tx. Useful for padding a block to a power of two number of txs.
146
+ * @returns A processed tx with empty data.
147
+ */
148
+ public makeEmptyProcessedTx(): ProcessedTx {
149
+ const { chainId, version } = this.globalVariables;
150
+ return makeEmptyProcessedTx(this.historicalHeader.clone(), chainId, version);
151
+ }
152
+
153
+ private async processTxWithPublicCalls(tx: Tx): Promise<[ProcessedTx, ProcessReturnValues | undefined]> {
154
+ let returnValues: ProcessReturnValues = undefined;
155
+ const publicRequests: PublicKernelRequest[] = [];
156
+ let phase: AbstractPhaseManager | undefined = PhaseManagerFactory.phaseFromTx(
157
+ tx,
158
+ this.db,
159
+ this.publicExecutor,
160
+ this.publicKernel,
161
+ this.globalVariables,
162
+ this.historicalHeader,
163
+ this.publicContractsDB,
164
+ this.publicStateDB,
165
+ );
166
+ this.log.debug(`Beginning processing in phase ${phase?.phase} for tx ${tx.getTxHash()}`);
167
+ let proof = tx.proof;
168
+ let publicKernelPublicInput = tx.data.toPublicKernelCircuitPublicInputs();
169
+ let finalKernelOutput: KernelCircuitPublicInputs | undefined;
170
+ let revertReason: SimulationError | undefined;
171
+ const timer = new Timer();
172
+ while (phase) {
173
+ const output = await phase.handle(tx, publicKernelPublicInput, proof);
174
+ if (phase.phase === PublicKernelPhase.APP_LOGIC) {
175
+ returnValues = output.returnValues;
176
+ }
177
+ publicRequests.push(...output.kernelRequests);
178
+ publicKernelPublicInput = output.publicKernelOutput;
179
+ finalKernelOutput = output.finalKernelOutput;
180
+ proof = output.publicKernelProof;
181
+ revertReason ??= output.revertReason;
182
+ phase = PhaseManagerFactory.phaseFromOutput(
183
+ publicKernelPublicInput,
184
+ phase,
185
+ this.db,
186
+ this.publicExecutor,
187
+ this.publicKernel,
188
+ this.globalVariables,
189
+ this.historicalHeader,
190
+ this.publicContractsDB,
191
+ this.publicStateDB,
192
+ );
193
+ }
194
+
195
+ if (!finalKernelOutput) {
196
+ throw new Error('Final public kernel was not executed.');
197
+ }
198
+
199
+ const processedTx = makeProcessedTx(tx, finalKernelOutput, proof, publicRequests, revertReason);
200
+
201
+ this.log.debug(`Processed public part of ${tx.getTxHash()}`, {
202
+ eventName: 'tx-sequencer-processing',
203
+ duration: timer.ms(),
204
+ effectsSize: toTxEffect(processedTx).toBuffer().length,
205
+ publicDataUpdateRequests:
206
+ processedTx.data.end.publicDataUpdateRequests.filter(x => !x.leafSlot.isZero()).length ?? 0,
207
+ ...tx.getStats(),
208
+ } satisfies TxSequencerProcessingStats);
209
+
210
+ return [processedTx, returnValues];
211
+ }
212
+ }
@@ -0,0 +1,66 @@
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';
8
+ import { type PublicExecutor, type PublicStateDB } from '@aztec/simulator';
9
+ import { type MerkleTreeOperations } from '@aztec/world-state';
10
+
11
+ import { AbstractPhaseManager, PublicKernelPhase } from './abstract_phase_manager.js';
12
+ import { type ContractsDataSourcePublicDB } from './public_executor.js';
13
+ import { type PublicKernelCircuitSimulator } from './public_kernel_circuit_simulator.js';
14
+
15
+ /**
16
+ * The phase manager responsible for performing the fee preparation phase.
17
+ */
18
+ export class SetupPhaseManager extends AbstractPhaseManager {
19
+ constructor(
20
+ protected db: MerkleTreeOperations,
21
+ protected publicExecutor: PublicExecutor,
22
+ protected publicKernel: PublicKernelCircuitSimulator,
23
+ protected globalVariables: GlobalVariables,
24
+ protected historicalHeader: Header,
25
+ protected publicContractsDB: ContractsDataSourcePublicDB,
26
+ protected publicStateDB: PublicStateDB,
27
+ public phase: PublicKernelPhase = PublicKernelPhase.SETUP,
28
+ ) {
29
+ super(db, publicExecutor, publicKernel, globalVariables, historicalHeader, phase);
30
+ }
31
+
32
+ override async handle(
33
+ tx: Tx,
34
+ previousPublicKernelOutput: PublicKernelCircuitPublicInputs,
35
+ previousPublicKernelProof: Proof,
36
+ ) {
37
+ this.log.verbose(`Processing tx ${tx.getTxHash()}`);
38
+ const [kernelInputs, publicKernelOutput, publicKernelProof, newUnencryptedFunctionLogs, revertReason] =
39
+ await this.processEnqueuedPublicCalls(tx, previousPublicKernelOutput, previousPublicKernelProof).catch(
40
+ // the abstract phase manager throws if simulation gives error in a non-revertible phase
41
+ async err => {
42
+ await this.publicStateDB.rollbackToCommit();
43
+ throw err;
44
+ },
45
+ );
46
+ tx.unencryptedLogs.addFunctionLogs(newUnencryptedFunctionLogs);
47
+ await this.publicStateDB.checkpoint();
48
+
49
+ // Return a list of setup proving requests
50
+ const kernelRequests = kernelInputs.map(input => {
51
+ const request: PublicKernelRequest = {
52
+ type: PublicKernelType.SETUP,
53
+ inputs: input,
54
+ };
55
+ return request;
56
+ });
57
+ return {
58
+ kernelRequests,
59
+ kernelInputs,
60
+ publicKernelOutput,
61
+ publicKernelProof,
62
+ revertReason,
63
+ returnValues: undefined,
64
+ };
65
+ }
66
+ }
@@ -0,0 +1,120 @@
1
+ import { type PublicKernelRequest, PublicKernelType, type Tx } from '@aztec/circuit-types';
2
+ import {
3
+ type Fr,
4
+ type GlobalVariables,
5
+ type Header,
6
+ type KernelCircuitPublicInputs,
7
+ MAX_NEW_NOTE_HASHES_PER_TX,
8
+ type Proof,
9
+ type PublicKernelCircuitPublicInputs,
10
+ PublicKernelTailCircuitPrivateInputs,
11
+ type SideEffect,
12
+ makeEmptyProof,
13
+ mergeAccumulatedData,
14
+ sortByCounter,
15
+ } from '@aztec/circuits.js';
16
+ import { type Tuple } from '@aztec/foundation/serialize';
17
+ import { type PublicExecutor, type PublicStateDB } from '@aztec/simulator';
18
+ import { type MerkleTreeOperations } from '@aztec/world-state';
19
+
20
+ import { AbstractPhaseManager, PublicKernelPhase } from './abstract_phase_manager.js';
21
+ import { type ContractsDataSourcePublicDB } from './public_executor.js';
22
+ import { type PublicKernelCircuitSimulator } from './public_kernel_circuit_simulator.js';
23
+
24
+ export class TailPhaseManager extends AbstractPhaseManager {
25
+ constructor(
26
+ protected db: MerkleTreeOperations,
27
+ protected publicExecutor: PublicExecutor,
28
+ protected publicKernel: PublicKernelCircuitSimulator,
29
+ protected globalVariables: GlobalVariables,
30
+ protected historicalHeader: Header,
31
+ protected publicContractsDB: ContractsDataSourcePublicDB,
32
+ protected publicStateDB: PublicStateDB,
33
+ public readonly phase: PublicKernelPhase = PublicKernelPhase.TAIL,
34
+ ) {
35
+ super(db, publicExecutor, publicKernel, globalVariables, historicalHeader, phase);
36
+ }
37
+
38
+ async handle(tx: Tx, previousPublicKernelOutput: PublicKernelCircuitPublicInputs, previousPublicKernelProof: Proof) {
39
+ this.log.verbose(`Processing tx ${tx.getTxHash()}`);
40
+ const [inputs, finalKernelOutput] = await this.runTailKernelCircuit(
41
+ previousPublicKernelOutput,
42
+ previousPublicKernelProof,
43
+ ).catch(
44
+ // the abstract phase manager throws if simulation gives error in non-revertible phase
45
+ async err => {
46
+ await this.publicStateDB.rollbackToCommit();
47
+ throw err;
48
+ },
49
+ );
50
+
51
+ // commit the state updates from this transaction
52
+ await this.publicStateDB.commit();
53
+
54
+ // Return a tail proving request
55
+ const request: PublicKernelRequest = {
56
+ type: PublicKernelType.TAIL,
57
+ inputs: inputs,
58
+ };
59
+
60
+ return {
61
+ kernelRequests: [request],
62
+ publicKernelOutput: previousPublicKernelOutput,
63
+ finalKernelOutput,
64
+ publicKernelProof: makeEmptyProof(),
65
+ revertReason: undefined,
66
+ returnValues: undefined,
67
+ };
68
+ }
69
+
70
+ private async runTailKernelCircuit(
71
+ previousOutput: PublicKernelCircuitPublicInputs,
72
+ previousProof: Proof,
73
+ ): Promise<[PublicKernelTailCircuitPrivateInputs, KernelCircuitPublicInputs]> {
74
+ const [inputs, output] = await this.simulate(previousOutput, previousProof);
75
+
76
+ // Temporary hack. Should sort them in the tail circuit.
77
+ const noteHashes = mergeAccumulatedData(
78
+ MAX_NEW_NOTE_HASHES_PER_TX,
79
+ previousOutput.endNonRevertibleData.newNoteHashes,
80
+ previousOutput.end.newNoteHashes,
81
+ );
82
+ output.end.newNoteHashes = this.sortNoteHashes<typeof MAX_NEW_NOTE_HASHES_PER_TX>(noteHashes);
83
+
84
+ return [inputs, output];
85
+ }
86
+
87
+ private async simulate(
88
+ previousOutput: PublicKernelCircuitPublicInputs,
89
+ previousProof: Proof,
90
+ ): Promise<[PublicKernelTailCircuitPrivateInputs, KernelCircuitPublicInputs]> {
91
+ const previousKernel = this.getPreviousKernelData(previousOutput, previousProof);
92
+
93
+ const { validationRequests, endNonRevertibleData, end } = previousOutput;
94
+ const nullifierReadRequestHints = await this.hintsBuilder.getNullifierReadRequestHints(
95
+ validationRequests.nullifierReadRequests,
96
+ endNonRevertibleData.newNullifiers,
97
+ end.newNullifiers,
98
+ );
99
+ const nullifierNonExistentReadRequestHints = await this.hintsBuilder.getNullifierNonExistentReadRequestHints(
100
+ validationRequests.nullifierNonExistentReadRequests,
101
+ endNonRevertibleData.newNullifiers,
102
+ end.newNullifiers,
103
+ );
104
+
105
+ // We take a deep copy (clone) of these to pass to the prover
106
+ const inputs = new PublicKernelTailCircuitPrivateInputs(
107
+ previousKernel,
108
+ nullifierReadRequestHints,
109
+ nullifierNonExistentReadRequestHints,
110
+ );
111
+ return [inputs.clone(), await this.publicKernel.publicKernelCircuitTail(inputs)];
112
+ }
113
+
114
+ private sortNoteHashes<N extends number>(noteHashes: Tuple<SideEffect, N>): Tuple<Fr, N> {
115
+ return sortByCounter(noteHashes.map(n => ({ ...n, counter: n.counter.toNumber() }))).map(n => n.value) as Tuple<
116
+ Fr,
117
+ N
118
+ >;
119
+ }
120
+ }
@@ -0,0 +1,66 @@
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';
8
+ import { type PublicExecutor, type PublicStateDB } from '@aztec/simulator';
9
+ import { type MerkleTreeOperations } from '@aztec/world-state';
10
+
11
+ import { AbstractPhaseManager, PublicKernelPhase } from './abstract_phase_manager.js';
12
+ import { type ContractsDataSourcePublicDB } from './public_executor.js';
13
+ import { type PublicKernelCircuitSimulator } from './public_kernel_circuit_simulator.js';
14
+
15
+ /**
16
+ * The phase manager responsible for performing the fee preparation phase.
17
+ */
18
+ export class TeardownPhaseManager extends AbstractPhaseManager {
19
+ constructor(
20
+ protected db: MerkleTreeOperations,
21
+ protected publicExecutor: PublicExecutor,
22
+ protected publicKernel: PublicKernelCircuitSimulator,
23
+ protected globalVariables: GlobalVariables,
24
+ protected historicalHeader: Header,
25
+ protected publicContractsDB: ContractsDataSourcePublicDB,
26
+ protected publicStateDB: PublicStateDB,
27
+ public phase: PublicKernelPhase = PublicKernelPhase.TEARDOWN,
28
+ ) {
29
+ super(db, publicExecutor, publicKernel, globalVariables, historicalHeader, phase);
30
+ }
31
+
32
+ override async handle(
33
+ tx: Tx,
34
+ previousPublicKernelOutput: PublicKernelCircuitPublicInputs,
35
+ previousPublicKernelProof: Proof,
36
+ ) {
37
+ this.log.verbose(`Processing tx ${tx.getTxHash()}`);
38
+ const [kernelInputs, publicKernelOutput, publicKernelProof, newUnencryptedFunctionLogs, revertReason] =
39
+ await this.processEnqueuedPublicCalls(tx, previousPublicKernelOutput, previousPublicKernelProof).catch(
40
+ // the abstract phase manager throws if simulation gives error in a non-revertible phase
41
+ async err => {
42
+ await this.publicStateDB.rollbackToCommit();
43
+ throw err;
44
+ },
45
+ );
46
+ tx.unencryptedLogs.addFunctionLogs(newUnencryptedFunctionLogs);
47
+ await this.publicStateDB.checkpoint();
48
+
49
+ // Return a list of teardown proving requests
50
+ const kernelRequests = kernelInputs.map(input => {
51
+ const request: PublicKernelRequest = {
52
+ type: PublicKernelType.TEARDOWN,
53
+ inputs: input,
54
+ };
55
+ return request;
56
+ });
57
+ return {
58
+ kernelRequests,
59
+ kernelInputs,
60
+ publicKernelOutput,
61
+ publicKernelProof,
62
+ revertReason,
63
+ returnValues: undefined,
64
+ };
65
+ }
66
+ }