@aztec/simulator 0.85.0 → 0.86.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 (133) hide show
  1. package/dest/private/providers/acvm_native.d.ts +1 -1
  2. package/dest/private/providers/acvm_native.d.ts.map +1 -1
  3. package/dest/private/providers/acvm_native.js +4 -3
  4. package/dest/private/providers/acvm_wasm.d.ts +2 -1
  5. package/dest/private/providers/acvm_wasm.d.ts.map +1 -1
  6. package/dest/private/providers/acvm_wasm.js +6 -1
  7. package/dest/private/providers/acvm_wasm_with_blobs.d.ts +2 -1
  8. package/dest/private/providers/acvm_wasm_with_blobs.d.ts.map +1 -1
  9. package/dest/private/providers/acvm_wasm_with_blobs.js +6 -1
  10. package/dest/private/providers/circuit_recording/simulation_provider_recorder_wrapper.d.ts +2 -1
  11. package/dest/private/providers/circuit_recording/simulation_provider_recorder_wrapper.d.ts.map +1 -1
  12. package/dest/private/providers/simulation_provider.d.ts +2 -1
  13. package/dest/private/providers/simulation_provider.d.ts.map +1 -1
  14. package/dest/public/avm/avm_context.d.ts +2 -2
  15. package/dest/public/avm/avm_context.d.ts.map +1 -1
  16. package/dest/public/avm/avm_simulator.d.ts +2 -1
  17. package/dest/public/avm/avm_simulator.d.ts.map +1 -1
  18. package/dest/public/avm/avm_simulator.js +2 -1
  19. package/dest/public/avm/avm_simulator_interface.d.ts +11 -0
  20. package/dest/public/avm/avm_simulator_interface.d.ts.map +1 -0
  21. package/dest/public/avm/avm_simulator_interface.js +3 -0
  22. package/dest/public/avm/errors.d.ts +1 -16
  23. package/dest/public/avm/errors.d.ts.map +1 -1
  24. package/dest/public/avm/errors.js +0 -37
  25. package/dest/public/avm/fixtures/avm_simulation_tester.d.ts +1 -1
  26. package/dest/public/avm/fixtures/avm_simulation_tester.d.ts.map +1 -1
  27. package/dest/public/avm/fixtures/avm_simulation_tester.js +1 -1
  28. package/dest/public/avm/fixtures/base_avm_simulation_tester.d.ts +1 -1
  29. package/dest/public/avm/fixtures/base_avm_simulation_tester.d.ts.map +1 -1
  30. package/dest/public/avm/fixtures/index.d.ts +2 -85
  31. package/dest/public/avm/fixtures/index.d.ts.map +1 -1
  32. package/dest/public/avm/fixtures/index.js +2 -174
  33. package/dest/public/avm/fixtures/initializers.d.ts +42 -0
  34. package/dest/public/avm/fixtures/initializers.d.ts.map +1 -0
  35. package/dest/public/avm/fixtures/initializers.js +42 -0
  36. package/dest/public/avm/fixtures/utils.d.ts +46 -0
  37. package/dest/public/avm/fixtures/utils.d.ts.map +1 -0
  38. package/dest/public/avm/fixtures/utils.js +136 -0
  39. package/dest/public/avm/index.d.ts +0 -1
  40. package/dest/public/avm/index.d.ts.map +1 -1
  41. package/dest/public/avm/index.js +0 -1
  42. package/dest/public/avm/opcodes/accrued_substate.d.ts.map +1 -1
  43. package/dest/public/avm/opcodes/accrued_substate.js +1 -1
  44. package/dest/public/avm/opcodes/external_calls.d.ts +3 -2
  45. package/dest/public/avm/opcodes/external_calls.d.ts.map +1 -1
  46. package/dest/public/avm/opcodes/external_calls.js +14 -9
  47. package/dest/public/avm/opcodes/instruction.d.ts +5 -5
  48. package/dest/public/avm/opcodes/instruction.d.ts.map +1 -1
  49. package/dest/public/avm/opcodes/instruction.js +6 -6
  50. package/dest/public/avm/revert_reason.d.ts +18 -0
  51. package/dest/public/avm/revert_reason.d.ts.map +1 -0
  52. package/dest/public/avm/revert_reason.js +38 -0
  53. package/dest/public/avm/serialization/bytecode_serialization.d.ts +2 -4
  54. package/dest/public/avm/serialization/bytecode_serialization.d.ts.map +1 -1
  55. package/dest/public/avm/serialization/bytecode_serialization.js +70 -69
  56. package/dest/{common → public}/debug_fn_name.d.ts +1 -1
  57. package/dest/{common → public}/debug_fn_name.d.ts.map +1 -1
  58. package/dest/public/fixtures/index.d.ts +1 -0
  59. package/dest/public/fixtures/index.d.ts.map +1 -1
  60. package/dest/public/fixtures/index.js +1 -0
  61. package/dest/public/fixtures/public_tx_simulation_tester.d.ts +1 -1
  62. package/dest/public/fixtures/public_tx_simulation_tester.d.ts.map +1 -1
  63. package/dest/public/fixtures/public_tx_simulation_tester.js +3 -4
  64. package/dest/public/fixtures/simple_contract_data_source.d.ts.map +1 -0
  65. package/dest/public/{avm/fixtures → fixtures}/simple_contract_data_source.js +1 -1
  66. package/dest/public/hinting_db_sources.d.ts +15 -5
  67. package/dest/public/hinting_db_sources.d.ts.map +1 -1
  68. package/dest/public/hinting_db_sources.js +65 -27
  69. package/dest/public/index.d.ts +2 -6
  70. package/dest/public/index.d.ts.map +1 -1
  71. package/dest/public/index.js +2 -6
  72. package/dest/public/public_db_sources.d.ts +19 -52
  73. package/dest/public/public_db_sources.d.ts.map +1 -1
  74. package/dest/public/public_db_sources.js +96 -107
  75. package/dest/public/public_processor/public_processor.d.ts +6 -6
  76. package/dest/public/public_processor/public_processor.d.ts.map +1 -1
  77. package/dest/public/public_processor/public_processor.js +24 -26
  78. package/dest/public/public_tx_simulator/measured_public_tx_simulator.d.ts +3 -2
  79. package/dest/public/public_tx_simulator/measured_public_tx_simulator.d.ts.map +1 -1
  80. package/dest/public/public_tx_simulator/measured_public_tx_simulator.js +2 -2
  81. package/dest/public/public_tx_simulator/public_tx_context.d.ts +3 -4
  82. package/dest/public/public_tx_simulator/public_tx_context.d.ts.map +1 -1
  83. package/dest/public/public_tx_simulator/public_tx_context.js +11 -21
  84. package/dest/public/public_tx_simulator/public_tx_simulator.d.ts +5 -4
  85. package/dest/public/public_tx_simulator/public_tx_simulator.d.ts.map +1 -1
  86. package/dest/public/public_tx_simulator/public_tx_simulator.js +21 -10
  87. package/dest/public/public_tx_simulator/telemetry_public_tx_simulator.d.ts +3 -2
  88. package/dest/public/public_tx_simulator/telemetry_public_tx_simulator.d.ts.map +1 -1
  89. package/dest/public/public_tx_simulator/telemetry_public_tx_simulator.js +2 -2
  90. package/dest/public/side_effect_trace.d.ts +1 -3
  91. package/dest/public/side_effect_trace.d.ts.map +1 -1
  92. package/dest/public/side_effect_trace.js +3 -2
  93. package/dest/public/state_manager/state_manager.d.ts +6 -4
  94. package/dest/public/state_manager/state_manager.d.ts.map +1 -1
  95. package/dest/public/state_manager/state_manager.js +20 -41
  96. package/package.json +14 -16
  97. package/src/private/providers/acvm_native.ts +5 -4
  98. package/src/private/providers/acvm_wasm.ts +5 -2
  99. package/src/private/providers/acvm_wasm_with_blobs.ts +5 -3
  100. package/src/private/providers/circuit_recording/simulation_provider_recorder_wrapper.ts +3 -2
  101. package/src/private/providers/simulation_provider.ts +2 -1
  102. package/src/public/avm/avm_context.ts +2 -2
  103. package/src/public/avm/avm_simulator.ts +4 -8
  104. package/src/public/avm/avm_simulator_interface.ts +8 -0
  105. package/src/public/avm/errors.ts +1 -53
  106. package/src/public/avm/fixtures/avm_simulation_tester.ts +1 -1
  107. package/src/public/avm/fixtures/base_avm_simulation_tester.ts +1 -1
  108. package/src/public/avm/fixtures/index.ts +2 -308
  109. package/src/public/avm/fixtures/initializers.ts +101 -0
  110. package/src/public/avm/fixtures/utils.ts +213 -0
  111. package/src/public/avm/index.ts +0 -1
  112. package/src/public/avm/opcodes/accrued_substate.ts +1 -5
  113. package/src/public/avm/opcodes/external_calls.ts +17 -11
  114. package/src/public/avm/opcodes/instruction.ts +9 -8
  115. package/src/public/avm/revert_reason.ts +55 -0
  116. package/src/public/avm/serialization/bytecode_serialization.ts +72 -74
  117. package/src/{common → public}/debug_fn_name.ts +1 -1
  118. package/src/public/fixtures/index.ts +1 -0
  119. package/src/public/fixtures/public_tx_simulation_tester.ts +3 -5
  120. package/src/public/{avm/fixtures → fixtures}/simple_contract_data_source.ts +1 -1
  121. package/src/public/hinting_db_sources.ts +104 -39
  122. package/src/public/index.ts +2 -6
  123. package/src/public/public_db_sources.ts +111 -164
  124. package/src/public/public_processor/public_processor.ts +27 -29
  125. package/src/public/public_tx_simulator/measured_public_tx_simulator.ts +4 -3
  126. package/src/public/public_tx_simulator/public_tx_context.ts +10 -47
  127. package/src/public/public_tx_simulator/public_tx_simulator.ts +25 -10
  128. package/src/public/public_tx_simulator/telemetry_public_tx_simulator.ts +4 -3
  129. package/src/public/side_effect_trace.ts +2 -4
  130. package/src/public/state_manager/state_manager.ts +24 -50
  131. package/dest/public/avm/fixtures/simple_contract_data_source.d.ts.map +0 -1
  132. /package/dest/{common → public}/debug_fn_name.js +0 -0
  133. /package/dest/public/{avm/fixtures → fixtures}/simple_contract_data_source.d.ts +0 -0
