Skip to content

Commit 4d09a60

Browse files
committed
refactor: decouple wsdb from barretenberg headers
wsdb compiles with zero barretenberg headers — its own FieldElement (32-byte canonical) + a poseidon2 c_bind are the only barretenberg link. Forks the bb-free common utils, moves merkle_tree Signal into wsdb, keeps lmdb keys backward-compatible (little-endian uint256), switches lmdblib to upstream msgpack + FetchContent (bb-free), and wires the lmdblib/wsdb C++ tests into the CI test engine. Squashed from the decouple branch (10 commits).
1 parent 2218d58 commit 4d09a60

115 files changed

Lines changed: 2670 additions & 762 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Makefile

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ endef
5555

5656
# Fast bootstrap.
5757
fast: release-image barretenberg boxes playground docs aztec-up \
58-
bb-tests l1-contracts-tests yarn-project-tests boxes-tests playground-tests aztec-up-tests docs-tests noir-protocol-circuits-tests release-image-tests spartan claude-tests ipc-codegen-tests
58+
bb-tests l1-contracts-tests yarn-project-tests boxes-tests playground-tests aztec-up-tests docs-tests noir-protocol-circuits-tests release-image-tests spartan claude-tests ipc-codegen-tests lmdblib-tests wsdb-tests
5959

6060
# Full bootstrap.
6161
full: fast bb-full-tests bb-cpp-full yarn-project-benches
@@ -326,7 +326,7 @@ ipc-runtime-cross: ipc-runtime ipc-runtime-cross-arm64-linux ipc-runtime-cross-a
326326

327327
# lmdblib and kvdb are barretenberg-free: they build against their own deps
328328
# (lmdb, msgpack-c, node-addon-api) only, never bb.
329-
.PHONY: lmdblib kvdb
329+
.PHONY: lmdblib kvdb lmdblib-tests wsdb-tests
330330
lmdblib:
331331
$(call build,$@,native-packages/lmdblib)
332332

@@ -336,6 +336,15 @@ kvdb: lmdblib
336336
wsdb: ipc-codegen ipc-runtime bb-cpp-native lmdblib
337337
$(call build,$@,native-packages/wsdb)
338338

339+
# Native-package C++ tests (self-contained gtest binaries). kvdb has no C++ tests
340+
# (its NAPI is exercised by yarn-project's kv-store tests). wsdb_tests use no bb
341+
# headers; the bb-header parity/equivalence target is manual (WSDB_BUILD_BB_TESTS).
342+
lmdblib-tests: lmdblib
343+
$(call test,$@,native-packages/lmdblib)
344+
345+
wsdb-tests: wsdb
346+
$(call test,$@,native-packages/wsdb)
347+
339348
#==============================================================================
340349
# .claude tooling
341350
#==============================================================================
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "barretenberg/crypto/poseidon2/poseidon2.hpp"
2+
#include "barretenberg/crypto/poseidon2/poseidon2_params.hpp"
3+
#include "barretenberg/ecc/curves/bn254/fr.hpp"
4+
#include <cstddef>
5+
#include <cstdint>
6+
#include <vector>
7+
8+
// Flat C ABI over Poseidon2, for in-process consumers that must hash at high
9+
// frequency but want no dependency on barretenberg's C++ headers, field type,
10+
// stdlib, or build flags (e.g. the standalone world-state DB service). Field
11+
// elements cross as 32 canonical bytes (barretenberg's field serialization);
12+
// the montgomery form stays private to barretenberg.
13+
namespace {
14+
using FF = bb::fr;
15+
using Poseidon2Bn254 = bb::crypto::Poseidon2<bb::crypto::Poseidon2Bn254ScalarFieldParams>;
16+
} // namespace
17+
18+
extern "C" {
19+
20+
// Hash `n` field elements. `elems` is n*32 canonical bytes; `out` receives 32.
21+
void bb_poseidon2_hash(const uint8_t* elems, size_t n, uint8_t* out)
22+
{
23+
std::vector<FF> inputs;
24+
inputs.reserve(n);
25+
for (size_t i = 0; i < n; ++i) {
26+
inputs.push_back(FF::serialize_from_buffer(elems + (i * 32)));
27+
}
28+
FF::serialize_to_buffer(Poseidon2Bn254::hash(inputs), out);
29+
}
30+
31+
// Hash { separator, lhs, rhs } — matches Poseidon2HashPolicy::hash_pair_with_separator,
32+
// the domain-separated internal-node hash used by the Aztec merkle trees.
33+
void bb_poseidon2_hash_pair_with_separator(uint64_t separator, const uint8_t* lhs, const uint8_t* rhs, uint8_t* out)
34+
{
35+
std::vector<FF> inputs{ FF(separator), FF::serialize_from_buffer(lhs), FF::serialize_from_buffer(rhs) };
36+
FF::serialize_to_buffer(Poseidon2Bn254::hash(inputs), out);
37+
}
38+
39+
} // extern "C"

