@aztec/simulator 0.37.0 → 0.38.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 (114) hide show
  1. package/dest/acvm/acvm.d.ts +1 -1
  2. package/dest/acvm/acvm.d.ts.map +1 -1
  3. package/dest/acvm/acvm.js +2 -2
  4. package/dest/acvm/oracle/oracle.d.ts +5 -4
  5. package/dest/acvm/oracle/oracle.d.ts.map +1 -1
  6. package/dest/acvm/oracle/oracle.js +19 -8
  7. package/dest/acvm/oracle/typed_oracle.d.ts +3 -2
  8. package/dest/acvm/oracle/typed_oracle.d.ts.map +1 -1
  9. package/dest/acvm/oracle/typed_oracle.js +6 -3
  10. package/dest/avm/avm_gas.d.ts.map +1 -1
  11. package/dest/avm/avm_gas.js +2 -1
  12. package/dest/avm/avm_memory_types.d.ts +1 -1
  13. package/dest/avm/avm_memory_types.d.ts.map +1 -1
  14. package/dest/avm/avm_simulator.d.ts.map +1 -1
  15. package/dest/avm/avm_simulator.js +3 -1
  16. package/dest/avm/journal/journal.d.ts +20 -1
  17. package/dest/avm/journal/journal.d.ts.map +1 -1
  18. package/dest/avm/journal/journal.js +69 -9
  19. package/dest/avm/journal/nullifiers.d.ts +3 -1
  20. package/dest/avm/journal/nullifiers.d.ts.map +1 -1
  21. package/dest/avm/journal/nullifiers.js +14 -6
  22. package/dest/avm/journal/public_storage.d.ts +10 -1
  23. package/dest/avm/journal/public_storage.d.ts.map +1 -1
  24. package/dest/avm/journal/public_storage.js +17 -2
  25. package/dest/avm/journal/trace.d.ts +1 -4
  26. package/dest/avm/journal/trace.d.ts.map +1 -1
  27. package/dest/avm/journal/trace.js +4 -5
  28. package/dest/avm/journal/trace_types.d.ts +1 -0
  29. package/dest/avm/journal/trace_types.d.ts.map +1 -1
  30. package/dest/avm/journal/trace_types.js +1 -1
  31. package/dest/avm/opcodes/bitwise.d.ts +4 -1
  32. package/dest/avm/opcodes/bitwise.d.ts.map +1 -1
  33. package/dest/avm/opcodes/bitwise.js +14 -2
  34. package/dest/avm/opcodes/environment_getters.d.ts +5 -0
  35. package/dest/avm/opcodes/environment_getters.d.ts.map +1 -1
  36. package/dest/avm/opcodes/environment_getters.js +8 -1
  37. package/dest/avm/opcodes/external_calls.d.ts.map +1 -1
  38. package/dest/avm/opcodes/external_calls.js +14 -13
  39. package/dest/avm/serialization/bytecode_serialization.d.ts.map +1 -1
  40. package/dest/avm/serialization/bytecode_serialization.js +3 -2
  41. package/dest/avm/serialization/instruction_serialization.d.ts +39 -38
  42. package/dest/avm/serialization/instruction_serialization.d.ts.map +1 -1
  43. package/dest/avm/serialization/instruction_serialization.js +40 -39
  44. package/dest/client/client_execution_context.d.ts +31 -18
  45. package/dest/client/client_execution_context.d.ts.map +1 -1
  46. package/dest/client/client_execution_context.js +48 -31
  47. package/dest/client/db_oracle.d.ts +3 -3
  48. package/dest/client/db_oracle.d.ts.map +1 -1
  49. package/dest/client/execution_result.d.ts +19 -15
  50. package/dest/client/execution_result.d.ts.map +1 -1
  51. package/dest/client/execution_result.js +45 -12
  52. package/dest/client/logs_cache.d.ts +33 -0
  53. package/dest/client/logs_cache.d.ts.map +1 -0
  54. package/dest/client/logs_cache.js +59 -0
  55. package/dest/client/private_execution.d.ts +2 -2
  56. package/dest/client/private_execution.d.ts.map +1 -1
  57. package/dest/client/private_execution.js +3 -7
  58. package/dest/client/simulator.d.ts +3 -3
  59. package/dest/client/simulator.d.ts.map +1 -1
  60. package/dest/client/simulator.js +3 -2
  61. package/dest/client/unconstrained_execution.d.ts +2 -2
  62. package/dest/client/unconstrained_execution.d.ts.map +1 -1
  63. package/dest/client/unconstrained_execution.js +1 -1
  64. package/dest/mocks/fixtures.d.ts.map +1 -1
  65. package/dest/mocks/fixtures.js +3 -1
  66. package/dest/public/abstract_phase_manager.d.ts +2 -0
  67. package/dest/public/abstract_phase_manager.d.ts.map +1 -1
  68. package/dest/public/abstract_phase_manager.js +13 -6
  69. package/dest/public/execution.d.ts +9 -0
  70. package/dest/public/execution.d.ts.map +1 -1
  71. package/dest/public/execution.js +1 -1
  72. package/dest/public/executor.d.ts +2 -2
  73. package/dest/public/executor.d.ts.map +1 -1
  74. package/dest/public/executor.js +34 -14
  75. package/dest/public/public_execution_context.d.ts +10 -4
  76. package/dest/public/public_execution_context.d.ts.map +1 -1
  77. package/dest/public/public_execution_context.js +19 -6
  78. package/dest/public/tail_phase_manager.d.ts +0 -1
  79. package/dest/public/tail_phase_manager.d.ts.map +1 -1
  80. package/dest/public/tail_phase_manager.js +3 -26
  81. package/dest/public/transitional_adaptors.d.ts +4 -17
  82. package/dest/public/transitional_adaptors.d.ts.map +1 -1
  83. package/dest/public/transitional_adaptors.js +27 -119
  84. package/package.json +8 -8
  85. package/src/acvm/acvm.ts +2 -2
  86. package/src/acvm/oracle/oracle.ts +39 -9
  87. package/src/acvm/oracle/typed_oracle.ts +7 -2
  88. package/src/avm/avm_gas.ts +1 -0
  89. package/src/avm/avm_memory_types.ts +1 -1
  90. package/src/avm/avm_simulator.ts +2 -0
  91. package/src/avm/journal/journal.ts +133 -9
  92. package/src/avm/journal/nullifiers.ts +19 -8
  93. package/src/avm/journal/public_storage.ts +23 -2
  94. package/src/avm/journal/trace.ts +3 -4
  95. package/src/avm/journal/trace_types.ts +1 -0
  96. package/src/avm/opcodes/bitwise.ts +18 -7
  97. package/src/avm/opcodes/environment_getters.ts +9 -0
  98. package/src/avm/opcodes/external_calls.ts +21 -16
  99. package/src/avm/serialization/bytecode_serialization.ts +2 -0
  100. package/src/avm/serialization/instruction_serialization.ts +1 -0
  101. package/src/client/client_execution_context.ts +55 -31
  102. package/src/client/db_oracle.ts +3 -9
  103. package/src/client/execution_result.ts +55 -24
  104. package/src/client/logs_cache.ts +65 -0
  105. package/src/client/private_execution.ts +4 -10
  106. package/src/client/simulator.ts +6 -4
  107. package/src/client/unconstrained_execution.ts +2 -2
  108. package/src/mocks/fixtures.ts +2 -0
  109. package/src/public/abstract_phase_manager.ts +13 -5
  110. package/src/public/execution.ts +9 -0
  111. package/src/public/executor.ts +47 -10
  112. package/src/public/public_execution_context.ts +18 -4
  113. package/src/public/tail_phase_manager.ts +2 -34
  114. package/src/public/transitional_adaptors.ts +39 -178
