Skip to content

Commit e0aac62

Browse files
committed
refactor(bbapi,wsdb): replace remaining msgpack_roundtrip with field-by-field converters
Eliminate the two remaining handler-boundary msgpack_roundtrip sites: bbapi: add fq2_{to,from}_wire (just c0/c1 bridged via field_{to,from}_wire<bb::fq>) and use it for bn254_g2_point_{to,from}_wire. The `detail::msgpack_roundtrip` template is gone. wsdb: add field-by-field converters for the nested status aggregates: - db_stats_{to,from}_wire (lmdblib::DBStats) - tree_db_stats_{to,from}_wire (crypto::merkle_tree::TreeDBStats) - tree_meta_{to,from}_wire (crypto::merkle_tree::TreeMeta, uses fr_{to,from}_wire for `root` / `initialRoot`) - world_state_db_stats_{to,from}_wire - world_state_meta_{to,from}_wire - world_state_status_summary_{to,from}_wire - world_state_status_full_{to,from}_wire The 6 wsdb_handlers.cpp call sites swap to the named helpers. The `msgpack_roundtrip_{to,from}_wire` templates are gone. Net effect: zero handler-boundary pack-then-unpack. Every wire ↔ domain crossing in bbapi and wsdb is now an explicit field-by-field walk; field renames and type drift surface at compile time. (The unrelated `msgpack_roundtrip` test helper in serialize/test_helper.hpp stays — it's a serde-equality assertion, not a type-bridge.)
1 parent 875aff9 commit e0aac62

3 files changed

Lines changed: 157 additions & 63 deletions

File tree

barretenberg/cpp/src/barretenberg/bbapi/bbapi_wire_convert.hpp

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,11 @@
1111
* aliases) and domain field types (`bb::fr`, `bb::fq`, `uint256_t`, …)
1212
* share a 32-byte msgpack `bin32` encoding, so the byte-level conversion
1313
* is a `serialize_to_buffer` / `serialize_from_buffer` call.
14-
*
15-
* The one residual `msgpack_roundtrip` usage is inside
16-
* `bn254_g2_point_{to,from}_wire`: the Fq2 nesting on g2 affine_element
17-
* makes the manual form genuinely uglier, and the call site is exactly
18-
* one (`Bn254G2Mul`).
1914
*/
2015
#include "barretenberg/bbapi/generated/bb_types.hpp"
2116
#include "barretenberg/ecc/curves/bn254/bn254.hpp"
2217
#include "barretenberg/ecc/curves/bn254/fq.hpp"
18+
#include "barretenberg/ecc/curves/bn254/fq2.hpp"
2319
#include "barretenberg/ecc/curves/bn254/fr.hpp"
2420
#include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp"
2521
#include "barretenberg/ecc/curves/secp256k1/secp256k1.hpp"
@@ -33,25 +29,6 @@
3329