@@ -0,0 +1,101 @@
1
+ import { MAX_L2_GAS_PER_TX_PUBLIC_PORTION } from '@aztec/constants';
2
+ import { EthAddress } from '@aztec/foundation/eth-address';
3
+ import { Fr } from '@aztec/foundation/fields';
4
+ import { AztecAddress } from '@aztec/stdlib/aztec-address';
5
+ import { GasFees } from '@aztec/stdlib/gas';
6
+ import { GlobalVariables } from '@aztec/stdlib/tx';
7
+
8
+ import { mock } from 'jest-mock-extended';
9
+
10
+ import type { PublicContractsDB, PublicTreesDB } from '../../public_db_sources.js';
11
+ import type { PublicSideEffectTraceInterface } from '../../side_effect_trace_interface.js';
12
+ import { NullifierManager } from '../../state_manager/nullifiers.js';
13
+ import { PublicStorage } from '../../state_manager/public_storage.js';
14
+ import { PublicPersistableStateManager } from '../../state_manager/state_manager.js';
15
+ import { AvmContext } from '../avm_context.js';
16
+ import { AvmExecutionEnvironment } from '../avm_execution_environment.js';
17
+ import { AvmMachineState } from '../avm_machine_state.js';
18
+ import { AvmSimulator } from '../avm_simulator.js';
19
+ import { DEFAULT_BLOCK_NUMBER } from './utils.js';
20
+
21
+ /**
22
+ * Create a new AVM context with default values.
23
+ */
24
+ export function initContext(overrides?: {
25
+ persistableState?: PublicPersistableStateManager;
26
+ env?: AvmExecutionEnvironment;
27
+ machineState?: AvmMachineState;
28
+ }): AvmContext {
29
+ const ctx = new AvmContext(
30
+ overrides?.persistableState || initPersistableStateManager(),
31
+ overrides?.env || initExecutionEnvironment(),
32
+ overrides?.machineState || initMachineState(),
33
+ );
34
+ ctx.provideSimulator = AvmSimulator.build;
35
+ return ctx;
36
+ }
37
+
38
+ /** Creates an empty state manager with mocked host storage. */
39
+ export function initPersistableStateManager(overrides?: {
40
+ treesDB?: PublicTreesDB;
41
+ contractsDB?: PublicContractsDB;
42
+ trace?: PublicSideEffectTraceInterface;
43
+ publicStorage?: PublicStorage;
44
+ nullifiers?: NullifierManager;
45
+ doMerkleOperations?: boolean;
46
+ firstNullifier?: Fr;
47
+ blockNumber?: number;
48
+ }): PublicPersistableStateManager {
49
+ const treesDB = overrides?.treesDB || mock<PublicTreesDB>();
50
+ return new PublicPersistableStateManager(
51
+ treesDB,
52
+ overrides?.contractsDB || mock<PublicContractsDB>(),
53
+ overrides?.trace || mock<PublicSideEffectTraceInterface>(),
54
+ overrides?.firstNullifier || new Fr(27),
55
+ overrides?.blockNumber || DEFAULT_BLOCK_NUMBER,
56
+ overrides?.doMerkleOperations || false,
57
+ overrides?.publicStorage,
58
+ overrides?.nullifiers,
59
+ );
60
+ }
61
+
62
+ /**
63
+ * Create an empty instance of the Execution Environment where all values are zero, unless overridden in the overrides object
64
+ */
65
+ export function initExecutionEnvironment(overrides?: Partial<AvmExecutionEnvironment>): AvmExecutionEnvironment {
66
+ return new AvmExecutionEnvironment(
67
+ overrides?.address ?? AztecAddress.zero(),
68
+ overrides?.sender ?? AztecAddress.zero(),
69
+ overrides?.contractCallDepth ?? Fr.zero(),
70
+ overrides?.transactionFee ?? Fr.zero(),
71
+ overrides?.globals ?? GlobalVariables.empty(),
72
+ overrides?.isStaticCall ?? false,
73
+ overrides?.calldata ?? [],
74
+ );
75
+ }
76
+
77
+ /**
78
+ * Create an empty instance of the Execution Environment where all values are zero, unless overridden in the overrides object
79
+ */
80
+ export function initGlobalVariables(overrides?: Partial<GlobalVariables>): GlobalVariables {
81
+ return new GlobalVariables(
82
+ overrides?.chainId ?? Fr.zero(),
83
+ overrides?.version ?? Fr.zero(),
84
+ overrides?.blockNumber ?? Fr.zero(),
85
+ overrides?.slotNumber ?? Fr.zero(),
86
+ overrides?.timestamp ?? Fr.zero(),
87
+ overrides?.coinbase ?? EthAddress.ZERO,
88
+ overrides?.feeRecipient ?? AztecAddress.zero(),
89
+ overrides?.gasFees ?? GasFees.empty(),
90
+ );
91
+ }
92
+
93
+ /**
94
+ * Create an empty instance of the Machine State where all values are set to a large enough amount, unless overridden in the overrides object
95
+ */
96
+ export function initMachineState(overrides?: Partial<AvmMachineState>): AvmMachineState {
97
+ return AvmMachineState.fromState({
98
+ l2GasLeft: overrides?.l2GasLeft ?? MAX_L2_GAS_PER_TX_PUBLIC_PORTION,
99
+ daGasLeft: overrides?.daGasLeft ?? 1e8,
100
+ });
101
+ }
@@ -0,0 +1,213 @@
1
+ import { DEPLOYER_CONTRACT_ADDRESS } from '@aztec/constants';
2
+ import { Fr } from '@aztec/foundation/fields';
3
+ import { AvmGadgetsTestContract } from '@aztec/noir-contracts.js/AvmGadgetsTest';
4
+ import { AvmTestContract } from '@aztec/noir-contracts.js/AvmTest';
5
+ import {
6
+ type ContractArtifact,
7
+ type FunctionAbi,
8
+ type FunctionArtifact,
9
+ FunctionSelector,
10
+ getAllFunctionAbis,
11
+ } from '@aztec/stdlib/abi';
12
+ import { AztecAddress } from '@aztec/stdlib/aztec-address';
13
+ import {
14
+ type ContractClassPublic,
15
+ type ContractInstanceWithAddress,
16
+ computeInitializationHash,
17
+ } from '@aztec/stdlib/contract';
18
+ import { isNoirCallStackUnresolved } from '@aztec/stdlib/errors';
19
+ import { siloNullifier } from '@aztec/stdlib/hash';
20
+ import { deriveKeys } from '@aztec/stdlib/keys';
21
+ import { makeContractClassPublic, makeContractInstanceFromClassId } from '@aztec/stdlib/testing';
22
+
23
+ import { strict as assert } from 'assert';
24
+ import merge from 'lodash.merge';
25
+
26
+ import { resolveAssertionMessageFromRevertData, traverseCauseChain } from '../../../common/index.js';
27
+ import { Field, Uint8, Uint32, Uint64 } from '../avm_memory_types.js';
28
+ import type { AvmRevertReason } from '../errors.js';
29
+
30
+ export const PUBLIC_DISPATCH_FN_NAME = 'public_dispatch';
31
+ export const DEFAULT_BLOCK_NUMBER = 42;
32
+
33
+ /**
34
+ * Create a new object with all the same properties as the original, except for the ones in the overrides object.
35
+ */
36
+ export function allSameExcept(original: any, overrides: any): any {
37
+ return merge({}, original, overrides);
38
+ }
39
+
40
+ export function randomMemoryBytes(length: number): Uint8[] {
41
+ return [...Array(length)].map(_ => new Uint8(Math.floor(Math.random() * 255)));
42
+ }
43
+
44
+ export function randomMemoryUint32s(length: number): Uint32[] {
45
+ return [...Array(length)].map(_ => new Uint32(Math.floor(Math.random() * 255)));
46
+ }
47
+
48
+ export function randomMemoryUint64s(length: number): Uint64[] {
49
+ return [...Array(length)].map(_ => new Uint64(Math.floor(Math.random() * 255)));
50
+ }
51
+
52
+ export function randomMemoryFields(length: number): Field[] {
53
+ return [...Array(length)].map(_ => new Field(Fr.random()));
54
+ }
55
+
56
+ export function getFunctionSelector(
57
+ functionName: string,
58
+ contractArtifact: ContractArtifact,
59
+ ): Promise<FunctionSelector> {
60
+ const fnArtifact = getAllFunctionAbis(contractArtifact).find(f => f.name === functionName)!;
61
+ assert(!!fnArtifact, `Function ${functionName} not found in ${contractArtifact.name}`);
62
+ const params = fnArtifact.parameters;
63
+ return FunctionSelector.fromNameAndParameters(fnArtifact.name, params);
64
+ }
65
+
66
+ export function getContractFunctionArtifact(
67
+ functionName: string,
68
+ contractArtifact: ContractArtifact,
69
+ ): FunctionArtifact | undefined {
70
+ return contractArtifact.functions.find(f => f.name === functionName);
71
+ }
72
+
73
+ export function getContractFunctionAbi(
74
+ functionName: string,
75
+ contractArtifact: ContractArtifact,
76
+ ): FunctionAbi | undefined {
77
+ return (
78
+ contractArtifact.functions.find(f => f.name === functionName) ??
79
+ contractArtifact.nonDispatchPublicFunctions.find(f => f.name === functionName)
80
+ );
81
+ }
82
+
83
+ export function resolveContractAssertionMessage(
84
+ functionName: string,
85
+ revertReason: AvmRevertReason,
86
+ output: Fr[],
87
+ contractArtifact: ContractArtifact,
88
+ ): string | undefined {
89
+ traverseCauseChain(revertReason, cause => {
90
+ revertReason = cause as AvmRevertReason;
91
+ });
92
+
93
+ const functionArtifact = getAllFunctionAbis(contractArtifact).find(f => f.name === functionName);
94
+ if (!functionArtifact || !revertReason.noirCallStack || !isNoirCallStackUnresolved(revertReason.noirCallStack)) {
95
+ return undefined;
96
+ }
97
+
98
+ return resolveAssertionMessageFromRevertData(output, functionArtifact);
99
+ }
100
+
101
+ export function getAvmTestContractFunctionSelector(functionName: string): Promise<FunctionSelector> {
102
+ return getFunctionSelector(functionName, AvmTestContract.artifactForPublic);
103
+ }
104
+
105
+ export function getAvmGadgetsTestContractFunctionSelector(functionName: string): Promise<FunctionSelector> {
106
+ const artifact = getAllFunctionAbis(AvmGadgetsTestContract.artifactForPublic).find(f => f.name === functionName)!;
107
+ assert(!!artifact, `Function ${functionName} not found in AvmGadgetsTestContractArtifact`);
108
+ const params = artifact.parameters;
109
+ return FunctionSelector.fromNameAndParameters(artifact.name, params);
110
+ }
111
+
112
+ export function getAvmTestContractArtifact(functionName: string): FunctionArtifact {
113
+ const artifact = getContractFunctionArtifact(functionName, AvmTestContract.artifactForPublic) as FunctionArtifact;
114
+ assert(
115
+ !!artifact?.bytecode,
116
+ `No bytecode found for function ${functionName}. Try re-running bootstrap.sh on the repository root.`,
117
+ );
118
+ return artifact;
119
+ }
120
+
121
+ export function getAvmGadgetsTestContractArtifact(functionName: string): FunctionArtifact {
122
+ const artifact = AvmGadgetsTestContract.artifactForPublic.functions.find(f => f.name === functionName)!;
123
+ assert(
124
+ !!artifact?.bytecode,
125
+ `No bytecode found for function ${functionName}. Try re-running bootstrap.sh on the repository root.`,
126
+ );
127
+ return artifact;
128
+ }
129
+
130
+ export function getAvmTestContractBytecode(functionName: string): Buffer {
131
+ const artifact = getAvmTestContractArtifact(functionName);
132
+ return artifact.bytecode;
133
+ }
134
+
135
+ export function getAvmGadgetsTestContractBytecode(functionName: string): Buffer {
136
+ const artifact = getAvmGadgetsTestContractArtifact(functionName);
137
+ return artifact.bytecode;
138
+ }
139
+
140
+ export function resolveAvmTestContractAssertionMessage(
141
+ functionName: string,
142
+ revertReason: AvmRevertReason,
143
+ output: Fr[],
144
+ ): string | undefined {
145
+ return resolveContractAssertionMessage(functionName, revertReason, output, AvmTestContract.artifactForPublic);
146
+ }
147
+
148
+ export function resolveAvmGadgetsTestContractAssertionMessage(
149
+ functionName: string,
150
+ revertReason: AvmRevertReason,
151
+ output: Fr[],
152
+ ): string | undefined {
153
+ traverseCauseChain(revertReason, cause => {
154
+ revertReason = cause as AvmRevertReason;
155
+ });
156
+
157
+ const functionArtifact = AvmGadgetsTestContract.artifactForPublic.functions.find(f => f.name === functionName);
158
+ if (!functionArtifact || !revertReason.noirCallStack || !isNoirCallStackUnresolved(revertReason.noirCallStack)) {
159
+ return undefined;
160
+ }
161
+
162
+ return resolveAssertionMessageFromRevertData(output, functionArtifact);
163
+ }
164
+
165
+ /**
166
+ * Create a contract class and instance given constructor args, artifact, etc.
167
+ * NOTE: This is useful for testing real-ish contract class registration and instance deployment TXs (via logs)
168
+ * @param constructorArgs - The constructor arguments for the contract.
169
+ * @param deployer - The deployer of the contract.
170
+ * @param contractArtifact - The contract artifact for the contract.
171
+ * @param seed - The seed for the contract.
172
+ * @param originalContractClassId - The original contract class ID (if upgraded)
173
+ * @returns The contract class, instance, and contract address nullifier.
174
+ */
175
+ export async function createContractClassAndInstance(
176
+ constructorArgs: any[],
177
+ deployer: AztecAddress,
178
+ contractArtifact: ContractArtifact,
179
+ seed = 0,
180
+ originalContractClassId?: Fr, // if previously upgraded
181
+ ): Promise<{
182
+ contractClass: ContractClassPublic;
183
+ contractInstance: ContractInstanceWithAddress;
184
+ contractAddressNullifier: Fr;
185
+ }> {
186
+ const bytecode = (getContractFunctionArtifact(PUBLIC_DISPATCH_FN_NAME, contractArtifact) as FunctionArtifact)!
187
+ .bytecode;
188
+ const contractClass = await makeContractClassPublic(seed, bytecode);
189
+
190
+ const constructorAbi = getContractFunctionAbi('constructor', contractArtifact);
191
+ const { publicKeys } = await deriveKeys(Fr.random());
192
+ const initializationHash = await computeInitializationHash(constructorAbi, constructorArgs);
193
+ const contractInstance =
194
+ originalContractClassId === undefined
195
+ ? await makeContractInstanceFromClassId(contractClass.id, seed, {
196
+ deployer,
197
+ initializationHash,
198
+ publicKeys,
199
+ })
200
+ : await makeContractInstanceFromClassId(originalContractClassId, seed, {
201
+ deployer,
202
+ initializationHash,
203
+ currentClassId: contractClass.id,
204
+ publicKeys,
205
+ });
206
+
207
+ const contractAddressNullifier = await siloNullifier(
208
+ AztecAddress.fromNumber(DEPLOYER_CONTRACT_ADDRESS),
209
+ contractInstance.address.toField(),
210
+ );
211
+
212
+ return { contractClass, contractInstance, contractAddressNullifier };
213
+ }
@@ -1,2 +1 @@
1
1
  export * from './avm_simulator.js';
