Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
wsdb_wire_convert.hpp
Go to the documentation of this file.
1#pragma once
13#include "barretenberg/wsdb/generated/wsdb_types.hpp"
14
15namespace bb::wsdb {
16
17inline Fr fr_to_wire(const bb::fr& d)
18{
19 Fr r{};
21 return r;
22}
23
24inline bb::fr fr_from_wire(const Fr& w)
25{
27}
28
29inline BlockHeaderHash block_header_hash_to_wire(const bb::fr& d)
30{
31 BlockHeaderHash r{};
33 return r;
34}
35
36inline bb::fr block_header_hash_from_wire(const BlockHeaderHash& w)
37{
38 return bb::fr::serialize_from_buffer(w.data());
39}
40
41inline PublicDataSlot public_data_slot_to_wire(const bb::fr& d)
42{
43 PublicDataSlot r{};
45 return r;
46}
47
48inline bb::fr public_data_slot_from_wire(const PublicDataSlot& w)
49{
50 return bb::fr::serialize_from_buffer(w.data());
51}
52
53inline PublicDataValue public_data_value_to_wire(const bb::fr& d)
54{
55 PublicDataValue r{};
57 return r;
58}
59
60inline bb::fr public_data_value_from_wire(const PublicDataValue& w)
61{
62 return bb::fr::serialize_from_buffer(w.data());
63}
64
65inline Nullifier nullifier_to_wire(const bb::fr& d)
66{
67 Nullifier r{};
69 return r;
70}
71
72inline bb::fr nullifier_from_wire(const Nullifier& w)
73{
74 return bb::fr::serialize_from_buffer(w.data());
75}
76
77inline std::vector<Fr> fr_vec_to_wire(const std::vector<bb::fr>& d)
78{
79 std::vector<Fr> r;
80 r.reserve(d.size());
81 for (const auto& x : d) {
82 r.push_back(fr_to_wire(x));
83 }
84 return r;
85}
86
87inline std::vector<bb::fr> fr_vec_from_wire(const std::vector<Fr>& w)
88{
89 std::vector<bb::fr> r;
90 r.reserve(w.size());
91 for (const auto& x : w) {
92 r.push_back(fr_from_wire(x));
93 }
94 return r;
95}
96
97inline wire::WorldStateRevision revision_to_wire(const world_state::WorldStateRevision& d)
98{
99 return wire::WorldStateRevision{
100 .forkId = d.forkId,
101 .blockNumber = d.blockNumber,
102 .includeUncommitted = d.includeUncommitted,
103 };
104}
105
106inline world_state::WorldStateRevision revision_from_wire(const wire::WorldStateRevision& w)
107{
109 .forkId = w.forkId,
110 .blockNumber = w.blockNumber,
111 .includeUncommitted = w.includeUncommitted,
112 };
113}
114
116{
117 return static_cast<MerkleTreeId>(d);
118}
119
124
126{
127 return { .slot = public_data_slot_to_wire(d.slot), .value = public_data_value_to_wire(d.value) };
128}
129
131{
132 return { public_data_slot_from_wire(w.slot), public_data_value_from_wire(w.value) };
133}
134
137{
139 r.reserve(d.size());
140 for (const auto& x : d) {
141 r.push_back(public_data_leaf_to_wire(x));
142 }
143 return r;
144}
145
148{
150 r.reserve(w.size());
151 for (const auto& x : w) {
152 r.push_back(public_data_leaf_from_wire(x));
153 }
154 return r;
155}
156
158{
159 return { .nullifier = nullifier_to_wire(d.nullifier) };
160}
161
163{
164 return { nullifier_from_wire(w.nullifier) };
165}
166
169{
171 r.reserve(d.size());
172 for (const auto& x : d) {
173 r.push_back(nullifier_leaf_to_wire(x));
174 }
175 return r;
176}
177
180{
182 r.reserve(w.size());
183 for (const auto& x : w) {
184 r.push_back(nullifier_leaf_from_wire(x));
185 }
186 return r;
187}
188
189inline wire::IndexedPublicDataLeafValue indexed_public_data_leaf_to_wire(
191{
192 return { .leaf = public_data_leaf_to_wire(d.leaf), .nextIndex = d.nextIndex, .nextKey = fr_to_wire(d.nextKey) };
193}
194
196 const wire::IndexedPublicDataLeafValue& w)
197{
198 return { public_data_leaf_from_wire(w.leaf), w.nextIndex, fr_from_wire(w.nextKey) };
199}
200
201inline wire::IndexedNullifierLeafValue indexed_nullifier_leaf_to_wire(
203{
204 return { .leaf = nullifier_leaf_to_wire(d.leaf), .nextIndex = d.nextIndex, .nextKey = fr_to_wire(d.nextKey) };
205}
206
208 const wire::IndexedNullifierLeafValue& w)
209{
210 return { nullifier_leaf_from_wire(w.leaf), w.nextIndex, fr_from_wire(w.nextKey) };
211}
212
213inline wire::PublicDataLeafUpdateWitnessData public_data_witness_to_wire(
215{
216 return { .leaf = indexed_public_data_leaf_to_wire(d.leaf), .index = d.index, .path = fr_vec_to_wire(d.path) };
217}
218
220public_data_witness_from_wire(const wire::PublicDataLeafUpdateWitnessData& w)
221{
222 return { indexed_public_data_leaf_from_wire(w.leaf), w.index, fr_vec_from_wire(w.path) };
223}
224
225inline wire::NullifierLeafUpdateWitnessData nullifier_witness_to_wire(
227{
228 return { .leaf = indexed_nullifier_leaf_to_wire(d.leaf), .index = d.index, .path = fr_vec_to_wire(d.path) };
229}
230
232 const wire::NullifierLeafUpdateWitnessData& w)
233{
234 return { indexed_nullifier_leaf_from_wire(w.leaf), w.index, fr_vec_from_wire(w.path) };
235}
236
237template <typename Wire, typename Domain, typename Fn>
239{
241 r.reserve(d.size());
242 for (const auto& x : d) {
243 r.push_back(fn(x));
244 }
245 return r;
246}
247
248template <typename Domain, typename Wire, typename Fn>
250{
252 r.reserve(w.size());
253 for (const auto& x : w) {
254 r.push_back(fn(x));
255 }
256 return r;
257}
258
259inline wire::BatchInsertionResultPublicData batch_public_data_to_wire(
261{
263 sorted.reserve(d.sorted_leaves.size());
264 for (const auto& [leaf, index] : d.sorted_leaves) {
265 sorted.push_back({ .leaf = public_data_leaf_to_wire(leaf), .index = index });
266 }
267 return { .lowLeafWitnessData = vec_to_wire<wire::PublicDataLeafUpdateWitnessData>(d.low_leaf_witness_data,
269 .sortedLeaves = std::move(sorted),
270 .subtreePath = fr_vec_to_wire(d.subtree_path) };
271}
272
274 const wire::BatchInsertionResultPublicData& w)
275{
278 vec_from_wire<crypto::merkle_tree::LeafUpdateWitnessData<crypto::merkle_tree::PublicDataLeafValue>>(
279 w.lowLeafWitnessData, public_data_witness_from_wire);
280 r.sorted_leaves.reserve(w.sortedLeaves.size());
281 for (const auto& x : w.sortedLeaves) {
282 r.sorted_leaves.emplace_back(public_data_leaf_from_wire(x.leaf), x.index);
283 }
284 r.subtree_path = fr_vec_from_wire(w.subtreePath);
285 return r;
286}
287
288inline wire::BatchInsertionResultNullifier batch_nullifier_to_wire(
290{
292 sorted.reserve(d.sorted_leaves.size());
293 for (const auto& [leaf, index] : d.sorted_leaves) {
294 sorted.push_back({ .leaf = nullifier_leaf_to_wire(leaf), .index = index });
295 }
296 return { .lowLeafWitnessData =
297 vec_to_wire<wire::NullifierLeafUpdateWitnessData>(d.low_leaf_witness_data, nullifier_witness_to_wire),
298 .sortedLeaves = std::move(sorted),
299 .subtreePath = fr_vec_to_wire(d.subtree_path) };
300}
301
303 const wire::BatchInsertionResultNullifier& w)
304{
307 vec_from_wire<crypto::merkle_tree::LeafUpdateWitnessData<crypto::merkle_tree::NullifierLeafValue>>(
308 w.lowLeafWitnessData, nullifier_witness_from_wire);
309 r.sorted_leaves.reserve(w.sortedLeaves.size());
310 for (const auto& x : w.sortedLeaves) {
311 r.sorted_leaves.emplace_back(nullifier_leaf_from_wire(x.leaf), x.index);
312 }
313 r.subtree_path = fr_vec_from_wire(w.subtreePath);
314 return r;
315}
316
317inline wire::SequentialInsertionResultPublicData sequential_public_data_to_wire(
319{
320 return { .lowLeafWitnessData = vec_to_wire<wire::PublicDataLeafUpdateWitnessData>(d.low_leaf_witness_data,
322 .insertionWitnessData = vec_to_wire<wire::PublicDataLeafUpdateWitnessData>(d.insertion_witness_data,
324}
325
327sequential_public_data_from_wire(const wire::SequentialInsertionResultPublicData& w)
328{
329 return { .low_leaf_witness_data =
330 vec_from_wire<crypto::merkle_tree::LeafUpdateWitnessData<crypto::merkle_tree::PublicDataLeafValue>>(
331 w.lowLeafWitnessData, public_data_witness_from_wire),
332 .insertion_witness_data =
334 w.insertionWitnessData, public_data_witness_from_wire) };
335}
336
337inline wire::SequentialInsertionResultNullifier sequential_nullifier_to_wire(
339{
340 return { .lowLeafWitnessData =
341 vec_to_wire<wire::NullifierLeafUpdateWitnessData>(d.low_leaf_witness_data, nullifier_witness_to_wire),
342 .insertionWitnessData = vec_to_wire<wire::NullifierLeafUpdateWitnessData>(d.insertion_witness_data,
344}
345
347 const wire::SequentialInsertionResultNullifier& w)
348{
349 return { .low_leaf_witness_data =
350 vec_from_wire<crypto::merkle_tree::LeafUpdateWitnessData<crypto::merkle_tree::NullifierLeafValue>>(
351 w.lowLeafWitnessData, nullifier_witness_from_wire),
352 .insertion_witness_data =
354 w.insertionWitnessData, nullifier_witness_from_wire) };
355}
356
358{
360 r.reserve(d.size());
361 for (const auto& [tree_id, tree_ref] : d) {
362 r.push_back(
363 { .treeId = tree_id_to_wire(tree_id), .root = fr_to_wire(tree_ref.first), .size = tree_ref.second });
364 }
365 return r;
366}
367
369{
371 r.reserve(w.size());
372 for (const auto& entry : w) {
373 r.emplace(tree_id_from_wire(entry.treeId),
374 world_state::TreeStateReference{ fr_from_wire(entry.root), entry.size });
375 }
376 return r;
377}
378
379inline wire::DBStats db_stats_to_wire(const bb::lmdblib::DBStats& d)
380{
381 return { .name = d.name, .numDataItems = d.numDataItems, .totalUsedSize = d.totalUsedSize };
382}
383
384inline bb::lmdblib::DBStats db_stats_from_wire(const wire::DBStats& w)
385{
386 return bb::lmdblib::DBStats(w.name, w.numDataItems, w.totalUsedSize);
387}
388
390{
391 return { .mapSize = d.mapSize,
392 .physicalFileSize = d.physicalFileSize,
393 .blocksDBStats = db_stats_to_wire(d.blocksDBStats),
394 .nodesDBStats = db_stats_to_wire(d.nodesDBStats),
395 .leafPreimagesDBStats = db_stats_to_wire(d.leafPreimagesDBStats),
396 .leafIndicesDBStats = db_stats_to_wire(d.leafIndicesDBStats),
397 .blockIndicesDBStats = db_stats_to_wire(d.blockIndicesDBStats) };
398}
399
401{
402 return { w.mapSize,
403 w.physicalFileSize,
404 db_stats_from_wire(w.blocksDBStats),
405 db_stats_from_wire(w.nodesDBStats),
406 db_stats_from_wire(w.leafPreimagesDBStats),
407 db_stats_from_wire(w.leafIndicesDBStats),
408 db_stats_from_wire(w.blockIndicesDBStats) };
409}
410
412{
413 return { .name = d.name,
414 .depth = d.depth,
415 .size = d.size,
416 .committedSize = d.committedSize,
417 .root = fr_to_wire(d.root),
418 .initialSize = d.initialSize,
419 .initialRoot = fr_to_wire(d.initialRoot),
420 .oldestHistoricBlock = d.oldestHistoricBlock,
421 .unfinalizedBlockHeight = d.unfinalizedBlockHeight,
422 .finalizedBlockHeight = d.finalizedBlockHeight };
423}
424
426{
427 return { w.name,
428 w.depth,
429 w.size,
430 w.committedSize,
431 fr_from_wire(w.root),
432 w.initialSize,
433 fr_from_wire(w.initialRoot),
434 w.oldestHistoricBlock,
435 w.unfinalizedBlockHeight,
436 w.finalizedBlockHeight };
437}
438
440{
441 return { .noteHashTreeStats = tree_db_stats_to_wire(d.noteHashTreeStats),
442 .messageTreeStats = tree_db_stats_to_wire(d.messageTreeStats),
443 .archiveTreeStats = tree_db_stats_to_wire(d.archiveTreeStats),
444 .publicDataTreeStats = tree_db_stats_to_wire(d.publicDataTreeStats),
445 .nullifierTreeStats = tree_db_stats_to_wire(d.nullifierTreeStats) };
446}
447
449{
450 return { tree_db_stats_from_wire(w.noteHashTreeStats),
451 tree_db_stats_from_wire(w.messageTreeStats),
452 tree_db_stats_from_wire(w.archiveTreeStats),
453 tree_db_stats_from_wire(w.publicDataTreeStats),
454 tree_db_stats_from_wire(w.nullifierTreeStats) };
455}
456
458{
459 return { .noteHashTreeMeta = tree_meta_to_wire(d.noteHashTreeMeta),
460 .messageTreeMeta = tree_meta_to_wire(d.messageTreeMeta),
461 .archiveTreeMeta = tree_meta_to_wire(d.archiveTreeMeta),
462 .publicDataTreeMeta = tree_meta_to_wire(d.publicDataTreeMeta),
463 .nullifierTreeMeta = tree_meta_to_wire(d.nullifierTreeMeta) };
464}
465
467{
468 return { tree_meta_from_wire(w.noteHashTreeMeta),
469 tree_meta_from_wire(w.messageTreeMeta),
470 tree_meta_from_wire(w.archiveTreeMeta),
471 tree_meta_from_wire(w.publicDataTreeMeta),
472 tree_meta_from_wire(w.nullifierTreeMeta) };
473}
474
475inline wire::WorldStateStatusSummary world_state_status_summary_to_wire(
477{
478 return { .unfinalizedBlockNumber = d.unfinalizedBlockNumber,
479 .finalizedBlockNumber = d.finalizedBlockNumber,
480 .oldestHistoricalBlock = d.oldestHistoricalBlock,
481 .treesAreSynched = d.treesAreSynched };
482}
483
485 const wire::WorldStateStatusSummary& w)
486{
487 return { w.unfinalizedBlockNumber, w.finalizedBlockNumber, w.oldestHistoricalBlock, w.treesAreSynched };
488}
489
491{
492 return { .summary = world_state_status_summary_to_wire(d.summary),
494 .meta = world_state_meta_to_wire(d.meta) };
495}
496
498{
499 return { world_state_status_summary_from_wire(w.summary),
502}
503
504} // namespace bb::wsdb
std::pair< bb::fr, bb::crypto::merkle_tree::index_t > TreeStateReference
Definition types.hpp:32
std::unordered_map< MerkleTreeId, TreeStateReference > StateReference
Definition types.hpp:33
world_state::StateReference state_reference_from_wire(const std::vector< wire::TreeStateReference > &w)
std::vector< bb::fr > fr_vec_from_wire(const std::vector< Fr > &w)
std::vector< crypto::merkle_tree::PublicDataLeafValue > public_data_leaf_vec_from_wire(const std::vector< wire::PublicDataLeafValue > &w)
crypto::merkle_tree::NullifierLeafValue nullifier_leaf_from_wire(const wire::NullifierLeafValue &w)
std::vector< wire::PublicDataLeafValue > public_data_leaf_vec_to_wire(const std::vector< crypto::merkle_tree::PublicDataLeafValue > &d)
std::vector< Fr > fr_vec_to_wire(const std::vector< bb::fr > &d)
world_state::SequentialInsertionResult< crypto::merkle_tree::NullifierLeafValue > sequential_nullifier_from_wire(const wire::SequentialInsertionResultNullifier &w)
wire::SequentialInsertionResultNullifier sequential_nullifier_to_wire(const world_state::SequentialInsertionResult< crypto::merkle_tree::NullifierLeafValue > &d)
wire::BatchInsertionResultPublicData batch_public_data_to_wire(const world_state::BatchInsertionResult< crypto::merkle_tree::PublicDataLeafValue > &d)
std::vector< crypto::merkle_tree::NullifierLeafValue > nullifier_leaf_vec_from_wire(const std::vector< wire::NullifierLeafValue > &w)
std::vector< wire::NullifierLeafValue > nullifier_leaf_vec_to_wire(const std::vector< crypto::merkle_tree::NullifierLeafValue > &d)
wire::NullifierLeafValue nullifier_leaf_to_wire(const crypto::merkle_tree::NullifierLeafValue &d)
wire::WorldStateRevision revision_to_wire(const world_state::WorldStateRevision &d)
wire::IndexedNullifierLeafValue indexed_nullifier_leaf_to_wire(const crypto::merkle_tree::IndexedLeaf< crypto::merkle_tree::NullifierLeafValue > &d)
wire::TreeDBStats tree_db_stats_to_wire(const bb::crypto::merkle_tree::TreeDBStats &d)
crypto::merkle_tree::PublicDataLeafValue public_data_leaf_from_wire(const wire::PublicDataLeafValue &w)
bb::crypto::merkle_tree::TreeDBStats tree_db_stats_from_wire(const wire::TreeDBStats &w)
wire::TreeMeta tree_meta_to_wire(const bb::crypto::merkle_tree::TreeMeta &d)
wire::WorldStateMeta world_state_meta_to_wire(const bb::world_state::WorldStateMeta &d)
wire::DBStats db_stats_to_wire(const bb::lmdblib::DBStats &d)
world_state::WorldStateRevision revision_from_wire(const wire::WorldStateRevision &w)
bb::fr public_data_slot_from_wire(const PublicDataSlot &w)
world_state::BatchInsertionResult< crypto::merkle_tree::NullifierLeafValue > batch_nullifier_from_wire(const wire::BatchInsertionResultNullifier &w)
world_state::MerkleTreeId tree_id_from_wire(MerkleTreeId w)
wire::SequentialInsertionResultPublicData sequential_public_data_to_wire(const world_state::SequentialInsertionResult< crypto::merkle_tree::PublicDataLeafValue > &d)
wire::WorldStateDBStats world_state_db_stats_to_wire(const bb::world_state::WorldStateDBStats &d)
wire::IndexedPublicDataLeafValue indexed_public_data_leaf_to_wire(const crypto::merkle_tree::IndexedLeaf< crypto::merkle_tree::PublicDataLeafValue > &d)
crypto::merkle_tree::LeafUpdateWitnessData< crypto::merkle_tree::PublicDataLeafValue > public_data_witness_from_wire(const wire::PublicDataLeafUpdateWitnessData &w)
world_state::BatchInsertionResult< crypto::merkle_tree::PublicDataLeafValue > batch_public_data_from_wire(const wire::BatchInsertionResultPublicData &w)
bb::lmdblib::DBStats db_stats_from_wire(const wire::DBStats &w)
BlockHeaderHash block_header_hash_to_wire(const bb::fr &d)
std::vector< Domain > vec_from_wire(const std::vector< Wire > &w, Fn fn)
crypto::merkle_tree::IndexedLeaf< crypto::merkle_tree::PublicDataLeafValue > indexed_public_data_leaf_from_wire(const wire::IndexedPublicDataLeafValue &w)
Fr fr_to_wire(const bb::fr &d)
wire::BatchInsertionResultNullifier batch_nullifier_to_wire(const world_state::BatchInsertionResult< crypto::merkle_tree::NullifierLeafValue > &d)
crypto::merkle_tree::LeafUpdateWitnessData< crypto::merkle_tree::NullifierLeafValue > nullifier_witness_from_wire(const wire::NullifierLeafUpdateWitnessData &w)
bb::world_state::WorldStateDBStats world_state_db_stats_from_wire(const wire::WorldStateDBStats &w)
wire::WorldStateStatusFull world_state_status_full_to_wire(const bb::world_state::WorldStateStatusFull &d)
world_state::SequentialInsertionResult< crypto::merkle_tree::PublicDataLeafValue > sequential_public_data_from_wire(const wire::SequentialInsertionResultPublicData &w)
bb::fr public_data_value_from_wire(const PublicDataValue &w)
MerkleTreeId tree_id_to_wire(world_state::MerkleTreeId d)
wire::PublicDataLeafValue public_data_leaf_to_wire(const crypto::merkle_tree::PublicDataLeafValue &d)
bb::fr nullifier_from_wire(const Nullifier &w)
bb::fr block_header_hash_from_wire(const BlockHeaderHash &w)
PublicDataValue public_data_value_to_wire(const bb::fr &d)
bb::fr fr_from_wire(const Fr &w)
crypto::merkle_tree::IndexedLeaf< crypto::merkle_tree::NullifierLeafValue > indexed_nullifier_leaf_from_wire(const wire::IndexedNullifierLeafValue &w)
bb::world_state::WorldStateStatusSummary world_state_status_summary_from_wire(const wire::WorldStateStatusSummary &w)
bb::world_state::WorldStateMeta world_state_meta_from_wire(const wire::WorldStateMeta &w)
wire::NullifierLeafUpdateWitnessData nullifier_witness_to_wire(const crypto::merkle_tree::LeafUpdateWitnessData< crypto::merkle_tree::NullifierLeafValue > &d)
Nullifier nullifier_to_wire(const bb::fr &d)
std::vector< Wire > vec_to_wire(const std::vector< Domain > &d, Fn fn)
wire::PublicDataLeafUpdateWitnessData public_data_witness_to_wire(const crypto::merkle_tree::LeafUpdateWitnessData< crypto::merkle_tree::PublicDataLeafValue > &d)
bb::crypto::merkle_tree::TreeMeta tree_meta_from_wire(const wire::TreeMeta &w)
bb::world_state::WorldStateStatusFull world_state_status_full_from_wire(const wire::WorldStateStatusFull &w)
wire::WorldStateStatusSummary world_state_status_summary_to_wire(const bb::world_state::WorldStateStatusSummary &d)
PublicDataSlot public_data_slot_to_wire(const bb::fr &d)
std::vector< wire::TreeStateReference > state_reference_to_wire(const world_state::StateReference &d)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
static field serialize_from_buffer(const uint8_t *buffer)
static void serialize_to_buffer(const field &value, uint8_t *buffer)
uint64_t totalUsedSize
Definition types.hpp:25
uint64_t numDataItems
Definition types.hpp:24
std::string name
Definition types.hpp:23
std::vector< crypto::merkle_tree::LeafUpdateWitnessData< LeafValueType > > low_leaf_witness_data
std::vector< std::pair< LeafValueType, index_t > > sorted_leaves
crypto::merkle_tree::fr_sibling_path subtree_path
std::vector< crypto::merkle_tree::LeafUpdateWitnessData< LeafValueType > > low_leaf_witness_data
std::vector< crypto::merkle_tree::LeafUpdateWitnessData< LeafValueType > > insertion_witness_data
WorldStateStatusSummary summary
Definition types.hpp:222