extern crate bloock_smt; use crate::h256_rocksdb_blake2b::common; use bloock_merge::hash_algorithms::blake2b::Blake2b; use bloock_smt::error::SmtError; use bloock_smt::tree::SparseMerkleTree; use bloock_storage::kv::kv_rocks::RocksDB as Database; use bloock_storage::kv::KeyValue; use bloock_types::bytes::h256::H256; /* new ========= ========= ========= ========= */ #[test] fn test_new_smt_emtpy_leaves() { let mut db = common::init_rocksdb("new_smt_emtpy_leaves"); let smt: SparseMerkleTree = SparseMerkleTree::::new(&mut db, None) .expect("Error createing new SMT."); assert_eq!( smt.get_root().as_ref().is_none(), true, "Root was not inserted correctly to SMT." ); common::delete_folder("new_smt_emtpy_leaves"); } #[test] fn test_new_smt_one_leaf() { let mut db = common::init_rocksdb("new_smt_one_leaf"); let smt: SparseMerkleTree = SparseMerkleTree::::new(&mut db, Some(vec![[1u8; 32]])) .expect("Error createing new SMT."); assert_eq!( smt.get_root().unwrap(), [1u8; 32], "Root was not inserted correctly to SMT." ); assert_eq!( db.get(&[1u8; 32]).expect("Error at DB level:").unwrap(), vec![1u8, 0, 0], "Value was not inserted correctly to DB." ); common::delete_folder("new_smt_one_leaf"); } #[test] fn test_new_smt_two_leaves() { let mut db = common::init_rocksdb("new_smt_two_leaves"); let smt: SparseMerkleTree = SparseMerkleTree::::new(&mut db, Some(vec![[0u8; 32], [1u8; 32]])) .expect("Error createing new SMT."); let key_0_1 = [ 3u8, 127, 45, 161, 237, 218, 238, 67, 106, 133, 220, 204, 7, 34, 69, 228, 123, 196, 15, 21, 154, 156, 67, 94, 39, 81, 41, 150, 54, 177, 239, 3, ]; assert_eq!( smt.get_root().unwrap(), key_0_1, "Root was not inserted correctly to SMT." ); assert_eq!( db.get(&key_0_1) .expect("Root - Error at DB level:") .unwrap(), vec![ 2u8, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ], "Root value was not inserted correctly to DB." ); assert_eq!( db.get(&[0u8; 32]) .expect("Leaf 0 - Error at DB level:") .unwrap(), vec![1u8, 0, 7], "Leaf 0 value was not inserted correctly to DB." ); assert_eq!( db.get(&[1u8; 32]) .expect("Leaf 1 - Error at DB level:") .unwrap(), vec![1u8, 0, 7], "Leaf 1 value was not inserted correctly to DB." ); common::delete_folder("new_smt_two_leaves"); } /* load ========= ========= ========= ========= */ #[test] fn test_load_ok() { let mut db = common::init_rocksdb("load_ok"); let key: H256 = [1; 32]; let value: Vec = vec![ 0u8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, ]; assert_eq!(Ok(()), db.put(&key, &value), "Error putting node database."); let smt: SparseMerkleTree<'_, H256, Database, Blake2b> = SparseMerkleTree::::load(key, &mut db).unwrap(); let root = smt.get_root().unwrap(); assert_eq!(root, key, "The node should be root"); common::delete_folder("load_ok") } #[test] fn test_load_err_not_found() { let mut db = common::init_rocksdb("load_err_not_found"); let key: H256 = [1; 32]; let smt: Result, SmtError> = SparseMerkleTree::::load(key, &mut db); assert_eq!(smt.is_err(), true, "The node should be root"); common::delete_folder("load_err_not_found") } /* get_root ========= ========= ========= ========= */ #[test] fn test_get_root() { let mut db = common::init_rocksdb("get_root_smt"); common::init_tree_2_4(&mut db); let key = [ 122, 252, 138, 54, 248, 253, 151, 142, 83, 210, 35, 173, 157, 102, 32, 117, 35, 213, 168, 147, 185, 161, 178, 126, 231, 110, 118, 87, 90, 141, 63, 203, ]; let smt: SparseMerkleTree = SparseMerkleTree::::load(key, &mut db).unwrap(); assert_eq!(key, smt.get_root().unwrap(),); common::delete_folder("get_root_smt") } /* get_datasource ========= ========= ========= ========= */ #[test] fn test_get_datasource() { let mut db = common::init_rocksdb("get_datasource_smt"); common::init_tree_2_4(&mut db); let smt: SparseMerkleTree = SparseMerkleTree::::load( [ 122, 252, 138, 54, 248, 253, 151, 142, 83, 210, 35, 173, 157, 102, 32, 117, 35, 213, 168, 147, 185, 161, 178, 126, 231, 110, 118, 87, 90, 141, 63, 203, ], &mut db, ) .unwrap(); assert_eq!( smt.get_datasource() .get(&[ 122, 252, 138, 54, 248, 253, 151, 142, 83, 210, 35, 173, 157, 102, 32, 117, 35, 213, 168, 147, 185, 161, 178, 126, 231, 110, 118, 87, 90, 141, 63, 203, ]) .unwrap() .is_some(), true ); common::delete_folder("get_datasource_smt") } /* ========= ========= ========= ========= */