@aztec/simulator 0.23.0 → 0.26.1

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 (177) hide show
  1. package/dest/acvm/deserialize.d.ts +5 -0
  2. package/dest/acvm/deserialize.d.ts.map +1 -1
  3. package/dest/acvm/deserialize.js +8 -1
  4. package/dest/acvm/oracle/oracle.d.ts +7 -6
  5. package/dest/acvm/oracle/oracle.d.ts.map +1 -1
  6. package/dest/acvm/oracle/oracle.js +28 -15
  7. package/dest/acvm/oracle/typed_oracle.d.ts +9 -11
  8. package/dest/acvm/oracle/typed_oracle.d.ts.map +1 -1
  9. package/dest/acvm/oracle/typed_oracle.js +11 -11
  10. package/dest/avm/avm_context.d.ts +4 -4
  11. package/dest/avm/avm_context.d.ts.map +1 -1
  12. package/dest/avm/avm_context.js +6 -6
  13. package/dest/avm/avm_execution_environment.d.ts +3 -2
  14. package/dest/avm/avm_execution_environment.d.ts.map +1 -1
  15. package/dest/avm/avm_execution_environment.js +6 -5
  16. package/dest/avm/avm_memory_types.d.ts +127 -37
  17. package/dest/avm/avm_memory_types.d.ts.map +1 -1
  18. package/dest/avm/avm_memory_types.js +98 -106
  19. package/dest/avm/avm_simulator.d.ts +6 -4
  20. package/dest/avm/avm_simulator.d.ts.map +1 -1
  21. package/dest/avm/avm_simulator.js +17 -19
  22. package/dest/avm/errors.d.ts +3 -1
  23. package/dest/avm/errors.d.ts.map +1 -1
  24. package/dest/avm/errors.js +9 -3
  25. package/dest/avm/fixtures/index.d.ts +21 -5
  26. package/dest/avm/fixtures/index.d.ts.map +1 -1
  27. package/dest/avm/fixtures/index.js +28 -9
  28. package/dest/avm/journal/host_storage.d.ts +1 -1
  29. package/dest/avm/journal/host_storage.d.ts.map +1 -1
  30. package/dest/avm/journal/host_storage.js +1 -1
  31. package/dest/avm/journal/journal.d.ts +78 -50
  32. package/dest/avm/journal/journal.d.ts.map +1 -1
  33. package/dest/avm/journal/journal.js +125 -169
  34. package/dest/avm/journal/nullifiers.d.ts +85 -0
  35. package/dest/avm/journal/nullifiers.d.ts.map +1 -0
  36. package/dest/avm/journal/nullifiers.js +147 -0
  37. package/dest/avm/journal/public_storage.d.ts +88 -0
  38. package/dest/avm/journal/public_storage.d.ts.map +1 -0
  39. package/dest/avm/journal/public_storage.js +135 -0
  40. package/dest/avm/journal/trace.d.ts +43 -0
  41. package/dest/avm/journal/trace.d.ts.map +1 -0
  42. package/dest/avm/journal/trace.js +204 -0
  43. package/dest/avm/journal/trace_types.d.ts +26 -0
  44. package/dest/avm/journal/trace_types.d.ts.map +1 -0
  45. package/dest/avm/journal/trace_types.js +6 -0
  46. package/dest/avm/opcodes/accrued_substate.d.ts +37 -4
  47. package/dest/avm/opcodes/accrued_substate.d.ts.map +1 -1
  48. package/dest/avm/opcodes/accrued_substate.js +109 -12
  49. package/dest/avm/opcodes/addressing_mode.d.ts +24 -0
  50. package/dest/avm/opcodes/addressing_mode.d.ts.map +1 -0
  51. package/dest/avm/opcodes/addressing_mode.js +62 -0
  52. package/dest/avm/opcodes/environment_getters.d.ts +14 -13
  53. package/dest/avm/opcodes/environment_getters.d.ts.map +1 -1
  54. package/dest/avm/opcodes/environment_getters.js +1 -1
  55. package/dest/avm/opcodes/external_calls.js +5 -5
  56. package/dest/avm/opcodes/hashing.d.ts +48 -0
  57. package/dest/avm/opcodes/hashing.d.ts.map +1 -0
  58. package/dest/avm/opcodes/hashing.js +127 -0
  59. package/dest/avm/opcodes/instruction.d.ts +4 -4
  60. package/dest/avm/opcodes/instruction.d.ts.map +1 -1
  61. package/dest/avm/opcodes/instruction.js +1 -1
  62. package/dest/avm/opcodes/memory.d.ts.map +1 -1
  63. package/dest/avm/opcodes/memory.js +5 -3
  64. package/dest/avm/opcodes/storage.d.ts.map +1 -1
  65. package/dest/avm/opcodes/storage.js +3 -3
  66. package/dest/avm/serialization/bytecode_serialization.d.ts.map +1 -1
  67. package/dest/avm/serialization/bytecode_serialization.js +28 -22
  68. package/dest/avm/serialization/instruction_serialization.d.ts +21 -16
  69. package/dest/avm/serialization/instruction_serialization.d.ts.map +1 -1
  70. package/dest/avm/serialization/instruction_serialization.js +23 -18
  71. package/dest/avm/temporary_executor_migration.d.ts +25 -0
  72. package/dest/avm/temporary_executor_migration.d.ts.map +1 -0
  73. package/dest/avm/temporary_executor_migration.js +71 -0
  74. package/dest/client/client_execution_context.d.ts +13 -7
  75. package/dest/client/client_execution_context.d.ts.map +1 -1
  76. package/dest/client/client_execution_context.js +52 -27
  77. package/dest/client/db_oracle.d.ts +7 -0
  78. package/dest/client/db_oracle.d.ts.map +1 -1
  79. package/dest/client/db_oracle.js +1 -1
  80. package/dest/client/execution_note_cache.js +1 -1
  81. package/dest/client/execution_result.d.ts +4 -2
  82. package/dest/client/execution_result.d.ts.map +1 -1
  83. package/dest/client/execution_result.js +1 -1
  84. package/dest/client/private_execution.d.ts.map +1 -1
  85. package/dest/client/private_execution.js +4 -4
  86. package/dest/client/simulator.d.ts +11 -6
  87. package/dest/client/simulator.d.ts.map +1 -1
  88. package/dest/client/simulator.js +21 -12
  89. package/dest/client/unconstrained_execution.js +2 -2
  90. package/dest/client/view_data_oracle.d.ts +9 -2
  91. package/dest/client/view_data_oracle.d.ts.map +1 -1
  92. package/dest/client/view_data_oracle.js +13 -5
  93. package/dest/public/db.d.ts +17 -4
  94. package/dest/public/db.d.ts.map +1 -1
  95. package/dest/public/execution.d.ts +9 -4
  96. package/dest/public/execution.d.ts.map +1 -1
  97. package/dest/public/execution.js +18 -5
  98. package/dest/public/executor.d.ts +7 -0
  99. package/dest/public/executor.d.ts.map +1 -1
  100. package/dest/public/executor.js +40 -6
  101. package/dest/public/public_execution_context.d.ts +5 -4
  102. package/dest/public/public_execution_context.d.ts.map +1 -1
  103. package/dest/public/public_execution_context.js +24 -13
  104. package/dest/public/state_actions.d.ts +1 -1
  105. package/dest/public/state_actions.d.ts.map +1 -1
  106. package/dest/public/state_actions.js +6 -7
  107. package/dest/test/utils.js +4 -4
  108. package/dest/utils.d.ts +5 -20
  109. package/dest/utils.d.ts.map +1 -1
  110. package/dest/utils.js +4 -21
  111. package/package.json +9 -6
  112. package/src/acvm/acvm.ts +156 -0
  113. package/src/acvm/acvm_types.ts +11 -0
  114. package/src/acvm/deserialize.ts +44 -0
  115. package/src/acvm/index.ts +5 -0
  116. package/src/acvm/oracle/debug.ts +109 -0
  117. package/src/acvm/oracle/index.ts +17 -0
  118. package/src/acvm/oracle/oracle.ts +356 -0
  119. package/src/acvm/oracle/typed_oracle.ts +225 -0
  120. package/src/acvm/serialize.ts +75 -0
  121. package/src/avm/avm_context.ts +63 -0
  122. package/src/avm/avm_execution_environment.ts +98 -0
  123. package/src/avm/avm_machine_state.ts +93 -0
  124. package/src/avm/avm_memory_types.ts +324 -0
  125. package/src/avm/avm_message_call_result.ts +29 -0
  126. package/src/avm/avm_simulator.ts +87 -0
  127. package/src/avm/errors.ts +57 -0
  128. package/src/avm/fixtures/index.ts +115 -0
  129. package/src/avm/journal/host_storage.ts +14 -0
  130. package/src/avm/journal/index.ts +2 -0
  131. package/src/avm/journal/journal.ts +231 -0
  132. package/src/avm/journal/nullifiers.ts +170 -0
  133. package/src/avm/journal/public_storage.ts +149 -0
  134. package/src/avm/journal/trace.ts +223 -0
  135. package/src/avm/journal/trace_types.ts +79 -0
  136. package/src/avm/opcodes/.eslintrc.cjs +8 -0
  137. package/src/avm/opcodes/accrued_substate.ts +214 -0
  138. package/src/avm/opcodes/addressing_mode.ts +66 -0
  139. package/src/avm/opcodes/arithmetic.ts +79 -0
  140. package/src/avm/opcodes/bitwise.ts +129 -0
  141. package/src/avm/opcodes/comparators.ts +69 -0
  142. package/src/avm/opcodes/control_flow.ts +129 -0
  143. package/src/avm/opcodes/environment_getters.ts +201 -0
  144. package/src/avm/opcodes/external_calls.ts +122 -0
  145. package/src/avm/opcodes/hashing.ts +170 -0
  146. package/src/avm/opcodes/index.ts +10 -0
  147. package/src/avm/opcodes/instruction.ts +64 -0
  148. package/src/avm/opcodes/instruction_impl.ts +52 -0
  149. package/src/avm/opcodes/memory.ts +194 -0
  150. package/src/avm/opcodes/storage.ts +79 -0
  151. package/src/avm/serialization/buffer_cursor.ts +109 -0
  152. package/src/avm/serialization/bytecode_serialization.ts +179 -0
  153. package/src/avm/serialization/instruction_serialization.ts +170 -0
  154. package/src/avm/temporary_executor_migration.ts +109 -0
  155. package/src/client/client_execution_context.ts +502 -0
  156. package/src/client/db_oracle.ts +192 -0
  157. package/src/client/execution_note_cache.ts +90 -0
  158. package/src/client/execution_result.ts +89 -0
  159. package/src/client/index.ts +3 -0
  160. package/src/client/pick_notes.ts +125 -0
  161. package/src/client/private_execution.ts +79 -0
  162. package/src/client/simulator.ts +317 -0
  163. package/src/client/unconstrained_execution.ts +49 -0
  164. package/src/client/view_data_oracle.ts +253 -0
  165. package/src/common/errors.ts +61 -0
  166. package/src/common/index.ts +3 -0
  167. package/src/common/packed_args_cache.ts +55 -0
  168. package/src/common/side_effect_counter.ts +12 -0
  169. package/src/index.ts +3 -0
  170. package/src/public/db.ts +100 -0
  171. package/src/public/execution.ts +161 -0
  172. package/src/public/executor.ts +178 -0
  173. package/src/public/index.ts +9 -0
  174. package/src/public/public_execution_context.ts +241 -0
  175. package/src/public/state_actions.ts +100 -0
  176. package/src/test/utils.ts +38 -0
  177. package/src/utils.ts +18 -0