@@ -1,32 +1,21 @@
1
1
  // All code in this file needs to die once the public executor is phased out in favor of the AVM.
2
- import { UnencryptedFunctionL2Logs, UnencryptedL2Log } from '@aztec/circuit-types';
2
+ import { UnencryptedFunctionL2Logs } from '@aztec/circuit-types';
3
3
  import {
4
4
  CallContext,
5
- ContractStorageRead,
6
- ContractStorageUpdateRequest,
7
5
  FunctionData,
8
- Gas,
6
+ type Gas,
9
7
  type GasSettings,
10
8
  type GlobalVariables,
11
9
  type Header,
12
- L2ToL1Message,
13
- NoteHash,
14
- Nullifier,
15
- ReadRequest,
16
- SideEffect,
17
10
  } from '@aztec/circuits.js';
18
11
  import { Fr } from '@aztec/foundation/fields';
19
12
 
20
13
  import { type AvmContext } from '../avm/avm_context.js';
21
14
  import { AvmExecutionEnvironment } from '../avm/avm_execution_environment.js';
22
- import { type AvmMachineState } from '../avm/avm_machine_state.js';
23
- import { AvmContractCallResults } from '../avm/avm_message_call_result.js';
24
- import { type JournalData } from '../avm/journal/journal.js';
15
+ import { type AvmContractCallResults } from '../avm/avm_message_call_result.js';
25
16
  import { Mov } from '../avm/opcodes/memory.js';