barretenberg/cpp/src/barretenberg/vm2_wsdb/wsdb_ipc_merkle_db.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ namespace bb::avm2::simulation {
99

1010
// Wire <-> domain conversion helpers are shared with the server handlers
1111
// (see wsdb_handlers.cpp) so both sides use the same encoding boundary.
12-
using bb::wsdb::Fr;
1312
using bb::wsdb::fr_from_wire;
1413
using bb::wsdb::fr_to_wire;
1514
using bb::wsdb::fr_vec_from_wire;
@@ -21,6 +20,7 @@ using bb::wsdb::revision_to_wire;
2120
using bb::wsdb::sequential_nullifier_from_wire;
2221
using bb::wsdb::sequential_public_data_from_wire;
2322
using bb::wsdb::tree_id_to_wire;
23+
using bb::wsdb::wire::Fr;
2424

2525
// ---------------------------------------------------------------------------
2626
// Constructor

barretenberg/cpp/src/barretenberg/vm2_wsdb/wsdb_wire_convert_client.hpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,77 +19,77 @@
1919

2020
namespace bb::wsdb {
2121

22-
inline Fr fr_to_wire(const bb::fr& d)
22+
inline wire::Fr fr_to_wire(const bb::fr& d)
2323
{
24-
Fr r{};
24+
wire::Fr r{};
2525
bb::fr::serialize_to_buffer(d, r.data());
2626
return r;
2727
}
2828

29-
inline bb::fr fr_from_wire(const Fr& w)
29+
inline bb::fr fr_from_wire(const wire::Fr& w)
3030
{
3131
return bb::fr::serialize_from_buffer(w.data());
3232
}
3333

34-
inline BlockHeaderHash block_header_hash_to_wire(const bb::fr& d)
34+
inline wire::BlockHeaderHash block_header_hash_to_wire(const bb::fr& d)
3535
{
36-
BlockHeaderHash r{};
36+
wire::BlockHeaderHash r{};
3737
bb::fr::serialize_to_buffer(d, r.data());
3838
return r;
3939
}
4040

41-
inline bb::fr block_header_hash_from_wire(const BlockHeaderHash& w)
41+
inline bb::fr block_header_hash_from_wire(const wire::BlockHeaderHash& w)
4242
{
4343
return bb::fr::serialize_from_buffer(w.data());
4444
}
4545

46-
inline PublicDataSlot public_data_slot_to_wire(const bb::fr& d)
46+
inline wire::PublicDataSlot public_data_slot_to_wire(const bb::fr& d)
4747
{
48-
PublicDataSlot r{};
48+
wire::PublicDataSlot r{};
4949
bb::fr::serialize_to_buffer(d, r.data());
5050
return r;
5151
}
5252

53-
inline bb::fr public_data_slot_from_wire(const PublicDataSlot& w)
53+
inline bb::fr public_data_slot_from_wire(const wire::PublicDataSlot& w)
5454
{
5555
return bb::fr::serialize_from_buffer(w.data());
5656
}
5757

58-
inline PublicDataValue public_data_value_to_wire(const bb::fr& d)
58+
inline wire::PublicDataValue public_data_value_to_wire(const bb::fr& d)
5959
{
60-
PublicDataValue r{};
60+
wire::PublicDataValue r{};
6161
bb::fr::serialize_to_buffer(d, r.data());
6262
return r;
6363
}
6464

65-
inline bb::fr public_data_value_from_wire(const PublicDataValue& w)
65+
inline bb::fr public_data_value_from_wire(const wire::PublicDataValue& w)
6666
{
6767
return bb::fr::serialize_from_buffer(w.data());
6868
}
6969

70-
inline Nullifier nullifier_to_wire(const bb::fr& d)
70+
inline wire::Nullifier nullifier_to_wire(const bb::fr& d)
7171
{
72-
Nullifier r{};
72+
wire::Nullifier r{};
7373
bb::fr::serialize_to_buffer(d, r.data());
7474
return r;
7575
}
7676

77-
inline bb::fr nullifier_from_wire(const Nullifier& w)
77+
inline bb::fr nullifier_from_wire(const wire::Nullifier& w)
7878
{
7979
return bb::fr::serialize_from_buffer(w.data());
8080
}
8181

82-
inline std::vector<Fr> fr_vec_to_wire(const std::vector<bb::fr>& d)
82+
inline std::vector<wire::Fr> fr_vec_to_wire(const std::vector<bb::fr>& d)
8383
{
84-
std::vector<Fr> r;
84+
std::vector<wire::Fr> r;
8585
r.reserve(d.size());
8686
for (const auto& x : d) {
8787
r.push_back(fr_to_wire(x));
8888
}
8989
return r;
9090
}
9191

92-
inline std::vector<bb::fr> fr_vec_from_wire(const std::vector<Fr>& w)
92+
inline std::vector<bb::fr> fr_vec_from_wire(const std::vector<wire::Fr>& w)
9393
{
9494
std::vector<bb::fr> r;
9595
r.reserve(w.size());
@@ -117,12 +117,12 @@ inline crypto::merkle_tree::WorldStateRevision revision_from_wire(const wire::Wo
117117
};
118118
}
119119

120-
inline MerkleTreeId tree_id_to_wire(crypto::merkle_tree::MerkleTreeId d)
120+
inline wire::MerkleTreeId tree_id_to_wire(crypto::merkle_tree::MerkleTreeId d)
121121
{
122-
return static_cast<MerkleTreeId>(d);
122+
return static_cast<wire::MerkleTreeId>(d);
123123
}
124124

125-
inline crypto::merkle_tree::MerkleTreeId tree_id_from_wire(MerkleTreeId w)
125+
inline crypto::merkle_tree::MerkleTreeId tree_id_from_wire(wire::MerkleTreeId w)
126126
{
127127
return static_cast<crypto::merkle_tree::MerkleTreeId>(w);
128128
}

ipc-codegen/echo_example/cpp/src/echo_client.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
} while (0)
1818

1919
namespace {
20-
echo::Fr test_hash(uint8_t base) {
21-
echo::Fr hash{};
20+
echo::wire::Fr test_hash(uint8_t base) {
21+
echo::wire::Fr hash{};
2222
for (size_t i = 0; i < hash.size(); ++i) {
2323
hash[i] = static_cast<uint8_t>(base + i);
2424
}
@@ -74,7 +74,7 @@ int main(int argc, char **argv) {
7474
CHECK(resp.treeId == 7, "EchoAliases treeId");
7575
CHECK(resp.hash == hash, "EchoAliases hash");
7676
CHECK(resp.maybeHash == second, "EchoAliases maybeHash");
77-
CHECK((resp.hashes == std::vector<echo::Fr>{hash, second}),
77+
CHECK((resp.hashes == std::vector<echo::wire::Fr>{hash, second}),
7878
"EchoAliases hashes");
7979
std::cerr << "echo_client(cpp): EchoAliases OK\n";
8080
}

ipc-codegen/echo_example/cpp/src/golden_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,12 @@ void check_response(const std::string &dir, const std::string &file,
151151
}
152152
}
153153

154-
echo::Fr test_hash(uint8_t base) {
154+
echo::wire::Fr test_hash(uint8_t base) {
155155
std::array<uint8_t, 32> bytes{};
156156
for (size_t i = 0; i < bytes.size(); i++) {
157157
bytes[i] = static_cast<uint8_t>(base + i);
158158
}
159-
return echo::Fr(bytes);
159+
return echo::wire::Fr(bytes);
160160
}
161161

162162
} // namespace

ipc-codegen/src/cpp_codegen.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,10 +550,10 @@ template <typename Tag> struct Bin32Alias {
550550
551551
namespace ${ns} {
552552
553-
${aliasDecls}
554-
555553
${this.opts.wireNamespace ? `namespace ${this.opts.wireNamespace} {` : ""}
556554
555+
${aliasDecls}
556+
557557
${structs}
558558
559559
${this.opts.wireNamespace ? `} // namespace ${this.opts.wireNamespace}` : ""}

native-packages/kvdb/cpp/CMakeLists.txt

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,36 +30,20 @@ get_filename_component(REPO_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../../.." ABSOLUTE
3030
set(LMDBLIB_PKG "${REPO_ROOT}/native-packages/lmdblib")
3131
set(LMDBLIB_SRC "${LMDBLIB_PKG}/cpp/src")
3232
set(LMDBLIB_LIB "${LMDBLIB_PKG}/cpp/build/lib/liblmdblib.a")
33-
set(LMDB_INCLUDE "${LMDBLIB_PKG}/cpp/build/_deps/lmdb/src/lmdb_repo/libraries/liblmdb")
34-
set(LMDB_LIB "${LMDB_INCLUDE}/liblmdb.a")
33+
set(LMDB_INCLUDE "${LMDBLIB_PKG}/cpp/build/_deps/lmdb-src/libraries/liblmdb")
34+
set(LMDB_LIB "${LMDBLIB_PKG}/cpp/build/lib/liblmdb.a")
35+
# msgpack-c headers (header-only) come transitively from lmdblib's build: lmdblib fetches
36+
# msgpack-c and compiles liblmdblib.a against it, so the wire ABI is single-sourced rather
37+
# than re-fetched here. (Matches how the wsdb package consumes msgpack.)
38+
set(MSGPACK_INCLUDE "${LMDBLIB_PKG}/cpp/build/_deps/msgpack-src/include")
3539

36-
foreach(artifact "${LMDBLIB_LIB}" "${LMDB_LIB}")
40+
foreach(artifact "${LMDBLIB_LIB}" "${LMDB_LIB}" "${MSGPACK_INCLUDE}")
3741
if(NOT EXISTS "${artifact}")
3842
message(FATAL_ERROR "Missing prebuilt artifact: ${artifact}\n"
3943
"Build lmdblib first (cd native-packages/lmdblib && ./bootstrap.sh).")
4044
endif()
4145
endforeach()
4246

43-
# ---------------------------------------------------------------------------
44-
# msgpack-c headers (own copy; commit must match lmdblib's so the wire ABI lines up).
45-
# ---------------------------------------------------------------------------
46-
set(DEPS_PREFIX "${CMAKE_BINARY_DIR}/_deps")
47-
include(ExternalProject)
48-
49-
set(MSGPACK_PREFIX "${DEPS_PREFIX}/msgpack-c")
50-
set(MSGPACK_INCLUDE "${MSGPACK_PREFIX}/src/msgpack-c/include")
51-
ExternalProject_Add(
52-
msgpack-c
53-
PREFIX ${MSGPACK_PREFIX}
54-
DOWNLOAD_COMMAND
55-
sh -c "mkdir -p ${MSGPACK_PREFIX}/src/msgpack-c && cd ${MSGPACK_PREFIX}/src/msgpack-c && git init --quiet && (git remote add origin https://github.com/AztecProtocol/msgpack-c.git 2>/dev/null || true) && git fetch --depth 1 origin --quiet c0334576ed657fb3b3c49e8e61402989fb84146d && git reset --quiet --hard FETCH_HEAD"
56-
SOURCE_DIR ${MSGPACK_PREFIX}/src/msgpack-c
57-
CONFIGURE_COMMAND ""
58-
BUILD_COMMAND ""
59-
INSTALL_COMMAND ""
60-
UPDATE_COMMAND ""
61-
)
62-
6347
# ---------------------------------------------------------------------------
6448
# Node addon-api / node-api headers (resolved from this package's node_modules).
6549
# ---------------------------------------------------------------------------
@@ -101,7 +85,6 @@ set(KVDB_SOURCES
10185
src/util/promise.cpp
10286
)
10387
add_library(nodejs_module SHARED ${KVDB_SOURCES})
104-
add_dependencies(nodejs_module msgpack-c)
10588
set_target_properties(nodejs_module PROPERTIES PREFIX "" SUFFIX ".node")
10689
target_include_directories(nodejs_module PRIVATE ${NODE_API_HEADERS_DIR} ${NODE_ADDON_API_DIR})
10790

native-packages/kvdb/cpp/src/init_module.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
Napi::Object Init(Napi::Env env, Napi::Object exports) {
55
exports.Set(Napi::String::New(env, "LMDBStore"),
6-
bb::nodejs::lmdb_store::LMDBStoreWrapper::get_class(env));
6+
azteclabs::kvdb::lmdb_store::LMDBStoreWrapper::get_class(env));
77
return exports;
88
}
99

native-packages/kvdb/cpp/src/lmdb_store/lmdb_store_message.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#pragma once
22
#include "lmdblib/types.hpp"
33
#include "messaging/header.hpp"
4-
#include "serialization.hpp"
4+
#include <msgpack.hpp>
55
#include "msgpack/adaptor/define_decl.hpp"
66
#include <cstdint>
77
#include <optional>
88
#include <string>
99

10-
namespace bb::nodejs::lmdb_store {
10+
namespace azteclabs::kvdb::lmdb_store {
1111

12-
using namespace bb::messaging;
12+
using namespace azteclabs::kvdb::messaging;
1313

1414
enum LMDBStoreMessageType {
1515
OPEN_DATABASE = FIRST_APP_MSG_TYPE,
@@ -139,6 +139,6 @@ struct CopyStoreRequest {
139139
MSGPACK_DEFINE_MAP(dstPath, compact);
140140
};
141141

142-
} // namespace bb::nodejs::lmdb_store
142+
} // namespace azteclabs::kvdb::lmdb_store
143143

144-
MSGPACK_ADD_ENUM(bb::nodejs::lmdb_store::LMDBStoreMessageType)
144+
MSGPACK_ADD_ENUM(azteclabs::kvdb::lmdb_store::LMDBStoreMessageType)

0 commit comments

Comments
 (0)