|
3 | 3 | * @file wsdb_wire_convert.hpp |
4 | 4 | * @brief Wire <-> domain conversion helpers for the aztec-wsdb service. |
5 | 5 | * |
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 |
10 | 11 | * side) and by wsdb_ipc_merkle_db.cpp (AVM client side). |
11 | 12 | */ |
| 13 | +#include "barretenberg/crypto/merkle_tree/node_store/tree_meta.hpp" |
12 | 14 | #include "barretenberg/crypto/merkle_tree/response.hpp" |
13 | 15 | #include "barretenberg/crypto/merkle_tree/types.hpp" |
14 | 16 | #include "barretenberg/ecc/curves/bn254/fr.hpp" |
| 17 | +#include "barretenberg/lmdblib/types.hpp" |
15 | 18 | #include "barretenberg/world_state/types.hpp" |
16 | 19 | #include "barretenberg/wsdb/generated/wsdb_types.hpp" |
17 | 20 |
|
@@ -113,33 +116,136 @@ inline world_state::StateReference state_reference_from_wire( |
113 | 116 | return r; |
114 | 117 | } |
115 | 118 |
|
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) }; |
143 | 249 | } |
144 | 250 |
|
145 | 251 | } // namespace bb::wsdb |
0 commit comments