Skip to content

Commit c5971c8

Browse files
committed
fix: give native-packages wsdb server its own wire-convert-client copy
The server pulled in bb's client wire-convert header, which dragged in bb's generated wsdb_types.hpp alongside the server's own generated wsdb_types.hpp — identical structs in bb::wsdb from two files, an ODR redefinition. Each side codegens its own types, so the converters can't share one header; the server now has a local copy bound to its own generated types.
1 parent fdbc792 commit c5971c8

2 files changed

Lines changed: 370 additions & 6 deletions

File tree

native-packages/wsdb/cpp/src/barretenberg/wsdb/wsdb_wire_convert.hpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
* @brief Server-side wire <-> domain conversion helpers for the aztec-wsdb
55
* service.
66
*
7-
* The converters that depend only on the generic merkle-tree vocabulary are
8-
* shared with bb's wsdb client and live in bb
9-
* (barretenberg/wsdb/wsdb_wire_convert.hpp). This header pulls those in and
10-
* adds the server-only converters that touch world_state aggregates (state
11-
* references, DB stats, tree/world-state meta), which require world_state and
12-
* the persistent merkle storage.
7+
* The converters that depend only on the generic merkle-tree vocabulary live
8+
* in a local wsdb_wire_convert_client.hpp, bound to this package's generated
9+
* wire types (bb's wsdb client keeps its own copy bound to bb's generated
10+
* types; the two can't share one header because each side codegens its own
11+
* wsdb_types.hpp). This header pulls those in and adds the server-only
12+
* converters that touch world_state aggregates (state references, DB stats,
13+
* tree/world-state meta), which require world_state and the persistent merkle
14+
* storage.
1315
*/
1416
#include "barretenberg/crypto/merkle_tree/response.hpp"
1517
#include "barretenberg/crypto/merkle_tree/tree_meta.hpp"
Lines changed: 362 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,362 @@
1+
#pragma once
2+
/**
3+
* @file wsdb_wire_convert.hpp
4+
* @brief Client-side wire <-> domain conversion helpers for the aztec-wsdb service.
5+
*
6+
* These converters depend only on the generic merkle-tree vocabulary
7+
* (crypto/merkle_tree) plus the generated wsdb wire types, so they can be
8+
* compiled into bb's wsdb client (consumed by the AVM simulator) without
9+
* pulling in world_state or the persistent merkle storage. The server-side
10+
* converters that touch world_state aggregates (state references, DB stats,
11+
* tree/world-state meta) live alongside the server in native-packages/wsdb.
12+
*/
13+
#include "barretenberg/crypto/merkle_tree/indexed_leaf.hpp"
14+
#include "barretenberg/crypto/merkle_tree/merkle_tree_id.hpp"
15+
#include "barretenberg/crypto/merkle_tree/response.hpp"
16+
#include "barretenberg/crypto/merkle_tree/types.hpp"
17+
#include "barretenberg/ecc/curves/bn254/fr.hpp"
18+
#include "barretenberg/wsdb/generated/wsdb_types.hpp"
19+
20+
namespace bb::wsdb {
21+
22+
inline Fr fr_to_wire(const bb::fr& d)
23+
{
24+
Fr r{};
25+
bb::fr::serialize_to_buffer(d, r.data());
26+
return r;
27+
}
28+
29+
inline bb::fr fr_from_wire(const Fr& w)
30+
{
31+
return bb::fr::serialize_from_buffer(w.data());
32+
}
33+
34+
inline BlockHeaderHash block_header_hash_to_wire(const bb::fr& d)
35+
{
36+
BlockHeaderHash r{};
37+
bb::fr::serialize_to_buffer(d, r.data());
38+
return r;
39+
}
40+
41+
inline bb::fr block_header_hash_from_wire(const BlockHeaderHash& w)
42+
{
43+
return bb::fr::serialize_from_buffer(w.data());
44+
}
45+
46+
inline PublicDataSlot public_data_slot_to_wire(const bb::fr& d)
47+
{
48+
PublicDataSlot r{};
49+
bb::fr::serialize_to_buffer(d, r.data());
50+
return r;
51+
}
52+
53+
inline bb::fr public_data_slot_from_wire(const PublicDataSlot& w)
54+
{
55+
return bb::fr::serialize_from_buffer(w.data());
56+
}
57+
58+
inline PublicDataValue public_data_value_to_wire(const bb::fr& d)
59+
{
60+
PublicDataValue r{};
61+
bb::fr::serialize_to_buffer(d, r.data());
62+
return r;
63+
}
64+
65+
inline bb::fr public_data_value_from_wire(const PublicDataValue& w)
66+
{
67+
return bb::fr::serialize_from_buffer(w.data());
68+
}
69+
70+
inline Nullifier nullifier_to_wire(const bb::fr& d)
71+
{
72+
Nullifier r{};
73+
bb::fr::serialize_to_buffer(d, r.data());
74+
return r;
75+
}
76+
77+
inline bb::fr nullifier_from_wire(const Nullifier& w)
78+
{
79+
return bb::fr::serialize_from_buffer(w.data());
80+
}
81+
82+
inline std::vector<Fr> fr_vec_to_wire(const std::vector<bb::fr>& d)
83+
{
84+
std::vector<Fr> r;
85+
r.reserve(d.size());
86+
for (const auto& x : d) {
87+
r.push_back(fr_to_wire(x));
88+
}
89+
return r;
90+
}
91+
92+
inline std::vector<bb::fr> fr_vec_from_wire(const std::vector<Fr>& w)
93+
{
94+
std::vector<bb::fr> r;
95+
r.reserve(w.size());
96+
for (const auto& x : w) {
97+
r.push_back(fr_from_wire(x));
98+
}
99+
return r;
100+
}
101+
102+
inline wire::WorldStateRevision revision_to_wire(const crypto::merkle_tree::WorldStateRevision& d)
103+
{
104+
return wire::WorldStateRevision{
105+
.forkId = d.forkId,
106+
.blockNumber = d.blockNumber,
107+
.includeUncommitted = d.includeUncommitted,
108+
};
109+
}
110+
111+
inline crypto::merkle_tree::WorldStateRevision revision_from_wire(const wire::WorldStateRevision& w)
112+
{
113+
return crypto::merkle_tree::WorldStateRevision{
114+
.forkId = w.forkId,
115+
.blockNumber = w.blockNumber,
116+
.includeUncommitted = w.includeUncommitted,
117+
};
118+
}
119+
120+
inline MerkleTreeId tree_id_to_wire(crypto::merkle_tree::MerkleTreeId d)
121+
{
122+
return static_cast<MerkleTreeId>(d);
123+
}
124+
125+
inline crypto::merkle_tree::MerkleTreeId tree_id_from_wire(MerkleTreeId w)
126+
{
127+
return static_cast<crypto::merkle_tree::MerkleTreeId>(w);
128+
}
129+
130+
inline wire::PublicDataLeafValue public_data_leaf_to_wire(const crypto::merkle_tree::PublicDataLeafValue& d)
131+
{
132+
return { .slot = public_data_slot_to_wire(d.slot), .value = public_data_value_to_wire(d.value) };
133+
}
134+
135+
inline crypto::merkle_tree::PublicDataLeafValue public_data_leaf_from_wire(const wire::PublicDataLeafValue& w)
136+
{
137+
return { public_data_slot_from_wire(w.slot), public_data_value_from_wire(w.value) };
138+
}
139+
140+
inline std::vector<wire::PublicDataLeafValue> public_data_leaf_vec_to_wire(
141+
const std::vector<crypto::merkle_tree::PublicDataLeafValue>& d)
142+
{
143+
std::vector<wire::PublicDataLeafValue> r;
144+
r.reserve(d.size());
145+
for (const auto& x : d) {
146+
r.push_back(public_data_leaf_to_wire(x));
147+
}
148+
return r;
149+
}
150+
151+
inline std::vector<crypto::merkle_tree::PublicDataLeafValue> public_data_leaf_vec_from_wire(
152+
const std::vector<wire::PublicDataLeafValue>& w)
153+
{
154+
std::vector<crypto::merkle_tree::PublicDataLeafValue> r;
155+
r.reserve(w.size());
156+
for (const auto& x : w) {
157+
r.push_back(public_data_leaf_from_wire(x));
158+
}
159+
return r;
160+
}
161+
162+
inline wire::NullifierLeafValue nullifier_leaf_to_wire(const crypto::merkle_tree::NullifierLeafValue& d)
163+
{
164+
return { .nullifier = nullifier_to_wire(d.nullifier) };
165+
}
166+
167+
inline crypto::merkle_tree::NullifierLeafValue nullifier_leaf_from_wire(const wire::NullifierLeafValue& w)
168+
{
169+
return { nullifier_from_wire(w.nullifier) };
170+
}
171+
172+
inline std::vector<wire::NullifierLeafValue> nullifier_leaf_vec_to_wire(
173+
const std::vector<crypto::merkle_tree::NullifierLeafValue>& d)
174+
{
175+
std::vector<wire::NullifierLeafValue> r;
176+
r.reserve(d.size());
177+
for (const auto& x : d) {
178+
r.push_back(nullifier_leaf_to_wire(x));
179+
}
180+
return r;
181+
}
182+
183+
inline std::vector<crypto::merkle_tree::NullifierLeafValue> nullifier_leaf_vec_from_wire(
184+
const std::vector<wire::NullifierLeafValue>& w)
185+
{
186+
std::vector<crypto::merkle_tree::NullifierLeafValue> r;
187+
r.reserve(w.size());
188+
for (const auto& x : w) {
189+
r.push_back(nullifier_leaf_from_wire(x));
190+
}
191+
return r;
192+
}
193+
194+
inline wire::IndexedPublicDataLeafValue indexed_public_data_leaf_to_wire(
195+
const crypto::merkle_tree::IndexedLeaf<crypto::merkle_tree::PublicDataLeafValue>& d)
196+
{
197+
return { .leaf = public_data_leaf_to_wire(d.leaf), .nextIndex = d.nextIndex, .nextKey = fr_to_wire(d.nextKey) };
198+
}
199+
200+
inline crypto::merkle_tree::IndexedLeaf<crypto::merkle_tree::PublicDataLeafValue> indexed_public_data_leaf_from_wire(
201+
const wire::IndexedPublicDataLeafValue& w)
202+
{
203+
return { public_data_leaf_from_wire(w.leaf), w.nextIndex, fr_from_wire(w.nextKey) };
204+
}
205+
206+
inline wire::IndexedNullifierLeafValue indexed_nullifier_leaf_to_wire(
207+
const crypto::merkle_tree::IndexedLeaf<crypto::merkle_tree::NullifierLeafValue>& d)
208+
{
209+
return { .leaf = nullifier_leaf_to_wire(d.leaf), .nextIndex = d.nextIndex, .nextKey = fr_to_wire(d.nextKey) };
210+
}
211+
212+
inline crypto::merkle_tree::IndexedLeaf<crypto::merkle_tree::NullifierLeafValue> indexed_nullifier_leaf_from_wire(
213+
const wire::IndexedNullifierLeafValue& w)
214+
{
215+
return { nullifier_leaf_from_wire(w.leaf), w.nextIndex, fr_from_wire(w.nextKey) };
216+
}
217+
218+
inline wire::PublicDataLeafUpdateWitnessData public_data_witness_to_wire(
219+
const crypto::merkle_tree::LeafUpdateWitnessData<crypto::merkle_tree::PublicDataLeafValue>& d)
220+
{
221+
return { .leaf = indexed_public_data_leaf_to_wire(d.leaf), .index = d.index, .path = fr_vec_to_wire(d.path) };
222+
}
223+
224+
inline crypto::merkle_tree::LeafUpdateWitnessData<crypto::merkle_tree::PublicDataLeafValue>
225+
public_data_witness_from_wire(const wire::PublicDataLeafUpdateWitnessData& w)
226+
{
227+
return { indexed_public_data_leaf_from_wire(w.leaf), w.index, fr_vec_from_wire(w.path) };
228+
}
229+
230+
inline wire::NullifierLeafUpdateWitnessData nullifier_witness_to_wire(
231+
const crypto::merkle_tree::LeafUpdateWitnessData<crypto::merkle_tree::NullifierLeafValue>& d)
232+
{
233+
return { .leaf = indexed_nullifier_leaf_to_wire(d.leaf), .index = d.index, .path = fr_vec_to_wire(d.path) };
234+
}
235+
236+
inline crypto::merkle_tree::LeafUpdateWitnessData<crypto::merkle_tree::NullifierLeafValue> nullifier_witness_from_wire(
237+
const wire::NullifierLeafUpdateWitnessData& w)
238+
{
239+
return { indexed_nullifier_leaf_from_wire(w.leaf), w.index, fr_vec_from_wire(w.path) };
240+
}
241+
242+
template <typename Wire, typename Domain, typename Fn>
243+
inline std::vector<Wire> vec_to_wire(const std::vector<Domain>& d, Fn fn)
244+
{
245+
std::vector<Wire> r;
246+
r.reserve(d.size());
247+
for (const auto& x : d) {
248+
r.push_back(fn(x));
249+
}
250+
return r;
251+
}
252+
253+
template <typename Domain, typename Wire, typename Fn>
254+
inline std::vector<Domain> vec_from_wire(const std::vector<Wire>& w, Fn fn)
255+
{
256+
std::vector<Domain> r;
257+
r.reserve(w.size());
258+
for (const auto& x : w) {
259+
r.push_back(fn(x));
260+
}
261+
return r;
262+
}
263+
264+
inline wire::BatchInsertionResultPublicData batch_public_data_to_wire(
265+
const crypto::merkle_tree::BatchInsertionResult<crypto::merkle_tree::PublicDataLeafValue>& d)
266+
{
267+
std::vector<wire::SortedPublicDataLeaf> sorted;
268+
sorted.reserve(d.sorted_leaves.size());
269+
for (const auto& [leaf, index] : d.sorted_leaves) {
270+
sorted.push_back({ .leaf = public_data_leaf_to_wire(leaf), .index = index });
271+
}
272+
return { .lowLeafWitnessData = vec_to_wire<wire::PublicDataLeafUpdateWitnessData>(d.low_leaf_witness_data,
273+
public_data_witness_to_wire),
274+
.sortedLeaves = std::move(sorted),
275+
.subtreePath = fr_vec_to_wire(d.subtree_path) };
276+
}
277+
278+
inline crypto::merkle_tree::BatchInsertionResult<crypto::merkle_tree::PublicDataLeafValue> batch_public_data_from_wire(
279+
const wire::BatchInsertionResultPublicData& w)
280+
{
281+
crypto::merkle_tree::BatchInsertionResult<crypto::merkle_tree::PublicDataLeafValue> r;
282+
r.low_leaf_witness_data =
283+
vec_from_wire<crypto::merkle_tree::LeafUpdateWitnessData<crypto::merkle_tree::PublicDataLeafValue>>(
284+
w.lowLeafWitnessData, public_data_witness_from_wire);
285+
r.sorted_leaves.reserve(w.sortedLeaves.size());
286+
for (const auto& x : w.sortedLeaves) {
287+
r.sorted_leaves.emplace_back(public_data_leaf_from_wire(x.leaf), x.index);
288+
}
289+
r.subtree_path = fr_vec_from_wire(w.subtreePath);
290+
return r;
291+
}
292+
293+
inline wire::BatchInsertionResultNullifier batch_nullifier_to_wire(
294+
const crypto::merkle_tree::BatchInsertionResult<crypto::merkle_tree::NullifierLeafValue>& d)
295+
{
296+
std::vector<wire::SortedNullifierLeaf> sorted;
297+
sorted.reserve(d.sorted_leaves.size());
298+
for (const auto& [leaf, index] : d.sorted_leaves) {
299+
sorted.push_back({ .leaf = nullifier_leaf_to_wire(leaf), .index = index });
300+
}
301+
return { .lowLeafWitnessData =
302+
vec_to_wire<wire::NullifierLeafUpdateWitnessData>(d.low_leaf_witness_data, nullifier_witness_to_wire),
303+
.sortedLeaves = std::move(sorted),
304+
.subtreePath = fr_vec_to_wire(d.subtree_path) };
305+
}
306+
307+
inline crypto::merkle_tree::BatchInsertionResult<crypto::merkle_tree::NullifierLeafValue> batch_nullifier_from_wire(
308+
const wire::BatchInsertionResultNullifier& w)
309+
{
310+
crypto::merkle_tree::BatchInsertionResult<crypto::merkle_tree::NullifierLeafValue> r;
311+
r.low_leaf_witness_data =
312+
vec_from_wire<crypto::merkle_tree::LeafUpdateWitnessData<crypto::merkle_tree::NullifierLeafValue>>(
313+
w.lowLeafWitnessData, nullifier_witness_from_wire);
314+
r.sorted_leaves.reserve(w.sortedLeaves.size());
315+
for (const auto& x : w.sortedLeaves) {
316+
r.sorted_leaves.emplace_back(nullifier_leaf_from_wire(x.leaf), x.index);
317+
}
318+
r.subtree_path = fr_vec_from_wire(w.subtreePath);
319+
return r;
320+
}
321+
322+
inline wire::SequentialInsertionResultPublicData sequential_public_data_to_wire(
323+
const crypto::merkle_tree::SequentialInsertionResult<crypto::merkle_tree::PublicDataLeafValue>& d)
324+
{
325+
return { .lowLeafWitnessData = vec_to_wire<wire::PublicDataLeafUpdateWitnessData>(d.low_leaf_witness_data,
326+
public_data_witness_to_wire),
327+
.insertionWitnessData = vec_to_wire<wire::PublicDataLeafUpdateWitnessData>(d.insertion_witness_data,
328+
public_data_witness_to_wire) };
329+
}
330+
331+
inline crypto::merkle_tree::SequentialInsertionResult<crypto::merkle_tree::PublicDataLeafValue>
332+
sequential_public_data_from_wire(const wire::SequentialInsertionResultPublicData& w)
333+
{
334+
return { .low_leaf_witness_data =
335+
vec_from_wire<crypto::merkle_tree::LeafUpdateWitnessData<crypto::merkle_tree::PublicDataLeafValue>>(
336+
w.lowLeafWitnessData, public_data_witness_from_wire),
337+
.insertion_witness_data =
338+
vec_from_wire<crypto::merkle_tree::LeafUpdateWitnessData<crypto::merkle_tree::PublicDataLeafValue>>(
339+
w.insertionWitnessData, public_data_witness_from_wire) };
340+
}
341+
342+
inline wire::SequentialInsertionResultNullifier sequential_nullifier_to_wire(
343+
const crypto::merkle_tree::SequentialInsertionResult<crypto::merkle_tree::NullifierLeafValue>& d)
344+
{
345+
return { .lowLeafWitnessData =
346+
vec_to_wire<wire::NullifierLeafUpdateWitnessData>(d.low_leaf_witness_data, nullifier_witness_to_wire),
347+
.insertionWitnessData = vec_to_wire<wire::NullifierLeafUpdateWitnessData>(d.insertion_witness_data,
348+
nullifier_witness_to_wire) };
349+
}
350+
351+
inline crypto::merkle_tree::SequentialInsertionResult<crypto::merkle_tree::NullifierLeafValue>
352+
sequential_nullifier_from_wire(const wire::SequentialInsertionResultNullifier& w)
353+
{
354+
return { .low_leaf_witness_data =
355+
vec_from_wire<crypto::merkle_tree::LeafUpdateWitnessData<crypto::merkle_tree::NullifierLeafValue>>(
356+
w.lowLeafWitnessData, nullifier_witness_from_wire),
357+
.insertion_witness_data =
358+
vec_from_wire<crypto::merkle_tree::LeafUpdateWitnessData<crypto::merkle_tree::NullifierLeafValue>>(
359+
w.insertionWitnessData, nullifier_witness_from_wire) };
360+
}
361+
362+
} // namespace bb::wsdb

0 commit comments

Comments
 (0)