|
| 1 | +#include "barretenberg/wsdb/cli.hpp" |
| 2 | +#include "barretenberg/common/log.hpp" |
| 3 | +#include "barretenberg/common/throw_or_abort.hpp" |
| 4 | +#include "barretenberg/serialize/msgpack.hpp" |
| 5 | +#include "barretenberg/wsdb/wsdb_execute.hpp" |
| 6 | +#include "barretenberg/wsdb/wsdb_ipc_server.hpp" |
| 7 | + |
| 8 | +#include "barretenberg/bb/deps/cli11.hpp" |
| 9 | +#include <cstdint> |
| 10 | +#include <iostream> |
| 11 | +#include <string> |
| 12 | +#include <unordered_map> |
| 13 | +#include <vector> |
| 14 | + |
| 15 | +namespace bb::wsdb { |
| 16 | + |
| 17 | +using namespace bb::world_state; |
| 18 | +using namespace bb::crypto::merkle_tree; |
| 19 | + |
| 20 | +namespace { |
| 21 | + |
| 22 | +struct WsdbApi { |
| 23 | + WsdbCommand commands; |
| 24 | + WsdbCommandResponse responses; |
| 25 | + SERIALIZATION_FIELDS(commands, responses); |
| 26 | +}; |
| 27 | + |
| 28 | +std::string get_wsdb_schema_as_json() |
| 29 | +{ |
| 30 | + return msgpack_schema_to_string(WsdbApi{}); |
| 31 | +} |
| 32 | + |
| 33 | +} // namespace |
| 34 | + |
| 35 | +int parse_and_run_wsdb(int argc, char* argv[]) |
| 36 | +{ |
| 37 | + CLI::App app{ "aztec-wsdb: Standalone world state database server" }; |
| 38 | + app.require_subcommand(1); |
| 39 | + |
| 40 | + // ----------------------------------------------------------------------- |
| 41 | + // Subcommand: msgpack |
| 42 | + // ----------------------------------------------------------------------- |
| 43 | + CLI::App* msgpack_command = app.add_subcommand("msgpack", "Msgpack API interface."); |
| 44 | + |
| 45 | + // msgpack schema |
| 46 | + CLI::App* msgpack_schema_command = |
| 47 | + msgpack_command->add_subcommand("schema", "Output a msgpack schema encoded as JSON to stdout."); |
| 48 | + |
| 49 | + // msgpack run |
| 50 | + CLI::App* msgpack_run_command = |
| 51 | + msgpack_command->add_subcommand("run", "Start the world state database IPC server."); |
| 52 | + |
| 53 | + std::string input_path; |
| 54 | + msgpack_run_command->add_option( |
| 55 | + "-i,--input", input_path, "IPC socket/shm path (.sock for UDS, .shm for shared memory)"); |
| 56 | + |
| 57 | + std::string data_dir; |
| 58 | + msgpack_run_command->add_option("-d,--data-dir", data_dir, "Data directory for LMDB stores")->required(); |
| 59 | + |
| 60 | + // Tree heights (JSON map: treeId -> height) |
| 61 | + std::string tree_heights_json; |
| 62 | + msgpack_run_command->add_option("--tree-heights", tree_heights_json, "Tree heights as JSON: {0:40,1:32,...}"); |
| 63 | + |
| 64 | + // Tree prefill sizes |
| 65 | + std::string tree_prefill_json; |
| 66 | + msgpack_run_command->add_option( |
| 67 | + "--tree-prefill", tree_prefill_json, "Tree prefill sizes as JSON: {0:128,2:128,...}"); |
| 68 | + |
| 69 | + // Map sizes (KB) |
| 70 | + std::string map_sizes_json; |
| 71 | + msgpack_run_command->add_option("--map-sizes", map_sizes_json, "LMDB map sizes in KB as JSON: {0:1024,...}"); |
| 72 | + |
| 73 | + uint32_t threads = 16; |
| 74 | + msgpack_run_command->add_option("-t,--threads", threads, "Thread pool size (default: 16)") |
| 75 | + ->check(CLI::PositiveNumber); |
| 76 | + |
| 77 | + uint32_t initial_header_generator_point = 0; |
| 78 | + msgpack_run_command->add_option( |
| 79 | + "--initial-header-generator-point", initial_header_generator_point, "Header generator point (default: 0)"); |
| 80 | + |
| 81 | + // Prefilled public data as JSON array of [slot_hex, value_hex] pairs |
| 82 | + std::string prefilled_public_data_json; |
| 83 | + msgpack_run_command->add_option( |
| 84 | + "--prefilled-public-data", prefilled_public_data_json, "Prefilled public data as JSON array"); |
| 85 | + |
| 86 | + uint64_t genesis_timestamp = 0; |
| 87 | + msgpack_run_command->add_option("--genesis-timestamp", genesis_timestamp, "Genesis block timestamp (default: 0)"); |
| 88 | + |
| 89 | + size_t request_ring_size = 1024 * 1024; |
| 90 | + msgpack_run_command |
| 91 | + ->add_option( |
| 92 | + "--request-ring-size", request_ring_size, "Request ring buffer size for shared memory IPC (default: 1MB)") |
| 93 | + ->check(CLI::PositiveNumber); |
| 94 | + |
| 95 | + size_t response_ring_size = 1024 * 1024; |
| 96 | + msgpack_run_command |
| 97 | + ->add_option("--response-ring-size", |
| 98 | + response_ring_size, |
| 99 | + "Response ring buffer size for shared memory IPC (default: 1MB)") |
| 100 | + ->check(CLI::PositiveNumber); |
| 101 | + |
| 102 | + // Parse CLI |
| 103 | + try { |
| 104 | + app.parse(argc, argv); |
| 105 | + } catch (const CLI::ParseError& e) { |
| 106 | + return app.exit(e); |
| 107 | + } |
| 108 | + |
| 109 | + try { |
| 110 | + if (msgpack_schema_command->parsed()) { |
| 111 | + std::cout << get_wsdb_schema_as_json() << std::endl; |
| 112 | + return 0; |
| 113 | + } |
| 114 | + |
| 115 | + if (msgpack_run_command->parsed()) { |
| 116 | + return execute_wsdb_server(input_path, |
| 117 | + data_dir, |
| 118 | + tree_heights_json, |
| 119 | + tree_prefill_json, |
| 120 | + map_sizes_json, |
| 121 | + threads, |
| 122 | + initial_header_generator_point, |
| 123 | + prefilled_public_data_json, |
| 124 | + genesis_timestamp, |
| 125 | + request_ring_size, |
| 126 | + response_ring_size); |
| 127 | + } |
| 128 | + } catch (const std::exception& e) { |
| 129 | + std::cerr << "Error: " << e.what() << '\n'; |
| 130 | + return 1; |
| 131 | + } |
| 132 | + |
| 133 | + return 0; |
| 134 | +} |
| 135 | + |
| 136 | +} // namespace bb::wsdb |
0 commit comments