@aztec/simulator 0.0.1-commit.5daedc8 → 0.0.1-commit.6230efd
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/common/errors.d.ts +2 -2
- package/dest/common/errors.d.ts.map +1 -1
- package/dest/private/acvm/deserialize.d.ts +2 -2
- package/dest/private/acvm/deserialize.d.ts.map +1 -1
- package/dest/private/acvm/deserialize.js +1 -1
- package/dest/private/acvm/serialize.d.ts +2 -2
- package/dest/private/acvm/serialize.d.ts.map +1 -1
- package/dest/private/acvm/serialize.js +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/circuit_recorder.js +16 -15
- package/dest/public/avm/avm_context.d.ts +2 -2
- package/dest/public/avm/avm_context.d.ts.map +1 -1
- package/dest/public/avm/avm_contract_call_result.d.ts +2 -2
- package/dest/public/avm/avm_contract_call_result.d.ts.map +1 -1
- package/dest/public/avm/avm_execution_environment.d.ts +2 -2
- package/dest/public/avm/avm_execution_environment.d.ts.map +1 -1
- package/dest/public/avm/avm_execution_environment.js +1 -1
- package/dest/public/avm/avm_gas.d.ts +1 -1
- package/dest/public/avm/avm_gas.d.ts.map +1 -1
- package/dest/public/avm/avm_machine_state.d.ts +2 -2
- package/dest/public/avm/avm_machine_state.d.ts.map +1 -1
- package/dest/public/avm/avm_memory_types.d.ts +2 -2
- package/dest/public/avm/avm_memory_types.d.ts.map +1 -1
- package/dest/public/avm/avm_memory_types.js +1 -1
- package/dest/public/avm/avm_simulator.d.ts +2 -2
- package/dest/public/avm/avm_simulator.d.ts.map +1 -1
- package/dest/public/avm/avm_simulator.js +6 -5
- package/dest/public/avm/errors.d.ts +8 -2
- package/dest/public/avm/errors.d.ts.map +1 -1
- package/dest/public/avm/errors.js +14 -2
- package/dest/public/avm/fixtures/avm_simulation_tester.js +1 -1
- package/dest/public/avm/fixtures/base_avm_simulation_tester.d.ts +2 -2
- package/dest/public/avm/fixtures/base_avm_simulation_tester.d.ts.map +1 -1
- package/dest/public/avm/fixtures/base_avm_simulation_tester.js +1 -1
- package/dest/public/avm/fixtures/initializers.d.ts +2 -2
- package/dest/public/avm/fixtures/initializers.d.ts.map +1 -1
- package/dest/public/avm/fixtures/initializers.js +3 -3
- package/dest/public/avm/fixtures/utils.d.ts +4 -3
- package/dest/public/avm/fixtures/utils.d.ts.map +1 -1
- package/dest/public/avm/fixtures/utils.js +3 -2
- package/dest/public/avm/opcodes/addressing_mode.js +2 -2
- package/dest/public/avm/opcodes/arithmetic.d.ts +3 -1
- package/dest/public/avm/opcodes/arithmetic.d.ts.map +1 -1
- package/dest/public/avm/opcodes/arithmetic.js +11 -1
- package/dest/public/avm/opcodes/ec_add.js +2 -2
- package/dest/public/avm/opcodes/hashing.d.ts +1 -1
- package/dest/public/avm/opcodes/hashing.d.ts.map +1 -1
- package/dest/public/avm/opcodes/hashing.js +9 -4
- package/dest/public/avm/opcodes/memory.js +1 -1
- package/dest/public/avm/opcodes/misc.js +2 -2
- package/dest/public/avm/revert_reason.d.ts +2 -2
- package/dest/public/avm/revert_reason.d.ts.map +1 -1
- package/dest/public/avm/revert_reason.js +3 -2
- package/dest/public/avm/serialization/bytecode_serialization.d.ts +1 -1
- package/dest/public/avm/serialization/bytecode_serialization.d.ts.map +1 -1
- package/dest/public/avm/serialization/bytecode_serialization.js +9 -6
- package/dest/public/avm/serialization/instruction_serialization.js +1 -1
- package/dest/public/avm/test_utils.d.ts +2 -2
- package/dest/public/avm/test_utils.d.ts.map +1 -1
- package/dest/public/avm/test_utils.js +1 -1
- package/dest/public/contracts_db_checkpoint.d.ts +2 -2
- package/dest/public/contracts_db_checkpoint.d.ts.map +1 -1
- package/dest/public/db_interfaces.d.ts +2 -2
- package/dest/public/db_interfaces.d.ts.map +1 -1
- package/dest/public/debug_fn_name.d.ts +15 -2
- package/dest/public/debug_fn_name.d.ts.map +1 -1
- package/dest/public/debug_fn_name.js +28 -2
- package/dest/public/fixtures/amm_test.js +2 -2
- package/dest/public/fixtures/bulk_test.js +4 -52
- package/dest/public/fixtures/custom_bytecode_tester.d.ts +28 -6
- package/dest/public/fixtures/custom_bytecode_tester.d.ts.map +1 -1
- package/dest/public/fixtures/custom_bytecode_tester.js +36 -12
- package/dest/public/fixtures/custom_bytecode_tests.d.ts +4 -1
- package/dest/public/fixtures/custom_bytecode_tests.d.ts.map +1 -1
- package/dest/public/fixtures/custom_bytecode_tests.js +74 -9
- package/dest/public/fixtures/index.d.ts +4 -2
- package/dest/public/fixtures/index.d.ts.map +1 -1
- package/dest/public/fixtures/index.js +3 -1
- package/dest/public/fixtures/minimal_public_tx.d.ts +2 -7
- package/dest/public/fixtures/minimal_public_tx.d.ts.map +1 -1
- package/dest/public/fixtures/minimal_public_tx.js +2 -12
- package/dest/public/fixtures/opcode_spammer.d.ts +123 -0
- package/dest/public/fixtures/opcode_spammer.d.ts.map +1 -0
- package/dest/public/fixtures/opcode_spammer.js +1681 -0
- package/dest/public/fixtures/public_tx_simulation_tester.d.ts +22 -3
- package/dest/public/fixtures/public_tx_simulation_tester.d.ts.map +1 -1
- package/dest/public/fixtures/public_tx_simulation_tester.js +50 -13
- package/dest/public/fixtures/simple_contract_data_source.d.ts +5 -4
- package/dest/public/fixtures/simple_contract_data_source.d.ts.map +1 -1
- package/dest/public/fixtures/simple_contract_data_source.js +4 -4
- package/dest/public/fixtures/token_test.d.ts +6 -2
- package/dest/public/fixtures/token_test.d.ts.map +1 -1
- package/dest/public/fixtures/token_test.js +13 -11
- package/dest/public/fixtures/utils.d.ts +2 -2
- package/dest/public/fixtures/utils.d.ts.map +1 -1
- package/dest/public/fixtures/utils.js +4 -3
- package/dest/public/fuzzing/avm_fuzzer_simulator.d.ts +46 -0
- package/dest/public/fuzzing/avm_fuzzer_simulator.d.ts.map +1 -0
- package/dest/public/fuzzing/avm_fuzzer_simulator.js +139 -0
- package/dest/public/fuzzing/avm_simulator_bin.d.ts +2 -0
- package/dest/public/fuzzing/avm_simulator_bin.d.ts.map +1 -0
- package/dest/public/fuzzing/avm_simulator_bin.js +100 -0
- package/dest/public/hinting_db_sources.d.ts +5 -3
- package/dest/public/hinting_db_sources.d.ts.map +1 -1
- package/dest/public/hinting_db_sources.js +5 -2
- package/dest/public/index.d.ts +2 -2
- package/dest/public/index.d.ts.map +1 -1
- package/dest/public/index.js +1 -1
- package/dest/public/public_db_sources.d.ts +2 -2
- package/dest/public/public_db_sources.d.ts.map +1 -1
- package/dest/public/public_db_sources.js +1 -1
- package/dest/public/public_processor/guarded_merkle_tree.d.ts +4 -2
- package/dest/public/public_processor/guarded_merkle_tree.d.ts.map +1 -1
- package/dest/public/public_processor/guarded_merkle_tree.js +3 -0
- package/dest/public/public_processor/public_processor.d.ts +5 -5
- package/dest/public/public_processor/public_processor.d.ts.map +1 -1
- package/dest/public/public_processor/public_processor.js +35 -22
- package/dest/public/public_tx_simulator/contract_provider_for_cpp.d.ts +1 -13
- package/dest/public/public_tx_simulator/contract_provider_for_cpp.d.ts.map +1 -1
- package/dest/public/public_tx_simulator/contract_provider_for_cpp.js +18 -53
- package/dest/public/public_tx_simulator/cpp_public_tx_simulator.d.ts +21 -34
- 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 +60 -139
- package/dest/public/public_tx_simulator/cpp_public_tx_simulator_with_hinted_dbs.d.ts +42 -0
- package/dest/public/public_tx_simulator/cpp_public_tx_simulator_with_hinted_dbs.d.ts.map +1 -0
- package/dest/public/public_tx_simulator/cpp_public_tx_simulator_with_hinted_dbs.js +86 -0
- package/dest/public/public_tx_simulator/cpp_vs_ts_public_tx_simulator.d.ts +30 -0
- package/dest/public/public_tx_simulator/cpp_vs_ts_public_tx_simulator.d.ts.map +1 -0
- package/dest/public/public_tx_simulator/cpp_vs_ts_public_tx_simulator.js +171 -0
- package/dest/public/public_tx_simulator/dumping_cpp_public_tx_simulator.d.ts +22 -0
- package/dest/public/public_tx_simulator/dumping_cpp_public_tx_simulator.d.ts.map +1 -0
- package/dest/public/public_tx_simulator/dumping_cpp_public_tx_simulator.js +52 -0
- package/dest/public/public_tx_simulator/factories.d.ts +13 -0
- package/dest/public/public_tx_simulator/factories.d.ts.map +1 -0
- package/dest/public/public_tx_simulator/factories.js +28 -0
- package/dest/public/public_tx_simulator/index.d.ts +4 -1
- package/dest/public/public_tx_simulator/index.d.ts.map +1 -1
- package/dest/public/public_tx_simulator/index.js +3 -0
- package/dest/public/public_tx_simulator/measured_public_tx_simulator.d.ts +2 -2
- package/dest/public/public_tx_simulator/measured_public_tx_simulator.d.ts.map +1 -1
- package/dest/public/public_tx_simulator/public_tx_context.d.ts +2 -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 +1 -1
- package/dest/public/public_tx_simulator/public_tx_simulator.d.ts +3 -3
- package/dest/public/public_tx_simulator/public_tx_simulator.d.ts.map +1 -1
- package/dest/public/public_tx_simulator/public_tx_simulator.js +11 -5
- package/dest/public/public_tx_simulator/public_tx_simulator_interface.d.ts +24 -1
- 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 +2 -2
- package/dest/public/public_tx_simulator/telemetry_public_tx_simulator.d.ts.map +1 -1
- package/dest/public/side_effect_trace.d.ts +2 -2
- package/dest/public/side_effect_trace.d.ts.map +1 -1
- package/dest/public/side_effect_trace.js +1 -1
- package/dest/public/side_effect_trace_interface.d.ts +2 -2
- package/dest/public/side_effect_trace_interface.d.ts.map +1 -1
- package/dest/public/state_manager/nullifiers.d.ts +2 -2
- package/dest/public/state_manager/nullifiers.d.ts.map +1 -1
- package/dest/public/state_manager/public_storage.d.ts +2 -2
- package/dest/public/state_manager/public_storage.d.ts.map +1 -1
- package/dest/public/state_manager/public_storage.js +1 -1
- package/dest/public/state_manager/state_manager.d.ts +7 -2
- package/dest/public/state_manager/state_manager.d.ts.map +1 -1
- package/dest/public/state_manager/state_manager.js +5 -2
- package/package.json +19 -19
- package/src/common/errors.ts +1 -1
- package/src/private/acvm/deserialize.ts +1 -1
- package/src/private/acvm/serialize.ts +1 -1
- package/src/private/circuit_recording/circuit_recorder.ts +17 -16
- package/src/public/avm/avm_context.ts +1 -1
- package/src/public/avm/avm_contract_call_result.ts +1 -1
- package/src/public/avm/avm_execution_environment.ts +1 -1
- package/src/public/avm/avm_gas.ts +3 -3
- package/src/public/avm/avm_machine_state.ts +1 -1
- package/src/public/avm/avm_memory_types.ts +1 -1
- package/src/public/avm/avm_simulator.ts +8 -5
- package/src/public/avm/errors.ts +17 -3
- package/src/public/avm/fixtures/avm_simulation_tester.ts +1 -1
- package/src/public/avm/fixtures/base_avm_simulation_tester.ts +1 -1
- package/src/public/avm/fixtures/initializers.ts +3 -3
- package/src/public/avm/fixtures/utils.ts +3 -2
- package/src/public/avm/opcodes/addressing_mode.ts +2 -2
- package/src/public/avm/opcodes/arithmetic.ts +13 -1
- package/src/public/avm/opcodes/ec_add.ts +2 -2
- package/src/public/avm/opcodes/hashing.ts +10 -4
- package/src/public/avm/opcodes/memory.ts +1 -1
- package/src/public/avm/opcodes/misc.ts +2 -2
- package/src/public/avm/revert_reason.ts +6 -3
- package/src/public/avm/serialization/bytecode_serialization.ts +17 -6
- package/src/public/avm/serialization/instruction_serialization.ts +1 -1
- package/src/public/avm/test_utils.ts +1 -1
- package/src/public/contracts_db_checkpoint.ts +1 -1
- package/src/public/db_interfaces.ts +1 -1
- package/src/public/debug_fn_name.ts +35 -3
- package/src/public/fixtures/amm_test.ts +2 -2
- package/src/public/fixtures/bulk_test.ts +7 -7
- package/src/public/fixtures/custom_bytecode_tester.ts +53 -19
- package/src/public/fixtures/custom_bytecode_tests.ts +93 -9
- package/src/public/fixtures/index.ts +7 -1
- package/src/public/fixtures/minimal_public_tx.ts +4 -13
- package/src/public/fixtures/opcode_spammer.ts +1638 -0
- package/src/public/fixtures/public_tx_simulation_tester.ts +69 -13
- package/src/public/fixtures/simple_contract_data_source.ts +8 -9
- package/src/public/fixtures/token_test.ts +18 -9
- package/src/public/fixtures/utils.ts +2 -3
- package/src/public/fuzzing/avm_fuzzer_simulator.ts +240 -0
- package/src/public/fuzzing/avm_simulator_bin.ts +140 -0
- package/src/public/hinting_db_sources.ts +8 -3
- package/src/public/index.ts +7 -1
- package/src/public/public_db_sources.ts +1 -1
- package/src/public/public_processor/guarded_merkle_tree.ts +6 -1
- package/src/public/public_processor/public_processor.ts +43 -27
- package/src/public/public_tx_simulator/contract_provider_for_cpp.ts +19 -60
- package/src/public/public_tx_simulator/cpp_public_tx_simulator.ts +68 -174
- package/src/public/public_tx_simulator/cpp_public_tx_simulator_with_hinted_dbs.ts +132 -0
- package/src/public/public_tx_simulator/cpp_vs_ts_public_tx_simulator.ts +237 -0
- package/src/public/public_tx_simulator/dumping_cpp_public_tx_simulator.ts +81 -0
- package/src/public/public_tx_simulator/factories.ts +41 -0
- package/src/public/public_tx_simulator/index.ts +3 -0
- package/src/public/public_tx_simulator/measured_public_tx_simulator.ts +1 -1
- package/src/public/public_tx_simulator/public_tx_context.ts +1 -1
- package/src/public/public_tx_simulator/public_tx_simulator.ts +24 -9
- package/src/public/public_tx_simulator/public_tx_simulator_interface.ts +23 -0
- package/src/public/public_tx_simulator/telemetry_public_tx_simulator.ts +1 -1
- package/src/public/side_effect_trace.ts +1 -1
- package/src/public/side_effect_trace_interface.ts +1 -1
- package/src/public/state_manager/nullifiers.ts +1 -1
- package/src/public/state_manager/public_storage.ts +1 -1
- package/src/public/state_manager/state_manager.ts +10 -3
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
2
|
-
import {
|
|
3
|
-
import { avmSimulate,
|
|
1
|
+
import { type Logger, createLogger, logLevel } from '@aztec/foundation/log';
|
|
2
|
+
import { sleep } from '@aztec/foundation/sleep';
|
|
3
|
+
import { type CancellationToken, avmSimulate, cancelSimulation, createCancellationToken } from '@aztec/native';
|
|
4
4
|
import { ProtocolContractsList } from '@aztec/protocol-contracts';
|
|
5
5
|
import {
|
|
6
|
-
AvmCircuitInputs,
|
|
7
6
|
AvmFastSimulationInputs,
|
|
8
7
|
AvmTxHint,
|
|
9
8
|
type PublicSimulatorConfig,
|
|
@@ -12,11 +11,13 @@ import {
|
|
|
12
11
|
} from '@aztec/stdlib/avm';
|
|
13
12
|
import { SimulationError } from '@aztec/stdlib/errors';
|
|
14
13
|
import type { MerkleTreeWriteOperations } from '@aztec/stdlib/trees';
|
|
15
|
-
import type { GlobalVariables,
|
|
14
|
+
import type { GlobalVariables, Tx } from '@aztec/stdlib/tx';
|
|
16
15
|
import { WorldStateRevisionWithHandle } from '@aztec/stdlib/world-state';
|
|
16
|
+
import { type TelemetryClient, type Tracer, getTelemetryClient } from '@aztec/telemetry-client';
|
|
17
17
|
|
|
18
18
|
import { strict as assert } from 'assert';
|
|
19
19
|
|
|
20
|
+
import { ExecutorMetrics } from '../executor_metrics.js';
|
|
20
21
|
import type { ExecutorMetricsInterface } from '../executor_metrics_interface.js';
|
|
21
22
|
import type { PublicContractsDB } from '../public_db_sources.js';
|
|
22
23
|
import { ContractProviderForCpp } from './contract_provider_for_cpp.js';
|
|
@@ -30,12 +31,13 @@ import type {
|
|
|
30
31
|
* C++ implementation of PublicTxSimulator using the C++ simulator.
|
|
31
32
|
* The C++ simulator accesses the world state directly/natively within C++.
|
|
32
33
|
* 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
34
|
*/
|
|
37
35
|
export class CppPublicTxSimulator extends PublicTxSimulator implements PublicTxSimulatorInterface {
|
|
38
36
|
protected override log: Logger;
|
|
37
|
+
/** Current cancellation token for in-flight simulation. */
|
|
38
|
+
private cancellationToken?: CancellationToken;
|
|
39
|
+
/** Current simulation promise, used to wait for completion after cancellation. */
|
|
40
|
+
private simulationPromise?: Promise<Buffer>;
|
|
39
41
|
|
|
40
42
|
constructor(
|
|
41
43
|
merkleTree: MerkleTreeWriteOperations,
|
|
@@ -59,33 +61,6 @@ export class CppPublicTxSimulator extends PublicTxSimulator implements PublicTxS
|
|
|
59
61
|
txHash,
|
|
60
62
|
});
|
|
61
63
|
|
|
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
64
|
// Using the "as WorldStateRevisionWithHandle" is a bit of a "trust me bro", hence the assert.
|
|
90
65
|
let wsRevision = this.merkleTree.getRevision();
|
|
91
66
|
assert(
|
|
@@ -95,7 +70,7 @@ export class CppPublicTxSimulator extends PublicTxSimulator implements PublicTxS
|
|
|
95
70
|
const wsCppHandle = (wsRevision as WorldStateRevisionWithHandle).handle;
|
|
96
71
|
wsRevision = wsRevision.toWorldStateRevision(); // for msgpack serialization, we don't include the handle in the type
|
|
97
72
|
|
|
98
|
-
this.log.
|
|
73
|
+
this.log.trace(`Running C++ simulation with world state revision ${JSON.stringify(wsRevision)}`);
|
|
99
74
|
|
|
100
75
|
// Create the fast simulation inputs
|
|
101
76
|
const txHint = AvmTxHint.fromTx(tx, this.globalVariables.gasFees);
|
|
@@ -112,62 +87,72 @@ export class CppPublicTxSimulator extends PublicTxSimulator implements PublicTxS
|
|
|
112
87
|
const contractProvider = new ContractProviderForCpp(this.contractsDB, this.globalVariables);
|
|
113
88
|
|
|
114
89
|
// Serialize to msgpack and call the C++ simulator
|
|
115
|
-
this.log.
|
|
90
|
+
this.log.trace(`Serializing fast simulation inputs to msgpack...`);
|
|
116
91
|
const inputBuffer = fastSimInputs.serializeWithMessagePack();
|
|
117
92
|
|
|
93
|
+
// Create cancellation token for this simulation
|
|
94
|
+
this.cancellationToken = createCancellationToken();
|
|
95
|
+
|
|
96
|
+
// Store the promise so cancel() can wait for it
|
|
97
|
+
this.log.debug(`Calling C++ simulator for tx ${txHash}`);
|
|
98
|
+
this.simulationPromise = avmSimulate(inputBuffer, contractProvider, wsCppHandle, logLevel, this.cancellationToken);
|
|
99
|
+
|
|
118
100
|
let resultBuffer: Buffer;
|
|
119
101
|
try {
|
|
120
|
-
|
|
121
|
-
resultBuffer = await avmSimulate(inputBuffer, contractProvider, wsCppHandle);
|
|
102
|
+
resultBuffer = await this.simulationPromise;
|
|
122
103
|
} catch (error: any) {
|
|
104
|
+
// Check if this was a cancellation
|
|
105
|
+
if (error.message?.includes('Simulation cancelled')) {
|
|
106
|
+
throw new SimulationError(`C++ simulation cancelled`, []);
|
|
107
|
+
}
|
|
123
108
|
throw new SimulationError(`C++ simulation failed: ${error.message}`, []);
|
|
109
|
+
} finally {
|
|
110
|
+
this.cancellationToken = undefined;
|
|
111
|
+
this.simulationPromise = undefined;
|
|
124
112
|
}
|
|
125
113
|
|
|
126
114
|
// 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
115
|
|
|
131
116
|
// Deserialize the msgpack result
|
|
132
|
-
this.log.
|
|
117
|
+
this.log.trace(`Deserializing C++ from buffer (size: ${resultBuffer.length})...`);
|
|
133
118
|
const cppResultJSON: object = deserializeFromMessagePack(resultBuffer);
|
|
134
|
-
|
|
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...`);
|
|
119
|
+
this.log.trace(`Deserializing C++ result to PublicTxResult...`);
|
|
141
120
|
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
121
|
|
|
162
|
-
this.log.
|
|
122
|
+
this.log.trace(`C++ simulation completed for tx ${txHash}`, {
|
|
163
123
|
txHash,
|
|
164
|
-
reverted: !
|
|
165
|
-
|
|
166
|
-
cppGasUsed: tsResult.gasUsed.totalGas.l2Gas,
|
|
124
|
+
reverted: !cppResult.revertCode.isOK(),
|
|
125
|
+
cppGasUsed: cppResult.gasUsed.totalGas.l2Gas,
|
|
167
126
|
});
|
|
168
127
|
|
|
169
|
-
|
|
170
|
-
|
|
128
|
+
return cppResult;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Cancel the current simulation if one is in progress.
|
|
133
|
+
* This signals the C++ simulator to stop at the next opcode or before the next WorldState write.
|
|
134
|
+
* Safe to call even if no simulation is in progress.
|
|
135
|
+
*
|
|
136
|
+
* @param waitTimeoutMs - If provided, wait up to this many ms for the simulation to actually stop.
|
|
137
|
+
* This is important because C++ might be in the middle of a slow operation
|
|
138
|
+
* (e.g., pad_trees) and won't check the cancellation flag until it completes.
|
|
139
|
+
* Default timeout of 100ms after cancellation.
|
|
140
|
+
*/
|
|
141
|
+
public async cancel(waitTimeoutMs: number = 100): Promise<void> {
|
|
142
|
+
if (this.cancellationToken) {
|
|
143
|
+
this.log.debug('Cancelling C++ simulation');
|
|
144
|
+
cancelSimulation(this.cancellationToken);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Wait for the simulation to actually complete if not already done
|
|
148
|
+
if (this.simulationPromise) {
|
|
149
|
+
this.log.debug(`Waiting up to ${waitTimeoutMs}ms for C++ simulation to stop`);
|
|
150
|
+
await Promise.race([
|
|
151
|
+
this.simulationPromise.catch(() => {}), // Ignore rejection, just wait for completion
|
|
152
|
+
sleep(waitTimeoutMs),
|
|
153
|
+
]);
|
|
154
|
+
this.log.debug('C++ simulation stopped or wait timed out');
|
|
155
|
+
}
|
|
171
156
|
}
|
|
172
157
|
}
|
|
173
158
|
|
|
@@ -195,112 +180,21 @@ export class MeasuredCppPublicTxSimulator extends CppPublicTxSimulator implement
|
|
|
195
180
|
}
|
|
196
181
|
|
|
197
182
|
/**
|
|
198
|
-
* C++
|
|
199
|
-
* This implementation runs TS simulation first to collect all hints,
|
|
200
|
-
* then passes the complete AvmCircuitInputs (hints + public inputs)
|
|
201
|
-
* to C++ to run hinted simulation.
|
|
183
|
+
* A C++ public tx simulator that tracks runtime/production metrics with telemetry.
|
|
202
184
|
*/
|
|
203
|
-
export class
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
constructor(
|
|
207
|
-
merkleTree: MerkleTreeWriteOperations,
|
|
208
|
-
contractsDB: PublicContractsDB,
|
|
209
|
-
globalVariables: GlobalVariables,
|
|
210
|
-
config?: Partial<PublicSimulatorConfig>,
|
|
211
|
-
) {
|
|
212
|
-
super(merkleTree, contractsDB, globalVariables, config);
|
|
213
|
-
this.log = createLogger(`simulator:cpp_public_tx_simulator_hinted_dbs`);
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* Simulate a transaction's public portion using the C++ vm2 simulator with hinted DBs.
|
|
218
|
-
*
|
|
219
|
-
* This implementation:
|
|
220
|
-
* 1. Runs the full TypeScript simulation to generate AvmCircuitInputs (hints + public inputs)
|
|
221
|
-
* 2. Passes the complete AvmCircuitInputs to C++ to run hinted simulation
|
|
222
|
-
*
|
|
223
|
-
* @param tx - The transaction to simulate.
|
|
224
|
-
* @returns The result of the transaction's public execution.
|
|
225
|
-
*/
|
|
226
|
-
public override async simulate(tx: Tx): Promise<PublicTxResult> {
|
|
227
|
-
const txHash = this.computeTxHash(tx);
|
|
228
|
-
this.log.debug(`C++ hinted DB simulation of ${tx.publicFunctionCalldata.length} public calls for tx ${txHash}`, {
|
|
229
|
-
txHash,
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
// First, run TS simulation to generate hints and public inputs
|
|
233
|
-
this.log.debug(`Running TS simulation for tx ${txHash}`);
|
|
234
|
-
|
|
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}`);
|
|
185
|
+
export class TelemetryCppPublicTxSimulator extends MeasuredCppPublicTxSimulator {
|
|
186
|
+
/* tracer needed by trackSpans */
|
|
187
|
+
public readonly tracer: Tracer;
|
|
239
188
|
|
|
240
|
-
// Extract the full AvmCircuitInputs from the TS result
|
|
241
|
-
const avmCircuitInputs = new AvmCircuitInputs(tsResult.hints!, tsResult.publicInputs);
|
|
242
|
-
|
|
243
|
-
// Second, run C++ simulation with hinted DBs
|
|
244
|
-
this.log.debug(`Running C++ simulation with hinted DBs for tx ${txHash}`);
|
|
245
|
-
|
|
246
|
-
// Serialize to msgpack and call the C++ simulator
|
|
247
|
-
const inputBuffer = avmCircuitInputs.serializeWithMessagePack();
|
|
248
|
-
|
|
249
|
-
let resultBuffer: Buffer;
|
|
250
|
-
try {
|
|
251
|
-
resultBuffer = await avmSimulateWithHintedDbs(inputBuffer);
|
|
252
|
-
} catch (error: any) {
|
|
253
|
-
throw new SimulationError(`C++ hinted simulation failed: ${error.message}`, []);
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
// Deserialize the msgpack result
|
|
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));
|
|
262
|
-
|
|
263
|
-
this.log.debug(`C++ hinted simulation completed for tx ${txHash}`, {
|
|
264
|
-
txHash,
|
|
265
|
-
reverted: !tsResult.revertCode.isOK(),
|
|
266
|
-
tsGasUsed: tsResult.gasUsed.totalGas.l2Gas,
|
|
267
|
-
cppGasUsed: tsResult.gasUsed.totalGas.l2Gas,
|
|
268
|
-
});
|
|
269
|
-
|
|
270
|
-
// TODO(fcarreiro): complete this.
|
|
271
|
-
return tsResult;
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* Class to record metrics for simulation.
|
|
277
|
-
*
|
|
278
|
-
* Note(dbanks12): We might not be able to collect all the same metrics in C++ as we do in TS!
|
|
279
|
-
* Unless we move some of the metrics collection to C++, we don't have inner functions exposed
|
|
280
|
-
* to TS for tracking.
|
|
281
|
-
*/
|
|
282
|
-
export class MeasuredCppPublicTxSimulatorHintedDbs
|
|
283
|
-
extends CppPublicTxSimulatorHintedDbs
|
|
284
|
-
implements MeasuredPublicTxSimulatorInterface
|
|
285
|
-
{
|
|
286
189
|
constructor(
|
|
287
190
|
merkleTree: MerkleTreeWriteOperations,
|
|
288
191
|
contractsDB: PublicContractsDB,
|
|
289
192
|
globalVariables: GlobalVariables,
|
|
290
|
-
|
|
193
|
+
telemetryClient: TelemetryClient = getTelemetryClient(),
|
|
291
194
|
config?: Partial<PublicSimulatorConfig>,
|
|
292
195
|
) {
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
public override async simulate(tx: Tx, txLabel: string = 'unlabeledTx'): Promise<PublicTxResult> {
|
|
297
|
-
this.metrics.startRecordingTxSimulation(txLabel);
|
|
298
|
-
let result: PublicTxResult | undefined;
|
|
299
|
-
try {
|
|
300
|
-
result = await super.simulate(tx);
|
|
301
|
-
} finally {
|
|
302
|
-
this.metrics.stopRecordingTxSimulation(txLabel, result?.gasUsed, result?.revertCode);
|
|
303
|
-
}
|
|
304
|
-
return result;
|
|
196
|
+
const metrics = new ExecutorMetrics(telemetryClient, 'CppPublicTxSimulator');
|
|
197
|
+
super(merkleTree, contractsDB, globalVariables, metrics, config);
|
|
198
|
+
this.tracer = metrics.tracer;
|
|
305
199
|
}
|
|
306
200
|
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { type Logger, createLogger, logLevel } from '@aztec/foundation/log';
|
|
2
|
+
import { avmSimulateWithHintedDbs } from '@aztec/native';
|
|
3
|
+
import {
|
|
4
|
+
AvmCircuitInputs,
|
|
5
|
+
type PublicSimulatorConfig,
|
|
6
|
+
PublicTxResult,
|
|
7
|
+
deserializeFromMessagePack,
|
|
8
|
+
} from '@aztec/stdlib/avm';
|
|
9
|
+
import { SimulationError } from '@aztec/stdlib/errors';
|
|
10
|
+
import type { MerkleTreeWriteOperations } from '@aztec/stdlib/trees';
|
|
11
|
+
import type { GlobalVariables, Tx } from '@aztec/stdlib/tx';
|
|
12
|
+
|
|
13
|
+
import { strict as assert } from 'assert';
|
|
14
|
+
|
|
15
|
+
import type { ExecutorMetricsInterface } from '../executor_metrics_interface.js';
|
|
16
|
+
import type { PublicContractsDB } from '../public_db_sources.js';
|
|
17
|
+
import { PublicTxSimulator } from './public_tx_simulator.js';
|
|
18
|
+
import type {
|
|
19
|
+
MeasuredPublicTxSimulatorInterface,
|
|
20
|
+
PublicTxSimulatorInterface,
|
|
21
|
+
} from './public_tx_simulator_interface.js';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* C++ implementation of PublicTxSimulator using pre-collected hints.
|
|
25
|
+
* This implementation runs TS simulation first to collect all hints,
|
|
26
|
+
* then passes the complete AvmCircuitInputs (hints + public inputs)
|
|
27
|
+
* to C++ to run hinted simulation.
|
|
28
|
+
*/
|
|
29
|
+
export class CppPublicTxSimulatorHintedDbs extends PublicTxSimulator implements PublicTxSimulatorInterface {
|
|
30
|
+
protected override log: Logger;
|
|
31
|
+
|
|
32
|
+
constructor(
|
|
33
|
+
merkleTree: MerkleTreeWriteOperations,
|
|
34
|
+
contractsDB: PublicContractsDB,
|
|
35
|
+
globalVariables: GlobalVariables,
|
|
36
|
+
config?: Partial<PublicSimulatorConfig>,
|
|
37
|
+
) {
|
|
38
|
+
super(merkleTree, contractsDB, globalVariables, config);
|
|
39
|
+
this.log = createLogger(`simulator:cpp_public_tx_simulator_hinted_dbs`);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Simulate a transaction's public portion using the C++ vm2 simulator with hinted DBs.
|
|
44
|
+
*
|
|
45
|
+
* This implementation:
|
|
46
|
+
* 1. Runs the full TypeScript simulation to generate AvmCircuitInputs (hints + public inputs)
|
|
47
|
+
* 2. Passes the complete AvmCircuitInputs to C++ to run hinted simulation
|
|
48
|
+
*
|
|
49
|
+
* @param tx - The transaction to simulate.
|
|
50
|
+
* @returns The result of the transaction's public execution.
|
|
51
|
+
*/
|
|
52
|
+
public override async simulate(tx: Tx): Promise<PublicTxResult> {
|
|
53
|
+
const txHash = this.computeTxHash(tx);
|
|
54
|
+
this.log.debug(`C++ hinted DB simulation of ${tx.publicFunctionCalldata.length} public calls for tx ${txHash}`, {
|
|
55
|
+
txHash,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// First, run TS simulation to generate hints and public inputs
|
|
59
|
+
this.log.debug(`Running TS simulation for tx ${txHash}`);
|
|
60
|
+
|
|
61
|
+
// Run the full TypeScript simulation using the parent class
|
|
62
|
+
// This will modify the merkle tree with the transaction's state changes
|
|
63
|
+
const tsResult = await super.simulate(tx);
|
|
64
|
+
this.log.debug(`TS simulation succeeded for tx ${txHash}`);
|
|
65
|
+
|
|
66
|
+
// Extract the full AvmCircuitInputs from the TS result
|
|
67
|
+
const avmCircuitInputs = new AvmCircuitInputs(tsResult.hints!, tsResult.publicInputs!);
|
|
68
|
+
|
|
69
|
+
// Second, run C++ simulation with hinted DBs
|
|
70
|
+
this.log.debug(`Running C++ simulation with hinted DBs for tx ${txHash}`);
|
|
71
|
+
|
|
72
|
+
// Serialize to msgpack and call the C++ simulator
|
|
73
|
+
const inputBuffer = avmCircuitInputs.serializeWithMessagePack();
|
|
74
|
+
|
|
75
|
+
let resultBuffer: Buffer;
|
|
76
|
+
try {
|
|
77
|
+
resultBuffer = await avmSimulateWithHintedDbs(inputBuffer, logLevel);
|
|
78
|
+
} catch (error: any) {
|
|
79
|
+
throw new SimulationError(`C++ hinted simulation failed: ${error.message}`, []);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Deserialize the msgpack result
|
|
83
|
+
const cppResultJSON: object = deserializeFromMessagePack(resultBuffer);
|
|
84
|
+
const cppResult = PublicTxResult.fromPlainObject(cppResultJSON);
|
|
85
|
+
|
|
86
|
+
assert(cppResult.revertCode.equals(tsResult.revertCode));
|
|
87
|
+
assert(cppResult.gasUsed.totalGas.equals(tsResult.gasUsed.totalGas));
|
|
88
|
+
|
|
89
|
+
this.log.debug(`C++ hinted simulation completed for tx ${txHash}`, {
|
|
90
|
+
txHash,
|
|
91
|
+
reverted: !tsResult.revertCode.isOK(),
|
|
92
|
+
tsGasUsed: tsResult.gasUsed.totalGas.l2Gas,
|
|
93
|
+
cppGasUsed: tsResult.gasUsed.totalGas.l2Gas,
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
// TODO(fcarreiro): complete this.
|
|
97
|
+
return tsResult;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Class to record metrics for simulation.
|
|
103
|
+
*
|
|
104
|
+
* Note(dbanks12): We might not be able to collect all the same metrics in C++ as we do in TS!
|
|
105
|
+
* Unless we move some of the metrics collection to C++, we don't have inner functions exposed
|
|
106
|
+
* to TS for tracking.
|
|
107
|
+
*/
|
|
108
|
+
export class MeasuredCppPublicTxSimulatorHintedDbs
|
|
109
|
+
extends CppPublicTxSimulatorHintedDbs
|
|
110
|
+
implements MeasuredPublicTxSimulatorInterface
|
|
111
|
+
{
|
|
112
|
+
constructor(
|
|
113
|
+
merkleTree: MerkleTreeWriteOperations,
|
|
114
|
+
contractsDB: PublicContractsDB,
|
|
115
|
+
globalVariables: GlobalVariables,
|
|
116
|
+
protected readonly metrics: ExecutorMetricsInterface,
|
|
117
|
+
config?: Partial<PublicSimulatorConfig>,
|
|
118
|
+
) {
|
|
119
|
+
super(merkleTree, contractsDB, globalVariables, config);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
public override async simulate(tx: Tx, txLabel: string = 'unlabeledTx'): Promise<PublicTxResult> {
|
|
123
|
+
this.metrics.startRecordingTxSimulation(txLabel);
|
|
124
|
+
let result: PublicTxResult | undefined;
|
|
125
|
+
try {
|
|
126
|
+
result = await super.simulate(tx);
|
|
127
|
+
} finally {
|
|
128
|
+
this.metrics.stopRecordingTxSimulation(txLabel, result?.gasUsed, result?.revertCode);
|
|
129
|
+
}
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
}
|