6#include <nlohmann/json.hpp>
33using json = nlohmann::json;
45 std::vector<std::string> calldata_strings;
46 calldata_strings.reserve(
calldata.size());
50 j[
"inputs"] = calldata_strings;
65 delete[] globals_buffer;
89 [[maybe_unused]]
const FF& transaction_fee,
113 .app_logic_enqueued_calls = {
118 .is_static_call = is_static_call,
126 .fee_payer = sender_address,
131 const std::vector<uint8_t>&
bytecode,
150 bool reverted = result.
revert_code != RevertCode::OK;
153 "C++ Simulator result - reverted: ", reverted,
", output size: ", result.
call_stack_metadata[0].output.size());
154 std::vector<FF> values = {};
157 values.push_back(output);
161 return { .reverted = reverted,
163 .end_tree_snapshots = result.
public_inputs->end_tree_snapshots };
165 return { .reverted = reverted, .output = values };
170 : simulator_path(simulator_path)
171 , process(
"LOG_LEVEL=silent node " + simulator_path +
" 2>/dev/null")
177 throw std::runtime_error(
"JsSimulator should be initializing in FUZZ INIT");
187 throw std::runtime_error(
"JsSimulator already initialized");
193 const std::vector<uint8_t>&
bytecode,
196 bool logging_enabled =
std::getenv(
"AVM_FUZZER_LOGGING") !=
nullptr;
203 if (logging_enabled) {
204 info(
"Sending request to simulator: ", serialized);
210 while (response.empty()) {
212 std::this_thread::sleep_for(std::chrono::milliseconds(10));
216 response.erase(response.find_last_not_of(
'\n') + 1);
219 if (logging_enabled) {
220 info(
"Received response from simulator: ", response);
222 json response_json = json::parse(response);
223 bool reverted = response_json[
"reverted"];
224 std::vector<std::string> output = response_json[
"output"];
225 std::string revert_reason = response_json.value(
"revertReason",
"");
226 std::vector<FF> output_fields;
227 output_fields.reserve(output.size());
228 for (
const auto&
field : output) {
229 output_fields.push_back(
FF(
field));
234 if (response_json.contains(
"endTreeSnapshots")) {
235 const auto& ets = response_json[
"endTreeSnapshots"];
237 .
root =
FF(ets[
"l1ToL2MessageTree"][
"root"].get<std::string>()),
238 .next_available_leaf_index = ets[
"l1ToL2MessageTree"][
"nextAvailableLeafIndex"].get<uint64_t>(),
241 .
root =
FF(ets[
"noteHashTree"][
"root"].get<std::string>()),
242 .next_available_leaf_index = ets[
"noteHashTree"][
"nextAvailableLeafIndex"].get<uint64_t>(),
245 .
root =
FF(ets[
"nullifierTree"][
"root"].get<std::string>()),
246 .next_available_leaf_index = ets[
"nullifierTree"][
"nextAvailableLeafIndex"].get<uint64_t>(),
249 .
root =
FF(ets[
"publicDataTree"][
"root"].get<std::string>()),
250 .next_available_leaf_index = ets[
"publicDataTree"][
"nextAvailableLeafIndex"].get<uint64_t>(),
256 .output = output_fields,
257 .end_tree_snapshots = end_tree_snapshots,
258 .revert_reason = revert_reason,
const std::optional< PublicCallRequestWithCalldata > TEARDOWN_ENQUEUED_CALLS
const uint128_t FEE_PER_DA_GAS
const std::vector< ScopedL2ToL1Message > NON_REVERTIBLE_ACCUMULATED_DATA_L2_TO_L1_MESSAGES
const uint32_t BLOCK_NUMBER
const std::vector< FF > NON_REVERTIBLE_ACCUMULATED_DATA_NULLIFIERS
const AztecAddress FEE_RECIPIENT
const std::vector< FF > NON_REVERTIBLE_ACCUMULATED_DATA_NOTE_HASHES
const std::vector< PublicCallRequestWithCalldata > SETUP_ENQUEUED_CALLS
const EthAddress COINBASE
const std::string TRANSACTION_HASH
const std::vector< ScopedL2ToL1Message > REVERTIBLE_ACCUMULATED_DATA_L2_TO_L1_MESSAGES
const Gas GAS_USED_BY_PRIVATE
const std::vector< FF > REVERTIBLE_ACCUMULATED_DATA_NOTE_HASHES
const FF CONTRACT_ADDRESS
const std::vector< FF > REVERTIBLE_ACCUMULATED_DATA_NULLIFIERS
const uint128_t FEE_PER_L2_GAS
const bool IS_STATIC_CALL
const GasFees EFFECTIVE_GAS_FEES
std::shared_ptr< Napi::ThreadSafeFunction > bytecode
std::string base64_encode(unsigned char const *bytes_to_encode, size_t in_len, bool url)
SimulatorResult simulate(fuzzer::FuzzerWorldStateManager &ws_mgr, const std::vector< uint8_t > &bytecode, const std::vector< FF > &calldata) override
uses the yarn-project/simulator to simulate the bytecode Singleton, because initializing the simulato...
static JsSimulator * getInstance()
static JsSimulator * instance
std::string simulator_path
SimulatorResult simulate(fuzzer::FuzzerWorldStateManager &ws_mgr, const std::vector< uint8_t > &bytecode, const std::vector< FF > &calldata) override
static void initialize(std::string &simulator_path)
JsSimulator(std::string &simulator_path)
std::string read_line() const
Reads a line from the process.
void write_line(const std::string &line) const
Ends line with a newline character, sends to the process.
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)
world_state::WorldState & get_world_state()
static const char * get_data_dir()
world_state::WorldStateRevision get_current_revision() const
static uint64_t get_map_size_kb()
Holds the Merkle trees responsible for storing the state of the Aztec protocol.
AztecAddress contract_address
std::pair< uint8_t *, size_t > msgpack_encode_buffer(auto &&obj, uint8_t *scratch_buf=nullptr, size_t scratch_size=0)
std::string field_to_string(const FF &ff)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
bool compare_simulator_results(const SimulatorResult &result1, const SimulatorResult &result2)
std::string serialize_simulation_request(const std::vector< uint8_t > &bytecode, const std::vector< FF > &calldata, const Tx &tx, const GlobalVariables &globals)
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)
GlobalVariables create_default_globals()
TreeSnapshots end_tree_snapshots
std::vector< FF > note_hashes
PublicCallRequest request
bool collect_call_metadata
AppendOnlyTreeSnapshot public_data_tree
AppendOnlyTreeSnapshot l1_to_l2_message_tree
AppendOnlyTreeSnapshot nullifier_tree
AppendOnlyTreeSnapshot note_hash_tree
std::vector< CallStackMetadata > call_stack_metadata
std::optional< PublicInputs > public_inputs
General class for prime fields see Prime field documentation["field documentation"] for general imple...