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// === AUDIT STATUS ===
2// internal: { status: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5// =====================
6
7#pragma once
8
15#include <cstdint>
16#include <gtest/gtest.h>
17#include <optional>
18
20
22 block_number_t blockNumber,
23 fr root,
24 bool expectedSuccess)
25{
26 BlockPayload blockData;
27 LMDBTreeStore::ReadTransaction::Ptr tx = db->create_read_transaction();
28 bool success = db->read_block_data(blockNumber, blockData, *tx);
29 EXPECT_EQ(success, expectedSuccess);
30 if (expectedSuccess) {
31 EXPECT_EQ(blockData.root, root);
32 }
33 NodePayload nodeData;
34 success = db->read_node(root, nodeData, *tx);
35 EXPECT_EQ(success, expectedSuccess);
36}
37
39 LMDBTreeStore::SharedPtr db, block_number_t blockNumber, fr root, bool expectedSuccess, bool expectedRootSuccess)
40{
41 BlockPayload blockData;
42 LMDBTreeStore::ReadTransaction::Ptr tx = db->create_read_transaction();
43 bool success = db->read_block_data(blockNumber, blockData, *tx);
44 EXPECT_EQ(success, expectedSuccess);
45 if (expectedSuccess) {
46 EXPECT_EQ(blockData.root, root);
47 }
48 NodePayload nodeData;
49 success = db->read_node(root, nodeData, *tx);
50 EXPECT_EQ(success, expectedRootSuccess);
51}
52
54 block_number_t blockNumber,
55 index_t expectedSize,
56 bool expectedSuccess)
57{
58 BlockPayload blockData;
59 LMDBTreeStore::ReadTransaction::Ptr tx = db->create_read_transaction();
60 bool success = db->read_block_data(blockNumber, blockData, *tx);
61 EXPECT_EQ(success, expectedSuccess);
62 if (expectedSuccess) {
63 EXPECT_EQ(blockData.size, expectedSize);
64 }
65}
66
68 LMDBTreeStore::SharedPtr db, fr leaf, index_t index, bool entryShouldBePresent, bool indexShouldBePresent)
69{
70 index_t retrieved = 0;
71 LMDBTreeStore::ReadTransaction::Ptr tx = db->create_read_transaction();
72 bool success = db->read_leaf_index(leaf, retrieved, *tx);
73 EXPECT_EQ(success, entryShouldBePresent);
74 if (entryShouldBePresent) {
75 EXPECT_EQ(index == retrieved, indexShouldBePresent);
76 }
77}
78
79inline void call_operation(std::function<void(std::function<void(const Response& response)>)> operation,
80 bool expected_success = true)
81{
82 Signal signal;
83 auto completion = [&](const Response& response) -> void {
84 EXPECT_EQ(response.success, expected_success);
85 signal.signal_level();
86 };
87 operation(completion);
88 signal.wait_for_level();
89}
90
91template <typename LeafType, typename Hash>
93{
94 LMDBTreeStore::ReadTransaction::Ptr tx = db->create_read_transaction();
95 IndexedLeaf<LeafType> fromStore;
96 bool success = db->read_leaf_by_hash(Hash::hash(leaf.get_hash_inputs()), fromStore, *tx);
97 EXPECT_EQ(success, shouldBePresent);
98 if (success) {
99 EXPECT_EQ(fromStore, leaf);
100 }
101}
102
103template <typename LeafValueType, typename TypeOfTree>
104void check_find_leaf_index(TypeOfTree& tree,
105 const std::vector<LeafValueType>& leaves,
106 const std::vector<std::optional<index_t>>& expected_indices,
107 bool expected_success,
108 bool includeUncommitted = true)
109{
110 Signal signal;
111 auto completion = [&](const TypedResponse<FindLeafIndexResponse>& response) -> void {
112 EXPECT_EQ(response.success, expected_success);
113 if (expected_success) {
114 EXPECT_EQ(response.inner.leaf_indices, expected_indices);
115 }
116 signal.signal_level();
117 };
118
119 tree.find_leaf_indices(leaves, includeUncommitted, completion);
120 signal.wait_for_level();
121}
122
123template <typename LeafValueType, typename TypeOfTree>
124void check_find_leaf_index_from(TypeOfTree& tree,
125 const std::vector<LeafValueType>& leaves,
126 index_t start_index,
127 const std::vector<std::optional<index_t>>& expected_indices,
128 bool expected_success,
129 bool includeUncommitted = true)
130{
131 Signal signal;
132 auto completion = [&](const TypedResponse<FindLeafIndexResponse>& response) -> void {
133 EXPECT_EQ(response.success, expected_success);
134 if (expected_success) {
135 EXPECT_EQ(response.inner.leaf_indices, expected_indices);
136 }
137 signal.signal_level();
138 };
139
140 tree.find_leaf_indices_from(leaves, start_index, includeUncommitted, completion);
141 signal.wait_for_level();
142}
143
144template <typename LeafValueType, typename TypeOfTree>
145void check_historic_find_leaf_index(TypeOfTree& tree,
146 const std::vector<LeafValueType>& leaves,
147 block_number_t blockNumber,
148 const std::vector<std::optional<index_t>>& expected_indices,
149 bool expected_success,
150 bool includeUncommitted = true)
151{
152 Signal signal;
153 auto completion = [&](const TypedResponse<FindLeafIndexResponse>& response) -> void {
154 EXPECT_EQ(response.success, expected_success);
155 if (expected_success) {
156 EXPECT_EQ(response.inner.leaf_indices, expected_indices);
157 }
158 signal.signal_level();
159 };
160
161 tree.find_leaf_indices(leaves, blockNumber, includeUncommitted, completion);
162 signal.wait_for_level();
163}
164
165template <typename LeafValueType, typename TypeOfTree>
167 const std::vector<LeafValueType>& leaves,
168 block_number_t blockNumber,
169 index_t start_index,
170 const std::vector<std::optional<index_t>>& expected_indices,
171 bool expected_success,
172 bool includeUncommitted = true)
173{
174 Signal signal;
175 auto completion = [&](const TypedResponse<FindLeafIndexResponse>& response) -> void {
176 EXPECT_EQ(response.success, expected_success);
177 if (expected_success) {
178 EXPECT_EQ(response.inner.leaf_indices, expected_indices);
179 }
180 signal.signal_level();
181 };
182
183 tree.find_leaf_indices_from(leaves, start_index, blockNumber, includeUncommitted, completion);
184 signal.wait_for_level();
185}
186
187template <typename LeafValueType, typename TypeOfTree>
188void check_find_leaf_index(TypeOfTree& tree,
189 const LeafValueType& leaf,
190 index_t expected_index,
191 bool expected_success,
192 bool includeUncommitted = true)
193{
194 check_find_leaf_index<LeafValueType, TypeOfTree>(
195 tree, { leaf }, { std::make_optional(expected_index) }, expected_success, includeUncommitted);
196}
197
198template <typename LeafValueType, typename TypeOfTree>
199void check_find_leaf_index_from(TypeOfTree& tree,
200 const LeafValueType& leaf,
201 index_t start_index,
202 index_t expected_index,
203 bool expected_success,
204 bool includeUncommitted = true)
205{
206 check_find_leaf_index_from<LeafValueType, TypeOfTree>(
207 tree, { leaf }, start_index, { std::make_optional(expected_index) }, expected_success, includeUncommitted);
208}
209
210template <typename LeafValueType, typename TypeOfTree>
211void check_historic_find_leaf_index(TypeOfTree& tree,
212 const LeafValueType& leaf,
213 block_number_t blockNumber,
214 index_t expected_index,
215 bool expected_success,
216 bool includeUncommitted = true)
217{
218 check_historic_find_leaf_index<LeafValueType, TypeOfTree>(
219 tree, { leaf }, blockNumber, { std::make_optional(expected_index) }, expected_success, includeUncommitted);
220}
221
222template <typename LeafValueType, typename TypeOfTree>
224 const LeafValueType& leaf,
225 block_number_t blockNumber,
226 index_t start_index,
227 index_t expected_index,
228 bool expected_success,
229 bool includeUncommitted = true)
230{
231 check_historic_find_leaf_index_from<LeafValueType, TypeOfTree>(tree,
232 { leaf },
233 blockNumber,
234 start_index,
235 { std::make_optional(expected_index) },
236 expected_success,
237 includeUncommitted);
238}
239
240template <typename TypeOfTree>
242 index_t index,
243 bool includeUncommitted = true,
244 bool expected_success = true)
245{
247 Signal signal;
248 auto completion = [&](const TypedResponse<GetSiblingPathResponse>& response) -> void {
249 EXPECT_EQ(response.success, expected_success);
250 if (response.success) {
251 h = response.inner.path;
252 }
253 signal.signal_level();
254 };
255 tree.get_sibling_path(index, completion, includeUncommitted);
256 signal.wait_for_level();
257 return h;
258}
259
260template <typename TreeType> void rollback_tree(TreeType& tree)
261{
262 auto completion = [&](auto completion) { tree.rollback(completion); };
263 call_operation(completion);
264}
265
266template <typename TreeType> void checkpoint_tree(TreeType& tree)
267{
268 auto completion = [&](auto completion) { tree.checkpoint(completion); };
269 call_operation(completion);
270}
271
272template <typename TreeType> void commit_checkpoint_tree(TreeType& tree, bool expected_success = true)
273
274{
275 auto completion = [&](auto completion) { tree.commit_checkpoint(completion); };
276 call_operation(completion, expected_success);
277}
278
279template <typename TreeType> void revert_checkpoint_tree(TreeType& tree, bool expected_success = true)
280{
281 auto completion = [&](auto completion) { tree.revert_checkpoint(completion); };
282 call_operation(completion, expected_success);
283}
284
285template <typename TreeType> void commit_all_tree_checkpoints(TreeType& tree, bool expected_success = true)
286
287{
288 auto completion = [&](auto completion) { tree.commit_all_checkpoints(completion); };
289 call_operation(completion, expected_success);
290}
291
292template <typename TreeType> void revert_all_tree_checkpoints(TreeType& tree, bool expected_success = true)
293{
294 auto completion = [&](auto completion) { tree.revert_all_checkpoints(completion); };
295 call_operation(completion, expected_success);
296}
297} // 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