Skip to content

Commit 6f899ed

Browse files
authored
Merge af7a252 into 2add6ae
2 parents 2add6ae + af7a252 commit 6f899ed

15 files changed

+196
-817
lines changed

barretenberg/cpp/src/barretenberg/crypto/merkle_tree/append_only_tree/content_addressed_append_only_tree.test.cpp

+20-124
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ TEST_F(PersistedContentAddressedAppendOnlyTreeTest, errors_are_caught_and_handle
475475
std::string name = random_string();
476476
std::string directory = random_temp_directory();
477477
std::filesystem::create_directories(directory);
478+
auto& random_engine = numeric::get_randomness();
478479

479480
{
480481
LMDBTreeStore::SharedPtr db = std::make_shared<LMDBTreeStore>(_directory, name, 50, _maxReaders);
@@ -492,9 +493,10 @@ TEST_F(PersistedContentAddressedAppendOnlyTreeTest, errors_are_caught_and_handle
492493

493494
// Add lots of values to the tree
494495
uint32_t num_values_to_add = 16 * 1024;
495-
std::vector<fr> values(num_values_to_add, VALUES[0]);
496+
std::vector<fr> values;
496497
for (uint32_t i = 0; i < num_values_to_add; i++) {
497-
memdb.update_element(i, VALUES[0]);
498+
values.emplace_back(random_engine.get_random_uint256());
499+
memdb.update_element(i, values[i]);
498500
}
499501
add_values(tree, values);
500502

@@ -714,46 +716,31 @@ TEST_F(PersistedContentAddressedAppendOnlyTreeTest, test_find_leaf_index)
714716

715717
commit_tree(tree);
716718

717-
values = { 16, 4, 18, 22 };
719+
values = { 16, 4, 19, 22 };
718720
add_values(tree, values);
719721

720-
// we now have duplicate leaf 18, one committed the other not
721-
check_find_leaf_index(tree, 18, 5, true, true);
722-
check_find_leaf_index(tree, 18, 5, true, false);
723-
724722
// verify the find index from api
725723
check_find_leaf_index_from(tree, 18, 0, 5, true, true);
726-
check_find_leaf_index_from(tree, 18, 6, 10, true, true);
727-
check_find_leaf_index_from(tree, 18, 6, 0, false, false);
724+
check_find_leaf_index_from(tree, 19, 6, 10, true, true);
725+
check_find_leaf_index_from(tree, 19, 0, 0, false, false);
728726

729727
commit_tree(tree);
730728

731-
// add another leaf 18
732-
add_value(tree, 18);
733-
734-
// should return the first index
735-
check_find_leaf_index_from(tree, 18, 0, 5, true, false);
736-
check_find_leaf_index_from(tree, 18, 0, 5, true, true);
737-
738729
add_value(tree, 88);
739-
// and another uncommitted 18
740-
add_value(tree, 18);
741730

742731
add_value(tree, 32);
743732

744-
// should return the first uncommitted
745-
check_find_leaf_index_from(tree, 18, 12, 12, true, true);
746-
check_find_leaf_index_from(tree, 18, 14, 14, true, true);
747-
check_find_leaf_index_from(tree, 18, 15, 0, false, true);
733+
check_size(tree, 14);
734+
check_size(tree, 12, false);
748735

749736
// look past the last instance of this leaf
750-
check_find_leaf_index_from(tree, 18, 17, 0, false, true);
737+
check_find_leaf_index_from(tree, 18, 6, 0, false, true);
751738

752739
// look beyond the end of uncommitted
753-
check_find_leaf_index_from(tree, 18, 18, 0, false, true);
740+
check_find_leaf_index_from(tree, 18, 15, 0, false, true);
754741

755742
// look beyond the end of committed and don't include uncomitted
756-
check_find_leaf_index_from(tree, 18, 14, 0, false, false);
743+
check_find_leaf_index_from(tree, 88, 13, 0, false, false);
757744
}
758745

759746
TEST_F(PersistedContentAddressedAppendOnlyTreeTest, can_add_multiple_values)
@@ -975,7 +962,7 @@ TEST_F(PersistedContentAddressedAppendOnlyTreeTest, test_find_historic_leaf_inde
975962

976963
commit_tree(tree);
977964

978-
values = { 16, 4, 18, 22 };
965+
values = { 16, 4, 19, 22 };
979966
add_values(tree, values);
980967

981968
// should not be present at block 1
@@ -987,15 +974,15 @@ TEST_F(PersistedContentAddressedAppendOnlyTreeTest, test_find_historic_leaf_inde
987974
check_find_historic_leaf_index_from(tree, 1, 18, 2, 0, false, false);
988975
// at block 2 it should be
989976
check_find_historic_leaf_index_from(tree, 2, 18, 2, 5, true);
990-
// at block 2, from index 6 it should not be found if looking only at committed
991-
check_find_historic_leaf_index_from(tree, 2, 18, 6, 5, false, false);
992-
// at block 2, from index 6 it should be found if looking at uncommitted too
993-
check_find_historic_leaf_index_from(tree, 2, 18, 6, 10, true);
977+
// at block 2, from index 6, 19 should not be found if looking only at committed
978+
check_find_historic_leaf_index_from(tree, 2, 19, 6, 5, false, false);
979+
// at block 2, from index 6, 19 should be found if looking at uncommitted too
980+
check_find_historic_leaf_index_from(tree, 2, 19, 6, 10, true);
994981

995982
commit_tree(tree);
996983

997-
// at block 3, from index 6 it should now be found in committed only
998-
check_find_historic_leaf_index_from(tree, 3, 18, 6, 10, true, false);
984+
// at block 3, from index 6, should now be found in committed only
985+
check_find_historic_leaf_index_from(tree, 3, 19, 6, 10, true, false);
999986
}
1000987

1001988
TEST_F(PersistedContentAddressedAppendOnlyTreeTest, can_be_filled)
@@ -1418,79 +1405,6 @@ TEST_F(PersistedContentAddressedAppendOnlyTreeTest, can_unwind_all_blocks)
14181405
test_unwind(_directory, "DB", _mapSize, _maxReaders, 10, 16, 16, 16, second);
14191406
}
14201407

1421-
TEST_F(PersistedContentAddressedAppendOnlyTreeTest, can_unwind_blocks_with_duplicate_leaves)
1422-
{
1423-
constexpr size_t depth = 4;
1424-
std::string name = random_string();
1425-
LMDBTreeStore::SharedPtr db = std::make_shared<LMDBTreeStore>(_directory, name, _mapSize, _maxReaders);
1426-
std::unique_ptr<Store> store = std::make_unique<Store>(name, depth, db);
1427-
ThreadPoolPtr pool = make_thread_pool(1);
1428-
TreeType tree(std::move(store), pool);
1429-
MemoryTree<Poseidon2HashPolicy> memdb(depth);
1430-
1431-
constexpr size_t blockSize = 2;
1432-
constexpr size_t numBlocks = 2;
1433-
constexpr size_t numBlocksToUnwind = 1;
1434-
1435-
std::vector<fr> values = create_values(blockSize);
1436-
1437-
// Add the same batch of values many times
1438-
for (size_t i = 0; i < numBlocks; i++) {
1439-
for (size_t j = 0; j < values.size(); j++) {
1440-
size_t ind = i * blockSize + j;
1441-
memdb.update_element(ind, values[j]);
1442-
}
1443-
add_values(tree, values);
1444-
commit_tree(tree);
1445-
check_block_and_root_data(db, i + 1, memdb.root(), true);
1446-
1447-
for (size_t j = 0; j < values.size(); j++) {
1448-
size_t ind = i * blockSize + j;
1449-
// query the indices db directly
1450-
check_indices_data(db, values[j], ind, true, true);
1451-
}
1452-
}
1453-
1454-
for (size_t i = 0; i < numBlocks; i++) {
1455-
index_t startIndex = i * blockSize;
1456-
index_t expectedIndex = startIndex + 1;
1457-
1458-
// search for the leaf from start of each batch
1459-
check_find_leaf_index_from(tree, values[1], startIndex, expectedIndex, true);
1460-
// search for the leaf from start of the next batch
1461-
check_find_leaf_index_from(tree, values[1], startIndex + 2, expectedIndex + blockSize, i < (numBlocks - 1));
1462-
}
1463-
1464-
const uint32_t blocksToRemove = numBlocksToUnwind;
1465-
for (uint32_t i = 0; i < blocksToRemove; i++) {
1466-
const index_t blockNumber = numBlocks - i;
1467-
unwind_block(tree, blockNumber);
1468-
1469-
const index_t previousValidBlock = blockNumber - 1;
1470-
index_t deletedBlockStartIndex = previousValidBlock * blockSize;
1471-
1472-
check_block_height(tree, previousValidBlock);
1473-
check_size(tree, deletedBlockStartIndex);
1474-
1475-
for (size_t j = 0; j < numBlocks; j++) {
1476-
index_t startIndex = j * blockSize;
1477-
index_t expectedIndex = startIndex + 1;
1478-
1479-
// search for the leaf from start of each batch
1480-
check_find_leaf_index_from(tree, values[1], startIndex, expectedIndex, j < previousValidBlock);
1481-
// search for the leaf from start of the next batch
1482-
check_find_leaf_index_from(
1483-
tree, values[1], startIndex + 2, expectedIndex + blockSize, j < (previousValidBlock - 1));
1484-
1485-
for (size_t k = 0; k < values.size(); k++) {
1486-
size_t ind = j * blockSize + k;
1487-
// query the indices db directly. If block number == 1 that means the entry should not be present
1488-
check_indices_data(db, values[k], ind, blockNumber > 1, ind < deletedBlockStartIndex);
1489-
}
1490-
}
1491-
}
1492-
}
1493-
14941408
TEST_F(PersistedContentAddressedAppendOnlyTreeTest, can_sync_and_unwind_large_blocks)
14951409
{
14961410

@@ -1534,23 +1448,15 @@ TEST_F(PersistedContentAddressedAppendOnlyTreeTest, can_advance_finalised_blocks
15341448

15351449
index_t expectedFinalisedBlock = i < finalisedBlockDelay ? 0 : i - finalisedBlockDelay;
15361450
check_finalised_block_height(tree, expectedFinalisedBlock);
1537-
index_t expectedPresentStart = i < finalisedBlockDelay ? 0 : (expectedFinalisedBlock * blockSize);
1538-
index_t expectedPresentEnd = ((i + 1) * blockSize) - 1;
1539-
std::vector<fr> toTest(values.begin() + static_cast<int64_t>(expectedPresentStart),
1540-
values.begin() + static_cast<int64_t>(expectedPresentEnd + 1));
1541-
check_leaf_keys_are_present(db, expectedPresentStart, expectedPresentEnd, toTest);
15421451

15431452
if (i >= finalisedBlockDelay) {
15441453

15451454
index_t blockToFinalise = expectedFinalisedBlock + 1;
15461455

1547-
// attemnpting to finalise a block that doesn't exist should fail
1456+
// attempting to finalise a block that doesn't exist should fail
15481457
finalise_block(tree, blockToFinalise + numBlocks, false);
15491458

15501459
finalise_block(tree, blockToFinalise, true);
1551-
1552-
index_t expectedNotPresentEnd = (blockToFinalise * blockSize) - 1;
1553-
check_leaf_keys_are_not_present(db, 0, expectedNotPresentEnd);
15541460
}
15551461
}
15561462
}
@@ -1585,12 +1491,7 @@ TEST_F(PersistedContentAddressedAppendOnlyTreeTest, can_finalise_multiple_blocks
15851491

15861492
index_t blockToFinalise = 8;
15871493

1588-
check_leaf_keys_are_present(db, 0, (numBlocks * blockSize) - 1, values);
1589-
15901494
finalise_block(tree, blockToFinalise);
1591-
1592-
index_t expectedNotPresentEnd = (blockToFinalise * blockSize) - 1;
1593-
check_leaf_keys_are_not_present(db, 0, expectedNotPresentEnd);
15941495
}
15951496

15961497
TEST_F(PersistedContentAddressedAppendOnlyTreeTest, can_not_finalise_block_beyond_pending_chain)
@@ -1630,12 +1531,7 @@ TEST_F(PersistedContentAddressedAppendOnlyTreeTest, can_not_finalise_block_beyon
16301531
// finalise the entire chain
16311532
index_t blockToFinalise = numBlocks;
16321533

1633-
check_leaf_keys_are_present(db, 0, (numBlocks * blockSize) - 1, values);
1634-
16351534
finalise_block(tree, blockToFinalise);
1636-
1637-
index_t expectedNotPresentEnd = (blockToFinalise * blockSize) - 1;
1638-
check_leaf_keys_are_not_present(db, 0, expectedNotPresentEnd);
16391535
}
16401536

16411537
TEST_F(PersistedContentAddressedAppendOnlyTreeTest, can_not_fork_from_unwound_blocks)

barretenberg/cpp/src/barretenberg/crypto/merkle_tree/lmdb_store/lmdb_transaction.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,9 @@ bool LMDBTransaction::get_value(std::vector<uint8_t>& key, std::vector<uint8_t>&
3434
{
3535
return lmdb_queries::get_value(key, data, db, *this);
3636
}
37+
38+
bool LMDBTransaction::get_value(std::vector<uint8_t>& key, index_t& data, const LMDBDatabase& db) const
39+
{
40+
return lmdb_queries::get_value(key, data, db, *this);
41+
}
3742
} // namespace bb::crypto::merkle_tree

barretenberg/cpp/src/barretenberg/crypto/merkle_tree/lmdb_store/lmdb_transaction.hpp

+20-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "barretenberg/crypto/merkle_tree/lmdb_store/lmdb_database.hpp"
33
#include "barretenberg/crypto/merkle_tree/lmdb_store/lmdb_environment.hpp"
44
#include "barretenberg/crypto/merkle_tree/lmdb_store/queries.hpp"
5+
#include "lmdb.h"
56
#include <functional>
67
#include <vector>
78

@@ -37,16 +38,18 @@ class LMDBTransaction {
3738
*/
3839
virtual void abort();
3940

40-
template <typename T>
41+
template <typename T, typename K>
4142
bool get_value_or_previous(T& key,
42-
std::vector<uint8_t>& data,
43+
K& data,
4344
const LMDBDatabase& db,
44-
const std::function<bool(const std::vector<uint8_t>&)>& is_valid) const;
45+
const std::function<bool(const MDB_val&)>& is_valid) const;
4546

46-
template <typename T> bool get_value_or_previous(T& key, std::vector<uint8_t>& data, const LMDBDatabase& db) const;
47+
template <typename T, typename K> bool get_value_or_previous(T& key, K& data, const LMDBDatabase& db) const;
4748

4849
template <typename T> bool get_value(T& key, std::vector<uint8_t>& data, const LMDBDatabase& db) const;
4950

51+
template <typename T> bool get_value(T& key, index_t& data, const LMDBDatabase& db) const;
52+
5053
template <typename T>
5154
void get_all_values_greater_or_equal_key(const T& key,
5255
std::vector<std::vector<uint8_t>>& data,
@@ -59,6 +62,8 @@ class LMDBTransaction {
5962

6063
bool get_value(std::vector<uint8_t>& key, std::vector<uint8_t>& data, const LMDBDatabase& db) const;
6164

65+
bool get_value(std::vector<uint8_t>& key, index_t& data, const LMDBDatabase& db) const;
66+
6267
protected:
6368
std::shared_ptr<LMDBEnvironment> _environment;
6469
MDB_txn* _transaction;
@@ -71,17 +76,23 @@ template <typename T> bool LMDBTransaction::get_value(T& key, std::vector<uint8_
7176
return get_value(keyBuffer, data, db);
7277
}
7378

74-
template <typename T>
75-
bool LMDBTransaction::get_value_or_previous(T& key, std::vector<uint8_t>& data, const LMDBDatabase& db) const
79+
template <typename T> bool LMDBTransaction::get_value(T& key, index_t& data, const LMDBDatabase& db) const
80+
{
81+
std::vector<uint8_t> keyBuffer = serialise_key(key);
82+
return get_value(keyBuffer, data, db);
83+
}
84+
85+
template <typename T, typename K>
86+
bool LMDBTransaction::get_value_or_previous(T& key, K& data, const LMDBDatabase& db) const
7687
{
7788
return lmdb_queries::get_value_or_previous(key, data, db, *this);
7889
}
7990

80-
template <typename T>
91+
template <typename T, typename K>
8192
bool LMDBTransaction::get_value_or_previous(T& key,
82-
std::vector<uint8_t>& data,
93+
K& data,
8394
const LMDBDatabase& db,
84-
const std::function<bool(const std::vector<uint8_t>&)>& is_valid) const
95+
const std::function<bool(const MDB_val&)>& is_valid) const
8596
{
8697
return lmdb_queries::get_value_or_previous(key, data, db, is_valid, *this);
8798
}

0 commit comments

Comments
 (0)