@aztec/simulator 0.0.1-commit.fce3e4f → 0.0.1-commit.ff7989d6c
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/README.md +6 -4
- package/dest/common/errors.d.ts +8 -2
- package/dest/common/errors.d.ts.map +1 -1
- package/dest/private/acvm/acvm.d.ts +4 -2
- package/dest/private/acvm/acvm.d.ts.map +1 -1
- package/dest/private/acvm/acvm.js +4 -3
- 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/acvm_native.d.ts +5 -3
- package/dest/private/acvm_native.d.ts.map +1 -1
- package/dest/private/acvm_native.js +8 -6
- package/dest/private/acvm_wasm.d.ts +4 -3
- package/dest/private/acvm_wasm.d.ts.map +1 -1
- package/dest/private/acvm_wasm.js +4 -4
- package/dest/private/circuit_recording/circuit_recorder.d.ts +4 -3
- package/dest/private/circuit_recording/circuit_recorder.d.ts.map +1 -1
- package/dest/private/circuit_recording/circuit_recorder.js +21 -18
- package/dest/private/circuit_recording/file_circuit_recorder.d.ts +3 -2
- package/dest/private/circuit_recording/file_circuit_recorder.d.ts.map +1 -1
- package/dest/private/circuit_recording/file_circuit_recorder.js +2 -2
- package/dest/private/circuit_recording/memory_circuit_recorder.d.ts +7 -2
- package/dest/private/circuit_recording/memory_circuit_recorder.d.ts.map +1 -1
- package/dest/private/circuit_recording/memory_circuit_recorder.js +4 -4
- package/dest/private/factory.d.ts +3 -3
- package/dest/private/factory.d.ts.map +1 -1
- package/dest/private/factory.js +7 -4
- package/dest/public/avm/avm_context.d.ts +3 -3
- package/dest/public/avm/avm_context.d.ts.map +1 -1
- package/dest/public/avm/avm_contract_call_result.d.ts +6 -6
- package/dest/public/avm/avm_contract_call_result.d.ts.map +1 -1
- package/dest/public/avm/avm_contract_call_result.js +3 -3
- package/dest/public/avm/avm_execution_environment.d.ts +7 -6
- 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_gas.js +3 -3
- package/dest/public/avm/avm_machine_state.d.ts +7 -6
- package/dest/public/avm/avm_machine_state.d.ts.map +1 -1
- package/dest/public/avm/avm_machine_state.js +3 -2
- 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 +4 -1
- package/dest/public/avm/avm_simulator.d.ts +4 -3
- package/dest/public/avm/avm_simulator.d.ts.map +1 -1
- package/dest/public/avm/avm_simulator.js +11 -9
- package/dest/public/avm/calldata.d.ts +51 -0
- package/dest/public/avm/calldata.d.ts.map +1 -0
- package/dest/public/avm/calldata.js +63 -0
- 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/account_proof_fetcher.d.ts +2 -0
- package/dest/public/avm/fixtures/account_proof_fetcher.d.ts.map +1 -0
- package/dest/public/avm/fixtures/account_proof_fetcher.js +152 -0
- 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 +4 -3
- 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 +5 -4
- 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/accrued_substate.d.ts +17 -18
- package/dest/public/avm/opcodes/accrued_substate.d.ts.map +1 -1
- package/dest/public/avm/opcodes/accrued_substate.js +42 -43
- package/dest/public/avm/opcodes/addressing_mode.js +2 -2
- package/dest/public/avm/opcodes/arithmetic.d.ts +10 -8
- package/dest/public/avm/opcodes/arithmetic.d.ts.map +1 -1
- package/dest/public/avm/opcodes/arithmetic.js +12 -2
- package/dest/public/avm/opcodes/bitwise.d.ts +7 -7
- package/dest/public/avm/opcodes/bitwise.d.ts.map +1 -1
- package/dest/public/avm/opcodes/bitwise.js +5 -5
- 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/comparators.js +1 -1
- package/dest/public/avm/opcodes/contract.d.ts +3 -3
- package/dest/public/avm/opcodes/contract.d.ts.map +1 -1
- package/dest/public/avm/opcodes/contract.js +8 -8
- package/dest/public/avm/opcodes/control_flow.d.ts +3 -3
- package/dest/public/avm/opcodes/control_flow.d.ts.map +1 -1
- package/dest/public/avm/opcodes/control_flow.js +4 -4
- package/dest/public/avm/opcodes/conversion.d.ts +3 -3
- package/dest/public/avm/opcodes/conversion.d.ts.map +1 -1
- package/dest/public/avm/opcodes/conversion.js +4 -4
- package/dest/public/avm/opcodes/ec_add.d.ts +4 -4
- package/dest/public/avm/opcodes/ec_add.d.ts.map +1 -1
- package/dest/public/avm/opcodes/ec_add.js +17 -8
- package/dest/public/avm/opcodes/environment_getters.d.ts +5 -5
- package/dest/public/avm/opcodes/environment_getters.d.ts.map +1 -1
- package/dest/public/avm/opcodes/environment_getters.js +6 -6
- package/dest/public/avm/opcodes/external_calls.d.ts +9 -9
- package/dest/public/avm/opcodes/external_calls.d.ts.map +1 -1
- package/dest/public/avm/opcodes/external_calls.js +23 -22
- package/dest/public/avm/opcodes/hashing.d.ts +8 -8
- package/dest/public/avm/opcodes/hashing.d.ts.map +1 -1
- package/dest/public/avm/opcodes/hashing.js +21 -16
- package/dest/public/avm/opcodes/instruction_impl.d.ts +4 -4
- package/dest/public/avm/opcodes/instruction_impl.d.ts.map +1 -1
- package/dest/public/avm/opcodes/instruction_impl.js +4 -4
- package/dest/public/avm/opcodes/memory.d.ts +14 -14
- package/dest/public/avm/opcodes/memory.d.ts.map +1 -1
- package/dest/public/avm/opcodes/memory.js +26 -26
- package/dest/public/avm/opcodes/misc.d.ts +3 -3
- package/dest/public/avm/opcodes/misc.d.ts.map +1 -1
- package/dest/public/avm/opcodes/misc.js +6 -6
- package/dest/public/avm/opcodes/storage.d.ts +16 -15
- package/dest/public/avm/opcodes/storage.d.ts.map +1 -1
- package/dest/public/avm/opcodes/storage.js +34 -24
- 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 +12 -9
- package/dest/public/avm/serialization/instruction_serialization.d.ts +2 -2
- package/dest/public/avm/serialization/instruction_serialization.d.ts.map +1 -1
- package/dest/public/avm/serialization/instruction_serialization.js +2 -2
- 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 +16 -3
- package/dest/public/debug_fn_name.d.ts.map +1 -1
- package/dest/public/debug_fn_name.js +31 -3
- package/dest/public/executor_metrics.d.ts +1 -1
- package/dest/public/executor_metrics.d.ts.map +1 -1
- package/dest/public/executor_metrics.js +12 -33
- package/dest/public/fixtures/amm_test.js +4 -4
- package/dest/public/fixtures/bulk_test.d.ts +3 -3
- package/dest/public/fixtures/bulk_test.d.ts.map +1 -1
- package/dest/public/fixtures/bulk_test.js +5 -69
- 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 +11 -8
- package/dest/public/fixtures/custom_bytecode_tests.d.ts.map +1 -1
- package/dest/public/fixtures/custom_bytecode_tests.js +83 -18
- 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 +4 -14
- package/dest/public/fixtures/opcode_spammer.d.ts +122 -0
- package/dest/public/fixtures/opcode_spammer.d.ts.map +1 -0
- package/dest/public/fixtures/opcode_spammer.js +1653 -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 +8 -7
- package/dest/public/fuzzing/avm_fuzzer_simulator.d.ts +60 -0
- package/dest/public/fuzzing/avm_fuzzer_simulator.d.ts.map +1 -0
- package/dest/public/fuzzing/avm_fuzzer_simulator.js +171 -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 +118 -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 +7 -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 +5 -4
- package/dest/public/public_db_sources.d.ts.map +1 -1
- package/dest/public/public_db_sources.js +5 -5
- 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 +5 -0
- package/dest/public/public_processor/public_processor.d.ts +10 -7
- package/dest/public/public_processor/public_processor.d.ts.map +1 -1
- package/dest/public/public_processor/public_processor.js +454 -51
- package/dest/public/public_processor/public_processor_metrics.d.ts +2 -2
- package/dest/public/public_processor/public_processor_metrics.d.ts.map +1 -1
- package/dest/public/public_processor/public_processor_metrics.js +28 -45
- package/dest/public/public_tx_simulator/contract_provider_for_cpp.d.ts +3 -14
- 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 +20 -55
- package/dest/public/public_tx_simulator/cpp_public_tx_simulator.d.ts +24 -37
- 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 +67 -145
- 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 +23 -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 +14 -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 +3 -3
- 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 +5 -4
- package/dest/public/public_tx_simulator/public_tx_context.d.ts.map +1 -1
- package/dest/public/public_tx_simulator/public_tx_context.js +9 -9
- package/dest/public/public_tx_simulator/public_tx_simulator.d.ts +8 -6
- package/dest/public/public_tx_simulator/public_tx_simulator.d.ts.map +1 -1
- package/dest/public/public_tx_simulator/public_tx_simulator.js +23 -12
- 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/public_tx_simulator/telemetry_public_tx_simulator.js +395 -19
- package/dest/public/side_effect_trace.d.ts +6 -5
- package/dest/public/side_effect_trace.d.ts.map +1 -1
- package/dest/public/side_effect_trace.js +4 -4
- 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 +16 -5
- package/dest/public/state_manager/state_manager.d.ts.map +1 -1
- package/dest/public/state_manager/state_manager.js +19 -9
- package/dest/public/test_executor_metrics.d.ts +3 -2
- package/dest/public/test_executor_metrics.d.ts.map +1 -1
- package/dest/public/test_executor_metrics.js +2 -2
- package/package.json +20 -20
- package/src/common/errors.ts +1 -1
- package/src/private/acvm/acvm.ts +4 -3
- package/src/private/acvm/deserialize.ts +1 -1
- package/src/private/acvm/serialize.ts +1 -1
- package/src/private/acvm_native.ts +11 -5
- package/src/private/acvm_wasm.ts +7 -3
- package/src/private/circuit_recording/circuit_recorder.ts +22 -19
- package/src/private/circuit_recording/file_circuit_recorder.ts +7 -2
- package/src/private/circuit_recording/memory_circuit_recorder.ts +6 -4
- package/src/private/factory.ts +7 -4
- package/src/public/avm/avm_context.ts +2 -2
- package/src/public/avm/avm_contract_call_result.ts +8 -6
- package/src/public/avm/avm_execution_environment.ts +10 -5
- package/src/public/avm/avm_gas.ts +5 -5
- package/src/public/avm/avm_machine_state.ts +7 -6
- package/src/public/avm/avm_memory_types.ts +5 -1
- package/src/public/avm/avm_simulator.ts +16 -10
- package/src/public/avm/calldata.ts +100 -0
- package/src/public/avm/errors.ts +17 -3
- package/src/public/avm/fixtures/account_proof.json +553 -0
- package/src/public/avm/fixtures/account_proof_fetcher.ts +166 -0
- package/src/public/avm/fixtures/avm_simulation_tester.ts +9 -3
- package/src/public/avm/fixtures/base_avm_simulation_tester.ts +1 -1
- package/src/public/avm/fixtures/initializers.ts +5 -4
- package/src/public/avm/fixtures/utils.ts +3 -2
- package/src/public/avm/opcodes/accrued_substate.ts +31 -34
- package/src/public/avm/opcodes/addressing_mode.ts +2 -2
- package/src/public/avm/opcodes/arithmetic.ts +14 -2
- package/src/public/avm/opcodes/bitwise.ts +3 -3
- package/src/public/avm/opcodes/comparators.ts +1 -1
- package/src/public/avm/opcodes/contract.ts +4 -7
- package/src/public/avm/opcodes/control_flow.ts +2 -2
- package/src/public/avm/opcodes/conversion.ts +3 -3
- package/src/public/avm/opcodes/ec_add.ts +15 -6
- package/src/public/avm/opcodes/environment_getters.ts +7 -7
- package/src/public/avm/opcodes/external_calls.ts +17 -15
- package/src/public/avm/opcodes/hashing.ts +16 -10
- package/src/public/avm/opcodes/instruction_impl.ts +2 -2
- package/src/public/avm/opcodes/memory.ts +20 -20
- package/src/public/avm/opcodes/misc.ts +4 -4
- package/src/public/avm/opcodes/storage.ts +30 -22
- package/src/public/avm/revert_reason.ts +6 -3
- package/src/public/avm/serialization/bytecode_serialization.ts +19 -8
- package/src/public/avm/serialization/instruction_serialization.ts +2 -2
- 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 +39 -5
- package/src/public/executor_metrics.ts +9 -33
- package/src/public/fixtures/amm_test.ts +4 -4
- package/src/public/fixtures/bulk_test.ts +9 -9
- package/src/public/fixtures/custom_bytecode_tester.ts +53 -19
- package/src/public/fixtures/custom_bytecode_tests.ts +111 -18
- package/src/public/fixtures/index.ts +7 -1
- package/src/public/fixtures/minimal_public_tx.ts +6 -15
- package/src/public/fixtures/opcode_spammer.ts +1717 -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 +6 -7
- package/src/public/fuzzing/avm_fuzzer_simulator.ts +288 -0
- package/src/public/fuzzing/avm_simulator_bin.ts +175 -0
- package/src/public/hinting_db_sources.ts +8 -3
- package/src/public/index.ts +7 -1
- package/src/public/public_db_sources.ts +16 -6
- package/src/public/public_processor/guarded_merkle_tree.ts +6 -1
- package/src/public/public_processor/public_processor.ts +81 -45
- package/src/public/public_processor/public_processor_metrics.ts +16 -44
- package/src/public/public_tx_simulator/contract_provider_for_cpp.ts +25 -63
- package/src/public/public_tx_simulator/cpp_public_tx_simulator.ts +83 -178
- package/src/public/public_tx_simulator/cpp_public_tx_simulator_with_hinted_dbs.ts +134 -0
- package/src/public/public_tx_simulator/cpp_vs_ts_public_tx_simulator.ts +239 -0
- package/src/public/public_tx_simulator/dumping_cpp_public_tx_simulator.ts +83 -0
- package/src/public/public_tx_simulator/factories.ts +43 -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 +14 -7
- package/src/public/public_tx_simulator/public_tx_simulator.ts +42 -16
- 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 +6 -3
- 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 +39 -23
- package/src/public/test_executor_metrics.ts +3 -3
|
@@ -1,25 +1,26 @@
|
|
|
1
|
-
import { createLogger } from '@aztec/foundation/log';
|
|
2
|
-
import {
|
|
3
|
-
import { avmSimulate,
|
|
1
|
+
import { createLogger, logLevel } from '@aztec/foundation/log';
|
|
2
|
+
import { sleep } from '@aztec/foundation/sleep';
|
|
3
|
+
import { avmSimulate, cancelSimulation, createCancellationToken } from '@aztec/native';
|
|
4
4
|
import { ProtocolContractsList } from '@aztec/protocol-contracts';
|
|
5
|
-
import {
|
|
5
|
+
import { AvmFastSimulationInputs, AvmTxHint, PublicTxResult, deserializeFromMessagePack } from '@aztec/stdlib/avm';
|
|
6
6
|
import { SimulationError } from '@aztec/stdlib/errors';
|
|
7
7
|
import { WorldStateRevisionWithHandle } from '@aztec/stdlib/world-state';
|
|
8
|
+
import { getTelemetryClient } from '@aztec/telemetry-client';
|
|
8
9
|
import { strict as assert } from 'assert';
|
|
10
|
+
import { ExecutorMetrics } from '../executor_metrics.js';
|
|
9
11
|
import { ContractProviderForCpp } from './contract_provider_for_cpp.js';
|
|
10
12
|
import { PublicTxSimulator } from './public_tx_simulator.js';
|
|
11
13
|
/**
|
|
12
14
|
* C++ implementation of PublicTxSimulator using the C++ simulator.
|
|
13
15
|
* The C++ simulator accesses the world state directly/natively within C++.
|
|
14
16
|
* For contract DB accesses, it makes callbacks through NAPI back to the TS PublicContractsDB cache.
|
|
15
|
-
*
|
|
16
|
-
* TODO(dbanks12): for now this still simulates first in TS to generate hints and public inputs,
|
|
17
|
-
* since the C++ simulator doesn't have hinting & PI generation logic yet.
|
|
18
17
|
*/ export class CppPublicTxSimulator extends PublicTxSimulator {
|
|
19
18
|
log;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
/** Current cancellation token for in-flight simulation. */ cancellationToken;
|
|
20
|
+
/** Current simulation promise, used to wait for completion after cancellation. */ simulationPromise;
|
|
21
|
+
constructor(merkleTree, contractsDB, globalVariables, config, bindings){
|
|
22
|
+
super(merkleTree, contractsDB, globalVariables, config, undefined, bindings);
|
|
23
|
+
this.log = createLogger(`simulator:cpp_public_tx_simulator`, bindings);
|
|
23
24
|
}
|
|
24
25
|
/**
|
|
25
26
|
* Simulate a transaction's public portion using the C++ avvm simulator.
|
|
@@ -31,171 +32,82 @@ import { PublicTxSimulator } from './public_tx_simulator.js';
|
|
|
31
32
|
this.log.debug(`C++ simulation of ${tx.publicFunctionCalldata.length} public calls for tx ${txHash}`, {
|
|
32
33
|
txHash
|
|
33
34
|
});
|
|
34
|
-
// TODO(dbanks12): remove the first run with TS that hints!
|
|
35
|
-
// Run TS simulation to generate hints and public inputs
|
|
36
|
-
this.log.debug(`Running TS simulation for tx ${txHash}`);
|
|
37
|
-
// create checkpoint for ws
|
|
38
|
-
let tsResult;
|
|
39
|
-
let tsStateRef;
|
|
40
|
-
await this.merkleTree.createCheckpoint();
|
|
41
|
-
this.contractsDB.createCheckpoint();
|
|
42
|
-
try {
|
|
43
|
-
// Run the full TypeScript simulation using the parent class
|
|
44
|
-
// This will modify the merkle tree with the transaction's state changes
|
|
45
|
-
tsResult = await super.simulate(tx);
|
|
46
|
-
this.log.debug(`TS simulation completed for tx ${txHash}`);
|
|
47
|
-
tsStateRef = await this.merkleTree.getStateReference(); // capture tree roots for later comparsion
|
|
48
|
-
} catch (error) {
|
|
49
|
-
this.log.warn(`TS simulation failed, but still continuing with C++ simulation: ${error.message} ${error.stack}`);
|
|
50
|
-
} finally{
|
|
51
|
-
// revert checkpoint for ws and clear contract db changes
|
|
52
|
-
// (cpp should reapply exactly the same changes if there are no bugs)
|
|
53
|
-
await this.merkleTree.revertCheckpoint();
|
|
54
|
-
this.contractsDB.revertCheckpoint();
|
|
55
|
-
}
|
|
56
|
-
this.log.debug(`Running C++ simulation for tx ${txHash}`);
|
|
57
35
|
// Using the "as WorldStateRevisionWithHandle" is a bit of a "trust me bro", hence the assert.
|
|
58
36
|
let wsRevision = this.merkleTree.getRevision();
|
|
59
37
|
assert(wsRevision instanceof WorldStateRevisionWithHandle, 'CppPublicTxSimulator a real NativeWorldStateInstance with a handle to the C++ WorldState object');
|
|
60
38
|
const wsCppHandle = wsRevision.handle;
|
|
61
39
|
wsRevision = wsRevision.toWorldStateRevision(); // for msgpack serialization, we don't include the handle in the type
|
|
62
|
-
this.log.
|
|
40
|
+
this.log.trace(`Running C++ simulation with world state revision ${JSON.stringify(wsRevision)}`);
|
|
63
41
|
// Create the fast simulation inputs
|
|
64
42
|
const txHint = AvmTxHint.fromTx(tx, this.globalVariables.gasFees);
|
|
65
43
|
const protocolContracts = ProtocolContractsList;
|
|
66
44
|
const fastSimInputs = new AvmFastSimulationInputs(wsRevision, this.config, txHint, this.globalVariables, protocolContracts);
|
|
67
45
|
// Create contract provider for callbacks to TypeScript PublicContractsDB from C++
|
|
68
|
-
const contractProvider = new ContractProviderForCpp(this.contractsDB, this.globalVariables);
|
|
46
|
+
const contractProvider = new ContractProviderForCpp(this.contractsDB, this.globalVariables, this.bindings);
|
|
69
47
|
// Serialize to msgpack and call the C++ simulator
|
|
70
|
-
this.log.
|
|
48
|
+
this.log.trace(`Serializing fast simulation inputs to msgpack...`);
|
|
71
49
|
const inputBuffer = fastSimInputs.serializeWithMessagePack();
|
|
50
|
+
// Create cancellation token for this simulation
|
|
51
|
+
this.cancellationToken = createCancellationToken();
|
|
52
|
+
// Store the promise so cancel() can wait for it
|
|
53
|
+
this.log.debug(`Calling C++ simulator for tx ${txHash}`);
|
|
54
|
+
this.simulationPromise = avmSimulate(inputBuffer, contractProvider, wsCppHandle, logLevel, // TODO: re-enable logging
|
|
55
|
+
undefined, this.cancellationToken);
|
|
72
56
|
let resultBuffer;
|
|
73
57
|
try {
|
|
74
|
-
|
|
75
|
-
resultBuffer = await avmSimulate(inputBuffer, contractProvider, wsCppHandle);
|
|
58
|
+
resultBuffer = await this.simulationPromise;
|
|
76
59
|
} catch (error) {
|
|
60
|
+
// Check if this was a cancellation
|
|
61
|
+
if (error.message?.includes('Simulation cancelled')) {
|
|
62
|
+
throw new SimulationError(`C++ simulation cancelled`, []);
|
|
63
|
+
}
|
|
77
64
|
throw new SimulationError(`C++ simulation failed: ${error.message}`, []);
|
|
65
|
+
} finally{
|
|
66
|
+
this.cancellationToken = undefined;
|
|
67
|
+
this.simulationPromise = undefined;
|
|
78
68
|
}
|
|
79
69
|
// If we've reached this point, C++ succeeded during simulation,
|
|
80
|
-
// so we assert that TS also succeeded.
|
|
81
|
-
assert(tsResult !== undefined, 'TS simulation should have succeeded if C++ succeeded');
|
|
82
|
-
assert(tsStateRef !== undefined, 'TS state reference should have been captured if C++ succeeded');
|
|
83
70
|
// Deserialize the msgpack result
|
|
84
|
-
this.log.
|
|
71
|
+
this.log.trace(`Deserializing C++ from buffer (size: ${resultBuffer.length})...`);
|
|
85
72
|
const cppResultJSON = deserializeFromMessagePack(resultBuffer);
|
|
86
|
-
|
|
87
|
-
writeTestData(`barretenberg/cpp/src/barretenberg/vm2/testing/tx_result_${txHash}.testdata.bin`, resultBuffer, /*raw=*/ true);
|
|
88
|
-
this.log.verbose(`Deserializing C++ result to PublicTxResult...`);
|
|
73
|
+
this.log.trace(`Deserializing C++ result to PublicTxResult...`);
|
|
89
74
|
const cppResult = PublicTxResult.fromPlainObject(cppResultJSON);
|
|
90
|
-
this.log.
|
|
91
|
-
// TODO(fcarreiro): complete this.
|
|
92
|
-
assert(cppResult.revertCode.equals(tsResult.revertCode));
|
|
93
|
-
assert(cppResult.gasUsed.totalGas.equals(tsResult.gasUsed.totalGas));
|
|
94
|
-
assert(cppResult.gasUsed.publicGas.equals(tsResult.gasUsed.publicGas));
|
|
95
|
-
assert(cppResult.gasUsed.teardownGas.equals(tsResult.gasUsed.teardownGas));
|
|
96
|
-
assert(cppResult.gasUsed.billedGas.equals(tsResult.gasUsed.billedGas));
|
|
97
|
-
assert(cppResult.publicInputs.toBuffer().equals(tsResult.publicInputs.toBuffer()));
|
|
98
|
-
if (this.config?.collectCallMetadata) {
|
|
99
|
-
assert(cppResult.appLogicReturnValues.length == tsResult.appLogicReturnValues.length);
|
|
100
|
-
assert(cppResult.appLogicReturnValues.every((v, i)=>v.equals(tsResult.appLogicReturnValues[i])));
|
|
101
|
-
}
|
|
102
|
-
// Confirm that tree roots match
|
|
103
|
-
const cppStateRef = await this.merkleTree.getStateReference();
|
|
104
|
-
assert(cppStateRef.equals(tsStateRef), `Tree roots mismatch between TS and C++ public simulations for tx ${txHash}`);
|
|
105
|
-
this.log.debug(`C++ simulation completed for tx ${txHash}`, {
|
|
75
|
+
this.log.trace(`C++ simulation completed for tx ${txHash}`, {
|
|
106
76
|
txHash,
|
|
107
|
-
reverted: !
|
|
108
|
-
|
|
109
|
-
cppGasUsed: tsResult.gasUsed.totalGas.l2Gas
|
|
77
|
+
reverted: !cppResult.revertCode.isOK(),
|
|
78
|
+
cppGasUsed: cppResult.gasUsed.totalGas.l2Gas
|
|
110
79
|
});
|
|
111
|
-
|
|
112
|
-
return tsResult;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
export class MeasuredCppPublicTxSimulator extends CppPublicTxSimulator {
|
|
116
|
-
metrics;
|
|
117
|
-
constructor(merkleTree, contractsDB, globalVariables, metrics, config){
|
|
118
|
-
super(merkleTree, contractsDB, globalVariables, config), this.metrics = metrics;
|
|
119
|
-
}
|
|
120
|
-
async simulate(tx, txLabel = 'unlabeledTx') {
|
|
121
|
-
this.metrics.startRecordingTxSimulation(txLabel);
|
|
122
|
-
let result;
|
|
123
|
-
try {
|
|
124
|
-
result = await super.simulate(tx);
|
|
125
|
-
} finally{
|
|
126
|
-
this.metrics.stopRecordingTxSimulation(txLabel, result?.gasUsed, result?.revertCode);
|
|
127
|
-
}
|
|
128
|
-
return result;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* C++ implementation of PublicTxSimulator using pre-collected hints.
|
|
133
|
-
* This implementation runs TS simulation first to collect all hints,
|
|
134
|
-
* then passes the complete AvmCircuitInputs (hints + public inputs)
|
|
135
|
-
* to C++ to run hinted simulation.
|
|
136
|
-
*/ export class CppPublicTxSimulatorHintedDbs extends PublicTxSimulator {
|
|
137
|
-
log;
|
|
138
|
-
constructor(merkleTree, contractsDB, globalVariables, config){
|
|
139
|
-
super(merkleTree, contractsDB, globalVariables, config);
|
|
140
|
-
this.log = createLogger(`simulator:cpp_public_tx_simulator_hinted_dbs`);
|
|
80
|
+
return cppResult;
|
|
141
81
|
}
|
|
142
82
|
/**
|
|
143
|
-
*
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
* 1. Runs the full TypeScript simulation to generate AvmCircuitInputs (hints + public inputs)
|
|
147
|
-
* 2. Passes the complete AvmCircuitInputs to C++ to run hinted simulation
|
|
83
|
+
* Cancel the current simulation if one is in progress.
|
|
84
|
+
* This signals the C++ simulator to stop at the next opcode or before the next WorldState write.
|
|
85
|
+
* Safe to call even if no simulation is in progress.
|
|
148
86
|
*
|
|
149
|
-
* @param
|
|
150
|
-
*
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
//
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
// Serialize to msgpack and call the C++ simulator
|
|
167
|
-
const inputBuffer = avmCircuitInputs.serializeWithMessagePack();
|
|
168
|
-
let resultBuffer;
|
|
169
|
-
try {
|
|
170
|
-
resultBuffer = await avmSimulateWithHintedDbs(inputBuffer);
|
|
171
|
-
} catch (error) {
|
|
172
|
-
throw new SimulationError(`C++ hinted simulation failed: ${error.message}`, []);
|
|
87
|
+
* @param waitTimeoutMs - If provided, wait up to this many ms for the simulation to actually stop.
|
|
88
|
+
* This is important because C++ might be in the middle of a slow operation
|
|
89
|
+
* (e.g., pad_trees) and won't check the cancellation flag until it completes.
|
|
90
|
+
* Default timeout of 100ms after cancellation.
|
|
91
|
+
*/ async cancel(waitTimeoutMs = 100) {
|
|
92
|
+
if (this.cancellationToken) {
|
|
93
|
+
this.log.debug('Cancelling C++ simulation');
|
|
94
|
+
cancelSimulation(this.cancellationToken);
|
|
95
|
+
}
|
|
96
|
+
// Wait for the simulation to actually complete if not already done
|
|
97
|
+
if (this.simulationPromise) {
|
|
98
|
+
this.log.debug(`Waiting up to ${waitTimeoutMs}ms for C++ simulation to stop`);
|
|
99
|
+
await Promise.race([
|
|
100
|
+
this.simulationPromise.catch(()=>{}),
|
|
101
|
+
sleep(waitTimeoutMs)
|
|
102
|
+
]);
|
|
103
|
+
this.log.debug('C++ simulation stopped or wait timed out');
|
|
173
104
|
}
|
|
174
|
-
// Deserialize the msgpack result
|
|
175
|
-
const cppResultJSON = deserializeFromMessagePack(resultBuffer);
|
|
176
|
-
const cppResult = PublicTxResult.fromPlainObject(cppResultJSON);
|
|
177
|
-
assert(cppResult.revertCode.equals(tsResult.revertCode));
|
|
178
|
-
assert(cppResult.gasUsed.totalGas.equals(tsResult.gasUsed.totalGas));
|
|
179
|
-
this.log.debug(`C++ hinted simulation completed for tx ${txHash}`, {
|
|
180
|
-
txHash,
|
|
181
|
-
reverted: !tsResult.revertCode.isOK(),
|
|
182
|
-
tsGasUsed: tsResult.gasUsed.totalGas.l2Gas,
|
|
183
|
-
cppGasUsed: tsResult.gasUsed.totalGas.l2Gas
|
|
184
|
-
});
|
|
185
|
-
// TODO(fcarreiro): complete this.
|
|
186
|
-
return tsResult;
|
|
187
105
|
}
|
|
188
106
|
}
|
|
189
|
-
|
|
190
|
-
* Class to record metrics for simulation.
|
|
191
|
-
*
|
|
192
|
-
* Note(dbanks12): We might not be able to collect all the same metrics in C++ as we do in TS!
|
|
193
|
-
* Unless we move some of the metrics collection to C++, we don't have inner functions exposed
|
|
194
|
-
* to TS for tracking.
|
|
195
|
-
*/ export class MeasuredCppPublicTxSimulatorHintedDbs extends CppPublicTxSimulatorHintedDbs {
|
|
107
|
+
export class MeasuredCppPublicTxSimulator extends CppPublicTxSimulator {
|
|
196
108
|
metrics;
|
|
197
|
-
constructor(merkleTree, contractsDB, globalVariables, metrics, config){
|
|
198
|
-
super(merkleTree, contractsDB, globalVariables, config), this.metrics = metrics;
|
|
109
|
+
constructor(merkleTree, contractsDB, globalVariables, metrics, config, bindings){
|
|
110
|
+
super(merkleTree, contractsDB, globalVariables, config, bindings), this.metrics = metrics;
|
|
199
111
|
}
|
|
200
112
|
async simulate(tx, txLabel = 'unlabeledTx') {
|
|
201
113
|
this.metrics.startRecordingTxSimulation(txLabel);
|
|
@@ -208,3 +120,13 @@ export class MeasuredCppPublicTxSimulator extends CppPublicTxSimulator {
|
|
|
208
120
|
return result;
|
|
209
121
|
}
|
|
210
122
|
}
|
|
123
|
+
/**
|
|
124
|
+
* A C++ public tx simulator that tracks runtime/production metrics with telemetry.
|
|
125
|
+
*/ export class TelemetryCppPublicTxSimulator extends MeasuredCppPublicTxSimulator {
|
|
126
|
+
/* tracer needed by trackSpans */ tracer;
|
|
127
|
+
constructor(merkleTree, contractsDB, globalVariables, telemetryClient = getTelemetryClient(), config, bindings){
|
|
128
|
+
const metrics = new ExecutorMetrics(telemetryClient, 'CppPublicTxSimulator');
|
|
129
|
+
super(merkleTree, contractsDB, globalVariables, metrics, config, bindings);
|
|
130
|
+
this.tracer = metrics.tracer;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { type Logger, type LoggerBindings } from '@aztec/foundation/log';
|
|
2
|
+
import { type PublicSimulatorConfig, PublicTxResult } from '@aztec/stdlib/avm';
|
|
3
|
+
import type { MerkleTreeWriteOperations } from '@aztec/stdlib/trees';
|
|
4
|
+
import type { GlobalVariables, Tx } from '@aztec/stdlib/tx';
|
|
5
|
+
import type { ExecutorMetricsInterface } from '../executor_metrics_interface.js';
|
|
6
|
+
import type { PublicContractsDB } from '../public_db_sources.js';
|
|
7
|
+
import { PublicTxSimulator } from './public_tx_simulator.js';
|
|
8
|
+
import type { MeasuredPublicTxSimulatorInterface, PublicTxSimulatorInterface } from './public_tx_simulator_interface.js';
|
|
9
|
+
/**
|
|
10
|
+
* C++ implementation of PublicTxSimulator using pre-collected hints.
|
|
11
|
+
* This implementation runs TS simulation first to collect all hints,
|
|
12
|
+
* then passes the complete AvmCircuitInputs (hints + public inputs)
|
|
13
|
+
* to C++ to run hinted simulation.
|
|
14
|
+
*/
|
|
15
|
+
export declare class CppPublicTxSimulatorHintedDbs extends PublicTxSimulator implements PublicTxSimulatorInterface {
|
|
16
|
+
protected log: Logger;
|
|
17
|
+
constructor(merkleTree: MerkleTreeWriteOperations, contractsDB: PublicContractsDB, globalVariables: GlobalVariables, config?: Partial<PublicSimulatorConfig>, bindings?: LoggerBindings);
|
|
18
|
+
/**
|
|
19
|
+
* Simulate a transaction's public portion using the C++ vm2 simulator with hinted DBs.
|
|
20
|
+
*
|
|
21
|
+
* This implementation:
|
|
22
|
+
* 1. Runs the full TypeScript simulation to generate AvmCircuitInputs (hints + public inputs)
|
|
23
|
+
* 2. Passes the complete AvmCircuitInputs to C++ to run hinted simulation
|
|
24
|
+
*
|
|
25
|
+
* @param tx - The transaction to simulate.
|
|
26
|
+
* @returns The result of the transaction's public execution.
|
|
27
|
+
*/
|
|
28
|
+
simulate(tx: Tx): Promise<PublicTxResult>;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Class to record metrics for simulation.
|
|
32
|
+
*
|
|
33
|
+
* Note(dbanks12): We might not be able to collect all the same metrics in C++ as we do in TS!
|
|
34
|
+
* Unless we move some of the metrics collection to C++, we don't have inner functions exposed
|
|
35
|
+
* to TS for tracking.
|
|
36
|
+
*/
|
|
37
|
+
export declare class MeasuredCppPublicTxSimulatorHintedDbs extends CppPublicTxSimulatorHintedDbs implements MeasuredPublicTxSimulatorInterface {
|
|
38
|
+
protected readonly metrics: ExecutorMetricsInterface;
|
|
39
|
+
constructor(merkleTree: MerkleTreeWriteOperations, contractsDB: PublicContractsDB, globalVariables: GlobalVariables, metrics: ExecutorMetricsInterface, config?: Partial<PublicSimulatorConfig>, bindings?: LoggerBindings);
|
|
40
|
+
simulate(tx: Tx, txLabel?: string): Promise<PublicTxResult>;
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY3BwX3B1YmxpY190eF9zaW11bGF0b3Jfd2l0aF9oaW50ZWRfZGJzLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvcHVibGljL3B1YmxpY190eF9zaW11bGF0b3IvY3BwX3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AAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAC7D,OAAO,KAAK,EACV,kCAAkC,EAClC,0BAA0B,EAC3B,MAAM,oCAAoC,CAAC;AAE5C;;;;;GAKG;AACH,qBAAa,6BAA8B,SAAQ,iBAAkB,YAAW,0BAA0B;IACxG,UAAmB,GAAG,EAAE,MAAM,CAAC;IAE/B,YACE,UAAU,EAAE,yBAAyB,EACrC,WAAW,EAAE,iBAAiB,EAC9B,eAAe,EAAE,eAAe,EAChC,MAAM,CAAC,EAAE,OAAO,CAAC,qBAAqB,CAAC,EACvC,QAAQ,CAAC,EAAE,cAAc,EAI1B;IAED;;;;;;;;;OASG;IACmB,QAAQ,CAAC,EAAE,EAAE,EAAE,GAAG,OAAO,CAAC,cAAc,CAAC,CA8C9D;CACF;AAED;;;;;;GAMG;AACH,qBAAa,qCACX,SAAQ,6BACR,YAAW,kCAAkC;IAM3C,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,wBAAwB;IAJtD,YACE,UAAU,EAAE,yBAAyB,EACrC,WAAW,EAAE,iBAAiB,EAC9B,eAAe,EAAE,eAAe,EACb,OAAO,EAAE,wBAAwB,EACpD,MAAM,CAAC,EAAE,OAAO,CAAC,qBAAqB,CAAC,EACvC,QAAQ,CAAC,EAAE,cAAc,EAG1B;IAEqB,QAAQ,CAAC,EAAE,EAAE,EAAE,EAAE,OAAO,GAAE,MAAsB,GAAG,OAAO,CAAC,cAAc,CAAC,CAS/F;CACF"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { createLogger, logLevel } from '@aztec/foundation/log';
|
|
2
|
+
import { avmSimulateWithHintedDbs } from '@aztec/native';
|
|
3
|
+
import { AvmCircuitInputs, PublicTxResult, deserializeFromMessagePack } from '@aztec/stdlib/avm';
|
|
4
|
+
import { SimulationError } from '@aztec/stdlib/errors';
|
|
5
|
+
import { strict as assert } from 'assert';
|
|
6
|
+
import { PublicTxSimulator } from './public_tx_simulator.js';
|
|
7
|
+
/**
|
|
8
|
+
* C++ implementation of PublicTxSimulator using pre-collected hints.
|
|
9
|
+
* This implementation runs TS simulation first to collect all hints,
|
|
10
|
+
* then passes the complete AvmCircuitInputs (hints + public inputs)
|
|
11
|
+
* to C++ to run hinted simulation.
|
|
12
|
+
*/ export class CppPublicTxSimulatorHintedDbs extends PublicTxSimulator {
|
|
13
|
+
log;
|
|
14
|
+
constructor(merkleTree, contractsDB, globalVariables, config, bindings){
|
|
15
|
+
super(merkleTree, contractsDB, globalVariables, config, undefined, bindings);
|
|
16
|
+
this.log = createLogger(`simulator:cpp_public_tx_simulator_hinted_dbs`, bindings);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Simulate a transaction's public portion using the C++ vm2 simulator with hinted DBs.
|
|
20
|
+
*
|
|
21
|
+
* This implementation:
|
|
22
|
+
* 1. Runs the full TypeScript simulation to generate AvmCircuitInputs (hints + public inputs)
|
|
23
|
+
* 2. Passes the complete AvmCircuitInputs to C++ to run hinted simulation
|
|
24
|
+
*
|
|
25
|
+
* @param tx - The transaction to simulate.
|
|
26
|
+
* @returns The result of the transaction's public execution.
|
|
27
|
+
*/ async simulate(tx) {
|
|
28
|
+
const txHash = this.computeTxHash(tx);
|
|
29
|
+
this.log.debug(`C++ hinted DB simulation of ${tx.publicFunctionCalldata.length} public calls for tx ${txHash}`, {
|
|
30
|
+
txHash
|
|
31
|
+
});
|
|
32
|
+
// First, run TS simulation to generate hints and public inputs
|
|
33
|
+
this.log.debug(`Running TS simulation for tx ${txHash}`);
|
|
34
|
+
// Run the full TypeScript simulation using the parent class
|
|
35
|
+
// This will modify the merkle tree with the transaction's state changes
|
|
36
|
+
const tsResult = await super.simulate(tx);
|
|
37
|
+
this.log.debug(`TS simulation succeeded for tx ${txHash}`);
|
|
38
|
+
// Extract the full AvmCircuitInputs from the TS result
|
|
39
|
+
const avmCircuitInputs = new AvmCircuitInputs(tsResult.hints, tsResult.publicInputs);
|
|
40
|
+
// Second, run C++ simulation with hinted DBs
|
|
41
|
+
this.log.debug(`Running C++ simulation with hinted DBs for tx ${txHash}`);
|
|
42
|
+
// Serialize to msgpack and call the C++ simulator
|
|
43
|
+
const inputBuffer = avmCircuitInputs.serializeWithMessagePack();
|
|
44
|
+
let resultBuffer;
|
|
45
|
+
try {
|
|
46
|
+
resultBuffer = await avmSimulateWithHintedDbs(inputBuffer, logLevel);
|
|
47
|
+
} catch (error) {
|
|
48
|
+
throw new SimulationError(`C++ hinted simulation failed: ${error.message}`, []);
|
|
49
|
+
}
|
|
50
|
+
// Deserialize the msgpack result
|
|
51
|
+
const cppResultJSON = deserializeFromMessagePack(resultBuffer);
|
|
52
|
+
const cppResult = PublicTxResult.fromPlainObject(cppResultJSON);
|
|
53
|
+
assert(cppResult.revertCode.equals(tsResult.revertCode));
|
|
54
|
+
assert(cppResult.gasUsed.totalGas.equals(tsResult.gasUsed.totalGas));
|
|
55
|
+
this.log.debug(`C++ hinted simulation completed for tx ${txHash}`, {
|
|
56
|
+
txHash,
|
|
57
|
+
reverted: !tsResult.revertCode.isOK(),
|
|
58
|
+
tsGasUsed: tsResult.gasUsed.totalGas.l2Gas,
|
|
59
|
+
cppGasUsed: tsResult.gasUsed.totalGas.l2Gas
|
|
60
|
+
});
|
|
61
|
+
// TODO(fcarreiro): complete this.
|
|
62
|
+
return tsResult;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Class to record metrics for simulation.
|
|
67
|
+
*
|
|
68
|
+
* Note(dbanks12): We might not be able to collect all the same metrics in C++ as we do in TS!
|
|
69
|
+
* Unless we move some of the metrics collection to C++, we don't have inner functions exposed
|
|
70
|
+
* to TS for tracking.
|
|
71
|
+
*/ export class MeasuredCppPublicTxSimulatorHintedDbs extends CppPublicTxSimulatorHintedDbs {
|
|
72
|
+
metrics;
|
|
73
|
+
constructor(merkleTree, contractsDB, globalVariables, metrics, config, bindings){
|
|
74
|
+
super(merkleTree, contractsDB, globalVariables, config, bindings), this.metrics = metrics;
|
|
75
|
+
}
|
|
76
|
+
async simulate(tx, txLabel = 'unlabeledTx') {
|
|
77
|
+
this.metrics.startRecordingTxSimulation(txLabel);
|
|
78
|
+
let result;
|
|
79
|
+
try {
|
|
80
|
+
result = await super.simulate(tx);
|
|
81
|
+
} finally{
|
|
82
|
+
this.metrics.stopRecordingTxSimulation(txLabel, result?.gasUsed, result?.revertCode);
|
|
83
|
+
}
|
|
84
|
+
return result;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type Logger, type LoggerBindings } from '@aztec/foundation/log';
|
|
2
|
+
import { type PublicSimulatorConfig, PublicTxResult } from '@aztec/stdlib/avm';
|
|
3
|
+
import type { MerkleTreeWriteOperations } from '@aztec/stdlib/trees';
|
|
4
|
+
import type { GlobalVariables, Tx } from '@aztec/stdlib/tx';
|
|
5
|
+
import type { ExecutorMetricsInterface } from '../executor_metrics_interface.js';
|
|
6
|
+
import type { PublicContractsDB } from '../public_db_sources.js';
|
|
7
|
+
import { PublicTxSimulator } from './public_tx_simulator.js';
|
|
8
|
+
import type { MeasuredPublicTxSimulatorInterface, PublicTxSimulatorInterface } from './public_tx_simulator_interface.js';
|
|
9
|
+
/**
|
|
10
|
+
* An implementation of PublicTxSimulator that first simulates in C++, then TS, an compares the results.
|
|
11
|
+
* The C++ simulator accesses the world state directly/natively within C++.
|
|
12
|
+
* For contract DB accesses, it makes callbacks through NAPI back to the TS PublicContractsDB cache.
|
|
13
|
+
*/
|
|
14
|
+
export declare class CppVsTsPublicTxSimulator extends PublicTxSimulator implements PublicTxSimulatorInterface {
|
|
15
|
+
protected log: Logger;
|
|
16
|
+
constructor(merkleTree: MerkleTreeWriteOperations, contractsDB: PublicContractsDB, globalVariables: GlobalVariables, config?: Partial<PublicSimulatorConfig>, bindings?: LoggerBindings);
|
|
17
|
+
/**
|
|
18
|
+
* Simulate a transaction's public portion using the C++ avvm simulator.
|
|
19
|
+
*
|
|
20
|
+
* @param tx - The transaction to simulate.
|
|
21
|
+
* @returns The result of the transaction's public execution.
|
|
22
|
+
*/
|
|
23
|
+
simulate(tx: Tx): Promise<PublicTxResult>;
|
|
24
|
+
}
|
|
25
|
+
export declare class MeasuredCppVsTsPublicTxSimulator extends CppVsTsPublicTxSimulator implements MeasuredPublicTxSimulatorInterface {
|
|
26
|
+
protected readonly metrics: ExecutorMetricsInterface;
|
|
27
|
+
constructor(merkleTree: MerkleTreeWriteOperations, contractsDB: PublicContractsDB, globalVariables: GlobalVariables, metrics: ExecutorMetricsInterface, config?: Partial<PublicSimulatorConfig>, bindings?: LoggerBindings);
|
|
28
|
+
simulate(tx: Tx, txLabel?: string): Promise<PublicTxResult>;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY3BwX3ZzX3RzX3B1YmxpY190eF9zaW11bGF0b3IuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy9wdWJsaWMvcHVibGljX3R4X3NpbXVsYXRvci9jcHBfdnNfdHNfcHVibGljX3R4X3NpbXVsYXRvci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEVBQUUsS0FBSyxNQUFNLEVBQUUsS0FBSyxjQUFjLEVBQTBCLE1BQU0sdUJBQXVCLENBQUM7QUFHakcsT0FBTyxFQUdMLEtBQUsscUJBQXFCLEVBQzFCLGNBQWMsRUFFZixNQUFNLG1CQUFtQixDQUFDO0FBRTNCLE9BQU8sS0FBSyxFQUFFLHlCQUF5QixFQUFFLE1BQU0scUJBQXFCLENBQUM7QUFDckUsT0FBTyxLQUFLLEVBQUUsZUFBZSxFQUFrQixFQUFFLEVBQUUsTUFBTSxrQkFBa0IsQ0FBQztBQUs1RSxPQUFPLEtBQUssRUFBRSx3QkFBd0IsRUFBRSxNQUFNLGtDQUFrQyxDQUFDO0FBQ2pGLE9BQU8sS0FB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EACrC,WAAW,EAAE,iBAAiB,EAC9B,eAAe,EAAE,eAAe,EACb,OAAO,EAAE,wBAAwB,EACpD,MAAM,CAAC,EAAE,OAAO,CAAC,qBAAqB,CAAC,EACvC,QAAQ,CAAC,EAAE,cAAc,EAG1B;IAEqB,QAAQ,CAAC,EAAE,EAAE,EAAE,EAAE,OAAO,GAAE,MAAsB,GAAG,OAAO,CAAC,cAAc,CAAC,CAS/F;CACF"}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { createLogger, logLevel } from '@aztec/foundation/log';
|
|
2
|
+
import { avmSimulate } from '@aztec/native';
|
|
3
|
+
import { ProtocolContractsList } from '@aztec/protocol-contracts';
|
|
4
|
+
import { AvmFastSimulationInputs, AvmTxHint, PublicTxResult, deserializeFromMessagePack } from '@aztec/stdlib/avm';
|
|
5
|
+
import { SimulationError } from '@aztec/stdlib/errors';
|
|
6
|
+
import { WorldStateRevisionWithHandle } from '@aztec/stdlib/world-state';
|
|
7
|
+
import { strict as assert } from 'assert';
|
|
8
|
+
import { ContractProviderForCpp } from './contract_provider_for_cpp.js';
|
|
9
|
+
import { PublicTxSimulator } from './public_tx_simulator.js';
|
|
10
|
+
/**
|
|
11
|
+
* An implementation of PublicTxSimulator that first simulates in C++, then TS, an compares the results.
|
|
12
|
+
* The C++ simulator accesses the world state directly/natively within C++.
|
|
13
|
+
* For contract DB accesses, it makes callbacks through NAPI back to the TS PublicContractsDB cache.
|
|
14
|
+
*/ export class CppVsTsPublicTxSimulator extends PublicTxSimulator {
|
|
15
|
+
log;
|
|
16
|
+
constructor(merkleTree, contractsDB, globalVariables, config, bindings){
|
|
17
|
+
super(merkleTree, contractsDB, globalVariables, config, undefined, bindings);
|
|
18
|
+
this.log = createLogger(`simulator:cpp_vs_public_tx_simulator`, bindings);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Simulate a transaction's public portion using the C++ avvm simulator.
|
|
22
|
+
*
|
|
23
|
+
* @param tx - The transaction to simulate.
|
|
24
|
+
* @returns The result of the transaction's public execution.
|
|
25
|
+
*/ async simulate(tx) {
|
|
26
|
+
const txHash = this.computeTxHash(tx);
|
|
27
|
+
this.log.debug(`C++ simulation of ${tx.publicFunctionCalldata.length} public calls for tx ${txHash}`, {
|
|
28
|
+
txHash
|
|
29
|
+
});
|
|
30
|
+
// Run TS simulation to generate hints and public inputs
|
|
31
|
+
this.log.debug(`Running TS simulation for tx ${txHash}`);
|
|
32
|
+
// create checkpoint for ws
|
|
33
|
+
let tsResult;
|
|
34
|
+
let tsStateRef;
|
|
35
|
+
await this.merkleTree.createCheckpoint();
|
|
36
|
+
this.contractsDB.createCheckpoint();
|
|
37
|
+
try {
|
|
38
|
+
// Run the full TypeScript simulation using the parent class
|
|
39
|
+
// This will modify the merkle tree with the transaction's state changes
|
|
40
|
+
tsResult = await super.simulate(tx);
|
|
41
|
+
this.log.debug(`TS simulation completed for tx ${txHash}`);
|
|
42
|
+
tsStateRef = await this.merkleTree.getStateReference(); // capture tree roots for later comparsion
|
|
43
|
+
} catch (error) {
|
|
44
|
+
this.log.warn(`TS simulation failed, but still continuing with C++ simulation: ${error.message} ${error.stack}`);
|
|
45
|
+
} finally{
|
|
46
|
+
// revert checkpoint for ws and clear contract db changes
|
|
47
|
+
// (cpp should reapply exactly the same changes if there are no bugs)
|
|
48
|
+
await this.merkleTree.revertCheckpoint();
|
|
49
|
+
this.contractsDB.revertCheckpoint();
|
|
50
|
+
}
|
|
51
|
+
this.log.debug(`Running C++ simulation for tx ${txHash}`);
|
|
52
|
+
// Using the "as WorldStateRevisionWithHandle" is a bit of a "trust me bro", hence the assert.
|
|
53
|
+
let wsRevision = this.merkleTree.getRevision();
|
|
54
|
+
assert(wsRevision instanceof WorldStateRevisionWithHandle, 'CppPublicTxSimulator a real NativeWorldStateInstance with a handle to the C++ WorldState object');
|
|
55
|
+
const wsCppHandle = wsRevision.handle;
|
|
56
|
+
wsRevision = wsRevision.toWorldStateRevision(); // for msgpack serialization, we don't include the handle in the type
|
|
57
|
+
this.log.debug(`Running C++ simulation with world state revision ${JSON.stringify(wsRevision)}`);
|
|
58
|
+
// Create the fast simulation inputs
|
|
59
|
+
const txHint = AvmTxHint.fromTx(tx, this.globalVariables.gasFees);
|
|
60
|
+
const protocolContracts = ProtocolContractsList;
|
|
61
|
+
const fastSimInputs = new AvmFastSimulationInputs(wsRevision, this.config, txHint, this.globalVariables, protocolContracts);
|
|
62
|
+
// Create contract provider for callbacks to TypeScript PublicContractsDB from C++
|
|
63
|
+
const contractProvider = new ContractProviderForCpp(this.contractsDB, this.globalVariables, this.bindings);
|
|
64
|
+
// Serialize to msgpack and call the C++ simulator
|
|
65
|
+
this.log.debug(`Serializing fast simulation inputs to msgpack...`);
|
|
66
|
+
const inputBuffer = fastSimInputs.serializeWithMessagePack();
|
|
67
|
+
let resultBuffer;
|
|
68
|
+
try {
|
|
69
|
+
this.log.debug(`Calling C++ simulator for tx ${txHash}`);
|
|
70
|
+
resultBuffer = await avmSimulate(inputBuffer, contractProvider, wsCppHandle, logLevel);
|
|
71
|
+
} catch (error) {
|
|
72
|
+
throw new SimulationError(`C++ simulation failed: ${error.message}`, []);
|
|
73
|
+
}
|
|
74
|
+
// If we've reached this point, C++ succeeded during simulation,
|
|
75
|
+
// so we assert that TS also succeeded.
|
|
76
|
+
assert(tsResult !== undefined, 'TS simulation should have succeeded if C++ succeeded');
|
|
77
|
+
assert(tsStateRef !== undefined, 'TS state reference should have been captured if C++ succeeded');
|
|
78
|
+
// Deserialize the msgpack result
|
|
79
|
+
this.log.debug(`Deserializing C++ from buffer (size: ${resultBuffer.length})...`);
|
|
80
|
+
const cppResultJSON = deserializeFromMessagePack(resultBuffer);
|
|
81
|
+
this.log.debug(`Deserializing C++ result to PublicTxResult...`);
|
|
82
|
+
const cppResult = PublicTxResult.fromPlainObject(cppResultJSON);
|
|
83
|
+
this.log.debug(`Done.`);
|
|
84
|
+
assert(cppResult.revertCode.equals(tsResult.revertCode));
|
|
85
|
+
assert(cppResult.gasUsed.totalGas.equals(tsResult.gasUsed.totalGas));
|
|
86
|
+
assert(cppResult.gasUsed.publicGas.equals(tsResult.gasUsed.publicGas));
|
|
87
|
+
assert(cppResult.gasUsed.teardownGas.equals(tsResult.gasUsed.teardownGas));
|
|
88
|
+
assert(cppResult.gasUsed.billedGas.equals(tsResult.gasUsed.billedGas));
|
|
89
|
+
assert(cppResult.publicTxEffect.equals(tsResult.publicTxEffect));
|
|
90
|
+
if (cppResult.publicInputs !== undefined) {
|
|
91
|
+
assert(cppResult.publicInputs.toBuffer().equals(tsResult.publicInputs.toBuffer()));
|
|
92
|
+
}
|
|
93
|
+
// TODO(fcarreiro): complete this.
|
|
94
|
+
// Check that C++ hints are a strict subset of TS hints.
|
|
95
|
+
// Then enable for misc tests and validate hints.
|
|
96
|
+
//if (this.config?.collectHints) {
|
|
97
|
+
//}
|
|
98
|
+
if (this.config?.collectCallMetadata) {
|
|
99
|
+
assert(cppResult.getAppLogicReturnValues().length === tsResult.getAppLogicReturnValues().length);
|
|
100
|
+
for(let i = 0; i < cppResult.getAppLogicReturnValues().length; i++){
|
|
101
|
+
assert(cppResult.getAppLogicReturnValues()[i].equals(tsResult.getAppLogicReturnValues()[i]));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
// Messages are still not ok for exceptional halts (they are not plumbed in C++).
|
|
105
|
+
const cppRevertReason = cppResult.findRevertReason() || {};
|
|
106
|
+
const tsRevertReason = tsResult.findRevertReason() || {};
|
|
107
|
+
const cppRevertReasonAsObject = JSON.parse(JSON.stringify(cppRevertReason));
|
|
108
|
+
const tsRevertReasonAsObject = JSON.parse(JSON.stringify(tsRevertReason));
|
|
109
|
+
if (JSON.stringify(cppRevertReasonAsObject) !== JSON.stringify(tsRevertReasonAsObject)) {
|
|
110
|
+
this.log.debug('cppResult.findRevertReason()', cppRevertReasonAsObject);
|
|
111
|
+
this.log.debug('tsResult.findRevertReason()', tsRevertReasonAsObject);
|
|
112
|
+
}
|
|
113
|
+
// TODO: dont compare the strings since this is not deterministic.
|
|
114
|
+
// Sometimes error messages are different between C++ and TS, so we omit in the default comparison
|
|
115
|
+
const cppRevertReasonWithoutMessage = {
|
|
116
|
+
...cppRevertReasonAsObject,
|
|
117
|
+
originalMessage: undefined
|
|
118
|
+
};
|
|
119
|
+
const tsRevertReasonWithoutMessage = {
|
|
120
|
+
...tsRevertReasonAsObject,
|
|
121
|
+
originalMessage: undefined
|
|
122
|
+
};
|
|
123
|
+
assert(JSON.stringify(cppRevertReasonWithoutMessage) === JSON.stringify(tsRevertReasonWithoutMessage));
|
|
124
|
+
const cppHasRevertMessage = cppRevertReasonAsObject.originalMessage && cppRevertReasonAsObject.originalMessage.length > 0;
|
|
125
|
+
const tsHasRevertMessage = tsRevertReasonAsObject.originalMessage && tsRevertReasonAsObject.originalMessage.length > 0;
|
|
126
|
+
// assert that if one of the error messages is non-empty, the other is
|
|
127
|
+
assert(cppHasRevertMessage === tsHasRevertMessage, 'One of the AVM simulators (C++ or TS) produced a revert message, but the other did not');
|
|
128
|
+
// Ideally, we'd love to be able to compare full error messages, but without a lot of work
|
|
129
|
+
// the two simulators will always be able to produce some differing errors.
|
|
130
|
+
// Commenting out the code below will enforce that the error messages are at least
|
|
131
|
+
// similar (one contains the other). Even this is not something we can guarantee.
|
|
132
|
+
//if (cppHasRevertMessage) {
|
|
133
|
+
// const cppRevertMessageContainsTs = cppRevertReasonAsObject.originalMessage.includes(
|
|
134
|
+
// tsRevertReasonAsObject.originalMessage,
|
|
135
|
+
// );
|
|
136
|
+
// const tsRevertMessageContainsCpp = tsRevertReasonAsObject.originalMessage.includes(
|
|
137
|
+
// cppRevertReasonAsObject.originalMessage,
|
|
138
|
+
// );
|
|
139
|
+
// assert(
|
|
140
|
+
// cppRevertMessageContainsTs || tsRevertMessageContainsCpp,
|
|
141
|
+
// 'The AVM simulators (C++ and TS) produced different revert messages (neither was a substring of the other)',
|
|
142
|
+
// );
|
|
143
|
+
//}
|
|
144
|
+
// Confirm that tree roots match
|
|
145
|
+
const cppStateRef = await this.merkleTree.getStateReference();
|
|
146
|
+
assert(cppStateRef.equals(tsStateRef), `Tree roots mismatch between TS and C++ public simulations for tx ${txHash}`);
|
|
147
|
+
this.log.debug(`C++ simulation completed for tx ${txHash}`, {
|
|
148
|
+
txHash,
|
|
149
|
+
reverted: !cppResult.revertCode.isOK(),
|
|
150
|
+
cppGasUsed: cppResult.gasUsed.totalGas.l2Gas
|
|
151
|
+
});
|
|
152
|
+
// Return cpp result as it has more detailed metadata / revert reasons
|
|
153
|
+
return cppResult;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
export class MeasuredCppVsTsPublicTxSimulator extends CppVsTsPublicTxSimulator {
|
|
157
|
+
metrics;
|
|
158
|
+
constructor(merkleTree, contractsDB, globalVariables, metrics, config, bindings){
|
|
159
|
+
super(merkleTree, contractsDB, globalVariables, config, bindings), this.metrics = metrics;
|
|
160
|
+
}
|
|
161
|
+
async simulate(tx, txLabel = 'unlabeledTx') {
|
|
162
|
+
this.metrics.startRecordingTxSimulation(txLabel);
|
|
163
|
+
let result;
|
|
164
|
+
try {
|
|
165
|
+
result = await super.simulate(tx);
|
|
166
|
+
} finally{
|
|
167
|
+
this.metrics.stopRecordingTxSimulation(txLabel, result?.gasUsed, result?.revertCode);
|
|
168
|
+
}
|
|
169
|
+
return result;
|
|
170
|
+
}
|
|
171
|
+
}
|