@aztec-labs/simulator 6.0.0-nightly.20260829

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 (215) hide show
  1. package/README.md +57 -0
  2. package/dest/client.d.ts +6 -0
  3. package/dest/client.d.ts.map +1 -0
  4. package/dest/client.js +3 -0
  5. package/dest/common/errors.d.ts +51 -0
  6. package/dest/common/errors.d.ts.map +1 -0
  7. package/dest/common/errors.js +154 -0
  8. package/dest/common/index.d.ts +3 -0
  9. package/dest/common/index.d.ts.map +1 -0
  10. package/dest/common/index.js +2 -0
  11. package/dest/common/stats/index.d.ts +2 -0
  12. package/dest/common/stats/index.d.ts.map +1 -0
  13. package/dest/common/stats/index.js +1 -0
  14. package/dest/common/stats/stats.d.ts +4 -0
  15. package/dest/common/stats/stats.d.ts.map +1 -0
  16. package/dest/common/stats/stats.js +10 -0
  17. package/dest/private/acvm/acvm.d.ts +43 -0
  18. package/dest/private/acvm/acvm.d.ts.map +1 -0
  19. package/dest/private/acvm/acvm.js +65 -0
  20. package/dest/private/acvm/acvm_types.d.ts +10 -0
  21. package/dest/private/acvm/acvm_types.d.ts.map +1 -0
  22. package/dest/private/acvm/acvm_types.js +3 -0
  23. package/dest/private/acvm/deserialize.d.ts +35 -0
  24. package/dest/private/acvm/deserialize.d.ts.map +1 -0
  25. package/dest/private/acvm/deserialize.js +50 -0
  26. package/dest/private/acvm/index.d.ts +5 -0
  27. package/dest/private/acvm/index.d.ts.map +1 -0
  28. package/dest/private/acvm/index.js +4 -0
  29. package/dest/private/acvm/serialize.d.ts +41 -0
  30. package/dest/private/acvm/serialize.d.ts.map +1 -0
  31. package/dest/private/acvm/serialize.js +99 -0
  32. package/dest/private/acvm_native.d.ts +41 -0
  33. package/dest/private/acvm_native.d.ts.map +1 -0
  34. package/dest/private/acvm_native.js +147 -0
  35. package/dest/private/acvm_wasm.d.ts +16 -0
  36. package/dest/private/acvm_wasm.d.ts.map +1 -0
  37. package/dest/private/acvm_wasm.js +67 -0
  38. package/dest/private/acvm_wasm_with_blobs.d.ts +20 -0
  39. package/dest/private/acvm_wasm_with_blobs.d.ts.map +1 -0
  40. package/dest/private/acvm_wasm_with_blobs.js +35 -0
  41. package/dest/private/circuit_simulator.d.ts +35 -0
  42. package/dest/private/circuit_simulator.d.ts.map +1 -0
  43. package/dest/private/circuit_simulator.js +43 -0
  44. package/dest/private/factory.d.ts +12 -0
  45. package/dest/private/factory.d.ts.map +1 -0
  46. package/dest/private/factory.js +30 -0
  47. package/dest/public/avm/testing/account_proof_fetcher.d.ts +2 -0
  48. package/dest/public/avm/testing/account_proof_fetcher.d.ts.map +1 -0
  49. package/dest/public/avm/testing/account_proof_fetcher.js +152 -0
  50. package/dest/public/avm/testing/base_avm_simulation_tester.d.ts +39 -0
  51. package/dest/public/avm/testing/base_avm_simulation_tester.d.ts.map +1 -0
  52. package/dest/public/avm/testing/base_avm_simulation_tester.js +121 -0
  53. package/dest/public/avm/testing/utils.d.ts +32 -0
  54. package/dest/public/avm/testing/utils.d.ts.map +1 -0
  55. package/dest/public/avm/testing/utils.js +66 -0
  56. package/dest/public/avm_simulator.d.ts +28 -0
  57. package/dest/public/avm_simulator.d.ts.map +1 -0
  58. package/dest/public/avm_simulator.js +4 -0
  59. package/dest/public/avm_simulator_pool.d.ts +84 -0
  60. package/dest/public/avm_simulator_pool.d.ts.map +1 -0
  61. package/dest/public/avm_simulator_pool.js +311 -0
  62. package/dest/public/cdb_ipc_server.d.ts +40 -0
  63. package/dest/public/cdb_ipc_server.d.ts.map +1 -0
  64. package/dest/public/cdb_ipc_server.js +153 -0
  65. package/dest/public/contracts_db_checkpoint.d.ts +16 -0
  66. package/dest/public/contracts_db_checkpoint.d.ts.map +1 -0
  67. package/dest/public/contracts_db_checkpoint.js +30 -0
  68. package/dest/public/db_interfaces.d.ts +68 -0
  69. package/dest/public/db_interfaces.d.ts.map +1 -0
  70. package/dest/public/db_interfaces.js +3 -0
  71. package/dest/public/executor_metrics.d.ts +21 -0
  72. package/dest/public/executor_metrics.d.ts.map +1 -0
  73. package/dest/public/executor_metrics.js +64 -0
  74. package/dest/public/executor_metrics_interface.d.ts +10 -0
  75. package/dest/public/executor_metrics_interface.d.ts.map +1 -0
  76. package/dest/public/executor_metrics_interface.js +1 -0
  77. package/dest/public/fixtures/amm_test.d.ts +10 -0
  78. package/dest/public/fixtures/amm_test.d.ts.map +1 -0
  79. package/dest/public/fixtures/amm_test.js +213 -0
  80. package/dest/public/fixtures/bulk_test.d.ts +6 -0
  81. package/dest/public/fixtures/bulk_test.d.ts.map +1 -0
  82. package/dest/public/fixtures/bulk_test.js +289 -0
  83. package/dest/public/fixtures/custom_bytecode_tester.d.ts +34 -0
  84. package/dest/public/fixtures/custom_bytecode_tester.d.ts.map +1 -0
  85. package/dest/public/fixtures/custom_bytecode_tester.js +53 -0
  86. package/dest/public/fixtures/index.d.ts +10 -0
  87. package/dest/public/fixtures/index.d.ts.map +1 -0
  88. package/dest/public/fixtures/index.js +9 -0
  89. package/dest/public/fixtures/public_processor_test_env.d.ts +30 -0
  90. package/dest/public/fixtures/public_processor_test_env.d.ts.map +1 -0
  91. package/dest/public/fixtures/public_processor_test_env.js +73 -0
  92. package/dest/public/fixtures/public_tx_simulation_tester.d.ts +77 -0
  93. package/dest/public/fixtures/public_tx_simulation_tester.d.ts.map +1 -0
  94. package/dest/public/fixtures/public_tx_simulation_tester.js +176 -0
  95. package/dest/public/fixtures/simple_contract_data_source.d.ts +36 -0
  96. package/dest/public/fixtures/simple_contract_data_source.d.ts.map +1 -0
  97. package/dest/public/fixtures/simple_contract_data_source.js +95 -0
  98. package/dest/public/fixtures/token_test.d.ts +12 -0
  99. package/dest/public/fixtures/token_test.d.ts.map +1 -0
  100. package/dest/public/fixtures/token_test.js +96 -0
  101. package/dest/public/fixtures/utils.d.ts +26 -0
  102. package/dest/public/fixtures/utils.d.ts.map +1 -0
  103. package/dest/public/fixtures/utils.js +170 -0
  104. package/dest/public/fuzzing/avm_fuzzer_simulator.d.ts +65 -0
  105. package/dest/public/fuzzing/avm_fuzzer_simulator.d.ts.map +1 -0
  106. package/dest/public/fuzzing/avm_fuzzer_simulator.js +181 -0
  107. package/dest/public/fuzzing/avm_simulator_bin.d.ts +2 -0
  108. package/dest/public/fuzzing/avm_simulator_bin.d.ts.map +1 -0
  109. package/dest/public/fuzzing/avm_simulator_bin.js +108 -0
  110. package/dest/public/index.d.ts +10 -0
  111. package/dest/public/index.d.ts.map +1 -0
  112. package/dest/public/index.js +6 -0
  113. package/dest/public/public_db_sources.d.ts +83 -0
  114. package/dest/public/public_db_sources.d.ts.map +1 -0
  115. package/dest/public/public_db_sources.js +297 -0
  116. package/dest/public/public_errors.d.ts +12 -0
  117. package/dest/public/public_errors.d.ts.map +1 -0
  118. package/dest/public/public_errors.js +13 -0
  119. package/dest/public/public_processor/guarded_merkle_tree.d.ts +52 -0
  120. package/dest/public/public_processor/guarded_merkle_tree.d.ts.map +1 -0
  121. package/dest/public/public_processor/guarded_merkle_tree.js +116 -0
  122. package/dest/public/public_processor/public_processor.d.ts +71 -0
  123. package/dest/public/public_processor/public_processor.d.ts.map +1 -0
  124. package/dest/public/public_processor/public_processor.js +858 -0
  125. package/dest/public/public_processor/public_processor_metrics.d.ts +30 -0
  126. package/dest/public/public_processor/public_processor_metrics.d.ts.map +1 -0
  127. package/dest/public/public_processor/public_processor_metrics.js +116 -0
  128. package/dest/public/public_tx_simulator/dumping_public_tx_simulator.d.ts +17 -0
  129. package/dest/public/public_tx_simulator/dumping_public_tx_simulator.d.ts.map +1 -0
  130. package/dest/public/public_tx_simulator/dumping_public_tx_simulator.js +39 -0
  131. package/dest/public/public_tx_simulator/factories.d.ts +14 -0
  132. package/dest/public/public_tx_simulator/factories.d.ts.map +1 -0
  133. package/dest/public/public_tx_simulator/factories.js +28 -0
  134. package/dest/public/public_tx_simulator/index.d.ts +6 -0
  135. package/dest/public/public_tx_simulator/index.d.ts.map +1 -0
  136. package/dest/public/public_tx_simulator/index.js +3 -0
  137. package/dest/public/public_tx_simulator/public_tx_simulator.d.ts +53 -0
  138. package/dest/public/public_tx_simulator/public_tx_simulator.d.ts.map +1 -0
  139. package/dest/public/public_tx_simulator/public_tx_simulator.js +127 -0
  140. package/dest/public/public_tx_simulator/public_tx_simulator_base.d.ts +24 -0
  141. package/dest/public/public_tx_simulator/public_tx_simulator_base.d.ts.map +1 -0
  142. package/dest/public/public_tx_simulator/public_tx_simulator_base.js +31 -0
  143. package/dest/public/public_tx_simulator/public_tx_simulator_interface.d.ts +32 -0
  144. package/dest/public/public_tx_simulator/public_tx_simulator_interface.d.ts.map +1 -0
  145. package/dest/public/public_tx_simulator/public_tx_simulator_interface.js +1 -0
  146. package/dest/public/side_effect_errors.d.ts +44 -0
  147. package/dest/public/side_effect_errors.d.ts.map +1 -0
  148. package/dest/public/side_effect_errors.js +75 -0
  149. package/dest/public/test_executor_metrics.d.ts +56 -0
  150. package/dest/public/test_executor_metrics.d.ts.map +1 -0
  151. package/dest/public/test_executor_metrics.js +305 -0
  152. package/dest/public/unique_class_ids.d.ts +37 -0
  153. package/dest/public/unique_class_ids.d.ts.map +1 -0
  154. package/dest/public/unique_class_ids.js +61 -0
  155. package/dest/public/utils.d.ts +3 -0
  156. package/dest/public/utils.d.ts.map +1 -0
  157. package/dest/public/utils.js +18 -0
  158. package/dest/server.d.ts +7 -0
  159. package/dest/server.d.ts.map +1 -0
  160. package/dest/server.js +5 -0
  161. package/package.json +109 -0
  162. package/src/client.ts +5 -0
  163. package/src/common/errors.ts +219 -0
  164. package/src/common/index.ts +2 -0
  165. package/src/common/stats/index.ts +1 -0
  166. package/src/common/stats/stats.ts +20 -0
  167. package/src/private/acvm/acvm.ts +114 -0
  168. package/src/private/acvm/acvm_types.ts +11 -0
  169. package/src/private/acvm/deserialize.ts +58 -0
  170. package/src/private/acvm/index.ts +4 -0
  171. package/src/private/acvm/serialize.ts +123 -0
  172. package/src/private/acvm_native.ts +200 -0
  173. package/src/private/acvm_wasm.ts +80 -0
  174. package/src/private/acvm_wasm_with_blobs.ts +55 -0
  175. package/src/private/circuit_simulator.ts +91 -0
  176. package/src/private/factory.ts +40 -0
  177. package/src/public/avm/testing/account_proof.json +553 -0
  178. package/src/public/avm/testing/account_proof_fetcher.ts +166 -0
  179. package/src/public/avm/testing/base_avm_simulation_tester.ts +162 -0
  180. package/src/public/avm/testing/utils.ts +114 -0
  181. package/src/public/avm_simulator.ts +29 -0
  182. package/src/public/avm_simulator_pool.ts +355 -0
  183. package/src/public/cdb_ipc_server.ts +198 -0
  184. package/src/public/contracts_db_checkpoint.ts +41 -0
  185. package/src/public/db_interfaces.ts +76 -0
  186. package/src/public/executor_metrics.ts +102 -0
  187. package/src/public/executor_metrics_interface.ts +20 -0
  188. package/src/public/fixtures/amm_test.ts +331 -0
  189. package/src/public/fixtures/bulk_test.ts +194 -0
  190. package/src/public/fixtures/custom_bytecode_tester.ts +83 -0
  191. package/src/public/fixtures/index.ts +13 -0
  192. package/src/public/fixtures/public_processor_test_env.ts +88 -0
  193. package/src/public/fixtures/public_tx_simulation_tester.ts +315 -0
  194. package/src/public/fixtures/simple_contract_data_source.ts +122 -0
  195. package/src/public/fixtures/token_test.ts +148 -0
  196. package/src/public/fixtures/utils.ts +269 -0
  197. package/src/public/fuzzing/avm_fuzzer_simulator.ts +302 -0
  198. package/src/public/fuzzing/avm_simulator_bin.ts +156 -0
  199. package/src/public/index.ts +16 -0
  200. package/src/public/public_db_sources.ts +405 -0
  201. package/src/public/public_errors.ts +14 -0
  202. package/src/public/public_processor/guarded_merkle_tree.ts +161 -0
  203. package/src/public/public_processor/public_processor.ts +657 -0
  204. package/src/public/public_processor/public_processor_metrics.ts +138 -0
  205. package/src/public/public_tx_simulator/dumping_public_tx_simulator.ts +66 -0
  206. package/src/public/public_tx_simulator/factories.ts +61 -0
  207. package/src/public/public_tx_simulator/index.ts +8 -0
  208. package/src/public/public_tx_simulator/public_tx_simulator.ts +190 -0
  209. package/src/public/public_tx_simulator/public_tx_simulator_base.ts +37 -0
  210. package/src/public/public_tx_simulator/public_tx_simulator_interface.ts +33 -0
  211. package/src/public/side_effect_errors.ts +96 -0
  212. package/src/public/test_executor_metrics.ts +399 -0
  213. package/src/public/unique_class_ids.ts +79 -0
  214. package/src/public/utils.ts +16 -0
  215. package/src/server.ts +6 -0
