Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
simulator.cpp
Go to the documentation of this file.
2
3#include <cstdint>
4#include <vector>
5
21
23using namespace bb::avm2;
24using namespace bb::avm2::simulation;
25using namespace bb::avm2::fuzzer;
26using namespace bb::world_state;
27
28constexpr auto MAX_RETURN_DATA_SIZE_IN_FIELDS = 1024;
29
30// Helper function to create default global variables for testing
32{
33 return GlobalVariables{
35 .version = VERSION,
36 .block_number = BLOCK_NUMBER,
37 .slot_number = SLOT_NUMBER,
38 .timestamp = TIMESTAMP,
39 .coinbase = COINBASE,
40 .fee_recipient = FEE_RECIPIENT,
41 .gas_fees = GasFees{ .fee_per_da_gas = FEE_PER_DA_GAS, .fee_per_l2_gas = FEE_PER_L2_GAS },
42 };
43}
44
48 const Tx& tx,
49 const GlobalVariables& globals,
50 [[maybe_unused]] const std::vector<bb::crypto::merkle_tree::PublicDataLeafValue>& public_data_writes,
51 [[maybe_unused]] const std::vector<FF>& note_hashes,
52 const ProtocolContracts& protocol_contracts)
53{
54 // Note: public_data_writes and note_hashes are already applied to C++ world state in setup_fuzzer_state
55
56 const PublicSimulatorConfig config{
57 .skip_fee_enforcement = false,
58 .collect_call_metadata = true,
59 .collect_public_inputs = true,
60 .collection_limits = {
61 .max_returndata_size_in_fields = MAX_RETURN_DATA_SIZE_IN_FIELDS,
62 },
63 };
64
67
69 TxSimulationResult result =
70 helper.simulate_fast_with_existing_ws(contract_db, ws_rev, ws, config, tx, globals, protocol_contracts);
71 bool reverted = result.revert_code != RevertCode::OK;
72 // Just process the top level call's output
73 vinfo(
74 "C++ Simulator result - reverted: ", reverted, ", output size: ", result.call_stack_metadata[0].output.size());
75 std::vector<FF> values = {};
76 if (result.call_stack_metadata.size() != 0) {
77 for (const auto& metadata : result.call_stack_metadata) {
78 // Only collect outputs from APP_LOGIC phase (matches TypeScript getAppLogicReturnValues())
79 if (metadata.phase == CoarseTransactionPhase::APP_LOGIC) {
80 for (const auto& output : metadata.output) {
81 values.push_back(output);
82 }
83 }
84 }
85 }
86 if (result.public_inputs.has_value()) {
87 return { .reverted = reverted,
88 .output = values,
89 .end_tree_snapshots = result.public_inputs->end_tree_snapshots,
90 .public_tx_effect = result.public_tx_effect };
91 }
92 return { .reverted = reverted, .output = values, .public_tx_effect = result.public_tx_effect };
93}
94
95// Creates a default transaction that the single app logic enqueued call can be inserted into
96Tx create_default_tx(const AztecAddress& contract_address,
97 const AztecAddress& sender_address,
98 const std::vector<FF>& calldata,
99 [[maybe_unused]] const FF& transaction_fee,
100 bool is_static_call,
101 const Gas& gas_limit)
102{
103 return Tx{
105 .gas_settings = GasSettings{
106 .gas_limits = gas_limit,
107 .max_fees_per_gas = GasFees{ .fee_per_da_gas = FEE_PER_DA_GAS, .fee_per_l2_gas = FEE_PER_L2_GAS },
108 },
109 .effective_gas_fees = EFFECTIVE_GAS_FEES,
110 .non_revertible_accumulated_data = AccumulatedData{
112 // This nullifier is needed to make the nonces for note hashes and expected by simulation_helper
115 },
116 .revertible_accumulated_data = AccumulatedData{
120 },
121 .setup_enqueued_calls = SETUP_ENQUEUED_CALLS,
122 .app_logic_enqueued_calls = {
126 .contract_address = contract_address,
127 .is_static_call = is_static_call,
128 .calldata_hash = compute_calldata_hash(calldata),
129 },
130 .calldata = calldata,
131 },
132 },
133 .teardown_enqueued_call = TEARDOWN_ENQUEUED_CALLS,
134 .gas_used_by_private = GAS_USED_BY_PRIVATE,
135 .fee_payer = sender_address,
136 };
137}
const std::optional< PublicCallRequestWithCalldata > TEARDOWN_ENQUEUED_CALLS
Definition constants.hpp:34
const std::vector< ScopedL2ToL1Message > NON_REVERTIBLE_ACCUMULATED_DATA_L2_TO_L1_MESSAGES
Definition constants.hpp:28
const uint32_t BLOCK_NUMBER
Definition constants.hpp:16
const std::vector< FF > NON_REVERTIBLE_ACCUMULATED_DATA_NULLIFIERS
Definition constants.hpp:26
const AztecAddress FEE_RECIPIENT
Definition constants.hpp:20
const std::vector< FF > NON_REVERTIBLE_ACCUMULATED_DATA_NOTE_HASHES
Definition constants.hpp:27
constexpr GasFees EFFECTIVE_GAS_FEES
Definition constants.hpp:24
const std::vector< PublicCallRequestWithCalldata > SETUP_ENQUEUED_CALLS
Definition constants.hpp:32
const EthAddress COINBASE
Definition constants.hpp:19
const FF MSG_SENDER
Definition constants.hpp:33
const FF SLOT_NUMBER
Definition constants.hpp:17
const std::string TRANSACTION_HASH
Definition constants.hpp:23
const FF CHAIN_ID
Definition constants.hpp:14
constexpr uint128_t FEE_PER_DA_GAS
Definition constants.hpp:21
const std::vector< ScopedL2ToL1Message > REVERTIBLE_ACCUMULATED_DATA_L2_TO_L1_MESSAGES
Definition constants.hpp:31
const Gas GAS_USED_BY_PRIVATE
Definition constants.hpp:35
const std::vector< FF > REVERTIBLE_ACCUMULATED_DATA_NOTE_HASHES
Definition constants.hpp:30
const std::vector< FF > REVERTIBLE_ACCUMULATED_DATA_NULLIFIERS
Definition constants.hpp:29
constexpr uint128_t FEE_PER_L2_GAS
Definition constants.hpp:22
StrictMock< MockContractDB > contract_db
SimulatorResult simulate(fuzzer::FuzzerWorldStateManager &ws_mgr, fuzzer::FuzzerContractDB &contract_db, const Tx &tx, const GlobalVariables &globals, const std::vector< bb::crypto::merkle_tree::PublicDataLeafValue > &public_data_writes, const std::vector< FF > &note_hashes, const ProtocolContracts &protocol_contracts) override
Definition simulator.cpp:45
TxSimulationResult simulate_fast_with_existing_ws(simulation::ContractDBInterface &raw_contract_db, const world_state::WorldStateRevision &world_state_revision, world_state::WorldState &ws, const PublicSimulatorConfig &config, const Tx &tx, const GlobalVariables &global_variables, const ProtocolContracts &protocol_contracts, simulation::CancellationTokenPtr cancellation_token=nullptr)
world_state::WorldState & get_world_state()
Definition dbs.hpp:96
world_state::WorldStateRevision get_current_revision() const
Definition dbs.cpp:206
Holds the Merkle trees responsible for storing the state of the Aztec protocol.
#define vinfo(...)
Definition log.hpp:94
FuzzerWorldStateManager * ws_mgr
Definition fuzz.test.cpp:15
AVM range check gadget for witness generation.
FF compute_calldata_hash(std::span< const FF > calldata)
Tx create_default_tx(const AztecAddress &contract_address, const AztecAddress &sender_address, const std::vector< FF > &calldata, const FF &transaction_fee, bool is_static_call, const Gas &gas_limit)
Definition simulator.cpp:96
GlobalVariables create_default_globals()
Definition simulator.cpp:31
constexpr auto MAX_RETURN_DATA_SIZE_IN_FIELDS
Definition simulator.cpp:28
std::vector< FF > note_hashes
Definition avm_io.hpp:320
uint128_t fee_per_da_gas
std::string hash
Definition avm_io.hpp:332
PublicTxEffect public_tx_effect
Definition avm_io.hpp:554
std::vector< CallStackMetadata > call_stack_metadata
Definition avm_io.hpp:556
std::optional< PublicInputs > public_inputs
Definition avm_io.hpp:559