26
17
  import { createSimulationError } from '../common/errors.js';
27
- import { PackedValuesCache, SideEffectCounter } from '../index.js';
28
18
  import { type PublicExecution, type PublicExecutionResult } from './execution.js';
29
- import { PublicExecutionContext } from './public_execution_context.js';
30
19
 
31
20
  /**
32
21
  * Convert a PublicExecution(Environment) object to an AvmExecutionEnvironment
@@ -60,184 +49,56 @@ export function createAvmExecutionEnvironment(
60
49
  );
61
50
  }
62
51
 
63
- export function createPublicExecutionContext(avmContext: AvmContext, calldata: Fr[]): PublicExecutionContext {
64
- const sideEffectCounter = avmContext.persistableState.trace.accessCounter;
52
+ export function createPublicExecution(
53
+ startSideEffectCounter: number,
54
+ avmEnvironment: AvmExecutionEnvironment,
55
+ calldata: Fr[],
56
+ ): PublicExecution {
65
57
  const callContext = CallContext.from({
66
- msgSender: avmContext.environment.sender,
67
- storageContractAddress: avmContext.environment.storageAddress,
68
- functionSelector: avmContext.environment.temporaryFunctionSelector,
69
- isDelegateCall: avmContext.environment.isDelegateCall,
70
- isStaticCall: avmContext.environment.isStaticCall,
71
- sideEffectCounter: sideEffectCounter,
58
+ msgSender: avmEnvironment.sender,
59
+ storageContractAddress: avmEnvironment.storageAddress,
60
+ functionSelector: avmEnvironment.temporaryFunctionSelector,
61
+ isDelegateCall: avmEnvironment.isDelegateCall,
62
+ isStaticCall: avmEnvironment.isStaticCall,
63
+ sideEffectCounter: startSideEffectCounter,
72
64
  });
73
- const functionData = new FunctionData(avmContext.environment.temporaryFunctionSelector, /*isPrivate=*/ false);
65
+ const functionData = new FunctionData(avmEnvironment.temporaryFunctionSelector, /*isPrivate=*/ false);
74
66
  const execution: PublicExecution = {
75
- contractAddress: avmContext.environment.address,
67
+ contractAddress: avmEnvironment.address,
76
68
  callContext,
77
69
  args: calldata,
78
70
  functionData,
79
71
  };
80
- const packedArgs = PackedValuesCache.create([]);
81
-
82
- const context = new PublicExecutionContext(
83
- execution,
84
- avmContext.environment.header,
85
- avmContext.environment.globals,
86
- packedArgs,
87
- new SideEffectCounter(sideEffectCounter),
88
- avmContext.persistableState.hostStorage.publicStateDb,
89
- avmContext.persistableState.hostStorage.contractsDb,
90
- avmContext.persistableState.hostStorage.commitmentsDb,
91
- Gas.from(avmContext.machineState.gasLeft),
92
- avmContext.environment.transactionFee,
93
- avmContext.environment.gasSettings,
94
- );
95
-
96
- return context;
72
+ return execution;
97
73
  }
