Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1018,10 +1018,10 @@ void ContentAddressedIndexedTree<Store, HashingPolicy>::generate_insertions(
if (!is_already_present) {
// Update the current leaf to point it to the new leaf
IndexedLeafValueType new_leaf =
IndexedLeafValueType(value_pair.first, low_leaf.nextIndex, low_leaf.nextValue);
IndexedLeafValueType(value_pair.first, low_leaf.nextIndex, low_leaf.nextKey);

low_leaf.nextIndex = index_of_new_leaf;
low_leaf.nextValue = value;
low_leaf.nextKey = value;
store_->set_leaf_key_at_index(index_of_new_leaf, new_leaf);

// std::cout << "NEW LEAf TO BE INSERTED at index: " << index_of_new_leaf << " : " << new_leaf
Expand All @@ -1039,7 +1039,7 @@ void ContentAddressedIndexedTree<Store, HashingPolicy>::generate_insertions(
} else if (IndexedLeafValueType::is_updateable()) {
// Update the current leaf's value, don't change it's link
IndexedLeafValueType replacement_leaf =
IndexedLeafValueType(value_pair.first, low_leaf.nextIndex, low_leaf.nextValue);
IndexedLeafValueType(value_pair.first, low_leaf.nextIndex, low_leaf.nextKey);
// IndexedLeafValueType empty_leaf = IndexedLeafValueType::empty();
// don't update the index for this empty leaf
// std::cout << "Low leaf updated at index " << low_leaf_index << " index of new leaf "
Expand Down Expand Up @@ -1088,7 +1088,7 @@ void ContentAddressedIndexedTree<Store, HashingPolicy>::update_leaf_and_hash_to_
// 3. Write the new node value
index_t index = leaf_index;
uint32_t level = depth_;
fr new_hash = leaf.value.is_empty() ? fr::zero() : HashingPolicy::hash(leaf.get_hash_inputs());
fr new_hash = leaf.leaf.is_empty() ? fr::zero() : HashingPolicy::hash(leaf.get_hash_inputs());

// Wait until we see that our leader has cleared 'depth_ - 1' (i.e. the level above the leaves that we are about
// to write into) this ensures that our leader is not still reading the leaves
Expand Down Expand Up @@ -1229,8 +1229,8 @@ std::pair<bool, fr> ContentAddressedIndexedTree<Store, HashingPolicy>::sparse_ba
}

// one of our leaves
new_hash = update.updated_leaf.value.is_empty() ? fr::zero()
: HashingPolicy::hash(update.updated_leaf.get_hash_inputs());
new_hash = update.updated_leaf.leaf.is_empty() ? fr::zero()
: HashingPolicy::hash(update.updated_leaf.get_hash_inputs());

// std::cout << "Hashing leaf at level " << level << " index " << update.leaf_index << " batch start "
// << start_index << " hash " << leaf_hash << std::endl;
Expand Down Expand Up @@ -1506,10 +1506,10 @@ void ContentAddressedIndexedTree<Store, HashingPolicy>::generate_sequential_inse
if (!is_already_present) {
// Update the current leaf to point it to the new leaf
IndexedLeafValueType new_leaf =
IndexedLeafValueType(new_payload, low_leaf.nextIndex, low_leaf.nextValue);
IndexedLeafValueType(new_payload, low_leaf.nextIndex, low_leaf.nextKey);
index_t index_of_new_leaf = current_size;
low_leaf.nextIndex = index_of_new_leaf;
low_leaf.nextValue = value;
low_leaf.nextKey = value;
current_size++;
// Cache the new leaf
store_->set_leaf_key_at_index(index_of_new_leaf, new_leaf);
Expand All @@ -1522,7 +1522,7 @@ void ContentAddressedIndexedTree<Store, HashingPolicy>::generate_sequential_inse
} else if (IndexedLeafValueType::is_updateable()) {
// Update the current leaf's value, don't change it's link
IndexedLeafValueType replacement_leaf =
IndexedLeafValueType(new_payload, low_leaf.nextIndex, low_leaf.nextValue);
IndexedLeafValueType(new_payload, low_leaf.nextIndex, low_leaf.nextKey);

store_->put_cached_leaf_by_index(low_leaf_index, replacement_leaf);
insertion_update.low_leaf_update.updated_leaf = replacement_leaf;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ void check_historic_leaf(TypeOfTree& tree,
auto completion = [&](const TypedResponse<GetIndexedLeafResponse<LeafValueType>>& response) -> void {
EXPECT_EQ(response.success, expected_success);
if (response.success) {
EXPECT_EQ(response.inner.indexed_leaf.value().value, leaf);
EXPECT_EQ(response.inner.indexed_leaf.value().leaf, leaf);
}
signal.signal_level();
};
Expand Down Expand Up @@ -665,7 +665,7 @@ void test_batch_insert(uint32_t batchSize, std::string directory, uint64_t mapSi
std::vector<fr_sibling_path> memory_tree_sibling_paths;
for (uint32_t j = 0; j < batch_size; j++) {
batch.emplace_back(random_engine.get_random_uint256());
fr_sibling_path path = memdb.update_element(batch[j].value);
fr_sibling_path path = memdb.update_element(batch[j].nullifier);
memory_tree_sibling_paths.push_back(path);
}
std::shared_ptr<std::vector<LeafUpdateWitnessData<NullifierLeafValue>>> tree1_low_leaf_witness_data;
Expand Down Expand Up @@ -752,7 +752,7 @@ void test_batch_insert_with_commit_restore(uint32_t batchSize,
std::vector<fr_sibling_path> memory_tree_sibling_paths;
for (uint32_t j = 0; j < batch_size; j++) {
batch.emplace_back(random_engine.get_random_uint256());
fr_sibling_path path = memdb.update_element(batch[j].value);
fr_sibling_path path = memdb.update_element(batch[j].nullifier);
memory_tree_sibling_paths.push_back(path);
}
std::shared_ptr<std::vector<LeafUpdateWitnessData<NullifierLeafValue>>> tree1_low_leaf_witness_data;
Expand Down Expand Up @@ -894,7 +894,7 @@ TEST_F(PersistedContentAddressedIndexedTreeTest, reports_an_error_if_batch_conta
values[8] = values[0];

std::stringstream ss;
ss << "Duplicate key not allowed in same batch, key value: " << values[0].value << ", tree: " << name;
ss << "Duplicate key not allowed in same batch, key value: " << values[0].nullifier << ", tree: " << name;

Signal signal;
auto add_completion = [&](const TypedResponse<AddIndexedDataResponse<NullifierLeafValue>>& response) {
Expand Down Expand Up @@ -941,7 +941,7 @@ void test_sequential_insert_vs_batch(uint32_t batchSize, std::string directory,
std::vector<fr_sibling_path> memory_tree_sibling_paths;
for (uint32_t j = 0; j < batch_size; j++) {
batch.emplace_back(random_engine.get_random_uint256());
fr_sibling_path path = memdb.update_element(batch[j].value);
fr_sibling_path path = memdb.update_element(batch[j].nullifier);
memory_tree_sibling_paths.push_back(path);
}
std::shared_ptr<std::vector<LeafUpdateWitnessData<NullifierLeafValue>>> sequential_tree_1_low_leaf_witness_data;
Expand Down Expand Up @@ -1046,7 +1046,7 @@ TEST_F(PersistedContentAddressedIndexedTreeTest, sequential_insert_allows_multip
std::vector<PublicDataLeafValue> values{ PublicDataLeafValue(42, 27), PublicDataLeafValue(42, 28) };
add_values_sequentially(tree, values);

EXPECT_EQ(get_leaf<PublicDataLeafValue>(tree, 2).value, values[1]);
EXPECT_EQ(get_leaf<PublicDataLeafValue>(tree, 2).leaf.value, values[1].value);
check_size(tree, 3);
}

Expand Down Expand Up @@ -1271,9 +1271,9 @@ TEST_F(PersistedContentAddressedIndexedTreeTest, test_indexed_tree)
std::vector<uint256_t> differences;
for (uint32_t i = 0; i < uint32_t(21); i++) {
uint256_t diff_hi =
abs_diff(uint256_t(new_member), uint256_t(get_leaf<NullifierLeafValue>(tree, i).value.get_key()));
abs_diff(uint256_t(new_member), uint256_t(get_leaf<NullifierLeafValue>(tree, i).leaf.get_key()));
uint256_t diff_lo =
abs_diff(uint256_t(new_member), uint256_t(get_leaf<NullifierLeafValue>(tree, i).value.get_key()));
abs_diff(uint256_t(new_member), uint256_t(get_leaf<NullifierLeafValue>(tree, i).leaf.get_key()));
differences.push_back(diff_hi + diff_lo);
}
auto it = std::min_element(differences.begin(), differences.end());
Expand Down Expand Up @@ -1726,7 +1726,7 @@ TEST_F(PersistedContentAddressedIndexedTreeTest, test_historic_sibling_path_retr

for (uint32_t j = 0; j < batch_size; j++) {
batch.emplace_back(random_engine.get_random_uint256());
memdb.update_element(batch[j].value);
memdb.update_element(batch[j].get_key());
}
memory_tree_sibling_paths_index_0.push_back(memdb.get_sibling_path(0));
std::shared_ptr<std::vector<LeafUpdateWitnessData<NullifierLeafValue>>> tree1_low_leaf_witness_data;
Expand Down Expand Up @@ -1968,7 +1968,7 @@ TEST_F(PersistedContentAddressedIndexedTreeTest, test_can_create_forks_at_histor
std::vector<NullifierLeafValue> batch1;
for (uint32_t j = 0; j < batch_size; j++) {
batch1.emplace_back(random_engine.get_random_uint256());
memdb.update_element(batch1[j].value);
memdb.update_element(batch1[j].nullifier);
}

fr_sibling_path block1SiblingPathIndex3 = memdb.get_sibling_path(3 + batch_size);
Expand All @@ -1979,7 +1979,7 @@ TEST_F(PersistedContentAddressedIndexedTreeTest, test_can_create_forks_at_histor
std::vector<NullifierLeafValue> batch2;
for (uint32_t j = 0; j < batch_size; j++) {
batch2.emplace_back(random_engine.get_random_uint256());
memdb.update_element(batch2[j].value);
memdb.update_element(batch2[j].nullifier);
}

add_values(tree1, batch2);
Expand All @@ -1993,7 +1993,7 @@ TEST_F(PersistedContentAddressedIndexedTreeTest, test_can_create_forks_at_histor
std::vector<NullifierLeafValue> batch3;
for (uint32_t j = 0; j < batch_size; j++) {
batch3.emplace_back(random_engine.get_random_uint256());
memdb.update_element(batch3[j].value);
memdb.update_element(batch3[j].nullifier);
}

add_values(tree1, batch3);
Expand All @@ -2009,7 +2009,7 @@ TEST_F(PersistedContentAddressedIndexedTreeTest, test_can_create_forks_at_histor
check_root(treeAtBlock2, block2Root);
check_sibling_path(treeAtBlock2, 3 + batch_size, block2SiblingPathIndex3, false, true);
auto block2TreeLeaf10 = get_leaf<NullifierLeafValue>(treeAtBlock2, 7 + batch_size);
EXPECT_EQ(block2TreeLeaf10.value, batch1[7].value);
EXPECT_EQ(block2TreeLeaf10.leaf.nullifier, batch1[7].nullifier);

check_find_leaf_index(treeAtBlock2, batch1[5], 5 + batch_size, true);
check_find_leaf_index_from(treeAtBlock2, batch1[5], 0, 5 + batch_size, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,50 @@
namespace bb::crypto::merkle_tree {

struct NullifierLeafValue {
fr value;
fr nullifier;

MSGPACK_FIELDS(value)
MSGPACK_FIELDS(nullifier)

NullifierLeafValue() = default;
NullifierLeafValue(const fr& v)
: value(v)
NullifierLeafValue(const fr& n)
: nullifier(n)
{}
NullifierLeafValue(const NullifierLeafValue& other) = default;
NullifierLeafValue(NullifierLeafValue&& other) = default;
NullifierLeafValue& operator=(const NullifierLeafValue& other)
{
if (this != &other) {
value = other.value;
nullifier = other.nullifier;
}
return *this;
}

NullifierLeafValue& operator=(NullifierLeafValue&& other) noexcept
{
if (this != &other) {
value = other.value;
nullifier = other.nullifier;
}
return *this;
}
~NullifierLeafValue() = default;

static bool is_updateable() { return false; }

bool operator==(NullifierLeafValue const& other) const { return value == other.value; }
bool operator==(NullifierLeafValue const& other) const { return nullifier == other.nullifier; }

friend std::ostream& operator<<(std::ostream& os, const NullifierLeafValue& v)
{
os << "value = " << v.value;
os << "nullifier = " << v.nullifier;
return os;
}

fr get_key() const { return value; }
fr get_key() const { return nullifier; }

bool is_empty() const { return value == fr::zero(); }
bool is_empty() const { return nullifier.is_zero(); }

std::vector<fr> get_hash_inputs(fr nextValue, fr nextIndex) const
std::vector<fr> get_hash_inputs(fr nextKey, fr nextIndex) const
{
return std::vector<fr>({ value, nextValue, nextIndex });
return std::vector<fr>({ nullifier, nextKey, nextIndex });
}

operator uint256_t() const { return get_key(); }
Expand All @@ -60,19 +60,19 @@ struct NullifierLeafValue {

static NullifierLeafValue padding(index_t i) { return { i }; }

static std::string name() { return std::string("NullifierLeafValue"); };
static std::string name() { return "NullifierLeafValue"; };
};

struct PublicDataLeafValue {
fr value;
fr slot;
fr value;

MSGPACK_FIELDS(value, slot)
MSGPACK_FIELDS(slot, value)

PublicDataLeafValue() = default;
PublicDataLeafValue(const fr& s, const fr& v)
: value(v)
, slot(s)
: slot(s)
, value(v)
{}
PublicDataLeafValue(const PublicDataLeafValue& other) = default;
PublicDataLeafValue(PublicDataLeafValue&& other) = default;
Expand Down Expand Up @@ -120,22 +120,22 @@ struct PublicDataLeafValue {

static PublicDataLeafValue padding(index_t i) { return { i, fr::zero() }; }

static std::string name() { return std::string("PublicDataLeafValue"); };
static std::string name() { return "PublicDataLeafValue"; };
};

template <typename LeafType> struct IndexedLeaf {
LeafType value;
LeafType leaf;
index_t nextIndex;
fr nextValue;
fr nextKey;

MSGPACK_FIELDS(value, nextIndex, nextValue)
MSGPACK_FIELDS(leaf, nextIndex, nextKey)

IndexedLeaf() = default;

IndexedLeaf(const LeafType& val, const index_t& nextIdx, const fr& nextVal)
: value(val)
IndexedLeaf(const LeafType& leaf, const index_t& nextIdx, const fr& nextKey)
: leaf(leaf)
, nextIndex(nextIdx)
, nextValue(nextVal)
, nextKey(nextKey)
{}

IndexedLeaf(const IndexedLeaf<LeafType>& other) = default;
Expand All @@ -148,14 +148,14 @@ template <typename LeafType> struct IndexedLeaf {

bool operator==(IndexedLeaf<LeafType> const& other) const
{
return value == other.value && nextValue == other.nextValue && nextIndex == other.nextIndex;
return leaf == other.leaf && nextKey == other.nextKey && nextIndex == other.nextIndex;
}

IndexedLeaf<LeafType>& operator=(IndexedLeaf<LeafType> const& other)
{
if (this != &other) {
value = other.value;
nextValue = other.nextValue;
leaf = other.leaf;
nextKey = other.nextKey;
nextIndex = other.nextIndex;
}
return *this;
Expand All @@ -164,22 +164,22 @@ template <typename LeafType> struct IndexedLeaf {
IndexedLeaf<LeafType>& operator=(IndexedLeaf<LeafType>&& other) noexcept
{
if (this != &other) {
value = other.value;
nextValue = other.nextValue;
leaf = other.leaf;
nextKey = other.nextKey;
nextIndex = other.nextIndex;
}
return *this;
}

friend std::ostream& operator<<(std::ostream& os, const IndexedLeaf<LeafType>& leaf)
{
os << leaf.value << "\nnextIdx = " << leaf.nextIndex << "\nnextVal = " << leaf.nextValue;
os << leaf.leaf << "\nnextIdx = " << leaf.nextIndex << "\nnextKey = " << leaf.nextKey;
return os;
}

std::vector<fr> get_hash_inputs() const { return value.get_hash_inputs(nextValue, nextIndex); }
std::vector<fr> get_hash_inputs() const { return leaf.get_hash_inputs(nextKey, nextIndex); }

bool is_empty() { return value.is_empty(); }
bool is_empty() { return leaf.is_empty(); }

static IndexedLeaf<LeafType> empty() { return { LeafType::empty(), 0, 0 }; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ void ContentAddressedCachedTreeStore<LeafValueType>::set_leaf_key_at_index(const
const IndexedLeafValueType& leaf)
{
// std::cout << "Set leaf key at index " << index << std::endl;
update_index(index, leaf.value.get_key());
update_index(index, leaf.leaf.get_key());
}

template <typename LeafValueType>
Expand Down Expand Up @@ -1069,15 +1069,15 @@ void ContentAddressedCachedTreeStore<LeafValueType>::remove_leaf(const fr& hash,
if (maxIndex.has_value()) {
// std::cout << "Max Index" << std::endl;
// We need to clear the entry from the leaf key to index database as this leaf never existed
IndexedLeafValueType leaf;
IndexedLeafValueType leaf_preimage;
fr key;
if (requires_preimage_for_key<LeafValueType>()) {
if constexpr (requires_preimage_for_key<LeafValueType>()) {
// std::cout << "Reading leaf by hash " << hash << std::endl;
if (!dataStore_->read_leaf_by_hash(hash, leaf, tx)) {
if (!dataStore_->read_leaf_by_hash(hash, leaf_preimage, tx)) {
throw std::runtime_error("Failed to find leaf pre-image when attempting to delete indices");
}
// std::cout << "Read leaf by hash " << hash << std::endl;
key = preimage_to_key(leaf.value);
key = preimage_to_key(leaf_preimage.leaf);
} else {
key = hash;
}
Expand Down
Loading