Skip to content

Commit 4acea16

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 cb30112 commit 4acea16

64 files changed

Lines changed: 2234 additions & 333 deletions

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"

native-packages/kvdb/cpp/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ 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")
3535

3636
foreach(artifact "${LMDBLIB_LIB}" "${LMDB_LIB}")
3737
if(NOT EXISTS "${artifact}")
@@ -52,7 +52,7 @@ ExternalProject_Add(
5252
msgpack-c
5353
PREFIX ${MSGPACK_PREFIX}
5454
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"
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/msgpack/msgpack-c.git 2>/dev/null || true) && git fetch --depth 1 origin --quiet refs/tags/cpp-7.0.0 && git reset --quiet --hard FETCH_HEAD"
5656
SOURCE_DIR ${MSGPACK_PREFIX}/src/msgpack-c
5757
CONFIGURE_COMMAND ""
5858
BUILD_COMMAND ""
Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,4 @@
11
#pragma once
22

3-
// msgpack-c entrypoint for kvdb. We use msgpack-c directly (no barretenberg
4-
// serialization framework). The AztecProtocol msgpack-c fork expects the
5-
// includer to provide THROW/RETHROW macros; define them to standard exception
6-
// handling.
7-
#ifndef THROW
8-
#define THROW throw
9-
#endif
10-
#ifndef RETHROW
11-
#define RETHROW throw
12-
#endif
13-
3+
// msgpack-c entrypoint (header-only, exceptions enabled).
144
#include <msgpack.hpp>

native-packages/lmdblib/bootstrap.sh

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,31 @@ hash=$(hash_str \
1313
function build_native {
1414
local build_dir="cpp/build"
1515
CC=$(which clang) CXX=$(which clang++) cmake -S cpp -B "$build_dir" -G Ninja >/dev/null
16-
cmake --build "$build_dir" --target lmdblib
16+
cmake --build "$build_dir" --target lmdblib lmdblib_tests
1717
}
1818

1919
function build {
2020
echo_header "lmdblib build"
2121
build_native
2222
}
2323

24+
# Emit test commands for the CI test engine (one self-contained gtest binary).
25+
function test_cmds {
26+
echo "$hash:CPUS=2:TIMEOUT=120s native-packages/lmdblib/cpp/build/lmdblib_tests"
27+
}
28+
29+
# Manual: build then run the tests directly.
30+
function test {
31+
echo_header "lmdblib test"
32+
build
33+
test_cmds | filter_test_cmds | parallelize
34+
}
35+
2436
function clean {
2537
rm -rf cpp/build
2638
}
2739

28-
export -f build_native build clean
40+
export -f build_native build test_cmds test clean
2941

3042
case "$cmd" in
3143
"")
Lines changed: 56 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
# Standalone build for lmdblib — the LMDB C++ wrapper.
1+
# lmdblib — a small C++ wrapper around LMDB.
22
#
3-
# This package owns the lmdb wrapper that barretenberg no longer needs (the
4-
# in-memory merkle core is lmdb-free). It builds two static archives that the
5-
# kvdb NAPI addon and the wsdb service link against:
6-
# - liblmdb.a : the upstream LMDB C library (pinned to bb's commit)
7-
# - liblmdblib.a : the bb::lmdblib C++ wrapper objects
3+
# Builds two static archives that the kvdb NAPI addon and the wsdb service link:
4+
# - liblmdb.a : the LMDB C library (built from source, pinned commit)
5+
# - liblmdblib.a : the lmdblib C++ wrapper objects
86
#
9-
# It is barretenberg-free: it includes no bb headers and links no bb archives. It
10-
# vendors its own msgpack-c + lmdb (commits pinned to barretenberg/cpp/cmake/*) and
11-
# defines the shared DBStats POD locally (src/lmdblib/types.hpp).
7+
# It is self-contained: it vendors LMDB and msgpack-c (header-only) via FetchContent.
128

