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 <iomanip>
5#include <iostream>
6#include <sys/wait.h>
7#include <unistd.h>
8#include <vector>
9
27
29using namespace bb::avm2;
30using namespace bb::avm2::simulation;
31using namespace bb::avm2::fuzzer;
32using namespace bb::world_state;
33
35
36// Helper function to serialize simulation request via msgpack
38 const GlobalVariables& globals,
40{
41 // Build vectors from contract_db
42 std::vector<ContractClass> classes_vec = contract_db.get_contract_classes();
43 std::vector<std::pair<AztecAddress, ContractInstance>> instances_vec = contract_db.get_contract_instances();
44
48 .tx = tx,
49 .globals = globals,
50 .contract_classes = std::move(classes_vec),
51 .contract_instances = std::move(instances_vec),
52 };
53
54 auto [buffer, size] = msgpack_encode_buffer(request);
55 std::string result = base64_encode(buffer, size);
56 delete[] buffer;
57 return result;
58}
59
60// Helper function to create default global variables for testing
62{
63 return GlobalVariables{
65 .version = VERSION,
66 .block_number = BLOCK_NUMBER,
67 .slot_number = SLOT_NUMBER,
68 .timestamp = TIMESTAMP,
69 .coinbase = COINBASE,
70 .fee_recipient = FEE_RECIPIENT,
71 .gas_fees = GasFees{ .fee_per_da_gas = FEE_PER_DA_GAS, .fee_per_l2_gas = FEE_PER_L2_GAS },
72 };
73}
74
77 const Tx& tx)
78{
79
80 const PublicSimulatorConfig config{
81 .skip_fee_enforcement = false,
82 .collect_call_metadata = true,
83 .collect_public_inputs = true,
84 .collection_limits = {
85 .max_returndata_size_in_fields = MAX_RETURN_DATA_SIZE_IN_FIELDS,
86 },
87 };
88
89 ProtocolContracts protocol_contracts{};
90
91 auto globals = create_default_globals();
92
95
97 TxSimulationResult result =
98 helper.simulate_fast_with_existing_ws(contract_db, ws_rev, ws, config, tx, globals, protocol_contracts);
99 bool reverted = result.revert_code != RevertCode::OK;
100 // Just process the top level call's output
101 vinfo(
102 "C++ Simulator result - reverted: ", reverted, ", output size: ", result.call_stack_metadata[0].output.size());
103 std::vector<FF> values = {};
104 if (result.call_stack_metadata.size() != 0) {
105 for (const auto& metadata : result.call_stack_metadata) {
106 // Only collect outputs from APP_LOGIC phase (matches TypeScript getAppLogicReturnValues())
107 if (metadata.phase == CoarseTransactionPhase::APP_LOGIC) {
108 for (const auto& output : metadata.output) {
109 values.push_back(output);
110 }
111 }
112 }
113 }
114 if (result.public_inputs.has_value()) {
115 return { .reverted = reverted,
116 .output = values,
117 .end_tree_snapshots = result.public_inputs->end_tree_snapshots };
118 }
119 return { .reverted = reverted, .output = values };
120}
121
123JsSimulator::JsSimulator(std::string& simulator_path)
124 : simulator_path(simulator_path)
125 , process("LOG_LEVEL=silent node " + simulator_path + " 2>/dev/null")
126{}
127
129{
130 if (instance == nullptr) {
131 throw std::runtime_error("JsSimulator should be initializing in FUZZ INIT");
132 }
133 return instance;
134}
135
138void JsSimulator::initialize(std::string& simulator_path)
139{
140 if (instance != nullptr) {
141 throw std::runtime_error("JsSimulator already initialized");
142 }
144}
145
148 const Tx& tx)
149{
150 auto globals = create_default_globals();
151
152 std::string serialized = serialize_simulation_request(tx, globals, contract_db);
153
154 // Send the request
155 process.write_line(serialized);
156 std::string response = process.read_line();
157 while (response.empty()) {
158 std::cout << "Empty response, reading again" << std::endl;
159 std::this_thread::sleep_for(std::chrono::milliseconds(10));
160 response = process.read_line();
161 }
162 // Remove the newline character
163 response.erase(response.find_last_not_of('\n') + 1);
164
165 // Parse with msg_pack
166 auto res_buffer = base64_decode(response);
167 SimulatorResult result;
168 result = msgpack::unpack(res_buffer.data(), res_buffer.size()).get().convert(result);
169 return result;
170}
171
173{
174 // Since the simulator results are interchangeable between TS and C++, we limit the return data size for comparison
175 // todo(ilyas): we ideally specfify one param as the TS result and truncate only that one
176 if (result1.output.size() > MAX_RETURN_DATA_SIZE_IN_FIELDS) {
178 }
179 if (result2.output.size() > MAX_RETURN_DATA_SIZE_IN_FIELDS) {
181 }
182
183 return result1.reverted == result2.reverted && result1.output == result2.output &&
184 result1.end_tree_snapshots == result2.end_tree_snapshots;
185}
186
187// Creates a default transaction that the single app logic enqueued call can be inserted into
189 const AztecAddress& sender_address,
190 const std::vector<FF>& calldata,
191 [[maybe_unused]] const FF& transaction_fee,
192 bool is_static_call,
193 const Gas& gas_limit)
194{
195 return Tx{
197 .gas_settings = GasSettings{
198 .gas_limits = gas_limit,
199 .max_fees_per_gas = GasFees{ .fee_per_da_gas = FEE_PER_DA_GAS, .fee_per_l2_gas = FEE_PER_L2_GAS },
200 },
201 .effective_gas_fees = EFFECTIVE_GAS_FEES,
202 .non_revertible_accumulated_data = AccumulatedData{
204 // This nullifier is needed to make the nonces for note hashes and expected by simulation_helper
207 },
208 .revertible_accumulated_data = AccumulatedData{
212 },
213 .setup_enqueued_calls = SETUP_ENQUEUED_CALLS,
214 .app_logic_enqueued_calls = {
218 .contract_address = contract_address,
219 .is_static_call = is_static_call,
220 .calldata_hash = compute_calldata_hash(calldata),
221 },
222 .calldata = calldata,
223 },
224 },
225 .teardown_enqueued_call = TEARDOWN_ENQUEUED_CALLS,
226 .gas_used_by_private = GAS_USED_BY_PRIVATE,
227 .fee_payer = sender_address,
228 };
229}
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
std::string base64_decode(std::string const &s, bool remove_linebreaks)
Definition base64.cpp:249
std::string base64_encode(unsigned char const *bytes_to_encode, size_t in_len, bool url)
Definition base64.cpp:117
StrictMock< MockContractDB > contract_db
SimulatorResult simulate(fuzzer::FuzzerWorldStateManager &ws_mgr, fuzzer::FuzzerContractDB &contract_db, const Tx &tx) override
Definition simulator.cpp:75
uses the yarn-project/simulator to simulate the bytecode Singleton, because initializing the simulato...
Definition simulator.hpp:63
static JsSimulator * getInstance()
static JsSimulator * instance
Definition simulator.hpp:65
std::string simulator_path
Definition simulator.hpp:66
static void initialize(std::string &simulator_path)
JsSimulator(std::string &simulator_path)
SimulatorResult simulate(fuzzer::FuzzerWorldStateManager &ws_mgr, fuzzer::FuzzerContractDB &contract_db, const Tx &tx) override
Process process
Definition simulator.hpp:68
std::string read_line() const
Reads a line from the process.
Definition process.cpp:70
void write_line(const std::string &line) const
Ends line with a newline character, sends to the process.
Definition process.cpp:49
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:94
static const char * get_data_dir()
Definition dbs.hpp:102
world_state::WorldStateRevision get_current_revision() const
Definition dbs.cpp:199
Holds the Merkle trees responsible for storing the state of the Aztec protocol.
#define vinfo(...)
Definition log.hpp:94
uint8_t buffer[RANDOM_BUFFER_SIZE]
Definition engine.cpp:34
FuzzerWorldStateManager * ws_mgr
Definition fuzz.test.cpp:16
std::pair< uint8_t *, size_t > msgpack_encode_buffer(auto &&obj, uint8_t *scratch_buf=nullptr, size_t scratch_size=0)
FF compute_calldata_hash(std::span< const FF > calldata)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
bool compare_simulator_results(SimulatorResult &result1, SimulatorResult &result2)
const auto MAX_RETURN_DATA_SIZE_IN_FIELDS
Definition simulator.cpp:34
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)
std::string serialize_simulation_request(const Tx &tx, const GlobalVariables &globals, const FuzzerContractDB &contract_db)
Definition simulator.cpp:37
GlobalVariables create_default_globals()
Definition simulator.cpp:61
GlobalVariables create_default_globals()
Definition simulator.cpp:61
TreeSnapshots end_tree_snapshots
Definition simulator.hpp:34
std::vector< FF > output
Definition simulator.hpp:33
std::vector< FF > note_hashes
Definition avm_io.hpp:318
uint128_t fee_per_da_gas
std::string hash
Definition avm_io.hpp:330
std::vector< CallStackMetadata > call_stack_metadata
Definition avm_io.hpp:554
std::optional< PublicInputs > public_inputs
Definition avm_io.hpp:557