From c4098ccd8051e015b9ceff5ce21531d4a52dfff2 Mon Sep 17 00:00:00 2001 From: Vera Gonzalez Date: Tue, 24 Oct 2023 11:22:25 -0400 Subject: [PATCH 1/9] feat: added prints --- wnfs-hamt/src/merge.rs | 5 +-- wnfs-hamt/src/node.rs | 31 ++++++++++++------ wnfs/Cargo.toml | 1 + wnfs/src/private/file.rs | 68 +++++++++++++++++++++++++++++----------- 4 files changed, 75 insertions(+), 30 deletions(-) diff --git a/wnfs-hamt/src/merge.rs b/wnfs-hamt/src/merge.rs index 27326237..db5dc7f7 100644 --- a/wnfs-hamt/src/merge.rs +++ b/wnfs-hamt/src/merge.rs @@ -4,6 +4,7 @@ use anyhow::Result; use serde::de::DeserializeOwned; use std::{hash::Hash, rc::Rc}; use wnfs_common::{BlockStore, Link}; +use std::fmt::Debug; //-------------------------------------------------------------------------------------------------- // Functions @@ -18,8 +19,8 @@ pub async fn merge( ) -> Result>> where F: Fn(&V, &V) -> Result, - K: DeserializeOwned + Eq + Clone + Hash + AsRef<[u8]>, - V: DeserializeOwned + Eq + Clone, + K: DeserializeOwned + Eq + Clone + Hash + AsRef<[u8]> + Debug, + V: DeserializeOwned + Eq + Clone + Debug, H: Hasher + Clone + 'static, { let kv_changes = super::diff(main_link.clone(), other_link.clone(), store).await?; diff --git a/wnfs-hamt/src/node.rs b/wnfs-hamt/src/node.rs index 07ae8b7d..ccd9c1fe 100644 --- a/wnfs-hamt/src/node.rs +++ b/wnfs-hamt/src/node.rs @@ -120,8 +120,8 @@ where /// ``` pub async fn get<'a>(&'a self, key: &K, store: &impl BlockStore) -> Result> where - K: DeserializeOwned + AsRef<[u8]>, - V: DeserializeOwned, + K: DeserializeOwned + AsRef<[u8]> + Debug, + V: DeserializeOwned + Debug, { let hash = &H::hash(key); @@ -200,15 +200,23 @@ where store: &impl BlockStore, ) -> Result> where - K: DeserializeOwned + AsRef<[u8]>, - V: DeserializeOwned, + K: DeserializeOwned + AsRef<[u8]> + Debug, + V: DeserializeOwned + Debug, { #[cfg(feature = "log")] debug!("get_by_hash: hash = {:02x?}", hash); + let mut hashnibble = HashNibbles::new(hash); + + println!("get_by_hash: hash = {:02x?}, digest = {:?}", hash, hashnibble.digest); + Ok(self - .get_value(&mut HashNibbles::new(hash), store) - .await? + .get_value(&mut hashnibble, store) + .await + .map_err(|err| { + println!("woah!!!! {}", err); + err + })? .map(|pair| &pair.value)) } @@ -375,12 +383,12 @@ where store: &impl BlockStore, ) -> Result>> where - K: DeserializeOwned + AsRef<[u8]>, - V: DeserializeOwned, + K: DeserializeOwned + AsRef<[u8]> + Debug, + V: DeserializeOwned + Debug, { let bit_index = hashnibbles.try_next()?; - // If the bit is not set yet, return None. + if !self.bitmask[bit_index] { return Ok(None); } @@ -388,11 +396,16 @@ where let value_index = self.get_value_index(bit_index); match &self.pointers[value_index] { Pointer::Values(values) => Ok({ + println!("trying find hashnibble {:?} in mysterious array", hashnibbles.digest); + for value in values { + println!("key: {:?} value: {:?}", &H::hash(&value.key), value.value); + } values .iter() .find(|p| &H::hash(&p.key) == hashnibbles.digest) }), Pointer::Link(link) => { + println!("trying to resolve value for {:?}", link.get_cid()); let child = link.resolve_value(store).await?; child.get_value(hashnibbles, store).await } diff --git a/wnfs/Cargo.toml b/wnfs/Cargo.toml index bb9626b0..06ab0b0f 100644 --- a/wnfs/Cargo.toml +++ b/wnfs/Cargo.toml @@ -26,6 +26,7 @@ async-stream = "0.3" async-trait = "0.1" chrono = { version = "0.4", default-features = false, features = ["clock", "std"] } futures = "0.3" +gloo = "0.10.0" libipld = { version = "0.16" } # TODO(appcypher): Change this to libipld_core once BlockStore codec has been changed to u64 value or enum multihash = "0.19" once_cell = "1.16" diff --git a/wnfs/src/private/file.rs b/wnfs/src/private/file.rs index 0f963bb5..e08e6953 100644 --- a/wnfs/src/private/file.rs +++ b/wnfs/src/private/file.rs @@ -351,7 +351,7 @@ impl PrivateFile { match &self.content.content { FileContent::Inline { data } => { if index != 0 { - Err(FsError::FileShardNotFound)? + Err(FsError::DirectoryAlreadyExists)? } yield data.clone() @@ -362,7 +362,8 @@ impl PrivateFile { .. } => { let bare_name = &self.header.bare_name; - for label in Self::generate_shard_labels(key, index, *block_count, bare_name) { + let shard_labels = Self::generate_shard_labels(key, index, *block_count, bare_name); + for label in shard_labels { let bytes = Self::decrypt_block(key, &label, forest, store).await?; yield bytes } @@ -591,20 +592,32 @@ impl PrivateFile { ) -> Result> { let label_hash = &Sha3_256::hash(&label.as_bytes()); - let cids = forest - .get_encrypted(label_hash, store) - .await? - .ok_or(FsError::FileShardNotFound)?; - - let cid = cids - .iter() - .next() - .expect("Expected set with at least a Cid"); - - let enc_bytes = store.get_block(cid).await?; - let bytes = key.decrypt(&enc_bytes)?; - - Ok(bytes) + println!("label_hash: {:?}", label_hash); + + match forest + .get_encrypted(label_hash, store) + .await { + Ok(Some(cids)) => { + println!("got all the cids! {:?}", cids); + let cid = cids + .iter() + .next() + .expect("Expected set with at least a Cid"); + + let enc_bytes = store.get_block(cid).await?; + let bytes = key.decrypt(&enc_bytes)?; + + Ok(bytes) + } + Ok(_) => { + println!("successfully realized no cids"); + panic!(""); + }, + Err(err) => { + println!("file shard not found: {}", err); + Err(FsError::DirectoryAlreadyExists.into()) + }, + } } pub async fn get_cids<'a>( @@ -622,9 +635,20 @@ impl PrivateFile { let label_hash = &Sha3_256::hash(&label.as_bytes()); let block_cids = forest .get_encrypted(label_hash, store) - .await? - .ok_or(FsError::FileShardNotFound)?; - cids.extend(block_cids) + .await; + + match block_cids { + Ok(Some(new_cids)) => { + cids.extend(new_cids); + }, + Ok(None) => { + println!("successfully realized there are no cids"); + } + Err(err) => { + println!("wsdfdfdfdfd {}", err); + panic!("dcjf;lkasdjf;sdlkfjsd"); + }, + } } Ok(cids) } @@ -639,6 +663,12 @@ impl PrivateFile { block_count: usize, bare_name: &'a Namefilter, ) -> impl Iterator + 'a { + let output = format!("generate_shard_labels:\nkey: {:?}, index: {}, block_count: {}, bare_name: {:?}", key.0.as_bytes(), index, block_count, bare_name.as_bytes()); + println!("{}", output); + + #[cfg(target_arch="wasm32")] + gloo::console::log!(output); + iter::from_fn(move || { if index >= block_count { return None; From 9cf5a48ebb54b36319b6c26eacb911ad701059bc Mon Sep 17 00:00:00 2001 From: Claudia Richoux Date: Tue, 24 Oct 2023 14:26:54 -0400 Subject: [PATCH 2/9] silly --- wnfs-hamt/Cargo.toml | 1 + wnfs-hamt/src/node.rs | 24 +++++++++++--- wnfs/src/private/file.rs | 68 ++++++++++++++++++++++++++++++++++++---- 3 files changed, 82 insertions(+), 11 deletions(-) diff --git a/wnfs-hamt/Cargo.toml b/wnfs-hamt/Cargo.toml index 34eb7b92..8cda7876 100644 --- a/wnfs-hamt/Cargo.toml +++ b/wnfs-hamt/Cargo.toml @@ -25,6 +25,7 @@ bitvec = { version = "1.0", features = ["serde"] } chrono = { version = "0.4.23", default-features = false, features = ["clock", "std"] } either = "1.8" futures = "0.3" +gloo = "0.10.0" libipld = { version = "0.16", features = ["dag-cbor", "derive", "serde-codec"] } log = { version = "0.4", optional = true } multihash = "0.18" diff --git a/wnfs-hamt/src/node.rs b/wnfs-hamt/src/node.rs index ccd9c1fe..17f99f67 100644 --- a/wnfs-hamt/src/node.rs +++ b/wnfs-hamt/src/node.rs @@ -208,7 +208,9 @@ where let mut hashnibble = HashNibbles::new(hash); - println!("get_by_hash: hash = {:02x?}, digest = {:?}", hash, hashnibble.digest); + println!("get_by_hash: hash = {:02x?}, digest = {:?}", hash, hashnibble); + #[cfg(target_arch="wasm32")] + gloo::console::log!(format!("get_by_hash: hash = {:02x?}, digest = {:?}", hash, hashnibble)); Ok(self .get_value(&mut hashnibble, store) @@ -217,7 +219,11 @@ where println!("woah!!!! {}", err); err })? - .map(|pair| &pair.value)) + .map(|pair| { + println!("woah!!!! here's da pair {:?}", pair); + #[cfg(target_arch="wasm32")] + gloo::console::log!(format!("woah!!!! here's da pair {:?}", pair)); + &pair.value})) } /// Removes the value at the key matching the provided hash. @@ -396,13 +402,21 @@ where let value_index = self.get_value_index(bit_index); match &self.pointers[value_index] { Pointer::Values(values) => Ok({ - println!("trying find hashnibble {:?} in mysterious array", hashnibbles.digest); + println!("trying find hashnibble {:?} in mysterious array", hashnibbles); + #[cfg(target_arch="wasm32")] + gloo::console::log!(format!("trying find hashnibble {:?} in mysterious array", hashnibbles)); for value in values { println!("key: {:?} value: {:?}", &H::hash(&value.key), value.value); + #[cfg(target_arch="wasm32")] + gloo::console::log!(format!("key: {:?} value: {:?}", &H::hash(&value.key), value.value)); } - values + let found = values .iter() - .find(|p| &H::hash(&p.key) == hashnibbles.digest) + .find(|p| &H::hash(&p.key) == hashnibbles.digest); + println!("found a guyyyy: {:?}", found); + #[cfg(target_arch="wasm32")] + gloo::console::log!(format!("found a guyyyy: {:?}", found)); + found }), Pointer::Link(link) => { println!("trying to resolve value for {:?}", link.get_cid()); diff --git a/wnfs/src/private/file.rs b/wnfs/src/private/file.rs index e08e6953..cdeec383 100644 --- a/wnfs/src/private/file.rs +++ b/wnfs/src/private/file.rs @@ -362,9 +362,20 @@ impl PrivateFile { .. } => { let bare_name = &self.header.bare_name; - let shard_labels = Self::generate_shard_labels(key, index, *block_count, bare_name); - for label in shard_labels { - let bytes = Self::decrypt_block(key, &label, forest, store).await?; + let shard_labels : Vec<_> = Self::generate_shard_labels(key, index, *block_count, bare_name).collect(); + println!("we are in stream_content"); + // println!("shard_labels: {:?}", &shard_labels); + // println!("forest: {:?}", forest); + println!("stream_content: bare_name: {:?}", bare_name.as_bytes()); + #[cfg(target_arch="wasm32")] + { + gloo::console::log!(format!("we are in stream_content")); + // gloo::console::log!(format!("shard_labels: {:?}", &shard_labels)); + // gloo::console::log!(format!("forest: {:?}", forest)); + gloo::console::log!(format!("stream_content: bare_name: {:?}", bare_name.as_bytes())); + }; + for label in shard_labels.iter() { + let bytes = Self::decrypt_block(key, label, forest, store).await?; yield bytes } } @@ -590,13 +601,30 @@ impl PrivateFile { forest: &PrivateForest, store: &impl BlockStore, ) -> Result> { + println!("WE ARE IN DECRYPT_BLOCK IN PRIVATE FILE"); + // println!("decrypt_block: label is {:?}", label); + // println!("snapshot key is: {:?}", key); + // println!("forest is: {:?}", forest); + #[cfg(target_arch="wasm32")] + { + gloo::console::log!(format!("WE ARE IN DECRYPT_BLOCK IN PRIVATE FILE")); + // gloo::console::log!(format!("decrypt_block: label is {:?}", label)); + // gloo::console::log!(format!("snapshot key is: {:?}", key)); + // gloo::console::log!(format!("forest is: {:?}", forest)); + }; + + let label_hash = &Sha3_256::hash(&label.as_bytes()); println!("label_hash: {:?}", label_hash); + #[cfg(target_arch="wasm32")] + { + gloo::console::log!(format!("label_hash: {:?}", label_hash)); + }; match forest - .get_encrypted(label_hash, store) - .await { + .get_encrypted(label_hash, store) + .await { Ok(Some(cids)) => { println!("got all the cids! {:?}", cids); let cid = cids @@ -631,6 +659,7 @@ impl PrivateFile { } => { let mut cids = >::new(); let bare_name = &self.header.bare_name; + for label in Self::generate_shard_labels(key, 0, *block_count, bare_name) { let label_hash = &Sha3_256::hash(&label.as_bytes()); let block_cids = forest @@ -671,11 +700,17 @@ impl PrivateFile { iter::from_fn(move || { if index >= block_count { + println!("returning none, index > block_count"); return None; } + println!("about to call create block label with index: {:?}", index); let label = Self::create_block_label(key, index, bare_name); index += 1; + + println!("in the end of generate shard labels: label: {:?}", label); + #[cfg(target_arch="wasm32")] + gloo::console::log!(format!("in the end of generate shard labels: label: {:?}", label)); Some(label) }) } @@ -683,12 +718,33 @@ impl PrivateFile { /// Creates the label for a block of a file. fn create_block_label(key: &SnapshotKey, index: usize, bare_name: &Namefilter) -> Namefilter { let key_bytes = key.0.as_bytes(); - let key_hash = Sha3_256::hash(&[key_bytes, &index.to_le_bytes()[..]].concat()); + let key_hash = Sha3_256::hash(&[key_bytes, &(index as u64).to_le_bytes()[..]].concat()); let mut label = bare_name.clone(); + println!("create_block_label: key_bytes: {:?}", key_bytes); + println!("create_block_label: key_hash: {:?}", key_hash); + println!("create_block_label: index: {:?} index_bytes: {:?}", index, &index.to_le_bytes()); + println!("create_block_label: label: {:?}", label.as_bytes()); + #[cfg(target_arch="wasm32")] + { + gloo::console::log!(format!("create_block_label: key_bytes: {:?}", key_bytes)); + gloo::console::log!(format!("create_block_label: key_hash: {:?}", key_hash)); + gloo::console::log!(format!("create_block_label: index: {:?} index_bytes: {:?}", index, &index.to_le_bytes())); + gloo::console::log!(format!("create_block_label: label: {:?}", label.as_bytes())); + }; + label.add(&key_bytes); + println!("create_block_label: label after adding key_bytes: {:?}", label.as_bytes()); + #[cfg(target_arch="wasm32")] + gloo::console::log!(format!("create_block_label: label after adding key_bytes: {:?}", label.as_bytes())); label.add(&key_hash); + println!("create_block_label: label after adding key_hash: {:?}", label.as_bytes()); + #[cfg(target_arch="wasm32")] + gloo::console::log!(format!("create_block_label: label after adding key_hash: {:?}", label.as_bytes())); label.saturate(); + println!("create_block_label: label after saturate: {:?}", label.as_bytes()); + #[cfg(target_arch="wasm32")] + gloo::console::log!(format!("create_block_label: label after saturate: {:?}", label.as_bytes())); label } From 9159c2d27e98a9230a6f9e329c72ddb5cb64ddf8 Mon Sep 17 00:00:00 2001 From: Vera Gonzalez Date: Wed, 25 Oct 2023 13:52:19 -0400 Subject: [PATCH 3/9] fix: removing old prints --- wnfs-hamt/src/merge.rs | 3 +- wnfs-hamt/src/node.rs | 38 ++-------- wnfs-hamt/src/strategies/changes.rs | 2 +- wnfs-wasm/src/fs/private/directory.rs | 2 +- wnfs/src/private/file.rs | 100 +++----------------------- 5 files changed, 17 insertions(+), 128 deletions(-) diff --git a/wnfs-hamt/src/merge.rs b/wnfs-hamt/src/merge.rs index db5dc7f7..b22f7005 100644 --- a/wnfs-hamt/src/merge.rs +++ b/wnfs-hamt/src/merge.rs @@ -2,9 +2,8 @@ use super::{ChangeType, Node}; use crate::{error::HamtError, Hasher}; use anyhow::Result; use serde::de::DeserializeOwned; -use std::{hash::Hash, rc::Rc}; +use std::{fmt::Debug, hash::Hash, rc::Rc}; use wnfs_common::{BlockStore, Link}; -use std::fmt::Debug; //-------------------------------------------------------------------------------------------------- // Functions diff --git a/wnfs-hamt/src/node.rs b/wnfs-hamt/src/node.rs index 17f99f67..5a9ed0d3 100644 --- a/wnfs-hamt/src/node.rs +++ b/wnfs-hamt/src/node.rs @@ -203,27 +203,11 @@ where K: DeserializeOwned + AsRef<[u8]> + Debug, V: DeserializeOwned + Debug, { - #[cfg(feature = "log")] - debug!("get_by_hash: hash = {:02x?}", hash); - let mut hashnibble = HashNibbles::new(hash); - - println!("get_by_hash: hash = {:02x?}, digest = {:?}", hash, hashnibble); - #[cfg(target_arch="wasm32")] - gloo::console::log!(format!("get_by_hash: hash = {:02x?}, digest = {:?}", hash, hashnibble)); - Ok(self .get_value(&mut hashnibble, store) - .await - .map_err(|err| { - println!("woah!!!! {}", err); - err - })? - .map(|pair| { - println!("woah!!!! here's da pair {:?}", pair); - #[cfg(target_arch="wasm32")] - gloo::console::log!(format!("woah!!!! here's da pair {:?}", pair)); - &pair.value})) + .await? + .map(|pair| &pair.value)) } /// Removes the value at the key matching the provided hash. @@ -394,7 +378,6 @@ where { let bit_index = hashnibbles.try_next()?; - if !self.bitmask[bit_index] { return Ok(None); } @@ -402,24 +385,11 @@ where let value_index = self.get_value_index(bit_index); match &self.pointers[value_index] { Pointer::Values(values) => Ok({ - println!("trying find hashnibble {:?} in mysterious array", hashnibbles); - #[cfg(target_arch="wasm32")] - gloo::console::log!(format!("trying find hashnibble {:?} in mysterious array", hashnibbles)); - for value in values { - println!("key: {:?} value: {:?}", &H::hash(&value.key), value.value); - #[cfg(target_arch="wasm32")] - gloo::console::log!(format!("key: {:?} value: {:?}", &H::hash(&value.key), value.value)); - } - let found = values + values .iter() - .find(|p| &H::hash(&p.key) == hashnibbles.digest); - println!("found a guyyyy: {:?}", found); - #[cfg(target_arch="wasm32")] - gloo::console::log!(format!("found a guyyyy: {:?}", found)); - found + .find(|p| &H::hash(&p.key) == hashnibbles.digest) }), Pointer::Link(link) => { - println!("trying to resolve value for {:?}", link.get_cid()); let child = link.resolve_value(store).await?; child.get_value(hashnibbles, store).await } diff --git a/wnfs-hamt/src/strategies/changes.rs b/wnfs-hamt/src/strategies/changes.rs index ea4a3eea..0a0550d8 100644 --- a/wnfs-hamt/src/strategies/changes.rs +++ b/wnfs-hamt/src/strategies/changes.rs @@ -31,7 +31,7 @@ pub(crate) fn generate_changes( pairs .clone() .into_iter() - .zip(randoms.into_iter()) + .zip(randoms) .filter(|(_, (num, _))| *num != 0) .map(|((k, _), (num, val))| match num { 1 => Change::Add(k, val), diff --git a/wnfs-wasm/src/fs/private/directory.rs b/wnfs-wasm/src/fs/private/directory.rs index 2f6a95ac..d462e154 100644 --- a/wnfs-wasm/src/fs/private/directory.rs +++ b/wnfs-wasm/src/fs/private/directory.rs @@ -125,7 +125,7 @@ impl PrivateDirectory { ratchet_seed, inumber, &mut forest, - &mut store, + &store, &mut rng, ) .await diff --git a/wnfs/src/private/file.rs b/wnfs/src/private/file.rs index cdeec383..ef108b75 100644 --- a/wnfs/src/private/file.rs +++ b/wnfs/src/private/file.rs @@ -363,17 +363,6 @@ impl PrivateFile { } => { let bare_name = &self.header.bare_name; let shard_labels : Vec<_> = Self::generate_shard_labels(key, index, *block_count, bare_name).collect(); - println!("we are in stream_content"); - // println!("shard_labels: {:?}", &shard_labels); - // println!("forest: {:?}", forest); - println!("stream_content: bare_name: {:?}", bare_name.as_bytes()); - #[cfg(target_arch="wasm32")] - { - gloo::console::log!(format!("we are in stream_content")); - // gloo::console::log!(format!("shard_labels: {:?}", &shard_labels)); - // gloo::console::log!(format!("forest: {:?}", forest)); - gloo::console::log!(format!("stream_content: bare_name: {:?}", bare_name.as_bytes())); - }; for label in shard_labels.iter() { let bytes = Self::decrypt_block(key, label, forest, store).await?; yield bytes @@ -601,50 +590,21 @@ impl PrivateFile { forest: &PrivateForest, store: &impl BlockStore, ) -> Result> { - println!("WE ARE IN DECRYPT_BLOCK IN PRIVATE FILE"); - // println!("decrypt_block: label is {:?}", label); - // println!("snapshot key is: {:?}", key); - // println!("forest is: {:?}", forest); - #[cfg(target_arch="wasm32")] - { - gloo::console::log!(format!("WE ARE IN DECRYPT_BLOCK IN PRIVATE FILE")); - // gloo::console::log!(format!("decrypt_block: label is {:?}", label)); - // gloo::console::log!(format!("snapshot key is: {:?}", key)); - // gloo::console::log!(format!("forest is: {:?}", forest)); - }; - - let label_hash = &Sha3_256::hash(&label.as_bytes()); - println!("label_hash: {:?}", label_hash); - #[cfg(target_arch="wasm32")] - { - gloo::console::log!(format!("label_hash: {:?}", label_hash)); - }; - - match forest - .get_encrypted(label_hash, store) - .await { + match forest.get_encrypted(label_hash, store).await { Ok(Some(cids)) => { - println!("got all the cids! {:?}", cids); let cid = cids .iter() .next() .expect("Expected set with at least a Cid"); - + let enc_bytes = store.get_block(cid).await?; let bytes = key.decrypt(&enc_bytes)?; - + Ok(bytes) } - Ok(_) => { - println!("successfully realized no cids"); - panic!(""); - }, - Err(err) => { - println!("file shard not found: {}", err); - Err(FsError::DirectoryAlreadyExists.into()) - }, + _ => Err(FsError::FileShardNotFound.into()), } } @@ -659,24 +619,18 @@ impl PrivateFile { } => { let mut cids = >::new(); let bare_name = &self.header.bare_name; - + for label in Self::generate_shard_labels(key, 0, *block_count, bare_name) { let label_hash = &Sha3_256::hash(&label.as_bytes()); - let block_cids = forest - .get_encrypted(label_hash, store) - .await; + let block_cids = forest.get_encrypted(label_hash, store).await; match block_cids { Ok(Some(new_cids)) => { cids.extend(new_cids); - }, - Ok(None) => { - println!("successfully realized there are no cids"); } - Err(err) => { - println!("wsdfdfdfdfd {}", err); - panic!("dcjf;lkasdjf;sdlkfjsd"); - }, + _ => { + return Err(FsError::FileShardNotFound.into()); + } } } Ok(cids) @@ -692,25 +646,13 @@ impl PrivateFile { block_count: usize, bare_name: &'a Namefilter, ) -> impl Iterator + 'a { - let output = format!("generate_shard_labels:\nkey: {:?}, index: {}, block_count: {}, bare_name: {:?}", key.0.as_bytes(), index, block_count, bare_name.as_bytes()); - println!("{}", output); - - #[cfg(target_arch="wasm32")] - gloo::console::log!(output); - iter::from_fn(move || { if index >= block_count { - println!("returning none, index > block_count"); return None; } - - println!("about to call create block label with index: {:?}", index); let label = Self::create_block_label(key, index, bare_name); index += 1; - println!("in the end of generate shard labels: label: {:?}", label); - #[cfg(target_arch="wasm32")] - gloo::console::log!(format!("in the end of generate shard labels: label: {:?}", label)); Some(label) }) } @@ -721,31 +663,9 @@ impl PrivateFile { let key_hash = Sha3_256::hash(&[key_bytes, &(index as u64).to_le_bytes()[..]].concat()); let mut label = bare_name.clone(); - println!("create_block_label: key_bytes: {:?}", key_bytes); - println!("create_block_label: key_hash: {:?}", key_hash); - println!("create_block_label: index: {:?} index_bytes: {:?}", index, &index.to_le_bytes()); - println!("create_block_label: label: {:?}", label.as_bytes()); - #[cfg(target_arch="wasm32")] - { - gloo::console::log!(format!("create_block_label: key_bytes: {:?}", key_bytes)); - gloo::console::log!(format!("create_block_label: key_hash: {:?}", key_hash)); - gloo::console::log!(format!("create_block_label: index: {:?} index_bytes: {:?}", index, &index.to_le_bytes())); - gloo::console::log!(format!("create_block_label: label: {:?}", label.as_bytes())); - }; - label.add(&key_bytes); - println!("create_block_label: label after adding key_bytes: {:?}", label.as_bytes()); - #[cfg(target_arch="wasm32")] - gloo::console::log!(format!("create_block_label: label after adding key_bytes: {:?}", label.as_bytes())); label.add(&key_hash); - println!("create_block_label: label after adding key_hash: {:?}", label.as_bytes()); - #[cfg(target_arch="wasm32")] - gloo::console::log!(format!("create_block_label: label after adding key_hash: {:?}", label.as_bytes())); label.saturate(); - println!("create_block_label: label after saturate: {:?}", label.as_bytes()); - #[cfg(target_arch="wasm32")] - gloo::console::log!(format!("create_block_label: label after saturate: {:?}", label.as_bytes())); - label } @@ -1150,7 +1070,7 @@ mod proptests { Utc::now(), &mut Cursor::new(vec![5u8; length]), forest, - &mut MemoryBlockStore::default(), + &MemoryBlockStore::default(), rng, ) .await From 5e86c260a63602d938505f8d167405bc2ff52cc9 Mon Sep 17 00:00:00 2001 From: Vera Gonzalez Date: Wed, 25 Oct 2023 13:55:52 -0400 Subject: [PATCH 4/9] fix: removed debugs --- wnfs-hamt/src/merge.rs | 6 +++--- wnfs-hamt/src/node.rs | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/wnfs-hamt/src/merge.rs b/wnfs-hamt/src/merge.rs index b22f7005..27326237 100644 --- a/wnfs-hamt/src/merge.rs +++ b/wnfs-hamt/src/merge.rs @@ -2,7 +2,7 @@ use super::{ChangeType, Node}; use crate::{error::HamtError, Hasher}; use anyhow::Result; use serde::de::DeserializeOwned; -use std::{fmt::Debug, hash::Hash, rc::Rc}; +use std::{hash::Hash, rc::Rc}; use wnfs_common::{BlockStore, Link}; //-------------------------------------------------------------------------------------------------- @@ -18,8 +18,8 @@ pub async fn merge( ) -> Result>> where F: Fn(&V, &V) -> Result, - K: DeserializeOwned + Eq + Clone + Hash + AsRef<[u8]> + Debug, - V: DeserializeOwned + Eq + Clone + Debug, + K: DeserializeOwned + Eq + Clone + Hash + AsRef<[u8]>, + V: DeserializeOwned + Eq + Clone, H: Hasher + Clone + 'static, { let kv_changes = super::diff(main_link.clone(), other_link.clone(), store).await?; diff --git a/wnfs-hamt/src/node.rs b/wnfs-hamt/src/node.rs index 5a9ed0d3..96a38089 100644 --- a/wnfs-hamt/src/node.rs +++ b/wnfs-hamt/src/node.rs @@ -120,8 +120,8 @@ where /// ``` pub async fn get<'a>(&'a self, key: &K, store: &impl BlockStore) -> Result> where - K: DeserializeOwned + AsRef<[u8]> + Debug, - V: DeserializeOwned + Debug, + K: DeserializeOwned + AsRef<[u8]>, + V: DeserializeOwned, { let hash = &H::hash(key); @@ -200,8 +200,8 @@ where store: &impl BlockStore, ) -> Result> where - K: DeserializeOwned + AsRef<[u8]> + Debug, - V: DeserializeOwned + Debug, + K: DeserializeOwned + AsRef<[u8]>, + V: DeserializeOwned, { let mut hashnibble = HashNibbles::new(hash); Ok(self @@ -373,8 +373,8 @@ where store: &impl BlockStore, ) -> Result>> where - K: DeserializeOwned + AsRef<[u8]> + Debug, - V: DeserializeOwned + Debug, + K: DeserializeOwned + AsRef<[u8]>, + V: DeserializeOwned, { let bit_index = hashnibbles.try_next()?; From 2385c5c2e3b5a63637f1d0404340ddf25d9d77bf Mon Sep 17 00:00:00 2001 From: Vera Gonzalez Date: Wed, 25 Oct 2023 14:00:32 -0400 Subject: [PATCH 5/9] fix: reverted silly changes --- wnfs/src/private/file.rs | 49 ++++++++++++++++------------------------ 1 file changed, 20 insertions(+), 29 deletions(-) diff --git a/wnfs/src/private/file.rs b/wnfs/src/private/file.rs index ef108b75..1415d083 100644 --- a/wnfs/src/private/file.rs +++ b/wnfs/src/private/file.rs @@ -351,7 +351,7 @@ impl PrivateFile { match &self.content.content { FileContent::Inline { data } => { if index != 0 { - Err(FsError::DirectoryAlreadyExists)? + Err(FsError::FileShardNotFound)? } yield data.clone() @@ -362,9 +362,8 @@ impl PrivateFile { .. } => { let bare_name = &self.header.bare_name; - let shard_labels : Vec<_> = Self::generate_shard_labels(key, index, *block_count, bare_name).collect(); - for label in shard_labels.iter() { - let bytes = Self::decrypt_block(key, label, forest, store).await?; + for label in Self::generate_shard_labels(key, index, *block_count, bare_name) { + let bytes = Self::decrypt_block(key, &label, forest, store).await?; yield bytes } } @@ -591,21 +590,18 @@ impl PrivateFile { store: &impl BlockStore, ) -> Result> { let label_hash = &Sha3_256::hash(&label.as_bytes()); - - match forest.get_encrypted(label_hash, store).await { - Ok(Some(cids)) => { - let cid = cids - .iter() - .next() - .expect("Expected set with at least a Cid"); - - let enc_bytes = store.get_block(cid).await?; - let bytes = key.decrypt(&enc_bytes)?; - - Ok(bytes) - } - _ => Err(FsError::FileShardNotFound.into()), - } + let cids = forest + .get_encrypted(label_hash, store) + .await? + .ok_or(FsError::FileShardNotFound)?; + let cid = cids + .iter() + .next() + .expect("Expected set with at least a Cid"); + + let enc_bytes = store.get_block(cid).await?; + let bytes = key.decrypt(&enc_bytes)?; + Ok(bytes) } pub async fn get_cids<'a>( @@ -622,16 +618,11 @@ impl PrivateFile { for label in Self::generate_shard_labels(key, 0, *block_count, bare_name) { let label_hash = &Sha3_256::hash(&label.as_bytes()); - let block_cids = forest.get_encrypted(label_hash, store).await; - - match block_cids { - Ok(Some(new_cids)) => { - cids.extend(new_cids); - } - _ => { - return Err(FsError::FileShardNotFound.into()); - } - } + let block_cids = forest + .get_encrypted(label_hash, store) + .await? + .ok_or(FsError::FileShardNotFound)?; + cids.extend(block_cids); } Ok(cids) } From fa33a8fdadc7437380fb2cc52c0f95fec15bd60c Mon Sep 17 00:00:00 2001 From: Vera Gonzalez Date: Wed, 25 Oct 2023 14:03:02 -0400 Subject: [PATCH 6/9] fix: more cleanup --- wnfs-hamt/Cargo.toml | 1 - wnfs-hamt/src/node.rs | 3 +-- wnfs/Cargo.toml | 1 - 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/wnfs-hamt/Cargo.toml b/wnfs-hamt/Cargo.toml index 8cda7876..34eb7b92 100644 --- a/wnfs-hamt/Cargo.toml +++ b/wnfs-hamt/Cargo.toml @@ -25,7 +25,6 @@ bitvec = { version = "1.0", features = ["serde"] } chrono = { version = "0.4.23", default-features = false, features = ["clock", "std"] } either = "1.8" futures = "0.3" -gloo = "0.10.0" libipld = { version = "0.16", features = ["dag-cbor", "derive", "serde-codec"] } log = { version = "0.4", optional = true } multihash = "0.18" diff --git a/wnfs-hamt/src/node.rs b/wnfs-hamt/src/node.rs index 96a38089..a3cfaa1d 100644 --- a/wnfs-hamt/src/node.rs +++ b/wnfs-hamt/src/node.rs @@ -203,9 +203,8 @@ where K: DeserializeOwned + AsRef<[u8]>, V: DeserializeOwned, { - let mut hashnibble = HashNibbles::new(hash); Ok(self - .get_value(&mut hashnibble, store) + .get_value(&mut HashNibbles::new(hash), store) .await? .map(|pair| &pair.value)) } diff --git a/wnfs/Cargo.toml b/wnfs/Cargo.toml index 06ab0b0f..bb9626b0 100644 --- a/wnfs/Cargo.toml +++ b/wnfs/Cargo.toml @@ -26,7 +26,6 @@ async-stream = "0.3" async-trait = "0.1" chrono = { version = "0.4", default-features = false, features = ["clock", "std"] } futures = "0.3" -gloo = "0.10.0" libipld = { version = "0.16" } # TODO(appcypher): Change this to libipld_core once BlockStore codec has been changed to u64 value or enum multihash = "0.19" once_cell = "1.16" From 0fd84bfd306cd4cd6c2834cce3ae25ebeeb7b7f7 Mon Sep 17 00:00:00 2001 From: Vera Gonzalez Date: Wed, 25 Oct 2023 14:07:00 -0400 Subject: [PATCH 7/9] feat: final version --- wnfs-hamt/src/node.rs | 4 ++++ wnfs/src/private/file.rs | 3 +++ 2 files changed, 7 insertions(+) diff --git a/wnfs-hamt/src/node.rs b/wnfs-hamt/src/node.rs index a3cfaa1d..f21c57f7 100644 --- a/wnfs-hamt/src/node.rs +++ b/wnfs-hamt/src/node.rs @@ -203,6 +203,9 @@ where K: DeserializeOwned + AsRef<[u8]>, V: DeserializeOwned, { + #[cfg(feature = "log")] + debug!("get: hash = {:02x?}", hash); + Ok(self .get_value(&mut HashNibbles::new(hash), store) .await? @@ -377,6 +380,7 @@ where { let bit_index = hashnibbles.try_next()?; + // If the bit is not set yet, return None. if !self.bitmask[bit_index] { return Ok(None); } diff --git a/wnfs/src/private/file.rs b/wnfs/src/private/file.rs index 1415d083..d1bb7a94 100644 --- a/wnfs/src/private/file.rs +++ b/wnfs/src/private/file.rs @@ -590,10 +590,12 @@ impl PrivateFile { store: &impl BlockStore, ) -> Result> { let label_hash = &Sha3_256::hash(&label.as_bytes()); + let cids = forest .get_encrypted(label_hash, store) .await? .ok_or(FsError::FileShardNotFound)?; + let cid = cids .iter() .next() @@ -601,6 +603,7 @@ impl PrivateFile { let enc_bytes = store.get_block(cid).await?; let bytes = key.decrypt(&enc_bytes)?; + Ok(bytes) } From 124341195c90e7e5adbefd899886e9c263670a20 Mon Sep 17 00:00:00 2001 From: Vera Gonzalez Date: Wed, 25 Oct 2023 14:39:22 -0400 Subject: [PATCH 8/9] fix: wasm test break undo --- wnfs-wasm/src/fs/private/directory.rs | 2 +- wnfs/src/private/file.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/wnfs-wasm/src/fs/private/directory.rs b/wnfs-wasm/src/fs/private/directory.rs index d462e154..2f6a95ac 100644 --- a/wnfs-wasm/src/fs/private/directory.rs +++ b/wnfs-wasm/src/fs/private/directory.rs @@ -125,7 +125,7 @@ impl PrivateDirectory { ratchet_seed, inumber, &mut forest, - &store, + &mut store, &mut rng, ) .await diff --git a/wnfs/src/private/file.rs b/wnfs/src/private/file.rs index d1bb7a94..66bc2c02 100644 --- a/wnfs/src/private/file.rs +++ b/wnfs/src/private/file.rs @@ -1064,7 +1064,7 @@ mod proptests { Utc::now(), &mut Cursor::new(vec![5u8; length]), forest, - &MemoryBlockStore::default(), + &mut MemoryBlockStore::default(), rng, ) .await From 8bf79f52b3d2a4ad6e8e88539c394543a90c9ab7 Mon Sep 17 00:00:00 2001 From: Vera Gonzalez Date: Wed, 25 Oct 2023 14:48:29 -0400 Subject: [PATCH 9/9] Update node.rs Signed-off-by: Vera Gonzalez --- wnfs-hamt/src/node.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wnfs-hamt/src/node.rs b/wnfs-hamt/src/node.rs index f21c57f7..07ae8b7d 100644 --- a/wnfs-hamt/src/node.rs +++ b/wnfs-hamt/src/node.rs @@ -204,7 +204,7 @@ where V: DeserializeOwned, { #[cfg(feature = "log")] - debug!("get: hash = {:02x?}", hash); + debug!("get_by_hash: hash = {:02x?}", hash); Ok(self .get_value(&mut HashNibbles::new(hash), store)