@@ -0,0 +1,148 @@
1
+ import { Fr } from '@aztec-labs/foundation/curves/bn254';
2
+ import type { Logger } from '@aztec-labs/foundation/log';
3
+ import { Timer } from '@aztec-labs/foundation/timer';
4
+ import type { ContractArtifact } from '@aztec-labs/stdlib/abi';
5
+ import { AztecAddress } from '@aztec-labs/stdlib/aztec-address';
6
+ import type { ContractInstanceWithAddress } from '@aztec-labs/stdlib/contract';
7
+
8
+ import { PublicTxSimulationTester } from './public_tx_simulation_tester.js';
9
+
10
+ export type TokenTestOptions = {
11
+ /** Skip return value assertions in balance checks (useful for benchmarking with collectCallMetadata=false) */
12
+ skipReturnValueAssertions?: boolean;
13
+ };
14
+
15
+ export async function tokenTest(
16
+ tester: PublicTxSimulationTester,
17
+ logger: Logger,
18
+ tokenArtifact: ContractArtifact,
19
+ expectToBeTrue: (x: boolean) => void,
20
+ options: TokenTestOptions = {},
21
+ ) {
22
+ const timer = new Timer();
23
+
24
+ const admin = AztecAddress.fromNumberUnsafe(42);
25
+ const sender = AztecAddress.fromNumberUnsafe(111);
26
+ const receiver = AztecAddress.fromNumberUnsafe(222);
27
+
28
+ const token = await setUpToken(tester, tokenArtifact, admin, expectToBeTrue);
29
+
30
+ const mintAmount = 100n;
31
+ // EXECUTE! This means that if using AvmProvingTester subclass, it will PROVE the transaction!
32
+ const mintResult = await tester.executeTxWithLabel(
33
+ /*txLabel=*/ 'Token/mint_to_public',
34
+ /*sender=*/ admin,
35
+ /*setupCalls=*/ [],
36
+ /*appCalls=*/ [
37
+ {
38
+ address: token.address,
39
+ fnName: 'mint_to_public',
40
+ args: [/*to=*/ sender, mintAmount],
41
+ },
42
+ ],
43
+ );
44
+ expectToBeTrue(mintResult.revertCode.isOK());
45
+ await checkBalance(tester, token, sender, sender, mintAmount, expectToBeTrue, options);
46
+
47
+ const authwitNonce = new Fr(0);
48
+ const transferAmount = 50n;
49
+ // EXECUTE! This means that if using AvmProvingTester subclass, it will PROVE the transaction!
50
+ const transferResult = await tester.executeTxWithLabel(
51
+ /*txLabel=*/ 'Token/transfer_in_public',
52
+ /*sender=*/ sender,
53
+ /*setupCalls=*/ [],
54
+ /*appCalls=*/ [
55
+ {
56
+ address: token.address,
57
+ fnName: 'transfer_in_public',
58
+ args: [/*from=*/ sender, /*to=*/ receiver, transferAmount, authwitNonce],
59
+ },
60
+ ],
61
+ );
62
+ expectToBeTrue(transferResult.revertCode.isOK());
63
+ await checkBalance(tester, token, sender, sender, mintAmount - transferAmount, expectToBeTrue, options);
64
+ await checkBalance(tester, token, sender, receiver, transferAmount, expectToBeTrue, options);
65
+
66
+ // EXECUTE! This means that if using AvmProvingTester subclass, it will PROVE the transaction!
67
+ const burnResult = await tester.executeTxWithLabel(
68
+ /*txLabel=*/ 'Token/burn_public',
69
+ /*sender=*/ receiver,
70
+ /*setupCalls=*/ [],
71
+ /*appCalls=*/ [
72
+ {
73
+ address: token.address,
74
+ fnName: 'burn_public',
75
+ args: [/*from=*/ receiver, transferAmount, authwitNonce],
76
+ },
77
+ ],
78
+ );
79
+ expectToBeTrue(burnResult.revertCode.isOK());
80
+ await checkBalance(tester, token, sender, receiver, 0n, expectToBeTrue, options);
81
+
82
+ logger.info(`TokenContract test took ${timer.ms()}ms\n`);
83
+ }
84
+
85
+ export async function setUpToken(
86
+ tester: PublicTxSimulationTester,
87
+ tokenArtifact: ContractArtifact,
88
+ admin: AztecAddress,
89
+ expectToBeTrue: (x: boolean) => void,
90
+ seed = 0,
91
+ ) {
92
+ const constructorArgs = [admin, /*name=*/ 'Token', /*symbol=*/ 'TOK', /*decimals=*/ new Fr(18)];
93
+ const token = await tester.registerAndDeployContract(
94
+ constructorArgs,
95
+ /*deployer=*/ admin,
96
+ tokenArtifact,
97
+ /*skipNullifierInsertion=*/ false,
98
+ seed,
99
+ );
100
+
101
+ // EXECUTE! This means that if using AvmProvingTester subclass, it will PROVE the transaction!
102
+ const result = await tester.executeTxWithLabel(
103
+ /*txLabel=*/ 'Token/constructor',
104
+ /*sender=*/ admin,
105
+ /*setupCalls=*/ [],
106
+ /*appCalls=*/ [
107
+ {
108
+ fnName: 'constructor',
109
+ args: constructorArgs,
110
+ address: token.address,
111
+ },
112
+ ],
113
+ );
114
+ expectToBeTrue(result.revertCode.isOK());
115
+ return token;
116
+ }
117
+
118
+ async function checkBalance(
119
+ tester: PublicTxSimulationTester,
120
+ token: ContractInstanceWithAddress,
121
+ sender: AztecAddress,
122
+ account: AztecAddress,
123
+ expectedBalance: bigint,
124
+ expectToBeTrue: (x: boolean) => void,
125
+ options: TokenTestOptions = {},
126
+ ) {
127
+ // Strictly simulate this! No need to "execute" (aka prove if using AvmProvingTester subclass).
128
+ const balResult = await tester.simulateTxWithLabel(
129
+ /*txLabel=*/ 'Token/balance_of_public',
130
+ sender,
131
+ /*setupCalls=*/ [],
132
+ /*appCalls=*/ [
133
+ {
134
+ address: token.address,
135
+ fnName: 'balance_of_public',
136
+ args: [/*owner=*/ account],
137
+ isStaticCall: true,
138
+ },
139
+ ],
140
+ );
141
+ expectToBeTrue(balResult.revertCode.isOK());
142
+ if (!options.skipReturnValueAssertions) {
143
+ // should be 1 call with 1 return value that is expectedBalance
144
+ const appLogicReturnValues = balResult.getAppLogicReturnValues();
145
+ expectToBeTrue(appLogicReturnValues.length === 1);
146
+ expectToBeTrue(appLogicReturnValues[0].values?.[0]?.toBigInt() === expectedBalance);
147
+ }
148
+ }
@@ -0,0 +1,269 @@
1
+ import {
2
+ CONTRACT_CLASS_PUBLISHED_MAGIC_VALUE,
3
+ CONTRACT_CLASS_REGISTRY_CONTRACT_ADDRESS,
4
+ CONTRACT_INSTANCE_REGISTRY_CONTRACT_ADDRESS,
5
+ MAX_PROCESSABLE_DA_GAS_PER_CHECKPOINT,
6
+ MAX_PROCESSABLE_L2_GAS,
7
+ PRIVATE_LOG_SIZE_IN_FIELDS,
8
+ } from '@aztec-labs/constants';
9
+ import { padArrayEnd } from '@aztec-labs/foundation/collection';
10
+ import { Fr } from '@aztec-labs/foundation/curves/bn254';
11
+ import { CONTRACT_INSTANCE_PUBLISHED_EVENT_TAG } from '@aztec-labs/protocol-contracts';
12
+ import { bufferAsFields } from '@aztec-labs/stdlib/abi';
13
+ import { AztecAddress } from '@aztec-labs/stdlib/aztec-address';
14
+ import type { ContractClassPublic, ContractInstanceWithAddress } from '@aztec-labs/stdlib/contract';
15
+ import { Gas, GasFees, GasSettings } from '@aztec-labs/stdlib/gas';
16
+ import { siloNullifier } from '@aztec-labs/stdlib/hash';
17
+ import {
18
+ LogHash,
19
+ PartialPrivateTailPublicInputsForPublic,
20
+ PartialPrivateTailPublicInputsForRollup,
21
+ PrivateKernelTailCircuitPublicInputs,
22
+ countAccumulatedItems,
23
+ } from '@aztec-labs/stdlib/kernel';
24
+ import { ContractClassLogFields, PrivateLog } from '@aztec-labs/stdlib/logs';
25
+ import type { ScopedL2ToL1Message } from '@aztec-labs/stdlib/messaging';
26
+ import { ChonkProof } from '@aztec-labs/stdlib/proofs';
27
+ import {
28
+ BlockHeader,
29
+ GlobalVariables,
30
+ HashedValues,
31
+ PublicCallRequestWithCalldata,
32
+ Tx,
33
+ TxConstantData,
34
+ TxContext,
35
+ } from '@aztec-labs/stdlib/tx';
36
+ import { strict as assert } from 'assert';
37
+
38
+ const TEARDOWN_DA_GAS_LIMIT = 98_304;
39
+ const TEARDOWN_L2_GAS_LIMIT = 817_500;
40
+
41
+ export type TestPrivateInsertions = {
42
+ revertible?: {
43
+ nullifiers?: Fr[];
44
+ noteHashes?: Fr[];
45
+ l2ToL1Msgs?: ScopedL2ToL1Message[];
46
+ };
47
+ nonRevertible?: {
48
+ nullifiers?: Fr[];
49
+ noteHashes?: Fr[];
50
+ l2ToL1Msgs?: ScopedL2ToL1Message[];
51
+ };
52
+ };
53
+
54
+ /**
55
+ * Craft a carrier transaction for some public calls for simulation by PublicTxSimulator.
56
+ */
57
+ export async function createTxForPublicCalls(
58
+ privateInsertions: TestPrivateInsertions,
59
+ setupCallRequests: PublicCallRequestWithCalldata[],
60
+ appCallRequests: PublicCallRequestWithCalldata[],
61
+ teardownCallRequest?: PublicCallRequestWithCalldata,
62
+ feePayer = AztecAddress.zero(),
63
+ gasUsedByPrivate: Gas = Gas.empty(),
64
+ globals: GlobalVariables = GlobalVariables.empty(),
65
+ gasLimits?: Gas,
66
+ ): Promise<Tx> {
67
+ assert(
68
+ setupCallRequests.length > 0 || appCallRequests.length > 0 || teardownCallRequest !== undefined,
69
+ "Can't create public tx with no enqueued calls",
70
+ );
71
+ // use max limits
72
+ gasLimits = gasLimits ?? new Gas(MAX_PROCESSABLE_DA_GAS_PER_CHECKPOINT, MAX_PROCESSABLE_L2_GAS);
73
+
74
+ const forPublic = PartialPrivateTailPublicInputsForPublic.empty();
75
+
76
+ // Non revertible private insertions
77
+ if (!privateInsertions.nonRevertible?.nullifiers?.length) {
78
+ throw new Error('At least one non-revertible nullifier is required');
79
+ }
80
+
81
+ for (let i = 0; i < privateInsertions.nonRevertible.nullifiers.length; i++) {
82
+ assert(i < forPublic.nonRevertibleAccumulatedData.nullifiers.length, 'Nullifier index out of bounds');
83
+ forPublic.nonRevertibleAccumulatedData.nullifiers[i] = privateInsertions.nonRevertible.nullifiers[i];
84
+ }
85
+ if (privateInsertions.nonRevertible.noteHashes) {
86
+ for (let i = 0; i < privateInsertions.nonRevertible.noteHashes.length; i++) {
87
+ assert(i < forPublic.nonRevertibleAccumulatedData.noteHashes.length, 'Note hash index out of bounds');
88
+ forPublic.nonRevertibleAccumulatedData.noteHashes[i] = privateInsertions.nonRevertible.noteHashes[i];
89
+ }
90
+ }
91
+ if (privateInsertions.nonRevertible.l2ToL1Msgs) {
92
+ for (let i = 0; i < privateInsertions.nonRevertible.l2ToL1Msgs.length; i++) {
93
+ assert(i < forPublic.nonRevertibleAccumulatedData.l2ToL1Msgs.length, 'L2 to L1 message index out of bounds');
94
+ forPublic.nonRevertibleAccumulatedData.l2ToL1Msgs[i] = privateInsertions.nonRevertible.l2ToL1Msgs[i];
95
+ }
96
+ }
97
+
98
+ // Revertible private insertions
99
+ if (privateInsertions.revertible) {
100
+ if (privateInsertions.revertible.noteHashes) {
101
+ for (let i = 0; i < privateInsertions.revertible.noteHashes.length; i++) {
102
+ assert(i < forPublic.revertibleAccumulatedData.noteHashes.length, 'Note hash index out of bounds');
103
+ forPublic.revertibleAccumulatedData.noteHashes[i] = privateInsertions.revertible.noteHashes[i];
104
+ }
105
+ }
106
+ if (privateInsertions.revertible.nullifiers) {
107
+ for (let i = 0; i < privateInsertions.revertible.nullifiers.length; i++) {
108
+ assert(i < forPublic.revertibleAccumulatedData.nullifiers.length, 'Nullifier index out of bounds');
109
+ forPublic.revertibleAccumulatedData.nullifiers[i] = privateInsertions.revertible.nullifiers[i];
110
+ }
111
+ }
112
+ if (privateInsertions.revertible.l2ToL1Msgs) {
113
+ for (let i = 0; i < privateInsertions.revertible.l2ToL1Msgs.length; i++) {
114
+ assert(i < forPublic.revertibleAccumulatedData.l2ToL1Msgs.length, 'L2 to L1 message index out of bounds');
115
+ forPublic.revertibleAccumulatedData.l2ToL1Msgs[i] = privateInsertions.revertible.l2ToL1Msgs[i];
116
+ }
117
+ }
118
+ }
119
+
120
+ for (let i = 0; i < setupCallRequests.length; i++) {
121
+ forPublic.nonRevertibleAccumulatedData.publicCallRequests[i] = setupCallRequests[i].request;
122
+ }
123
+ for (let i = 0; i < appCallRequests.length; i++) {
124
+ forPublic.revertibleAccumulatedData.publicCallRequests[i] = appCallRequests[i].request;
125
+ }
126
+ if (teardownCallRequest) {
127
+ forPublic.publicTeardownCallRequest = teardownCallRequest.request;
128
+ }
129
+
130
+ const maxFeesPerGas = feePayer.isZero() ? GasFees.empty() : new GasFees(10, 10);
131
+ const teardownGasLimits = teardownCallRequest ? new Gas(TEARDOWN_DA_GAS_LIMIT, TEARDOWN_L2_GAS_LIMIT) : Gas.empty();
132
+ const gasSettings = new GasSettings(gasLimits, teardownGasLimits, maxFeesPerGas, GasFees.empty());
133
+ const txContext = new TxContext(Fr.zero(), Fr.zero(), gasSettings);
134
+ const header = BlockHeader.empty({ globalVariables: globals });
135
+ const constantData = new TxConstantData(header, txContext, Fr.zero(), Fr.zero());
136
+ const expirationTimestamp = 0n; // Not used in the simulator.
137
+
138
+ const txData = new PrivateKernelTailCircuitPublicInputs(
139
+ constantData,
140
+ /*gasUsed=*/ gasUsedByPrivate,
141
+ feePayer,
142
+ expirationTimestamp,
143
+ forPublic,
144
+ );
145
+
146
+ const calldata = [
147
+ ...setupCallRequests,
148
+ ...appCallRequests,
149
+ ...(teardownCallRequest ? [teardownCallRequest] : []),
150
+ ].map(r => new HashedValues(r.calldata, r.request.calldataHash));
151
+
152
+ return await Tx.create({
153
+ data: txData,
154
+ chonkProof: ChonkProof.random(),
155
+ contractClassLogFields: [],
156
+ publicFunctionCalldata: calldata,
157
+ });
158
+ }
159
+
160
+ export async function createTxForPrivateOnly(
161
+ feePayer = AztecAddress.zero(),
162
+ gasUsedByPrivate: Gas = new Gas(10, 10),
163
+ ): Promise<Tx> {
164
+ // use max limits
165
+ const gasLimits = new Gas(MAX_PROCESSABLE_DA_GAS_PER_CHECKPOINT, MAX_PROCESSABLE_L2_GAS);
166
+
167
+ const forRollup = PartialPrivateTailPublicInputsForRollup.empty();
168
+
169
+ const maxFeesPerGas = feePayer.isZero() ? GasFees.empty() : new GasFees(10, 10);
170
+ const gasSettings = new GasSettings(gasLimits, Gas.empty(), maxFeesPerGas, GasFees.empty());
171
+ const txContext = new TxContext(Fr.zero(), Fr.zero(), gasSettings);
172
+ const constantData = new TxConstantData(BlockHeader.empty(), txContext, Fr.zero(), Fr.zero());
173
+ const expirationTimestamp = 0n; // Not used in the simulator.
174
+
175
+ const txData = new PrivateKernelTailCircuitPublicInputs(
176
+ constantData,
177
+ /*gasUsed=*/ gasUsedByPrivate,
178
+ feePayer,
179
+ expirationTimestamp,
180
+ /*forPublic=*/ undefined,
181
+ forRollup,
182
+ );
183
+ return await Tx.create({
184
+ data: txData,
185
+ chonkProof: ChonkProof.empty(),
186
+ contractClassLogFields: [],
187
+ publicFunctionCalldata: [],
188
+ });
189
+ }
190
+
191
+ export async function addNewContractClassToTx(
192
+ tx: Tx,
193
+ contractClass: ContractClassPublic,
194
+ skipNullifierInsertion = false,
195
+ ) {
196
+ const contractClassLogFields = [
197
+ new Fr(CONTRACT_CLASS_PUBLISHED_MAGIC_VALUE),
198
+ contractClass.id,
199
+ new Fr(contractClass.version),
200
+ new Fr(contractClass.artifactHash),
201
+ new Fr(contractClass.privateFunctionsRoot),
202
+ ...bufferAsFields(contractClass.packedBytecode, Math.ceil(contractClass.packedBytecode.length / 31) + 1),
203
+ ];
204
+ const contractAddress = new AztecAddress(new Fr(CONTRACT_CLASS_REGISTRY_CONTRACT_ADDRESS));
205
+ const emittedLength = contractClassLogFields.length;
206
+ const logFields = ContractClassLogFields.fromEmittedFields(contractClassLogFields);
207
+
208
+ const contractClassLogHash = LogHash.from({
209
+ value: await logFields.hash(),
210
+ length: emittedLength,
211
+ }).scope(contractAddress);
212
+
213
+ const accumulatedData = tx.data.forPublic ? tx.data.forPublic!.revertibleAccumulatedData : tx.data.forRollup!.end;
214
+ if (!skipNullifierInsertion) {
215
+ const nextNullifierIndex = countAccumulatedItems(accumulatedData.nullifiers);
216
+ accumulatedData.nullifiers[nextNullifierIndex] = contractClass.id;
217
+ }
218
+
219
+ const nextLogIndex = countAccumulatedItems(accumulatedData.contractClassLogsHashes);
220
+ accumulatedData.contractClassLogsHashes[nextLogIndex] = contractClassLogHash;
221
+
222
+ tx.contractClassLogFields.push(logFields);
223
+ }
224
+
225
+ export async function addNewContractInstanceToTx(
226
+ tx: Tx,
227
+ contractInstance: ContractInstanceWithAddress,
228
+ skipNullifierInsertion = false,
229
+ ) {
230
+ // Only ivpk_m is broadcast as a point (x, y); the other five keys are hashes.
231
+ const publicKeysAsFields = [
232
+ contractInstance.publicKeys.npkMHash,
233
+ contractInstance.publicKeys.ivpkM.x,
234
+ contractInstance.publicKeys.ivpkM.y,
235
+ contractInstance.publicKeys.ovpkMHash,
236
+ contractInstance.publicKeys.tpkMHash,
237
+ contractInstance.publicKeys.mspkMHash,
238
+ contractInstance.publicKeys.fbpkMHash,
239
+ ];
240
+ const logFields = [
241
+ CONTRACT_INSTANCE_PUBLISHED_EVENT_TAG,
242
+ contractInstance.address.toField(),
243
+ new Fr(contractInstance.version),
244
+ new Fr(contractInstance.salt),
245
+ contractInstance.currentContractClassId,
246
+ contractInstance.initializationHash,
247
+ contractInstance.immutablesHash,
248
+ ...publicKeysAsFields,
249
+ contractInstance.deployer.toField(),
250
+ ];
251
+ const contractInstanceLog = new PrivateLog(
252
+ padArrayEnd(logFields, Fr.ZERO, PRIVATE_LOG_SIZE_IN_FIELDS),
253
+ logFields.length,
254
+ );
255
+
256
+ const contractAddressNullifier = await siloNullifier(
257
+ AztecAddress.fromNumberUnsafe(CONTRACT_INSTANCE_REGISTRY_CONTRACT_ADDRESS),
258
+ contractInstance.address.toField(),
259
+ );
260
+
261
+ const accumulatedData = tx.data.forPublic ? tx.data.forPublic!.revertibleAccumulatedData : tx.data.forRollup!.end;
262
+ if (!skipNullifierInsertion) {
263
+ const nextNullifierIndex = countAccumulatedItems(accumulatedData.nullifiers);
264
+ accumulatedData.nullifiers[nextNullifierIndex] = contractAddressNullifier;
265
+ }
266
+
267
+ const nextLogIndex = countAccumulatedItems(accumulatedData.privateLogs);
268
+ accumulatedData.privateLogs[nextLogIndex] = contractInstanceLog;
269
+ }