139
cmake_minimum_required(VERSION 3.24)
14-
# C is enabled so CMAKE_C_COMPILER is populated for lmdb's ExternalProject build.
10+
# C is enabled to compile the LMDB C sources.
1511
project(aztec_lmdblib CXX C)
1612

1713
set(CMAKE_CXX_STANDARD 20)
@@ -21,66 +17,54 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
2117

2218
# Default to Release (-O3 -DNDEBUG). lmdblib's templated/inline headers (serialise_key,
2319
# value_cmp, the msgpack adaptors) are compiled into both liblmdblib.a and into every
24-
# consumer that includes them (the kvdb .node, the wsdb service). Matching the consumers'
20+
# consumer that includes them (the kvdb addon, the wsdb service); matching the consumers'
2521
# Release build keeps that inline codegen identical and avoids ODR divergence.
2622
if(NOT CMAKE_BUILD_TYPE)
2723
set(CMAKE_BUILD_TYPE Release)
2824
endif()
2925

3026
# ---------------------------------------------------------------------------
31-
# Third-party deps (own copies; commits must match barretenberg/cpp/cmake/*).
27+
# Third-party dependencies (fetched at configure time).
3228
# ---------------------------------------------------------------------------
33-
set(DEPS_PREFIX "${CMAKE_BINARY_DIR}/_deps")
34-
include(ExternalProject)
29+
include(FetchContent)
3530

