Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
api_msgpack.hpp
Go to the documentation of this file.
1#pragma once
2
7#include <cstdint>
8#include <fstream>
9#include <iostream>
10#include <vector>
11
12namespace bb {
13
28inline int process_msgpack_commands(std::istream& input_stream)
29{
30 // Redirect std::cout to stderr to prevent accidental writes to stdout
31 auto* original_cout_buf = std::cout.rdbuf();
32 std::cout.rdbuf(std::cerr.rdbuf());
33
34 // Create an ostream that writes directly to stdout
35 std::ostream stdout_stream(original_cout_buf);
36
37 // Process length-encoded msgpack buffers
38 while (!input_stream.eof()) {
39 // Read 4-byte length prefix in little-endian format
40 uint32_t length = 0;
41 input_stream.read(reinterpret_cast<char*>(&length), sizeof(length));
42
43 if (input_stream.gcount() != sizeof(length)) {
44 // End of stream or incomplete length
45 break;
46 }
47
48 // Read the msgpack buffer
49 std::vector<uint8_t> buffer(length);
50 input_stream.read(reinterpret_cast<char*>(buffer.data()), static_cast<std::streamsize>(length));
51
52 if (input_stream.gcount() != static_cast<std::streamsize>(length)) {
53 std::cerr << "Error: Incomplete msgpack buffer read" << std::endl;
54 // Restore original cout buffer before returning
55 std::cout.rdbuf(original_cout_buf);
56 return 1;
57 }
58
59 // Deserialize the msgpack buffer
60 auto unpacked = msgpack::unpack(reinterpret_cast<const char*>(buffer.data()), buffer.size());
61 auto obj = unpacked.get();
62 // access object assuming it is an array of size 2
63 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
64 if (obj.type != msgpack::type::ARRAY || obj.via.array.size != 2) {
65 throw_or_abort("Expected an array of size 2 [command-name, payload] for bbapi command deserialization");
66 }
67 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
68 auto& arr = obj.via.array;
69 if (arr.ptr[0].type != msgpack::type::STR) {
70 throw_or_abort("Expected first element to be a string (type name) in bbapi command deserialization");
71 }
72
73 // Convert to Command (which is a NamedUnion)
74 bb::bbapi::Command command;
75 obj.convert(command);
76
77 // Execute the command
78 auto response = bbapi::bbapi(std::move(command));
79
80 // Serialize the response
81 msgpack::sbuffer response_buffer;
82 msgpack::pack(response_buffer, response);
83
84 // Write length-encoded response directly to stdout
85 uint32_t response_length = static_cast<uint32_t>(response_buffer.size());
86 stdout_stream.write(reinterpret_cast<const char*>(&response_length), sizeof(response_length));
87 stdout_stream.write(response_buffer.data(), static_cast<std::streamsize>(response_buffer.size()));
88 stdout_stream.flush();
89 }
90
91 // Restore original cout buffer
92 std::cout.rdbuf(original_cout_buf);
93 return 0;
94}
95
105inline int execute_msgpack_run(const std::string& msgpack_input_file)
106{
107 // Process msgpack API commands from stdin or file
108 std::istream* input_stream = &std::cin;
109 std::ifstream file_stream;
110
111 if (!msgpack_input_file.empty()) {
112 file_stream.open(msgpack_input_file, std::ios::binary);
113 if (!file_stream.is_open()) {
114 std::cerr << "Error: Could not open input file: " << msgpack_input_file << std::endl;
115 return 1;
116 }
117 input_stream = &file_stream;
118 }
119
120 return process_msgpack_commands(*input_stream);
121}
122
123} // namespace bb
A wrapper around std::variant that provides msgpack serialization based on type names.
uint8_t const size_t length
Definition data_store.hpp:9
uint8_t buffer[RANDOM_BUFFER_SIZE]
Definition engine.cpp:34
CommandResponse bbapi(Command &&command)
Main API function that processes commands and returns responses.
Definition c_bind.cpp:28
Entry point for Barretenberg command-line interface.
int process_msgpack_commands(std::istream &input_stream)
Process msgpack API commands from an input stream.
int execute_msgpack_run(const std::string &msgpack_input_file)
Execute msgpack run command.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
void throw_or_abort(std::string const &err)