3430
namespace bb::bbapi {
3531

36-
// ---------------------------------------------------------------------------
37-
// Local msgpack roundtrip — only used by bn254_g2_point conversions below,
38-
// where Fq2 nesting on g2 affine_element makes the manual form intricate
39-
// enough that a generic pack+unpack pair is the cleanest available bridge.
40-
// Restricted to a single call site (`Bn254G2Mul`); not exposed for general use.
41-
// ---------------------------------------------------------------------------
42-
43-
namespace detail {
44-
template <typename Target, typename Source> inline Target msgpack_roundtrip(const Source& src)
45-
{
46-
msgpack::sbuffer buf;
47-
msgpack::pack(buf, src);
48-
auto unpacked = msgpack::unpack(buf.data(), buf.size());
49-
Target target;
50-
unpacked.get().convert(target);
51-
return target;
52-
}
53-
} // namespace detail
54-
5532
// ---------------------------------------------------------------------------
5633
// Field element conversions. All field types (bb::fr, bb::fq, grumpkin::fr,
5734
// grumpkin::fq, secp256k1::*, secp256r1::*) pack as msgpack bin32. Wire `Fr`
@@ -198,14 +175,25 @@ inline bb::g1::affine_element bn254_g1_point_from_wire(const wire::Bn254G1Point&
198175
return { field_from_wire<bb::fq>(w.x), field_from_wire<bb::fq>(w.y) };
199176
}
200177

178+
// Fq2 = { c0: bb::fq, c1: bb::fq }; wire field2 = std::array<std::array<uint8_t,32>, 2>.
179+
inline std::array<std::array<uint8_t, 32>, 2> fq2_to_wire(const bb::fq2& d)
180+
{
181+
return { field_to_wire<bb::fq>(d.c0), field_to_wire<bb::fq>(d.c1) };
182+
}
183+
184+
inline bb::fq2 fq2_from_wire(const std::array<std::array<uint8_t, 32>, 2>& w)
185+
{
186+
return { field_from_wire<bb::fq>(w[0]), field_from_wire<bb::fq>(w[1]) };
187+
}
188+
201189
inline wire::Bn254G2Point bn254_g2_point_to_wire(const bb::g2::affine_element& d)
202190
{
203-
return detail::msgpack_roundtrip<wire::Bn254G2Point>(d);
191+
return { .x = fq2_to_wire(d.x), .y = fq2_to_wire(d.y) };
204192
}
205193

206194
inline bb::g2::affine_element bn254_g2_point_from_wire(const wire::Bn254G2Point& w)
207195
{
208-
return detail::msgpack_roundtrip<bb::g2::affine_element>(w);
196+
return { fq2_from_wire(w.x), fq2_from_wire(w.y) };
209197
}
210198

211199
inline wire::Secp256k1Point secp256k1_point_to_wire(const secp256k1::g1::affine_element& d)

barretenberg/cpp/src/barretenberg/wsdb/wsdb_handlers.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ wire::WsdbCommitResponse handle_commit(WsdbRequest& ctx, wire::WsdbCommit&&)
354354
WorldStateStatusFull status;
355355
ctx.world_state.commit(status);
356356
return wire::WsdbCommitResponse{
357-
.status = msgpack_roundtrip_to_wire<wire::WorldStateStatusFull>(status),
357+
.status = world_state_status_full_to_wire(status),
358358
};
359359
}
360360

@@ -394,7 +394,7 @@ wire::WsdbSyncBlockResponse handle_sync_block(WsdbRequest& ctx, wire::WsdbSyncBl
394394
padded_nullifiers,
395395
public_data_writes);
396396
return wire::WsdbSyncBlockResponse{
397-
.status = msgpack_roundtrip_to_wire<wire::WorldStateStatusFull>(status),
397+
.status = world_state_status_full_to_wire(status),
398398
};
399399
}
400400

