Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
api_msgpack.cpp
Go to the documentation of this file.
4#include "barretenberg/bbapi/generated/bb_dispatch.hpp"
7#include <cstdint>
8#include <fstream>
9#include <iostream>
10#include <string>
11#include <vector>
12
13#if !defined(__wasm__) && !defined(_WIN32)
14#include "ipc_runtime/serve_helper.hpp"
15#include "ipc_runtime/signal_handlers.hpp"
16#endif
17
18namespace bb {
19
20int process_msgpack_commands(std::istream& input_stream)
21{
22 // Redirect std::cout to stderr to prevent accidental writes to stdout
23 auto* original_cout_buf = std::cout.rdbuf();
24 std::cout.rdbuf(std::cerr.rdbuf());
25 std::ostream stdout_stream(original_cout_buf);
26
27 // Dispatcher is the codegen-emitted handler that owns the
28 // command-name → handle_<method> table and runs the per-call
29 // serialize / deserialize / exception → ErrorResponse plumbing.
30 // BBApiRequest lives across calls so IVC state (loaded circuit,
31 // accumulator, etc.) persists between Chonk* invocations.
33 auto handler = bb::bbapi::make_bb_handler(request);
34
35 while (!input_stream.eof()) {
36 uint32_t length = 0;
37 input_stream.read(reinterpret_cast<char*>(&length), sizeof(length));
38 if (input_stream.gcount() != sizeof(length)) {
39 break; // EOF or incomplete length
40 }
41
42 std::vector<uint8_t> buffer(length);
43 input_stream.read(reinterpret_cast<char*>(buffer.data()), static_cast<std::streamsize>(length));
44 if (input_stream.gcount() != static_cast<std::streamsize>(length)) {
45 std::cerr << "Error: Incomplete msgpack buffer read" << '\n';
46 std::cout.rdbuf(original_cout_buf);
47 return 1;
48 }
49
50 std::vector<uint8_t> response = handler(buffer);
51
52 uint32_t response_length = static_cast<uint32_t>(response.size());
53 stdout_stream.write(reinterpret_cast<const char*>(&response_length), sizeof(response_length));
54 stdout_stream.write(reinterpret_cast<const char*>(response.data()),
55 static_cast<std::streamsize>(response.size()));
56 stdout_stream.flush();
57 }
58
59 std::cout.rdbuf(original_cout_buf);
60 return 0;
61}
62
63#if !defined(__wasm__) && !defined(_WIN32)
65{
66 // Install runtime lifecycle handlers (SIGTERM/SIGINT → request_shutdown,
67 // SIGBUS/SIGSEGV → close+exit, parent-death watch via prctl/kqueue).
68 // MUST be installed before listen() since SIGBUS can occur during init
69 // when shared memory is exhausted.
70 ipc::install_default_signal_handlers(*server);
71
72 if (!server->listen()) {
73 std::cerr << "Error: Could not start IPC server" << '\n';
74 return 1;
75 }
76
77 std::cerr << "IPC server ready" << '\n';
78
79 // Keep one request context for the command stream so stateful command
80 // sequences, such as ChonkStart/Load/Accumulate/Prove, share IVC state.
82 auto handler = bb::bbapi::make_bb_handler(request);
83 server->run([&handler](int /*client_id*/, std::span<const uint8_t> raw) {
84 return handler(std::vector<uint8_t>(raw.begin(), raw.end()));
85 });
86
87 server->close();
88 return 0;
89}
90#endif
91
92int execute_msgpack_run(const std::string& msgpack_input_file,
93 [[maybe_unused]] int max_clients,
94 [[maybe_unused]] size_t request_ring_size,
95 [[maybe_unused]] size_t response_ring_size)
96{
97#if !defined(__wasm__) && !defined(_WIN32)
98 if (!msgpack_input_file.empty()) {
99 ipc::ServerOptions opts{
100 .max_shm_clients = static_cast<std::size_t>(max_clients),
101 .shm_request_ring_size = request_ring_size,
102 .shm_response_ring_size = response_ring_size,
103 .socket_backlog = max_clients,
104 };
105 auto server = ipc::make_server(msgpack_input_file, opts);
106 if (server) {
107 std::cerr << "IPC server at " << msgpack_input_file << '\n';
109 }
110 }
111#endif
112
113 // Process msgpack API commands from stdin or file
114 std::istream* input_stream = &std::cin;
115 std::ifstream file_stream;
116
117 if (!msgpack_input_file.empty()) {
118 file_stream.open(msgpack_input_file, std::ios::binary);
119 if (!file_stream.is_open()) {
120 std::cerr << "Error: Could not open input file: " << msgpack_input_file << '\n';
121 return 1;
122 }
123 input_stream = &file_stream;
124 }
125
126 return process_msgpack_commands(*input_stream);
127}
128
129} // namespace bb
Non-template handler declarations for the bb service.
Shared type definitions for the Barretenberg RPC API.
std::unique_ptr< uint8_t[]> buffer
Definition engine.cpp:60
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
int execute_msgpack_ipc_server(std::unique_ptr< ipc::IpcServer > server)
Execute msgpack commands over IPC.
int execute_msgpack_run(const std::string &msgpack_input_file, int max_clients, size_t request_ring_size, size_t response_ring_size)
Execute msgpack run command.
int process_msgpack_commands(std::istream &input_stream)
Process msgpack API commands from an input stream.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13