Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
avm_execute.cpp
Go to the documentation of this file.
11
12namespace bb::avm {
13
14using namespace bb::avm2;
15using namespace bb::world_state;
16
17// Global cancellation token for the currently active simulation.
18// Set before simulation starts, cleared after. SIGUSR1 handler reads this to cancel.
19// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
21
22// ---------------------------------------------------------------------------
23// Helper: serialize a value to msgpack bytes
24// ---------------------------------------------------------------------------
25
26template <typename T> static std::vector<uint8_t> serialize_to_msgpack(const T& value)
27{
28 msgpack::sbuffer buf;
29 msgpack::pack(buf, value);
30 return std::vector<uint8_t>(buf.data(), buf.data() + buf.size());
31}
32
33template <typename T> static T deserialize_from_msgpack(const std::vector<uint8_t>& bytes)
34{
35 auto unpacked = msgpack::unpack(reinterpret_cast<const char*>(bytes.data()), bytes.size());
36 T value;
37 unpacked.get().convert(value);
38 return value;
39}
40
41// ---------------------------------------------------------------------------
42// Top-level dispatch
43// ---------------------------------------------------------------------------
44
46{
47 return execute(request, std::move(command));
48}
49
50// ---------------------------------------------------------------------------
51// AvmSimulate
52// ---------------------------------------------------------------------------
53
55{
56 // Deserialize AvmFastSimulationInputs from opaque bytes
57 auto sim_inputs = deserialize_from_msgpack<AvmFastSimulationInputs>(inputs);
58
59 // If a fork ID was provided (block builder's fork), use it directly.
60 // Otherwise create a temporary fork for this simulation.
61 const bool use_external_fork = sim_inputs.ws_revision.forkId != 0;
62 uint64_t fork_id = sim_inputs.ws_revision.forkId;
63
64 if (!use_external_fork) {
65 auto fork_resp = request.wsdb_client.create_fork(wsdb::WsdbCreateFork{ .latest = true, .blockNumber = 0 });
66 fork_id = fork_resp.forkId;
67 vinfo("Created WSDB fork ", fork_id, " for AVM simulation");
68 } else {
69 vinfo("Using external WSDB fork ", fork_id, " for AVM simulation");
70 }
71
72 // Route CDB requests to the correct PublicContractsDB via fork ID
73 request.cdb_client.set_fork_id(fork_id);
74
75 // Create a cancellation token for this simulation and expose it globally
76 // so the SIGUSR1 handler can signal cancellation from TypeScript.
78 g_active_cancellation_token.store(cancellation_token.get(), std::memory_order_release);
79
80 try {
81 // Create revision pointing to the fork
82 WorldStateRevision revision = {
83 .forkId = fork_id,
84 .blockNumber = 0,
85 .includeUncommitted = true,
86 };
87
88 // Create IPC-backed MerkleDB and ContractDB
89 WsdbIpcMerkleDB merkle_db(request.wsdb_client, revision);
90
91 // Run simulation using the helper that takes raw DB interfaces.
92 // Route to hint collection or fast path based on config.
93 AvmSimulationHelper simulation_helper;
94 auto result = sim_inputs.config.collect_hints
95 ? simulation_helper.simulate_for_hint_collection_internal(request.cdb_client,
97 sim_inputs.config,
98 sim_inputs.tx,
99 sim_inputs.global_variables,
100 sim_inputs.protocol_contracts,
101 cancellation_token)
102 : simulation_helper.simulate_fast_internal(request.cdb_client,
103 merkle_db,
104 sim_inputs.config,
105 sim_inputs.tx,
106 sim_inputs.global_variables,
107 sim_inputs.protocol_contracts,
108 cancellation_token);
109
111
112 // Only clean up fork if we created it
113 if (!use_external_fork) {
114 request.wsdb_client.delete_fork(wsdb::WsdbDeleteFork{ .forkId = fork_id });
115 }
116
117 return Response{ .result = serialize_to_msgpack(result) };
118 } catch (...) {
120
121 // Only clean up fork on error if we created it
122 if (!use_external_fork) {
123 try {
124 request.wsdb_client.delete_fork(wsdb::WsdbDeleteFork{ .forkId = fork_id });
125 } catch (...) {
126 // Ignore cleanup errors
127 }
128 }
129 throw;
130 }
131}
132
133// ---------------------------------------------------------------------------
134// AvmSimulateWithHints
135// ---------------------------------------------------------------------------
136
138{
139 (void)request;
140
141 // Deserialize AvmProvingInputs from opaque bytes
142 auto proving_inputs = deserialize_from_msgpack<AvmProvingInputs>(inputs);
143
144 // Run simulation with hinted DBs (self-contained, no external DB needed)
145 AvmSimAPI api;
146 auto result = api.simulate_with_hinted_dbs(proving_inputs);
147
148 return Response{ .result = serialize_to_msgpack(result) };
149}
150
151// ---------------------------------------------------------------------------
152// AvmShutdown
153// ---------------------------------------------------------------------------
154
156{
157 (void)request;
158 return Response{};
159}
160
161} // namespace bb::avm
AvmCommand NamedUnion, AvmRequest context, and dispatch function.
StrictMock< MockHighLevelMerkleDB > merkle_db
A wrapper around std::variant that provides msgpack serialization based on type names.
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)
#define vinfo(...)
Definition log.hpp:94
AvmProvingInputs inputs
AvmCommandResponse execute(AvmRequest &request, AvmCommand &&command)
Execute an AVM command using the visitor pattern.
std::atomic< avm2::simulation::CancellationToken * > g_active_cancellation_token
AvmCommandResponse avm_dispatch(AvmRequest &request, AvmCommand &&command)
Top-level AVM API entry point. Takes an AvmRequest and dispatches the command.
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.
Response execute(AvmRequest &request) &&
std::vector< uint8_t > result
Response execute(AvmRequest &request) &&
Response execute(AvmRequest &request) &&
NamedUnion command structs for the aztec-wsdb world state database API.
LowLevelMerkleDBInterface implementation backed by WSDB IPC.