Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove LeveldbStore from stdlib_merkle_tree #149

Merged
merged 1 commit into from
Feb 17, 2023
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
3 changes: 0 additions & 3 deletions cpp/src/aztec/join_split_example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
if(NOT WASM)
link_libraries(leveldb)
endif()
add_subdirectory(proofs)
42 changes: 0 additions & 42 deletions cpp/src/aztec/stdlib/merkle_tree/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,43 +1 @@
barretenberg_module(stdlib_merkle_tree stdlib_primitives stdlib_blake3s stdlib_pedersen)

if(NOT WASM)
include(FetchContent)
FetchContent_Declare(
leveldb
GIT_REPOSITORY https://github.com/google/leveldb.git
GIT_TAG 1.22
FIND_PACKAGE_ARGS
)

# Disable some leveldb targets before we call FetchContent_MakeAvailable
# so they are configured correctly if it needs to fetch
set(LEVELDB_BUILD_TESTS OFF CACHE BOOL "LevelDB tests off")
set(LEVELDB_BUILD_BENCHMARKS OFF CACHE BOOL "LevelDB benchmarks off")

FetchContent_MakeAvailable(leveldb)

if (leveldb_FOUND)
# Globally installed leveldb needs Threads available as Threads::Threads as discovered by `find_package`
find_package(Threads REQUIRED)

foreach(target IN LISTS stdlib_merkle_tree_lib_targets stdlib_merkle_tree_exe_targets)
target_link_libraries(${target} PRIVATE leveldb::leveldb)
endforeach()
else()
# FetchContent_MakeAvailable calls FetchContent_Populate if `find_package` is unsuccessful
# so these variables will be available if we reach this case
set_property(DIRECTORY ${leveldb_SOURCE_DIR} PROPERTY EXCLUDE_FROM_ALL)
set_property(DIRECTORY ${leveldb_BINARY_DIR} PROPERTY EXCLUDE_FROM_ALL)

# Silence all compiler warnings from LevelDB
target_compile_options(
leveldb
PRIVATE
-w
)

foreach(target IN LISTS stdlib_merkle_tree_lib_targets stdlib_merkle_tree_exe_targets)
target_link_libraries(${target} PRIVATE leveldb)
endforeach()
endif()
endif()
1 change: 0 additions & 1 deletion cpp/src/aztec/stdlib/merkle_tree/index.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once
#include "hash_path.hpp"
#include "hash.hpp"
#include "leveldb_store.hpp"
#include "membership.hpp"
#include "memory_store.hpp"
#include "memory_tree.hpp"
Expand Down
105 changes: 0 additions & 105 deletions cpp/src/aztec/stdlib/merkle_tree/leveldb_store.hpp

This file was deleted.

1 change: 0 additions & 1 deletion cpp/src/aztec/stdlib/merkle_tree/membership.test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "leveldb_store.hpp"
#include "merkle_tree.hpp"
#include "membership.hpp"
#include "memory_store.hpp"
Expand Down
21 changes: 8 additions & 13 deletions cpp/src/aztec/stdlib/merkle_tree/merkle_tree.bench.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include "hash.hpp"
#include "leveldb_store.hpp"
#include "memory_store.hpp"
#include "merkle_tree.hpp"
#include <benchmark/benchmark.h>
#include <leveldb/db.h>
#include <numeric/random/engine.hpp>

using namespace benchmark;
Expand All @@ -14,7 +13,6 @@ auto& engine = numeric::random::get_debug_engine();

constexpr size_t DEPTH = 256;
constexpr size_t MAX = 4096;
const std::string DB_PATH = "/tmp/leveldb_test";

