diff --git a/cpp/src/aztec/join_split_example/CMakeLists.txt b/cpp/src/aztec/join_split_example/CMakeLists.txt index 62bba1c3d9..3df3519f9f 100644 --- a/cpp/src/aztec/join_split_example/CMakeLists.txt +++ b/cpp/src/aztec/join_split_example/CMakeLists.txt @@ -1,4 +1 @@ -if(NOT WASM) - link_libraries(leveldb) -endif() add_subdirectory(proofs) \ No newline at end of file diff --git a/cpp/src/aztec/stdlib/merkle_tree/CMakeLists.txt b/cpp/src/aztec/stdlib/merkle_tree/CMakeLists.txt index dac8373d78..2457820baf 100644 --- a/cpp/src/aztec/stdlib/merkle_tree/CMakeLists.txt +++ b/cpp/src/aztec/stdlib/merkle_tree/CMakeLists.txt @@ -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() diff --git a/cpp/src/aztec/stdlib/merkle_tree/index.hpp b/cpp/src/aztec/stdlib/merkle_tree/index.hpp index 741a4ad2cb..9453daeda7 100644 --- a/cpp/src/aztec/stdlib/merkle_tree/index.hpp +++ b/cpp/src/aztec/stdlib/merkle_tree/index.hpp @@ -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" diff --git a/cpp/src/aztec/stdlib/merkle_tree/leveldb_store.hpp b/cpp/src/aztec/stdlib/merkle_tree/leveldb_store.hpp deleted file mode 100644 index 1d464a495f..0000000000 --- a/cpp/src/aztec/stdlib/merkle_tree/leveldb_store.hpp +++ /dev/null @@ -1,105 +0,0 @@ -#pragma once -#ifndef __wasm__ -#include "hash_path.hpp" -#include -#include -#include -#include -#include - -namespace plonk { -namespace stdlib { -namespace merkle_tree { - -using namespace leveldb; - -inline std::string to_string(std::vector const& input) -{ - return std::string((char*)input.data(), input.size()); -} - -class LevelDbStore { - public: - LevelDbStore(std::string const& db_path) - { - leveldb::DB* db; - leveldb::Options options; - options.create_if_missing = true; - options.compression = leveldb::kNoCompression; - leveldb::Status status = leveldb::DB::Open(options, db_path, &db); - ASSERT(status.ok()); - db_.reset(db); - } - - static void destroy(std::string path) { leveldb::DestroyDB(path, leveldb::Options()); } - - bool put(std::vector const& key, std::vector const& value) - { - auto key_str = to_string(key); - return put(key_str, value); - } - - bool put(std::string const& key, std::vector const& value) - { - puts_[key] = to_string(value); - deletes_.erase(key); - return true; - } - - bool del(std::vector const& key) - { - auto key_str = to_string(key); - puts_.erase(key_str); - deletes_.insert(key_str); - return true; - }; - - bool get(std::vector const& key, std::vector& value) { return get(to_string(key), value); } - - bool get(std::string const& key, std::vector& value) - { - if (deletes_.find(key) != deletes_.end()) { - return false; - } - auto it = puts_.find(key); - if (it != puts_.end()) { - value = std::vector(it->second.begin(), it->second.end()); - return true; - } else { - std::string result; - leveldb::Status status = db_->Get(ReadOptions(), key, &result); - value = { result.begin(), result.end() }; - return status.ok(); - } - } - - void commit() - { - leveldb::WriteBatch batch; - for (auto it : puts_) { - batch.Put(it.first, it.second); - } - for (auto key : deletes_) { - batch.Delete(key); - } - db_->Write(leveldb::WriteOptions(), &batch); - puts_.clear(); - deletes_.clear(); - } - - void rollback() - { - puts_.clear(); - deletes_.clear(); - } - - private: - std::unique_ptr db_; - std::map puts_; - std::set deletes_; -}; - -} // namespace merkle_tree -} // namespace stdlib -} // namespace plonk -#endif \ No newline at end of file diff --git a/cpp/src/aztec/stdlib/merkle_tree/membership.test.cpp b/cpp/src/aztec/stdlib/merkle_tree/membership.test.cpp index 89f3ad2118..561be281cd 100644 --- a/cpp/src/aztec/stdlib/merkle_tree/membership.test.cpp +++ b/cpp/src/aztec/stdlib/merkle_tree/membership.test.cpp @@ -1,4 +1,3 @@ -#include "leveldb_store.hpp" #include "merkle_tree.hpp" #include "membership.hpp" #include "memory_store.hpp" diff --git a/cpp/src/aztec/stdlib/merkle_tree/merkle_tree.bench.cpp b/cpp/src/aztec/stdlib/merkle_tree/merkle_tree.bench.cpp index b41f30f963..ebf89bb03f 100644 --- a/cpp/src/aztec/stdlib/merkle_tree/merkle_tree.bench.cpp +++ b/cpp/src/aztec/stdlib/merkle_tree/merkle_tree.bench.cpp @@ -1,8 +1,7 @@ #include "hash.hpp" -#include "leveldb_store.hpp" +#include "memory_store.hpp" #include "merkle_tree.hpp" #include -#include #include using namespace benchmark; @@ -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 VALUES = []() { std::vector values(MAX); @@ -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 db(store, DEPTH); for (auto _ : state) { db.update_element(0, VALUES[1]); @@ -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 db(store, DEPTH); state.ResumeTiming(); for (size_t i = 0; i < (size_t)state.range(0); ++i) { db.update_element(i, VALUES[i]); @@ -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::index_t(engine.get_random_uint256()); state.ResumeTiming(); db.update_element(index, VALUES[i]); } diff --git a/cpp/src/aztec/stdlib/merkle_tree/merkle_tree.cpp b/cpp/src/aztec/stdlib/merkle_tree/merkle_tree.cpp index 645f2ee31d..ce4db0b01d 100644 --- a/cpp/src/aztec/stdlib/merkle_tree/merkle_tree.cpp +++ b/cpp/src/aztec/stdlib/merkle_tree/merkle_tree.cpp @@ -1,6 +1,5 @@ #include "merkle_tree.hpp" #include "hash.hpp" -#include "leveldb_store.hpp" #include "memory_store.hpp" #include #include @@ -289,9 +288,6 @@ template void MerkleTree::remove(fr const& key) store_.del(key.to_buffer()); } -#ifndef __wasm__ -template class MerkleTree; -#endif template class MerkleTree; } // namespace merkle_tree diff --git a/cpp/src/aztec/stdlib/merkle_tree/merkle_tree.hpp b/cpp/src/aztec/stdlib/merkle_tree/merkle_tree.hpp index 976af80e16..5e97bd3efe 100644 --- a/cpp/src/aztec/stdlib/merkle_tree/merkle_tree.hpp +++ b/cpp/src/aztec/stdlib/merkle_tree/merkle_tree.hpp @@ -8,7 +8,6 @@ namespace merkle_tree { using namespace barretenberg; -class LevelDbStore; class MemoryStore; template class MerkleTree { @@ -96,11 +95,8 @@ template class MerkleTree { uint8_t tree_id_; }; -extern template class MerkleTree; extern template class MerkleTree; -typedef MerkleTree LevelDbTree; - } // namespace merkle_tree } // namespace stdlib } // namespace plonk \ No newline at end of file diff --git a/cpp/src/aztec/stdlib/merkle_tree/merkle_tree.test.cpp b/cpp/src/aztec/stdlib/merkle_tree/merkle_tree.test.cpp index 8f2efa0f0b..4f1aee66a1 100644 --- a/cpp/src/aztec/stdlib/merkle_tree/merkle_tree.test.cpp +++ b/cpp/src/aztec/stdlib/merkle_tree/merkle_tree.test.cpp @@ -1,4 +1,3 @@ -#include "leveldb_store.hpp" #include "merkle_tree.hpp" #include "memory_store.hpp" #include "memory_tree.hpp" @@ -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; @@ -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 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 \ No newline at end of file +} \ No newline at end of file