2
- export * from './fixtures/simple_contract_data_source.js';
@@ -181,11 +181,7 @@ export class L1ToL2MessageExists extends Instruction {
181
181
 
182
182
  const msgHash = memory.get(msgHashOffset).toFr();
183
183
  const msgLeafIndex = memory.get(msgLeafIndexOffset).toFr();
184
- const exists = await context.persistableState.checkL1ToL2MessageExists(
185
- context.environment.address,
186
- msgHash,
187
- msgLeafIndex,
188
- );
184
+ const exists = await context.persistableState.checkL1ToL2MessageExists(msgHash, msgLeafIndex);
189
185
  memory.set(existsOffset, exists ? new Uint1(1) : new Uint1(0));
190
186
  }
191
187
  }
@@ -9,16 +9,18 @@ abstract class ExternalCall extends Instruction {
9
9
  // Informs (de)serialization. See Instruction.deserialize.
10
10
  static readonly wireFormat: OperandType[] = [
11
11
  OperandType.UINT8,
12
- OperandType.UINT8, // Indirect
13
- OperandType.UINT16,
14
- OperandType.UINT16,
15
- OperandType.UINT16,
16
- OperandType.UINT16,
12
+ OperandType.UINT16, // Indirect
13
+ OperandType.UINT16, // L2 gas offset
14
+ OperandType.UINT16, // DA gas offset
15
+ OperandType.UINT16, // Address offset
16
+ OperandType.UINT16, // Args offset
17
+ OperandType.UINT16, // Args size offset
17
18
  ];
18
19
 
19
20
  constructor(
20
21
  private indirect: number,
21
- private gasOffset: number,
22
+ private l2GasOffset: number,
23
+ private daGasOffset: number,
22
24
  private addrOffset: number,
23
25
  private argsOffset: number,
24
26
  private argsSizeOffset: number,
@@ -28,10 +30,12 @@ abstract class ExternalCall extends Instruction {
28
30
 
29
31
  public async execute(context: AvmContext) {
30
32
  const memory = context.machineState.memory;
31
- const operands = [this.gasOffset, this.addrOffset, this.argsOffset, this.argsSizeOffset];
33
+ const operands = [this.l2GasOffset, this.daGasOffset, this.addrOffset, this.argsOffset, this.argsSizeOffset];
32
34
  const addressing = Addressing.fromWire(this.indirect, operands.length);
33
- const [gasOffset, addrOffset, argsOffset, argsSizeOffset] = addressing.resolve(operands, memory);
34
- memory.checkTags(TypeTag.FIELD, gasOffset, gasOffset + 1);
35
+ const [l2GasOffset, daGasOffset, addrOffset, argsOffset, argsSizeOffset] = addressing.resolve(operands, memory);
36
+ // TODO: Should be U32
37
+ memory.checkTags(TypeTag.FIELD, l2GasOffset);
38
+ memory.checkTags(TypeTag.FIELD, daGasOffset);
35
39
  memory.checkTag(TypeTag.FIELD, addrOffset);
36
40
  memory.checkTag(TypeTag.UINT32, argsSizeOffset);
37
41
 
@@ -49,9 +53,11 @@ abstract class ExternalCall extends Instruction {
49
53
  // Gas allocation is capped by the amount of gas left in the current context.
50
54
  // We have to do some dancing here because the gas allocation is a field,
51
55
  // but in the machine state we track gas as a number.
52
- const allocatedL2Gas = Number(BigIntMin(memory.get(gasOffset).toBigInt(), BigInt(context.machineState.l2GasLeft)));
56
+ const allocatedL2Gas = Number(
57
+ BigIntMin(memory.get(l2GasOffset).toBigInt(), BigInt(context.machineState.l2GasLeft)),
58
+ );
53
59
  const allocatedDaGas = Number(
54
- BigIntMin(memory.get(gasOffset + 1).toBigInt(), BigInt(context.machineState.daGasLeft)),
60
+ BigIntMin(memory.get(daGasOffset).toBigInt(), BigInt(context.machineState.daGasLeft)),
55
61
  );
56
62
  const allocatedGas = { l2Gas: allocatedL2Gas, daGas: allocatedDaGas };
57
63
  context.machineState.consumeGas(allocatedGas);
@@ -1,9 +1,10 @@
1
+ import type { Bufferable } from '@aztec/foundation/serialize';
2
+
1
3
  import { strict as assert } from 'assert';
2
4
 
3
5
  import type { AvmContext } from '../avm_context.js';
4
6
  import { type Gas, getBaseGasCost, getDynamicGasCost, mulGas, sumGas } from '../avm_gas.js';
5
7
  import type { BufferCursor } from '../serialization/buffer_cursor.js';
6
- import type { Serializable } from '../serialization/bytecode_serialization.js';
7
8
  import { Opcode, type OperandType, deserialize, serializeAs } from '../serialization/instruction_serialization.js';
8
9
 
9
10
  type InstructionConstructor = {
@@ -44,19 +45,19 @@ export abstract class Instruction {
44
45
  }
45
46
 
46
47
  // Default deserialization which uses Class.opcode and Class.wireFormat.
47
- public static deserialize(
48
+ public static fromBuffer(
48
49
  this: InstructionConstructor & { wireFormat: OperandType[]; as: any },
49
50
  buf: BufferCursor | Buffer,
50
51
  ): Instruction {
51
- return this.as(this.wireFormat).deserialize(buf);
52
+ return this.as(this.wireFormat).fromBuffer(buf);
52
53
  }
53
54
 
54
55
  // Default serialization which uses Class.opcode and Class.wireFormat.
55
- public serialize(): Buffer {
56
+ public toBuffer(): Buffer {
56
57
  const klass = this.constructor as any;
57
58
  assert(klass.opcode !== undefined && klass.opcode !== null);
58
59
  assert(klass.wireFormat !== undefined && klass.wireFormat !== null);
59
- return this.as(klass.opcode, klass.wireFormat).serialize();
60
+ return this.as(klass.opcode, klass.wireFormat).toBuffer();
60
61
  }
61
62
 
62
63
  /**
@@ -65,8 +66,8 @@ export abstract class Instruction {
65
66
  * @param wireFormat The wire format of the instruction.
66
67
  * @returns The new instruction instance.
67
68
  */
68
- public as(opcode: Opcode, wireFormat: OperandType[]): Instruction & Serializable {
69
- return Object.defineProperty(this, 'serialize', {
69
+ public as(opcode: Opcode, wireFormat: OperandType[]): Instruction & Bufferable {
70
+ return Object.defineProperty(this, 'toBuffer', {
70
71
  value: (): Buffer => {
71
72
  return serializeAs(wireFormat, opcode, this);
72
73
  },
@@ -82,7 +83,7 @@ export abstract class Instruction {
82
83
  */
83
84
  public static as(this: InstructionConstructor, wireFormat: OperandType[]) {
84
85
  return Object.assign(this, {
85
- deserialize: (buf: BufferCursor | Buffer): Instruction => {
86
+ fromBuffer: (buf: BufferCursor | Buffer): Instruction => {
86
87
  const res = deserialize(buf, wireFormat);
87
88
  const args = res.slice(1); // Remove opcode.
88
89
  return new this(...args);
@@ -0,0 +1,55 @@
1
+ import type { Fr } from '@aztec/foundation/fields';
2
+
3
+ import type { AvmContext } from './avm_context.js';
4
+ import { type AvmExecutionError, AvmRevertReason } from './errors.js';
5
+
6
+ async function createRevertReason(message: string, revertData: Fr[], context: AvmContext): Promise<AvmRevertReason> {
7
+ // We drop the returnPc information.
8
+ const internalCallStack = context.machineState.internalCallStack.map(entry => entry.callPc);
9
+
10
+ // If we are reverting due to the same error that we have been tracking, we use the nested error as the cause.
11
+ let nestedError = undefined;
12
+ const revertDataEquals = (a: Fr[], b: Fr[]) => a.length === b.length && a.every((v, i) => v.equals(b[i]));
13
+ if (
14
+ context.machineState.collectedRevertInfo &&
15
+ revertDataEquals(context.machineState.collectedRevertInfo.revertDataRepresentative, revertData)
16
+ ) {
17
+ nestedError = context.machineState.collectedRevertInfo.recursiveRevertReason;
18
+ message = context.machineState.collectedRevertInfo.recursiveRevertReason.message;
19
+ }
20
+
21
+ const fnName = await context.persistableState.getPublicFunctionDebugName(context.environment);
22
+
23
+ return new AvmRevertReason(
24
+ message,
25
+ /*failingFunction=*/ {
26
+ contractAddress: context.environment.address,
27
+ functionName: fnName,
28
+ },
29
+ /*noirCallStack=*/ [...internalCallStack, context.machineState.pc].map(pc => `0.${pc}`),
30
+ /*options=*/ { cause: nestedError },
31
+ );
32
+ }
33
+
34
+ /**
35
+ * Create a "revert reason" error for an exceptional halt.
36
+ *
37
+ * @param haltingError - the lower-level error causing the exceptional halt
38
+ * @param context - the context of the AVM execution used to extract the failingFunction and noirCallStack
39
+ */
40
+ export async function revertReasonFromExceptionalHalt(
41
+ haltingError: AvmExecutionError,
42
+ context: AvmContext,
43
+ ): Promise<AvmRevertReason> {
44
+ return await createRevertReason(haltingError.message, [], context);
45
+ }
46
+
47
+ /**
48
+ * Create a "revert reason" error for an explicit revert (a root cause).
49
+ *
50
+ * @param revertData - output data of the explicit REVERT instruction
51
+ * @param context - the context of the AVM execution used to extract the failingFunction and noirCallStack
52
+ */
53
+ export async function revertReasonFromExplicitRevert(revertData: Fr[], context: AvmContext): Promise<AvmRevertReason> {
54
+ return await createRevertReason('Assertion failed: ', revertData, context);
55
+ }