static std::vector<fr> VALUES = []() {
std::vector<fr> values(MAX);
Expand All @@ -34,9 +32,8 @@ BENCHMARK(hash)->MinTime(5);

void update_first_element(State& state) noexcept
{
LevelDbStore::destroy(DB_PATH);
LevelDbStore store(DB_PATH);
LevelDbTree db(store, DEPTH);
MemoryStore store;
MerkleTree<MemoryStore> db(store, DEPTH);

for (auto _ : state) {
db.update_element(0, VALUES[1]);
Expand All @@ -48,9 +45,8 @@ void update_elements(State& state) noexcept
{
for (auto _ : state) {
state.PauseTiming();
LevelDbStore::destroy(DB_PATH);
LevelDbStore store(DB_PATH);
LevelDbTree db(store, DEPTH);
MemoryStore store;
MerkleTree<MemoryStore> db(store, DEPTH);
state.ResumeTiming();
for (size_t i = 0; i < (size_t)state.range(0); ++i) {
db.update_element(i, VALUES[i]);
Expand All @@ -63,12 +59,11 @@ void update_random_elements(State& state) noexcept
{
for (auto _ : state) {
state.PauseTiming();
LevelDbStore::destroy(DB_PATH);
LevelDbStore store(DB_PATH);
LevelDbTree db(store, DEPTH);
MemoryStore store;
MerkleTree db(store, DEPTH);
for (size_t i = 0; i < (size_t)state.range(0); i++) {
state.PauseTiming();
auto index = LevelDbTree::index_t(engine.get_random_uint256());
auto index = MerkleTree<MemoryStore>::index_t(engine.get_random_uint256());
state.ResumeTiming();
db.update_element(index, VALUES[i]);
}
Expand Down
4 changes: 0 additions & 4 deletions cpp/src/aztec/stdlib/merkle_tree/merkle_tree.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "merkle_tree.hpp"
#include "hash.hpp"
#include "leveldb_store.hpp"
#include "memory_store.hpp"
#include <common/net.hpp>
#include <iostream>
Expand Down Expand Up @@ -289,9 +288,6 @@ template <typename Store> void MerkleTree<Store>::remove(fr const& key)
store_.del(key.to_buffer());
}

#ifndef __wasm__
template class MerkleTree<LevelDbStore>;
#endif
template class MerkleTree<MemoryStore>;

} // namespace merkle_tree
Expand Down
4 changes: 0 additions & 4 deletions cpp/src/aztec/stdlib/merkle_tree/merkle_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace merkle_tree {

using namespace barretenberg;

class LevelDbStore;
class MemoryStore;

template <typename Store> class MerkleTree {
Expand Down Expand Up @@ -96,11 +95,8 @@ template <typename Store> class MerkleTree {
uint8_t tree_id_;
};

extern template class MerkleTree<LevelDbStore>;
extern template class MerkleTree<MemoryStore>;

typedef MerkleTree<LevelDbStore> LevelDbTree;

} // namespace merkle_tree
} // namespace stdlib
} // namespace plonk
68 changes: 2 additions & 66 deletions cpp/src/aztec/stdlib/merkle_tree/merkle_tree.test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "leveldb_store.hpp"
#include "merkle_tree.hpp"
#include "memory_store.hpp"
#include "memory_tree.hpp"
Expand Down Expand Up @@ -101,7 +100,7 @@ TEST(stdlib_merkle_tree, test_get_hash_path)
EXPECT_EQ(db.get_hash_path(512), memdb.get_hash_path(512));
}

TEST(stdlib_merkle_tree, test_leveldb_get_hash_path_layers)
TEST(stdlib_merkle_tree, test_get_hash_path_layers)
{
{
MemoryStore store;
Expand All @@ -128,67 +127,4 @@ TEST(stdlib_merkle_tree, test_leveldb_get_hash_path_layers)
EXPECT_EQ(before[1], after[1]);
EXPECT_NE(before[2], after[2]);
}
}

#ifndef __wasm__
std::string DB_PATH = format("/tmp/leveldb_test_", random_engine.get_random_uint128());

TEST(stdlib_merkle_tree, test_leveldb_vs_memory_consistency)
{
constexpr size_t depth = 10;
MemoryTree memdb(depth);

LevelDbStore::destroy(DB_PATH);
LevelDbStore store(DB_PATH);
LevelDbTree db(store, depth);

std::vector<size_t> indicies(1 << depth);
std::iota(indicies.begin(), indicies.end(), 0);
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(indicies.begin(), indicies.end(), g);

for (size_t i = 0; i < indicies.size(); ++i) {
size_t idx = indicies[i];
memdb.update_element(idx, VALUES[idx]);
db.update_element(idx, VALUES[idx]);
}

for (size_t i = 0; i < indicies.size(); ++i) {
size_t idx = indicies[i];
EXPECT_EQ(db.get_hash_path(idx), memdb.get_hash_path(idx));
}

EXPECT_EQ(db.root(), memdb.root());

LevelDbStore::destroy(DB_PATH);
}

TEST(stdlib_merkle_tree, test_leveldb_persistence)
{
LevelDbStore::destroy(DB_PATH);

fr root;
fr_hash_path path;
{
LevelDbStore store(DB_PATH);
LevelDbTree db(store, 256);
db.update_element(0, VALUES[1]);
db.update_element(1, VALUES[2]);
db.update_element(2, VALUES[3]);
root = db.root();
path = db.get_hash_path(2);
store.commit();
}
{
LevelDbStore store(DB_PATH);
LevelDbTree db(store, 256);

EXPECT_EQ(db.root(), root);
EXPECT_EQ(db.size(), 3ULL);
EXPECT_EQ(db.get_hash_path(2), path);
}

LevelDbStore::destroy(DB_PATH);
}
#endif
}