@@ -0,0 +1,55 @@
1
+ import { PackedArguments } from '@aztec/circuit-types';
2
+ import { Fr } from '@aztec/circuits.js';
3
+
4
+ /**
5
+ * A cache for packed arguments during transaction execution.
6
+ */
7
+ export class PackedArgsCache {
8
+ private cache: Map<bigint, Fr[]>;
9
+
10
+ constructor(initialArguments: PackedArguments[] = []) {
11
+ this.cache = new Map();
12
+ for (const initialArg of initialArguments) {
13
+ this.cache.set(initialArg.hash.toBigInt(), initialArg.args);
14
+ }
15
+ }
16
+
17
+ /**
18
+ * Creates a new packed arguments cache.
19
+ * @param initialArguments - The initial arguments to add to the cache.
20
+ * @returns The new packed arguments cache.
21
+ */
22
+ public static create(initialArguments: PackedArguments[] = []) {
23
+ return new PackedArgsCache(initialArguments);
24
+ }
25
+
26
+ /**
27
+ * Unpacks packed arguments.
28
+ * @param hash - The hash of the packed arguments.
29
+ * @returns The unpacked arguments.
30
+ */
31
+ public unpack(hash: Fr): Fr[] {
32
+ if (hash.equals(Fr.ZERO)) {
33
+ return [];
34
+ }
35
+ const packedArgs = this.cache.get(hash.value);
36
+ if (!packedArgs) {
37
+ throw new Error(`Packed arguments for hash ${hash.toString()} not found in cache`);
38
+ }
39
+ return packedArgs;
40
+ }
41
+
42
+ /**
43
+ * Packs arguments.
44
+ * @param args - The arguments to pack.
45
+ * @returns The hash of the packed arguments.
46
+ */
47
+ public pack(args: Fr[]) {
48
+ if (args.length === 0) {
49
+ return Fr.ZERO;
50
+ }
51
+ const packedArguments = PackedArguments.fromArgs(args);
52
+ this.cache.set(packedArguments.hash.value, packedArguments.args);
53
+ return packedArguments.hash;
54
+ }
55
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Keep track of the number of side effects across execution contexts.
3
+ */
4
+ export class SideEffectCounter {
5
+ constructor(private value = 0) {}
6
+
7
+ count() {
8
+ const value = this.value;
9
+ this.value++;
10
+ return value;
11
+ }
12
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './client/index.js';
2
+ export * from './acvm/index.js';
3
+ export * from './public/index.js';
@@ -0,0 +1,100 @@
1
+ import { NullifierMembershipWitness } from '@aztec/circuit-types';
2
+ import { EthAddress, FunctionSelector, L1_TO_L2_MSG_TREE_HEIGHT } from '@aztec/circuits.js';
3
+ import { AztecAddress } from '@aztec/foundation/aztec-address';
4
+ import { Fr } from '@aztec/foundation/fields';
5
+
6
+ import { MessageLoadOracleInputs } from '../acvm/index.js';
7
+
8
+ /**
9
+ * Database interface for providing access to public state.
10
+ */
11
+ export interface PublicStateDB {
12
+ /**
13
+ * Reads a value from public storage, returning zero if none.
14
+ * @param contract - Owner of the storage.
15
+ * @param slot - Slot to read in the contract storage.
16
+ * @returns The current value in the storage slot.
17
+ */
18
+ storageRead(contract: AztecAddress, slot: Fr): Promise<Fr>;
19
+
20
+ /**
21
+ * Records a write to public storage.
22
+ * @param contract - Owner of the storage.
23
+ * @param slot - Slot to read in the contract storage.
24
+ * @param newValue - The new value to store.
25
+ * @returns Nothing.
26
+ */
27
+ storageWrite(contract: AztecAddress, slot: Fr, newValue: Fr): Promise<void>;
28
+
29
+ /**
30
+ * Commit the pending changes to the DB.
31
+ * @returns Nothing.
32
+ */
33
+ commit(): Promise<void>;
34
+
35
+ /**
36
+ * Rollback the pending changes.
37
+ * @returns Nothing.
38
+ */
39
+ rollback(): Promise<void>;
40
+ }
41
+
42
+ /**
43
+ * Database interface for providing access to public contract data.
44
+ */
45
+ export interface PublicContractsDB {
46
+ /**
47
+ * Returns the brillig (public bytecode) of a function.
48
+ * @param address - The contract address that owns this function.
49
+ * @param selector - The selector for the function.
50
+ * @returns The bytecode or undefined if not found.
51
+ */
52
+ getBytecode(address: AztecAddress, selector: FunctionSelector): Promise<Buffer | undefined>;
53
+
54
+ /**
55
+ * Returns whether a function is internal or not.
56
+ * @param address - The contract address that owns this function.
57
+ * @param selector - The selector for the function.
58
+ * @returns The `isInternal` flag found, undefined if not found.
59
+ */
60
+ getIsInternal(address: AztecAddress, selector: FunctionSelector): Promise<boolean | undefined>;
61
+
62
+ /**
63
+ * Returns the portal contract address for an L2 address.
64
+ * @param address - The L2 contract address.
65
+ * @returns The portal contract address or undefined if not found.
66
+ */
67
+ getPortalContractAddress(address: AztecAddress): Promise<EthAddress | undefined>;
68
+ }
69
+
70
+ /** Database interface for providing access to commitment tree, l1 to l2 message tree, and nullifier tree. */
71
+ export interface CommitmentsDB {
72
+ /**
73
+ * Gets a confirmed L1 to L2 message for the given entry key.
74
+ * TODO(Maddiaa): Can be combined with aztec-node method that does the same thing.
75
+ * @param entryKey - The entry key.
76
+ * @returns - The l1 to l2 message object
77
+ */
78
+ getL1ToL2MembershipWitness(entryKey: Fr): Promise<MessageLoadOracleInputs<typeof L1_TO_L2_MSG_TREE_HEIGHT>>;
79
+
80
+ /**
81
+ * Gets the index of a commitment in the note hash tree.
82
+ * @param commitment - The commitment.
83
+ * @returns - The index of the commitment. Undefined if it does not exist in the tree.
84
+ */
85
+ getCommitmentIndex(commitment: Fr): Promise<bigint | undefined>;
86
+
87
+ /**
88
+ * Gets the index of a nullifier in the nullifier tree.
89
+ * @param nullifier - The nullifier.
90
+ * @returns - The index of the nullifier. Undefined if it does not exist in the tree.
91
+ */
92
+ getNullifierIndex(nullifier: Fr): Promise<bigint | undefined>;
93
+
94
+ /**
95
+ * Returns a nullifier membership witness for the given nullifier or undefined if not found.
96
+ * REFACTOR: Same as getL1ToL2MembershipWitness, can be combined with aztec-node method that does almost the same thing.
97
+ * @param nullifier - Nullifier we're looking for.
98
+ */
99
+ getNullifierMembershipWitnessAtLatestBlock(nullifier: Fr): Promise<NullifierMembershipWitness | undefined>;
100
+ }
@@ -0,0 +1,161 @@
1
+ import { FunctionL2Logs } from '@aztec/circuit-types';
2
+ import {
3
+ AztecAddress,
4
+ CallContext,
5
+ ContractStorageRead,
6
+ ContractStorageUpdateRequest,
7
+ Fr,
8
+ FunctionData,
9
+ L2ToL1Message,
10
+ PublicDataRead,
11
+ PublicDataUpdateRequest,
12
+ SideEffect,
13
+ SideEffectLinkedToNoteHash,
14
+ } from '@aztec/circuits.js';
15
+ import { computePublicDataTreeLeafSlot, computePublicDataTreeValue } from '@aztec/circuits.js/hash';
16
+
17
+ /**
18
+ * The public function execution result.
19
+ */
20
+ export interface PublicExecutionResult {
21
+ /** The execution that triggered this result. */
22
+ execution: PublicExecution;
23
+ /** The return values of the function. */
24
+ returnValues: Fr[];
25
+ /** The new note hashes to be inserted into the note hashes tree. */
26
+ newNoteHashes: SideEffect[];
27
+ /** The new l2 to l1 messages generated in this call. */
28
+ newL2ToL1Messages: L2ToL1Message[];
29
+ /** The new nullifiers to be inserted into the nullifier tree. */
30
+ newNullifiers: SideEffectLinkedToNoteHash[];
31
+ /** The contract storage reads performed by the function. */
32
+ contractStorageReads: ContractStorageRead[];
33
+ /** The contract storage update requests performed by the function. */
34
+ contractStorageUpdateRequests: ContractStorageUpdateRequest[];
35
+ /** The results of nested calls. */
36
+ nestedExecutions: this[];
37
+ /**
38
+ * Unencrypted logs emitted during execution of this function call.
39
+ * Note: These are preimages to `unencryptedLogsHash`.
40
+ */
41
+ unencryptedLogs: FunctionL2Logs;
42
+ }
43
+
44
+ /**
45
+ * The execution of a public function.
46
+ */
47
+ export interface PublicExecution {
48
+ /** Address of the contract being executed. */
49
+ contractAddress: AztecAddress;
50
+ /** Function of the contract being called. */
51
+ functionData: FunctionData;
52
+ /** Arguments for the call. */
53
+ args: Fr[];
54
+ /** Context of the call. */
55
+ callContext: CallContext;
56
+ }
57
+
58
+ /**
59
+ * Returns if the input is a public execution result and not just a public execution.
60
+ * @param input - Public execution or public execution result.
61
+ * @returns Whether the input is a public execution result and not just a public execution.
62
+ */
63
+ export function isPublicExecutionResult(
64
+ input: PublicExecution | PublicExecutionResult,
65
+ ): input is PublicExecutionResult {
66
+ return !!(input as PublicExecutionResult).execution;
67
+ }
68
+
69
+ /**
70
+ * Collect all public storage reads across all nested executions
71
+ * and convert them to PublicDataReads (to match kernel output).
72
+ * @param execResult - The topmost execution result.
73
+ * @returns All public data reads (in execution order).
74
+ */
75
+ export function collectPublicDataReads(execResult: PublicExecutionResult): PublicDataRead[] {
76
+ // HACK(#1622): part of temporary hack - may be able to remove this function after public state ordering is fixed
77
+ const contractAddress = execResult.execution.callContext.storageContractAddress;
78
+
79
+ const thisExecPublicDataReads = execResult.contractStorageReads.map(read =>
80
+ contractStorageReadToPublicDataRead(read, contractAddress),
81
+ );
82
+ const unsorted = [
83
+ ...thisExecPublicDataReads,
84
+ ...[...execResult.nestedExecutions].flatMap(result => collectPublicDataReads(result)),
85
+ ];
86
+ return unsorted.sort((a, b) => a.sideEffectCounter! - b.sideEffectCounter!);
87
+ }
88
+
89
+ /**
90
+ * Collect all public storage update requests across all nested executions
91
+ * and convert them to PublicDataUpdateRequests (to match kernel output).
92
+ * @param execResult - The topmost execution result.
93
+ * @returns All public data reads (in execution order).
94
+ */
95
+ export function collectPublicDataUpdateRequests(execResult: PublicExecutionResult): PublicDataUpdateRequest[] {
96
+ // HACK(#1622): part of temporary hack - may be able to remove this function after public state ordering is fixed
97
+ const contractAddress = execResult.execution.callContext.storageContractAddress;
98
+
99
+ const thisExecPublicDataUpdateRequests = execResult.contractStorageUpdateRequests.map(update =>
100
+ contractStorageUpdateRequestToPublicDataUpdateRequest(update, contractAddress),
101
+ );
102
+ const unsorted = [
103
+ ...thisExecPublicDataUpdateRequests,
104
+ ...[...execResult.nestedExecutions].flatMap(result => collectPublicDataUpdateRequests(result)),
105
+ ];
106
+ return unsorted.sort((a, b) => a.sideEffectCounter! - b.sideEffectCounter!);
107
+ }
108
+
109
+ /**
110
+ * Convert a Contract Storage Read to a Public Data Read.
111
+ * @param read - the contract storage read to convert
112
+ * @param contractAddress - the contract address of the read
113
+ * @returns The public data read.
114
+ */
115
+ function contractStorageReadToPublicDataRead(read: ContractStorageRead, contractAddress: AztecAddress): PublicDataRead {
116
+ return new PublicDataRead(
117
+ computePublicDataTreeLeafSlot(contractAddress, read.storageSlot),
118
+ computePublicDataTreeValue(read.currentValue),
119
+ read.sideEffectCounter!,
120
+ );
121
+ }
122
+
123
+ /**
124
+ * Convert a Contract Storage Update Request to a Public Data Update Request.
125
+ * @param update - the contract storage update request to convert
126
+ * @param contractAddress - the contract address of the data update request.
127
+ * @returns The public data update request.
128
+ */
129
+ function contractStorageUpdateRequestToPublicDataUpdateRequest(
130
+ update: ContractStorageUpdateRequest,
131
+ contractAddress: AztecAddress,
132
+ ): PublicDataUpdateRequest {
133
+ return new PublicDataUpdateRequest(
134
+ computePublicDataTreeLeafSlot(contractAddress, update.storageSlot),
135
+ computePublicDataTreeValue(update.newValue),
136
+ update.sideEffectCounter!,
137
+ );
138
+ }
139
+
140
+ /**
141
+ * Checks whether the child execution result is valid for a static call (no state modifications).
142
+ * @param executionResult - The execution result of a public function
143
+ */
144
+
145
+ export function checkValidStaticCall(
146
+ newNoteHashes: SideEffect[],
147
+ newNullifiers: SideEffectLinkedToNoteHash[],
148
+ contractStorageUpdateRequests: ContractStorageUpdateRequest[],
149
+ newL2ToL1Messages: L2ToL1Message[],
150
+ unencryptedLogs: FunctionL2Logs,
151
+ ) {
152
+ if (
153
+ contractStorageUpdateRequests.length > 0 ||
154
+ newNoteHashes.length > 0 ||
155
+ newNullifiers.length > 0 ||
156
+ newL2ToL1Messages.length > 0 ||
157
+ unencryptedLogs.logs.length > 0
158
+ ) {
159
+ throw new Error('Static call cannot update the state, emit L2->L1 messages or generate logs');
160
+ }
161
+ }
@@ -0,0 +1,178 @@
1
+ import { GlobalVariables, Header, PublicCircuitPublicInputs } from '@aztec/circuits.js';
2
+ import { createDebugLogger } from '@aztec/foundation/log';
3
+
4
+ import { Oracle, acvm, extractCallStack, extractReturnWitness } from '../acvm/index.js';
5
+ import { AvmContext } from '../avm/avm_context.js';
6
+ import { AvmMachineState } from '../avm/avm_machine_state.js';
7
+ import { AvmSimulator } from '../avm/avm_simulator.js';
8
+ import { HostStorage } from '../avm/journal/host_storage.js';
9
+ import { AvmPersistableStateManager } from '../avm/journal/index.js';
10
+ import {
11
+ temporaryConvertAvmResults,
12
+ temporaryCreateAvmExecutionEnvironment,
13
+ } from '../avm/temporary_executor_migration.js';
14
+ import { ExecutionError, createSimulationError } from '../common/errors.js';
15
+ import { SideEffectCounter } from '../common/index.js';
16
+ import { PackedArgsCache } from '../common/packed_args_cache.js';
17
+ import { AcirSimulator } from '../index.js';
18
+ import { CommitmentsDB, PublicContractsDB, PublicStateDB } from './db.js';
19
+ import { PublicExecution, PublicExecutionResult, checkValidStaticCall } from './execution.js';
20
+ import { PublicExecutionContext } from './public_execution_context.js';
21
+
22
+ /**
23
+ * Execute a public function and return the execution result.
24
+ */
25
+ export async function executePublicFunction(
26
+ context: PublicExecutionContext,
27
+ acir: Buffer,
28
+ log = createDebugLogger('aztec:simulator:public_execution'),
29
+ ): Promise<PublicExecutionResult> {
30
+ const execution = context.execution;
31
+ const { contractAddress, functionData } = execution;
32
+ const selector = functionData.selector;
33
+ log(`Executing public external function ${contractAddress.toString()}:${selector}`);
34
+
35
+ const initialWitness = context.getInitialWitness();
36
+ const acvmCallback = new Oracle(context);
37
+ const { partialWitness } = await acvm(await AcirSimulator.getSolver(), acir, initialWitness, acvmCallback).catch(
38
+ (err: Error) => {
39
+ throw new ExecutionError(
40
+ err.message,
41
+ {
42
+ contractAddress,
43
+ functionSelector: selector,
44
+ },
45
+ extractCallStack(err),
46
+ { cause: err },
47
+ );
48
+ },
49
+ );
50
+
51
+ const returnWitness = extractReturnWitness(acir, partialWitness);
52
+ const {
53
+ returnValues,
54
+ newL2ToL1Msgs,
55
+ newNoteHashes: newNoteHashesPadded,
56
+ newNullifiers: newNullifiersPadded,
57
+ } = PublicCircuitPublicInputs.fromFields(returnWitness);
58
+
59
+ const newL2ToL1Messages = newL2ToL1Msgs.filter(v => !v.isEmpty());
60
+ const newNoteHashes = newNoteHashesPadded.filter(v => !v.isEmpty());
61
+ const newNullifiers = newNullifiersPadded.filter(v => !v.isEmpty());
62
+
63
+ const { contractStorageReads, contractStorageUpdateRequests } = context.getStorageActionData();
64
+
65
+ log(
66
+ `Contract storage reads: ${contractStorageReads
67
+ .map(r => r.toFriendlyJSON() + ` - sec: ${r.sideEffectCounter}`)
68
+ .join(', ')}`,
69
+ );
70
+ log(
71
+ `Contract storage update requests: ${contractStorageUpdateRequests
72
+ .map(r => r.toFriendlyJSON() + ` - sec: ${r.sideEffectCounter}`)
73
+ .join(', ')}`,
74
+ );
75
+
76
+ const nestedExecutions = context.getNestedExecutions();
77
+ const unencryptedLogs = context.getUnencryptedLogs();
78
+
79
+ return {
80
+ execution,
81
+ newNoteHashes,
82
+ newL2ToL1Messages,
83
+ newNullifiers,
84
+ contractStorageReads,
85
+ contractStorageUpdateRequests,
86
+ returnValues,
87
+ nestedExecutions,
88
+ unencryptedLogs,
89
+ };
90
+ }
91
+
92
+ /**
93
+ * Handles execution of public functions.
94
+ */
95
+ export class PublicExecutor {
96
+ constructor(
97
+ private readonly stateDb: PublicStateDB,
98
+ private readonly contractsDb: PublicContractsDB,
99
+ private readonly commitmentsDb: CommitmentsDB,
100
+ private readonly header: Header,
101
+ ) {}
102
+
103
+ /**
104
+ * Executes a public execution request.
105
+ * @param execution - The execution to run.
106
+ * @param globalVariables - The global variables to use.
107
+ * @returns The result of the run plus all nested runs.
108
+ */
109
+ public async simulate(execution: PublicExecution, globalVariables: GlobalVariables): Promise<PublicExecutionResult> {
110
+ const selector = execution.functionData.selector;
111
+ const acir = await this.contractsDb.getBytecode(execution.contractAddress, selector);
112
+ if (!acir) {
113
+ throw new Error(`Bytecode not found for ${execution.contractAddress}:${selector}`);
114
+ }
115
+
116
+ // Functions can request to pack arguments before calling other functions.
117
+ // We use this cache to hold the packed arguments.
118
+ const packedArgs = PackedArgsCache.create([]);
119
+
120
+ const sideEffectCounter = new SideEffectCounter();
121
+
122
+ const context = new PublicExecutionContext(
123
+ execution,
124
+ this.header,
125
+ globalVariables,
126
+ packedArgs,
127
+ sideEffectCounter,
128
+ this.stateDb,
129
+ this.contractsDb,
130
+ this.commitmentsDb,
131
+ );
132
+
133
+ let executionResult;
134
+
135
+ try {
136
+ executionResult = await executePublicFunction(context, acir);
137
+ } catch (err) {
138
+ throw createSimulationError(err instanceof Error ? err : new Error('Unknown error during public execution'));
139
+ }
140
+
141
+ if (executionResult.execution.callContext.isStaticCall) {
142
+ checkValidStaticCall(
143
+ executionResult.newNoteHashes,
144
+ executionResult.newNullifiers,
145
+ executionResult.contractStorageUpdateRequests,
146
+ executionResult.newL2ToL1Messages,
147
+ executionResult.unencryptedLogs,
148
+ );
149
+ }
150
+
151
+ return executionResult;
152
+ }
153
+
154
+ /**
155
+ * Executes a public execution request in the avm.
156
+ * @param execution - The execution to run.
157
+ * @param globalVariables - The global variables to use.
158
+ * @returns The result of the run plus all nested runs.
159
+ */
160
+ public async simulateAvm(
161
+ execution: PublicExecution,
162
+ globalVariables: GlobalVariables,
163
+ ): Promise<PublicExecutionResult> {
164
+ // Temporary code to construct the AVM context
165
+ // These data structures will permiate across the simulator when the public executor is phased out
166
+ const hostStorage = new HostStorage(this.stateDb, this.contractsDb, this.commitmentsDb);
167
+ const worldStateJournal = new AvmPersistableStateManager(hostStorage);
168
+ const executionEnv = temporaryCreateAvmExecutionEnvironment(execution, globalVariables);
169
+ const machineState = new AvmMachineState(0, 0, 0);
170
+
171
+ const context = new AvmContext(worldStateJournal, executionEnv, machineState);
172
+ const simulator = new AvmSimulator(context);
173
+
174
+ const result = await simulator.execute();
175
+ const newWorldState = context.persistableState.flush();
176
+ return temporaryConvertAvmResults(execution, newWorldState, result);
177
+ }
178
+ }
@@ -0,0 +1,9 @@
1
+ export * from './db.js';
2
+ export {
3
+ PublicExecution,
4
+ PublicExecutionResult,
5
+ isPublicExecutionResult,
6
+ collectPublicDataReads,
7
+ collectPublicDataUpdateRequests,
8
+ } from './execution.js';
9
+ export { PublicExecutor } from './executor.js';