Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
avm_ipc_server.cpp
Go to the documentation of this file.
5#include "barretenberg/wsdb/generated/wsdb_ipc_client.hpp"
6#include "ipc_runtime/ipc_server.hpp"
7#include "ipc_runtime/serve_helper.hpp"
8#include "ipc_runtime/signal_handlers.hpp"
9
10#include <chrono>
11#include <csignal>
12#include <memory>
13#include <string>
14#include <thread>
15#include <vector>
16
17namespace bb::avm {
18
19int execute_avm_server(const std::string& input_path, const std::string& wsdb_path, const std::string& cdb_path)
20{
21 info("Connecting to aztec-wsdb at ", wsdb_path);
22 constexpr int max_retries = 50;
23 constexpr int retry_delay_ms = 100;
25 for (int attempt = 0; attempt < max_retries; ++attempt) {
26 try {
27 wsdb_client = std::make_unique<wsdb::WsdbIpcClient>(wsdb_path);
28 break;
29 } catch (const std::exception& e) {
30 if (attempt == max_retries - 1) {
31 info("Failed to connect to aztec-wsdb after ", max_retries, " attempts: ", e.what());
32 return 1;
33 }
34 std::this_thread::sleep_for(std::chrono::milliseconds(retry_delay_ms));
35 }
36 }
37
38 info("Connecting to CDB at ", cdb_path);
40 for (int attempt = 0; attempt < max_retries; ++attempt) {
41 try {
42 cdb_client = std::make_unique<cdb::CdbIpcContractDB>(cdb_path);
43 break;
44 } catch (const std::exception& e) {
45 if (attempt == max_retries - 1) {
46 info("Failed to connect to CDB after ", max_retries, " attempts: ", e.what());
47 return 1;
48 }
49 std::this_thread::sleep_for(std::chrono::milliseconds(retry_delay_ms));
50 }
51 }
52
53 AvmRequest request{ .cdb_client = *cdb_client, .wsdb_client = *wsdb_client };
54
55 ipc::ServerOptions opts;
56 opts.max_shm_clients = 1;
57 auto server = ipc::make_server(input_path, opts);
58 if (!server) {
59 info("Error: --input path must end with .sock or .shm: ", input_path);
60 return 1;
61 }
62
63 info("bb-avm-sim listening on ", input_path);
64 ipc::install_default_signal_handlers(*server);
65 auto cancel_simulation_handler = [](int /*signal*/) {
67 if (token) {
68 token->cancel();
69 }
70 };
71
72 (void)std::signal(SIGUSR1, cancel_simulation_handler);
73
74 if (!server->listen()) {
75 info("Error: Could not start IPC server");
76 return 1;
77 }
78
79 info("bb-avm-sim IPC server ready");
80
81 // A single AVM simulation runs synchronously per connection (the simulator
82 // pool gives each bb-avm-sim process one connection, one in-flight request),
83 // so the handler runs inline on the reactor thread and responds before the
84 // reactor loops — no dispatch pool needed.
85 auto handler = make_avm_handler(request);
86 server->run_reactor([&handler](int /*client_id*/, std::span<const uint8_t> raw, ipc::IpcServer::Respond respond) {
87 handler(raw, std::move(respond));
88 });
89
90 server->close();
91 return 0;
92}
93
94} // namespace bb::avm
AVM IPC handler context.
#define info(...)
Definition log.hpp:93
std::atomic< avm2::simulation::CancellationToken * > g_active_cancellation_token
int execute_avm_server(const std::string &input_path, const std::string &wsdb_path, const std::string &cdb_path)
Start the bb-avm-sim IPC server.
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