@aztec/simulator 0.35.0 → 0.36.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.
- package/dest/acvm/oracle/oracle.d.ts +6 -5
- package/dest/acvm/oracle/oracle.d.ts.map +1 -1
- package/dest/acvm/oracle/oracle.js +30 -23
- package/dest/acvm/oracle/typed_oracle.d.ts +15 -21
- package/dest/acvm/oracle/typed_oracle.d.ts.map +1 -1
- package/dest/acvm/oracle/typed_oracle.js +10 -10
- package/dest/avm/avm_execution_environment.d.ts +1 -5
- package/dest/avm/avm_execution_environment.d.ts.map +1 -1
- package/dest/avm/avm_execution_environment.js +5 -8
- package/dest/avm/avm_gas.d.ts +1 -9
- package/dest/avm/avm_gas.d.ts.map +1 -1
- package/dest/avm/avm_gas.js +6 -13
- package/dest/avm/avm_machine_state.d.ts +1 -3
- package/dest/avm/avm_machine_state.d.ts.map +1 -1
- package/dest/avm/avm_machine_state.js +8 -9
- package/dest/avm/avm_simulator.js +2 -2
- package/dest/avm/fixtures/index.d.ts +2 -6
- package/dest/avm/fixtures/index.d.ts.map +1 -1
- package/dest/avm/fixtures/index.js +3 -12
- package/dest/avm/journal/journal.d.ts +2 -1
- package/dest/avm/journal/journal.d.ts.map +1 -1
- package/dest/avm/journal/journal.js +8 -17
- package/dest/avm/journal/trace.d.ts +3 -1
- package/dest/avm/journal/trace.d.ts.map +1 -1
- package/dest/avm/journal/trace.js +11 -1
- package/dest/avm/journal/trace_types.d.ts +4 -0
- package/dest/avm/journal/trace_types.d.ts.map +1 -1
- package/dest/avm/journal/trace_types.js +1 -1
- package/dest/avm/opcodes/arithmetic.d.ts.map +1 -1
- package/dest/avm/opcodes/arithmetic.js +1 -1
- package/dest/avm/opcodes/context_getters.d.ts +0 -5
- package/dest/avm/opcodes/context_getters.d.ts.map +1 -1
- package/dest/avm/opcodes/context_getters.js +1 -8
- package/dest/avm/opcodes/contract.d.ts.map +1 -1
- package/dest/avm/opcodes/contract.js +1 -3
- package/dest/avm/opcodes/environment_getters.d.ts +0 -15
- package/dest/avm/opcodes/environment_getters.d.ts.map +1 -1
- package/dest/avm/opcodes/environment_getters.js +1 -35
- package/dest/avm/opcodes/external_calls.d.ts.map +1 -1
- package/dest/avm/opcodes/external_calls.js +5 -6
- package/dest/avm/opcodes/memory.d.ts.map +1 -1
- package/dest/avm/opcodes/memory.js +3 -3
- package/dest/avm/opcodes/storage.d.ts.map +1 -1
- package/dest/avm/opcodes/storage.js +1 -1
- package/dest/avm/serialization/bytecode_serialization.d.ts.map +1 -1
- package/dest/avm/serialization/bytecode_serialization.js +3 -8
- package/dest/avm/serialization/instruction_serialization.d.ts +42 -47
- package/dest/avm/serialization/instruction_serialization.d.ts.map +1 -1
- package/dest/avm/serialization/instruction_serialization.js +43 -48
- package/dest/client/client_execution_context.d.ts +13 -16
- package/dest/client/client_execution_context.d.ts.map +1 -1
- package/dest/client/client_execution_context.js +36 -34
- package/dest/client/db_oracle.d.ts +5 -16
- package/dest/client/db_oracle.d.ts.map +1 -1
- package/dest/client/db_oracle.js +1 -1
- package/dest/client/execution_note_cache.d.ts +6 -2
- package/dest/client/execution_note_cache.d.ts.map +1 -1
- package/dest/client/execution_note_cache.js +10 -7
- package/dest/client/execution_result.d.ts +10 -5
- package/dest/client/execution_result.d.ts.map +1 -1
- package/dest/client/execution_result.js +7 -1
- package/dest/client/private_execution.d.ts.map +1 -1
- package/dest/client/private_execution.js +4 -10
- package/dest/client/simulator.d.ts +1 -3
- package/dest/client/simulator.d.ts.map +1 -1
- package/dest/client/simulator.js +5 -9
- package/dest/client/view_data_oracle.d.ts +7 -11
- package/dest/client/view_data_oracle.d.ts.map +1 -1
- package/dest/client/view_data_oracle.js +8 -14
- package/dest/mocks/fixtures.d.ts +1 -1
- package/dest/mocks/fixtures.d.ts.map +1 -1
- package/dest/mocks/fixtures.js +9 -5
- package/dest/public/abstract_phase_manager.d.ts +4 -4
- package/dest/public/abstract_phase_manager.d.ts.map +1 -1
- package/dest/public/abstract_phase_manager.js +28 -43
- package/dest/public/app_logic_phase_manager.d.ts +1 -7
- package/dest/public/app_logic_phase_manager.d.ts.map +1 -1
- package/dest/public/app_logic_phase_manager.js +1 -7
- package/dest/public/db.d.ts +6 -7
- package/dest/public/db.d.ts.map +1 -1
- package/dest/public/execution.d.ts +15 -6
- package/dest/public/execution.d.ts.map +1 -1
- package/dest/public/execution.js +7 -9
- package/dest/public/executor.d.ts +2 -2
- package/dest/public/executor.d.ts.map +1 -1
- package/dest/public/executor.js +19 -10
- package/dest/public/hints_builder.d.ts +10 -8
- package/dest/public/hints_builder.d.ts.map +1 -1
- package/dest/public/hints_builder.js +29 -35
- package/dest/public/public_execution_context.d.ts +8 -13
- package/dest/public/public_execution_context.d.ts.map +1 -1
- package/dest/public/public_execution_context.js +12 -29
- package/dest/public/public_executor.d.ts +2 -2
- package/dest/public/public_executor.d.ts.map +1 -1
- package/dest/public/public_executor.js +4 -5
- package/dest/public/public_processor.d.ts +1 -2
- package/dest/public/public_processor.d.ts.map +1 -1
- package/dest/public/public_processor.js +1 -1
- package/dest/public/setup_phase_manager.d.ts +0 -6
- package/dest/public/setup_phase_manager.d.ts.map +1 -1
- package/dest/public/setup_phase_manager.js +1 -7
- package/dest/public/state_actions.d.ts.map +1 -1
- package/dest/public/state_actions.js +3 -1
- package/dest/public/tail_phase_manager.d.ts +3 -6
- package/dest/public/tail_phase_manager.d.ts.map +1 -1
- package/dest/public/tail_phase_manager.js +47 -16
- package/dest/public/teardown_phase_manager.d.ts +3 -7
- package/dest/public/teardown_phase_manager.d.ts.map +1 -1
- package/dest/public/teardown_phase_manager.js +14 -7
- package/dest/public/transitional_adaptors.d.ts +2 -2
- package/dest/public/transitional_adaptors.d.ts.map +1 -1
- package/dest/public/transitional_adaptors.js +19 -21
- package/dest/public/utils.d.ts.map +1 -1
- package/dest/public/utils.js +4 -2
- package/dest/test/utils.d.ts.map +1 -1
- package/dest/test/utils.js +3 -5
- package/package.json +16 -8
- package/src/acvm/oracle/oracle.ts +39 -21
- package/src/acvm/oracle/typed_oracle.ts +18 -26
- package/src/avm/avm_execution_environment.ts +0 -13
- package/src/avm/avm_gas.ts +6 -14
- package/src/avm/avm_machine_state.ts +8 -11
- package/src/avm/avm_simulator.ts +1 -1
- package/src/avm/fixtures/index.ts +2 -24
- package/src/avm/journal/journal.ts +14 -25
- package/src/avm/journal/trace.ts +12 -0
- package/src/avm/journal/trace_types.ts +7 -0
- package/src/avm/opcodes/arithmetic.ts +2 -2
- package/src/avm/opcodes/context_getters.ts +0 -9
- package/src/avm/opcodes/contract.ts +0 -2
- package/src/avm/opcodes/environment_getters.ts +0 -45
- package/src/avm/opcodes/external_calls.ts +5 -6
- package/src/avm/opcodes/memory.ts +5 -5
- package/src/avm/opcodes/storage.ts +1 -1
- package/src/avm/serialization/bytecode_serialization.ts +2 -10
- package/src/avm/serialization/instruction_serialization.ts +1 -6
- package/src/client/client_execution_context.ts +79 -54
- package/src/client/db_oracle.ts +5 -17
- package/src/client/execution_note_cache.ts +16 -7
- package/src/client/execution_result.ts +17 -6
- package/src/client/private_execution.ts +4 -9
- package/src/client/simulator.ts +3 -15
- package/src/client/view_data_oracle.ts +25 -29
- package/src/mocks/fixtures.ts +7 -16
- package/src/public/abstract_phase_manager.ts +45 -56
- package/src/public/app_logic_phase_manager.ts +6 -6
- package/src/public/db.ts +7 -8
- package/src/public/execution.ts +22 -18
- package/src/public/executor.ts +31 -5
- package/src/public/hints_builder.ts +62 -62
- package/src/public/public_execution_context.ts +30 -36
- package/src/public/public_executor.ts +4 -6
- package/src/public/public_processor.ts +1 -1
- package/src/public/setup_phase_manager.ts +6 -6
- package/src/public/state_actions.ts +2 -0
- package/src/public/tail_phase_manager.ts +98 -23
- package/src/public/teardown_phase_manager.ts +24 -6
- package/src/public/transitional_adaptors.ts +25 -19
- package/src/public/utils.ts +3 -1
- package/src/test/utils.ts +2 -4
package/src/public/executor.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import { UnencryptedFunctionL2Logs } from '@aztec/circuit-types';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
Fr,
|
|
4
|
+
Gas,
|
|
5
|
+
type GlobalVariables,
|
|
6
|
+
type Header,
|
|
7
|
+
PublicCircuitPublicInputs,
|
|
8
|
+
type TxContext,
|
|
9
|
+
} from '@aztec/circuits.js';
|
|
3
10
|
import { createDebugLogger } from '@aztec/foundation/log';
|
|
4
11
|
|
|
5
12
|
import { spawn } from 'child_process';
|
|
@@ -64,9 +71,11 @@ async function executePublicFunctionAvm(executionContext: PublicExecutionContext
|
|
|
64
71
|
executionContext.execution,
|
|
65
72
|
executionContext.header,
|
|
66
73
|
executionContext.globalVariables,
|
|
74
|
+
executionContext.gasSettings,
|
|
75
|
+
executionContext.transactionFee,
|
|
67
76
|
);
|
|
68
77
|
|
|
69
|
-
const machineState = new AvmMachineState(executionContext.
|
|
78
|
+
const machineState = new AvmMachineState(executionContext.availableGas);
|
|
70
79
|
const context = new AvmContext(worldStateJournal, executionEnv, machineState);
|
|
71
80
|
const simulator = new AvmSimulator(context);
|
|
72
81
|
|
|
@@ -148,10 +157,13 @@ async function executePublicFunctionAcvm(
|
|
|
148
157
|
contractStorageReads: [],
|
|
149
158
|
contractStorageUpdateRequests: [],
|
|
150
159
|
nestedExecutions: [],
|
|
160
|
+
unencryptedLogsHashes: [],
|
|
151
161
|
unencryptedLogs: UnencryptedFunctionL2Logs.empty(),
|
|
152
162
|
reverted,
|
|
153
163
|
revertReason,
|
|
154
|
-
|
|
164
|
+
startGasLeft: context.availableGas,
|
|
165
|
+
endGasLeft: Gas.empty(),
|
|
166
|
+
transactionFee: context.transactionFee,
|
|
155
167
|
};
|
|
156
168
|
}
|
|
157
169
|
|
|
@@ -169,6 +181,7 @@ async function executePublicFunctionAcvm(
|
|
|
169
181
|
newNullifiers: newNullifiersPadded,
|
|
170
182
|
startSideEffectCounter,
|
|
171
183
|
endSideEffectCounter,
|
|
184
|
+
unencryptedLogsHashes: unencryptedLogsHashesPadded,
|
|
172
185
|
} = PublicCircuitPublicInputs.fromFields(returnWitness);
|
|
173
186
|
const returnValues = await context.unpackReturns(returnsHash);
|
|
174
187
|
|
|
@@ -177,6 +190,7 @@ async function executePublicFunctionAcvm(
|
|
|
177
190
|
const newL2ToL1Messages = newL2ToL1Msgs.filter(v => !v.isEmpty());
|
|
178
191
|
const newNoteHashes = newNoteHashesPadded.filter(v => !v.isEmpty());
|
|
179
192
|
const newNullifiers = newNullifiersPadded.filter(v => !v.isEmpty());
|
|
193
|
+
const unencryptedLogsHashes = unencryptedLogsHashesPadded.filter(v => !v.isEmpty());
|
|
180
194
|
|
|
181
195
|
const { contractStorageReads, contractStorageUpdateRequests } = context.getStorageActionData();
|
|
182
196
|
|
|
@@ -193,7 +207,10 @@ async function executePublicFunctionAcvm(
|
|
|
193
207
|
|
|
194
208
|
const nestedExecutions = context.getNestedExecutions();
|
|
195
209
|
const unencryptedLogs = context.getUnencryptedLogs();
|
|
196
|
-
|
|
210
|
+
|
|
211
|
+
// TODO(palla/gas): We should be loading these values from the returned PublicCircuitPublicInputs
|
|
212
|
+
const startGasLeft = context.availableGas;
|
|
213
|
+
const endGasLeft = context.availableGas; // No gas consumption in non-AVM
|
|
197
214
|
|
|
198
215
|
return {
|
|
199
216
|
execution,
|
|
@@ -208,10 +225,13 @@ async function executePublicFunctionAcvm(
|
|
|
208
225
|
contractStorageUpdateRequests,
|
|
209
226
|
returnValues,
|
|
210
227
|
nestedExecutions,
|
|
228
|
+
unencryptedLogsHashes,
|
|
211
229
|
unencryptedLogs,
|
|
212
230
|
reverted: false,
|
|
213
231
|
revertReason: undefined,
|
|
214
|
-
|
|
232
|
+
startGasLeft,
|
|
233
|
+
endGasLeft,
|
|
234
|
+
transactionFee: context.transactionFee,
|
|
215
235
|
};
|
|
216
236
|
}
|
|
217
237
|
|
|
@@ -236,6 +256,9 @@ export class PublicExecutor {
|
|
|
236
256
|
public async simulate(
|
|
237
257
|
execution: PublicExecution,
|
|
238
258
|
globalVariables: GlobalVariables,
|
|
259
|
+
availableGas: Gas,
|
|
260
|
+
txContext: TxContext,
|
|
261
|
+
transactionFee: Fr = Fr.ZERO,
|
|
239
262
|
sideEffectCounter: number = 0,
|
|
240
263
|
): Promise<PublicExecutionResult> {
|
|
241
264
|
// Functions can request to pack arguments before calling other functions.
|
|
@@ -251,6 +274,9 @@ export class PublicExecutor {
|
|
|
251
274
|
this.stateDb,
|
|
252
275
|
this.contractsDb,
|
|
253
276
|
this.commitmentsDb,
|
|
277
|
+
availableGas,
|
|
278
|
+
transactionFee,
|
|
279
|
+
txContext.gasSettings,
|
|
254
280
|
);
|
|
255
281
|
|
|
256
282
|
const executionResult = await executePublicFunction(context, /*nested=*/ false);
|
|
@@ -1,65 +1,77 @@
|
|
|
1
1
|
import { MerkleTreeId } from '@aztec/circuit-types';
|
|
2
2
|
import {
|
|
3
3
|
type Fr,
|
|
4
|
-
MAX_NEW_NULLIFIERS_PER_TX,
|
|
4
|
+
type MAX_NEW_NULLIFIERS_PER_TX,
|
|
5
5
|
type MAX_NULLIFIER_NON_EXISTENT_READ_REQUESTS_PER_TX,
|
|
6
6
|
type MAX_NULLIFIER_READ_REQUESTS_PER_TX,
|
|
7
|
-
|
|
7
|
+
type MAX_PUBLIC_DATA_HINTS,
|
|
8
|
+
type MAX_PUBLIC_DATA_READS_PER_TX,
|
|
9
|
+
type MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
|
|
8
10
|
MembershipWitness,
|
|
9
11
|
NULLIFIER_TREE_HEIGHT,
|
|
12
|
+
type Nullifier,
|
|
10
13
|
PUBLIC_DATA_TREE_HEIGHT,
|
|
14
|
+
type PublicDataHint,
|
|
11
15
|
type PublicDataRead,
|
|
12
|
-
PublicDataTreeLeafPreimage,
|
|
16
|
+
type PublicDataTreeLeafPreimage,
|
|
17
|
+
type PublicDataUpdateRequest,
|
|
13
18
|
type ReadRequestContext,
|
|
14
|
-
type SideEffectLinkedToNoteHash,
|
|
15
19
|
buildNullifierNonExistentReadRequestHints,
|
|
16
20
|
buildNullifierReadRequestHints,
|
|
17
|
-
|
|
21
|
+
buildPublicDataHints,
|
|
22
|
+
buildPublicDataReadRequestHints,
|
|
18
23
|
} from '@aztec/circuits.js';
|
|
19
|
-
import { makeTuple } from '@aztec/foundation/array';
|
|
20
24
|
import { type Tuple } from '@aztec/foundation/serialize';
|
|
21
|
-
import { type MerkleTreeOperations } from '@aztec/world-state';
|
|
25
|
+
import { type IndexedTreeId, type MerkleTreeOperations } from '@aztec/world-state';
|
|
22
26
|
|
|
23
27
|
export class HintsBuilder {
|
|
24
28
|
constructor(private db: MerkleTreeOperations) {}
|
|
25
29
|
|
|
26
30
|
getNullifierReadRequestHints(
|
|
27
31
|
nullifierReadRequests: Tuple<ReadRequestContext, typeof MAX_NULLIFIER_READ_REQUESTS_PER_TX>,
|
|
28
|
-
|
|
29
|
-
nullifiersRevertible: Tuple<SideEffectLinkedToNoteHash, typeof MAX_NEW_NULLIFIERS_PER_TX>,
|
|
32
|
+
pendingNullifiers: Tuple<Nullifier, typeof MAX_NEW_NULLIFIERS_PER_TX>,
|
|
30
33
|
) {
|
|
31
|
-
return buildNullifierReadRequestHints(
|
|
32
|
-
this,
|
|
33
|
-
nullifierReadRequests,
|
|
34
|
-
mergeAccumulatedData(MAX_NEW_NULLIFIERS_PER_TX, nullifiersNonRevertible, nullifiersRevertible),
|
|
35
|
-
);
|
|
34
|
+
return buildNullifierReadRequestHints(this, nullifierReadRequests, pendingNullifiers);
|
|
36
35
|
}
|
|
37
36
|
|
|
38
37
|
getNullifierNonExistentReadRequestHints(
|
|
39
38
|
nullifierNonExistentReadRequests: Tuple<ReadRequestContext, typeof MAX_NULLIFIER_NON_EXISTENT_READ_REQUESTS_PER_TX>,
|
|
40
|
-
|
|
41
|
-
nullifiersRevertible: Tuple<SideEffectLinkedToNoteHash, typeof MAX_NEW_NULLIFIERS_PER_TX>,
|
|
39
|
+
pendingNullifiers: Tuple<Nullifier, typeof MAX_NEW_NULLIFIERS_PER_TX>,
|
|
42
40
|
) {
|
|
43
|
-
const pendingNullifiers = mergeAccumulatedData(
|
|
44
|
-
MAX_NEW_NULLIFIERS_PER_TX,
|
|
45
|
-
nullifiersNonRevertible,
|
|
46
|
-
nullifiersRevertible,
|
|
47
|
-
);
|
|
48
41
|
return buildNullifierNonExistentReadRequestHints(this, nullifierNonExistentReadRequests, pendingNullifiers);
|
|
49
42
|
}
|
|
50
43
|
|
|
44
|
+
getPublicDataHints(
|
|
45
|
+
publicDataReads: Tuple<PublicDataRead, typeof MAX_PUBLIC_DATA_READS_PER_TX>,
|
|
46
|
+
publicDataUpdateRequests: Tuple<PublicDataUpdateRequest, typeof MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX>,
|
|
47
|
+
) {
|
|
48
|
+
return buildPublicDataHints(this, publicDataReads, publicDataUpdateRequests);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
getPublicDataReadRequestHints(
|
|
52
|
+
publicDataReads: Tuple<PublicDataRead, typeof MAX_PUBLIC_DATA_READS_PER_TX>,
|
|
53
|
+
publicDataUpdateRequests: Tuple<PublicDataUpdateRequest, typeof MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX>,
|
|
54
|
+
publicDataHints: Tuple<PublicDataHint, typeof MAX_PUBLIC_DATA_HINTS>,
|
|
55
|
+
) {
|
|
56
|
+
return buildPublicDataReadRequestHints(publicDataReads, publicDataUpdateRequests, publicDataHints);
|
|
57
|
+
}
|
|
58
|
+
|
|
51
59
|
async getNullifierMembershipWitness(nullifier: Fr) {
|
|
52
60
|
const index = await this.db.findLeafIndex(MerkleTreeId.NULLIFIER_TREE, nullifier.toBuffer());
|
|
53
61
|
if (index === undefined) {
|
|
54
|
-
|
|
62
|
+
throw new Error(`Cannot find the leaf for nullifier ${nullifier.toBigInt()}.`);
|
|
55
63
|
}
|
|
56
64
|
|
|
57
|
-
return this.
|
|
65
|
+
return this.getMembershipWitnessWithPreimage<typeof NULLIFIER_TREE_HEIGHT>(
|
|
66
|
+
MerkleTreeId.NULLIFIER_TREE,
|
|
67
|
+
NULLIFIER_TREE_HEIGHT,
|
|
68
|
+
index,
|
|
69
|
+
);
|
|
58
70
|
}
|
|
59
71
|
|
|
60
72
|
async getLowNullifierMembershipWitness(nullifier: Fr) {
|
|
61
73
|
const res = await this.db.getPreviousValueIndex(MerkleTreeId.NULLIFIER_TREE, nullifier.toBigInt());
|
|
62
|
-
if (res
|
|
74
|
+
if (!res) {
|
|
63
75
|
throw new Error(`Cannot find the low leaf for nullifier ${nullifier.toBigInt()}.`);
|
|
64
76
|
}
|
|
65
77
|
|
|
@@ -68,52 +80,40 @@ export class HintsBuilder {
|
|
|
68
80
|
throw new Error(`Nullifier ${nullifier.toBigInt()} already exists in the tree.`);
|
|
69
81
|
}
|
|
70
82
|
|
|
71
|
-
return this.
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
private async getNullifierMembershipWitnessWithPreimage(index: bigint) {
|
|
75
|
-
const siblingPath = await this.db.getSiblingPath<typeof NULLIFIER_TREE_HEIGHT>(MerkleTreeId.NULLIFIER_TREE, index);
|
|
76
|
-
const membershipWitness = new MembershipWitness(
|
|
83
|
+
return this.getMembershipWitnessWithPreimage<typeof NULLIFIER_TREE_HEIGHT>(
|
|
84
|
+
MerkleTreeId.NULLIFIER_TREE,
|
|
77
85
|
NULLIFIER_TREE_HEIGHT,
|
|
78
86
|
index,
|
|
79
|
-
siblingPath.toTuple<typeof NULLIFIER_TREE_HEIGHT>(),
|
|
80
87
|
);
|
|
88
|
+
}
|
|
81
89
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
90
|
+
async getMatchOrLowPublicDataMembershipWitness(leafSlot: bigint) {
|
|
91
|
+
const res = await this.db.getPreviousValueIndex(MerkleTreeId.PUBLIC_DATA_TREE, leafSlot);
|
|
92
|
+
if (!res) {
|
|
93
|
+
throw new Error(`Cannot find the previous value index for public data ${leafSlot}.`);
|
|
85
94
|
}
|
|
86
95
|
|
|
87
|
-
|
|
96
|
+
const { membershipWitness, leafPreimage } = await this.getMembershipWitnessWithPreimage<
|
|
97
|
+
typeof PUBLIC_DATA_TREE_HEIGHT
|
|
98
|
+
>(MerkleTreeId.PUBLIC_DATA_TREE, PUBLIC_DATA_TREE_HEIGHT, res.index);
|
|
99
|
+
|
|
100
|
+
// Should find a way to stop casting IndexedTreeLeafPreimage as PublicDataTreeLeafPreimage everywhere.
|
|
101
|
+
return { membershipWitness, leafPreimage: leafPreimage as PublicDataTreeLeafPreimage };
|
|
88
102
|
}
|
|
89
103
|
|
|
90
|
-
async
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
const lowLeafResult = await this.db.getPreviousValueIndex(MerkleTreeId.PUBLIC_DATA_TREE, leafSlot);
|
|
102
|
-
if (!lowLeafResult) {
|
|
103
|
-
throw new Error(`Public data tree should have one initial leaf`);
|
|
104
|
-
}
|
|
105
|
-
const preimage = await this.db.getLeafPreimage(MerkleTreeId.PUBLIC_DATA_TREE, lowLeafResult.index);
|
|
106
|
-
const path = await this.db.getSiblingPath(MerkleTreeId.PUBLIC_DATA_TREE, lowLeafResult.index);
|
|
107
|
-
newPublicDataReadsWitnesses[i] = new MembershipWitness(
|
|
108
|
-
PUBLIC_DATA_TREE_HEIGHT,
|
|
109
|
-
BigInt(lowLeafResult.index),
|
|
110
|
-
path.toTuple<typeof PUBLIC_DATA_TREE_HEIGHT>(),
|
|
111
|
-
);
|
|
112
|
-
newPublicDataReadsPreimages[i] = preimage! as PublicDataTreeLeafPreimage;
|
|
104
|
+
private async getMembershipWitnessWithPreimage<TREE_HEIGHT extends number>(
|
|
105
|
+
treeId: IndexedTreeId,
|
|
106
|
+
treeHeight: TREE_HEIGHT,
|
|
107
|
+
index: bigint,
|
|
108
|
+
) {
|
|
109
|
+
const siblingPath = await this.db.getSiblingPath(treeId, index);
|
|
110
|
+
const membershipWitness = new MembershipWitness<TREE_HEIGHT>(treeHeight, index, siblingPath.toTuple());
|
|
111
|
+
|
|
112
|
+
const leafPreimage = await this.db.getLeafPreimage(treeId, index);
|
|
113
|
+
if (!leafPreimage) {
|
|
114
|
+
throw new Error(`Cannot find the leaf preimage for tree ${treeId} at index ${index}.`);
|
|
113
115
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
newPublicDataReadsPreimages,
|
|
117
|
-
};
|
|
116
|
+
|
|
117
|
+
return { membershipWitness, leafPreimage };
|
|
118
118
|
}
|
|
119
119
|
}
|
|
@@ -3,11 +3,13 @@ import {
|
|
|
3
3
|
CallContext,
|
|
4
4
|
FunctionData,
|
|
5
5
|
type FunctionSelector,
|
|
6
|
+
type Gas,
|
|
7
|
+
type GasSettings,
|
|
6
8
|
type GlobalVariables,
|
|
7
9
|
type Header,
|
|
10
|
+
PublicContextInputs,
|
|
8
11
|
} from '@aztec/circuits.js';
|
|
9
12
|
import { type AztecAddress } from '@aztec/foundation/aztec-address';
|
|
10
|
-
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
11
13
|
import { Fr } from '@aztec/foundation/fields';
|
|
12
14
|
import { createDebugLogger } from '@aztec/foundation/log';
|
|
13
15
|
import { type ContractInstance } from '@aztec/types/contracts';
|
|
@@ -39,6 +41,9 @@ export class PublicExecutionContext extends TypedOracle {
|
|
|
39
41
|
public readonly stateDb: PublicStateDB,
|
|
40
42
|
public readonly contractsDb: PublicContractsDB,
|
|
41
43
|
public readonly commitmentsDb: CommitmentsDB,
|
|
44
|
+
public readonly availableGas: Gas,
|
|
45
|
+
public readonly transactionFee: Fr,
|
|
46
|
+
public readonly gasSettings: GasSettings,
|
|
42
47
|
private log = createDebugLogger('aztec:simulator:public_execution_context'),
|
|
43
48
|
) {
|
|
44
49
|
super();
|
|
@@ -56,14 +61,15 @@ export class PublicExecutionContext extends TypedOracle {
|
|
|
56
61
|
*/
|
|
57
62
|
public getInitialWitness(witnessStartIndex = 0) {
|
|
58
63
|
const { callContext, args } = this.execution;
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
64
|
+
const publicContextInputs = new PublicContextInputs(
|
|
65
|
+
callContext,
|
|
66
|
+
this.header,
|
|
67
|
+
this.globalVariables,
|
|
68
|
+
this.sideEffectCounter.current(),
|
|
69
|
+
this.availableGas,
|
|
70
|
+
this.transactionFee,
|
|
71
|
+
);
|
|
72
|
+
const fields = [...publicContextInputs.toFields(), ...args];
|
|
67
73
|
return toACVMWitness(witnessStartIndex, fields);
|
|
68
74
|
}
|
|
69
75
|
|
|
@@ -90,10 +96,10 @@ export class PublicExecutionContext extends TypedOracle {
|
|
|
90
96
|
}
|
|
91
97
|
|
|
92
98
|
/**
|
|
93
|
-
* Pack the given arguments.
|
|
99
|
+
* Pack the given array of arguments.
|
|
94
100
|
* @param args - Arguments to pack
|
|
95
101
|
*/
|
|
96
|
-
public
|
|
102
|
+
public override packArgumentsArray(args: Fr[]): Promise<Fr> {
|
|
97
103
|
return Promise.resolve(this.packedValuesCache.pack(args));
|
|
98
104
|
}
|
|
99
105
|
|
|
@@ -101,7 +107,7 @@ export class PublicExecutionContext extends TypedOracle {
|
|
|
101
107
|
* Pack the given returns.
|
|
102
108
|
* @param returns - Returns to pack
|
|
103
109
|
*/
|
|
104
|
-
public packReturns(returns: Fr[]): Promise<Fr> {
|
|
110
|
+
public override packReturns(returns: Fr[]): Promise<Fr> {
|
|
105
111
|
return Promise.resolve(this.packedValuesCache.pack(returns));
|
|
106
112
|
}
|
|
107
113
|
|
|
@@ -109,7 +115,7 @@ export class PublicExecutionContext extends TypedOracle {
|
|
|
109
115
|
* Unpack the given returns.
|
|
110
116
|
* @param returnsHash - Returns hash to unpack
|
|
111
117
|
*/
|
|
112
|
-
public unpackReturns(returnsHash: Fr): Promise<Fr[]> {
|
|
118
|
+
public override unpackReturns(returnsHash: Fr): Promise<Fr[]> {
|
|
113
119
|
return Promise.resolve(this.packedValuesCache.unpack(returnsHash));
|
|
114
120
|
}
|
|
115
121
|
|
|
@@ -121,7 +127,7 @@ export class PublicExecutionContext extends TypedOracle {
|
|
|
121
127
|
* @dev Contract address and secret are only used to compute the nullifier to get non-nullified messages
|
|
122
128
|
* @returns The l1 to l2 membership witness (index of message in the tree and sibling path).
|
|
123
129
|
*/
|
|
124
|
-
public async getL1ToL2MembershipWitness(contractAddress: AztecAddress, messageHash: Fr, secret: Fr) {
|
|
130
|
+
public override async getL1ToL2MembershipWitness(contractAddress: AztecAddress, messageHash: Fr, secret: Fr) {
|
|
125
131
|
return await this.commitmentsDb.getL1ToL2MembershipWitness(contractAddress, messageHash, secret);
|
|
126
132
|
}
|
|
127
133
|
|
|
@@ -129,20 +135,11 @@ export class PublicExecutionContext extends TypedOracle {
|
|
|
129
135
|
* Emit an unencrypted log.
|
|
130
136
|
* @param log - The unencrypted log to be emitted.
|
|
131
137
|
*/
|
|
132
|
-
public emitUnencryptedLog(log: UnencryptedL2Log) {
|
|
138
|
+
public override emitUnencryptedLog(log: UnencryptedL2Log) {
|
|
133
139
|
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/885)
|
|
134
140
|
this.unencryptedLogs.push(log);
|
|
135
141
|
this.log.verbose(`Emitted unencrypted log: "${log.toHumanReadable()}"`);
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Retrieves the portal contract address associated with the given contract address.
|
|
140
|
-
* Returns zero address if the input contract address is not found or invalid.
|
|
141
|
-
* @param contractAddress - The address of the contract whose portal address is to be fetched.
|
|
142
|
-
* @returns The portal contract address.
|
|
143
|
-
*/
|
|
144
|
-
public async getPortalContractAddress(contractAddress: AztecAddress) {
|
|
145
|
-
return (await this.contractsDb.getPortalContractAddress(contractAddress)) ?? EthAddress.ZERO;
|
|
142
|
+
return Fr.fromBuffer(log.hash());
|
|
146
143
|
}
|
|
147
144
|
|
|
148
145
|
/**
|
|
@@ -150,7 +147,7 @@ export class PublicExecutionContext extends TypedOracle {
|
|
|
150
147
|
* @param startStorageSlot - The starting storage slot.
|
|
151
148
|
* @param numberOfElements - Number of elements to read from the starting storage slot.
|
|
152
149
|
*/
|
|
153
|
-
public async storageRead(startStorageSlot: Fr, numberOfElements: number) {
|
|
150
|
+
public override async storageRead(startStorageSlot: Fr, numberOfElements: number) {
|
|
154
151
|
const values = [];
|
|
155
152
|
for (let i = 0; i < Number(numberOfElements); i++) {
|
|
156
153
|
const storageSlot = new Fr(startStorageSlot.value + BigInt(i));
|
|
@@ -167,7 +164,7 @@ export class PublicExecutionContext extends TypedOracle {
|
|
|
167
164
|
* @param startStorageSlot - The starting storage slot.
|
|
168
165
|
* @param values - The values to be written.
|
|
169
166
|
*/
|
|
170
|
-
public async storageWrite(startStorageSlot: Fr, values: Fr[]) {
|
|
167
|
+
public override async storageWrite(startStorageSlot: Fr, values: Fr[]) {
|
|
171
168
|
const newValues = [];
|
|
172
169
|
for (let i = 0; i < values.length; i++) {
|
|
173
170
|
const storageSlot = new Fr(startStorageSlot.toBigInt() + BigInt(i));
|
|
@@ -188,7 +185,7 @@ export class PublicExecutionContext extends TypedOracle {
|
|
|
188
185
|
* @param argsHash - The packed arguments to pass to the function.
|
|
189
186
|
* @returns The return values of the public function.
|
|
190
187
|
*/
|
|
191
|
-
public async callPublicFunction(
|
|
188
|
+
public override async callPublicFunction(
|
|
192
189
|
targetContractAddress: AztecAddress,
|
|
193
190
|
functionSelector: FunctionSelector,
|
|
194
191
|
argsHash: Fr,
|
|
@@ -203,20 +200,14 @@ export class PublicExecutionContext extends TypedOracle {
|
|
|
203
200
|
`Public function call: addr=${targetContractAddress} selector=${functionSelector} args=${args.join(',')}`,
|
|
204
201
|
);
|
|
205
202
|
|
|
206
|
-
const portalAddress = (await this.contractsDb.getPortalContractAddress(targetContractAddress)) ?? EthAddress.ZERO;
|
|
207
203
|
const functionData = new FunctionData(functionSelector, /*isPrivate=*/ false);
|
|
208
|
-
const { transactionFee, gasSettings, gasLeft } = this.execution.callContext;
|
|
209
204
|
const callContext = CallContext.from({
|
|
210
205
|
msgSender: isDelegateCall ? this.execution.callContext.msgSender : this.execution.contractAddress,
|
|
211
206
|
storageContractAddress: isDelegateCall ? this.execution.contractAddress : targetContractAddress,
|
|
212
|
-
portalContractAddress: portalAddress,
|
|
213
207
|
functionSelector,
|
|
214
|
-
gasLeft, // Propagate the same gas left as when we started since ACVM public functions don't have any metering
|
|
215
208
|
isDelegateCall,
|
|
216
209
|
isStaticCall,
|
|
217
210
|
sideEffectCounter,
|
|
218
|
-
gasSettings,
|
|
219
|
-
transactionFee,
|
|
220
211
|
});
|
|
221
212
|
|
|
222
213
|
const nestedExecution: PublicExecution = {
|
|
@@ -235,6 +226,9 @@ export class PublicExecutionContext extends TypedOracle {
|
|
|
235
226
|
this.stateDb,
|
|
236
227
|
this.contractsDb,
|
|
237
228
|
this.commitmentsDb,
|
|
229
|
+
this.availableGas,
|
|
230
|
+
this.transactionFee,
|
|
231
|
+
this.gasSettings,
|
|
238
232
|
this.log,
|
|
239
233
|
);
|
|
240
234
|
|
|
@@ -256,12 +250,12 @@ export class PublicExecutionContext extends TypedOracle {
|
|
|
256
250
|
return childExecutionResult.returnValues;
|
|
257
251
|
}
|
|
258
252
|
|
|
259
|
-
public async checkNullifierExists(nullifier: Fr): Promise<boolean> {
|
|
253
|
+
public override async checkNullifierExists(nullifier: Fr): Promise<boolean> {
|
|
260
254
|
const witness = await this.commitmentsDb.getNullifierMembershipWitnessAtLatestBlock(nullifier);
|
|
261
255
|
return !!witness;
|
|
262
256
|
}
|
|
263
257
|
|
|
264
|
-
public async getContractInstance(address: AztecAddress): Promise<ContractInstance> {
|
|
258
|
+
public override async getContractInstance(address: AztecAddress): Promise<ContractInstance> {
|
|
265
259
|
// Note to AVM implementor: The wrapper of the oracle call get_contract_instance in aztec-nr
|
|
266
260
|
// automatically checks that the returned instance is correct, by hashing it together back
|
|
267
261
|
// into the address. However, in the AVM, we also need to prove the negative, otherwise a malicious
|
|
@@ -3,7 +3,6 @@ import {
|
|
|
3
3
|
type AztecAddress,
|
|
4
4
|
ContractClassRegisteredEvent,
|
|
5
5
|
ContractInstanceDeployedEvent,
|
|
6
|
-
type EthAddress,
|
|
7
6
|
Fr,
|
|
8
7
|
type FunctionSelector,
|
|
9
8
|
type L1_TO_L2_MSG_TREE_HEIGHT,
|
|
@@ -95,11 +94,6 @@ export class ContractsDataSourcePublicDB implements PublicContractsDB {
|
|
|
95
94
|
}
|
|
96
95
|
return contractClass.publicFunctions.find(f => f.selector.equals(selector))?.bytecode;
|
|
97
96
|
}
|
|
98
|
-
|
|
99
|
-
async getPortalContractAddress(address: AztecAddress): Promise<EthAddress | undefined> {
|
|
100
|
-
const contract = await this.getContractInstance(address);
|
|
101
|
-
return contract?.portalContractAddress;
|
|
102
|
-
}
|
|
103
97
|
}
|
|
104
98
|
|
|
105
99
|
/**
|
|
@@ -257,6 +251,10 @@ export class WorldStateDB implements CommitmentsDB {
|
|
|
257
251
|
return new MessageLoadOracleInputs<typeof L1_TO_L2_MSG_TREE_HEIGHT>(messageIndex, siblingPath);
|
|
258
252
|
}
|
|
259
253
|
|
|
254
|
+
public async getL1ToL2LeafValue(leafIndex: bigint): Promise<Fr | undefined> {
|
|
255
|
+
return await this.db.getLeafValue(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, leafIndex);
|
|
256
|
+
}
|
|
257
|
+
|
|
260
258
|
public async getCommitmentIndex(commitment: Fr): Promise<bigint | undefined> {
|
|
261
259
|
return await this.db.findLeafIndex(MerkleTreeId.NOTE_HASH_TREE, commitment);
|
|
262
260
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
type BlockProver,
|
|
3
3
|
type FailedTx,
|
|
4
|
+
type ProcessReturnValues,
|
|
4
5
|
type ProcessedTx,
|
|
5
6
|
type PublicKernelRequest,
|
|
6
7
|
type SimulationError,
|
|
@@ -13,7 +14,6 @@ import {
|
|
|
13
14
|
} from '@aztec/circuit-types';
|
|
14
15
|
import { type TxSequencerProcessingStats } from '@aztec/circuit-types/stats';
|
|
15
16
|
import { type GlobalVariables, type Header, type KernelCircuitPublicInputs } from '@aztec/circuits.js';
|
|
16
|
-
import { type ProcessReturnValues } from '@aztec/foundation/abi';
|
|
17
17
|
import { createDebugLogger } from '@aztec/foundation/log';
|
|
18
18
|
import { Timer } from '@aztec/foundation/timer';
|
|
19
19
|
import { PublicExecutor, type PublicStateDB, type SimulationProvider } from '@aztec/simulator';
|
|
@@ -17,14 +17,14 @@ import { type PublicKernelCircuitSimulator } from './public_kernel_circuit_simul
|
|
|
17
17
|
*/
|
|
18
18
|
export class SetupPhaseManager extends AbstractPhaseManager {
|
|
19
19
|
constructor(
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
db: MerkleTreeOperations,
|
|
21
|
+
publicExecutor: PublicExecutor,
|
|
22
|
+
publicKernel: PublicKernelCircuitSimulator,
|
|
23
|
+
globalVariables: GlobalVariables,
|
|
24
|
+
historicalHeader: Header,
|
|
25
25
|
protected publicContractsDB: ContractsDataSourcePublicDB,
|
|
26
26
|
protected publicStateDB: PublicStateDB,
|
|
27
|
-
|
|
27
|
+
phase: PublicKernelPhase = PublicKernelPhase.SETUP,
|
|
28
28
|
) {
|
|
29
29
|
super(db, publicExecutor, publicKernel, globalVariables, historicalHeader, phase);
|
|
30
30
|
}
|
|
@@ -83,6 +83,7 @@ export class ContractStorageActionsCollector {
|
|
|
83
83
|
public collect(): [ContractStorageRead[], ContractStorageUpdateRequest[]] {
|
|
84
84
|
const reads = Array.from(this.contractStorageReads.entries()).map(([slot, valueAndCounter]) =>
|
|
85
85
|
ContractStorageRead.from({
|
|
86
|
+
contractAddress: this.address,
|
|
86
87
|
storageSlot: new Fr(slot),
|
|
87
88
|
...valueAndCounter,
|
|
88
89
|
}),
|
|
@@ -90,6 +91,7 @@ export class ContractStorageActionsCollector {
|
|
|
90
91
|
|
|
91
92
|
const updateRequests = Array.from(this.contractStorageUpdateRequests.entries()).map(([slot, valuesAndCounter]) =>
|
|
92
93
|
ContractStorageUpdateRequest.from({
|
|
94
|
+
contractAddress: this.address,
|
|
93
95
|
storageSlot: new Fr(slot),
|
|
94
96
|
...valuesAndCounter,
|
|
95
97
|
}),
|