Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
test_fixtures.hpp
Go to the documentation of this file.
1#pragma once
2
9#include <cstdint>
10#include <gtest/gtest.h>
11#include <optional>
12
14
16 block_number_t blockNumber,
17 fr root,
18 bool expectedSuccess)
19{
20 BlockPayload blockData;
21 LMDBTreeStore::ReadTransaction::Ptr tx = db->create_read_transaction();
22 bool success = db->read_block_data(blockNumber, blockData, *tx);
23 EXPECT_EQ(success, expectedSuccess);
24 if (expectedSuccess) {
25 EXPECT_EQ(blockData.root, root);
26 }
27 NodePayload nodeData;
28 success = db->read_node(root, nodeData, *tx);
29 EXPECT_EQ(success, expectedSuccess);
30}
31
33 LMDBTreeStore::SharedPtr db, block_number_t blockNumber, fr root, bool expectedSuccess, bool expectedRootSuccess)
34{
35 BlockPayload blockData;
36 LMDBTreeStore::ReadTransaction::Ptr tx = db->create_read_transaction();
37 bool success = db->read_block_data(blockNumber, blockData, *tx);
38 EXPECT_EQ(success, expectedSuccess);
39 if (expectedSuccess) {
40 EXPECT_EQ(blockData.root, root);
41 }
42 NodePayload nodeData;
43 success = db->read_node(root, nodeData, *tx);
44 EXPECT_EQ(success, expectedRootSuccess);
45}
46
48 block_number_t blockNumber,
49 index_t expectedSize,
50 bool expectedSuccess)
51{
52 BlockPayload blockData;
53 LMDBTreeStore::ReadTransaction::Ptr tx = db->create_read_transaction();
54 bool success = db->read_block_data(blockNumber, blockData, *tx);
55 EXPECT_EQ(success, expectedSuccess);
56 if (expectedSuccess) {
57 EXPECT_EQ(blockData.size, expectedSize);
58 }
59}
60
62 LMDBTreeStore::SharedPtr db, fr leaf, index_t index, bool entryShouldBePresent, bool indexShouldBePresent)
63{
64 index_t retrieved = 0;
65 LMDBTreeStore::ReadTransaction::Ptr tx = db->create_read_transaction();
66 bool success = db->read_leaf_index(leaf, retrieved, *tx);
67 EXPECT_EQ(success, entryShouldBePresent);
68 if (entryShouldBePresent) {
69 EXPECT_EQ(index == retrieved, indexShouldBePresent);
70 }
71}
72
73inline void call_operation(std::function<void(std::function<void(const Response& response)>)> operation,
74 bool expected_success = true)
75{
76 Signal signal;
77 auto completion = [&](const Response& response) -> void {
78 EXPECT_EQ(response.success, expected_success);
79 signal.signal_level();
80 };
81 operation(completion);
82 signal.wait_for_level();
83}
84
85template <typename LeafType, typename Hash>
87{
88 LMDBTreeStore::ReadTransaction::Ptr tx = db->create_read_transaction();
89 IndexedLeaf<LeafType> fromStore;
90 bool success = db->read_leaf_by_hash(Hash::hash(leaf.get_hash_inputs()), fromStore, *tx);
91 EXPECT_EQ(success, shouldBePresent);
92 if (success) {
93 EXPECT_EQ(fromStore, leaf);
94 }
95}
96
97template <typename LeafValueType, typename TypeOfTree>
98void check_find_leaf_index(TypeOfTree& tree,
99 const std::vector<LeafValueType>& leaves,
100 const std::vector<std::optional<index_t>>& expected_indices,
101 bool expected_success,
102 bool includeUncommitted = true)
103{
104 Signal signal;
105 auto completion = [&](const TypedResponse<FindLeafIndexResponse>& response) -> void {
106 EXPECT_EQ(response.success, expected_success);
107 if (expected_success) {
108 EXPECT_EQ(response.inner.leaf_indices, expected_indices);
109 }
110 signal.signal_level();
111 };
112
113 tree.find_leaf_indices(leaves, includeUncommitted, completion);
114 signal.wait_for_level();
115}
116
117template <typename LeafValueType, typename TypeOfTree>
118void check_find_leaf_index_from(TypeOfTree& tree,
119 const std::vector<LeafValueType>& leaves,
120 index_t start_index,
121 const std::vector<std::optional<index_t>>& expected_indices,
122 bool expected_success,
123 bool includeUncommitted = true)
124{
125 Signal signal;
126 auto completion = [&](const TypedResponse<FindLeafIndexResponse>& response) -> void {
127 EXPECT_EQ(response.success, expected_success);
128 if (expected_success) {
129 EXPECT_EQ(response.inner.leaf_indices, expected_indices);
130 }
131 signal.signal_level();
132 };
133
134 tree.find_leaf_indices_from(leaves, start_index, includeUncommitted, completion);
135 signal.wait_for_level();
136}
137
138template <typename LeafValueType, typename TypeOfTree>
139void check_historic_find_leaf_index(TypeOfTree& tree,
140 const std::vector<LeafValueType>& leaves,
141 block_number_t blockNumber,
142 const std::vector<std::optional<index_t>>& expected_indices,
143 bool expected_success,
144 bool includeUncommitted = true)
145{
146 Signal signal;
147 auto completion = [&](const TypedResponse<FindLeafIndexResponse>& response) -> void {
148 EXPECT_EQ(response.success, expected_success);
149 if (expected_success) {
150 EXPECT_EQ(response.inner.leaf_indices, expected_indices);
151 }
152 signal.signal_level();
153 };
154
155 tree.find_leaf_indices(leaves, blockNumber, includeUncommitted, completion);
156 signal.wait_for_level();
157}
158
159template <typename LeafValueType, typename TypeOfTree>
161 const std::vector<LeafValueType>& leaves,
162 block_number_t blockNumber,
163 index_t start_index,
164 const std::vector<std::optional<index_t>>& expected_indices,
165 bool expected_success,
166 bool includeUncommitted = true)
167{
168 Signal signal;
169 auto completion = [&](const TypedResponse<FindLeafIndexResponse>& response) -> void {
170 EXPECT_EQ(response.success, expected_success);
171 if (expected_success) {
172 EXPECT_EQ(response.inner.leaf_indices, expected_indices);
173 }
174 signal.signal_level();
175 };
176
177 tree.find_leaf_indices_from(leaves, start_index, blockNumber, includeUncommitted, completion);
178 signal.wait_for_level();
179}
180
181template <typename LeafValueType, typename TypeOfTree>
182void check_find_leaf_index(TypeOfTree& tree,
183 const LeafValueType& leaf,
184 index_t expected_index,
185 bool expected_success,
186 bool includeUncommitted = true)
187{
188 check_find_leaf_index<LeafValueType, TypeOfTree>(
189 tree, { leaf }, { std::make_optional(expected_index) }, expected_success, includeUncommitted);
190}
191
192template <typename LeafValueType, typename TypeOfTree>
193void check_find_leaf_index_from(TypeOfTree& tree,
194 const LeafValueType& leaf,
195 index_t start_index,
196 index_t expected_index,
197 bool expected_success,
198 bool includeUncommitted = true)
199{
200 check_find_leaf_index_from<LeafValueType, TypeOfTree>(
201 tree, { leaf }, start_index, { std::make_optional(expected_index) }, expected_success, includeUncommitted);
202}
203
204template <typename LeafValueType, typename TypeOfTree>
205void check_historic_find_leaf_index(TypeOfTree& tree,
206 const LeafValueType& leaf,
207 block_number_t blockNumber,
208 index_t expected_index,
209 bool expected_success,
210 bool includeUncommitted = true)
211{
212 check_historic_find_leaf_index<LeafValueType, TypeOfTree>(
213 tree, { leaf }, blockNumber, { std::make_optional(expected_index) }, expected_success, includeUncommitted);
214}
215
216template <typename LeafValueType, typename TypeOfTree>
218 const LeafValueType& leaf,
219 block_number_t blockNumber,
220 index_t start_index,
221 index_t expected_index,
222 bool expected_success,
223 bool includeUncommitted = true)
224{
225 check_historic_find_leaf_index_from<LeafValueType, TypeOfTree>(tree,
226 { leaf },
227 blockNumber,
228 start_index,
229 { std::make_optional(expected_index) },
230 expected_success,
231 includeUncommitted);
232}
233
234template <typename TypeOfTree>
237 bool includeUncommitted = true,
238 bool expected_success = true)
239{
241 Signal signal;
242 auto completion = [&](const TypedResponse<GetSiblingPathResponse>& response) -> void {
243 EXPECT_EQ(response.success, expected_success);
244 if (response.success) {
245 h = response.inner.path;
246 }
247 signal.signal_level();
248 };
249 tree.get_sibling_path(index, completion, includeUncommitted);
250 signal.wait_for_level();
251 return h;
252}
253
254template <typename TreeType> void rollback_tree(TreeType& tree)
255{
256 auto completion = [&](auto completion) { tree.rollback(completion); };
257 call_operation(completion);
258}
259
260template <typename TreeType> void checkpoint_tree(TreeType& tree)
261{
262 auto completion = [&](auto completion) { tree.checkpoint(completion); };
263 call_operation(completion);
264}
265
266template <typename TreeType> void commit_checkpoint_tree(TreeType& tree, bool expected_success = true)
267
268{
269 auto completion = [&](auto completion) { tree.commit_checkpoint(completion); };
270 call_operation(completion, expected_success);
271}
272
273template <typename TreeType> void revert_checkpoint_tree(TreeType& tree, bool expected_success = true)
274{
275 auto completion = [&](auto completion) { tree.revert_checkpoint(completion); };
276 call_operation(completion, expected_success);
277}
278
279template <typename TreeType> void commit_all_tree_checkpoints(TreeType& tree, bool expected_success = true)
280
281{
282 auto completion = [&](auto completion) { tree.commit_all_checkpoints(completion); };
283 call_operation(completion, expected_success);
284}
285
286template <typename TreeType> void revert_all_tree_checkpoints(TreeType& tree, bool expected_success = true)
287{
288 auto completion = [&](auto completion) { tree.revert_all_checkpoints(completion); };
289 call_operation(completion, expected_success);
290}
291} // namespace bb::crypto::merkle_tree
std::shared_ptr< LMDBTreeStore > SharedPtr
Used in parallel insertions in the the IndexedTree. Workers signal to other following workes as they ...
Definition signal.hpp:17
void signal_level(uint32_t level=0)
Signals that the given level has been passed.
Definition signal.hpp:54
void wait_for_level(uint32_t level=0)
Causes the thread to wait until the required level has been signalled.
Definition signal.hpp:40
std::unique_ptr< LMDBReadTransaction > Ptr
PublicDataLeafValue LeafValueType
void checkpoint_tree(TreeType &tree)
void commit_all_tree_checkpoints(TreeType &tree, bool expected_success=true)
void rollback_tree(TreeType &tree)
void check_indices_data(LMDBTreeStore::SharedPtr db, fr leaf, index_t index, bool entryShouldBePresent, bool indexShouldBePresent)
void check_historic_find_leaf_index_from(TypeOfTree &tree, const std::vector< LeafValueType > &leaves, block_number_t blockNumber, index_t start_index, const std::vector< std::optional< index_t > > &expected_indices, bool expected_success, bool includeUncommitted=true)
void check_leaf_by_hash(LMDBTreeStore::SharedPtr db, IndexedLeaf< LeafType > leaf, bool shouldBePresent)
uint32_t block_number_t
Definition types.hpp:19
void check_find_leaf_index(TypeOfTree &tree, const std::vector< LeafValueType > &leaves, const std::vector< std::optional< index_t > > &expected_indices, bool expected_success, bool includeUncommitted=true)
void check_block_and_root_data(LMDBTreeStore::SharedPtr db, block_number_t blockNumber, fr root, bool expectedSuccess)
void check_find_leaf_index_from(TypeOfTree &tree, const std::vector< LeafValueType > &leaves, index_t start_index, const std::vector< std::optional< index_t > > &expected_indices, bool expected_success, bool includeUncommitted=true)
void call_operation(std::function< void(std::function< void(const Response &response)>)> operation, bool expected_success=true)
void check_block_and_size_data(LMDBTreeStore::SharedPtr db, block_number_t blockNumber, index_t expectedSize, bool expectedSuccess)
void commit_checkpoint_tree(TreeType &tree, bool expected_success=true)
std::vector< fr > fr_sibling_path
Definition hash_path.hpp:16
void revert_checkpoint_tree(TreeType &tree, bool expected_success=true)
void revert_all_tree_checkpoints(TreeType &tree, bool expected_success=true)
void check_historic_find_leaf_index(TypeOfTree &tree, const std::vector< LeafValueType > &leaves, block_number_t blockNumber, const std::vector< std::optional< index_t > > &expected_indices, bool expected_success, bool includeUncommitted=true)
fr_sibling_path get_sibling_path(TypeOfTree &tree, index_t index, bool includeUncommitted=true, bool expected_success=true)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::vector< fr > get_hash_inputs() const