36-
# lmdb: build our own liblmdb.a (pinned to bb's commit in cmake/lmdb.cmake).
37-
set(LMDB_PREFIX "${DEPS_PREFIX}/lmdb")
38-
set(LMDB_INCLUDE "${LMDB_PREFIX}/src/lmdb_repo/libraries/liblmdb")
39-
set(LMDB_LIB "${LMDB_INCLUDE}/liblmdb.a")
40-
ExternalProject_Add(
41-
lmdb_repo
42-
PREFIX ${LMDB_PREFIX}
43-
DOWNLOAD_COMMAND
44-
sh -c "mkdir -p ${LMDB_PREFIX}/src/lmdb_repo && cd ${LMDB_PREFIX}/src/lmdb_repo && git init --quiet && (git remote add origin https://github.com/LMDB/lmdb.git 2>/dev/null || true) && git fetch --depth 1 origin --quiet ddd0a773e2f44d38e4e31ec9ed81af81f4e4ccbb && git reset --quiet --hard FETCH_HEAD"
45-
SOURCE_DIR ${LMDB_PREFIX}/src/lmdb_repo
46-
BUILD_IN_SOURCE YES
47-
CONFIGURE_COMMAND ""
48-
BUILD_COMMAND ${CMAKE_COMMAND} -E env --unset=CFLAGS --unset=CXXFLAGS CC=${CMAKE_C_COMPILER}${CMAKE_C_COMPILER_ARG1} AR=${CMAKE_AR} make -e -C libraries/liblmdb XCFLAGS=-fPIC liblmdb.a
49-
INSTALL_COMMAND ""
50-
UPDATE_COMMAND ""
51-
BUILD_BYPRODUCTS ${LMDB_LIB}
31+
# LMDB: a plain-Makefile C library (no CMake). Fetch the source and compile the two
32+
# translation units ourselves so they use this project's toolchain and flags.
33+
FetchContent_Declare(
34+
lmdb
35+
GIT_REPOSITORY https://github.com/LMDB/lmdb.git
36+
GIT_TAG ddd0a773e2f44d38e4e31ec9ed81af81f4e4ccbb
5237
)
53-
add_library(lmdb STATIC IMPORTED GLOBAL)
54-
add_dependencies(lmdb lmdb_repo)
55-
set_target_properties(lmdb PROPERTIES IMPORTED_LOCATION ${LMDB_LIB})
38+
FetchContent_MakeAvailable(lmdb)
39+
set(LMDB_DIR ${lmdb_SOURCE_DIR}/libraries/liblmdb)
40+
add_library(lmdb STATIC ${LMDB_DIR}/mdb.c ${LMDB_DIR}/midl.c)
41+
target_include_directories(lmdb PUBLIC ${LMDB_DIR})
5642

57-
# msgpack-c: header-only; fetch for includes (pinned to bb's commit in cmake/msgpack.cmake).
58-
set(MSGPACK_PREFIX "${DEPS_PREFIX}/msgpack-c")
59-
set(MSGPACK_INCLUDE "${MSGPACK_PREFIX}/src/msgpack-c/include")
60-
ExternalProject_Add(
61-
msgpack-c
62-
PREFIX ${MSGPACK_PREFIX}
63-
DOWNLOAD_COMMAND
64-
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"
65-
SOURCE_DIR ${MSGPACK_PREFIX}/src/msgpack-c
66-
CONFIGURE_COMMAND ""
67-
BUILD_COMMAND ""
68-
INSTALL_COMMAND ""
69-
UPDATE_COMMAND ""
43+
# msgpack-c: header-only. Fetch the source and use its include directory directly
44+
# (we don't build msgpack-c's own CMake targets/tests).
45+
FetchContent_Declare(
46+
msgpack
47+
GIT_REPOSITORY https://github.com/msgpack/msgpack-c.git
48+
GIT_TAG cpp-7.0.0
7049
)
50+
FetchContent_GetProperties(msgpack)
51+
if(NOT msgpack_POPULATED)
52+
FetchContent_Populate(msgpack)
53+
endif()
54+
set(MSGPACK_INCLUDE ${msgpack_SOURCE_DIR}/include)
7155

72-
# lmdblib is barretenberg-free: it links no bb archives and includes no bb headers.
73-
# It is still compiled with libc++ and -march=skylake so its objects are ABI- and
74-
# arch-compatible when linked into consumers (wsdb, kvdb) alongside bb's archives.
75-
add_compile_definitions(MSGPACK_NO_BOOST MSGPACK_USE_STD_VARIANT_ADAPTOR NO_PAR_ALGOS)
56+
# msgpack-c is used header-only with exceptions (no boost). Compiled with libc++ and
57+
# -march=skylake so lmdblib's objects are ABI- and arch-compatible with the consumers
58+
# (kvdb, wsdb) they are linked into.
59+
add_compile_definitions(MSGPACK_NO_BOOST NO_PAR_ALGOS)
7660
add_compile_options(-march=skylake -fconstexpr-steps=100000000 -fbracket-depth=1024)
7761
add_compile_options(-Wno-deprecated-declarations -Wno-missing-field-initializers)
7862
add_compile_options(-stdlib=libc++)
7963

8064
include_directories(
8165
${CMAKE_CURRENT_SOURCE_DIR}/src
8266
${MSGPACK_INCLUDE}
83-
${LMDB_INCLUDE}
67+
${LMDB_DIR}
8468
)
8569

8670
# ---------------------------------------------------------------------------
@@ -100,4 +84,24 @@ set(LMDBLIB_SOURCES
10084
src/lmdblib/queries.cpp
10185
)
10286
add_library(lmdblib STATIC ${LMDBLIB_SOURCES})
103-
add_dependencies(lmdblib lmdb_repo msgpack-c)
87+
add_dependencies(lmdblib lmdb)
88+
89+
# ---------------------------------------------------------------------------
90+
# Tests (vendored googletest; self-contained, no external dependencies).
91+
# ---------------------------------------------------------------------------
92+
option(LMDBLIB_BUILD_TESTS "Build lmdblib C++ tests" ON)
93+
if(LMDBLIB_BUILD_TESTS)
94+
add_link_options(-stdlib=libc++)
95+
FetchContent_Declare(
96+
googletest
97+
GIT_REPOSITORY https://github.com/google/googletest.git
98+
GIT_TAG v1.13.0
99+
)
100+
set(INSTALL_GTEST OFF CACHE BOOL "" FORCE)
101+
FetchContent_MakeAvailable(googletest)
102+
add_executable(lmdblib_tests
103+
src/lmdblib/lmdb_store.test.cpp
104+
src/lmdblib/lmdb_environment.test.cpp
105+
)
106+
target_link_libraries(lmdblib_tests PRIVATE lmdblib lmdb GTest::gtest_main pthread)
107+
endif()

native-packages/lmdblib/cpp/src/lmdblib/lmdb_environment.test.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
#include <thread>
1212
#include <vector>
1313

14-
#include "barretenberg/common/serialize.hpp"
15-
#include "barretenberg/common/streams.hpp"
16-
#include "barretenberg/common/test.hpp"
1714
#include "lmdblib/fixtures.hpp"
1815
#include "lmdblib/lmdb_database.hpp"
1916
#include "lmdblib/lmdb_db_transaction.hpp"

native-packages/lmdblib/cpp/src/lmdblib/lmdb_store.test.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
#include <thread>
1313
#include <vector>
1414

15-
#include "barretenberg/common/serialize.hpp"
16-
#include "barretenberg/common/streams.hpp"
17-
#include "barretenberg/common/test.hpp"
1815
#include "lmdblib/fixtures.hpp"
1916
#include "lmdblib/lmdb_cursor.hpp"
2017
#include "lmdblib/lmdb_database.hpp"
Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,4 @@
11
#pragma once
22

3-
// msgpack-c entrypoint for lmdblib. We use msgpack-c directly (no barretenberg
4-
// serialization framework). The AztecProtocol msgpack-c fork expects the
5-
// includer to provide THROW/RETHROW macros; define them to standard exception
6-
// handling.
7-
#ifndef THROW
8-
#define THROW throw
9-
#endif
10-
#ifndef RETHROW
11-
#define RETHROW throw
12-
#endif
13-
3+
// msgpack-c entrypoint for lmdblib (header-only, exceptions enabled).
144
#include <msgpack.hpp>

native-packages/wsdb/bootstrap.sh

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function generate_ts_package {
3333
function build_native {
3434
local build_dir="cpp/build"
3535
CC=$(which clang) CXX=$(which clang++) cmake -S cpp -B "$build_dir" -G Ninja >/dev/null
36-
cmake --build "$build_dir" --target "$WSDB_BINARY"
36+
cmake --build "$build_dir" --target "$WSDB_BINARY" wsdb_tests
3737
local target_dir="ts/build/$(arch)-$(os)"
3838
mkdir -p "$target_dir"
3939
cp "$build_dir/bin/$WSDB_BINARY" "$target_dir/$WSDB_BINARY"
@@ -48,6 +48,20 @@ function build {
4848
(cd ts && ./scripts/prepare_arch_packages.sh "$(arch)-$(os)=build/$(arch)-$(os)/$WSDB_BINARY")
4949
}
5050

51+
# Emit test commands for the CI test engine. The decoupled wsdb_tests use no
52+
# barretenberg headers; they link libbarretenberg.a only for the poseidon2 c_bind.
53+
# (The optional bb-header parity/equivalence target, WSDB_BUILD_BB_TESTS, is manual.)
54+
function test_cmds {
55+
echo "$hash:CPUS=8:TIMEOUT=600s native-packages/wsdb/cpp/build/bin/wsdb_tests"
56+
}
57+
58+
# Manual: build then run the tests directly.
59+
function test {
60+
echo_header "wsdb test"
61+
build
62+
test_cmds | filter_test_cmds | parallelize
63+
}
64+
5165
function clean {
5266
rm -rf ts node_modules cpp/build
5367
}
@@ -64,7 +78,7 @@ function release {
6478
(cd ts && retry "deploy_npm ${REF_NAME#v}")
6579
}
6680

67-
export -f generate_ts_package build_native build clean release
81+
export -f generate_ts_package build_native build test_cmds test clean release
6882

6983
case "$cmd" in
7084
"")

0 commit comments

Comments
 (0)