Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
avm_execute.cpp
Go to the documentation of this file.
10
11namespace bb::avm {
12
13using namespace bb::avm2;
14using namespace bb::world_state;
15
16// Cancellation for the single in-flight simulation. bb-avm-sim runs exactly one
17// simulation at a time; the SIGUSR1 handler (which may run on any thread) cancels
18// it through g_active_cancellation_token.
19//
20// The token is process-lifetime and never freed. A per-request token would let
21// the signal handler dereference a pointer to a token the completing request had
22// already freed (use-after-free), since the handler can run concurrently with the
23// request thread unwinding. g_active_cancellation_token points at this token only
24// while a simulation runs and is null otherwise, so a signal between simulations
25// is a safe no-op.
26//
27// NOTE: cancellation is process-scoped (a signal), not request-scoped. A signal
28// delivered late — after its target finished and the next simulation began on this
29// process — would cancel the wrong simulation. The pool runs one simulation per
30// process and signals only the in-flight one, so this isn't exercised today;
31// hardening it would need a request-scoped cancel channel rather than a signal.
34// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
36
37template <typename T> static std::vector<uint8_t> serialize_to_msgpack(const T& value)
38{
39 msgpack::sbuffer buf;
40 msgpack::pack(buf, value);
41 return std::vector<uint8_t>(buf.data(), buf.data() + buf.size());
42}
43
44template <typename T> static T deserialize_from_msgpack(const std::vector<uint8_t>& bytes)
45{
46 auto unpacked = msgpack::unpack(reinterpret_cast<const char*>(bytes.data()), bytes.size());
47 T value;
48 unpacked.get().convert(value);
49 return value;
50}
51
52template <>
53void handle_simulate(AvmRequest& request, wire::AvmSimulate&& command, Responder<wire::AvmSimulateResponse> respond)
54{
55 // Reuse the process-lifetime token (cleared of any prior cancellation) instead
56 // of allocating a per-request one the signal handler could outlive.
59 try {
60 auto sim_inputs = deserialize_from_msgpack<AvmFastSimulationInputs>(command.inputs);
61
62 // The caller owns fork lifecycle and registers the matching contracts DB
63 // on the CDB server before invoking AvmSimulate. Fork ID 0 is valid.
64 uint64_t fork_id = sim_inputs.ws_revision.forkId;
65 vinfo("Using external WSDB fork ", fork_id, " for AVM simulation");
66
67 request.cdb_client.set_fork_id(fork_id);
68
69 g_active_cancellation_token.store(cancellation_token.get(), std::memory_order_release);
70
71 // Create revision pointing to the fork. blockNumber = LATEST sentinel routes the WSDB
72 // through its non-historical path so the fork's uncommitted leaves are visible.
73 WorldStateRevision revision = {
74 .forkId = fork_id,
75 .blockNumber = WorldStateRevision::LATEST,
76 .includeUncommitted = true,
77 };
78
80
81 AvmSimulationHelper simulation_helper;
82 auto result = sim_inputs.config.collect_hints
83 ? simulation_helper.simulate_for_hint_collection_internal(request.cdb_client,
85 sim_inputs.config,
86 sim_inputs.tx,
87 sim_inputs.global_variables,
88 sim_inputs.protocol_contracts,
89 cancellation_token)
90 : simulation_helper.simulate_fast_internal(request.cdb_client,
92 sim_inputs.config,
93 sim_inputs.tx,
94 sim_inputs.global_variables,
95 sim_inputs.protocol_contracts,
96 cancellation_token);
97
99
100 respond.ok(wire::AvmSimulateResponse{ .result = serialize_to_msgpack(result) });
101 } catch (const std::exception& e) {
103 respond.error(e.what());
104 }
105}
106
107template <>
109 wire::AvmSimulateWithHints&& command,
110 Responder<wire::AvmSimulateWithHintsResponse> respond)
111{
112 (void)request;
113
114 try {
115 auto proving_inputs = deserialize_from_msgpack<AvmProvingInputs>(command.inputs);
116
117 AvmSimAPI api;
118 auto result = api.simulate_with_hinted_dbs(proving_inputs);
119
120 respond.ok(wire::AvmSimulateWithHintsResponse{ .result = serialize_to_msgpack(result) });
121 } catch (const std::exception& e) {
122 respond.error(e.what());
123 }
124}
125
126} // namespace bb::avm
AVM IPC handler context.
StrictMock< MockHighLevelMerkleDB > merkle_db
TxSimulationResult simulate_with_hinted_dbs(const AvmProvingInputs &inputs)
TxSimulationResult simulate_fast_internal(simulation::ContractDBInterface &raw_contract_db, simulation::LowLevelMerkleDBInterface &raw_merkle_db, const PublicSimulatorConfig &config, const Tx &tx, const GlobalVariables &global_variables, const ProtocolContracts &protocol_contracts, simulation::CancellationTokenPtr cancellation_token=nullptr)
TxSimulationResult simulate_for_hint_collection_internal(simulation::ContractDBInterface &raw_contract_db, simulation::LowLevelMerkleDBInterface &raw_merkle_db, const PublicSimulatorConfig &config, const Tx &tx, const GlobalVariables &global_variables, const ProtocolContracts &protocol_contracts, simulation::CancellationTokenPtr cancellation_token=nullptr)
void set_fork_id(uint64_t fork_id)
#define vinfo(...)
Definition log.hpp:94
std::shared_ptr< CancellationToken > CancellationTokenPtr
const avm2::simulation::CancellationTokenPtr g_sim_cancellation_token
void handle_simulate_with_hints(AvmRequest &request, wire::AvmSimulateWithHints &&command, Responder< wire::AvmSimulateWithHintsResponse > respond)
std::atomic< avm2::simulation::CancellationToken * > g_active_cancellation_token
void handle_simulate(AvmRequest &request, wire::AvmSimulate &&command, Responder< wire::AvmSimulateResponse > respond)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
Context passed to each command's execute() method. Provides access to WSDB and CDB IPC clients.
cdb::CdbIpcContractDB & cdb_client
wsdb::WsdbIpcClient & wsdb_client
static constexpr block_number_t LATEST
Definition types.hpp:42
VectorField result
LowLevelMerkleDBInterface implementation backed by WSDB IPC.