diff --git a/.github/workflows/check_lint.yml b/.github/workflows/check_lint.yml index 17df54c..f95f00b 100644 --- a/.github/workflows/check_lint.yml +++ b/.github/workflows/check_lint.yml @@ -5,18 +5,18 @@ on: - oss -name: Check and Lint and wasm +name: Check and Lint and no-std jobs: - build-wasm: - name: Build wasm + build-no-std: + name: Build no-std runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Install rust working-directory: ./ensure_no_std run: rustup show - - name: Build wasm + - name: Build no-std run: cargo build fmt: @@ -49,12 +49,12 @@ jobs: - name: Clippy default features run: cargo clippy - clippy-wasm: + clippy-no-std: name: Clippy runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Install rust run: rustup show - - name: Clippy wasm + - name: Clippy no-std run: cargo clippy --no-default-features diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2265daa..251c4dd 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -42,6 +42,7 @@ jobs: tree; zip -0 ccov.zip `find . \( -name "$PROJECT_NAME_UNDERSCORE*.gc*" \) -print`; grcov ccov.zip -s . -t lcov --llvm --ignore-not-existing --ignore "/*" --ignore "tests/*" -o lcov.info; + cargo test $CARGO_OPTIONS --no-default-features - name: Upload test results uses: EnricoMi/publish-unit-test-result-action@v1 with: diff --git a/.gitignore b/.gitignore index b1dec57..baa0341 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ target/ rocksdb/ .vscode/ -documentation/node_modules/ \ No newline at end of file +documentation/node_modules/ +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml index d1245bf..4ab77d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,12 +33,11 @@ rocksdb = { optional = true, version = "0.21.0", features = [ ] } [dev-dependencies] -mp-commitments = { git = "https://github.com/keep-starknet-strange/madara.git", rev = "f30acea8af7e28e956e771928130e12bfc084832", package = "mp-commitments" } -mp-felt = { git = "https://github.com/keep-starknet-strange/madara.git", rev = "f30acea8af7e28e956e771928130e12bfc084832", package = "mp-felt", features = [ +mp-felt = { git = "https://github.com/keep-starknet-strange/madara.git", branch = "main", package = "mp-felt", features = [ "parity-scale-codec", "serde", ] } -mp-hashers = { git = "https://github.com/keep-starknet-strange/madara.git", rev = "f30acea8af7e28e956e771928130e12bfc084832", package = "mp-hashers" } +mp-hashers = { git = "https://github.com/keep-starknet-strange/madara.git", branch = "main", package = "mp-hashers" } pathfinder-common = { git = "https://github.com/massalabs/pathfinder.git", package = "pathfinder-common", rev = "b7b6d76a76ab0e10f92e5f84ce099b5f727cb4db" } pathfinder-crypto = { git = "https://github.com/massalabs/pathfinder.git", package = "pathfinder-crypto", rev = "b7b6d76a76ab0e10f92e5f84ce099b5f727cb4db" } pathfinder-merkle-tree = { git = "https://github.com/massalabs/pathfinder.git", package = "pathfinder-merkle-tree", rev = "b7b6d76a76ab0e10f92e5f84ce099b5f727cb4db" } diff --git a/ensure_no_std/Cargo.lock b/ensure_no_std/Cargo.lock index c273b59..05af383 100644 --- a/ensure_no_std/Cargo.lock +++ b/ensure_no_std/Cargo.lock @@ -14,6 +14,12 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "allocator-api2" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0942ffc6dcaadf03badf6e6a2d0228460359d5e34b57ccdc720b7382dfbd5ec5" + [[package]] name = "arrayvec" version = "0.7.4" @@ -57,7 +63,6 @@ dependencies = [ "log", "parity-scale-codec", "serde", - "serde_derive", "starknet-types-core", ] @@ -156,6 +161,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" dependencies = [ "ahash", + "allocator-api2", ] [[package]] diff --git a/src/bonsai_database.rs b/src/bonsai_database.rs index 6f5b3c4..0d49152 100644 --- a/src/bonsai_database.rs +++ b/src/bonsai_database.rs @@ -31,6 +31,9 @@ impl KeyType<'_> { /// Trait to be implemented on any type that can be used as a database. pub trait BonsaiDatabase { type Batch: Default; + #[cfg(feature = "std")] + type DatabaseError: std::error::Error + Into; + #[cfg(not(feature = "std"))] type DatabaseError: Into; /// Create a new empty batch of changes to be used in `insert`, `remove` and applied in database using `write_batch`. @@ -78,6 +81,9 @@ pub trait BonsaiDatabase { } pub trait BonsaiPersistentDatabase { + #[cfg(feature = "std")] + type DatabaseError: std::error::Error + Into; + #[cfg(not(feature = "std"))] type DatabaseError: Into; type Transaction: BonsaiDatabase; /// Save a snapshot of the current database state diff --git a/src/databases/hashmap_db.rs b/src/databases/hashmap_db.rs index bf13e4c..74a2d13 100644 --- a/src/databases/hashmap_db.rs +++ b/src/databases/hashmap_db.rs @@ -1,26 +1,23 @@ +use crate::{ + bonsai_database::BonsaiPersistentDatabase, error::BonsaiStorageError, id::Id, BonsaiDatabase, +}; #[cfg(not(feature = "std"))] use alloc::{ - fmt, - fmt::Display, vec::Vec, {collections::BTreeMap, string::ToString}, }; +use core::{fmt, fmt::Display}; #[cfg(not(feature = "std"))] use hashbrown::HashMap; #[cfg(feature = "std")] -use std::{ - collections::{BTreeMap, HashMap}, - fmt, - fmt::Display, -}; - -use crate::{ - bonsai_database::BonsaiPersistentDatabase, error::BonsaiStorageError, id::Id, BonsaiDatabase, -}; +use std::collections::{BTreeMap, HashMap}; #[derive(Debug)] pub struct HashMapDbError {} +#[cfg(feature = "std")] +impl std::error::Error for HashMapDbError {} + impl Display for HashMapDbError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "") @@ -34,25 +31,11 @@ impl From for BonsaiStorageError { } #[derive(Clone, Default)] -pub struct HashMapDbConfig {} - -#[derive(Clone)] pub struct HashMapDb { - config: HashMapDbConfig, db: HashMap, Vec>, snapshots: BTreeMap>, } -impl HashMapDb { - pub fn new(config: HashMapDbConfig) -> Self { - Self { - config, - db: HashMap::new(), - snapshots: BTreeMap::new(), - } - } -} - impl BonsaiDatabase for HashMapDb { type Batch = (); type DatabaseError = HashMapDbError; @@ -122,7 +105,7 @@ impl BonsaiDatabase for HashMapDb { #[cfg(test)] fn dump_database(&self) { - println!("{:?}", self.db); + log::debug!("{:?}", self.db); } } diff --git a/src/databases/mod.rs b/src/databases/mod.rs index 2f3eb0f..e73625d 100644 --- a/src/databases/mod.rs +++ b/src/databases/mod.rs @@ -1,6 +1,6 @@ #![allow(dead_code)] mod hashmap_db; -pub use hashmap_db::{HashMapDb, HashMapDbConfig}; +pub use hashmap_db::HashMapDb; #[cfg(feature = "rocksdb")] mod rocks_db; diff --git a/src/lib.rs b/src/lib.rs index 10dfb19..8335803 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,7 @@ //! This implementation can be used with any database that implements the `BonsaiDatabase` trait. //! //! Example usage with a RocksDB database: -//! ``` +//! ```ignore //! # use bonsai_trie::{ //! # databases::{RocksDB, create_rocks_db, RocksDBConfig}, //! # BonsaiStorageError, diff --git a/src/tests/madara_comparison.rs b/src/tests/madara_comparison.rs index 07e81c5..6a525f5 100644 --- a/src/tests/madara_comparison.rs +++ b/src/tests/madara_comparison.rs @@ -1,3 +1,4 @@ +#![cfg(feature = "std")] use bitvec::{bits, order::Msb0, vec::BitVec}; use starknet_types_core::{felt::Felt, hash::Pedersen}; diff --git a/src/tests/proof.rs b/src/tests/proof.rs index 479f019..a315eda 100644 --- a/src/tests/proof.rs +++ b/src/tests/proof.rs @@ -1,8 +1,4 @@ -#[cfg(feature = "std")] -use std::collections::HashMap; -#[cfg(not(feature = "std"))] -use {alloc::string::String, hashbrown::HashMap}; - +#![cfg(feature = "std")] use bitvec::vec::BitVec; use pathfinder_common::{hash::PedersenHash, trie::TrieNode}; use pathfinder_crypto::Felt as PathfinderFelt; @@ -10,6 +6,7 @@ use pathfinder_merkle_tree::tree::{MerkleTree, TestStorage}; use pathfinder_storage::{Node, StoredNode}; use rand::Rng; use starknet_types_core::{felt::Felt, hash::Pedersen}; +use std::collections::HashMap; use crate::{ databases::{create_rocks_db, RocksDB, RocksDBConfig}, diff --git a/src/tests/simple.rs b/src/tests/simple.rs index ef84608..a67902f 100644 --- a/src/tests/simple.rs +++ b/src/tests/simple.rs @@ -1,3 +1,4 @@ +#![cfg(feature = "std")] use crate::{ databases::{create_rocks_db, RocksDB, RocksDBConfig}, id::BasicIdBuilder, diff --git a/src/tests/transactional_state.rs b/src/tests/transactional_state.rs index 41e2c00..84c1fd7 100644 --- a/src/tests/transactional_state.rs +++ b/src/tests/transactional_state.rs @@ -1,3 +1,4 @@ +#![cfg(feature = "std")] use crate::{ databases::{create_rocks_db, RocksDB, RocksDBConfig}, id::BasicIdBuilder, diff --git a/src/tests/trie_log.rs b/src/tests/trie_log.rs index f061e25..3145425 100644 --- a/src/tests/trie_log.rs +++ b/src/tests/trie_log.rs @@ -1,3 +1,4 @@ +#![cfg(feature = "std")] use crate::{ databases::{create_rocks_db, RocksDB, RocksDBConfig}, id::BasicIdBuilder, diff --git a/src/trie/merkle_tree.rs b/src/trie/merkle_tree.rs index 071c7a8..df2debe 100644 --- a/src/trie/merkle_tree.rs +++ b/src/trie/merkle_tree.rs @@ -1209,7 +1209,6 @@ mod tests { KeyValueDB, }; use bitvec::vec::BitVec; - use mp_commitments::{calculate_class_commitment_leaf_hash, StateCommitmentTree}; use mp_felt::Felt252Wrapper; use mp_hashers::pedersen::PedersenHasher; use parity_scale_codec::{Decode, Encode}; @@ -1228,170 +1227,170 @@ mod tests { Felt252Wrapper::decode(&mut &encoded[..]).unwrap() } - #[test] - fn one_commit_tree_compare() { - let mut elements = vec![]; - let tempdir = tempfile::tempdir().unwrap(); - let mut rng = rand::thread_rng(); - let tree_size = rng.gen_range(10..100); - for _ in 0..tree_size { - let mut element = String::from("0x"); - let element_size = rng.gen_range(10..32); - for _ in 0..element_size { - let random_byte: u8 = rng.gen(); - element.push_str(&format!("{:02x}", random_byte)); - } - elements.push(Felt::from_hex(&element).unwrap()); - } - let madara_elements = elements - .iter() - .map(madara_felt_from_felt) - .collect::>(); - let rocks_db = create_rocks_db(std::path::Path::new(tempdir.path())).unwrap(); - let rocks_db = RocksDB::new(&rocks_db, RocksDBConfig::default()); - let db = KeyValueDB::new(rocks_db, KeyValueDBConfig::default(), None); - let mut bonsai_tree: super::MerkleTree, BasicId> = - super::MerkleTree::new(db).unwrap(); - let root_hash = mp_commitments::calculate_class_commitment_tree_root_hash::( - &madara_elements, - ); - elements - .iter() - .zip(madara_elements.iter()) - .for_each(|(element, madara_element)| { - let final_hash = - calculate_class_commitment_leaf_hash::(*madara_element); - let key = &element.to_bytes_be()[..31]; - bonsai_tree - .set( - &BitVec::from_vec(key.to_vec()), - felt_from_madara_felt(&final_hash), - ) - .unwrap(); - }); - bonsai_tree.display(); - assert_eq!( - bonsai_tree.commit().unwrap(), - felt_from_madara_felt(&root_hash) - ); - } + // #[test] + // fn one_commit_tree_compare() { + // let mut elements = vec![]; + // let tempdir = tempfile::tempdir().unwrap(); + // let mut rng = rand::thread_rng(); + // let tree_size = rng.gen_range(10..100); + // for _ in 0..tree_size { + // let mut element = String::from("0x"); + // let element_size = rng.gen_range(10..32); + // for _ in 0..element_size { + // let random_byte: u8 = rng.gen(); + // element.push_str(&format!("{:02x}", random_byte)); + // } + // elements.push(Felt::from_hex(&element).unwrap()); + // } + // let madara_elements = elements + // .iter() + // .map(madara_felt_from_felt) + // .collect::>(); + // let rocks_db = create_rocks_db(std::path::Path::new(tempdir.path())).unwrap(); + // let rocks_db = RocksDB::new(&rocks_db, RocksDBConfig::default()); + // let db = KeyValueDB::new(rocks_db, KeyValueDBConfig::default(), None); + // let mut bonsai_tree: super::MerkleTree, BasicId> = + // super::MerkleTree::new(db).unwrap(); + // let root_hash = mp_commitments::calculate_class_commitment_tree_root_hash::( + // &madara_elements, + // ); + // elements + // .iter() + // .zip(madara_elements.iter()) + // .for_each(|(element, madara_element)| { + // let final_hash = + // calculate_class_commitment_leaf_hash::(*madara_element); + // let key = &element.to_bytes_be()[..31]; + // bonsai_tree + // .set( + // &BitVec::from_vec(key.to_vec()), + // felt_from_madara_felt(&final_hash), + // ) + // .unwrap(); + // }); + // bonsai_tree.display(); + // assert_eq!( + // bonsai_tree.commit().unwrap(), + // felt_from_madara_felt(&root_hash) + // ); + // } - #[test] - fn simple_commits() { - let tempdir = tempfile::tempdir().unwrap(); - let mut madara_tree = StateCommitmentTree::::default(); - let rocks_db = create_rocks_db(std::path::Path::new(tempdir.path())).unwrap(); - let rocks_db = RocksDB::new(&rocks_db, RocksDBConfig::default()); - let db = KeyValueDB::new(rocks_db, KeyValueDBConfig::default(), None); - let mut bonsai_tree: super::MerkleTree, BasicId> = - super::MerkleTree::new(db).unwrap(); - let elements = [ - [Felt::from_hex("0x665342762FDD54D0303c195fec3ce2568b62052e").unwrap()], - [Felt::from_hex("0x66342762FDD54D0303c195fec3ce2568b62052e").unwrap()], - [Felt::from_hex("0x66342762FDD54D033c195fec3ce2568b62052e").unwrap()], - ]; - for elem in elements { - elem.iter().for_each(|class_hash| { - let final_hash = - felt_from_madara_felt(&calculate_class_commitment_leaf_hash::( - madara_felt_from_felt(class_hash), - )); - madara_tree.set( - madara_felt_from_felt(class_hash), - madara_felt_from_felt(&final_hash), - ); - let key = &class_hash.to_bytes_be()[..31]; - bonsai_tree - .set(&BitVec::from_vec(key.to_vec()), final_hash) - .unwrap(); - }); - } - let madara_root_hash = madara_tree.commit(); - let bonsai_root_hash = bonsai_tree.commit().unwrap(); - assert_eq!(bonsai_root_hash, felt_from_madara_felt(&madara_root_hash)); - } + // #[test] + // fn simple_commits() { + // let tempdir = tempfile::tempdir().unwrap(); + // let mut madara_tree = StateCommitmentTree::::default(); + // let rocks_db = create_rocks_db(std::path::Path::new(tempdir.path())).unwrap(); + // let rocks_db = RocksDB::new(&rocks_db, RocksDBConfig::default()); + // let db = KeyValueDB::new(rocks_db, KeyValueDBConfig::default(), None); + // let mut bonsai_tree: super::MerkleTree, BasicId> = + // super::MerkleTree::new(db).unwrap(); + // let elements = [ + // [Felt::from_hex("0x665342762FDD54D0303c195fec3ce2568b62052e").unwrap()], + // [Felt::from_hex("0x66342762FDD54D0303c195fec3ce2568b62052e").unwrap()], + // [Felt::from_hex("0x66342762FDD54D033c195fec3ce2568b62052e").unwrap()], + // ]; + // for elem in elements { + // elem.iter().for_each(|class_hash| { + // let final_hash = + // felt_from_madara_felt(&calculate_class_commitment_leaf_hash::( + // madara_felt_from_felt(class_hash), + // )); + // madara_tree.set( + // madara_felt_from_felt(class_hash), + // madara_felt_from_felt(&final_hash), + // ); + // let key = &class_hash.to_bytes_be()[..31]; + // bonsai_tree + // .set(&BitVec::from_vec(key.to_vec()), final_hash) + // .unwrap(); + // }); + // } + // let madara_root_hash = madara_tree.commit(); + // let bonsai_root_hash = bonsai_tree.commit().unwrap(); + // assert_eq!(bonsai_root_hash, felt_from_madara_felt(&madara_root_hash)); + // } - #[test] - fn simple_commits_and_delete() { - let tempdir = tempfile::tempdir().unwrap(); - let rocks_db = create_rocks_db(std::path::Path::new(tempdir.path())).unwrap(); - let rocks_db = RocksDB::new(&rocks_db, RocksDBConfig::default()); - let db = KeyValueDB::new(rocks_db, KeyValueDBConfig::default(), None); - let mut bonsai_tree: super::MerkleTree, BasicId> = - super::MerkleTree::new(db).unwrap(); - let elements = [ - [Felt::from_hex("0x665342762FDD54D0303c195fec3ce2568b62052e").unwrap()], - [Felt::from_hex("0x66342762FDD54D0303c195fec3ce2568b62052e").unwrap()], - [Felt::from_hex("0x66342762FDD54D033c195fec3ce2568b62052e").unwrap()], - ]; - for elem in elements { - elem.iter().for_each(|class_hash| { - let final_hash = calculate_class_commitment_leaf_hash::( - madara_felt_from_felt(class_hash), - ); - let key = &class_hash.to_bytes_be()[..31]; - bonsai_tree - .set( - &BitVec::from_vec(key.to_vec()), - felt_from_madara_felt(&final_hash), - ) - .unwrap(); - }); - } - bonsai_tree.commit().unwrap(); - for elem in elements { - elem.iter().for_each(|class_hash| { - let key = &class_hash.to_bytes_be()[..31]; - bonsai_tree - .set(&BitVec::from_vec(key.to_vec()), Felt::ZERO) - .unwrap(); - }); - } - bonsai_tree.commit().unwrap(); - } + // #[test] + // fn simple_commits_and_delete() { + // let tempdir = tempfile::tempdir().unwrap(); + // let rocks_db = create_rocks_db(std::path::Path::new(tempdir.path())).unwrap(); + // let rocks_db = RocksDB::new(&rocks_db, RocksDBConfig::default()); + // let db = KeyValueDB::new(rocks_db, KeyValueDBConfig::default(), None); + // let mut bonsai_tree: super::MerkleTree, BasicId> = + // super::MerkleTree::new(db).unwrap(); + // let elements = [ + // [Felt::from_hex("0x665342762FDD54D0303c195fec3ce2568b62052e").unwrap()], + // [Felt::from_hex("0x66342762FDD54D0303c195fec3ce2568b62052e").unwrap()], + // [Felt::from_hex("0x66342762FDD54D033c195fec3ce2568b62052e").unwrap()], + // ]; + // for elem in elements { + // elem.iter().for_each(|class_hash| { + // let final_hash = calculate_class_commitment_leaf_hash::( + // madara_felt_from_felt(class_hash), + // ); + // let key = &class_hash.to_bytes_be()[..31]; + // bonsai_tree + // .set( + // &BitVec::from_vec(key.to_vec()), + // felt_from_madara_felt(&final_hash), + // ) + // .unwrap(); + // }); + // } + // bonsai_tree.commit().unwrap(); + // for elem in elements { + // elem.iter().for_each(|class_hash| { + // let key = &class_hash.to_bytes_be()[..31]; + // bonsai_tree + // .set(&BitVec::from_vec(key.to_vec()), Felt::ZERO) + // .unwrap(); + // }); + // } + // bonsai_tree.commit().unwrap(); + // } - #[test] - fn multiple_commits_tree_compare() { - let mut rng = rand::thread_rng(); - let tempdir = tempfile::tempdir().unwrap(); - let mut madara_tree = StateCommitmentTree::::default(); - let rocks_db = create_rocks_db(std::path::Path::new(tempdir.path())).unwrap(); - let rocks_db = RocksDB::new(&rocks_db, RocksDBConfig::default()); - let db = KeyValueDB::new(rocks_db, KeyValueDBConfig::default(), None); - let mut bonsai_tree: super::MerkleTree, BasicId> = - super::MerkleTree::new(db).unwrap(); - let nb_commits = rng.gen_range(2..4); - for _ in 0..nb_commits { - let mut elements = vec![]; - let tree_size = rng.gen_range(10..100); - for _ in 0..tree_size { - let mut element = String::from("0x"); - let element_size = rng.gen_range(10..32); - for _ in 0..element_size { - let random_byte: u8 = rng.gen(); - element.push_str(&format!("{:02x}", random_byte)); - } - elements.push(Felt::from_hex(&element).unwrap()); - } - elements.iter().for_each(|class_hash| { - let final_hash = calculate_class_commitment_leaf_hash::( - madara_felt_from_felt(class_hash), - ); - madara_tree.set(madara_felt_from_felt(class_hash), final_hash); - let key = &class_hash.to_bytes_be()[..31]; - bonsai_tree - .set( - &BitVec::from_vec(key.to_vec()), - felt_from_madara_felt(&final_hash), - ) - .unwrap(); - }); - - let bonsai_root_hash = bonsai_tree.commit().unwrap(); - let madara_root_hash = madara_tree.commit(); - assert_eq!(bonsai_root_hash, felt_from_madara_felt(&madara_root_hash)); - } - } + // #[test] + // fn multiple_commits_tree_compare() { + // let mut rng = rand::thread_rng(); + // let tempdir = tempfile::tempdir().unwrap(); + // let mut madara_tree = StateCommitmentTree::::default(); + // let rocks_db = create_rocks_db(std::path::Path::new(tempdir.path())).unwrap(); + // let rocks_db = RocksDB::new(&rocks_db, RocksDBConfig::default()); + // let db = KeyValueDB::new(rocks_db, KeyValueDBConfig::default(), None); + // let mut bonsai_tree: super::MerkleTree, BasicId> = + // super::MerkleTree::new(db).unwrap(); + // let nb_commits = rng.gen_range(2..4); + // for _ in 0..nb_commits { + // let mut elements = vec![]; + // let tree_size = rng.gen_range(10..100); + // for _ in 0..tree_size { + // let mut element = String::from("0x"); + // let element_size = rng.gen_range(10..32); + // for _ in 0..element_size { + // let random_byte: u8 = rng.gen(); + // element.push_str(&format!("{:02x}", random_byte)); + // } + // elements.push(Felt::from_hex(&element).unwrap()); + // } + // elements.iter().for_each(|class_hash| { + // let final_hash = calculate_class_commitment_leaf_hash::( + // madara_felt_from_felt(class_hash), + // ); + // madara_tree.set(madara_felt_from_felt(class_hash), final_hash); + // let key = &class_hash.to_bytes_be()[..31]; + // bonsai_tree + // .set( + // &BitVec::from_vec(key.to_vec()), + // felt_from_madara_felt(&final_hash), + // ) + // .unwrap(); + // }); + + // let bonsai_root_hash = bonsai_tree.commit().unwrap(); + // let madara_root_hash = madara_tree.commit(); + // assert_eq!(bonsai_root_hash, felt_from_madara_felt(&madara_root_hash)); + // } + // } // #[test] // fn multiple_commits_tree_compare_with_deletes() { // let mut rng = rand::thread_rng(); @@ -1442,40 +1441,40 @@ mod tests { // assert_eq!(bonsai_root_hash, madara_root_hash); // } - #[test] - fn test_proof() { - let tempdir = tempfile::tempdir().unwrap(); - let rocks_db = create_rocks_db(std::path::Path::new(tempdir.path())).unwrap(); - let rocks_db = RocksDB::new(&rocks_db, RocksDBConfig::default()); - let db = KeyValueDB::new(rocks_db, KeyValueDBConfig::default(), None); - let mut bonsai_tree: super::MerkleTree, BasicId> = - super::MerkleTree::new(db).unwrap(); - let elements = [ - [Felt252Wrapper::from_hex_be("0x665342762FDD54D0303c195fec3ce2568b62052e").unwrap()], - [Felt252Wrapper::from_hex_be("0x66342762FDD54D0303c195fec3ce2568b62052e").unwrap()], - [Felt252Wrapper::from_hex_be("0x66342762FDD54D033c195fec3ce2568b62052e").unwrap()], - ]; - for elem in elements { - elem.iter().for_each(|class_hash| { - let final_hash = - calculate_class_commitment_leaf_hash::(*class_hash); - let key = &class_hash.0.to_bytes_be()[..31]; - bonsai_tree - .set( - &BitVec::from_vec(key.to_vec()), - Felt::from_bytes_be(&final_hash.0.to_bytes_be()), - ) - .unwrap(); - }); - } - bonsai_tree.commit().unwrap(); - let bonsai_proof = bonsai_tree - .get_proof(&BitVec::from_vec( - elements[0][0].0.to_bytes_be()[..31].to_vec(), - )) - .unwrap(); - println!("bonsai_proof: {:?}", bonsai_proof); - } + // #[test] + // fn test_proof() { + // let tempdir = tempfile::tempdir().unwrap(); + // let rocks_db = create_rocks_db(std::path::Path::new(tempdir.path())).unwrap(); + // let rocks_db = RocksDB::new(&rocks_db, RocksDBConfig::default()); + // let db = KeyValueDB::new(rocks_db, KeyValueDBConfig::default(), None); + // let mut bonsai_tree: super::MerkleTree, BasicId> = + // super::MerkleTree::new(db).unwrap(); + // let elements = [ + // [Felt252Wrapper::from_hex_be("0x665342762FDD54D0303c195fec3ce2568b62052e").unwrap()], + // [Felt252Wrapper::from_hex_be("0x66342762FDD54D0303c195fec3ce2568b62052e").unwrap()], + // [Felt252Wrapper::from_hex_be("0x66342762FDD54D033c195fec3ce2568b62052e").unwrap()], + // ]; + // for elem in elements { + // elem.iter().for_each(|class_hash| { + // let final_hash = + // calculate_class_commitment_leaf_hash::(*class_hash); + // let key = &class_hash.0.to_bytes_be()[..31]; + // bonsai_tree + // .set( + // &BitVec::from_vec(key.to_vec()), + // Felt::from_bytes_be(&final_hash.0.to_bytes_be()), + // ) + // .unwrap(); + // }); + // } + // bonsai_tree.commit().unwrap(); + // let bonsai_proof = bonsai_tree + // .get_proof(&BitVec::from_vec( + // elements[0][0].0.to_bytes_be()[..31].to_vec(), + // )) + // .unwrap(); + // println!("bonsai_proof: {:?}", bonsai_proof); + // } // test in madara // #[test]