98
74
 
99
- /**
100
- * Convert the result of an AVM contract call to a PublicExecutionResult for the public kernel
101
- *
102
- * @param execution
103
- * @param newWorldState
104
- * @param result
105
- * @returns
106
- */
107
- export async function convertAvmResults(
108
- executionContext: PublicExecutionContext,
109
- newWorldState: JournalData,
110
- result: AvmContractCallResults,
111
- endMachineState: AvmMachineState,
112
- ): Promise<PublicExecutionResult> {
113
- const execution = executionContext.execution;
114
-
115
- const contractStorageReads: ContractStorageRead[] = newWorldState.storageReads.map(
116
- read => new ContractStorageRead(read.slot, read.value, read.counter.toNumber(), read.storageAddress),
117
- );
118
- const contractStorageUpdateRequests: ContractStorageUpdateRequest[] = newWorldState.storageWrites.map(
119
- write => new ContractStorageUpdateRequest(write.slot, write.value, write.counter.toNumber(), write.storageAddress),
120
- );
121
- // We need to write the storage updates to the DB, because that's what the ACVM expects.
122
- // Assumes the updates are in the right order.
123
- for (const write of newWorldState.storageWrites) {
124
- await executionContext.stateDb.storageWrite(write.storageAddress, write.slot, write.value);
125
- }
126
-
127
- const newNoteHashes = newWorldState.newNoteHashes.map(
128
- noteHash => new NoteHash(noteHash.noteHash, noteHash.counter.toNumber()),
129
- );
130
- const nullifierReadRequests: ReadRequest[] = newWorldState.nullifierChecks
131
- .filter(nullifierCheck => nullifierCheck.exists)
132
- .map(nullifierCheck => new ReadRequest(nullifierCheck.nullifier, nullifierCheck.counter.toNumber()));
133
- const nullifierNonExistentReadRequests: ReadRequest[] = newWorldState.nullifierChecks
134
- .filter(nullifierCheck => !nullifierCheck.exists)
135
- .map(nullifierCheck => new ReadRequest(nullifierCheck.nullifier, nullifierCheck.counter.toNumber()));
136
- const newNullifiers: Nullifier[] = newWorldState.newNullifiers.map(
137
- tracedNullifier =>
138
- new Nullifier(
139
- /*value=*/ tracedNullifier.nullifier,
140
- tracedNullifier.counter.toNumber(),
141
- /*noteHash=*/ Fr.ZERO, // NEEDED?
142
- ),
143
- );
144
- const unencryptedLogs: UnencryptedFunctionL2Logs = new UnencryptedFunctionL2Logs(
145
- newWorldState.newLogs.map(log => new UnencryptedL2Log(log.contractAddress, log.selector, log.data)),
146
- );
147
- const unencryptedLogsHashes = newWorldState.newLogsHashes.map(
148
- logHash => new SideEffect(logHash.logHash, logHash.counter),
149
- );
150
- const newL2ToL1Messages = newWorldState.newL1Messages.map(m => new L2ToL1Message(m.recipient, m.content));
151
-
152
- const returnValues = result.output;
153
-
154
- // TODO: Support nested executions.
155
- const nestedExecutions: PublicExecutionResult[] = [];
156
- // TODO keep track of side effect counters
157
- const startSideEffectCounter = Fr.ZERO;
158
- const endSideEffectCounter = Fr.ZERO;
159
-
75
+ export function convertAvmResultsToPxResult(
76
+ avmResult: AvmContractCallResults,
77
+ startSideEffectCounter: number,
78
+ fromPx: PublicExecution,
79
+ startGas: Gas,
80
+ endAvmContext: AvmContext,
81
+ ): PublicExecutionResult {
82
+ const endPersistableState = endAvmContext.persistableState;
83
+ const endMachineState = endAvmContext.machineState;
160
84
  return {
161
- execution,
162
- nullifierReadRequests,
163
- nullifierNonExistentReadRequests,
164
- newNoteHashes,
165
- newL2ToL1Messages,
166
- startSideEffectCounter,
167
- endSideEffectCounter,
168
- newNullifiers,
169
- contractStorageReads,
170
- contractStorageUpdateRequests,
171
- returnValues,
172
- nestedExecutions,
173
- unencryptedLogsHashes,
174
- unencryptedLogs,
175
- reverted: result.reverted,
176
- revertReason: result.revertReason ? createSimulationError(result.revertReason) : undefined,
177
- startGasLeft: executionContext.availableGas,
85
+ ...endPersistableState.transitionalExecutionResult, // includes nestedExecutions
86
+ execution: fromPx,
87
+ returnValues: avmResult.output,
88
+ startSideEffectCounter: new Fr(startSideEffectCounter),
89
+ endSideEffectCounter: new Fr(endPersistableState.trace.accessCounter),
90
+ unencryptedLogs: new UnencryptedFunctionL2Logs(endPersistableState.transitionalExecutionResult.unencryptedLogs),
91
+ allUnencryptedLogs: new UnencryptedFunctionL2Logs(
92
+ endPersistableState.transitionalExecutionResult.allUnencryptedLogs,
93
+ ),
94
+ reverted: avmResult.reverted,
95
+ revertReason: avmResult.revertReason ? createSimulationError(avmResult.revertReason) : undefined,
96
+ startGasLeft: startGas,
178
97
  endGasLeft: endMachineState.gasLeft,
179
- transactionFee: executionContext.transactionFee,
98
+ transactionFee: endAvmContext.environment.transactionFee,
180
99
  };
181
100
  }
182
101
 
183
- export function convertPublicExecutionResult(res: PublicExecutionResult): AvmContractCallResults {
184
- return new AvmContractCallResults(res.reverted, res.returnValues, res.revertReason);
185
- }
186
-
187
- export function updateAvmContextFromPublicExecutionResult(ctx: AvmContext, result: PublicExecutionResult): void {
188
- // We have to push these manually and not use the trace* functions
189
- // so that we respect the side effect counters.
190
- for (const readRequest of result.contractStorageReads) {
191
- ctx.persistableState.trace.publicStorageReads.push({
192
- storageAddress: ctx.environment.storageAddress,
193
- exists: true, // FIXME
194
- slot: readRequest.storageSlot,
195
- value: readRequest.currentValue,
196
- counter: new Fr(readRequest.sideEffectCounter ?? Fr.ZERO),
197
- });
198
- }
199
-
200
- for (const updateRequest of result.contractStorageUpdateRequests) {
201
- ctx.persistableState.trace.publicStorageWrites.push({
202
- storageAddress: ctx.environment.storageAddress,
203
- slot: updateRequest.storageSlot,
204
- value: updateRequest.newValue,
205
- counter: new Fr(updateRequest.sideEffectCounter ?? Fr.ZERO),
206
- });
207
-
208
- // We need to manually populate the cache.
209
- ctx.persistableState.publicStorage.write(
210
- ctx.environment.storageAddress,
211
- updateRequest.storageSlot,
212
- updateRequest.newValue,
213
- );
214
- }
215
-
216
- for (const nullifier of result.newNullifiers) {
217
- ctx.persistableState.trace.newNullifiers.push({
218
- storageAddress: ctx.environment.storageAddress,
219
- nullifier: nullifier.value,
220
- counter: new Fr(nullifier.counter),
221
- });
222
- }
223
-
224
- for (const noteHash of result.newNoteHashes) {
225
- ctx.persistableState.trace.newNoteHashes.push({
226
- storageAddress: ctx.environment.storageAddress,
227
- noteHash: noteHash.value,
228
- counter: new Fr(noteHash.counter),
229
- });
230
- }
231
-
232
- for (const message of result.newL2ToL1Messages) {
233
- ctx.persistableState.newL1Messages.push(message);
234
- }
235
-
236
- for (const log of result.unencryptedLogs.logs) {
237
- ctx.persistableState.newLogs.push(new UnencryptedL2Log(log.contractAddress, log.selector, log.data));
238
- }
239
- }
240
-
241
102
  const AVM_MAGIC_SUFFIX = Buffer.from([
242
103
  Mov.opcode, // opcode
243
104
  0x00, // indirect