@aztec/simulator 0.0.1-commit.b655e406 → 0.0.1-commit.fce3e4f
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/client.d.ts +1 -1
- package/dest/common/errors.d.ts +1 -7
- package/dest/common/errors.d.ts.map +1 -1
- package/dest/common/index.d.ts +1 -1
- package/dest/common/stats/index.d.ts +1 -1
- package/dest/common/stats/stats.d.ts +1 -1
- package/dest/private/acvm/acvm.d.ts +1 -1
- package/dest/private/acvm/acvm_types.d.ts +1 -1
- package/dest/private/acvm/deserialize.d.ts +1 -1
- package/dest/private/acvm/index.d.ts +1 -1
- package/dest/private/acvm/serialize.d.ts +1 -1
- package/dest/private/acvm_native.d.ts +1 -1
- package/dest/private/acvm_native.d.ts.map +1 -1
- package/dest/private/acvm_wasm.d.ts +1 -1
- package/dest/private/acvm_wasm.d.ts.map +1 -1
- package/dest/private/acvm_wasm_with_blobs.d.ts +1 -1
- package/dest/private/acvm_wasm_with_blobs.d.ts.map +1 -1
- package/dest/private/circuit_recording/circuit_recorder.d.ts +1 -1
- package/dest/private/circuit_recording/circuit_recorder.d.ts.map +1 -1
- package/dest/private/circuit_recording/file_circuit_recorder.d.ts +1 -1
- package/dest/private/circuit_recording/file_circuit_recorder.d.ts.map +1 -1
- package/dest/private/circuit_recording/memory_circuit_recorder.d.ts +1 -1
- package/dest/private/circuit_recording/memory_circuit_recorder.d.ts.map +1 -1
- package/dest/private/circuit_recording/simulator_recorder_wrapper.d.ts +1 -1
- package/dest/private/circuit_recording/simulator_recorder_wrapper.d.ts.map +1 -1
- package/dest/private/circuit_simulator.d.ts +1 -1
- package/dest/private/factory.d.ts +1 -1
- package/dest/public/avm/avm_context.d.ts +1 -1
- package/dest/public/avm/avm_context.d.ts.map +1 -1
- package/dest/public/avm/avm_contract_call_result.d.ts +1 -1
- package/dest/public/avm/avm_contract_call_result.d.ts.map +1 -1
- package/dest/public/avm/avm_execution_environment.d.ts +4 -4
- package/dest/public/avm/avm_execution_environment.d.ts.map +1 -1
- package/dest/public/avm/avm_execution_environment.js +4 -7
- package/dest/public/avm/avm_gas.d.ts +2 -2
- package/dest/public/avm/avm_gas.d.ts.map +1 -1
- package/dest/public/avm/avm_machine_state.d.ts +1 -1
- package/dest/public/avm/avm_machine_state.d.ts.map +1 -1
- package/dest/public/avm/avm_memory_types.d.ts +13 -13
- package/dest/public/avm/avm_memory_types.d.ts.map +1 -1
- package/dest/public/avm/avm_simulator.d.ts +3 -2
- package/dest/public/avm/avm_simulator.d.ts.map +1 -1
- package/dest/public/avm/avm_simulator.js +2 -2
- package/dest/public/avm/avm_simulator_interface.d.ts +1 -1
- package/dest/public/avm/errors.d.ts +1 -1
- package/dest/public/avm/errors.d.ts.map +1 -1
- package/dest/public/avm/fixtures/avm_simulation_tester.d.ts +1 -1
- package/dest/public/avm/fixtures/avm_simulation_tester.d.ts.map +1 -1
- package/dest/public/avm/fixtures/avm_simulation_tester.js +11 -2
- package/dest/public/avm/fixtures/base_avm_simulation_tester.d.ts +1 -1
- package/dest/public/avm/fixtures/base_avm_simulation_tester.d.ts.map +1 -1
- package/dest/public/avm/fixtures/initializers.d.ts +1 -2
- package/dest/public/avm/fixtures/initializers.d.ts.map +1 -1
- package/dest/public/avm/fixtures/initializers.js +6 -4
- package/dest/public/avm/fixtures/utils.d.ts +1 -1
- package/dest/public/avm/index.d.ts +1 -1
- package/dest/public/avm/opcodes/accrued_substate.d.ts +1 -1
- package/dest/public/avm/opcodes/accrued_substate.d.ts.map +1 -1
- package/dest/public/avm/opcodes/addressing_mode.d.ts +1 -1
- package/dest/public/avm/opcodes/addressing_mode.d.ts.map +1 -1
- package/dest/public/avm/opcodes/arithmetic.d.ts +8 -8
- package/dest/public/avm/opcodes/arithmetic.d.ts.map +1 -1
- package/dest/public/avm/opcodes/bitwise.d.ts +5 -5
- package/dest/public/avm/opcodes/bitwise.d.ts.map +1 -1
- package/dest/public/avm/opcodes/comparators.d.ts +4 -4
- package/dest/public/avm/opcodes/comparators.d.ts.map +1 -1
- package/dest/public/avm/opcodes/contract.d.ts +1 -1
- package/dest/public/avm/opcodes/contract.d.ts.map +1 -1
- package/dest/public/avm/opcodes/control_flow.d.ts +1 -1
- package/dest/public/avm/opcodes/control_flow.d.ts.map +1 -1
- package/dest/public/avm/opcodes/conversion.d.ts +1 -1
- package/dest/public/avm/opcodes/conversion.d.ts.map +1 -1
- package/dest/public/avm/opcodes/ec_add.d.ts +2 -2
- package/dest/public/avm/opcodes/ec_add.d.ts.map +1 -1
- package/dest/public/avm/opcodes/environment_getters.d.ts +1 -1
- package/dest/public/avm/opcodes/environment_getters.d.ts.map +1 -1
- package/dest/public/avm/opcodes/external_calls.d.ts +1 -1
- package/dest/public/avm/opcodes/external_calls.d.ts.map +1 -1
- package/dest/public/avm/opcodes/hashing.d.ts +2 -2
- package/dest/public/avm/opcodes/hashing.d.ts.map +1 -1
- package/dest/public/avm/opcodes/index.d.ts +1 -1
- package/dest/public/avm/opcodes/instruction.d.ts +2 -2
- package/dest/public/avm/opcodes/instruction.d.ts.map +1 -1
- package/dest/public/avm/opcodes/instruction_impl.d.ts +1 -1
- package/dest/public/avm/opcodes/instruction_impl.d.ts.map +1 -1
- package/dest/public/avm/opcodes/memory.d.ts +2 -2
- package/dest/public/avm/opcodes/memory.d.ts.map +1 -1
- package/dest/public/avm/opcodes/misc.d.ts +1 -1
- package/dest/public/avm/opcodes/misc.d.ts.map +1 -1
- package/dest/public/avm/opcodes/misc.js +4 -4
- package/dest/public/avm/opcodes/storage.d.ts +3 -3
- package/dest/public/avm/opcodes/storage.d.ts.map +1 -1
- package/dest/public/avm/revert_reason.d.ts +1 -1
- package/dest/public/avm/serialization/buffer_cursor.d.ts +1 -1
- package/dest/public/avm/serialization/buffer_cursor.d.ts.map +1 -1
- package/dest/public/avm/serialization/bytecode_serialization.d.ts +1 -1
- package/dest/public/avm/serialization/instruction_serialization.d.ts +1 -1
- package/dest/public/avm/test_utils.d.ts +1 -1
- package/dest/public/contracts_db_checkpoint.d.ts +16 -0
- package/dest/public/contracts_db_checkpoint.d.ts.map +1 -0
- package/dest/public/contracts_db_checkpoint.js +30 -0
- package/dest/public/db_interfaces.d.ts +13 -1
- package/dest/public/db_interfaces.d.ts.map +1 -1
- package/dest/public/debug_fn_name.d.ts +1 -1
- package/dest/public/executor_metrics.d.ts +3 -2
- package/dest/public/executor_metrics.d.ts.map +1 -1
- package/dest/public/executor_metrics.js +1 -1
- package/dest/public/executor_metrics_interface.d.ts +3 -2
- package/dest/public/executor_metrics_interface.d.ts.map +1 -1
- package/dest/public/fixtures/amm_test.d.ts +1 -1
- package/dest/public/fixtures/bulk_test.d.ts +1 -1
- package/dest/public/fixtures/custom_bytecode_tester.d.ts +2 -2
- package/dest/public/fixtures/custom_bytecode_tester.d.ts.map +1 -1
- package/dest/public/fixtures/custom_bytecode_tests.d.ts +1 -1
- package/dest/public/fixtures/index.d.ts +1 -1
- package/dest/public/fixtures/minimal_public_tx.d.ts +2 -3
- package/dest/public/fixtures/minimal_public_tx.d.ts.map +1 -1
- package/dest/public/fixtures/minimal_public_tx.js +2 -2
- package/dest/public/fixtures/public_tx_simulation_tester.d.ts +4 -5
- package/dest/public/fixtures/public_tx_simulation_tester.d.ts.map +1 -1
- package/dest/public/fixtures/public_tx_simulation_tester.js +15 -11
- package/dest/public/fixtures/simple_contract_data_source.d.ts +1 -1
- package/dest/public/fixtures/simple_contract_data_source.d.ts.map +1 -1
- package/dest/public/fixtures/token_test.d.ts +1 -1
- package/dest/public/fixtures/token_test.js +3 -4
- package/dest/public/fixtures/utils.d.ts +1 -1
- package/dest/public/hinting_db_sources.d.ts +10 -2
- package/dest/public/hinting_db_sources.d.ts.map +1 -1
- package/dest/public/hinting_db_sources.js +64 -11
- package/dest/public/index.d.ts +3 -2
- package/dest/public/index.d.ts.map +1 -1
- package/dest/public/public_db_sources.d.ts +21 -62
- package/dest/public/public_db_sources.d.ts.map +1 -1
- package/dest/public/public_db_sources.js +73 -130
- package/dest/public/public_errors.d.ts +1 -1
- package/dest/public/public_errors.d.ts.map +1 -1
- package/dest/public/public_processor/guarded_merkle_tree.d.ts +3 -3
- package/dest/public/public_processor/guarded_merkle_tree.d.ts.map +1 -1
- package/dest/public/public_processor/public_processor.d.ts +7 -14
- package/dest/public/public_processor/public_processor.d.ts.map +1 -1
- package/dest/public/public_processor/public_processor.js +27 -29
- package/dest/public/public_processor/public_processor_metrics.d.ts +1 -1
- package/dest/public/public_processor/public_processor_metrics.d.ts.map +1 -1
- package/dest/public/public_tx_simulator/contract_provider_for_cpp.d.ts +30 -0
- package/dest/public/public_tx_simulator/contract_provider_for_cpp.d.ts.map +1 -0
- package/dest/public/public_tx_simulator/contract_provider_for_cpp.js +133 -0
- package/dest/public/public_tx_simulator/cpp_public_tx_simulator.d.ts +29 -4
- package/dest/public/public_tx_simulator/cpp_public_tx_simulator.d.ts.map +1 -1
- package/dest/public/public_tx_simulator/cpp_public_tx_simulator.js +138 -17
- package/dest/public/public_tx_simulator/index.d.ts +2 -1
- package/dest/public/public_tx_simulator/index.d.ts.map +1 -1
- package/dest/public/public_tx_simulator/measured_public_tx_simulator.d.ts +13 -6
- package/dest/public/public_tx_simulator/measured_public_tx_simulator.d.ts.map +1 -1
- package/dest/public/public_tx_simulator/measured_public_tx_simulator.js +5 -5
- package/dest/public/public_tx_simulator/public_tx_context.d.ts +5 -2
- package/dest/public/public_tx_simulator/public_tx_context.d.ts.map +1 -1
- package/dest/public/public_tx_simulator/public_tx_context.js +12 -4
- package/dest/public/public_tx_simulator/public_tx_simulator.d.ts +10 -30
- package/dest/public/public_tx_simulator/public_tx_simulator.d.ts.map +1 -1
- package/dest/public/public_tx_simulator/public_tx_simulator.js +102 -125
- package/dest/public/public_tx_simulator/public_tx_simulator_interface.d.ts +2 -2
- package/dest/public/public_tx_simulator/public_tx_simulator_interface.d.ts.map +1 -1
- package/dest/public/public_tx_simulator/telemetry_public_tx_simulator.d.ts +3 -3
- package/dest/public/public_tx_simulator/telemetry_public_tx_simulator.d.ts.map +1 -1
- package/dest/public/side_effect_errors.d.ts +1 -1
- package/dest/public/side_effect_errors.d.ts.map +1 -1
- package/dest/public/side_effect_trace.d.ts +1 -2
- package/dest/public/side_effect_trace.d.ts.map +1 -1
- package/dest/public/side_effect_trace_interface.d.ts +2 -3
- package/dest/public/side_effect_trace_interface.d.ts.map +1 -1
- package/dest/public/state_manager/index.d.ts +1 -1
- package/dest/public/state_manager/nullifiers.d.ts +1 -1
- package/dest/public/state_manager/nullifiers.d.ts.map +1 -1
- package/dest/public/state_manager/public_storage.d.ts +1 -1
- package/dest/public/state_manager/public_storage.d.ts.map +1 -1
- package/dest/public/state_manager/state_manager.d.ts +3 -10
- package/dest/public/state_manager/state_manager.d.ts.map +1 -1
- package/dest/public/state_manager/state_manager.js +5 -4
- package/dest/public/test_executor_metrics.d.ts +4 -3
- package/dest/public/test_executor_metrics.d.ts.map +1 -1
- package/dest/public/test_executor_metrics.js +5 -4
- package/dest/public/unique_class_ids.d.ts +1 -1
- package/dest/public/unique_class_ids.d.ts.map +1 -1
- package/dest/public/utils.d.ts +1 -1
- package/dest/server.d.ts +1 -1
- package/dest/testing.d.ts +1 -1
- package/package.json +21 -20
- package/src/public/avm/avm_execution_environment.ts +3 -5
- package/src/public/avm/avm_simulator.ts +3 -4
- package/src/public/avm/fixtures/avm_simulation_tester.ts +9 -1
- package/src/public/avm/fixtures/initializers.ts +6 -8
- package/src/public/avm/opcodes/misc.ts +4 -4
- package/src/public/contracts_db_checkpoint.ts +41 -0
- package/src/public/db_interfaces.ts +15 -0
- package/src/public/executor_metrics.ts +2 -1
- package/src/public/executor_metrics_interface.ts +2 -1
- package/src/public/fixtures/custom_bytecode_tester.ts +1 -1
- package/src/public/fixtures/minimal_public_tx.ts +3 -4
- package/src/public/fixtures/public_tx_simulation_tester.ts +18 -10
- package/src/public/fixtures/token_test.ts +3 -4
- package/src/public/hinting_db_sources.ts +104 -7
- package/src/public/index.ts +2 -6
- package/src/public/public_db_sources.ts +92 -171
- package/src/public/public_processor/guarded_merkle_tree.ts +2 -2
- package/src/public/public_processor/public_processor.ts +46 -36
- package/src/public/public_tx_simulator/contract_provider_for_cpp.ts +162 -0
- package/src/public/public_tx_simulator/cpp_public_tx_simulator.ts +199 -21
- package/src/public/public_tx_simulator/index.ts +1 -0
- package/src/public/public_tx_simulator/measured_public_tx_simulator.ts +9 -13
- package/src/public/public_tx_simulator/public_tx_context.ts +8 -2
- package/src/public/public_tx_simulator/public_tx_simulator.ts +145 -192
- package/src/public/public_tx_simulator/public_tx_simulator_interface.ts +1 -2
- package/src/public/public_tx_simulator/telemetry_public_tx_simulator.ts +2 -2
- package/src/public/state_manager/state_manager.ts +4 -11
- package/src/public/test_executor_metrics.ts +7 -5
- package/dest/public/tx_contract_cache.d.ts +0 -41
- package/dest/public/tx_contract_cache.d.ts.map +0 -1
- package/dest/public/tx_contract_cache.js +0 -49
- package/src/public/tx_contract_cache.ts +0 -69
|
@@ -7,7 +7,14 @@ import { DateProvider, Timer, elapsed, executeTimeout } from '@aztec/foundation/
|
|
|
7
7
|
import { ProtocolContractAddress } from '@aztec/protocol-contracts';
|
|
8
8
|
import { ContractClassPublishedEvent } from '@aztec/protocol-contracts/class-registry';
|
|
9
9
|
import { computeFeePayerBalanceLeafSlot, computeFeePayerBalanceStorageSlot } from '@aztec/protocol-contracts/fee-juice';
|
|
10
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
AvmCircuitInputs,
|
|
12
|
+
AvmCircuitPublicInputs,
|
|
13
|
+
AvmExecutionHints,
|
|
14
|
+
type AvmProvingRequest,
|
|
15
|
+
PublicDataWrite,
|
|
16
|
+
PublicSimulatorConfig,
|
|
17
|
+
} from '@aztec/stdlib/avm';
|
|
11
18
|
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
12
19
|
import type { ContractDataSource } from '@aztec/stdlib/contract';
|
|
13
20
|
import { computeTransactionFee } from '@aztec/stdlib/fees';
|
|
@@ -18,6 +25,7 @@ import type {
|
|
|
18
25
|
PublicProcessorValidator,
|
|
19
26
|
SequencerConfig,
|
|
20
27
|
} from '@aztec/stdlib/interfaces/server';
|
|
28
|
+
import { ProvingRequestType } from '@aztec/stdlib/proofs';
|
|
21
29
|
import { MerkleTreeId } from '@aztec/stdlib/trees';
|
|
22
30
|
import {
|
|
23
31
|
type FailedTx,
|
|
@@ -26,7 +34,6 @@ import {
|
|
|
26
34
|
type ProcessedTx,
|
|
27
35
|
StateReference,
|
|
28
36
|
Tx,
|
|
29
|
-
TxExecutionPhase,
|
|
30
37
|
makeProcessedTxFromPrivateOnlyTx,
|
|
31
38
|
makeProcessedTxFromTxWithPublicCalls,
|
|
32
39
|
} from '@aztec/stdlib/tx';
|
|
@@ -70,23 +77,12 @@ export class PublicProcessorFactory {
|
|
|
70
77
|
public create(
|
|
71
78
|
merkleTree: MerkleTreeWriteOperations,
|
|
72
79
|
globalVariables: GlobalVariables,
|
|
73
|
-
config:
|
|
74
|
-
skipFeeEnforcement: boolean;
|
|
75
|
-
clientInitiatedSimulation: boolean;
|
|
76
|
-
proverId?: Fr;
|
|
77
|
-
maxDebugLogMemoryReads?: number;
|
|
78
|
-
},
|
|
80
|
+
config: PublicSimulatorConfig,
|
|
79
81
|
): PublicProcessor {
|
|
80
82
|
const contractsDB = new PublicContractsDB(this.contractDataSource);
|
|
81
83
|
|
|
82
84
|
const guardedFork = new GuardedMerkleTreeOperations(merkleTree);
|
|
83
|
-
const publicTxSimulator = this.createPublicTxSimulator(guardedFork, contractsDB, globalVariables,
|
|
84
|
-
proverId: config.proverId,
|
|
85
|
-
doMerkleOperations: true,
|
|
86
|
-
skipFeeEnforcement: config.skipFeeEnforcement,
|
|
87
|
-
clientInitiatedSimulation: config.clientInitiatedSimulation,
|
|
88
|
-
maxDebugLogMemoryReads: config.maxDebugLogMemoryReads,
|
|
89
|
-
});
|
|
85
|
+
const publicTxSimulator = this.createPublicTxSimulator(guardedFork, contractsDB, globalVariables, config);
|
|
90
86
|
|
|
91
87
|
return new PublicProcessor(
|
|
92
88
|
globalVariables,
|
|
@@ -232,12 +228,15 @@ export class PublicProcessor implements Traceable {
|
|
|
232
228
|
// Note: We use the underlying fork here not the guarded one, this ensures that it's not impacted by stopping the guarded version
|
|
233
229
|
const checkpoint = await ForkCheckpoint.new(this.guardedMerkleTree.getUnderlyingFork());
|
|
234
230
|
const startStateReference = await this.guardedMerkleTree.getUnderlyingFork().getStateReference();
|
|
231
|
+
this.contractsDB.createCheckpoint();
|
|
235
232
|
|
|
236
233
|
try {
|
|
237
234
|
const [processedTx, returnValues] = await this.processTx(tx, deadline);
|
|
238
235
|
|
|
236
|
+
const txBlobFields = processedTx.txEffect.getNumBlobFields();
|
|
237
|
+
|
|
239
238
|
// If the actual size of this tx would exceed block size, skip it
|
|
240
|
-
const txSize =
|
|
239
|
+
const txSize = txBlobFields * Fr.SIZE_IN_BYTES;
|
|
241
240
|
if (maxBlockSize !== undefined && totalSizeInBytes + txSize > maxBlockSize) {
|
|
242
241
|
this.log.debug(`Skipping processed tx ${txHash} sized ${txSize} due to max block size.`, {
|
|
243
242
|
txHash,
|
|
@@ -247,11 +246,11 @@ export class PublicProcessor implements Traceable {
|
|
|
247
246
|
});
|
|
248
247
|
// Need to revert the checkpoint here and don't go any further
|
|
249
248
|
await checkpoint.revert();
|
|
249
|
+
this.contractsDB.revertCheckpoint();
|
|
250
250
|
continue;
|
|
251
251
|
}
|
|
252
252
|
|
|
253
253
|
// If the actual blob fields of this tx would exceed the limit, skip it
|
|
254
|
-
const txBlobFields = processedTx.txEffect.toBlobFields().length;
|
|
255
254
|
if (maxBlobFields !== undefined && totalBlobFields + txBlobFields > maxBlobFields) {
|
|
256
255
|
this.log.debug(
|
|
257
256
|
`Skipping processed tx ${txHash} with ${txBlobFields} blob fields due to max blob fields limit.`,
|
|
@@ -264,6 +263,7 @@ export class PublicProcessor implements Traceable {
|
|
|
264
263
|
);
|
|
265
264
|
// Need to revert the checkpoint here and don't go any further
|
|
266
265
|
await checkpoint.revert();
|
|
266
|
+
this.contractsDB.revertCheckpoint();
|
|
267
267
|
continue;
|
|
268
268
|
}
|
|
269
269
|
|
|
@@ -298,6 +298,9 @@ export class PublicProcessor implements Traceable {
|
|
|
298
298
|
// This needs to be done directly on the underlying fork as the guarded fork has been stopped.
|
|
299
299
|
await this.guardedMerkleTree.getUnderlyingFork().revertAllCheckpoints();
|
|
300
300
|
|
|
301
|
+
// Revert any contracts added to the DB for the tx.
|
|
302
|
+
this.contractsDB.revertCheckpoint();
|
|
303
|
+
|
|
301
304
|
// Ensure we're at the same state as when we started processing this tx.
|
|
302
305
|
await this.checkWorldStateUnchanged(startStateReference, txHash, err);
|
|
303
306
|
|
|
@@ -308,6 +311,7 @@ export class PublicProcessor implements Traceable {
|
|
|
308
311
|
// Roll back state to start of TX before proceeding to next TX
|
|
309
312
|
await checkpoint.revert();
|
|
310
313
|
await this.guardedMerkleTree.getUnderlyingFork().revertAllCheckpoints();
|
|
314
|
+
this.contractsDB.revertCheckpoint();
|
|
311
315
|
const errorMessage = err instanceof Error || err instanceof AssertionError ? err.message : 'Unknown error';
|
|
312
316
|
this.log.warn(`Failed to process tx ${txHash.toString()}: ${errorMessage} ${err?.stack}`);
|
|
313
317
|
failed.push({ tx, error: err instanceof Error ? err : new Error(errorMessage) });
|
|
@@ -318,8 +322,7 @@ export class PublicProcessor implements Traceable {
|
|
|
318
322
|
} finally {
|
|
319
323
|
// Base case is we always commit the checkpoint. Using the ForkCheckpoint means this has no effect if the tx was previously reverted
|
|
320
324
|
await checkpoint.commit();
|
|
321
|
-
|
|
322
|
-
this.contractsDB.clearContractsForTx();
|
|
325
|
+
this.contractsDB.commitCheckpointOkIfNone();
|
|
323
326
|
}
|
|
324
327
|
}
|
|
325
328
|
|
|
@@ -507,10 +510,7 @@ export class PublicProcessor implements Traceable {
|
|
|
507
510
|
// Fee payment insertion has already been done. Do the rest.
|
|
508
511
|
await this.doTreeInsertionsForPrivateOnlyTx(processedTx);
|
|
509
512
|
|
|
510
|
-
// Add any contracts registered/deployed in this private-only tx to the block-level cache
|
|
511
|
-
// (add to tx-level cache and then commit to block-level cache)
|
|
512
513
|
await this.contractsDB.addNewContracts(tx);
|
|
513
|
-
this.contractsDB.commitContractsForTx();
|
|
514
514
|
|
|
515
515
|
return [processedTx, undefined];
|
|
516
516
|
}
|
|
@@ -521,22 +521,14 @@ export class PublicProcessor implements Traceable {
|
|
|
521
521
|
private async processTxWithPublicCalls(tx: Tx): Promise<[ProcessedTx, NestedProcessReturnValues[]]> {
|
|
522
522
|
const timer = new Timer();
|
|
523
523
|
|
|
524
|
-
const {
|
|
524
|
+
const { hints, publicInputs, gasUsed, revertCode, revertReason, appLogicReturnValues } =
|
|
525
525
|
await this.publicTxSimulator.simulate(tx);
|
|
526
526
|
|
|
527
|
-
if (!
|
|
527
|
+
if (!hints) {
|
|
528
528
|
this.metrics.recordFailedTx();
|
|
529
529
|
throw new Error('Avm proving result was not generated.');
|
|
530
530
|
}
|
|
531
531
|
|
|
532
|
-
processedPhases.forEach(phase => {
|
|
533
|
-
if (phase.reverted) {
|
|
534
|
-
this.metrics.recordRevertedPhase(phase.phase);
|
|
535
|
-
} else {
|
|
536
|
-
this.metrics.recordPhaseDuration(phase.phase, phase.durationMs ?? 0);
|
|
537
|
-
}
|
|
538
|
-
});
|
|
539
|
-
|
|
540
532
|
const contractClassLogs = revertCode.isOK()
|
|
541
533
|
? tx.getContractClassLogs()
|
|
542
534
|
: tx.getSplitContractClassLogs(false /* revertible */);
|
|
@@ -546,14 +538,32 @@ export class PublicProcessor implements Traceable {
|
|
|
546
538
|
.map(log => ContractClassPublishedEvent.fromLog(log)),
|
|
547
539
|
);
|
|
548
540
|
|
|
549
|
-
|
|
541
|
+
// TODO(fcarreiro): remove phase count metric.
|
|
542
|
+
const phaseCount = 1;
|
|
550
543
|
const durationMs = timer.ms();
|
|
551
544
|
this.metrics.recordTx(phaseCount, durationMs, gasUsed.publicGas);
|
|
552
545
|
|
|
553
|
-
const processedTx = makeProcessedTxFromTxWithPublicCalls(
|
|
546
|
+
const processedTx = makeProcessedTxFromTxWithPublicCalls(
|
|
547
|
+
tx,
|
|
548
|
+
PublicProcessor.generateProvingRequest(publicInputs, hints),
|
|
549
|
+
gasUsed,
|
|
550
|
+
revertCode,
|
|
551
|
+
revertReason,
|
|
552
|
+
);
|
|
554
553
|
|
|
555
|
-
|
|
554
|
+
return [processedTx, appLogicReturnValues ?? []];
|
|
555
|
+
}
|
|
556
556
|
|
|
557
|
-
|
|
557
|
+
/**
|
|
558
|
+
* Generate the proving request for the AVM circuit.
|
|
559
|
+
*/
|
|
560
|
+
private static generateProvingRequest(
|
|
561
|
+
publicInputs: AvmCircuitPublicInputs,
|
|
562
|
+
hints: AvmExecutionHints,
|
|
563
|
+
): AvmProvingRequest {
|
|
564
|
+
return {
|
|
565
|
+
type: ProvingRequestType.PUBLIC_VM,
|
|
566
|
+
inputs: new AvmCircuitInputs(hints, publicInputs),
|
|
567
|
+
};
|
|
558
568
|
}
|
|
559
569
|
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { Fr } from '@aztec/foundation/fields';
|
|
2
|
+
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
3
|
+
import type { ContractProvider } from '@aztec/native';
|
|
4
|
+
import { FunctionSelector } from '@aztec/stdlib/abi';
|
|
5
|
+
import { deserializeFromMessagePack, serializeWithMessagePack } from '@aztec/stdlib/avm';
|
|
6
|
+
import { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
7
|
+
import { ContractDeploymentData } from '@aztec/stdlib/contract';
|
|
8
|
+
import { ContractClassLog, ContractClassLogFields, PrivateLog } from '@aztec/stdlib/logs';
|
|
9
|
+
import type { GlobalVariables } from '@aztec/stdlib/tx';
|
|
10
|
+
|
|
11
|
+
import type { PublicContractsDB } from '../public_db_sources.js';
|
|
12
|
+
|
|
13
|
+
export class ContractProviderForCpp implements ContractProvider {
|
|
14
|
+
private log: Logger = createLogger('simulator:contract_provider_for_cpp');
|
|
15
|
+
|
|
16
|
+
constructor(
|
|
17
|
+
private contractsDB: PublicContractsDB,
|
|
18
|
+
private globalVariables: GlobalVariables,
|
|
19
|
+
) {}
|
|
20
|
+
|
|
21
|
+
public getContractInstance = async (address: string): Promise<Buffer | undefined> => {
|
|
22
|
+
this.log.debug(`Contract provider callback: getContractInstance(${address})`);
|
|
23
|
+
|
|
24
|
+
const aztecAddr = AztecAddress.fromString(address);
|
|
25
|
+
|
|
26
|
+
const instance = await this.contractsDB.getContractInstance(aztecAddr, this.globalVariables.timestamp);
|
|
27
|
+
|
|
28
|
+
if (!instance) {
|
|
29
|
+
this.log.debug(`Contract instance not found: ${address}`);
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return serializeWithMessagePack(instance);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
public getContractClass = async (classId: string): Promise<Buffer | undefined> => {
|
|
37
|
+
this.log.debug(`Contract provider callback: getContractClass(${classId})`);
|
|
38
|
+
|
|
39
|
+
// Parse classId string to Fr
|
|
40
|
+
const classIdFr = Fr.fromString(classId);
|
|
41
|
+
|
|
42
|
+
// Fetch contract class from the contracts DB
|
|
43
|
+
const contractClass = await this.contractsDB.getContractClass(classIdFr);
|
|
44
|
+
|
|
45
|
+
if (!contractClass) {
|
|
46
|
+
this.log.debug(`Contract class not found: ${classId}`);
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return serializeWithMessagePack(contractClass);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
public addContracts = async (contractDeploymentDataBuffer: Buffer): Promise<void> => {
|
|
54
|
+
this.log.debug(`Contract provider callback: addContracts`);
|
|
55
|
+
|
|
56
|
+
const rawData: any = deserializeFromMessagePack(contractDeploymentDataBuffer);
|
|
57
|
+
|
|
58
|
+
// Construct class instances using the from method
|
|
59
|
+
const contractDeploymentData = this.reconstructContractDeploymentData(rawData);
|
|
60
|
+
|
|
61
|
+
// Add contracts to the contracts DB
|
|
62
|
+
this.log.debug(`Calling contractsDB.addContracts`);
|
|
63
|
+
await this.contractsDB.addContracts(contractDeploymentData);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
public getBytecodeCommitment = async (classId: string): Promise<Buffer | undefined> => {
|
|
67
|
+
this.log.debug(`Contract provider callback: getBytecodeCommitment(${classId})`);
|
|
68
|
+
|
|
69
|
+
// Parse classId string to Fr
|
|
70
|
+
const classIdFr = Fr.fromString(classId);
|
|
71
|
+
|
|
72
|
+
// Fetch bytecode commitment from the contracts DB
|
|
73
|
+
const commitment = await this.contractsDB.getBytecodeCommitment(classIdFr);
|
|
74
|
+
|
|
75
|
+
if (!commitment) {
|
|
76
|
+
this.log.debug(`Bytecode commitment not found: ${classId}`);
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Serialize the Fr to buffer
|
|
81
|
+
return serializeWithMessagePack(commitment);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
public getDebugFunctionName = async (address: string, selector: string): Promise<string | undefined> => {
|
|
85
|
+
this.log.debug(`Contract provider callback: getDebugFunctionName(${address}, ${selector})`);
|
|
86
|
+
|
|
87
|
+
// Parse address and selector strings
|
|
88
|
+
const aztecAddr = AztecAddress.fromString(address);
|
|
89
|
+
const selectorFr = Fr.fromString(selector);
|
|
90
|
+
const functionSelector = FunctionSelector.fromField(selectorFr);
|
|
91
|
+
|
|
92
|
+
// Fetch debug function name from the contracts DB
|
|
93
|
+
const name = await this.contractsDB.getDebugFunctionName(aztecAddr, functionSelector);
|
|
94
|
+
|
|
95
|
+
if (!name) {
|
|
96
|
+
this.log.debug(`Debug function name not found for ${address}:${selector}`);
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return name;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
public createCheckpoint = (): Promise<void> => {
|
|
104
|
+
this.log.debug(`Contract provider callback: createCheckpoint`);
|
|
105
|
+
return Promise.resolve(this.contractsDB.createCheckpoint());
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
public commitCheckpoint = (): Promise<void> => {
|
|
109
|
+
this.log.debug(`Contract provider callback: commitCheckpoint`);
|
|
110
|
+
return Promise.resolve(this.contractsDB.commitCheckpoint());
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
public revertCheckpoint = (): Promise<void> => {
|
|
114
|
+
this.log.debug(`Contract provider callback: revertCheckpoint`);
|
|
115
|
+
return Promise.resolve(this.contractsDB.revertCheckpoint());
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Reconstruct ContractDeploymentData from plain msgpack-deserialized objects.
|
|
120
|
+
*
|
|
121
|
+
* msgpackr does not automatically apply extensions to nested fields, so we need to
|
|
122
|
+
* manually reconstruct ContractClassLog and PrivateLog instances with proper types.
|
|
123
|
+
*
|
|
124
|
+
* TODO(dbanks12): we really shouldn't have to do this.... We need to for now because
|
|
125
|
+
* msgpack deserialization doesn't give us actual typed objects, but rather just JSON.
|
|
126
|
+
* It would be easier if all types matched between languages (like AztecAddress which is just
|
|
127
|
+
* FF in C++).
|
|
128
|
+
*/
|
|
129
|
+
private reconstructContractDeploymentData(rawData: any): ContractDeploymentData {
|
|
130
|
+
// Helper to ensure a value is an Fr instance
|
|
131
|
+
const toFr = (value: any): Fr => {
|
|
132
|
+
if (value instanceof Fr) {
|
|
133
|
+
return value;
|
|
134
|
+
}
|
|
135
|
+
if (Buffer.isBuffer(value)) {
|
|
136
|
+
return Fr.fromBuffer(value);
|
|
137
|
+
}
|
|
138
|
+
return new Fr(value);
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
// Reconstruct ContractClassLogs
|
|
142
|
+
const contractClassLogs = (rawData.contractClassLogs || []).map((log: any) => {
|
|
143
|
+
// Convert contractAddress to TS AztecAddress
|
|
144
|
+
const addressFr = toFr(log.contractAddress);
|
|
145
|
+
const address = AztecAddress.fromField(addressFr);
|
|
146
|
+
|
|
147
|
+
// Ensure all fields are Fr instances
|
|
148
|
+
const fields = (log.fields.fields || []).map((field: any) => toFr(field));
|
|
149
|
+
|
|
150
|
+
// Create proper ContractClassLog instance
|
|
151
|
+
return new ContractClassLog(address, new ContractClassLogFields(fields), log.emittedLength);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
// Reconstruct PrivateLogs - ensure fields are Fr instances
|
|
155
|
+
const privateLogs = (rawData.privateLogs || []).map((log: any) => {
|
|
156
|
+
const fields = (log.fields || []).map((field: any) => toFr(field));
|
|
157
|
+
return new PrivateLog(fields as any, log.emittedLength);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
return new ContractDeploymentData(contractClassLogs, privateLogs);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
@@ -1,18 +1,199 @@
|
|
|
1
1
|
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { writeTestData } from '@aztec/foundation/testing/files';
|
|
3
|
+
import { avmSimulate, avmSimulateWithHintedDbs } from '@aztec/native';
|
|
4
|
+
import { ProtocolContractsList } from '@aztec/protocol-contracts';
|
|
5
|
+
import {
|
|
6
|
+
AvmCircuitInputs,
|
|
7
|
+
AvmFastSimulationInputs,
|
|
8
|
+
AvmTxHint,
|
|
9
|
+
type PublicSimulatorConfig,
|
|
10
|
+
PublicTxResult,
|
|
11
|
+
deserializeFromMessagePack,
|
|
12
|
+
} from '@aztec/stdlib/avm';
|
|
4
13
|
import { SimulationError } from '@aztec/stdlib/errors';
|
|
5
14
|
import type { MerkleTreeWriteOperations } from '@aztec/stdlib/trees';
|
|
6
|
-
import type { GlobalVariables, Tx } from '@aztec/stdlib/tx';
|
|
15
|
+
import type { GlobalVariables, StateReference, Tx } from '@aztec/stdlib/tx';
|
|
16
|
+
import { WorldStateRevisionWithHandle } from '@aztec/stdlib/world-state';
|
|
17
|
+
|
|
18
|
+
import { strict as assert } from 'assert';
|
|
7
19
|
|
|
8
20
|
import type { ExecutorMetricsInterface } from '../executor_metrics_interface.js';
|
|
9
21
|
import type { PublicContractsDB } from '../public_db_sources.js';
|
|
10
|
-
import {
|
|
22
|
+
import { ContractProviderForCpp } from './contract_provider_for_cpp.js';
|
|
23
|
+
import { PublicTxSimulator } from './public_tx_simulator.js';
|
|
11
24
|
import type {
|
|
12
25
|
MeasuredPublicTxSimulatorInterface,
|
|
13
26
|
PublicTxSimulatorInterface,
|
|
14
27
|
} from './public_tx_simulator_interface.js';
|
|
15
28
|
|
|
29
|
+
/**
|
|
30
|
+
* C++ implementation of PublicTxSimulator using the C++ simulator.
|
|
31
|
+
* The C++ simulator accesses the world state directly/natively within C++.
|
|
32
|
+
* For contract DB accesses, it makes callbacks through NAPI back to the TS PublicContractsDB cache.
|
|
33
|
+
*
|
|
34
|
+
* TODO(dbanks12): for now this still simulates first in TS to generate hints and public inputs,
|
|
35
|
+
* since the C++ simulator doesn't have hinting & PI generation logic yet.
|
|
36
|
+
*/
|
|
37
|
+
export class CppPublicTxSimulator extends PublicTxSimulator implements PublicTxSimulatorInterface {
|
|
38
|
+
protected override log: Logger;
|
|
39
|
+
|
|
40
|
+
constructor(
|
|
41
|
+
merkleTree: MerkleTreeWriteOperations,
|
|
42
|
+
contractsDB: PublicContractsDB,
|
|
43
|
+
globalVariables: GlobalVariables,
|
|
44
|
+
config?: Partial<PublicSimulatorConfig>,
|
|
45
|
+
) {
|
|
46
|
+
super(merkleTree, contractsDB, globalVariables, config);
|
|
47
|
+
this.log = createLogger(`simulator:cpp_public_tx_simulator`);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Simulate a transaction's public portion using the C++ avvm simulator.
|
|
52
|
+
*
|
|
53
|
+
* @param tx - The transaction to simulate.
|
|
54
|
+
* @returns The result of the transaction's public execution.
|
|
55
|
+
*/
|
|
56
|
+
public override async simulate(tx: Tx): Promise<PublicTxResult> {
|
|
57
|
+
const txHash = this.computeTxHash(tx);
|
|
58
|
+
this.log.debug(`C++ simulation of ${tx.publicFunctionCalldata.length} public calls for tx ${txHash}`, {
|
|
59
|
+
txHash,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// TODO(dbanks12): remove the first run with TS that hints!
|
|
63
|
+
// Run TS simulation to generate hints and public inputs
|
|
64
|
+
this.log.debug(`Running TS simulation for tx ${txHash}`);
|
|
65
|
+
|
|
66
|
+
// create checkpoint for ws
|
|
67
|
+
let tsResult: PublicTxResult | undefined;
|
|
68
|
+
let tsStateRef: StateReference | undefined;
|
|
69
|
+
await this.merkleTree.createCheckpoint();
|
|
70
|
+
this.contractsDB.createCheckpoint();
|
|
71
|
+
try {
|
|
72
|
+
// Run the full TypeScript simulation using the parent class
|
|
73
|
+
// This will modify the merkle tree with the transaction's state changes
|
|
74
|
+
tsResult = await super.simulate(tx);
|
|
75
|
+
this.log.debug(`TS simulation completed for tx ${txHash}`);
|
|
76
|
+
|
|
77
|
+
tsStateRef = await this.merkleTree.getStateReference(); // capture tree roots for later comparsion
|
|
78
|
+
} catch (error: any) {
|
|
79
|
+
this.log.warn(`TS simulation failed, but still continuing with C++ simulation: ${error.message} ${error.stack}`);
|
|
80
|
+
} finally {
|
|
81
|
+
// revert checkpoint for ws and clear contract db changes
|
|
82
|
+
// (cpp should reapply exactly the same changes if there are no bugs)
|
|
83
|
+
await this.merkleTree.revertCheckpoint();
|
|
84
|
+
this.contractsDB.revertCheckpoint();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
this.log.debug(`Running C++ simulation for tx ${txHash}`);
|
|
88
|
+
|
|
89
|
+
// Using the "as WorldStateRevisionWithHandle" is a bit of a "trust me bro", hence the assert.
|
|
90
|
+
let wsRevision = this.merkleTree.getRevision();
|
|
91
|
+
assert(
|
|
92
|
+
wsRevision instanceof WorldStateRevisionWithHandle,
|
|
93
|
+
'CppPublicTxSimulator a real NativeWorldStateInstance with a handle to the C++ WorldState object',
|
|
94
|
+
);
|
|
95
|
+
const wsCppHandle = (wsRevision as WorldStateRevisionWithHandle).handle;
|
|
96
|
+
wsRevision = wsRevision.toWorldStateRevision(); // for msgpack serialization, we don't include the handle in the type
|
|
97
|
+
|
|
98
|
+
this.log.debug(`Running C++ simulation with world state revision ${JSON.stringify(wsRevision)}`);
|
|
99
|
+
|
|
100
|
+
// Create the fast simulation inputs
|
|
101
|
+
const txHint = AvmTxHint.fromTx(tx, this.globalVariables.gasFees);
|
|
102
|
+
const protocolContracts = ProtocolContractsList;
|
|
103
|
+
const fastSimInputs = new AvmFastSimulationInputs(
|
|
104
|
+
wsRevision,
|
|
105
|
+
this.config,
|
|
106
|
+
txHint,
|
|
107
|
+
this.globalVariables,
|
|
108
|
+
protocolContracts,
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
// Create contract provider for callbacks to TypeScript PublicContractsDB from C++
|
|
112
|
+
const contractProvider = new ContractProviderForCpp(this.contractsDB, this.globalVariables);
|
|
113
|
+
|
|
114
|
+
// Serialize to msgpack and call the C++ simulator
|
|
115
|
+
this.log.verbose(`Serializing fast simulation inputs to msgpack...`);
|
|
116
|
+
const inputBuffer = fastSimInputs.serializeWithMessagePack();
|
|
117
|
+
|
|
118
|
+
let resultBuffer: Buffer;
|
|
119
|
+
try {
|
|
120
|
+
this.log.verbose(`Calling C++ simulator for tx ${txHash}`);
|
|
121
|
+
resultBuffer = await avmSimulate(inputBuffer, contractProvider, wsCppHandle);
|
|
122
|
+
} catch (error: any) {
|
|
123
|
+
throw new SimulationError(`C++ simulation failed: ${error.message}`, []);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// If we've reached this point, C++ succeeded during simulation,
|
|
127
|
+
// so we assert that TS also succeeded.
|
|
128
|
+
assert(tsResult !== undefined, 'TS simulation should have succeeded if C++ succeeded');
|
|
129
|
+
assert(tsStateRef !== undefined, 'TS state reference should have been captured if C++ succeeded');
|
|
130
|
+
|
|
131
|
+
// Deserialize the msgpack result
|
|
132
|
+
this.log.verbose(`Deserializing C++ from buffer (size: ${resultBuffer.length})...`);
|
|
133
|
+
const cppResultJSON: object = deserializeFromMessagePack(resultBuffer);
|
|
134
|
+
// Write testdata if AZTEC_WRITE_TESTDATA=1.
|
|
135
|
+
writeTestData(
|
|
136
|
+
`barretenberg/cpp/src/barretenberg/vm2/testing/tx_result_${txHash}.testdata.bin`,
|
|
137
|
+
resultBuffer,
|
|
138
|
+
/*raw=*/ true,
|
|
139
|
+
);
|
|
140
|
+
this.log.verbose(`Deserializing C++ result to PublicTxResult...`);
|
|
141
|
+
const cppResult = PublicTxResult.fromPlainObject(cppResultJSON);
|
|
142
|
+
this.log.verbose(`Done.`);
|
|
143
|
+
// TODO(fcarreiro): complete this.
|
|
144
|
+
assert(cppResult.revertCode.equals(tsResult.revertCode));
|
|
145
|
+
assert(cppResult.gasUsed.totalGas.equals(tsResult.gasUsed.totalGas));
|
|
146
|
+
assert(cppResult.gasUsed.publicGas.equals(tsResult.gasUsed.publicGas));
|
|
147
|
+
assert(cppResult.gasUsed.teardownGas.equals(tsResult.gasUsed.teardownGas));
|
|
148
|
+
assert(cppResult.gasUsed.billedGas.equals(tsResult.gasUsed.billedGas));
|
|
149
|
+
assert(cppResult.publicInputs.toBuffer().equals(tsResult.publicInputs.toBuffer()));
|
|
150
|
+
if (this.config?.collectCallMetadata) {
|
|
151
|
+
assert(cppResult.appLogicReturnValues.length == tsResult.appLogicReturnValues.length);
|
|
152
|
+
assert(cppResult.appLogicReturnValues.every((v, i) => v.equals(tsResult.appLogicReturnValues[i])));
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Confirm that tree roots match
|
|
156
|
+
const cppStateRef = await this.merkleTree.getStateReference();
|
|
157
|
+
assert(
|
|
158
|
+
cppStateRef.equals(tsStateRef),
|
|
159
|
+
`Tree roots mismatch between TS and C++ public simulations for tx ${txHash}`,
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
this.log.debug(`C++ simulation completed for tx ${txHash}`, {
|
|
163
|
+
txHash,
|
|
164
|
+
reverted: !tsResult.revertCode.isOK(),
|
|
165
|
+
tsGasUsed: tsResult.gasUsed.totalGas.l2Gas,
|
|
166
|
+
cppGasUsed: tsResult.gasUsed.totalGas.l2Gas,
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
// TODO(dbanks12): C++ should return PublicTxResult (or something similar)
|
|
170
|
+
return tsResult;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export class MeasuredCppPublicTxSimulator extends CppPublicTxSimulator implements MeasuredPublicTxSimulatorInterface {
|
|
175
|
+
constructor(
|
|
176
|
+
merkleTree: MerkleTreeWriteOperations,
|
|
177
|
+
contractsDB: PublicContractsDB,
|
|
178
|
+
globalVariables: GlobalVariables,
|
|
179
|
+
protected readonly metrics: ExecutorMetricsInterface,
|
|
180
|
+
config?: Partial<PublicSimulatorConfig>,
|
|
181
|
+
) {
|
|
182
|
+
super(merkleTree, contractsDB, globalVariables, config);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
public override async simulate(tx: Tx, txLabel: string = 'unlabeledTx'): Promise<PublicTxResult> {
|
|
186
|
+
this.metrics.startRecordingTxSimulation(txLabel);
|
|
187
|
+
let result: PublicTxResult | undefined;
|
|
188
|
+
try {
|
|
189
|
+
result = await super.simulate(tx);
|
|
190
|
+
} finally {
|
|
191
|
+
this.metrics.stopRecordingTxSimulation(txLabel, result?.gasUsed, result?.revertCode);
|
|
192
|
+
}
|
|
193
|
+
return result;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
16
197
|
/**
|
|
17
198
|
* C++ implementation of PublicTxSimulator using pre-collected hints.
|
|
18
199
|
* This implementation runs TS simulation first to collect all hints,
|
|
@@ -26,7 +207,7 @@ export class CppPublicTxSimulatorHintedDbs extends PublicTxSimulator implements
|
|
|
26
207
|
merkleTree: MerkleTreeWriteOperations,
|
|
27
208
|
contractsDB: PublicContractsDB,
|
|
28
209
|
globalVariables: GlobalVariables,
|
|
29
|
-
config?: Partial<
|
|
210
|
+
config?: Partial<PublicSimulatorConfig>,
|
|
30
211
|
) {
|
|
31
212
|
super(merkleTree, contractsDB, globalVariables, config);
|
|
32
213
|
this.log = createLogger(`simulator:cpp_public_tx_simulator_hinted_dbs`);
|
|
@@ -51,20 +232,13 @@ export class CppPublicTxSimulatorHintedDbs extends PublicTxSimulator implements
|
|
|
51
232
|
// First, run TS simulation to generate hints and public inputs
|
|
52
233
|
this.log.debug(`Running TS simulation for tx ${txHash}`);
|
|
53
234
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
tsResult = await super.simulate(tx);
|
|
59
|
-
this.log.debug(`TS simulation succeeded for tx ${txHash}`);
|
|
60
|
-
} catch (error: any) {
|
|
61
|
-
// If TS simulation fails, clear any partial contract additions and re-throw the error
|
|
62
|
-
this.contractsDB.clearContractsForTx();
|
|
63
|
-
throw error;
|
|
64
|
-
}
|
|
235
|
+
// Run the full TypeScript simulation using the parent class
|
|
236
|
+
// This will modify the merkle tree with the transaction's state changes
|
|
237
|
+
const tsResult = await super.simulate(tx);
|
|
238
|
+
this.log.debug(`TS simulation succeeded for tx ${txHash}`);
|
|
65
239
|
|
|
66
240
|
// Extract the full AvmCircuitInputs from the TS result
|
|
67
|
-
const avmCircuitInputs = tsResult.
|
|
241
|
+
const avmCircuitInputs = new AvmCircuitInputs(tsResult.hints!, tsResult.publicInputs);
|
|
68
242
|
|
|
69
243
|
// Second, run C++ simulation with hinted DBs
|
|
70
244
|
this.log.debug(`Running C++ simulation with hinted DBs for tx ${txHash}`);
|
|
@@ -80,7 +254,11 @@ export class CppPublicTxSimulatorHintedDbs extends PublicTxSimulator implements
|
|
|
80
254
|
}
|
|
81
255
|
|
|
82
256
|
// Deserialize the msgpack result
|
|
83
|
-
const
|
|
257
|
+
const cppResultJSON: object = deserializeFromMessagePack(resultBuffer);
|
|
258
|
+
const cppResult = PublicTxResult.fromPlainObject(cppResultJSON);
|
|
259
|
+
|
|
260
|
+
assert(cppResult.revertCode.equals(tsResult.revertCode));
|
|
261
|
+
assert(cppResult.gasUsed.totalGas.equals(tsResult.gasUsed.totalGas));
|
|
84
262
|
|
|
85
263
|
this.log.debug(`C++ hinted simulation completed for tx ${txHash}`, {
|
|
86
264
|
txHash,
|
|
@@ -89,7 +267,7 @@ export class CppPublicTxSimulatorHintedDbs extends PublicTxSimulator implements
|
|
|
89
267
|
cppGasUsed: tsResult.gasUsed.totalGas.l2Gas,
|
|
90
268
|
});
|
|
91
269
|
|
|
92
|
-
// TODO(
|
|
270
|
+
// TODO(fcarreiro): complete this.
|
|
93
271
|
return tsResult;
|
|
94
272
|
}
|
|
95
273
|
}
|
|
@@ -110,7 +288,7 @@ export class MeasuredCppPublicTxSimulatorHintedDbs
|
|
|
110
288
|
contractsDB: PublicContractsDB,
|
|
111
289
|
globalVariables: GlobalVariables,
|
|
112
290
|
protected readonly metrics: ExecutorMetricsInterface,
|
|
113
|
-
config?: Partial<
|
|
291
|
+
config?: Partial<PublicSimulatorConfig>,
|
|
114
292
|
) {
|
|
115
293
|
super(merkleTree, contractsDB, globalVariables, config);
|
|
116
294
|
}
|
|
@@ -121,7 +299,7 @@ export class MeasuredCppPublicTxSimulatorHintedDbs
|
|
|
121
299
|
try {
|
|
122
300
|
result = await super.simulate(tx);
|
|
123
301
|
} finally {
|
|
124
|
-
this.metrics.stopRecordingTxSimulation(txLabel, result?.revertCode);
|
|
302
|
+
this.metrics.stopRecordingTxSimulation(txLabel, result?.gasUsed, result?.revertCode);
|
|
125
303
|
}
|
|
126
304
|
return result;
|
|
127
305
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export * from './public_tx_simulator.js';
|
|
2
2
|
export type { PublicTxSimulatorInterface } from './public_tx_simulator_interface.js';
|
|
3
3
|
export { TelemetryPublicTxSimulator } from './telemetry_public_tx_simulator.js';
|
|
4
|
+
export type { PublicTxResult, PublicSimulatorConfig as PublicTxSimulatorConfig } from '@aztec/stdlib/avm';
|