@@ -423,15 +423,15 @@ wire::WsdbFinalizeBlocksResponse handle_finalize_blocks(WsdbRequest& ctx, wire::
423423
{
424424
WorldStateStatusSummary status = ctx.world_state.set_finalized_blocks(cmd.toBlockNumber);
425425
return wire::WsdbFinalizeBlocksResponse{
426-
.status = msgpack_roundtrip_to_wire<wire::WorldStateStatusSummary>(status),
426+
.status = world_state_status_summary_to_wire(status),
427427
};
428428
}
429429

430430
wire::WsdbUnwindBlocksResponse handle_unwind_blocks(WsdbRequest& ctx, wire::WsdbUnwindBlocks&& cmd)
431431
{
432432
WorldStateStatusFull status = ctx.world_state.unwind_blocks(cmd.toBlockNumber);
433433
return wire::WsdbUnwindBlocksResponse{
434-
.status = msgpack_roundtrip_to_wire<wire::WorldStateStatusFull>(status),
434+
.status = world_state_status_full_to_wire(status),
435435
};
436436
}
437437

@@ -440,7 +440,7 @@ wire::WsdbRemoveHistoricalBlocksResponse handle_remove_historical_blocks(WsdbReq
440440
{
441441
WorldStateStatusFull status = ctx.world_state.remove_historical_blocks(cmd.toBlockNumber);
442442
return wire::WsdbRemoveHistoricalBlocksResponse{
443-
.status = msgpack_roundtrip_to_wire<wire::WorldStateStatusFull>(status),
443+
.status = world_state_status_full_to_wire(status),
444444
};
445445
}
446446

@@ -453,7 +453,7 @@ wire::WsdbGetStatusResponse handle_get_status(WsdbRequest& ctx, wire::WsdbGetSta
453453
WorldStateStatusSummary status;
454454
ctx.world_state.get_status_summary(status);
455455
return wire::WsdbGetStatusResponse{
456-
.status = msgpack_roundtrip_to_wire<wire::WorldStateStatusSummary>(status),
456+
.status = world_state_status_summary_to_wire(status),
457457
};
458458
}
459459

barretenberg/cpp/src/barretenberg/wsdb/wsdb_wire_convert.hpp

Lines changed: 137 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
* @file wsdb_wire_convert.hpp
44
* @brief Wire <-> domain conversion helpers for the aztec-wsdb service.
55
*
6-
* The codegen-emitted wire types in generated/wsdb_types.hpp are POD-shaped
7-
* (uint32_t for tree IDs, std::array<uint8_t, 32> for field elements, etc).
8-
* Domain types come from world_state/, crypto/merkle_tree/, ecc/. This file
9-
* is the single place that translates between them — used by handlers (server
6+
* All conversions are field-by-field. The codegen-emitted wire types in
7+
* generated/wsdb_types.hpp are POD-shaped (uint32_t for tree IDs,
8+
* std::array<uint8_t, 32> for field elements, etc); domain types come from
9+
* world_state/, crypto/merkle_tree/, ecc/, lmdblib/. This file is the
10+
* single place that translates between them — used by handlers (server
1011
* side) and by wsdb_ipc_merkle_db.cpp (AVM client side).
1112
*/
13+
#include "barretenberg/crypto/merkle_tree/node_store/tree_meta.hpp"
1214
#include "barretenberg/crypto/merkle_tree/response.hpp"
1315
#include "barretenberg/crypto/merkle_tree/types.hpp"
1416
#include "barretenberg/ecc/curves/bn254/fr.hpp"
17+
#include "barretenberg/lmdblib/types.hpp"
1518
#include "barretenberg/world_state/types.hpp"
1619
#include "barretenberg/wsdb/generated/wsdb_types.hpp"
1720

@@ -113,33 +116,136 @@ inline world_state::StateReference state_reference_from_wire(
113116
return r;
114117
}
115118

116-
// Generic msgpack roundtrip — used only for `WorldStateStatusFull` and
117-
// `WorldStateStatusSummary`. Both are 4-level nested aggregates (Full →
118-
// DBStats → TreeDBStats → DBStats, and parallel Meta → TreeMeta with
119-
// `bb::fr root` fields, fanning out across all 5 trees). Hand-writing
120-
// field-by-field convertors is ~150 LOC of mechanical boilerplate that
121-
// would have to track future field additions on the world_state side.
122-
// The 6 call sites (commit / sync / finalize / unwind / remove_historical
123-
// / get_status) are all dominated by the merkle-tree work they bracket,
124-
// so the per-call pack+unpack overhead is not in the critical path.
125-
template <typename Wire, typename Domain> inline Wire msgpack_roundtrip_to_wire(const Domain& d)
126-
{
127-
msgpack::sbuffer buf;
128-
msgpack::pack(buf, d);
129-
auto unpacked = msgpack::unpack(buf.data(), buf.size());
130-
Wire w;
131-
unpacked.get().convert(w);
132-
return w;
133-
}
134-
135-
template <typename Domain, typename Wire> inline Domain msgpack_roundtrip_from_wire(const Wire& w)
136-
{
137-
msgpack::sbuffer buf;
138-
msgpack::pack(buf, w);
139-
auto unpacked = msgpack::unpack(buf.data(), buf.size());
140-
Domain d;
141-
unpacked.get().convert(d);
142-
return d;
119+
// World-state status conversions. Field-by-field walk of the
120+
// WorldStateStatusFull / WorldStateStatusSummary aggregates and their
121+
// nested DBStats / TreeDBStats / TreeMeta / WorldStateDBStats /
122+
// WorldStateMeta components. The wire types live in `bb::wsdb::wire`,
123+
// the domain types in `bb::lmdblib`, `bb::crypto::merkle_tree`,
124+
// `bb::world_state`.
125+
126+
inline wire::DBStats db_stats_to_wire(const bb::lmdblib::DBStats& d)
127+
{
128+
return { .name = d.name, .numDataItems = d.numDataItems, .totalUsedSize = d.totalUsedSize };
129+
}
130+
131+
inline bb::lmdblib::DBStats db_stats_from_wire(const wire::DBStats& w)
132+
{
133+
return bb::lmdblib::DBStats(w.name, w.numDataItems, w.totalUsedSize);
134+
}
135+
136+
inline wire::TreeDBStats tree_db_stats_to_wire(const bb::crypto::merkle_tree::TreeDBStats& d)
137+
{
138+
return { .mapSize = d.mapSize,
139+
.physicalFileSize = d.physicalFileSize,
140+
.blocksDBStats = db_stats_to_wire(d.blocksDBStats),
141+
.nodesDBStats = db_stats_to_wire(d.nodesDBStats),
142+
.leafPreimagesDBStats = db_stats_to_wire(d.leafPreimagesDBStats),
143+
.leafIndicesDBStats = db_stats_to_wire(d.leafIndicesDBStats),
144+
.blockIndicesDBStats = db_stats_to_wire(d.blockIndicesDBStats) };
145+
}
146+
147+
inline bb::crypto::merkle_tree::TreeDBStats tree_db_stats_from_wire(const wire::TreeDBStats& w)
148+
{
149+
return { w.mapSize,
150+
w.physicalFileSize,
151+
db_stats_from_wire(w.blocksDBStats),
152+
db_stats_from_wire(w.nodesDBStats),
153+
db_stats_from_wire(w.leafPreimagesDBStats),
154+
db_stats_from_wire(w.leafIndicesDBStats),
155+
db_stats_from_wire(w.blockIndicesDBStats) };
156+
}
157+
158+
inline wire::TreeMeta tree_meta_to_wire(const bb::crypto::merkle_tree::TreeMeta& d)
159+
{
160+
return { .name = d.name,
161+
.depth = d.depth,
162+
.size = d.size,
163+
.committedSize = d.committedSize,
164+
.root = fr_to_wire(d.root),
165+
.initialSize = d.initialSize,
166+
.initialRoot = fr_to_wire(d.initialRoot),
167+
.oldestHistoricBlock = d.oldestHistoricBlock,
168+
.unfinalizedBlockHeight = d.unfinalizedBlockHeight,
169+
.finalizedBlockHeight = d.finalizedBlockHeight };
170+
}
171+
172+
inline bb::crypto::merkle_tree::TreeMeta tree_meta_from_wire(const wire::TreeMeta& w)
173+
{
174+
return { w.name,
175+
w.depth,
176+
w.size,
177+
w.committedSize,
178+
fr_from_wire(w.root),
179+
w.initialSize,
180+
fr_from_wire(w.initialRoot),
181+
w.oldestHistoricBlock,
182+
w.unfinalizedBlockHeight,
183+
w.finalizedBlockHeight };
184+
}
185+
186+
inline wire::WorldStateDBStats world_state_db_stats_to_wire(const bb::world_state::WorldStateDBStats& d)
187+
{
188+
return { .noteHashTreeStats = tree_db_stats_to_wire(d.noteHashTreeStats),
189+
.messageTreeStats = tree_db_stats_to_wire(d.messageTreeStats),
190+
.archiveTreeStats = tree_db_stats_to_wire(d.archiveTreeStats),
191+
.publicDataTreeStats = tree_db_stats_to_wire(d.publicDataTreeStats),
192+
.nullifierTreeStats = tree_db_stats_to_wire(d.nullifierTreeStats) };
193+
}
194+
195+
inline bb::world_state::WorldStateDBStats world_state_db_stats_from_wire(const wire::WorldStateDBStats& w)
196+
{
197+
return { tree_db_stats_from_wire(w.noteHashTreeStats),
198+
tree_db_stats_from_wire(w.messageTreeStats),
199+
tree_db_stats_from_wire(w.archiveTreeStats),
200+
tree_db_stats_from_wire(w.publicDataTreeStats),
201+
tree_db_stats_from_wire(w.nullifierTreeStats) };
202+
}
203+
204+
inline wire::WorldStateMeta world_state_meta_to_wire(const bb::world_state::WorldStateMeta& d)
205+
{
206+
return { .noteHashTreeMeta = tree_meta_to_wire(d.noteHashTreeMeta),
207+
.messageTreeMeta = tree_meta_to_wire(d.messageTreeMeta),
208+
.archiveTreeMeta = tree_meta_to_wire(d.archiveTreeMeta),
209+
.publicDataTreeMeta = tree_meta_to_wire(d.publicDataTreeMeta),
210+
.nullifierTreeMeta = tree_meta_to_wire(d.nullifierTreeMeta) };
211+
}
212+
213+
inline bb::world_state::WorldStateMeta world_state_meta_from_wire(const wire::WorldStateMeta& w)
214+
{
215+
return { tree_meta_from_wire(w.noteHashTreeMeta),
216+
tree_meta_from_wire(w.messageTreeMeta),
217+
tree_meta_from_wire(w.archiveTreeMeta),
218+
tree_meta_from_wire(w.publicDataTreeMeta),
219+
tree_meta_from_wire(w.nullifierTreeMeta) };
220+
}
221+
222+
inline wire::WorldStateStatusSummary world_state_status_summary_to_wire(
223+
const bb::world_state::WorldStateStatusSummary& d)
224+
{
225+
return { .unfinalizedBlockNumber = d.unfinalizedBlockNumber,
226+
.finalizedBlockNumber = d.finalizedBlockNumber,
227+
.oldestHistoricalBlock = d.oldestHistoricalBlock,
228+
.treesAreSynched = d.treesAreSynched };
229+
}
230+
231+
inline bb::world_state::WorldStateStatusSummary world_state_status_summary_from_wire(
232+
const wire::WorldStateStatusSummary& w)
233+
{
234+
return { w.unfinalizedBlockNumber, w.finalizedBlockNumber, w.oldestHistoricalBlock, w.treesAreSynched };
235+
}
236+
237+
inline wire::WorldStateStatusFull world_state_status_full_to_wire(const bb::world_state::WorldStateStatusFull& d)
238+
{
239+
return { .summary = world_state_status_summary_to_wire(d.summary),
240+
.dbStats = world_state_db_stats_to_wire(d.dbStats),
241+
.meta = world_state_meta_to_wire(d.meta) };
242+
}
243+
244+
inline bb::world_state::WorldStateStatusFull world_state_status_full_from_wire(const wire::WorldStateStatusFull& w)
245+
{
246+
return { world_state_status_summary_from_wire(w.summary),
247+
world_state_db_stats_from_wire(w.dbStats),
248+
world_state_meta_from_wire(w.meta) };
143249
}
144250

145251
} // namespace bb::wsdb

0 commit comments

Comments
 (0)